From 0dc03ed11e5270ac6b90c10551a98f9447ced7b1 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 5 Nov 2019 15:07:52 +0530 Subject: [PATCH] Viewer: Change the default color scheme to "System" which matches the colors used by the rest of calibre/the operating system Also means that if you switch color schemes while the viewer is running, the viewer will follow along automatically. --- src/calibre/gui2/viewer/web_view.py | 24 ++++++++++++++++++++++-- src/pyj/read_book/globals.pyj | 11 +++++++++++ src/pyj/session.pyj | 2 +- src/pyj/viewer-main.pyj | 20 ++++++++++++++------ 4 files changed, 48 insertions(+), 9 deletions(-) diff --git a/src/calibre/gui2/viewer/web_view.py b/src/calibre/gui2/viewer/web_view.py index 63158d826e..b67ad86fec 100644 --- a/src/calibre/gui2/viewer/web_view.py +++ b/src/calibre/gui2/viewer/web_view.py @@ -269,6 +269,7 @@ class ViewerBridge(Bridge): background_image_changed = to_js() goto_frac = to_js() trigger_shortcut = to_js() + set_system_palette = to_js() def apply_font_settings(page_or_view): @@ -384,6 +385,15 @@ class Inspector(QWidget): return QSize(600, 1200) +def system_colors(): + pal = QApplication.instance().palette() + return { + 'background': pal.color(pal.Base).name(), + 'foreground': pal.color(pal.Text).name(), + 'link': pal.color(pal.Link).name(), + } + + class WebView(RestartingWebEngineView): cfi_changed = pyqtSignal(object) @@ -417,6 +427,7 @@ class WebView(RestartingWebEngineView): self.dead_renderer_error_shown = False self.render_process_failed.connect(self.render_process_died) w = QApplication.instance().desktop().availableGeometry(self).width() + QApplication.instance().palette_changed.connect(self.palette_changed) self.show_home_page_on_ready = True self._size_hint = QSize(int(w/3), int(w/2)) self._page = WebPage(self) @@ -508,9 +519,15 @@ class WebView(RestartingWebEngineView): def on_bridge_ready(self): f = QApplication.instance().font() fi = QFontInfo(f) + ui_data = { + 'all_font_families': QFontDatabase().families(), + 'ui_font_family': f.family(), + 'ui_font_sz': '{}px'.format(fi.pixelSize()), + 'show_home_page_on_ready': self.show_home_page_on_ready, + 'system_colors': system_colors(), + } self.bridge.create_view( - vprefs['session_data'], vprefs['local_storage'], QFontDatabase().families(), field_metadata.all_metadata(), - f.family(), '{}px'.format(fi.pixelSize()), self.show_home_page_on_ready) + vprefs['session_data'], vprefs['local_storage'], field_metadata.all_metadata(), ui_data) for func, args in iteritems(self.pending_bridge_ready_actions): getattr(self.bridge, func)(*args) @@ -592,3 +609,6 @@ class WebView(RestartingWebEngineView): def trigger_shortcut(self, which): self.execute_when_ready('trigger_shortcut', which) + + def palette_changed(self): + self.execute_when_ready('set_system_palette', system_colors()) diff --git a/src/pyj/read_book/globals.pyj b/src/pyj/read_book/globals.pyj index 0f43d38ba7..b906c67f31 100644 --- a/src/pyj/read_book/globals.pyj +++ b/src/pyj/read_book/globals.pyj @@ -48,17 +48,28 @@ def set_toc_anchor_map(val): toc_anchor_map.value = val default_color_schemes = { + 'system':{'foreground':'#000000', 'background':'#ffffff', 'name':_('System')}, 'white':{'foreground':'#000000', 'background':'#ffffff', 'name':_('White')}, 'black':{'foreground':'#ffffff', 'background':'#000000', 'link': '#4f81bd', 'name':_('Black')}, 'sepia-light':{'foreground':'#39322B', 'background':'#F6F3E9', 'name':_('Sepia light')}, 'sepia-dark': {'background':'#39322B', 'foreground':'#F6F3E9', 'link': '#4f81bd', 'name':_('Sepia dark')}, } + +def set_system_colors(spec): + c = default_color_schemes.system + c.foreground = spec.foreground + c.background = spec.background + c.link = spec.link + register_callback(def(): # Ensure the color scheme names are translated for key in default_color_schemes: scheme = default_color_schemes[key] scheme.name = gt(scheme.name) + # set the system colors if in dark mode + if window.matchMedia and window.matchMedia('(prefers-color-scheme: dark)').matches: + set_system_colors({'background': '#111', 'foreground': '#ddd', 'link': '#6cb4ee'}) ) runtime = { diff --git a/src/pyj/session.pyj b/src/pyj/session.pyj index edc4b888af..6d0bc72dd8 100644 --- a/src/pyj/session.pyj +++ b/src/pyj/session.pyj @@ -37,7 +37,7 @@ defaults = { 'background_image': None, 'background_image_style': 'scaled', 'background_image_fade': 0, - 'current_color_scheme': 'white', + 'current_color_scheme': 'system', 'user_color_schemes': {}, 'base_font_size': 16, 'controls_help_shown_count': 0, diff --git a/src/pyj/viewer-main.pyj b/src/pyj/viewer-main.pyj index 4cb4d15b3b..e7c149a3d7 100644 --- a/src/pyj/viewer-main.pyj +++ b/src/pyj/viewer-main.pyj @@ -16,7 +16,7 @@ from modals import create_modal_container from qt import from_python, to_python from read_book.db import new_book from read_book.footnotes import main as footnotes_main -from read_book.globals import runtime, ui_operations +from read_book.globals import runtime, ui_operations, set_system_colors from read_book.iframe import main as iframe_main from read_book.shortcuts import add_standalone_viewer_shortcuts from read_book.view import View @@ -214,19 +214,27 @@ def create_session_data(prefs, local_storage_data): @from_python -def create_view(prefs, local_storage, all_font_families, field_metadata, ui_font_family, ui_font_sz, show_home_page_on_ready): +def create_view(prefs, local_storage, field_metadata, ui_data): nonlocal view - runtime.all_font_families = all_font_families + set_system_colors(ui_data.system_colors) + runtime.all_font_families = ui_data.all_font_families library_data.field_metadata = field_metadata - document.documentElement.style.fontFamily = f'"{ui_font_family}", sans-serif' - document.documentElement.style.fontSize = ui_font_sz + document.documentElement.style.fontFamily = f'"{ui_data.ui_font_family}", sans-serif' + document.documentElement.style.fontSize = ui_data.ui_font_sz if view is None: create_session_data(prefs, local_storage) view = View(document.getElementById('view')) - if show_home_page_on_ready: + if ui_data.show_home_page_on_ready: view.overlay.open_book(False) +@from_python +def set_system_palette(system_colors): + set_system_colors(system_colors) + if view: + view.update_color_scheme() + + @from_python def show_home_page(): view.overlay.open_book(False)