From 23dcafa265472d8cc547ed9cc840d5f209919b9f Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 22 Oct 2019 09:02:27 +0530 Subject: [PATCH] Viewer: Fix shortcuts preferences not being translated --- src/pyj/read_book/prefs/keyboard.pyj | 13 +++++----- src/pyj/read_book/shortcuts.pyj | 39 ++++++++++++++++++---------- src/pyj/viewer-main.pyj | 3 ++- 3 files changed, 34 insertions(+), 21 deletions(-) diff --git a/src/pyj/read_book/prefs/keyboard.pyj b/src/pyj/read_book/prefs/keyboard.pyj index b187041f18..c9a52a4c5a 100644 --- a/src/pyj/read_book/prefs/keyboard.pyj +++ b/src/pyj/read_book/prefs/keyboard.pyj @@ -9,7 +9,8 @@ from book_list.globals import get_session_data from book_list.item_list import create_item, create_item_list from dom import clear, svgicon, unique_id from read_book.shortcuts import ( - GROUP_DESC, SHORTCUTS, key_as_text, keyevent_as_shortcut, shortcut_differs + key_as_text, keyevent_as_shortcut, shortcut_differs, shortcuts_definition, + shortcuts_group_desc ) from widgets import create_button @@ -22,7 +23,7 @@ def restore_defaults(close_func): get_container().dataset.changed = 'true' for item in get_container().querySelectorAll('[data-user-data]'): q = JSON.parse(item.dataset.userData) - q.shortcuts = SHORTCUTS[q.name].shortcuts + q.shortcuts = shortcuts_definition()[q.name].shortcuts item.dataset.userData = JSON.stringify(q) close_func() @@ -127,7 +128,7 @@ def customize_shortcut(sc_name): container = container.lastChild clear(container) container.dataset.scName = sc_name - sc = SHORTCUTS[sc_name] + sc = shortcuts_definition()[sc_name] container.appendChild(E.h4(sc.short)) if sc.long: container.appendChild(E.div(sc.long, style='font-style: italic; font-size: smaller; margin-top: 1ex')) @@ -155,9 +156,9 @@ def create_keyboard_panel(container, close_func, onchange): container = container.firstChild sd = get_session_data() custom_shortcuts = sd.get('keyboard_shortcuts') - groups = as_groups(SHORTCUTS) + groups = as_groups(shortcuts_definition()) for group_name in Object.keys(groups): - container.appendChild(E.h3(style='margin-top: 1ex', GROUP_DESC[group_name])) + container.appendChild(E.h3(style='margin-top: 1ex', shortcuts_group_desc()[group_name])) group = groups[group_name] items = [] for sc_name in sorted(Object.keys(group), key=sort_group_key.bind(None, group)): @@ -188,7 +189,7 @@ def commit_keyboard(onchange): vals = {} for item in get_container().querySelectorAll('[data-user-data]'): q = JSON.parse(item.dataset.userData) - if shortcuts_differ(q.shortcuts, SHORTCUTS[q.name].shortcuts): + if shortcuts_differ(q.shortcuts, shortcuts_definition()[q.name].shortcuts): vals[q.name] = q.shortcuts sd.set('keyboard_shortcuts', vals) create_keyboard_panel.onchange = None diff --git a/src/pyj/read_book/shortcuts.pyj b/src/pyj/read_book/shortcuts.pyj index 2b186d19bd..91e1d1234f 100644 --- a/src/pyj/read_book/shortcuts.pyj +++ b/src/pyj/read_book/shortcuts.pyj @@ -86,13 +86,10 @@ def key_as_text(evt): return mods + key -GROUP_DESC = { - 'scroll': _('Navigation'), - 'ui': _('Interface'), -} - - -SHORTCUTS = { +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', @@ -248,31 +245,44 @@ SHORTCUTS = { 'ui', _('Show the viewer controls'), ), -} + } + 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 - SHORTCUTS['toggle_bookmarks'] = desc( + sc = shortcuts_definition() + sc['toggle_bookmarks'] = desc( v"['Ctrl+b']", 'ui', _('Show/hide bookmarks'), ) - SHORTCUTS['toggle_inspector'] = desc( + sc['toggle_inspector'] = desc( v"['Ctrl+i']", 'ui', _('Show/hide Inspector'), ) - SHORTCUTS['toggle_lookup'] = desc( + sc['toggle_lookup'] = desc( v"['Ctrl+l']", 'ui', _('Show/hide the word lookup panel'), ) quit_shortcut = 'Meta+q' if ismacos else 'Ctrl+q' - SHORTCUTS['quit'] = desc( + sc['quit'] = desc( quit_shortcut, 'ui', _('Quit the viewer'), @@ -280,8 +290,9 @@ def add_standalone_viewer_shortcuts(): def create_shortcut_map(custom_shortcuts): ans = {} - for sc_name in Object.keys(SHORTCUTS): - entry = SHORTCUTS[sc_name] + 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] diff --git a/src/pyj/viewer-main.pyj b/src/pyj/viewer-main.pyj index 87c9d3e9a5..5add96e56a 100644 --- a/src/pyj/viewer-main.pyj +++ b/src/pyj/viewer-main.pyj @@ -28,7 +28,6 @@ runtime.is_standalone_viewer = True runtime.FAKE_HOST = FAKE_HOST runtime.SANDBOX_HOST = FAKE_HOST.rpartition('.')[0] + '.sandbox' runtime.FAKE_PROTOCOL = FAKE_PROTOCOL -add_standalone_viewer_shortcuts() book = None view = None @@ -288,6 +287,7 @@ if window is window.top: TRANSLATIONS_DATA = v'__TRANSLATIONS_DATA__' if TRANSLATIONS_DATA: install(TRANSLATIONS_DATA) + add_standalone_viewer_shortcuts() ui_operations.get_file = get_file ui_operations.get_mathjax_files = get_mathjax_files ui_operations.update_url_state = update_url_state @@ -360,3 +360,4 @@ else: footnotes_main() else: iframe_main() + add_standalone_viewer_shortcuts()