2020-01-23 06:08:10 +01:00
|
|
|
# Copyright: Ankitects Pty Ltd and contributors
|
|
|
|
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
|
|
|
2021-02-05 06:26:12 +01:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2020-01-23 06:08:10 +01:00
|
|
|
import platform
|
2021-02-05 06:26:12 +01:00
|
|
|
from dataclasses import dataclass
|
|
|
|
from typing import Dict, Optional, Tuple, Union
|
2020-01-23 06:08:10 +01:00
|
|
|
|
2020-01-23 09:05:55 +01:00
|
|
|
from anki.utils import isMac
|
2021-02-05 06:26:12 +01:00
|
|
|
from aqt import QApplication, colors, gui_hooks, isWin
|
2021-02-04 10:07:34 +01:00
|
|
|
from aqt.platform import set_dark_mode
|
2021-02-05 06:26:12 +01:00
|
|
|
from aqt.qt import QColor, QIcon, QPainter, QPalette, QPixmap, QStyleFactory, Qt
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class ColoredIcon:
|
|
|
|
path: str
|
|
|
|
# (day, night)
|
|
|
|
color: Tuple[str, str]
|
|
|
|
|
|
|
|
def current_color(self, night_mode: bool) -> str:
|
|
|
|
if night_mode:
|
|
|
|
return self.color[1]
|
|
|
|
else:
|
|
|
|
return self.color[0]
|
|
|
|
|
|
|
|
def with_color(self, color: Tuple[str, str]) -> ColoredIcon:
|
|
|
|
return ColoredIcon(path=self.path, color=color)
|
2020-01-23 06:08:10 +01:00
|
|
|
|
|
|
|
|
|
|
|
class ThemeManager:
|
2020-01-31 04:14:16 +01:00
|
|
|
_night_mode_preference = False
|
2020-01-31 04:30:12 +01:00
|
|
|
_icon_cache_light: Dict[str, QIcon] = {}
|
|
|
|
_icon_cache_dark: Dict[str, QIcon] = {}
|
2020-01-23 06:08:10 +01:00
|
|
|
_icon_size = 128
|
2021-02-04 10:07:34 +01:00
|
|
|
_dark_mode_available: Optional[bool] = None
|
2020-01-23 06:08:10 +01:00
|
|
|
|
2020-01-31 04:14:16 +01:00
|
|
|
def macos_dark_mode(self) -> bool:
|
2020-04-15 13:37:16 +02:00
|
|
|
"True if the user has night mode on, and has forced native widgets."
|
2020-01-31 05:12:37 +01:00
|
|
|
if not isMac:
|
|
|
|
return False
|
|
|
|
|
2021-02-04 10:07:34 +01:00
|
|
|
if not self._night_mode_preference:
|
|
|
|
return False
|
|
|
|
|
|
|
|
if self._dark_mode_available is None:
|
|
|
|
self._dark_mode_available = set_dark_mode(True)
|
|
|
|
|
2020-04-15 13:37:16 +02:00
|
|
|
from aqt import mw
|
|
|
|
|
2021-02-04 10:07:34 +01:00
|
|
|
return self._dark_mode_available and mw.pm.dark_mode_widgets()
|
2020-01-31 04:14:16 +01:00
|
|
|
|
|
|
|
def get_night_mode(self) -> bool:
|
2020-04-15 13:37:16 +02:00
|
|
|
return self._night_mode_preference
|
2020-01-31 04:14:16 +01:00
|
|
|
|
|
|
|
def set_night_mode(self, val: bool) -> None:
|
|
|
|
self._night_mode_preference = val
|
2020-02-07 04:19:41 +01:00
|
|
|
self._update_stat_colors()
|
2020-01-31 04:14:16 +01:00
|
|
|
|
|
|
|
night_mode = property(get_night_mode, set_night_mode)
|
|
|
|
|
2021-02-05 06:26:12 +01:00
|
|
|
def icon_from_resources(self, path: Union[str, ColoredIcon]) -> QIcon:
|
2020-01-23 06:08:10 +01:00
|
|
|
"Fetch icon from Qt resources, and invert if in night mode."
|
2020-01-31 04:30:12 +01:00
|
|
|
if self.night_mode:
|
|
|
|
cache = self._icon_cache_light
|
|
|
|
else:
|
|
|
|
cache = self._icon_cache_dark
|
2021-02-05 06:26:12 +01:00
|
|
|
|
|
|
|
if isinstance(path, str):
|
|
|
|
key = path
|
|
|
|
else:
|
|
|
|
key = f"{path.path}-{path.color}"
|
|
|
|
|
|
|
|
icon = cache.get(key)
|
2020-01-23 06:08:10 +01:00
|
|
|
if icon:
|
|
|
|
return icon
|
|
|
|
|
2021-02-05 06:26:12 +01:00
|
|
|
if isinstance(path, str):
|
|
|
|
# default black/white
|
|
|
|
icon = QIcon(path)
|
|
|
|
if self.night_mode:
|
|
|
|
img = icon.pixmap(self._icon_size, self._icon_size).toImage()
|
|
|
|
img.invertPixels()
|
|
|
|
icon = QIcon(QPixmap(img))
|
|
|
|
else:
|
|
|
|
# specified colours
|
|
|
|
icon = QIcon(path.path)
|
|
|
|
img = icon.pixmap(16)
|
|
|
|
painter = QPainter(img)
|
|
|
|
painter.setCompositionMode(QPainter.CompositionMode_SourceIn)
|
|
|
|
painter.fillRect(img.rect(), QColor(path.current_color(self.night_mode)))
|
|
|
|
painter.end()
|
|
|
|
icon = QIcon(img)
|
|
|
|
return icon
|
2020-01-23 06:08:10 +01:00
|
|
|
|
2020-01-31 04:30:12 +01:00
|
|
|
return cache.setdefault(path, icon)
|
2020-01-23 06:08:10 +01:00
|
|
|
|
2020-07-31 06:47:17 +02:00
|
|
|
def body_class(self, night_mode: Optional[bool] = None) -> str:
|
2020-01-23 09:05:55 +01:00
|
|
|
"Returns space-separated class list for platform/theme."
|
|
|
|
classes = []
|
|
|
|
if isWin:
|
|
|
|
classes.append("isWin")
|
|
|
|
elif isMac:
|
|
|
|
classes.append("isMac")
|
|
|
|
else:
|
|
|
|
classes.append("isLin")
|
2020-07-31 06:47:17 +02:00
|
|
|
|
|
|
|
if night_mode is None:
|
|
|
|
night_mode = self.night_mode
|
|
|
|
if night_mode:
|
2020-01-30 22:00:01 +01:00
|
|
|
classes.extend(["nightMode", "night_mode"])
|
2020-01-31 04:39:52 +01:00
|
|
|
if self.macos_dark_mode():
|
|
|
|
classes.append("macos-dark-mode")
|
2020-01-23 09:05:55 +01:00
|
|
|
return " ".join(classes)
|
2020-01-23 06:08:10 +01:00
|
|
|
|
2020-07-31 06:47:17 +02:00
|
|
|
def body_classes_for_card_ord(
|
|
|
|
self, card_ord: int, night_mode: Optional[bool] = None
|
|
|
|
) -> str:
|
2020-01-23 06:08:10 +01:00
|
|
|
"Returns body classes used when showing a card."
|
2020-07-31 06:47:17 +02:00
|
|
|
return f"card card{card_ord+1} {self.body_class(night_mode)}"
|
2020-01-23 06:08:10 +01:00
|
|
|
|
2021-02-05 09:50:01 +01:00
|
|
|
def color(self, colors: Tuple[str, str]) -> str:
|
|
|
|
"""Given day/night colors, return the correct one for the current theme."""
|
2020-08-27 08:12:30 +02:00
|
|
|
idx = 1 if self.night_mode else 0
|
2021-02-05 09:50:01 +01:00
|
|
|
return colors[idx]
|
2021-02-05 06:26:12 +01:00
|
|
|
|
2021-02-05 09:50:01 +01:00
|
|
|
def qcolor(self, colors: Tuple[str, str]) -> QColor:
|
|
|
|
return QColor(self.color(colors))
|
2020-01-23 06:08:10 +01:00
|
|
|
|
|
|
|
def apply_style(self, app: QApplication) -> None:
|
|
|
|
self._apply_palette(app)
|
|
|
|
self._apply_style(app)
|
|
|
|
|
|
|
|
def _apply_style(self, app: QApplication) -> None:
|
|
|
|
buf = ""
|
|
|
|
|
|
|
|
if isWin and platform.release() == "10" and not self.night_mode:
|
|
|
|
# add missing bottom border to menubar
|
|
|
|
buf += """
|
|
|
|
QMenuBar {
|
|
|
|
border-bottom: 1px solid #aaa;
|
|
|
|
background: white;
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
# qt bug? setting the above changes the browser sidebar
|
|
|
|
# to white as well, so set it back
|
|
|
|
buf += """
|
|
|
|
QTreeWidget {
|
|
|
|
background: #eee;
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
2020-01-23 22:55:14 +01:00
|
|
|
if self.night_mode:
|
|
|
|
buf += """
|
|
|
|
QToolTip {
|
|
|
|
border: 0;
|
2020-01-26 00:13:38 +01:00
|
|
|
}
|
2020-01-23 22:55:14 +01:00
|
|
|
"""
|
|
|
|
|
2020-02-02 03:22:10 +01:00
|
|
|
if not self.macos_dark_mode():
|
|
|
|
buf += """
|
|
|
|
QScrollBar { background-color: %s; }
|
|
|
|
QScrollBar::handle { background-color: %s; border-radius: 5px; }
|
|
|
|
|
|
|
|
QScrollBar:horizontal { height: 12px; }
|
|
|
|
QScrollBar::handle:horizontal { min-width: 50px; }
|
|
|
|
|
|
|
|
QScrollBar:vertical { width: 12px; }
|
|
|
|
QScrollBar::handle:vertical { min-height: 50px; }
|
|
|
|
|
|
|
|
QScrollBar::add-line {
|
|
|
|
border: none;
|
|
|
|
background: none;
|
|
|
|
}
|
|
|
|
|
|
|
|
QScrollBar::sub-line {
|
|
|
|
border: none;
|
|
|
|
background: none;
|
|
|
|
}
|
2020-02-02 03:53:35 +01:00
|
|
|
|
|
|
|
QTabWidget { background-color: %s; }
|
2020-02-02 04:09:02 +01:00
|
|
|
""" % (
|
2021-02-05 09:50:01 +01:00
|
|
|
self.color(colors.WINDOW_BG),
|
2020-10-06 06:35:21 +02:00
|
|
|
# fushion-button-hover-bg
|
|
|
|
"#656565",
|
2021-02-05 09:50:01 +01:00
|
|
|
self.color(colors.WINDOW_BG),
|
2020-02-02 04:09:02 +01:00
|
|
|
)
|
2020-02-02 03:22:10 +01:00
|
|
|
|
2020-01-23 06:08:10 +01:00
|
|
|
# allow addons to modify the styling
|
|
|
|
buf = gui_hooks.style_did_init(buf)
|
|
|
|
|
|
|
|
app.setStyleSheet(buf)
|
|
|
|
|
|
|
|
def _apply_palette(self, app: QApplication) -> None:
|
|
|
|
if not self.night_mode:
|
|
|
|
return
|
|
|
|
|
2020-01-31 04:14:16 +01:00
|
|
|
if not self.macos_dark_mode():
|
|
|
|
app.setStyle(QStyleFactory.create("fusion")) # type: ignore
|
2020-01-23 06:08:10 +01:00
|
|
|
|
|
|
|
palette = QPalette()
|
|
|
|
|
2021-02-05 09:50:01 +01:00
|
|
|
text_fg = self.qcolor(colors.TEXT_FG)
|
2020-01-23 06:08:10 +01:00
|
|
|
palette.setColor(QPalette.WindowText, text_fg)
|
|
|
|
palette.setColor(QPalette.ToolTipText, text_fg)
|
|
|
|
palette.setColor(QPalette.Text, text_fg)
|
|
|
|
palette.setColor(QPalette.ButtonText, text_fg)
|
|
|
|
|
2021-02-05 09:50:01 +01:00
|
|
|
hlbg = self.qcolor(colors.HIGHLIGHT_BG)
|
2020-01-23 06:08:10 +01:00
|
|
|
hlbg.setAlpha(64)
|
2021-02-05 09:50:01 +01:00
|
|
|
palette.setColor(QPalette.HighlightedText, self.qcolor(colors.HIGHLIGHT_FG))
|
2020-01-23 06:08:10 +01:00
|
|
|
palette.setColor(QPalette.Highlight, hlbg)
|
|
|
|
|
2021-02-05 09:50:01 +01:00
|
|
|
window_bg = self.qcolor(colors.WINDOW_BG)
|
2020-01-23 06:08:10 +01:00
|
|
|
palette.setColor(QPalette.Window, window_bg)
|
|
|
|
palette.setColor(QPalette.AlternateBase, window_bg)
|
2020-02-02 03:53:35 +01:00
|
|
|
|
2020-08-27 07:57:24 +02:00
|
|
|
palette.setColor(QPalette.Button, QColor("#454545"))
|
2020-01-23 06:08:10 +01:00
|
|
|
|
2021-02-05 09:50:01 +01:00
|
|
|
frame_bg = self.qcolor(colors.FRAME_BG)
|
2020-01-23 22:55:14 +01:00
|
|
|
palette.setColor(QPalette.Base, frame_bg)
|
|
|
|
palette.setColor(QPalette.ToolTipBase, frame_bg)
|
2020-01-23 06:08:10 +01:00
|
|
|
|
2021-02-05 09:50:01 +01:00
|
|
|
disabled_color = self.qcolor(colors.DISABLED)
|
2020-01-23 06:08:10 +01:00
|
|
|
palette.setColor(QPalette.Disabled, QPalette.Text, disabled_color)
|
|
|
|
palette.setColor(QPalette.Disabled, QPalette.ButtonText, disabled_color)
|
|
|
|
palette.setColor(QPalette.Disabled, QPalette.HighlightedText, disabled_color)
|
|
|
|
|
2021-02-05 09:50:01 +01:00
|
|
|
palette.setColor(QPalette.Link, self.qcolor(colors.LINK))
|
2020-01-23 06:08:10 +01:00
|
|
|
|
|
|
|
palette.setColor(QPalette.BrightText, Qt.red)
|
|
|
|
|
|
|
|
app.setPalette(palette)
|
|
|
|
|
2020-02-27 03:11:00 +01:00
|
|
|
def _update_stat_colors(self) -> None:
|
2020-02-07 04:19:41 +01:00
|
|
|
import anki.stats as s
|
2020-02-07 08:55:26 +01:00
|
|
|
|
2021-02-05 09:50:01 +01:00
|
|
|
s.colLearn = self.color(colors.NEW_COUNT)
|
|
|
|
s.colRelearn = self.color(colors.LEARN_COUNT)
|
|
|
|
s.colCram = self.color(colors.SUSPENDED_BG)
|
|
|
|
s.colSusp = self.color(colors.SUSPENDED_BG)
|
|
|
|
s.colMature = self.color(colors.REVIEW_COUNT)
|
2020-02-07 04:19:41 +01:00
|
|
|
|
2020-01-23 06:08:10 +01:00
|
|
|
|
|
|
|
theme_manager = ThemeManager()
|