From db3401b02bd7acbdcc28b9f62fb1db8632dad494 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 20 Feb 2024 21:05:52 +0530 Subject: [PATCH] More work on profiles --- src/pyj/read_book/overlay.pyj | 11 +++++- src/pyj/read_book/prefs/misc.pyj | 16 +------- src/pyj/read_book/profiles.pyj | 68 ++++++++++++++++++++++++++++++++ src/pyj/session.pyj | 31 +++++++++++++-- src/pyj/viewer-main.pyj | 9 ++--- 5 files changed, 112 insertions(+), 23 deletions(-) create mode 100644 src/pyj/read_book/profiles.pyj diff --git a/src/pyj/read_book/overlay.pyj b/src/pyj/read_book/overlay.pyj index 9a3d513373..66460e49e8 100644 --- a/src/pyj/read_book/overlay.pyj +++ b/src/pyj/read_book/overlay.pyj @@ -22,6 +22,7 @@ from read_book.bookmarks import create_bookmarks_panel from read_book.globals import runtime, ui_operations from read_book.goto import create_goto_panel, create_location_overlay from read_book.highlights import create_highlights_panel +from read_book.profiles import create_profiles_panel from read_book.open_book import create_open_book from read_book.prefs.font_size import create_font_size_panel from read_book.prefs.head_foot import time_formatter @@ -347,7 +348,10 @@ class MainOverlay: # {{{ ac(_('Read aloud'), _('Read the book aloud'), def(): self.overlay.hide() self.overlay.view.start_read_aloud() - , 'bullhorn') + , 'bullhorn'), + ac(_('Profiles'), _('Quickly switch between different settings'), def(): + self.overlay.show_profiles() + , 'convert') )) if full_screen_actions.length: @@ -743,6 +747,11 @@ class Overlay: self.panels.push(TOCOverlay(self, create_highlights_panel.bind(None, self.view.annotations_manager, self.hide_current_panel), _('Highlights'))) self.show_current_panel() + def show_profiles(self): + self.hide_current_panel() + self.panels.push(TOCOverlay(self, create_profiles_panel.bind(None, self.hide_current_panel), _('Profiles'))) + self.show_current_panel() + def show_goto(self): self.hide_current_panel() self.panels.push(TOCOverlay(self, create_goto_panel.bind(None, self.view.current_position_data), _('Go to…'))) diff --git a/src/pyj/read_book/prefs/misc.pyj b/src/pyj/read_book/prefs/misc.pyj index 840320f999..7949f36cb6 100644 --- a/src/pyj/read_book/prefs/misc.pyj +++ b/src/pyj/read_book/prefs/misc.pyj @@ -3,28 +3,16 @@ 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 gettext import gettext as _ from read_book.globals import ui_operations from read_book.prefs.utils import create_button_box -from session import session_defaults +from session import session_defaults, standalone_reader_defaults as DEFAULTS from widgets import create_button CONTAINER = unique_id('standalone-misc-settings') -DEFAULTS = { - 'remember_window_geometry': False, - 'remember_last_read': True, - 'show_actions_toolbar': False, - 'show_actions_toolbar_in_fullscreen': False, - 'save_annotations_in_ebook': True, - 'sync_annots_user': '', - 'singleinstance': False, - 'auto_hide_mouse': True, - 'restore_docks': True, -} - def restore_defaults(): container = get_container() diff --git a/src/pyj/read_book/profiles.pyj b/src/pyj/read_book/profiles.pyj new file mode 100644 index 0000000000..02825a3b9a --- /dev/null +++ b/src/pyj/read_book/profiles.pyj @@ -0,0 +1,68 @@ +# vim:fileencoding=utf-8 +# License: GPL v3 Copyright: 2024, Kovid Goyal +from __python__ import bound_methods, hash_literals + +from elementmaker import E + +from book_list.globals import get_session_data +from dom import clear, unique_id +from gettext import gettext as _ +from read_book.globals import ui_operations +from session import settings_for_reader_profile +from widgets import create_button + + +def save_profile(container_id, hide_panel): + container = document.getElementById(container_id) + if not container: + return + name = container.querySelector('[name=profile_name]').value + sd = get_session_data() + settings = settings_for_reader_profile(sd) + ui_operations.save_profile(name, settings, def(err_msg): + if err_msg: + ui_operations.show_error(_('Unhandled error'), _('Could not save profile with error: ') + err_msg) + else: + container = document.getElementById(container_id) + if container: + r = container.querySelector('.saved_msg') + if r: + clear(r) + r.appendChild(E.div(E.i(_('Saved: ') + name), style='margin-top: 1rem; margin-bottom: 1rem')) + ) + + + +def got_all_profiles(hide_panel, container_id, all_profiles): + container = document.getElementById(container_id) + if not container: + return + print(all_profiles) + + +def create_profiles_panel(hide_panel, book, container, onclick): + c = E.div(style='margin: 1rem', id=unique_id('placeholder-prefs')) + container_id = c.id + a = E.div( + style='display:flex', + E.div( + E.label(_('Save current settings as profile:') + ' '), E.input( + name='profile_name', placeholder=_('Name of profile'), onkeydown=def(e): + if e.key is 'Enter': + save_profile(container_id, hide_panel) + ), + ), + E.div('\xa0'), + E.div( + create_button(_('Save'), 'bookmark', save_profile.bind(None, container_id, hide_panel)) + ) + ) + container.appendChild(c) + c.appendChild(a) + c.appendChild(E.div(class_='saved_msg')) + c.appendChild(E.hr()) + c.appendChild(E.div( + style='padding-bottom: 1rem', class_='loading_msg', + _('Loading saved profiles, please wait…') + )) + ui_operations.get_all_profiles(got_all_profiles.bind(None, hide_panel, container.id) diff --git a/src/pyj/session.pyj b/src/pyj/session.pyj index 5362b38258..502ac85c47 100644 --- a/src/pyj/session.pyj +++ b/src/pyj/session.pyj @@ -230,15 +230,40 @@ def get_subset_of_settings(sd, filter_func): curval = sd.get(setting_name, ans) metadata = all_settings[setting_name] is_set = curval is not ans - if filter_func(setting_name, metadata, is_set): + if filter_func(setting_name, metadata, is_set, curval): ans[setting_name] = window.structuredClone(curval if is_set else metadata.default) return ans +standalone_reader_defaults = { + 'remember_window_geometry': False, + 'remember_last_read': True, + 'show_actions_toolbar': False, + 'show_actions_toolbar_in_fullscreen': False, + 'save_annotations_in_ebook': True, + 'sync_annots_user': '', + 'singleinstance': False, + 'auto_hide_mouse': True, + 'restore_docks': True, +} + def settings_for_reader_profile(sd): - def filter_func(setting_name, metadata, is_set): - if not is_set or metadata.category is not 'read_book' or metadata.disallowed_in_profile: + margin_text_settings = {'footer': True, 'header': True, 'controls_footer': True, 'left-margin': True, 'right-margin': True} + + def filter_func(setting_name, metadata, is_set, curval): + if margin_text_settings[setting_name]: + curval = window.structuredClone(curval) + for x in v"['left', 'right', 'middle']": + if curval[x] is 'empty': + v'delete curval[x]' + elif setting_name is 'standalone_misc_settings': + curval = window.structuredClone(curval) + for key in Object.keys(curval): + if curval[key] is standalone_reader_defaults[key]: + v'delete curval[key]' + + if not is_set or metadata.category is not 'read_book' or metadata.disallowed_in_profile or deep_eq(curval, metadata.default): return False return True diff --git a/src/pyj/viewer-main.pyj b/src/pyj/viewer-main.pyj index fab5a693a7..27e576cfa8 100644 --- a/src/pyj/viewer-main.pyj +++ b/src/pyj/viewer-main.pyj @@ -81,9 +81,7 @@ def profiles_recevied(proceed, end_type, xhr, ev): 'Could not load viewer profiles with error: {}').format(xhr.error_html)) return if not xhr.responseType or xhr.responseType is 'text': - result = xhr.responseText - else if xhr.responseType is 'blob': - result = xhr.response + result = JSON.parse(xhr.responseText) else: show_error(_('Failed to load profiles'), _( 'Could not load viewer profiles: unknown response type: {}').format(xhr.responseType)) @@ -93,7 +91,7 @@ def profiles_recevied(proceed, end_type, xhr, ev): def get_all_profiles(proceed): - xhr = ajax('all-profiles/', profiles_recevied.bind(None, proceed), ok_code=0) + xhr = ajax('all-profiles', profiles_recevied.bind(None, proceed), ok_code=0) xhr.responseType = 'text' xhr.send() @@ -353,8 +351,9 @@ if window is window.top: if TRANSLATIONS_DATA: install(TRANSLATIONS_DATA) ui_operations.get_all_profiles = get_all_profiles - ui_operations.save_profile = def(name, settings): + ui_operations.save_profile = def(name, settings, proceed): to_python.save_profile(name, settings) + proceed() ui_operations.get_file = get_file ui_operations.get_url_for_book_file_name = get_url_for_book_file_name ui_operations.get_mathjax_files = get_mathjax_files