Add some basic operations to the selection bar

This commit is contained in:
Kovid Goyal 2020-07-22 15:52:22 +05:30
parent 626b2209d6
commit 1c38d64b1a
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
5 changed files with 35 additions and 5 deletions

View File

@ -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

View File

@ -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):

View File

@ -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:

View File

@ -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':

View File

@ -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):