Debian Codegraph @

src/pyj/read_book/shortcuts.pyj

raw
1 # vim:fileencoding=utf-8
2 # License: GPL v3 Copyright: 2019, Kovid Goyal <kovid at kovidgoyal.net>
3 from __python__ import bound_methods, hash_literals
4
5 from gettext import gettext as _
6
7 from read_book.globals import runtime
8
9
10 def parse_key_repr(sc):
11 parts = sc.split('+')
12 if sc.endsWith('++'):
13 parts = parts[:-2]
14 parts.push('+')
15 key = parts[-1]
16 ans = {'key': key, 'altKey': False, 'ctrlKey': False, 'metaKey': False, 'shiftKey': False}
17 for modifier in parts[:-1]:
18 q = modifier.toLowerCase()
19 if q is 'ctrl':
20 ans.ctrlKey = True
21 elif q is 'alt':
22 ans.altKey = True
23 elif q is 'meta' or q is 'cmd':
24 ans.metaKey = True
25 elif q is 'shift':
26 ans.shiftKey = True
27 return ans
28
29
30 def desc(sc, group, short, long):
31 if jstype(sc) is 'string':
32 sc = [sc]
33 pkey = []
34 for x in sc:
35 pkey.push(parse_key_repr(x))
36 return {'group': group, 'short': short, 'long': long, 'shortcuts': pkey}
37
38
39 def keyevent_as_shortcut(evt):
40 key = evt.key
41 if capital_letters[key] and evt.shiftKey:
42 key = key.toLowerCase()
43 return {
44 'key': key, 'altKey': evt.altKey, 'ctrlKey': evt.ctrlKey,
45 'metaKey': evt.metaKey, 'shiftKey': evt.shiftKey
46 }
47
48
49 def shortcut_differs(a, b):
50 return not (a.key is b.key and a.altKey is b.altKey and a.ctrlKey is b.ctrlKey and a.metaKey is b.metaKey and a.shiftKey is b.shiftKey)
51
52
53 capital_letters = {x: True for x in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'}
54
55
56 def get_key_text(evt):
57 key = evt.key
58 if key:
59 # on Windows pressing ctrl+alt+ascii char gives capital letters. On all
60 # platforms shift+key gives capital letters we define shortcuts using
61 # lowercase letters, so lowercase them here.
62 if evt.code and key.toLowerCase() is not key and evt.code.startsWith('Key') and capital_letters[key]:
63 key = key.toLowerCase()
64 return key
65
66
67 def keyevent_to_index(evt):
68 parts = []
69 for mod in ['altKey', 'ctrlKey', 'metaKey', 'shiftKey']:
70 parts.push('y' if evt[mod] else 'n')
71 return parts.join('') + get_key_text(evt)
72
73
74 def key_as_text(evt):
75 mods = []
76 for x in ('alt', 'ctrl', 'meta', 'shift'):
77 if evt[x + 'Key']:
78 if 'macos' in window.navigator.userAgent:
79 if x is 'alt':
80 x = 'option'
81 elif x is 'meta':
82 x = '⌘'
83 mods.push(x.capitalize())
84 mods = '+'.join(mods)
85 if mods:
86 mods += '+'
87 key = get_key_text(evt)
88 if key is ' ':
89 key = 'Space'
90 return mods + key
91
92
93 def common_shortcuts(): # {{{
94 return {
95 'start_of_file': desc(
96 ['Ctrl+ArrowUp', 'Ctrl+ArrowLeft', 'Home'],
97 'scroll',
98 _('Scroll to the beginning of the current file'),
99 _('When the e-book is made of multiple individual files, scroll to the start of the current file'),
100 ),
101 'start_of_book': desc(
102 'Ctrl+Home',
103 'scroll',
104 _('Scroll to the beginning of the book'),
105 ),
106 'end_of_book': desc(
107 'Ctrl+End',
108 'scroll',
109 _('Scroll to the end of the book'),
110 ),
111 'end_of_file': desc(
112 ['Ctrl+ArrowDown', 'Ctrl+ArrowRight', 'End'],
113 'scroll',
114 _('Scroll to the end of the current file'),
115 _('When the e-book is made of multiple individual files, scroll to the end of the current file'),
116 ),
117 'up': desc(
118 'ArrowUp',
119 'scroll',
120 _('Scroll backwards smoothly (by screen-fulls in paged mode)'),
121 _('Scroll backwards, smoothly in flow mode and by screen fulls in paged mode'),
122 ),
123 'down': desc(
124 'ArrowDown',
125 'scroll',
126 _('Scroll forwards smoothly (by screen-fulls in paged mode)'),
127 _('Scroll forwards, smoothly in flow mode and by screen fulls in paged mode'),
128 ),
129 'left': desc(
130 'ArrowLeft',
131 'scroll',
132 _('Scroll left'),
133 _('Scroll leftwards by a little in flow mode and by a page in paged mode'),
134 ),
135 'right': desc(
136 'ArrowRight',
137 'scroll',
138 _('Scroll right'),
139 _('Scroll rightwards by a little in flow mode and by a page in paged mode'),
140 ),
141 'pageup': desc(
142 ['PageUp', 'Shift+ '],
143 'scroll',
144 _('Scroll backwards by screen-fulls'),
145 ),
146 'pagedown': desc(
147 [' ', 'PageDown'],
148 'scroll',
149 _('Scroll forwards by screen-fulls'),
150 ),
151 'previous_section': desc(
152 'Ctrl+PageUp',
153 'scroll',
154 _('Scroll to the previous section')
155 ),
156 'next_section': desc(
157 'Ctrl+PageDown',
158 'scroll',
159 _('Scroll to the next section')
160 ),
161 'back': desc(
162 ['Alt+ArrowLeft'],
163 'scroll',
164 _('Back'),
165 ),
166 'forward': desc(
167 ['Alt+ArrowRight'],
168 'scroll',
169 _('Forward'),
170 ),
171 'toggle_toc': desc(
172 'Ctrl+t',
173 'ui',
174 _('Show/hide Table of Contents'),
175 ),
176 'read_aloud': desc(
177 'Ctrl+s',
178 'ui',
179 _('Read aloud')
180 ),
181 'toggle_hints': desc(
182 'Alt+f',
183 'ui',
184 _('Follow links with the keyboard')
185 ),
186 'copy_to_clipboard': desc(
187 ['Ctrl+c', 'Meta+c'],
188 'ui',
189 _('Copy to clipboard'),
190 ),
191 'copy_location_to_clipboard': desc(
192 ['Alt+c'],
193 'ui',
194 _('Copy current location to clipboard'),
195 ),
196 'copy_location_as_url_to_clipboard': desc(
197 ['Ctrl+Shift+c'],
198 'ui',
199 _('Copy current location as calibre:// URL to clipboard'),
200 ),
201 'start_search': desc(
202 ['/', 'Ctrl+f', 'Cmd+f'],
203 'ui',
204 _('Start search'),
205 ),
206 'next_match': desc(
207 ['F3', 'Enter'],
208 'ui',
209 _('Find next'),
210 ),
211 'previous_match': desc(
212 ['Shift+F3', 'Shift+Enter'],
213 'ui',
214 _('Find previous'),
215 ),
216 'increase_font_size': desc(
217 ['Ctrl+=', 'Ctrl++', 'Ctrl+Shift++', 'Ctrl+Shift+=', 'Meta++', 'Meta+Shift++', 'Meta+Shift+='],
218 'ui',
219 _('Increase font size'),
220 ),
221 'decrease_font_size': desc(
222 ['Ctrl+-', 'Ctrl+_', 'Ctrl+Shift+-', 'Ctrl+Shift+_', 'Meta+-', 'Meta+_'],
223 'ui',
224 _('Decrease font size'),
225 ),
226 'default_font_size': desc(
227 'Ctrl+0',
228 'ui',
229 _('Restore default font size'),
230 ),
231 'increase_number_of_columns': desc(
232 ['Ctrl+]'],
233 'ui',
234 _('Increase number of pages per screen'),
235 ),
236 'decrease_number_of_columns': desc(
237 ['Ctrl+['],
238 'ui',
239 _('Decrease number of pages per screen'),
240 ),
241 'reset_number_of_columns': desc(
242 ['Ctrl+Alt+c'],
243 'ui',
244 _('Make number of pages per screen automatic'),
245 ),
246 'toggle_full_screen': desc(
247 ['F11', 'Ctrl+Shift+f'],
248 'ui',
249 _('Toggle full screen'),
250 ),
251 'toggle_paged_mode': desc(
252 'Ctrl+m',
253 'ui',
254 _('Toggle between Paged mode and Flow mode for text layout')
255 ),
256 'toggle_scrollbar': desc(
257 'Ctrl+w',
258 'ui',
259 _('Toggle the scrollbar')
260 ),
261 'toggle_reference_mode': desc(
262 'Ctrl+x',
263 'ui',
264 _('Toggle the Reference mode')
265 ),
266 'toggle_bookmarks': desc(
267 ['Ctrl+b'],
268 'ui',
269 _('Show/hide bookmarks'),
270 ),
271 'new_bookmark': desc(
272 ['Ctrl+Alt+b'],
273 'ui',
274 _('Create a new bookmark'),
275 ),
276 'metadata': desc(
277 ['Ctrl+n', 'Ctrl+e'],
278 'ui',
279 _('Show the book metadata')
280 ),
281 'show_profiles': desc(
282 ['Alt+p'],
283 'ui',
284 _('Change settings quickly by creating and switching to "profiles"')
285 ),
286 'reload_book': desc(
287 ['Ctrl+Alt+F5', 'Ctrl+Alt+r'],
288 'ui',
289 _('Reload book'),
290 ),
291 'extend_selection_by_word': desc(
292 ['Ctrl+Shift+ArrowRight'],
293 'ui',
294 _('Alter the current selection forward by a word'),
295 ),
296 'shrink_selection_by_word': desc(
297 ['Ctrl+Shift+ArrowLeft'],
298 'ui',
299 _('Alter the current selection backwards by a word'),
300 ),
301 'extend_selection_by_character': desc(
302 ['Shift+ArrowRight'],
303 'ui',
304 _('Alter the current selection forward by a character'),
305 ),
306 'shrink_selection_by_character': desc(
307 ['Shift+ArrowLeft'],
308 'ui',
309 _('Alter the current selection backwards by a character'),
310 ),
311 'extend_selection_by_line': desc(
312 ['Shift+ArrowDown'],
313 'ui',
314 _('Alter the current selection forward by a line'),
315 ),
316 'extend_selection_to_start_of_line': desc(
317 ['Shift+Home'],
318 'ui',
319 _('Extend the current selection to the start of the line'),
320 ),
321 'extend_selection_to_end_of_line': desc(
322 ['Shift+End'],
323 'ui',
324 _('Extend the current selection to the end of the line'),
325 ),
326 'select_all': desc(
327 ['Ctrl+a'],
328 'ui',
329 _('Select all')
330 ),
331 'shrink_selection_by_line': desc(
332 ['Shift+ArrowUp'],
333 'ui',
334 _('Alter the current selection backwards by a line'),
335 ),
336 'extend_selection_by_paragraph': desc(
337 ['Ctrl+Shift+ArrowDown'],
338 'ui',
339 _('Alter the current selection forward by a paragraph'),
340 ),
341 'shrink_selection_by_paragraph': desc(
342 ['Ctrl+Shift+ArrowUp'],
343 'ui',
344 _('Alter the current selection backwards by a paragraph'),
345 ),
346 'show_chrome': desc(
347 ['Escape', 'ContextMenu'],
348 'ui',
349 _('Show the E-book viewer controls'),
350 ),
351 'preferences': desc(
352 ['Ctrl+,', 'Ctrl+Escape', 'Meta+Escape', 'Meta+,'],
353 'ui',
354 _('Show E-book viewer preferences'),
355 ),
356 'goto_location': desc(
357 [';', ':', 'Shift+:', 'Shift+;', 'Ctrl+g'],
358 'ui',
359 _('Go to a specified book location or position'),
360 ),
361 'toggle_autoscroll': desc(
362 'Ctrl+ ',
363 'scroll',
364 _('Toggle auto-scroll'),
365 ),
366 'scrollspeed_increase': desc(
367 'Alt+ArrowUp',
368 'scroll',
369 _('Auto scroll faster'),
370 ),
371 'scrollspeed_decrease': desc(
372 'Alt+ArrowDown',
373 'scroll',
374 _('Auto scroll slower'),
375 ),
376 } # }}}
377
378
379 def shortcuts_definition():
380 ans = shortcuts_definition.ans
381 if not ans:
382 ans = shortcuts_definition.ans = common_shortcuts()
383
384 if runtime.is_standalone_viewer:
385 add_standalone_viewer_shortcuts(ans)
386 else:
387 ans['sync_book'] = desc(
388 [],
389 'ui',
390 _('Sync last read position/annotations'),
391 )
392
393 return ans
394
395
396 def shortcuts_group_desc():
397 ans = shortcuts_group_desc.ans
398 if not ans:
399 ans = shortcuts_group_desc.ans = {
400 'scroll': _('Navigation'),
401 'ui': _('Interface'),
402 }
403 return ans
404
405
406 def add_standalone_viewer_shortcuts(sc):
407 ismacos = 'macos' in window.navigator.userAgent
408 sc['toggle_inspector'] = desc(
409 ['Ctrl+i'],
410 'ui',
411 _('Show/hide Inspector'),
412 )
413
414 sc['toggle_lookup'] = desc(
415 ['Ctrl+l'],
416 'ui',
417 _('Show/hide the word lookup panel'),
418 )
419
420 quit_shortcut = 'Meta+q' if ismacos else 'Ctrl+q'
421 sc['quit'] = desc(
422 quit_shortcut,
423 'ui',
424 _('Quit the E-book viewer'),
425 )
426
427 sc['print'] = desc(
428 'Ctrl+p',
429 'ui',
430 _('Print book to PDF'),
431 )
432
433 sc['toggle_toolbar'] = desc(
434 'Ctrl+F11',
435 'ui',
436 _('Toggle the toolbar'),
437 )
438
439 sc['toggle_highlights'] = desc(
440 'Ctrl+h',
441 'ui',
442 _('Toggle the highlights panel')
443 )
444
445 sc['edit_book'] = desc(
446 'Ctrl+d',
447 'ui',
448 _('Edit this book')
449 )
450
451
452 def create_shortcut_map(custom_shortcuts):
453 ans = {}
454 scd = shortcuts_definition()
455 for sc_name in Object.keys(scd):
456 entry = scd[sc_name]
457 shortcuts = entry.shortcuts
458 if custom_shortcuts and custom_shortcuts[sc_name]:
459 shortcuts = custom_shortcuts[sc_name]
460 for sc in shortcuts:
461 ans[keyevent_to_index(sc)] = sc_name
462 return ans
463
464
465 def shortcut_for_key_event(evt, shortcut_map):
466 idx = keyevent_to_index(evt)
467 return shortcut_map[idx]