diff --git a/src/pyj/read_book/iframe.pyj b/src/pyj/read_book/iframe.pyj index 4edf3d7728..daa9846f18 100644 --- a/src/pyj/read_book/iframe.pyj +++ b/src/pyj/read_book/iframe.pyj @@ -146,6 +146,7 @@ class IframeBoss: 'annotations': self.annotations_msg_received, 'copy_selection': self.copy_selection, 'replace_highlights': self.replace_highlights, + 'clear_selection': def(): window.getSelection().removeAllRanges();, } self.comm = IframeClient(handlers) self.last_window_ypos = 0 diff --git a/src/pyj/read_book/prefs/main.pyj b/src/pyj/read_book/prefs/main.pyj index 3c4be6353f..9fbfe588f1 100644 --- a/src/pyj/read_book/prefs/main.pyj +++ b/src/pyj/read_book/prefs/main.pyj @@ -94,7 +94,7 @@ class Prefs: ci(_('Keyboard shortcuts'), 'keyboard', _('Customize the keyboard shortcuts')) if runtime.is_standalone_viewer: ci(_('Fonts'), 'fonts', _('Font choices')) - ci(_('Miscellaneous'), 'misc', _('Window size, last read position, etc.')) + ci(_('Miscellaneous'), 'misc', _('Window size, last read position, toolbar, etc.')) build_list(c, items) def display_fonts(self, container): diff --git a/src/pyj/read_book/selection_bar.pyj b/src/pyj/read_book/selection_bar.pyj index bdcf74c9c2..4f50976426 100644 --- a/src/pyj/read_book/selection_bar.pyj +++ b/src/pyj/read_book/selection_bar.pyj @@ -3,8 +3,11 @@ from __python__ import bound_methods, hash_literals from elementmaker import E +from gettext import gettext as _ from book_list.theme import get_color +from dom import svgicon +from read_book.globals import ui_operations class SelectionBar: @@ -13,11 +16,25 @@ class SelectionBar: self.view = view c = self.container bar = E.div( - style='position: absolute; left: 0; top: 0; height: 3ex; border: solid 1px currentColor; border-radius: 5px; overflow: hidden;' - 'pointer-events: auto; min-width: 50px; padding: 5px; background-color: {}'.format(get_color("window-background")) + style='position: absolute; height: 4ex; border: solid 1px currentColor; border-radius: 5px; overflow: hidden;' + 'display: flex; align-items: center; pointer-events: auto; padding: 5px; left: 0; top: 0;' + 'background-color: {}'.format(get_color("window-background")) ) c.appendChild(bar) + def cb(icon, tooltip, callback): + ans = svgicon(icon, '3ex', '3ex', tooltip) + ans.addEventListener('click', callback) + ans.classList.add('simple-link') + ans.style.marginLeft = ans.style.marginRight = '0.5rem' + return ans + + if ui_operations.copy_selection: + bar.appendChild(cb('copy', _('Copy to clipboard'), self.copy_to_clipboard)) + if ui_operations.toggle_lookup: + bar.appendChild(cb('library', _('Lookup/search selected word'), self.lookup)) + bar.appendChild(cb('close', _('Clear the selection'), self.clear_selection)) + @property def container(self): return document.getElementById('book-selection-bar-overlay') @@ -36,6 +53,16 @@ class SelectionBar: def is_visible(self): return self.container.style.display is not 'none' + def copy_to_clipboard(self): + if self.view.currently_showing.selected_text and ui_operations.copy_selection: + ui_operations.copy_selection(self.view.currently_showing.selected_text) + + def lookup(self): + ui_operations.toggle_lookup(True) + + def clear_selection(self): + self.view.on_handle_shortcut({'name': 'clear_selection'}) + def update_position(self): cs = self.view.currently_showing if not cs.has_selection: diff --git a/src/pyj/read_book/view.pyj b/src/pyj/read_book/view.pyj index 15a2107c25..0559437ead 100644 --- a/src/pyj/read_book/view.pyj +++ b/src/pyj/read_book/view.pyj @@ -490,6 +490,8 @@ class View: elif data.name is 'previous': self.iframe_wrapper.send_message( 'next_screen', backwards=True, all_pages_on_screen=get_session_data().get('paged_margin_clicks_scroll_by_screen')) + elif data.name is 'clear_selection': + self.iframe_wrapper.send_message('clear_selection') elif data.name is 'print': ui_operations.print_book() elif data.name is 'preferences': diff --git a/src/pyj/viewer-main.pyj b/src/pyj/viewer-main.pyj index 9ba62fed43..2393d45aff 100644 --- a/src/pyj/viewer-main.pyj +++ b/src/pyj/viewer-main.pyj @@ -346,8 +346,8 @@ if window is window.top: to_python.reset_interface() ui_operations.quit = def(): to_python.quit() - ui_operations.toggle_lookup = def(): - to_python.toggle_lookup() + ui_operations.toggle_lookup = def(force_show): + to_python.toggle_lookup(v'!!force_show') ui_operations.selection_changed = def(selected_text): to_python.selection_changed(selected_text) ui_operations.update_current_toc_nodes = def(current_node_id, top_level_node_id):