Skip to content

Commit

Permalink
Profiles basically work in standalone viewer
Browse files Browse the repository at this point in the history
  • Loading branch information
kovidgoyal committed Feb 21, 2024
1 parent 43338d6 commit d016186
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 13 deletions.
18 changes: 10 additions & 8 deletions src/calibre/gui2/viewer/web_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -695,14 +695,16 @@ def set_session_data(self, key, val):
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:
Expand Down
12 changes: 10 additions & 2 deletions src/pyj/read_book/overlay.pyj
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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: # {{{
Expand Down Expand Up @@ -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):
Expand Down
8 changes: 6 additions & 2 deletions src/pyj/read_book/profiles.pyj
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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):
Expand Down Expand Up @@ -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',
Expand Down
21 changes: 21 additions & 0 deletions src/pyj/session.pyj
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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',
Expand Down
2 changes: 1 addition & 1 deletion src/pyj/viewer-main.pyj
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit d016186

Please sign in to comment.