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