From 31710ba335c52aae06eba3997846187f0fb11292 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Fri, 7 Aug 2020 12:43:53 +0530 Subject: [PATCH] DRYer --- src/pyj/book_list/details_list.pyj | 4 ++-- src/pyj/book_list/theme.pyj | 6 +++++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/pyj/book_list/details_list.pyj b/src/pyj/book_list/details_list.pyj index df5527fa35..5c814d8554 100644 --- a/src/pyj/book_list/details_list.pyj +++ b/src/pyj/book_list/details_list.pyj @@ -5,7 +5,7 @@ from __python__ import bound_methods, hash_literals from elementmaker import E from gettext import gettext as _ -from book_list.theme import css_for_variables, get_color +from book_list.theme import browser_in_dark_mode, css_for_variables, get_color from dom import build_rule, clear, set_css, svgicon from session import get_interface_data from utils import fmt_sidx, safe_set_inner_html, sandboxed_html @@ -66,7 +66,7 @@ def on_img_load(img, load_type): def sandbox_css(): - is_dark_theme = window.matchMedia('(prefers-color-scheme: dark)').matches + is_dark_theme = browser_in_dark_mode() if not sandbox_css.ans or sandbox_css.is_dark_theme is not is_dark_theme: sandbox_css.ans = css_for_variables() + '\n\n' sandbox_css.ans += 'html {{ overflow: hidden; color: {} }}'.format(get_color('window-foreground')) diff --git a/src/pyj/book_list/theme.pyj b/src/pyj/book_list/theme.pyj index 82d894e72c..0f2fd15506 100644 --- a/src/pyj/book_list/theme.pyj +++ b/src/pyj/book_list/theme.pyj @@ -74,9 +74,13 @@ def set_ui_colors(is_dark_theme): cached_color_to_rgba.cache = {} +def browser_in_dark_mode(): + return window.matchMedia('(prefers-color-scheme: dark)').matches + + def css_for_variables(): input_css = 'input, textarea { color: var(--calibre-color-input-foreground); background-color: var(--calibre-color-input-background); }' - is_dark_theme = window.matchMedia('(prefers-color-scheme: dark)').matches + is_dark_theme = browser_in_dark_mode() attr = 'dark' if is_dark_theme else 'light' ans = v'[]' for k in DEFAULT_COLORS: