From 5e05b450a37fb04b9e5c496b3090b50f2590ccbc Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Mon, 26 Aug 2019 09:01:16 +0530 Subject: [PATCH] Implement search shortcuts --- src/pyj/read_book/search.pyj | 13 ++++++++++++- src/pyj/read_book/shortcuts.pyj | 18 ++++++++++++++++++ src/pyj/read_book/view.pyj | 11 +++++++++++ 3 files changed, 41 insertions(+), 1 deletion(-) diff --git a/src/pyj/read_book/search.pyj b/src/pyj/read_book/search.pyj index 258d7637dc..1b129b806f 100644 --- a/src/pyj/read_book/search.pyj +++ b/src/pyj/read_book/search.pyj @@ -10,6 +10,7 @@ from complete import create_search_bar from dom import add_extra_css, build_rule, svgicon from read_book.globals import ui_operations from read_book.resources import text_from_serialized_html +from read_book.shortcuts import shortcut_for_key_event CLASS_NAME = 'book-search-container' @@ -40,12 +41,22 @@ class SearchOverlay: create_search_bar(self.find_next, 'search-in-book', placeholder=_('Search') + '…', button=next_button, associated_widgets=[prev_button, close_button]), '\xa0', next_button, '\xa0', prev_button, '\xa0', close_button )) - c.firstChild.addEventListener('keydown', self.onkeydown) + c.firstChild.addEventListener('keydown', self.onkeydown, {'passive': False}) def onkeydown(self, event): if event.key is 'Escape' or event.key is 'Esc': self.hide() event.preventDefault(), event.stopPropagation() + return + sc_name = shortcut_for_key_event(event, self.view.keyboard_shortcut_map) + if sc_name is 'next_match': + self.find_next() + event.preventDefault(), event.stopPropagation() + return + if sc_name is 'previous_match': + self.find_previous() + event.preventDefault(), event.stopPropagation() + return @property def container(self): diff --git a/src/pyj/read_book/shortcuts.pyj b/src/pyj/read_book/shortcuts.pyj index 57265d6281..19b9efb48f 100644 --- a/src/pyj/read_book/shortcuts.pyj +++ b/src/pyj/read_book/shortcuts.pyj @@ -157,6 +157,24 @@ SHORTCUTS = { _('Show/hide Table of Contents'), ), + 'start_search': desc( + v"['/', 'Ctrl+f']", + 'ui', + _('Start search'), + ), + + 'next_match': desc( + v"['F3']", + 'ui', + _('Find next'), + ), + + 'previous_match': desc( + v"['Shift+F3']", + 'ui', + _('Find previous'), + ), + } diff --git a/src/pyj/read_book/view.pyj b/src/pyj/read_book/view.pyj index 7868d749a4..b5c6af3499 100644 --- a/src/pyj/read_book/view.pyj +++ b/src/pyj/read_book/view.pyj @@ -24,6 +24,7 @@ from read_book.prefs.font_size import change_font_size_by from read_book.prefs.head_foot import render_head_foot from read_book.resources import load_resources from read_book.search import SearchOverlay, find_in_spine +from read_book.shortcuts import create_shortcut_map from read_book.timers import Timers from read_book.toc import get_current_toc_nodes, update_visible_toc_nodes from read_book.touch import set_left_margin_handler, set_right_margin_handler @@ -106,6 +107,7 @@ class View: self.show_chrome_counter = 0 self.clock_timer_id = 0 sd = get_session_data() + self.keyboard_shortcut_map = create_shortcut_map(sd.get('keyboard_shortcuts')) left_margin = E.div(svgicon('caret-left'), style='width:{}px;'.format(sd.get('margin_left', 20)), class_='book-side-margin', id='book-left-margin', onclick=self.left_margin_clicked) set_left_margin_handler(left_margin) right_margin = E.div(svgicon('caret-right'), style='width:{}px;'.format(sd.get('margin_right', 20)), class_='book-side-margin', id='book-right-margin', onclick=self.right_margin_clicked) @@ -229,6 +231,12 @@ class View: ui_operations.toggle_inspector() elif data.name is 'toggle_lookup': ui_operations.toggle_lookup() + elif data.name is 'start_search': + self.show_search() + elif data.name is 'next_match': + self.search_overlay.find_next() + elif data.name is 'previous_match': + self.search_overlay.find_previous() def on_selection_change(self, data): self.currently_showing.selected_text = data.text @@ -429,6 +437,9 @@ class View: sd.set('controls_help_shown_count', c + 1) def redisplay_book(self): + # redisplay_book() is called when settings are changed + sd = get_session_data() + self.keyboard_shortcut_map = create_shortcut_map(sd.get('keyboard_shortcuts')) self.display_book(self.book) def iframe_settings(self, name):