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 .material3 import Material3DynamicThemeImpl, Material3ThemeImpl
from .qml import ThemeQmlExposer 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_NAME = "internal.ui.theme"
QML_IMPORT_MAJOR_VERSION = 1 QML_IMPORT_MAJOR_VERSION = 1
@ -27,7 +27,7 @@ class ThemeManager(QObject):
super().__init__(parent) super().__init__(parent)
self._qPalette = QPalette() self._qPalette = QPalette()
self._customPalette: _TCustomPalette = ThemeImpl.DEFAULT_CUSTOM_PALETTE self._customPalette: CustomPalette = ThemeImpl.DEFAULT_CUSTOM_PALETTE
self._lastThemeInfo = ThemeImpl().info self._lastThemeInfo = ThemeImpl().info
self._qmlExposer = ThemeQmlExposer(themeImpl=ThemeImpl()) self._qmlExposer = ThemeQmlExposer(themeImpl=ThemeImpl())
@ -135,7 +135,7 @@ class ThemeManager(QObject):
return self._qPalette return self._qPalette
@Property(dict, notify=themeChanged) @Property(dict, notify=themeChanged)
def customPalette(self) -> _TCustomPalette: def customPalette(self) -> CustomPalette:
return self._customPalette return self._customPalette
@Property(ThemeQmlExposer, notify=themeChanged) @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 materialyoucolor.scheme.scheme_tonal_spot import SchemeTonalSpot
from PySide6.QtGui import QColor, QPalette from PySide6.QtGui import QColor, QPalette
from .shared import ThemeImpl, ThemeInfo, _TCustomPalette, _TScheme from .shared import CustomPalette, ThemeImpl, ThemeInfo, _TScheme
from .types import ( from .types import (
TMaterial3DynamicThemeData, TMaterial3DynamicThemeData,
TMaterial3ThemeData, TMaterial3ThemeData,
@ -84,7 +84,7 @@ class Material3ThemeImpl(ThemeImpl):
return qPalette return qPalette
@property @property
def customPalette(self) -> _TCustomPalette: def customPalette(self) -> CustomPalette:
primaryHct = _hexToHct(self.themeData["schemes"][self.scheme]["primary"]) primaryHct = _hexToHct(self.themeData["schemes"][self.scheme]["primary"])
secondaryHct = _hexToHct(self.themeData["schemes"][self.scheme]["secondary"]) secondaryHct = _hexToHct(self.themeData["schemes"][self.scheme]["secondary"])
tertiaryHct = _hexToHct(self.themeData["schemes"][self.scheme]["tertiary"]) tertiaryHct = _hexToHct(self.themeData["schemes"][self.scheme]["tertiary"])
@ -98,16 +98,15 @@ class Material3ThemeImpl(ThemeImpl):
Blend.harmonize(successHct.to_int(), primaryHct.to_int()) Blend.harmonize(successHct.to_int(), primaryHct.to_int())
) )
return { return CustomPalette(
**ThemeImpl.DEFAULT_CUSTOM_PALETTE, primary=_hctToQColor(primaryHct),
"primary": _hctToQColor(primaryHct), secondary=_hctToQColor(secondaryHct),
"secondary": _hctToQColor(secondaryHct), tertiary=_hctToQColor(tertiaryHct),
"tertiary": _hctToQColor(tertiaryHct), success=_hctToQColor(successHarmonizedHct),
"success": _hctToQColor(successHarmonizedHct), error=QColor.fromString(self.themeData["schemes"][self.scheme]["error"]),
"error": QColor.fromString(self.themeData["schemes"][self.scheme]["error"]), toolTipBase=self.qPalette.color(QPalette.ColorRole.ToolTipBase),
"toolTipBase": self.qPalette.color(QPalette.ColorRole.ToolTipBase), toolTipText=self.qPalette.color(QPalette.ColorRole.ToolTipText),
"toolTipText": self.qPalette.color(QPalette.ColorRole.ToolTipText), )
}
class Material3DynamicThemeImpl(ThemeImpl): class Material3DynamicThemeImpl(ThemeImpl):
@ -198,7 +197,7 @@ class Material3DynamicThemeImpl(ThemeImpl):
return qPalette return qPalette
@property @property
def customPalette(self) -> _TCustomPalette: def customPalette(self) -> CustomPalette:
primaryHct = MaterialDynamicColors.primary.get_hct(self.material3Scheme) primaryHct = MaterialDynamicColors.primary.get_hct(self.material3Scheme)
secondaryHct = MaterialDynamicColors.secondary.get_hct(self.material3Scheme) secondaryHct = MaterialDynamicColors.secondary.get_hct(self.material3Scheme)
tertiaryHct = MaterialDynamicColors.tertiary.get_hct(self.material3Scheme) tertiaryHct = MaterialDynamicColors.tertiary.get_hct(self.material3Scheme)
@ -211,16 +210,16 @@ class Material3DynamicThemeImpl(ThemeImpl):
colorTonalPalette = TonalPalette.from_int(colorHarmonized) colorTonalPalette = TonalPalette.from_int(colorHarmonized)
colorSurfacePaletteOptions = DynamicColor.from_palette( # colorSurfacePaletteOptions = DynamicColor.from_palette(
FromPaletteOptions( # FromPaletteOptions(
name=f"{colorName}_container", # name=f"{colorName}_container",
palette=lambda s: colorTonalPalette, # palette=lambda s: colorTonalPalette,
tone=lambda s: 30 if s.is_dark else 90, # tone=lambda s: 30 if s.is_dark else 90,
is_background=True, # is_background=True,
background=lambda s: MaterialDynamicColors.highestSurface(s), # background=lambda s: MaterialDynamicColors.highestSurface(s),
contrast_curve=ContrastCurve(1, 1, 3, 4.5), # contrast_curve=ContrastCurve(1, 1, 3, 4.5),
) # )
) # )
extendedPalettes[colorName] = DynamicColor.from_palette( extendedPalettes[colorName] = DynamicColor.from_palette(
FromPaletteOptions( FromPaletteOptions(
@ -233,15 +232,14 @@ class Material3DynamicThemeImpl(ThemeImpl):
) )
) )
return { return CustomPalette(
**ThemeImpl.DEFAULT_CUSTOM_PALETTE, primary=_hctToQColor(primaryHct),
"primary": _hctToQColor(primaryHct), secondary=_hctToQColor(secondaryHct),
"secondary": _hctToQColor(secondaryHct), tertiary=_hctToQColor(tertiaryHct),
"tertiary": _hctToQColor(tertiaryHct), success=_hctToQColor(
"success": _hctToQColor(
extendedPalettes["success"].get_hct(self.material3Scheme) extendedPalettes["success"].get_hct(self.material3Scheme)
), ),
"error": _hctToQColor(errorHct), error=_hctToQColor(errorHct),
"toolTipBase": self.qPalette.color(QPalette.ColorRole.ToolTipBase), toolTipBase=self.qPalette.color(QPalette.ColorRole.ToolTipBase),
"toolTipText": self.qPalette.color(QPalette.ColorRole.ToolTipText), toolTipText=self.qPalette.color(QPalette.ColorRole.ToolTipText),
} )

View File

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

View File

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