Debian Codegraph @

src/pyj/read_book/prefs/scrolling.pyj

raw
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 unique_id
10 from read_book.prefs.utils import create_button_box
11 from session import session_defaults
12
13 CONTAINER = unique_id('standalone-scrolling-settings')
14
15 # Scroll speeds in lines/sec
16 MIN_SCROLL_SPEED_AUTO = 0.05
17 MAX_SCROLL_SPEED_AUTO = 5
18
19 MIN_SCROLL_AUTO_DELAY = -1
20 MAX_SCROLL_AUTO_DELAY = 50
21
22 MIN_SCROLL_SPEED_SMOOTH = 5
23 MAX_SCROLL_SPEED_SMOOTH = 80
24
25 MIN_SCROLL_MOUSE_DELAY = 0
26 MAX_SCROLL_MOUSE_DELAY = 5
27
28
29 def restore_defaults():
30 container = get_container()
31 for control in container.querySelectorAll('input[name]'):
32 val = session_defaults()[control.getAttribute('name')]
33 if control.type is 'checkbox':
34 control.checked = val
35 else:
36 control.valueAsNumber = val
37
38
39 def get_container():
40 return document.getElementById(CONTAINER)
41
42
43 def change_scroll_speed(amt):
44 sd = get_session_data()
45 lps = sd.get('lines_per_sec_auto')
46 nlps = max(MIN_SCROLL_SPEED_AUTO, min(lps + amt, MAX_SCROLL_SPEED_AUTO))
47 if nlps != lps:
48 sd.set('lines_per_sec_auto', nlps)
49 return nlps
50
51
52 def create_scrolling_panel(container, apply_func, cancel_func):
53 container.appendChild(E.div(id=CONTAINER, style='margin: 1rem'))
54 container = container.lastChild
55 sd = get_session_data()
56
57 def cb(name, text, title):
58 ans = E.input(type='checkbox', name=name, style='margin-left: 0')
59 if sd.get(name):
60 ans.checked = True
61 return E.div(style='margin-top:1ex', E.label(ans, text, title=title or ''))
62
63 def spinner(name, text, **kwargs):
64 ans = E.input(type='number', name=name, id=name)
65 for key, val in Object.entries(kwargs):
66 ans[key] = val
67 ans.valueAsNumber = sd.get(name, session_defaults()[name])
68 return E.label('for'=name, text), ans
69
70 container.appendChild(E.div(style='margin-top:1ex', _('Control how scrolling works in paged mode')))
71 container.appendChild(E.div(style='margin-left: 1rem'))
72 container.lastChild.appendChild(cb(
73 'paged_wheel_scrolls_by_screen', _('Mouse wheel scrolls by screen fulls instead of pages')))
74 container.lastChild.appendChild(cb(
75 'paged_wheel_section_jumps', _('Horizontal mouse wheel jumps to next/previous section')))
76 container.lastChild.appendChild(cb(
77 'paged_margin_clicks_scroll_by_screen', _('Clicking on the margins scrolls by screen fulls instead of pages')))
78 container.lastChild.appendChild(
79 E.div(style='display:grid;margin-top:1ex;align-items:center;grid-template-columns:auto min-content;grid-gap:1ex; max-width: 30em',
80 *spinner(
81 'paged_pixel_scroll_threshold',
82 '\xa0' + _('Pixel scroll threshold:'),
83 title=_('When using a touchpad or mouse wheel that produces scroll events in pixels, set the number of pixels before a page turn is triggered'),
84 step=5,
85 min=0,
86 max=10000
87 ),
88 ))
89
90 container.appendChild(E.hr(style='margin-top:1ex; margin-bottom: 1ex'))
91 container.appendChild(E.div(style='margin-top:1ex', _('Control how smooth scrolling works in flow mode')))
92 container.appendChild(E.div(style='margin-left: 1rem'))
93 container.lastChild.appendChild(cb(
94 'scroll_stop_boundaries',
95 _('Stop at internal file boundaries when smooth scrolling by holding down the scroll key')
96 ))
97 container.lastChild.appendChild(
98 E.div(style='display:grid;margin-top:1ex;align-items:center;grid-template-columns:auto min-content;grid-gap:1ex; max-width: 30em',
99 *spinner(
100 'lines_per_sec_smooth',
101 _('Smooth scrolling speed in lines/sec:'),
102 step=5,
103 min=MIN_SCROLL_SPEED_SMOOTH,
104 max=MAX_SCROLL_SPEED_SMOOTH
105 ),
106 *spinner(
107 'lines_per_sec_auto',
108 _('Auto scrolling speed in lines/sec:'),
109 step=0.05,
110 min=MIN_SCROLL_SPEED_AUTO,
111 max=MAX_SCROLL_SPEED_AUTO
112 ),
113 *spinner(
114 'scroll_auto_boundary_delay',
115 _('Seconds to pause before auto-scrolling past internal file boundaries:'),
116 title=_('Use negative values to not auto-scroll past internal file boundaries'),
117 step=0.25,
118 min=MIN_SCROLL_AUTO_DELAY,
119 max=MAX_SCROLL_AUTO_DELAY
120 ),
121 *spinner(
122 'scroll_mouse_boundary_delay',
123 _('Seconds to pause when mouse-scrolling past internal file boundaries:'),
124 title=_('Set to zero to scroll past internal file boundaries without delay'),
125 step=0.25,
126 min=MIN_SCROLL_MOUSE_DELAY,
127 max=MAX_SCROLL_MOUSE_DELAY
128 )
129 )
130 )
131
132 container.appendChild(E.hr(style='margin-top:1ex; margin-bottom: 1ex'))
133 container.appendChild(E.div(style='margin-top:1ex', _('Miscellaneous')))
134 container.appendChild(E.div(style='margin-left: 1rem'))
135 container.lastChild.appendChild(cb('book_scrollbar', _('Show a scrollbar')))
136 container.lastChild.appendChild(cb(
137 'reverse_page_turn_zones', _('Invert the page turn tap areas'),
138 _('Have tapping on the left side turn the page forward and the right side backwards')
139 ))
140
141 container.appendChild(create_button_box(restore_defaults, apply_func, cancel_func))
142
143
144 develop = create_scrolling_panel
145
146
147 def commit_scrolling(onchange):
148 sd = get_session_data()
149 container = get_container()
150 changed = False
151 for control in container.querySelectorAll('input[name]'):
152 name = control.getAttribute('name')
153 val = control.checked if control.type is 'checkbox' else control.valueAsNumber
154 if val is not sd.get(name) and control.validity.valid:
155 sd.set(name, val)
156 changed = True
157 if changed:
158 onchange()