mirror of
https://github.com/ankitects/anki.git
synced 2025-11-11 15:17:12 -05:00
Create dataclass for variables
This commit is contained in:
parent
63155b1361
commit
5724a004b9
3 changed files with 18 additions and 12 deletions
|
|
@ -14,6 +14,7 @@ from anki.collection import BrowserRow
|
||||||
from anki.notes import NoteId
|
from anki.notes import NoteId
|
||||||
from aqt import colors
|
from aqt import colors
|
||||||
from aqt.qt import QColor
|
from aqt.qt import QColor
|
||||||
|
from aqt.theme import AnkiVariable
|
||||||
from aqt.utils import tr
|
from aqt.utils import tr
|
||||||
|
|
||||||
Column = Columns.Column
|
Column = Columns.Column
|
||||||
|
|
@ -49,7 +50,7 @@ class CellRow:
|
||||||
) -> None:
|
) -> None:
|
||||||
self.refreshed_at: float = time.time()
|
self.refreshed_at: float = time.time()
|
||||||
self.cells: tuple[Cell, ...] = tuple(Cell(*cell) for cell in cells)
|
self.cells: tuple[Cell, ...] = tuple(Cell(*cell) for cell in cells)
|
||||||
self.color: dict[str, str] | None = backend_color_to_aqt_color(color)
|
self.color: AnkiVariable | None = backend_color_to_aqt_color(color)
|
||||||
self.font_name: str = font_name or "arial"
|
self.font_name: str = font_name or "arial"
|
||||||
self.font_size: int = font_size if font_size > 0 else 12
|
self.font_size: int = font_size if font_size > 0 else 12
|
||||||
|
|
||||||
|
|
@ -76,8 +77,8 @@ class CellRow:
|
||||||
return row
|
return row
|
||||||
|
|
||||||
|
|
||||||
def backend_color_to_aqt_color(color: BrowserRow.Color.V) -> dict[str, str] | None:
|
def backend_color_to_aqt_color(color: BrowserRow.Color.V) -> AnkiVariable | None:
|
||||||
temp_color = {}
|
temp_color = None
|
||||||
|
|
||||||
if color == BrowserRow.COLOR_MARKED:
|
if color == BrowserRow.COLOR_MARKED:
|
||||||
temp_color = colors.STATE_MARKED
|
temp_color = colors.STATE_MARKED
|
||||||
|
|
@ -101,8 +102,8 @@ def backend_color_to_aqt_color(color: BrowserRow.Color.V) -> dict[str, str] | No
|
||||||
return adjusted_bg_color(temp_color)
|
return adjusted_bg_color(temp_color)
|
||||||
|
|
||||||
|
|
||||||
def adjusted_bg_color(color: dict[str, str]) -> dict[str, str]:
|
def adjusted_bg_color(color: AnkiVariable) -> AnkiVariable:
|
||||||
if "light" in color and "dark" in color:
|
if color:
|
||||||
color.light = color.light.lighter(150).name()
|
color.light = color.light.lighter(150).name()
|
||||||
color.dark = color.dark.darker(150).name()
|
color.dark = color.dark.darker(150).name()
|
||||||
return color
|
return color
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@ from typing import cast
|
||||||
|
|
||||||
from aqt import colors
|
from aqt import colors
|
||||||
from aqt.qt import *
|
from aqt.qt import *
|
||||||
from aqt.theme import theme_manager
|
from aqt.theme import AnkiVariable, theme_manager
|
||||||
|
|
||||||
|
|
||||||
class Switch(QAbstractButton):
|
class Switch(QAbstractButton):
|
||||||
|
|
@ -19,8 +19,8 @@ class Switch(QAbstractButton):
|
||||||
radius: int = 10,
|
radius: int = 10,
|
||||||
left_label: str = "",
|
left_label: str = "",
|
||||||
right_label: str = "",
|
right_label: str = "",
|
||||||
left_color: dict[str, str] = colors.ACCENT_CARD,
|
left_color: AnkiVariable = colors.ACCENT_CARD,
|
||||||
right_color: dict[str, str] = colors.ACCENT_NOTE,
|
right_color: AnkiVariable = colors.ACCENT_NOTE,
|
||||||
parent: QWidget = None,
|
parent: QWidget = None,
|
||||||
) -> None:
|
) -> None:
|
||||||
super().__init__(parent=parent)
|
super().__init__(parent=parent)
|
||||||
|
|
|
||||||
|
|
@ -26,11 +26,16 @@ from aqt.qt import (
|
||||||
Qt,
|
Qt,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class AnkiVariable:
|
||||||
|
light: str
|
||||||
|
dark: str
|
||||||
|
comment: str
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class ColoredIcon:
|
class ColoredIcon:
|
||||||
path: str
|
path: str
|
||||||
color: dict[str, str]
|
color: AnkiVariable
|
||||||
|
|
||||||
def current_color(self, night_mode: bool) -> str:
|
def current_color(self, night_mode: bool) -> str:
|
||||||
if night_mode:
|
if night_mode:
|
||||||
|
|
@ -38,7 +43,7 @@ class ColoredIcon:
|
||||||
else:
|
else:
|
||||||
return self.color.get("light", "")
|
return self.color.get("light", "")
|
||||||
|
|
||||||
def with_color(self, color: dict[str, str]) -> ColoredIcon:
|
def with_color(self, color: AnkiVariable) -> ColoredIcon:
|
||||||
return ColoredIcon(path=self.path, color=color)
|
return ColoredIcon(path=self.path, color=color)
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -176,11 +181,11 @@ 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 var(self, vars: dict[str, str]) -> str:
|
def var(self, vars: AnkiVariable) -> str:
|
||||||
"""Given day/night colors/props, return the correct one for the current theme."""
|
"""Given day/night colors/props, return the correct one for the current theme."""
|
||||||
return vars["dark" if self.night_mode else "light"]
|
return vars["dark" if self.night_mode else "light"]
|
||||||
|
|
||||||
def qcolor(self, colors: dict[str, str]) -> QColor:
|
def qcolor(self, colors: AnkiVariable) -> QColor:
|
||||||
return QColor(self.var(colors))
|
return QColor(self.var(colors))
|
||||||
|
|
||||||
def _determine_night_mode(self) -> bool:
|
def _determine_night_mode(self) -> bool:
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue