From 393e47dcdb4e2ad4a4dedb8b5b28d952a45d70bd Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 28 Jul 2020 13:49:13 +0530 Subject: [PATCH] Add a remove highlight action to the popup bar Only visible if a highlight is selected --- src/pyj/read_book/prefs/selection.pyj | 7 ++++++- src/pyj/read_book/selection_bar.pyj | 17 ++++++++++++----- src/pyj/session.pyj | 2 +- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src/pyj/read_book/prefs/selection.pyj b/src/pyj/read_book/prefs/selection.pyj index 6789b8145a..20dfec1678 100644 --- a/src/pyj/read_book/prefs/selection.pyj +++ b/src/pyj/read_book/prefs/selection.pyj @@ -41,7 +41,7 @@ def create_selection_panel(container, apply_func, cancel_func): return E.div(style='margin-top:1ex', E.label(ans, '\xa0' + text)) def url(name, text, title): - ans = E.input(type='url', name=name, value=sd.get(name), size='50', title=title or '') + ans = E.input(type='url', name=name, value=sd.get(name), size='50', title=title or '', style='margin-top: 1ex') return E.div(style='margin-top:1ex', E.label(text, E.br(), ans)) container.appendChild(cb( @@ -50,6 +50,11 @@ def create_selection_panel(container, apply_func, cancel_func): 'net_search_url', _('URL to query when searching the internet'), _('The {q} in the URL is replaced by the selected text'))) + container.appendChild(E.div( + style='margin-top: 2ex; border-top: solid 1px; padding-top: 1ex;', + _('Customize which actions are shown in the selection popup bar') + )) + container.appendChild(create_button_box(restore_defaults, apply_func, cancel_func)) diff --git a/src/pyj/read_book/selection_bar.pyj b/src/pyj/read_book/selection_bar.pyj index d2b1b2e569..9a906c20db 100644 --- a/src/pyj/read_book/selection_bar.pyj +++ b/src/pyj/read_book/selection_bar.pyj @@ -12,8 +12,8 @@ from read_book.globals import runtime, ui_operations def all_actions(): - def a(icon, text, func): - return {'icon': icon, 'text': text, 'function_name': func} + def a(icon, text, func, needs_highlight): + return {'icon': icon, 'text': text, 'function_name': func, 'needs_highlight': v'!!needs_highlight'} if not all_actions.ans: all_actions.ans = { @@ -21,6 +21,7 @@ def all_actions(): 'lookup': a('library', _('Lookup/search selected word'), 'lookup'), 'highlight': a('highlight', _('Highlight selection'), 'create_highlight'), 'search_net': a('search', _('Search for selection on the net'), 'internet_search'), + 'remove_highlight': a('eraser', _('Remove this highlight'), 'remove_highlight', True), 'clear': a('close', _('Clear selection'), 'clear_selection'), } return all_actions.ans @@ -31,7 +32,8 @@ class SelectionBar: def __init__(self, view): self.view = view - def build_bar(self, notes): + def build_bar(self, annot_id): + notes = self.view.annotations_manager.notes_for_highlight(annot_id) c = self.container max_width = 'min(50rem, 90vw)' if self.supports_css_min_max else '50rem' bar_container = E.div( @@ -65,7 +67,7 @@ class SelectionBar: sd = get_session_data() for acname in sd.get('selection_bar_actions'): ac = actions[acname] - if ac: + if ac and (not ac.needs_highlight or v'!!annot_id'): bar.appendChild(cb(ac.icon, ac.text, self[ac.function_name])) self.show_notes(bar_container, notes) return bar_container @@ -117,6 +119,11 @@ class SelectionBar: def create_highlight(self): self.view.initiate_create_annotation(True) + def remove_highlight(self): + annot_id = self.view.currently_showing.selection.annot_id + if annot_id: + self.view.create_annotation.remove_highlight(annot_id) + def show_notes(self, bar, notes): notes = (notes or "").strip() if not notes: @@ -167,7 +174,7 @@ class SelectionBar: def map_boundary(x): return {'x': (x.x or 0) + margins.left, 'y': (x.y or 0) + margins.top, 'height': x.height or 0, 'onscreen': x.onscreen} - bar = self.build_bar(self.view.annotations_manager.notes_for_highlight(cs.annot_id)) + bar = self.build_bar(cs.annot_id) start = map_boundary(cs.start) end = map_boundary(cs.end) self.show() diff --git a/src/pyj/session.pyj b/src/pyj/session.pyj index f16eeb1147..cf7a4013c7 100644 --- a/src/pyj/session.pyj +++ b/src/pyj/session.pyj @@ -66,7 +66,7 @@ defaults = { 'custom_highlight_colors': v'[]', 'show_selection_bar': True, 'net_search_url': 'https://google.com/search?q={q}', - 'selection_bar_actions': v"['copy', 'lookup', 'highlight', 'search_net', 'clear']", + 'selection_bar_actions': v"['copy', 'lookup', 'highlight', 'remove_highlight', 'search_net', 'clear']", } is_local_setting = {