| 1 |
# vim:fileencoding=utf-8 |
| 2 |
# License: GPL v3 Copyright: 2016, Kovid Goyal <kovid at kovidgoyal.net> |
| 3 |
from __python__ import bound_methods, hash_literals |
| 4 |
|
| 5 |
from elementmaker import E |
| 6 |
from gettext import gettext as _ |
| 7 |
|
| 8 |
from book_list.globals import get_session_data |
| 9 |
from dom import svgicon |
| 10 |
from dom import unique_id |
| 11 |
from read_book.gestures import ( |
| 12 |
allowed_actions_for_flow_mode_drag, allowed_actions_for_flow_mode_flick, |
| 13 |
allowed_actions_for_paged_mode_swipe, allowed_actions_for_tap, |
| 14 |
allowed_actions_for_two_fingers, current_action_for_gesture_type, |
| 15 |
get_action_descriptions |
| 16 |
) |
| 17 |
from read_book.prefs.utils import create_button_box |
| 18 |
from read_book.touch import GESTURE, GESTURE_NAMES |
| 19 |
|
| 20 |
CONTAINER = unique_id('touch-settings') |
| 21 |
|
| 22 |
|
| 23 |
def restore_defaults(): |
| 24 |
apply_settings_to_ui({}) |
| 25 |
|
| 26 |
|
| 27 |
def get_container(): |
| 28 |
return document.getElementById(CONTAINER) |
| 29 |
|
| 30 |
|
| 31 |
def apply_settings_to_ui(overrides): |
| 32 |
overrides = overrides or {} |
| 33 |
for group in get_container().querySelectorAll('[data-group]'): |
| 34 |
group_name = group.dataset.group |
| 35 |
in_flow_mode = group_name.indexOf('flow') >= 0 |
| 36 |
for select in group.querySelectorAll('select'): |
| 37 |
allowed_actions = [] |
| 38 |
for option in select.querySelectorAll('option'): |
| 39 |
allowed_actions.push(option.value) |
| 40 |
gesture_type = select.name |
| 41 |
current_action = current_action_for_gesture_type(overrides, gesture_type, in_flow_mode) |
| 42 |
if allowed_actions.indexOf(current_action) < 0: |
| 43 |
current_action = current_action_for_gesture_type({}, gesture_type, in_flow_mode) |
| 44 |
if not current_action or allowed_actions.indexOf(current_action) < 0: |
| 45 |
current_action = 'none' |
| 46 |
select.value = current_action |
| 47 |
select.dispatchEvent(new Event('change', {'view': window, 'bubbles': True})) |
| 48 |
|
| 49 |
|
| 50 |
def get_overrides_from_ui(): |
| 51 |
ans = {} |
| 52 |
for group in get_container().querySelectorAll('[data-group]'): |
| 53 |
group_name = group.dataset.group |
| 54 |
in_flow_mode = group_name.indexOf('flow') >= 0 |
| 55 |
if group_name is 'paged_swipe': |
| 56 |
attr = 'paged_mode' |
| 57 |
elif group_name is 'flow_swipe': |
| 58 |
attr = 'flow_mode' |
| 59 |
else: |
| 60 |
attr = 'common' |
| 61 |
if not ans[attr]: |
| 62 |
ans[attr] = {} |
| 63 |
for select in group.querySelectorAll('select'): |
| 64 |
val = select.value |
| 65 |
defval = current_action_for_gesture_type({}, select.name, in_flow_mode) |
| 66 |
if val is not defval: |
| 67 |
ans[attr][select.name] = val |
| 68 |
for which in Object.keys(ans): |
| 69 |
if Object.keys(ans[which]).length is 0: |
| 70 |
v'delete ans[which]' |
| 71 |
return ans |
| 72 |
|
| 73 |
|
| 74 |
def create_touch_panel(container, apply_func, cancel_func): |
| 75 |
container.appendChild(E.div(id=CONTAINER, style='margin: 1rem')) |
| 76 |
container = container.lastChild |
| 77 |
sd = get_session_data() |
| 78 |
overrides = sd.get('gesture_overrides') |
| 79 |
action_descriptions = get_action_descriptions() |
| 80 |
in_flow_mode = False |
| 81 |
|
| 82 |
def on_select_change(ev): |
| 83 |
select = ev.target |
| 84 |
ad = action_descriptions[select.value] |
| 85 |
span = select.parentNode.parentNode.querySelector('.help-text') |
| 86 |
span.textContent = ad.long |
| 87 |
|
| 88 |
def make_setting(gesture_type, allowed_actions): |
| 89 |
ans = E.div(style='margin-top: 1ex; display: flex; flex-direction: column') |
| 90 |
title = GESTURE_NAMES()[gesture_type] |
| 91 |
sid = unique_id(gesture_type) |
| 92 |
ans.appendChild(E.h4(E.label(title, 'for'=sid))) |
| 93 |
select = E.select(name=gesture_type, id=sid) |
| 94 |
for action in allowed_actions: |
| 95 |
ad = action_descriptions[action] |
| 96 |
select.appendChild(E.option(ad.short, value=action)) |
| 97 |
select.addEventListener('change', on_select_change) |
| 98 |
c = E.div(select, E.span(svgicon('angle-down')), data_component='select') |
| 99 |
ans.appendChild(E.div( |
| 100 |
c, E.span(class_='help-text', style='font-size: smaller; font-style: italic'), |
| 101 |
style='display: flex; flex-wrap: wrap; gap: 1ex; align-items: center' |
| 102 |
)) |
| 103 |
on_select_change({'target': select}) |
| 104 |
return ans |
| 105 |
|
| 106 |
container.appendChild(E.h2(_('Tapping'))) |
| 107 |
container.appendChild(E.div(_( |
| 108 |
'There are three tap zones, depending on where on the screen you tap. When the tap is' |
| 109 |
' on a link, the link is followed, otherwise a configurable action based on the zone is performed.'))) |
| 110 |
c = E.div('data-group'='tap') |
| 111 |
container.appendChild(c) |
| 112 |
aat = allowed_actions_for_tap() |
| 113 |
c.appendChild(make_setting(GESTURE.control_zone_tap, aat)) |
| 114 |
c.appendChild(make_setting(GESTURE.forward_zone_tap, aat)) |
| 115 |
c.appendChild(make_setting(GESTURE.back_zone_tap, aat)) |
| 116 |
c.appendChild(make_setting(GESTURE.long_tap, aat)) |
| 117 |
container.appendChild(E.hr(style='margin-top:1ex; margin-bottom: 1ex')) |
| 118 |
|
| 119 |
container.appendChild(E.h2(_('Two finger gestures'))) |
| 120 |
c = E.div('data-group'='two_finger') |
| 121 |
container.appendChild(c) |
| 122 |
aat = allowed_actions_for_two_fingers() |
| 123 |
c.appendChild(make_setting(GESTURE.two_finger_tap, aat)) |
| 124 |
c.appendChild(make_setting(GESTURE.pinch_in, aat)) |
| 125 |
c.appendChild(make_setting(GESTURE.pinch_out, aat)) |
| 126 |
container.appendChild(E.hr(style='margin-top:1ex; margin-bottom: 1ex')) |
| 127 |
|
| 128 |
container.appendChild(E.h2(_('Swiping'))) |
| 129 |
container.appendChild(E.div(_( |
| 130 |
'Swiping works differently in paged and flow mode, with different actions. For an English like language, swiping in' |
| 131 |
' the writing direction means swiping horizontally. For languages written vertically, it means swiping vertically.' |
| 132 |
' For languages written left-to-right "going forward" means swiping right-to-left, like turning a page with your finger.' |
| 133 |
))) |
| 134 |
container.appendChild(E.h3(_('Swiping in paged mode'), style='padding-top: 1ex')) |
| 135 |
c = E.div('data-group'='paged_swipe') |
| 136 |
container.appendChild(c) |
| 137 |
aap = allowed_actions_for_paged_mode_swipe() |
| 138 |
c.appendChild(make_setting(GESTURE.flick_block_forward, aap)) |
| 139 |
c.appendChild(make_setting(GESTURE.flick_block_backward, aap)) |
| 140 |
c.appendChild(make_setting(GESTURE.flick_inline_forward, aap)) |
| 141 |
c.appendChild(make_setting(GESTURE.flick_inline_backward, aap)) |
| 142 |
c.appendChild(make_setting(GESTURE.swipe_inline_forward_hold, aap)) |
| 143 |
c.appendChild(make_setting(GESTURE.swipe_inline_backward_hold, aap)) |
| 144 |
c.appendChild(make_setting(GESTURE.swipe_block_forward_hold, aap)) |
| 145 |
c.appendChild(make_setting(GESTURE.swipe_block_backward_hold, aap)) |
| 146 |
container.appendChild(E.hr(style='margin-top:1ex; margin-bottom: 1ex')) |
| 147 |
container.appendChild(E.h3(_('Swiping in flow mode'), style='padding-top: 1ex')) |
| 148 |
c = E.div('data-group'='flow_swipe') |
| 149 |
container.appendChild(c) |
| 150 |
in_flow_mode = True |
| 151 |
in_flow_mode |
| 152 |
aaf = allowed_actions_for_flow_mode_flick() |
| 153 |
c.appendChild(make_setting(GESTURE.flick_block_forward, aaf)) |
| 154 |
c.appendChild(make_setting(GESTURE.flick_block_backward, aaf)) |
| 155 |
c.appendChild(make_setting(GESTURE.flick_inline_forward, aaf)) |
| 156 |
c.appendChild(make_setting(GESTURE.flick_inline_backward, aaf)) |
| 157 |
aaf = allowed_actions_for_flow_mode_drag() |
| 158 |
c.appendChild(make_setting(GESTURE.swipe_inline_backward_in_progress, aaf)) |
| 159 |
c.appendChild(make_setting(GESTURE.swipe_inline_forward_in_progress, aaf)) |
| 160 |
c.appendChild(make_setting(GESTURE.swipe_block_backward_in_progress, aaf)) |
| 161 |
c.appendChild(make_setting(GESTURE.swipe_block_forward_in_progress, aaf)) |
| 162 |
|
| 163 |
container.appendChild(E.hr(style='margin-top:1ex; margin-bottom: 1ex')) |
| 164 |
container.appendChild(create_button_box(restore_defaults, apply_func, cancel_func)) |
| 165 |
apply_settings_to_ui(overrides) |
| 166 |
|
| 167 |
|
| 168 |
develop = create_touch_panel |
| 169 |
|
| 170 |
|
| 171 |
def commit_touch(onchange): |
| 172 |
sd = get_session_data() |
| 173 |
current_overrides = sd.get('gesture_overrides') |
| 174 |
overrides = get_overrides_from_ui() |
| 175 |
changed = overrides != current_overrides |
| 176 |
if changed: |
| 177 |
sd.set('gesture_overrides', overrides) |
| 178 |
onchange() |