77from PyQt6 .QtGui import QGuiApplication , QStyleHints
88from PyQt6 .QtWidgets import QApplication
99
10- from .theme import Theme , load_qss
10+ from .theme import ColorScheme , load_qss
1111
1212logger = logging .getLogger (__name__ )
1313
1414_STYLE_HINTS_MISSING = "styleHints() unavailable; is a QGuiApplication running?"
1515_QAPPLICATION_MISSING = (
16- "QApplication instance not found; construct a QApplication before applying a theme."
16+ "QApplication instance not found; construct a QApplication before applying "
17+ "a colour scheme."
1718)
1819
1920_DARK_BASE_VALUE_THRESHOLD = 70
@@ -34,87 +35,78 @@ def _require_style_hints() -> QStyleHints:
3435 return hints
3536
3637
37- def detect_system_theme () -> Theme :
38- """Return the OS-reported colour scheme, or fall back to a palette heuristic.
38+ def detect_system_color_scheme () -> ColorScheme :
3939
40- Uses ``QGuiApplication.styleHints().colorScheme()`` on Qt 6.5+. When the
41- style hint returns ``Unknown`` (or the platform integration does not
42- report a value), the base-colour brightness of the current application
43- palette is inspected instead.
44-
45- A running ``QApplication`` (or at least a ``QGuiApplication``) must exist
46- before calling this function.
47- """
4840 hints = _require_style_hints ()
4941 scheme = hints .colorScheme ()
5042 if scheme == Qt .ColorScheme .Dark :
51- return Theme .DARK
43+ return ColorScheme .DARK
5244 if scheme == Qt .ColorScheme .Light :
53- return Theme .LIGHT
45+ return ColorScheme .LIGHT
5446 return _palette_fallback ()
5547
5648
57- def _palette_fallback () -> Theme :
49+ def _palette_fallback () -> ColorScheme :
5850 app = cast (QApplication | None , QApplication .instance ())
5951 if app is None :
60- return Theme .LIGHT
52+ return ColorScheme .LIGHT
6153 return (
62- Theme .DARK
54+ ColorScheme .DARK
6355 if app .palette ().base ().color ().value () < _DARK_BASE_VALUE_THRESHOLD
64- else Theme .LIGHT
56+ else ColorScheme .LIGHT
6557 )
6658
6759
68- class ThemeManager (QObject ):
69- theme_changed = pyqtSignal (Theme )
60+ class ColorSchemeManager (QObject ):
61+ color_scheme_changed = pyqtSignal (ColorScheme )
7062
7163 def __init__ (self , parent : QObject | None = None ) -> None :
7264 super ().__init__ (parent )
73- self ._follows_system_theme : bool = True
74- self ._current_theme : Theme = detect_system_theme ()
65+ self ._follows_system_color_scheme : bool = True
66+ self ._current_color_scheme : ColorScheme = detect_system_color_scheme ()
7567 hints = _require_style_hints ()
7668 hints .colorSchemeChanged .connect (self ._on_system_scheme_changed )
7769 self .apply_stylesheet_from_qss ()
7870
7971 @property
80- def current_theme (self ) -> Theme :
81- return self ._current_theme
72+ def current_color_scheme (self ) -> ColorScheme :
73+ return self ._current_color_scheme
8274
8375 @property
8476 def follows_system (self ) -> bool :
85- return self ._follows_system_theme
77+ return self ._follows_system_color_scheme
8678
87- def set_theme (self , theme : Theme ) -> None :
88- """Pin the manager to ``theme `` and stop following the OS scheme."""
89- self ._follows_system_theme = False
90- self ._set_theme_internal ( theme )
79+ def set_color_scheme (self , color_scheme : ColorScheme ) -> None :
80+ """Pin the manager to ``color_scheme `` and stop following the OS scheme."""
81+ self ._follows_system_color_scheme = False
82+ self ._set_color_scheme_internal ( color_scheme )
9183
9284 def follow_system (self ) -> None :
9385 """Resume following the OS colour scheme; re-syncs immediately."""
94- self ._follows_system_theme = True
95- self ._set_theme_internal ( detect_system_theme ())
86+ self ._follows_system_color_scheme = True
87+ self ._set_color_scheme_internal ( detect_system_color_scheme ())
9688
9789 def apply_stylesheet_from_qss (self ) -> None :
9890 app = cast (QApplication | None , QApplication .instance ())
9991 if app is None :
10092 raise RuntimeError (_QAPPLICATION_MISSING )
10193 try :
102- stylesheet = load_qss (self ._current_theme )
94+ stylesheet = load_qss (self ._current_color_scheme )
10395 except (OSError , UnicodeDecodeError ):
10496 logger .exception (
105- "Failed to load QSS for theme '%s'; keeping previous styling." ,
106- self ._current_theme .value ,
97+ "Failed to load QSS for colour scheme '%s'; keeping previous styling." ,
98+ self ._current_color_scheme .value ,
10799 )
108100 return
109101 app .setStyleSheet (stylesheet )
110102
111103 def _on_system_scheme_changed (self , _scheme : Qt .ColorScheme ) -> None :
112- if self ._follows_system_theme :
113- self ._set_theme_internal ( detect_system_theme ())
104+ if self ._follows_system_color_scheme :
105+ self ._set_color_scheme_internal ( detect_system_color_scheme ())
114106
115- def _set_theme_internal (self , theme : Theme ) -> None :
116- if theme == self ._current_theme :
107+ def _set_color_scheme_internal (self , color_scheme : ColorScheme ) -> None :
108+ if color_scheme == self ._current_color_scheme :
117109 return
118- self ._current_theme = theme
110+ self ._current_color_scheme = color_scheme
119111 self .apply_stylesheet_from_qss ()
120- self .theme_changed .emit (theme )
112+ self .color_scheme_changed .emit (color_scheme )
0 commit comments