From 20b76a0ae75e72373bec945b9e654ddc0d0e1cce Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Mon, 27 Jul 2020 19:26:05 +0530 Subject: [PATCH] Add a preference to turn off the selection popup bar --- src/pyj/read_book/prefs/main.pyj | 8 ++++ src/pyj/read_book/prefs/selection.pyj | 68 +++++++++++++++++++++++++++ src/pyj/read_book/selection_bar.pyj | 4 +- src/pyj/session.pyj | 1 + 4 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 src/pyj/read_book/prefs/selection.pyj diff --git a/src/pyj/read_book/prefs/main.pyj b/src/pyj/read_book/prefs/main.pyj index 9fbfe588f1..191eb8fdb0 100644 --- a/src/pyj/read_book/prefs/main.pyj +++ b/src/pyj/read_book/prefs/main.pyj @@ -12,6 +12,7 @@ from read_book.prefs.colors import commit_colors, create_colors_panel from read_book.prefs.fonts import commit_fonts, create_fonts_panel from read_book.prefs.head_foot import commit_head_foot, create_head_foot_panel from read_book.prefs.keyboard import commit_keyboard, create_keyboard_panel +from read_book.prefs.selection import commit_selection, create_selection_panel from read_book.prefs.scrolling import commit_scrolling, create_scrolling_panel from read_book.prefs.layout import commit_layout, create_layout_panel from read_book.prefs.misc import commit_misc, create_misc_panel @@ -91,6 +92,7 @@ class Prefs: ci(_('Styles'), 'user_stylesheet', _('Style rules for text and background image')) ci(_('Headers and footers'), 'head_foot', _('Customize the headers and footers')) ci(_('Scrolling behavior'), 'scrolling', _('Control how the viewer scrolls')) + ci(_('Selection behavior'), 'selection', _('Control how the viewer selects text')) ci(_('Keyboard shortcuts'), 'keyboard', _('Customize the keyboard shortcuts')) if runtime.is_standalone_viewer: ci(_('Fonts'), 'fonts', _('Font choices')) @@ -145,6 +147,12 @@ class Prefs: def close_scrolling(self): commit_scrolling(self.onchange, self.container) + def display_selection(self, container): + self.create_panel(container, 'selection', create_selection_panel) + + def close_selection(self): + commit_selection(self.onchange, self.container) + def create_prefs_panel(container, close_func, on_change): return Prefs(container, close_func, on_change) diff --git a/src/pyj/read_book/prefs/selection.pyj b/src/pyj/read_book/prefs/selection.pyj new file mode 100644 index 0000000000..977dabf771 --- /dev/null +++ b/src/pyj/read_book/prefs/selection.pyj @@ -0,0 +1,68 @@ +# vim:fileencoding=utf-8 +# License: GPL v3 Copyright: 2016, Kovid Goyal +from __python__ import bound_methods, hash_literals + +from elementmaker import E +from gettext import gettext as _ + +from book_list.globals import get_session_data +from dom import unique_id +from read_book.prefs.utils import create_button_box +from session import defaults + +CONTAINER = unique_id('selection-settings') + + +def restore_defaults(): + container = get_container() + for control in container.querySelectorAll('input[name]'): + val = defaults[control.getAttribute('name')] + if control.type is 'checkbox': + control.checked = val + elif control.type is 'number': + control.valueAsNumber = val + else: + control.value = val + + +def get_container(): + return document.getElementById(CONTAINER) + + +def create_selection_panel(container, apply_func, cancel_func): + container.appendChild(E.div(id=CONTAINER, style='margin: 1rem')) + container = container.lastChild + sd = get_session_data() + + def cb(name, text): + ans = E.input(type='checkbox', name=name) + if sd.get(name): + ans.checked = True + return E.div(style='margin-top:1ex', E.label(ans, '\xa0' + text)) + + container.appendChild(cb( + 'show_selection_bar', _('Show a popup bar with common actions next to selected text'))) + + container.appendChild(create_button_box(restore_defaults, apply_func, cancel_func)) + + +develop = create_selection_panel + + +def commit_selection(onchange): + sd = get_session_data() + container = get_container() + changed = False + for control in container.querySelectorAll('input[name]'): + name = control.getAttribute('name') + if control.type is 'checkbox': + val = control.checked + elif control.type is 'number': + val = control.valueAsNumber + else: + val = control.value + if val is not sd.get(name) and control.validity.valid: + sd.set(name, val) + changed = True + if changed: + onchange() diff --git a/src/pyj/read_book/selection_bar.pyj b/src/pyj/read_book/selection_bar.pyj index e6d795dcf9..d391778865 100644 --- a/src/pyj/read_book/selection_bar.pyj +++ b/src/pyj/read_book/selection_bar.pyj @@ -5,6 +5,7 @@ from __python__ import bound_methods, hash_literals from elementmaker import E from gettext import gettext as _ +from book_list.globals import get_session_data from book_list.theme import get_color from dom import clear, svgicon from read_book.globals import runtime, ui_operations @@ -66,7 +67,8 @@ class SelectionBar: self.container.style.display = 'none' def show(self): - if not self.view.create_annotation.is_visible: + sd = get_session_data() + if not self.view.create_annotation.is_visible and sd.get('show_selection_bar'): self.container.style.display = 'block' @property diff --git a/src/pyj/session.pyj b/src/pyj/session.pyj index 18412d49b2..e3704e0439 100644 --- a/src/pyj/session.pyj +++ b/src/pyj/session.pyj @@ -64,6 +64,7 @@ defaults = { 'word_actions': v'[]', 'highlight_style': None, 'custom_highlight_colors': v'[]', + 'show_selection_bar': True, } is_local_setting = {