Set --window-bg of day/night mode to proper colors

including the following changes:
- rename "_getWindowColor" to "get_window_bg_color" to follow PEP8
- add "night_mode" argument to "get_window_bg_color"
- make it possible to get the correct colors regardless of the current theme
This commit is contained in:
hikaru-y 2021-06-20 03:04:24 +09:00
parent fa0e3540eb
commit 4bff3f4854

View File

@ -215,7 +215,10 @@ class AnkiWebView(QWebEngineView):
QWebEngineView.__init__(self, parent=parent) QWebEngineView.__init__(self, parent=parent)
self.set_title(title) self.set_title(title)
self._page = AnkiWebPage(self._onBridgeCmd) self._page = AnkiWebPage(self._onBridgeCmd)
self._page.setBackgroundColor(self._getWindowColor()) # reduce flicker # reduce flicker
self._page.setBackgroundColor(
self.get_window_bg_color(theme_manager.night_mode)
)
# in new code, use .set_bridge_command() instead of setting this directly # in new code, use .set_bridge_command() instead of setting this directly
self.onBridgeCmd: Callable[[str], Any] = self.defaultOnBridgeCmd self.onBridgeCmd: Callable[[str], Any] = self.defaultOnBridgeCmd
@ -388,16 +391,17 @@ class AnkiWebView(QWebEngineView):
else: else:
return 3 return 3
def _getWindowColor(self) -> QColor: def get_window_bg_color(self, night_mode: bool) -> QColor:
if theme_manager.night_mode: if night_mode:
return theme_manager.qcolor(colors.WINDOW_BG) return QColor(colors.WINDOW_BG[1])
if isMac: elif isMac:
# standard palette does not return correct window color on macOS # standard palette does not return correct window color on macOS
return QColor("#ececec") return QColor("#ececec")
return self.style().standardPalette().color(QPalette.Window) else:
return theme_manager.default_palette.color(QPalette.Window)
def standard_css(self) -> str: def standard_css(self) -> str:
palette = self.style().standardPalette() palette = theme_manager.default_palette
color_hl = palette.color(QPalette.Highlight).name() color_hl = palette.color(QPalette.Highlight).name()
if isWin: if isWin:
@ -437,7 +441,10 @@ div[contenteditable="true"]:focus {
} }
zoom = self.zoomFactor() zoom = self.zoomFactor()
background = self._getWindowColor().name()
window_bg_day = self.get_window_bg_color(False).name()
window_bg_night = self.get_window_bg_color(True).name()
body_bg = window_bg_night if theme_manager.night_mode else window_bg_day
if is_rtl(anki.lang.currentLang): if is_rtl(anki.lang.currentLang):
lang_dir = "rtl" lang_dir = "rtl"
@ -445,11 +452,11 @@ div[contenteditable="true"]:focus {
lang_dir = "ltr" lang_dir = "ltr"
return f""" return f"""
body {{ zoom: {zoom}; background: {background}; direction: {lang_dir}; }} body {{ zoom: {zoom}; background-color: {body_bg}; direction: {lang_dir}; }}
html {{ {font} }} html {{ {font} }}
{button_style} {button_style}
:root {{ --window-bg: {background} }} :root {{ --window-bg: {window_bg_day} }}
:root[class*=night-mode] {{ --window-bg: {background} }} :root[class*=night-mode] {{ --window-bg: {window_bg_night} }}
""" """
def stdHtml( def stdHtml(