impr(ui.theme): convert CustomPalette to dataclass

This commit is contained in:
2025-11-15 13:20:18 +08:00
parent 0966a3eb40
commit 71e9f05632
4 changed files with 52 additions and 61 deletions

View File

@ -8,7 +8,7 @@ from PySide6.QtGui import QGuiApplication, QPalette
from .material3 import Material3DynamicThemeImpl, Material3ThemeImpl
from .qml import ThemeQmlExposer
from .shared import ThemeImpl, ThemeInfo, TThemeInfoCacheKey, _TCustomPalette, _TScheme
from .shared import CustomPalette, ThemeImpl, ThemeInfo, TThemeInfoCacheKey, _TScheme
QML_IMPORT_NAME = "internal.ui.theme"
QML_IMPORT_MAJOR_VERSION = 1
@ -27,7 +27,7 @@ class ThemeManager(QObject):
super().__init__(parent)
self._qPalette = QPalette()
self._customPalette: _TCustomPalette = ThemeImpl.DEFAULT_CUSTOM_PALETTE
self._customPalette: CustomPalette = ThemeImpl.DEFAULT_CUSTOM_PALETTE
self._lastThemeInfo = ThemeImpl().info
self._qmlExposer = ThemeQmlExposer(themeImpl=ThemeImpl())
@ -135,7 +135,7 @@ class ThemeManager(QObject):
return self._qPalette
@Property(dict, notify=themeChanged)
def customPalette(self) -> _TCustomPalette:
def customPalette(self) -> CustomPalette:
return self._customPalette
@Property(ThemeQmlExposer, notify=themeChanged)

View File

@ -7,7 +7,7 @@ from materialyoucolor.palettes.tonal_palette import TonalPalette
from materialyoucolor.scheme.scheme_tonal_spot import SchemeTonalSpot
from PySide6.QtGui import QColor, QPalette
from .shared import ThemeImpl, ThemeInfo, _TCustomPalette, _TScheme
from .shared import CustomPalette, ThemeImpl, ThemeInfo, _TScheme
from .types import (
TMaterial3DynamicThemeData,
TMaterial3ThemeData,
@ -84,7 +84,7 @@ class Material3ThemeImpl(ThemeImpl):
return qPalette
@property
def customPalette(self) -> _TCustomPalette:
def customPalette(self) -> CustomPalette:
primaryHct = _hexToHct(self.themeData["schemes"][self.scheme]["primary"])
secondaryHct = _hexToHct(self.themeData["schemes"][self.scheme]["secondary"])
tertiaryHct = _hexToHct(self.themeData["schemes"][self.scheme]["tertiary"])
@ -98,16 +98,15 @@ class Material3ThemeImpl(ThemeImpl):
Blend.harmonize(successHct.to_int(), primaryHct.to_int())
)
return {
**ThemeImpl.DEFAULT_CUSTOM_PALETTE,
"primary": _hctToQColor(primaryHct),
"secondary": _hctToQColor(secondaryHct),
"tertiary": _hctToQColor(tertiaryHct),
"success": _hctToQColor(successHarmonizedHct),
"error": QColor.fromString(self.themeData["schemes"][self.scheme]["error"]),
"toolTipBase": self.qPalette.color(QPalette.ColorRole.ToolTipBase),
"toolTipText": self.qPalette.color(QPalette.ColorRole.ToolTipText),
}
return CustomPalette(
primary=_hctToQColor(primaryHct),
secondary=_hctToQColor(secondaryHct),
tertiary=_hctToQColor(tertiaryHct),
success=_hctToQColor(successHarmonizedHct),
error=QColor.fromString(self.themeData["schemes"][self.scheme]["error"]),
toolTipBase=self.qPalette.color(QPalette.ColorRole.ToolTipBase),
toolTipText=self.qPalette.color(QPalette.ColorRole.ToolTipText),
)
class Material3DynamicThemeImpl(ThemeImpl):
@ -198,7 +197,7 @@ class Material3DynamicThemeImpl(ThemeImpl):
return qPalette
@property
def customPalette(self) -> _TCustomPalette:
def customPalette(self) -> CustomPalette:
primaryHct = MaterialDynamicColors.primary.get_hct(self.material3Scheme)
secondaryHct = MaterialDynamicColors.secondary.get_hct(self.material3Scheme)
tertiaryHct = MaterialDynamicColors.tertiary.get_hct(self.material3Scheme)
@ -211,16 +210,16 @@ class Material3DynamicThemeImpl(ThemeImpl):
colorTonalPalette = TonalPalette.from_int(colorHarmonized)
colorSurfacePaletteOptions = DynamicColor.from_palette(
FromPaletteOptions(
name=f"{colorName}_container",
palette=lambda s: colorTonalPalette,
tone=lambda s: 30 if s.is_dark else 90,
is_background=True,
background=lambda s: MaterialDynamicColors.highestSurface(s),
contrast_curve=ContrastCurve(1, 1, 3, 4.5),
)
)
# colorSurfacePaletteOptions = DynamicColor.from_palette(
# FromPaletteOptions(
# name=f"{colorName}_container",
# palette=lambda s: colorTonalPalette,
# tone=lambda s: 30 if s.is_dark else 90,
# is_background=True,
# background=lambda s: MaterialDynamicColors.highestSurface(s),
# contrast_curve=ContrastCurve(1, 1, 3, 4.5),
# )
# )
extendedPalettes[colorName] = DynamicColor.from_palette(
FromPaletteOptions(
@ -233,15 +232,14 @@ class Material3DynamicThemeImpl(ThemeImpl):
)
)
return {
**ThemeImpl.DEFAULT_CUSTOM_PALETTE,
"primary": _hctToQColor(primaryHct),
"secondary": _hctToQColor(secondaryHct),
"tertiary": _hctToQColor(tertiaryHct),
"success": _hctToQColor(
return CustomPalette(
primary=_hctToQColor(primaryHct),
secondary=_hctToQColor(secondaryHct),
tertiary=_hctToQColor(tertiaryHct),
success=_hctToQColor(
extendedPalettes["success"].get_hct(self.material3Scheme)
),
"error": _hctToQColor(errorHct),
"toolTipBase": self.qPalette.color(QPalette.ColorRole.ToolTipBase),
"toolTipText": self.qPalette.color(QPalette.ColorRole.ToolTipText),
}
error=_hctToQColor(errorHct),
toolTipBase=self.qPalette.color(QPalette.ColorRole.ToolTipBase),
toolTipText=self.qPalette.color(QPalette.ColorRole.ToolTipText),
)

View File

@ -26,20 +26,20 @@ class ThemeQmlExposer(QObject):
@Property(QColor, notify=themeChanged)
def primary(self):
return self._themeImpl.customPalette["primary"]
return self._themeImpl.customPalette.primary
@Property(QColor, notify=themeChanged)
def success(self):
return self._themeImpl.customPalette["success"]
return self._themeImpl.customPalette.success
@Property(QColor, notify=themeChanged)
def error(self):
return self._themeImpl.customPalette["error"]
return self._themeImpl.customPalette.error
@Property(QColor, notify=themeChanged)
def toolTipBase(self):
return self._themeImpl.customPalette["toolTipBase"]
return self._themeImpl.customPalette.toolTipBase
@Property(QColor, notify=themeChanged)
def toolTipText(self):
return self._themeImpl.customPalette["toolTipText"]
return self._themeImpl.customPalette.toolTipText

View File

@ -1,18 +1,19 @@
from dataclasses import dataclass
from typing import Literal, TypedDict
from dataclasses import dataclass, field
from typing import Literal
from PySide6.QtGui import QColor, QPalette
class _TCustomPalette(TypedDict):
primary: QColor
secondary: QColor
tertiary: QColor
success: QColor
error: QColor
@dataclass(kw_only=True)
class CustomPalette:
primary: QColor = field(default_factory=lambda: QColor.fromRgb(0x616161))
secondary: QColor = field(default_factory=lambda: QColor.fromRgb(0x616161))
tertiary: QColor = field(default_factory=lambda: QColor.fromRgb(0x616161))
success: QColor = field(default_factory=lambda: QColor.fromRgb(0x616161))
error: QColor = field(default_factory=lambda: QColor.fromRgb(0x616161))
toolTipBase: QColor
toolTipText: QColor
toolTipBase: QColor = field(default_factory=lambda: QColor.fromRgb(0x616161))
toolTipText: QColor = field(default_factory=lambda: QColor.fromRgb(0x616161))
_TScheme = Literal["light", "dark"]
@ -32,15 +33,7 @@ class ThemeInfo:
class ThemeImpl:
DEFAULT_CUSTOM_PALETTE: _TCustomPalette = {
"primary": QColor.fromString("#616161"),
"secondary": QColor.fromString("#616161"),
"tertiary": QColor.fromString("#616161"),
"success": QColor.fromString("#616161"),
"error": QColor.fromString("#616161"),
"toolTipBase": QColor.fromString("#616161"),
"toolTipText": QColor.fromString("#616161"),
}
DEFAULT_CUSTOM_PALETTE: CustomPalette = CustomPalette()
@property
def info(self) -> ThemeInfo:
@ -56,5 +49,5 @@ class ThemeImpl:
return QPalette()
@property
def customPalette(self) -> _TCustomPalette:
def customPalette(self) -> CustomPalette:
return self.DEFAULT_CUSTOM_PALETTE # pyright: ignore[reportReturnType]