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.
This commit is contained in:
Kovid Goyal 2019-11-05 15:07:52 +05:30
parent c46a5f8127
commit 0dc03ed11e
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
4 changed files with 48 additions and 9 deletions

View File

@ -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())

View File

@ -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 = {

View File

@ -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,

View File

@ -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)