use constants for other color references

str_color/qcolor() doesn't appear to have been used by any add-ons
except one of mine, so changing the signature should be safe
This commit is contained in:
Damien Elmes 2021-02-05 18:50:01 +10:00
parent b8d67cdad5
commit bb30e8f7bc
6 changed files with 32 additions and 41 deletions

View file

@ -21,7 +21,7 @@ from anki.models import NoteType
from anki.notes import Note from anki.notes import Note
from anki.stats import CardStats from anki.stats import CardStats
from anki.utils import htmlToTextLine, ids2str, isMac, isWin from anki.utils import htmlToTextLine, ids2str, isMac, isWin
from aqt import AnkiQt, gui_hooks from aqt import AnkiQt, colors, gui_hooks
from aqt.editor import Editor from aqt.editor import Editor
from aqt.exporting import ExportDialog from aqt.exporting import ExportDialog
from aqt.main import ResetReason from aqt.main import ResetReason
@ -412,13 +412,13 @@ class StatusDelegate(QItemDelegate):
col = None col = None
if c.userFlag() > 0: if c.userFlag() > 0:
col = theme_manager.qcolor(f"flag{c.userFlag()}-bg") col = getattr(colors, f"FLAG{c.userFlag()}_BG")
elif c.note().hasTag("Marked"): elif c.note().hasTag("Marked"):
col = theme_manager.qcolor("marked-bg") col = colors.MARKED_BG
elif c.queue == QUEUE_TYPE_SUSPENDED: elif c.queue == QUEUE_TYPE_SUSPENDED:
col = theme_manager.qcolor("suspended-bg") col = colors.SUSPENDED_BG
if col: if col:
brush = QBrush(col) brush = QBrush(theme_manager.qcolor(col))
painter.save() painter.save()
painter.fillRect(option.rect, brush) painter.fillRect(option.rect, brush)
painter.restore() painter.restore()
@ -744,7 +744,7 @@ class Browser(QMainWindow):
"selection-color: black; }" "selection-color: black; }"
) )
elif theme_manager.macos_dark_mode(): elif theme_manager.macos_dark_mode():
grid = theme_manager.str_color("frame-bg") grid = colors.FRAME_BG
self.form.tableView.setStyleSheet( self.form.tableView.setStyleSheet(
f""" f"""
QTableView {{ gridline-color: {grid} }} QTableView {{ gridline-color: {grid} }}

View file

@ -8,7 +8,7 @@ from anki.collection import SearchTerm
from anki.decks import Deck, DeckRenameError from anki.decks import Deck, DeckRenameError
from anki.errors import InvalidInput from anki.errors import InvalidInput
from anki.lang import without_unicode_isolation from anki.lang import without_unicode_isolation
from aqt import AnkiQt, gui_hooks from aqt import AnkiQt, colors, gui_hooks
from aqt.qt import * from aqt.qt import *
from aqt.theme import theme_manager from aqt.theme import theme_manager
from aqt.utils import ( from aqt.utils import (
@ -70,7 +70,7 @@ class DeckConf(QDialog):
self.set_custom_searches(search, search_2) self.set_custom_searches(search, search_2)
qconnect(self.form.search_button.clicked, self.on_search_button) qconnect(self.form.search_button.clicked, self.on_search_button)
qconnect(self.form.search_button_2.clicked, self.on_search_button_2) qconnect(self.form.search_button_2.clicked, self.on_search_button_2)
color = theme_manager.str_color("link") color = theme_manager.color(colors.LINK)
self.setStyleSheet( self.setStyleSheet(
f"""QPushButton[flat=true] {{ text-align: left; color: {color}; padding: 0; border: 0 }} f"""QPushButton[flat=true] {{ text-align: left; color: {color}; padding: 0; border: 0 }}
QPushButton[flat=true]:hover {{ text-decoration: underline }}""" QPushButton[flat=true]:hover {{ text-decoration: underline }}"""

View file

@ -27,7 +27,7 @@ from anki.hooks import runFilter
from anki.httpclient import HttpClient from anki.httpclient import HttpClient
from anki.notes import Note from anki.notes import Note
from anki.utils import checksum, isLin, isWin, namedtmp from anki.utils import checksum, isLin, isWin, namedtmp
from aqt import AnkiQt, gui_hooks from aqt import AnkiQt, colors, gui_hooks
from aqt.main import ResetReason from aqt.main import ResetReason
from aqt.qt import * from aqt.qt import *
from aqt.sound import av_player from aqt.sound import av_player
@ -629,7 +629,7 @@ class Editor:
self.tags.setToolTip( self.tags.setToolTip(
shortcut(tr(TR.EDITING_JUMP_TO_TAGS_WITH_CTRLANDSHIFTANDT)) shortcut(tr(TR.EDITING_JUMP_TO_TAGS_WITH_CTRLANDSHIFTANDT))
) )
border = theme_manager.str_color("border") border = theme_manager.color(colors.BORDER)
self.tags.setStyleSheet(f"border: 1px solid {border}") self.tags.setStyleSheet(f"border: 1px solid {border}")
tb.addWidget(self.tags, 1, 1) tb.addWidget(self.tags, 1, 1)
g.setLayout(tb) g.setLayout(tb)

View file

@ -372,7 +372,7 @@ class SidebarTreeView(QTreeView):
) -> None: ) -> None:
if self.current_search and (item := self.model().item_for_index(idx)): if self.current_search and (item := self.model().item_for_index(idx)):
if item.is_highlighted(): if item.is_highlighted():
brush = QBrush(theme_manager.qcolor("suspended-bg")) brush = QBrush(theme_manager.qcolor(colors.SUSPENDED_BG))
painter.save() painter.save()
painter.fillRect(options.rect, brush) painter.fillRect(options.rect, brush)
painter.restore() painter.restore()

View file

@ -121,22 +121,13 @@ class ThemeManager:
"Returns body classes used when showing a card." "Returns body classes used when showing a card."
return f"card card{card_ord+1} {self.body_class(night_mode)}" return f"card card{card_ord+1} {self.body_class(night_mode)}"
def str_color(self, key: str) -> str: def color(self, colors: Tuple[str, str]) -> str:
"""Get a color defined in _vars.scss """Given day/night colors, return the correct one for the current theme."""
If the colour is called '--frame-bg', key should be
'frame-bg'.
Returns the color as a string hex code or color name."""
idx = 1 if self.night_mode else 0 idx = 1 if self.night_mode else 0
return colors[idx]
key = key.replace("-", "_").upper() def qcolor(self, colors: Tuple[str, str]) -> QColor:
return QColor(self.color(colors))
return getattr(colors, key)[idx]
def qcolor(self, key: str) -> QColor:
"""Get a color defined in _vars.scss as a QColor."""
return QColor(self.str_color(key))
def apply_style(self, app: QApplication) -> None: def apply_style(self, app: QApplication) -> None:
self._apply_palette(app) self._apply_palette(app)
@ -191,10 +182,10 @@ QScrollBar::sub-line {
QTabWidget { background-color: %s; } QTabWidget { background-color: %s; }
""" % ( """ % (
self.str_color("window-bg"), self.color(colors.WINDOW_BG),
# fushion-button-hover-bg # fushion-button-hover-bg
"#656565", "#656565",
self.str_color("window-bg"), self.color(colors.WINDOW_BG),
) )
# allow addons to modify the styling # allow addons to modify the styling
@ -211,33 +202,33 @@ QTabWidget { background-color: %s; }
palette = QPalette() palette = QPalette()
text_fg = self.qcolor("text-fg") text_fg = self.qcolor(colors.TEXT_FG)
palette.setColor(QPalette.WindowText, text_fg) palette.setColor(QPalette.WindowText, text_fg)
palette.setColor(QPalette.ToolTipText, text_fg) palette.setColor(QPalette.ToolTipText, text_fg)
palette.setColor(QPalette.Text, text_fg) palette.setColor(QPalette.Text, text_fg)
palette.setColor(QPalette.ButtonText, text_fg) palette.setColor(QPalette.ButtonText, text_fg)
hlbg = self.qcolor("highlight-bg") hlbg = self.qcolor(colors.HIGHLIGHT_BG)
hlbg.setAlpha(64) hlbg.setAlpha(64)
palette.setColor(QPalette.HighlightedText, self.qcolor("highlight-fg")) palette.setColor(QPalette.HighlightedText, self.qcolor(colors.HIGHLIGHT_FG))
palette.setColor(QPalette.Highlight, hlbg) palette.setColor(QPalette.Highlight, hlbg)
window_bg = self.qcolor("window-bg") window_bg = self.qcolor(colors.WINDOW_BG)
palette.setColor(QPalette.Window, window_bg) palette.setColor(QPalette.Window, window_bg)
palette.setColor(QPalette.AlternateBase, window_bg) palette.setColor(QPalette.AlternateBase, window_bg)
palette.setColor(QPalette.Button, QColor("#454545")) palette.setColor(QPalette.Button, QColor("#454545"))
frame_bg = self.qcolor("frame-bg") frame_bg = self.qcolor(colors.FRAME_BG)
palette.setColor(QPalette.Base, frame_bg) palette.setColor(QPalette.Base, frame_bg)
palette.setColor(QPalette.ToolTipBase, frame_bg) palette.setColor(QPalette.ToolTipBase, frame_bg)
disabled_color = self.qcolor("disabled") disabled_color = self.qcolor(colors.DISABLED)
palette.setColor(QPalette.Disabled, QPalette.Text, disabled_color) palette.setColor(QPalette.Disabled, QPalette.Text, disabled_color)
palette.setColor(QPalette.Disabled, QPalette.ButtonText, disabled_color) palette.setColor(QPalette.Disabled, QPalette.ButtonText, disabled_color)
palette.setColor(QPalette.Disabled, QPalette.HighlightedText, disabled_color) palette.setColor(QPalette.Disabled, QPalette.HighlightedText, disabled_color)
palette.setColor(QPalette.Link, self.qcolor("link")) palette.setColor(QPalette.Link, self.qcolor(colors.LINK))
palette.setColor(QPalette.BrightText, Qt.red) palette.setColor(QPalette.BrightText, Qt.red)
@ -246,11 +237,11 @@ QTabWidget { background-color: %s; }
def _update_stat_colors(self) -> None: def _update_stat_colors(self) -> None:
import anki.stats as s import anki.stats as s
s.colLearn = self.str_color("new-count") s.colLearn = self.color(colors.NEW_COUNT)
s.colRelearn = self.str_color("learn-count") s.colRelearn = self.color(colors.LEARN_COUNT)
s.colCram = self.str_color("suspended-bg") s.colCram = self.color(colors.SUSPENDED_BG)
s.colSusp = self.str_color("suspended-bg") s.colSusp = self.color(colors.SUSPENDED_BG)
s.colMature = self.str_color("review-count") s.colMature = self.color(colors.REVIEW_COUNT)
theme_manager = ThemeManager() theme_manager = ThemeManager()

View file

@ -10,7 +10,7 @@ from typing import Any, Callable, List, Optional, Sequence, Tuple, cast
import anki import anki
from anki.lang import is_rtl from anki.lang import is_rtl
from anki.utils import isLin, isMac, isWin from anki.utils import isLin, isMac, isWin
from aqt import gui_hooks from aqt import colors, gui_hooks
from aqt.qt import * from aqt.qt import *
from aqt.theme import theme_manager from aqt.theme import theme_manager
from aqt.utils import TR, openLink, showInfo, tr from aqt.utils import TR, openLink, showInfo, tr
@ -378,7 +378,7 @@ class AnkiWebView(QWebEngineView):
def _getWindowColor(self) -> QColor: def _getWindowColor(self) -> QColor:
if theme_manager.night_mode: if theme_manager.night_mode:
return theme_manager.qcolor("window-bg") return theme_manager.qcolor(colors.WINDOW_BG)
if isMac: if 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")