From d0161868608dd6a64daf0daafbb464c49db0844f Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 21 Feb 2024 20:36:46 +0530 Subject: [PATCH] Profiles basically work in standalone viewer --- src/calibre/gui2/viewer/web_view.py | 18 ++++++++++-------- src/pyj/read_book/overlay.pyj | 12 ++++++++++-- src/pyj/read_book/profiles.pyj | 8 ++++++-- src/pyj/session.pyj | 21 +++++++++++++++++++++ src/pyj/viewer-main.pyj | 2 +- 5 files changed, 48 insertions(+), 13 deletions(-) diff --git a/src/calibre/gui2/viewer/web_view.py b/src/calibre/gui2/viewer/web_view.py index 8acc486e12..bf3154031f 100644 --- a/src/calibre/gui2/viewer/web_view.py +++ b/src/calibre/gui2/viewer/web_view.py @@ -695,14 +695,16 @@ class WebView(RestartingWebEngineView): self.standalone_misc_settings_changed.emit() elif key != '*': sd = vprefs['session_data'] - sd[key] = val - vprefs['session_data'] = sd - if key in ('standalone_font_settings', 'base_font_size'): - apply_font_settings(self) - elif key == 'read_mode': - self.paged_mode_changed.emit() - elif key == 'standalone_misc_settings': - self.standalone_misc_settings_changed.emit(val) + changed = sd.get(key) == val + if changed: + sd[key] = val + vprefs['session_data'] = sd + if key in ('standalone_font_settings', 'base_font_size'): + apply_font_settings(self) + elif key == 'read_mode': + self.paged_mode_changed.emit() + elif key == 'standalone_misc_settings': + self.standalone_misc_settings_changed.emit(val) def set_local_storage(self, key, val): if key == '*' and val is None: diff --git a/src/pyj/read_book/overlay.pyj b/src/pyj/read_book/overlay.pyj index 66460e49e8..98c48e5db4 100644 --- a/src/pyj/read_book/overlay.pyj +++ b/src/pyj/read_book/overlay.pyj @@ -540,10 +540,13 @@ class PrefsOverlay: # {{{ def on_container_click(self, evt): pass # Dont allow panel to be closed by a click + def create_panel_impl(self, container): + return create_prefs_panel(container, self.overlay.hide_current_panel, self.on_change) + def show(self, container): self.changes_occurred = False container.style.backgroundColor = get_color('window-background') - self.prefs = create_prefs_panel(container, self.overlay.hide_current_panel, self.on_change) + self.prefs = self.create_panel_impl(container) def on_change(self): self.changes_occurred = True @@ -556,6 +559,11 @@ class PrefsOverlay: # {{{ self.changes_occurred = False self.overlay.view.preferences_changed() + +class ProfilesOverlay(PrefsOverlay): + def create_panel_impl(self, container): + return create_profiles_panel(container, self.overlay.hide_current_panel, self.on_change) + # }}} class FontSizeOverlay: # {{{ @@ -749,7 +757,7 @@ class Overlay: def show_profiles(self): self.hide_current_panel() - self.panels.push(TOCOverlay(self, create_profiles_panel.bind(None, self.hide_current_panel), _('Profiles'))) + self.panels.push(ProfilesOverlay(self)) self.show_current_panel() def show_goto(self): diff --git a/src/pyj/read_book/profiles.pyj b/src/pyj/read_book/profiles.pyj index a2139f47b1..83d3cafd7c 100644 --- a/src/pyj/read_book/profiles.pyj +++ b/src/pyj/read_book/profiles.pyj @@ -10,7 +10,7 @@ from dom import clear, unique_id from gettext import gettext as _ from modals import question_dialog from read_book.globals import ui_operations -from session import settings_for_reader_profile +from session import apply_reader_profile, settings_for_reader_profile from widgets import create_button @@ -36,9 +36,12 @@ def use_profile(container_id, profile_name, profile_data): container = document.getElementById(container_id) if not container: return + apply_reader_profile(get_session_data(), profile_data) + container.dispatchEvent(new Event('settings_changed')) container.dispatchEvent(new Event('close_panel')) + def delete_profile(container_id, profile_name): question_dialog(_('Are you sure?'), _('Are you sure you want to delete the saved profile named: {}?').format(profile_name), def (ok): @@ -84,9 +87,10 @@ def load_profiles(container_id): ui_operations.get_all_profiles(got_all_profiles.bind(None, container_id)) -def create_profiles_panel(hide_panel, book, container, onclick): +def create_profiles_panel(container, hide_panel, on_change): c = E.div(style='margin: 1rem', id=unique_id('placeholder-prefs')) c.addEventListener('close_panel', def(): hide_panel();, False) + c.addEventListener('settings_changed', def(): on_change();, False) container_id = c.id a = E.div( style='display:flex', diff --git a/src/pyj/session.pyj b/src/pyj/session.pyj index 502ac85c47..beaa26b49e 100644 --- a/src/pyj/session.pyj +++ b/src/pyj/session.pyj @@ -234,6 +234,21 @@ def get_subset_of_settings(sd, filter_func): ans[setting_name] = window.structuredClone(curval if is_set else metadata.default) return ans +def apply_profile(sd, profile, filter_func): + sentinel = {} + for setting_name in Object.keys(all_settings): + metadata = all_settings[setting_name] + curval = sd.get(setting_name, sentinel) + is_set = curval is not sentinel + if filter_func(setting_name, metadata, is_set, curval): + newval = None + if Object.prototype.hasOwnProperty.call(profile, setting_name): + newval = profile[setting_name] + if deep_eq(newval, metadata.default): + newval = None + sd.set(setting_name, window.structuredClone(newval)) + + standalone_reader_defaults = { 'remember_window_geometry': False, 'remember_last_read': True, @@ -270,6 +285,12 @@ def settings_for_reader_profile(sd): return get_subset_of_settings(sd, filter_func) +def apply_reader_profile(sd, profile): + def filter_func(setting_name, metadata, is_set, curval): + return metadata.category is 'read_book' + apply_profile(sd, profile, filter_func) + + default_interface_data = { 'username': None, 'output_format': 'EPUB', diff --git a/src/pyj/viewer-main.pyj b/src/pyj/viewer-main.pyj index 9c69d92f5f..49e3c8186f 100644 --- a/src/pyj/viewer-main.pyj +++ b/src/pyj/viewer-main.pyj @@ -161,7 +161,7 @@ class SessionData: def set(self, key, val): if val is None: - self.data[key] = session_defaults()[key] + self.data[key] = val = session_defaults()[key] else: self.data[key] = val to_python.set_session_data(key, val)