From 247ff40920bc62060e604a5302d164700baf61ec Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 3 Oct 2026 21:59:33 -0500 Subject: [PATCH 1/5] Add a dark mode toggle Short Circuit only ever rendered with the OS's light/native widget style. Several widgets also hardcode light-theme-only colors (status message text, the four security-priority spinbox backgrounds, and the path-table row highlighting), so simply applying a dark QApplication stylesheet on top would leave those specific widgets unreadable (e.g. black status text on a dark background). This adds a View > Dark Mode checkable menu action that applies a QSS dark theme app-wide, persisted via QSettings like the other preferences. On Windows, Qt style sheets don't fully restyle some native-style controls (combo box/spin box arrows, checkboxes), so enabling dark mode also switches the base QStyle to Fusion, which is stylesheet-driven, and restores the original style when toggled off. The hardcoded-color widgets are made theme-aware individually rather than through the global stylesheet: a widget's own setStyleSheet() call takes precedence over an ancestor/application stylesheet for the same widget in Qt's cascade, regardless of selector specificity, so the four priority spinboxes (whose colors are set directly in the generated gui_main.py from the .ui file) needed explicit light/dark variants rather than relying on inheritance. The path-table row colors are intentionally fixed light pastels for security-class coding in both themes, so their text is now pinned to black instead of inheriting the theme's (light) default foreground color. --- src/shortcircuit/app.py | 76 +++++++++++++-- src/shortcircuit/view/theme.py | 169 +++++++++++++++++++++++++++++++++ 2 files changed, 237 insertions(+), 8 deletions(-) create mode 100644 src/shortcircuit/view/theme.py diff --git a/src/shortcircuit/app.py b/src/shortcircuit/app.py index 78e429a..da546bb 100644 --- a/src/shortcircuit/app.py +++ b/src/shortcircuit/app.py @@ -28,6 +28,7 @@ from .view.gui_main import Ui_MainWindow from .view.gui_mappers import Ui_MappersDialog from .view.gui_tripwire import Ui_TripwireDialog +from .view.theme import apply_theme class StateEVEConnection(TypedDict): @@ -326,6 +327,8 @@ def __init__(self, parent=None): self.global_proxy: str = "" self.mapper_configs: List[MapperConfig] = [] self.mapper_states: Dict[str, StateMapper] = {} + self.dark_mode: bool = False + self._label_states: Dict[QtWidgets.QLabel, tuple] = {} self.state_eve_connection = StateEVEConnection({ "connected": False, "char_name": None, "error": None @@ -476,6 +479,49 @@ def additional_gui_setup(self): self.table_item_selection_changed ) + # View menu + menu_view = self.menubar.addMenu("&View") + self.action_dark_mode = QtWidgets.QAction("&Dark Mode", self) + self.action_dark_mode.setCheckable(True) + self.action_dark_mode.toggled.connect(self.toggle_dark_mode) + menu_view.addAction(self.action_dark_mode) + self.action_dark_mode.setChecked(self.dark_mode) + + @QtCore.Slot(bool) + def toggle_dark_mode(self, enabled: bool): + self.dark_mode = enabled + apply_theme(QtWidgets.QApplication.instance(), enabled) + self._apply_priority_spinbox_colors() + self._refresh_label_colors() + + def _apply_priority_spinbox_colors(self): + if self.dark_mode: + self.spinBox_prio_hs.setStyleSheet( + "QSpinBox { background-color: #2d4a33; color: #dcdcdc; }" + ) + self.spinBox_prio_ns.setStyleSheet( + "QSpinBox { background-color: #4a2d2d; color: #dcdcdc; }" + ) + self.spinBox_prio_wh.setStyleSheet( + "QSpinBox { background-color: #27374a; color: #dcdcdc; }" + ) + self.spinBox_prio_ls.setStyleSheet( + "QSpinBox { background-color: #4a452d; color: #dcdcdc; }" + ) + else: + self.spinBox_prio_hs.setStyleSheet( + "QSpinBox { background-color: #DFF0D8; }" + ) + self.spinBox_prio_ns.setStyleSheet( + "QSpinBox { background-color: #F2DEDE; }" + ) + self.spinBox_prio_wh.setStyleSheet( + "QSpinBox { background-color: #D2E2F2; }" + ) + self.spinBox_prio_ls.setStyleSheet( + "QSpinBox { background-color: #FCF8E3; }" + ) + def read_settings_mappers(self): self.global_proxy = self.settings.value('proxy') or "" migrated = migrate_legacy(self.settings) @@ -540,6 +586,9 @@ def read_settings(self): self.spinBox_prio_ns.setValue(int(self.settings.value("prio_ns", "1"))) self.spinBox_prio_wh.setValue(int(self.settings.value("prio_wh", "1"))) + # Appearance + self.dark_mode = self.settings.value("dark_mode", "false") == "true" + self.settings.endGroup() def write_settings_mappers(self): @@ -595,6 +644,9 @@ def write_settings(self): self.settings.setValue("prio_ns", self.spinBox_prio_ns.value()) self.settings.setValue("prio_wh", self.spinBox_prio_wh.value()) + # Appearance + self.settings.setValue("dark_mode", self.dark_mode) + self.settings.endGroup() def _message_box(self, title, text): @@ -603,28 +655,33 @@ def _message_box(self, title, text): msg_box.setText(text) return msg_box.exec_() - @staticmethod - def _label_message(label, message, message_type): + def _label_message(self, label, message, message_type): # FIXME(secondfry): set only color, not entire stylesheet. + self._label_states[label] = (message, message_type) if message_type == MessageType.OK: - label.setStyleSheet("QLabel {color: green;}") + color = "#81c784" if self.dark_mode else "green" elif message_type == MessageType.ERROR: - label.setStyleSheet("QLabel {color: red;}") + color = "#e57373" if self.dark_mode else "red" else: - label.setStyleSheet("QLabel {color: black;}") + color = "#dcdcdc" if self.dark_mode else "black" + label.setStyleSheet("QLabel {{color: {};}}".format(color)) label.setText(message) + def _refresh_label_colors(self): + for label, (message, message_type) in self._label_states.items(): + self._label_message(label, message, message_type) + def _avoid_message(self, message, message_type): self.statusBar().showMessage(message, 5000) def _path_message(self, message, message_type): - MainWindow._label_message(self.label_status, message, message_type) + self._label_message(self.label_status, message, message_type) def _status_eve_connection(self, message, message_type=MessageType.INFO): - MainWindow._label_message(self.status_eve_connection, message, message_type) + self._label_message(self.status_eve_connection, message, message_type) def _status_mappers(self, message, message_type=MessageType.INFO): - MainWindow._label_message(self.status_mappers, message, message_type) + self._label_message(self.status_mappers, message, message_type) def avoidance_enabled(self) -> bool: return self.groupBox_avoidance.isChecked() @@ -697,6 +754,9 @@ def add_data_to_table(self, route): item.setIcon(self.icon_wormhole) item.setBackground(color) + # The row colors above are fixed light pastels regardless of theme, + # so force dark text to keep them legible in dark mode. + item.setForeground(QtGui.QColor(0, 0, 0)) self.tableWidget_path.setItem(route_step_id, ui_col_id, item) ui_col_id += 1 diff --git a/src/shortcircuit/view/theme.py b/src/shortcircuit/view/theme.py new file mode 100644 index 0000000..41a24f5 --- /dev/null +++ b/src/shortcircuit/view/theme.py @@ -0,0 +1,169 @@ +# theme.py +# +# Hand-written, unlike the other modules in this package (those starting with +# `gui_` are generated by generate_gui.sh from resources/ui/*.ui and must not +# be edited directly). This file is plain Python and safe to edit. + +from PySide2 import QtWidgets + +DARK_STYLESHEET = """ +QWidget { + background-color: #2b2b2b; + color: #dcdcdc; + selection-background-color: #3a6ea5; + selection-color: #ffffff; +} + +QMainWindow, QDialog { + background-color: #2b2b2b; +} + +QGroupBox { + border: 1px solid #555555; + border-radius: 3px; + margin-top: 8px; + padding-top: 6px; +} + +QGroupBox::title { + subcontrol-origin: margin; + left: 8px; + padding: 0 4px; + color: #dcdcdc; +} + +QLabel { + background-color: transparent; +} + +QLineEdit, QListWidget, QTableWidget, QComboBox, +QSpinBox, QDoubleSpinBox, QAbstractSpinBox { + background-color: #3c3f41; + color: #dcdcdc; + border: 1px solid #555555; + border-radius: 2px; +} + +QTableWidget { + gridline-color: #555555; +} + +QHeaderView::section { + background-color: #454545; + color: #dcdcdc; + padding: 4px; + border: 1px solid #555555; +} + +QComboBox QAbstractItemView { + background-color: #3c3f41; + color: #dcdcdc; + selection-background-color: #3a6ea5; +} + +QPushButton { + background-color: #454545; + color: #dcdcdc; + border: 1px solid #5a5a5a; + border-radius: 3px; + padding: 4px 10px; +} + +QPushButton:hover { + background-color: #525252; +} + +QPushButton:pressed { + background-color: #3a3a3a; +} + +QPushButton:disabled { + color: #777777; + background-color: #3a3a3a; +} + +QCheckBox, QRadioButton { + background-color: transparent; + spacing: 6px; +} + +QMenuBar { + background-color: #2b2b2b; + color: #dcdcdc; +} + +QMenuBar::item:selected { + background-color: #3a6ea5; +} + +QMenu { + background-color: #3c3f41; + color: #dcdcdc; + border: 1px solid #555555; +} + +QMenu::item:selected { + background-color: #3a6ea5; +} + +QStatusBar { + background-color: #2b2b2b; + color: #dcdcdc; +} + +QToolTip { + background-color: #454545; + color: #dcdcdc; + border: 1px solid #5a5a5a; +} + +QScrollBar:vertical, QScrollBar:horizontal { + background-color: #2b2b2b; + border: none; +} + +QScrollBar::handle:vertical, QScrollBar::handle:horizontal { + background-color: #5a5a5a; + border-radius: 3px; +} + +QScrollBar::handle:vertical:hover, QScrollBar::handle:horizontal:hover { + background-color: #6e6e6e; +} + +QScrollBar::add-line, QScrollBar::sub-line { + height: 0px; + width: 0px; +} +""" + +_ORIGINAL_STYLE_PROPERTY = "shortcircuit_original_style" + + +def apply_theme(app: QtWidgets.QApplication, dark: bool) -> None: + """ + Apply or clear the dark theme on the whole application. + + Qt style sheets interact poorly with Windows' native widget style (combo + box/spin box arrows, checkboxes, etc. don't fully respect QSS colors under + it), so dark mode also switches the base QStyle to Fusion, which is fully + stylesheet-driven. The original style is restored when dark mode is + turned back off. The original style name is stashed as a dynamic Qt + property on the application object rather than module state, since it + needs to survive across calls without relying on a Python global. + """ + if app.property(_ORIGINAL_STYLE_PROPERTY) is None: + app.setProperty(_ORIGINAL_STYLE_PROPERTY, app.style().objectName()) + + if dark: + fusion_style = QtWidgets.QStyleFactory.create("Fusion") + if fusion_style is not None: + app.setStyle(fusion_style) + app.setStyleSheet(DARK_STYLESHEET) + else: + original_style = QtWidgets.QStyleFactory.create( + app.property(_ORIGINAL_STYLE_PROPERTY) + ) + if original_style is not None: + app.setStyle(original_style) + app.setStyleSheet("") From a08621dc7517a5884c418951222164663dee87bb Mon Sep 17 00:00:00 2001 From: Rustam SecondFry Gubaydullin Date: Tue, 24 Nov 2026 01:09:14 +0300 Subject: [PATCH 2/5] Replace the dark-mode stylesheet with a System / Light / Dark choice On Qt 6 the native Windows style already follows the OS colour scheme, so the original toggle's "off" state was not light on a dark desktop, and its Fusion + QSS theme fought the native look instead of reusing it. Asking Qt for a colour scheme (styleHints().setColorScheme) keeps the native style in both schemes and lets "System" track the OS live. mogglemoss suggested the same direction on the PR. What still needs per-widget help is only what hardcodes colours: the priority spinboxes get dark backgrounds (their light ones stay owned by gui_main.ui, restored from what setupUi set), the OK/ERROR status labels pick scheme-appropriate colours while INFO text falls back to the palette, and the .ui links drop their inline #0000ff so they use the palette link colour. The [?] link label is re-set on a scheme change because a rich-text label bakes its link colour at setText. The qapp fixture moves to conftest.py so the new tests can share it. Co-Authored-By: Claude Opus 5.5 --- resources/ui/gui_main.ui | 2 +- resources/ui/gui_tripwire.ui | 2 +- resources/ui/gui_wanderer.ui | 2 +- src/shortcircuit/app.py | 131 +++++++++------ src/shortcircuit/model/conftest.py | 13 ++ src/shortcircuit/model/test_appearance.py | 74 +++++++++ src/shortcircuit/model/test_generated_ui.py | 12 -- src/shortcircuit/view/gui_main.py | 2 +- src/shortcircuit/view/gui_tripwire.py | 5 +- src/shortcircuit/view/gui_wanderer.py | 2 +- src/shortcircuit/view/theme.py | 169 -------------------- 11 files changed, 175 insertions(+), 239 deletions(-) create mode 100644 src/shortcircuit/model/conftest.py create mode 100644 src/shortcircuit/model/test_appearance.py delete mode 100644 src/shortcircuit/view/theme.py diff --git a/resources/ui/gui_main.ui b/resources/ui/gui_main.ui index 2bf0b5e..8748e8d 100644 --- a/resources/ui/gui_main.ui +++ b/resources/ui/gui_main.ui @@ -525,7 +525,7 @@ - <html><head/><body><p><a href="https://github.com/secondfry/shortcircuit/blob/master/README.md#security-prioritization"><span style=" text-decoration: underline; color:#0000ff;">[?]</span></a></p></body></html> + <html><head/><body><p><a href="https://github.com/secondfry/shortcircuit/blob/master/README.md#security-prioritization"><span style=" text-decoration: underline;">[?]</span></a></p></body></html> true diff --git a/resources/ui/gui_tripwire.ui b/resources/ui/gui_tripwire.ui index f47c648..0453e3b 100644 --- a/resources/ui/gui_tripwire.ui +++ b/resources/ui/gui_tripwire.ui @@ -152,7 +152,7 @@ - <html><head/><body><p><a href="https://tripwire.eve-apps.com/"><span style=" text-decoration: underline; color:#0000ff;">Don't have a Tripwire account, yet?</span></a></p></body></html> + <html><head/><body><p><a href="https://tripwire.eve-apps.com/"><span style=" text-decoration: underline;">Don't have a Tripwire account, yet?</span></a></p></body></html> Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter diff --git a/resources/ui/gui_wanderer.ui b/resources/ui/gui_wanderer.ui index 6b6c3bf..8cf8903 100644 --- a/resources/ui/gui_wanderer.ui +++ b/resources/ui/gui_wanderer.ui @@ -109,7 +109,7 @@ - <html><head/><body><p>Generate the token in Wanderer under Map Settings → Public Api → Generate. <a href="https://github.com/wanderer-industries/wanderer"><span style=" text-decoration: underline; color:#0000ff;">About Wanderer</span></a></p></body></html> + <html><head/><body><p>Generate the token in Wanderer under Map Settings → Public Api → Generate. <a href="https://github.com/wanderer-industries/wanderer"><span style=" text-decoration: underline;">About Wanderer</span></a></p></body></html> true diff --git a/src/shortcircuit/app.py b/src/shortcircuit/app.py index cd1a546..6d349fd 100644 --- a/src/shortcircuit/app.py +++ b/src/shortcircuit/app.py @@ -30,7 +30,6 @@ from .view.gui_mappers import Ui_MappersDialog from .view.gui_tripwire import Ui_TripwireDialog from .view.gui_wanderer import Ui_WandererDialog -from .view.theme import apply_theme class StateEVEConnection(TypedDict): @@ -66,6 +65,29 @@ def read_whsize_index(settings: QtCore.QSettings) -> int: return index +# Saved View > Appearance choice -> the colour scheme Qt is asked for; +# Unknown hands the choice back to the OS. +APPEARANCE_KEY = "appearance" +COLOR_SCHEMES = { + "system": QtCore.Qt.ColorScheme.Unknown, + "light": QtCore.Qt.ColorScheme.Light, + "dark": QtCore.Qt.ColorScheme.Dark, +} + +# The light backgrounds come from gui_main.ui; only dark ones live here. +DARK_PRIORITY_BACKGROUNDS = { + "spinBox_prio_hs": "#2d4a33", + "spinBox_prio_ns": "#4a2d2d", + "spinBox_prio_wh": "#27374a", + "spinBox_prio_ls": "#4a452d", +} + + +def read_appearance(settings: QtCore.QSettings) -> str: + appearance = settings.value(APPEARANCE_KEY, "system") + return appearance if appearance in COLOR_SCHEMES else "system" + + def size_restriction_for_index(combo_index: int) -> Dict[WormholeSize, bool]: """Block every size below the "Wormhole size at least" choice; the last entry, "[Ignore wormholes]", blocks them all.""" @@ -404,8 +426,12 @@ def __init__(self, parent=None): self.global_proxy: str = "" self.mapper_configs: List[MapperConfig] = [] self.mapper_states: Dict[str, StateMapper] = {} - self.dark_mode: bool = False + self.appearance: str = "system" self._label_states: Dict[QtWidgets.QLabel, tuple] = {} + self._light_priority_sheets = { + name: getattr(self, name).styleSheet() + for name in DARK_PRIORITY_BACKGROUNDS + } self.state_eve_connection = StateEVEConnection({ "connected": False, "char_name": None, "error": None @@ -559,47 +585,50 @@ def additional_gui_setup(self): ) # View menu - menu_view = self.menubar.addMenu("&View") - self.action_dark_mode = QtWidgets.QAction("&Dark Mode", self) - self.action_dark_mode.setCheckable(True) - self.action_dark_mode.toggled.connect(self.toggle_dark_mode) - menu_view.addAction(self.action_dark_mode) - self.action_dark_mode.setChecked(self.dark_mode) + menu_appearance = self.menubar.addMenu("&View").addMenu("&Appearance") + self.appearance_group = QtGui.QActionGroup(self) + for appearance in COLOR_SCHEMES: + action = menu_appearance.addAction(appearance.capitalize()) + action.setCheckable(True) + action.setData(appearance) + self.appearance_group.addAction(action) + self.appearance_group.triggered.connect( + lambda action: self.set_appearance(action.data()) + ) + QtGui.QGuiApplication.styleHints().colorSchemeChanged.connect( + self._apply_scheme_colors + ) + self.set_appearance(self.appearance) + + def set_appearance(self, appearance: str): + self.appearance = appearance + for action in self.appearance_group.actions(): + action.setChecked(action.data() == appearance) + QtGui.QGuiApplication.styleHints().setColorScheme( + COLOR_SCHEMES[appearance] + ) + # No colorSchemeChanged when the effective scheme stays the same. + self._apply_scheme_colors() - @QtCore.Slot(bool) - def toggle_dark_mode(self, enabled: bool): - self.dark_mode = enabled - apply_theme(QtWidgets.QApplication.instance(), enabled) - self._apply_priority_spinbox_colors() - self._refresh_label_colors() - - def _apply_priority_spinbox_colors(self): - if self.dark_mode: - self.spinBox_prio_hs.setStyleSheet( - "QSpinBox { background-color: #2d4a33; color: #dcdcdc; }" - ) - self.spinBox_prio_ns.setStyleSheet( - "QSpinBox { background-color: #4a2d2d; color: #dcdcdc; }" - ) - self.spinBox_prio_wh.setStyleSheet( - "QSpinBox { background-color: #27374a; color: #dcdcdc; }" - ) - self.spinBox_prio_ls.setStyleSheet( - "QSpinBox { background-color: #4a452d; color: #dcdcdc; }" - ) - else: - self.spinBox_prio_hs.setStyleSheet( - "QSpinBox { background-color: #DFF0D8; }" - ) - self.spinBox_prio_ns.setStyleSheet( - "QSpinBox { background-color: #F2DEDE; }" - ) - self.spinBox_prio_wh.setStyleSheet( - "QSpinBox { background-color: #D2E2F2; }" - ) - self.spinBox_prio_ls.setStyleSheet( - "QSpinBox { background-color: #FCF8E3; }" + @staticmethod + def _is_dark() -> bool: + scheme = QtGui.QGuiApplication.styleHints().colorScheme() + return scheme == QtCore.Qt.ColorScheme.Dark + + def _apply_scheme_colors(self): + dark = self._is_dark() + for name, background in DARK_PRIORITY_BACKGROUNDS.items(): + getattr(self, name).setStyleSheet( + "QSpinBox { background-color: %s; }" % background + if dark else self._light_priority_sheets[name] ) + for label, (message, message_type) in self._label_states.items(): + self._label_message(label, message, message_type) + # A rich-text label takes its link colour from the palette only when its + # text is set, and setText() ignores unchanged text. + link_text = self.label_10.text() + self.label_10.clear() + self.label_10.setText(link_text) def read_settings_mappers(self): self.global_proxy = self.settings.value('proxy') or "" @@ -664,7 +693,7 @@ def read_settings(self): self.spinBox_prio_wh.setValue(int(self.settings.value("prio_wh", "1"))) # Appearance - self.dark_mode = self.settings.value("dark_mode", "false") == "true" + self.appearance = read_appearance(self.settings) self.settings.endGroup() @@ -720,7 +749,7 @@ def write_settings(self): self.settings.setValue("prio_wh", self.spinBox_prio_wh.value()) # Appearance - self.settings.setValue("dark_mode", self.dark_mode) + self.settings.setValue(APPEARANCE_KEY, self.appearance) self.settings.endGroup() @@ -733,19 +762,19 @@ def _message_box(self, title, text): def _label_message(self, label, message, message_type): # FIXME(secondfry): set only color, not entire stylesheet. self._label_states[label] = (message, message_type) + dark = self._is_dark() if message_type == MessageType.OK: - color = "#81c784" if self.dark_mode else "green" + label.setStyleSheet( + "QLabel {color: %s;}" % ("#81c784" if dark else "green") + ) elif message_type == MessageType.ERROR: - color = "#e57373" if self.dark_mode else "red" + label.setStyleSheet( + "QLabel {color: %s;}" % ("#e57373" if dark else "red") + ) else: - color = "#dcdcdc" if self.dark_mode else "black" - label.setStyleSheet("QLabel {{color: {};}}".format(color)) + label.setStyleSheet("") label.setText(message) - def _refresh_label_colors(self): - for label, (message, message_type) in self._label_states.items(): - self._label_message(label, message, message_type) - def _avoid_message(self, message, message_type): self.statusBar().showMessage(message, 5000) diff --git a/src/shortcircuit/model/conftest.py b/src/shortcircuit/model/conftest.py new file mode 100644 index 0000000..d763904 --- /dev/null +++ b/src/shortcircuit/model/conftest.py @@ -0,0 +1,13 @@ +import os + +# The suite is headless: select the Qt platform plugin before Qt is imported. +os.environ.setdefault("QT_QPA_PLATFORM", "offscreen") + +import pytest +from PySide6 import QtWidgets + + +@pytest.fixture(scope="session") +def qapp(): + app = QtWidgets.QApplication.instance() or QtWidgets.QApplication([]) + yield app diff --git a/src/shortcircuit/model/test_appearance.py b/src/shortcircuit/model/test_appearance.py new file mode 100644 index 0000000..6e77207 --- /dev/null +++ b/src/shortcircuit/model/test_appearance.py @@ -0,0 +1,74 @@ +# test_appearance.py +"""View > Appearance: the saved choice and the colour scheme it asks Qt for.""" + +import pytest +from PySide6 import QtCore, QtGui + +from shortcircuit import app as app_module +from shortcircuit.app import APPEARANCE_KEY, MainWindow, read_appearance + + +def _settings(tmp_path, **values): + settings = QtCore.QSettings(str(tmp_path / 'settings.ini'), QtCore.QSettings.IniFormat) + for key, value in values.items(): + settings.setValue(key, value) + return settings + + +@pytest.mark.parametrize('saved, expected', [ + (None, 'system'), + ('dark', 'dark'), + ('light', 'light'), + ('true', 'system'), +]) +def test_read_appearance(tmp_path, saved, expected): + values = {} if saved is None else {APPEARANCE_KEY: saved} + assert read_appearance(_settings(tmp_path, **values)) == expected + + +@pytest.fixture +def window(qapp, tmp_path, monkeypatch): + # Offscreen Qt ignores scheme requests, so record them and fake the result. + requested = [] + monkeypatch.setattr(QtGui.QStyleHints, 'setColorScheme', lambda _, scheme: requested.append(scheme)) + monkeypatch.setattr( + QtGui.QStyleHints, 'colorScheme', + lambda _: requested[-1] if requested else QtCore.Qt.ColorScheme.Unknown, + ) + monkeypatch.setattr(app_module.VersionCheck, 'process', lambda _: None) + QtCore.QSettings.setPath(QtCore.QSettings.IniFormat, QtCore.QSettings.UserScope, str(tmp_path)) + win = MainWindow() + win.requested = requested + yield win + win.version_thread.quit() + win.version_thread.wait() + win.deleteLater() + + +def _choose(win, appearance): + action = next(a for a in win.appearance_group.actions() if a.data() == appearance) + action.trigger() + return action + + +def test_choosing_dark_sets_scheme_and_saves_it(window): + assert _choose(window, 'dark').isChecked() + assert window.requested[-1] == QtCore.Qt.ColorScheme.Dark + window.write_settings() + window.settings.beginGroup('MainWindow') + assert read_appearance(window.settings) == 'dark' + window.settings.endGroup() + + +def test_choosing_system_hands_scheme_back_to_os(window): + _choose(window, 'dark') + _choose(window, 'system') + assert window.requested[-1] == QtCore.Qt.ColorScheme.Unknown + + +def test_light_restores_the_ui_file_spinbox_background(window): + light_sheet = window.spinBox_prio_hs.styleSheet() + _choose(window, 'dark') + assert '#2d4a33' in window.spinBox_prio_hs.styleSheet() + _choose(window, 'light') + assert window.spinBox_prio_hs.styleSheet() == light_sheet == 'QSpinBox { background-color: #DFF0D8; }' diff --git a/src/shortcircuit/model/test_generated_ui.py b/src/shortcircuit/model/test_generated_ui.py index 1a8992a..24dc81e 100644 --- a/src/shortcircuit/model/test_generated_ui.py +++ b/src/shortcircuit/model/test_generated_ui.py @@ -5,21 +5,9 @@ regeneration that no longer builds its widgets under the installed PySide6. """ -import os - -# The suite is headless: select the Qt platform plugin before Qt is imported. -os.environ.setdefault("QT_QPA_PLATFORM", "offscreen") - -import pytest from PySide6 import QtWidgets -@pytest.fixture(scope="session") -def qapp(): - app = QtWidgets.QApplication.instance() or QtWidgets.QApplication([]) - yield app - - def test_generated_ui_modules_build_widgets(qapp): from shortcircuit.view.gui_about import Ui_AboutDialog from shortcircuit.view.gui_main import Ui_MainWindow diff --git a/src/shortcircuit/view/gui_main.py b/src/shortcircuit/view/gui_main.py index 269980a..77bd26b 100644 --- a/src/shortcircuit/view/gui_main.py +++ b/src/shortcircuit/view/gui_main.py @@ -523,7 +523,7 @@ def retranslateUi(self, MainWindow): self.label_4.setText(QCoreApplication.translate("MainWindow", u"hours ago", None)) self.groupBox_security.setTitle(QCoreApplication.translate("MainWindow", u"Security prioritization", None)) self.label_6.setText(QCoreApplication.translate("MainWindow", u"NS:", None)) - self.label_10.setText(QCoreApplication.translate("MainWindow", u"

[?]

", None)) + self.label_10.setText(QCoreApplication.translate("MainWindow", u"

[?]

", None)) self.label_7.setText(QCoreApplication.translate("MainWindow", u"HS:", None)) self.label_8.setText(QCoreApplication.translate("MainWindow", u"LS:", None)) self.label_9.setText(QCoreApplication.translate("MainWindow", u"WH:", None)) diff --git a/src/shortcircuit/view/gui_tripwire.py b/src/shortcircuit/view/gui_tripwire.py index f6858a3..40cb83a 100644 --- a/src/shortcircuit/view/gui_tripwire.py +++ b/src/shortcircuit/view/gui_tripwire.py @@ -3,7 +3,7 @@ ################################################################################ ## Form generated from reading UI file 'gui_tripwire.ui' ## -## Created by: Qt User Interface Compiler version 6.11.2 +## Created by: Qt User Interface Compiler version 6.9.3 ## ## WARNING! All changes made in this file will be lost when recompiling UI file! ################################################################################ @@ -144,5 +144,6 @@ def retranslateUi(self, TripwireDialog): self.label_7.setText(QCoreApplication.translate("TripwireDialog", u"Proxy:", None)) self.lineEdit_proxy.setInputMask("") self.lineEdit_proxy.setPlaceholderText(QCoreApplication.translate("TripwireDialog", u"Leave empty to disable", None)) - self.label_5.setText(QCoreApplication.translate("TripwireDialog", u"

Don't have a Tripwire account, yet?

", None)) + self.label_5.setText(QCoreApplication.translate("TripwireDialog", u"

Don't have a Tripwire account, yet?

", None)) # retranslateUi + diff --git a/src/shortcircuit/view/gui_wanderer.py b/src/shortcircuit/view/gui_wanderer.py index f5e2d38..6f765da 100644 --- a/src/shortcircuit/view/gui_wanderer.py +++ b/src/shortcircuit/view/gui_wanderer.py @@ -118,6 +118,6 @@ def retranslateUi(self, WandererDialog): self.label_map.setText(QCoreApplication.translate("WandererDialog", u"Map slug:", None)) self.lineEdit_map.setPlaceholderText(QCoreApplication.translate("WandererDialog", u"Map identifier from the map URL", None)) self.label_token.setText(QCoreApplication.translate("WandererDialog", u"API token:", None)) - self.label_help.setText(QCoreApplication.translate("WandererDialog", u"

Generate the token in Wanderer under Map Settings \u2192 Public Api \u2192 Generate. About Wanderer

", None)) + self.label_help.setText(QCoreApplication.translate("WandererDialog", u"

Generate the token in Wanderer under Map Settings \u2192 Public Api \u2192 Generate. About Wanderer

", None)) # retranslateUi diff --git a/src/shortcircuit/view/theme.py b/src/shortcircuit/view/theme.py deleted file mode 100644 index 41a24f5..0000000 --- a/src/shortcircuit/view/theme.py +++ /dev/null @@ -1,169 +0,0 @@ -# theme.py -# -# Hand-written, unlike the other modules in this package (those starting with -# `gui_` are generated by generate_gui.sh from resources/ui/*.ui and must not -# be edited directly). This file is plain Python and safe to edit. - -from PySide2 import QtWidgets - -DARK_STYLESHEET = """ -QWidget { - background-color: #2b2b2b; - color: #dcdcdc; - selection-background-color: #3a6ea5; - selection-color: #ffffff; -} - -QMainWindow, QDialog { - background-color: #2b2b2b; -} - -QGroupBox { - border: 1px solid #555555; - border-radius: 3px; - margin-top: 8px; - padding-top: 6px; -} - -QGroupBox::title { - subcontrol-origin: margin; - left: 8px; - padding: 0 4px; - color: #dcdcdc; -} - -QLabel { - background-color: transparent; -} - -QLineEdit, QListWidget, QTableWidget, QComboBox, -QSpinBox, QDoubleSpinBox, QAbstractSpinBox { - background-color: #3c3f41; - color: #dcdcdc; - border: 1px solid #555555; - border-radius: 2px; -} - -QTableWidget { - gridline-color: #555555; -} - -QHeaderView::section { - background-color: #454545; - color: #dcdcdc; - padding: 4px; - border: 1px solid #555555; -} - -QComboBox QAbstractItemView { - background-color: #3c3f41; - color: #dcdcdc; - selection-background-color: #3a6ea5; -} - -QPushButton { - background-color: #454545; - color: #dcdcdc; - border: 1px solid #5a5a5a; - border-radius: 3px; - padding: 4px 10px; -} - -QPushButton:hover { - background-color: #525252; -} - -QPushButton:pressed { - background-color: #3a3a3a; -} - -QPushButton:disabled { - color: #777777; - background-color: #3a3a3a; -} - -QCheckBox, QRadioButton { - background-color: transparent; - spacing: 6px; -} - -QMenuBar { - background-color: #2b2b2b; - color: #dcdcdc; -} - -QMenuBar::item:selected { - background-color: #3a6ea5; -} - -QMenu { - background-color: #3c3f41; - color: #dcdcdc; - border: 1px solid #555555; -} - -QMenu::item:selected { - background-color: #3a6ea5; -} - -QStatusBar { - background-color: #2b2b2b; - color: #dcdcdc; -} - -QToolTip { - background-color: #454545; - color: #dcdcdc; - border: 1px solid #5a5a5a; -} - -QScrollBar:vertical, QScrollBar:horizontal { - background-color: #2b2b2b; - border: none; -} - -QScrollBar::handle:vertical, QScrollBar::handle:horizontal { - background-color: #5a5a5a; - border-radius: 3px; -} - -QScrollBar::handle:vertical:hover, QScrollBar::handle:horizontal:hover { - background-color: #6e6e6e; -} - -QScrollBar::add-line, QScrollBar::sub-line { - height: 0px; - width: 0px; -} -""" - -_ORIGINAL_STYLE_PROPERTY = "shortcircuit_original_style" - - -def apply_theme(app: QtWidgets.QApplication, dark: bool) -> None: - """ - Apply or clear the dark theme on the whole application. - - Qt style sheets interact poorly with Windows' native widget style (combo - box/spin box arrows, checkboxes, etc. don't fully respect QSS colors under - it), so dark mode also switches the base QStyle to Fusion, which is fully - stylesheet-driven. The original style is restored when dark mode is - turned back off. The original style name is stashed as a dynamic Qt - property on the application object rather than module state, since it - needs to survive across calls without relying on a Python global. - """ - if app.property(_ORIGINAL_STYLE_PROPERTY) is None: - app.setProperty(_ORIGINAL_STYLE_PROPERTY, app.style().objectName()) - - if dark: - fusion_style = QtWidgets.QStyleFactory.create("Fusion") - if fusion_style is not None: - app.setStyle(fusion_style) - app.setStyleSheet(DARK_STYLESHEET) - else: - original_style = QtWidgets.QStyleFactory.create( - app.property(_ORIGINAL_STYLE_PROPERTY) - ) - if original_style is not None: - app.setStyle(original_style) - app.setStyleSheet("") From 26644e9d9911516530cfc8e53c78d8fa69eca7be Mon Sep 17 00:00:00 2001 From: Rustam SecondFry Gubaydullin Date: Tue, 24 Nov 2026 01:15:51 +0300 Subject: [PATCH 3/5] Pick appearance colours from the drawn palette, not the requested scheme On Windows 10 Qt's default style is windowsvista, which cannot draw dark: asking for Dark makes colorScheme() report Dark while the palette stays light. Keying the spinbox and status colours off colorScheme() gave a light window with dark spinbox backgrounds and pale status text. Judging by the palette's Window lightness keeps them matched to what is actually drawn, so Dark there simply leaves the light look. The colours are re-applied on the window's PaletteChange rather than colorSchemeChanged, because the palette is what they now depend on and the window only sees PaletteChange once the new palette is in place. (ApplicationPaletteChange never reaches changeEvent.) Test upkeep from review: the QSettings helper moves into conftest.py next to qapp, the appearance fixture restores the global QSettings path and app palette it changes, and the appearance block no longer splits the wormhole-size helpers. Co-Authored-By: Claude Opus 5.5 --- src/shortcircuit/app.py | 33 ++++++----- src/shortcircuit/model/conftest.py | 12 +++- src/shortcircuit/model/test_app_settings.py | 25 +++------ src/shortcircuit/model/test_appearance.py | 62 ++++++++++++++------- 4 files changed, 81 insertions(+), 51 deletions(-) diff --git a/src/shortcircuit/app.py b/src/shortcircuit/app.py index 6d349fd..0f088dd 100644 --- a/src/shortcircuit/app.py +++ b/src/shortcircuit/app.py @@ -65,6 +65,12 @@ def read_whsize_index(settings: QtCore.QSettings) -> int: return index +def size_restriction_for_index(combo_index: int) -> Dict[WormholeSize, bool]: + """Block every size below the "Wormhole size at least" choice; the last + entry, "[Ignore wormholes]", blocks them all.""" + return {size: i < combo_index for i, size in enumerate(KNOWN_WH_SIZES)} + + # Saved View > Appearance choice -> the colour scheme Qt is asked for; # Unknown hands the choice back to the OS. APPEARANCE_KEY = "appearance" @@ -88,12 +94,6 @@ def read_appearance(settings: QtCore.QSettings) -> str: return appearance if appearance in COLOR_SCHEMES else "system" -def size_restriction_for_index(combo_index: int) -> Dict[WormholeSize, bool]: - """Block every size below the "Wormhole size at least" choice; the last - entry, "[Ignore wormholes]", blocks them all.""" - return {size: i < combo_index for i, size in enumerate(KNOWN_WH_SIZES)} - - class TripwireDialog(QtWidgets.QDialog, Ui_TripwireDialog): """ Per-instance Tripwire edit dialog, opened from MappersDialog. @@ -595,9 +595,6 @@ def additional_gui_setup(self): self.appearance_group.triggered.connect( lambda action: self.set_appearance(action.data()) ) - QtGui.QGuiApplication.styleHints().colorSchemeChanged.connect( - self._apply_scheme_colors - ) self.set_appearance(self.appearance) def set_appearance(self, appearance: str): @@ -607,13 +604,15 @@ def set_appearance(self, appearance: str): QtGui.QGuiApplication.styleHints().setColorScheme( COLOR_SCHEMES[appearance] ) - # No colorSchemeChanged when the effective scheme stays the same. + # No palette change event when the drawn palette stays the same. self._apply_scheme_colors() @staticmethod def _is_dark() -> bool: - scheme = QtGui.QGuiApplication.styleHints().colorScheme() - return scheme == QtCore.Qt.ColorScheme.Dark + # Judge by what is drawn: some styles (windowsvista, Windows 10's + # default) report a Dark scheme yet keep a light palette. + window = QtGui.QGuiApplication.palette().color(QtGui.QPalette.ColorRole.Window) + return window.lightness() < 128 def _apply_scheme_colors(self): dark = self._is_dark() @@ -625,7 +624,8 @@ def _apply_scheme_colors(self): for label, (message, message_type) in self._label_states.items(): self._label_message(label, message, message_type) # A rich-text label takes its link colour from the palette only when its - # text is set, and setText() ignores unchanged text. + # text is set, and setText() ignores unchanged text. label_10 is the only + # link label in the main window. link_text = self.label_10.text() self.label_10.clear() self.label_10.setText(link_text) @@ -1294,6 +1294,13 @@ def version_check_done(self, latest): ) ) + def changeEvent(self, event): + # The window follows the application palette, so a scheme switch (or a + # live OS switch under System) arrives here as PaletteChange. + if event.type() == QtCore.QEvent.Type.PaletteChange: + self._apply_scheme_colors() + super().changeEvent(event) + # event: QCloseEvent def closeEvent(self, event): self.write_settings() diff --git a/src/shortcircuit/model/conftest.py b/src/shortcircuit/model/conftest.py index d763904..2f78d15 100644 --- a/src/shortcircuit/model/conftest.py +++ b/src/shortcircuit/model/conftest.py @@ -4,10 +4,20 @@ os.environ.setdefault("QT_QPA_PLATFORM", "offscreen") import pytest -from PySide6 import QtWidgets +from PySide6 import QtCore, QtWidgets @pytest.fixture(scope="session") def qapp(): app = QtWidgets.QApplication.instance() or QtWidgets.QApplication([]) yield app + + +@pytest.fixture +def make_settings(tmp_path): + def make(**values): + settings = QtCore.QSettings(str(tmp_path / 'settings.ini'), QtCore.QSettings.IniFormat) + for key, value in values.items(): + settings.setValue(key, value) + return settings + return make diff --git a/src/shortcircuit/model/test_app_settings.py b/src/shortcircuit/model/test_app_settings.py index 0a76e75..188dec1 100644 --- a/src/shortcircuit/model/test_app_settings.py +++ b/src/shortcircuit/model/test_app_settings.py @@ -1,8 +1,6 @@ # test_app_settings.py """The "Wormhole size at least" combo: its saved index and the sizes it blocks.""" -from PySide6 import QtCore - from shortcircuit.app import ( LEGACY_WHSIZE_KEY, WHSIZE_KEY, @@ -14,13 +12,6 @@ FREIGHTER, CAPITAL, IGNORE_WORMHOLES = 3, 4, 5 -def _settings(tmp_path, **values): - settings = QtCore.QSettings(str(tmp_path / 'settings.ini'), QtCore.QSettings.IniFormat) - for key, value in values.items(): - settings.setValue(key, value) - return settings - - def _allowed(combo_index): return {size for size, blocked in size_restriction_for_index(combo_index).items() if not blocked} @@ -38,18 +29,18 @@ def test_ignore_wormholes_blocks_every_size(): assert len(_allowed(0)) == 5 -def test_legacy_ignore_wormholes_is_kept(tmp_path): - assert read_whsize_index(_settings(tmp_path, **{LEGACY_WHSIZE_KEY: 4})) == IGNORE_WORMHOLES +def test_legacy_ignore_wormholes_is_kept(make_settings): + assert read_whsize_index(make_settings(**{LEGACY_WHSIZE_KEY: 4})) == IGNORE_WORMHOLES -def test_legacy_xlarge_becomes_freighter(tmp_path): - assert read_whsize_index(_settings(tmp_path, **{LEGACY_WHSIZE_KEY: 3})) == FREIGHTER +def test_legacy_xlarge_becomes_freighter(make_settings): + assert read_whsize_index(make_settings(**{LEGACY_WHSIZE_KEY: 3})) == FREIGHTER -def test_new_key_wins_over_legacy(tmp_path): - settings = _settings(tmp_path, **{LEGACY_WHSIZE_KEY: 4, WHSIZE_KEY: CAPITAL}) +def test_new_key_wins_over_legacy(make_settings): + settings = make_settings(**{LEGACY_WHSIZE_KEY: 4, WHSIZE_KEY: CAPITAL}) assert read_whsize_index(settings) == CAPITAL -def test_no_saved_index_is_small(tmp_path): - assert read_whsize_index(_settings(tmp_path)) == 0 +def test_no_saved_index_is_small(make_settings): + assert read_whsize_index(make_settings()) == 0 diff --git a/src/shortcircuit/model/test_appearance.py b/src/shortcircuit/model/test_appearance.py index 6e77207..127d956 100644 --- a/src/shortcircuit/model/test_appearance.py +++ b/src/shortcircuit/model/test_appearance.py @@ -1,5 +1,8 @@ # test_appearance.py -"""View > Appearance: the saved choice and the colour scheme it asks Qt for.""" +"""View > Appearance: the saved choice, the colour scheme it asks Qt for, and +the colours that follow the palette Qt actually draws.""" + +import os import pytest from PySide6 import QtCore, QtGui @@ -7,12 +10,8 @@ from shortcircuit import app as app_module from shortcircuit.app import APPEARANCE_KEY, MainWindow, read_appearance - -def _settings(tmp_path, **values): - settings = QtCore.QSettings(str(tmp_path / 'settings.ini'), QtCore.QSettings.IniFormat) - for key, value in values.items(): - settings.setValue(key, value) - return settings +LIGHT_HS_SHEET = 'QSpinBox { background-color: #DFF0D8; }' +DARK, LIGHT = '#1e1e1e', '#f3f3f3' @pytest.mark.parametrize('saved, expected', [ @@ -21,28 +20,37 @@ def _settings(tmp_path, **values): ('light', 'light'), ('true', 'system'), ]) -def test_read_appearance(tmp_path, saved, expected): +def test_read_appearance(make_settings, saved, expected): values = {} if saved is None else {APPEARANCE_KEY: saved} - assert read_appearance(_settings(tmp_path, **values)) == expected + assert read_appearance(make_settings(**values)) == expected + + +def _palette(window_colour): + # Built from the button colour, which QPalette also uses for Window. + return QtGui.QPalette(QtGui.QColor(window_colour)) @pytest.fixture def window(qapp, tmp_path, monkeypatch): - # Offscreen Qt ignores scheme requests, so record them and fake the result. + # Offscreen Qt ignores scheme requests: record them, and let each test + # decide whether the style answers with a dark palette. requested = [] monkeypatch.setattr(QtGui.QStyleHints, 'setColorScheme', lambda _, scheme: requested.append(scheme)) - monkeypatch.setattr( - QtGui.QStyleHints, 'colorScheme', - lambda _: requested[-1] if requested else QtCore.Qt.ColorScheme.Unknown, - ) monkeypatch.setattr(app_module.VersionCheck, 'process', lambda _: None) - QtCore.QSettings.setPath(QtCore.QSettings.IniFormat, QtCore.QSettings.UserScope, str(tmp_path)) + ini, user = QtCore.QSettings.IniFormat, QtCore.QSettings.UserScope + # QSettings(format, scope, org) lives at /.ini. + old_path = os.path.dirname(QtCore.QSettings(ini, user, 'probe').fileName()) + old_palette = qapp.palette() + QtCore.QSettings.setPath(ini, user, str(tmp_path)) + qapp.setPalette(_palette(LIGHT)) win = MainWindow() win.requested = requested yield win win.version_thread.quit() win.version_thread.wait() win.deleteLater() + QtCore.QSettings.setPath(ini, user, old_path) + qapp.setPalette(old_palette) def _choose(win, appearance): @@ -66,9 +74,23 @@ def test_choosing_system_hands_scheme_back_to_os(window): assert window.requested[-1] == QtCore.Qt.ColorScheme.Unknown -def test_light_restores_the_ui_file_spinbox_background(window): - light_sheet = window.spinBox_prio_hs.styleSheet() - _choose(window, 'dark') +def _switch_palette(qapp, win, window_colour): + # What a scheme switch does on a real platform: new application palette, + # then PaletteChange to the window (offscreen setPalette skips the latter + # once child stylesheets exist). + qapp.setPalette(_palette(window_colour)) + qapp.sendEvent(win, QtCore.QEvent(QtCore.QEvent.Type.PaletteChange)) + + +def test_dark_palette_darkens_and_light_restores_the_ui_file_background(window, qapp): + _switch_palette(qapp, window, DARK) assert '#2d4a33' in window.spinBox_prio_hs.styleSheet() - _choose(window, 'light') - assert window.spinBox_prio_hs.styleSheet() == light_sheet == 'QSpinBox { background-color: #DFF0D8; }' + _switch_palette(qapp, window, LIGHT) + assert window.spinBox_prio_hs.styleSheet() == LIGHT_HS_SHEET + + +def test_dark_request_kept_light_by_the_style_stays_light(window, monkeypatch): + # windowsvista (Windows 10's default) reports Dark but draws light. + monkeypatch.setattr(QtGui.QStyleHints, 'colorScheme', lambda _: window.requested[-1]) + _choose(window, 'dark') + assert window.spinBox_prio_hs.styleSheet() == LIGHT_HS_SHEET From 6071b0fd828f3336c57eb66de222d891b0915686 Mon Sep 17 00:00:00 2001 From: Rustam SecondFry Gubaydullin Date: Tue, 24 Nov 2026 01:30:48 +0300 Subject: [PATCH 4/5] Test the real palette-change and start-up paths for appearance colours The palette test sent PaletteChange by hand on the belief that offscreen setPalette skips it. It does not: the event is posted, so the test now lets it arrive and fails if changeEvent stops re-applying colours. The direct _apply_scheme_colors() call in set_appearance had no test at all; it is what colours the UI under System on an already-dark OS, where no palette change ever comes, so that case now has one. The annotations and the split line bring app.py in line with the typed attributes around it and with the 80-column limit in .style.yapf. Co-Authored-By: Claude Opus 5.5 --- src/shortcircuit/app.py | 9 +++++---- src/shortcircuit/model/test_appearance.py | 24 +++++++++++++++-------- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/src/shortcircuit/app.py b/src/shortcircuit/app.py index 0f088dd..eb02982 100644 --- a/src/shortcircuit/app.py +++ b/src/shortcircuit/app.py @@ -4,7 +4,7 @@ import json import sys from functools import partial -from typing import Dict, List, Optional, TypedDict, Union +from typing import Dict, List, Optional, Tuple, TypedDict, Union from PySide6 import QtCore, QtGui, QtWidgets @@ -427,8 +427,8 @@ def __init__(self, parent=None): self.mapper_configs: List[MapperConfig] = [] self.mapper_states: Dict[str, StateMapper] = {} self.appearance: str = "system" - self._label_states: Dict[QtWidgets.QLabel, tuple] = {} - self._light_priority_sheets = { + self._label_states: Dict[QtWidgets.QLabel, Tuple[str, MessageType]] = {} + self._light_priority_sheets: Dict[str, str] = { name: getattr(self, name).styleSheet() for name in DARK_PRIORITY_BACKGROUNDS } @@ -611,7 +611,8 @@ def set_appearance(self, appearance: str): def _is_dark() -> bool: # Judge by what is drawn: some styles (windowsvista, Windows 10's # default) report a Dark scheme yet keep a light palette. - window = QtGui.QGuiApplication.palette().color(QtGui.QPalette.ColorRole.Window) + palette = QtGui.QGuiApplication.palette() + window = palette.color(QtGui.QPalette.ColorRole.Window) return window.lightness() < 128 def _apply_scheme_colors(self): diff --git a/src/shortcircuit/model/test_appearance.py b/src/shortcircuit/model/test_appearance.py index 127d956..2574b2d 100644 --- a/src/shortcircuit/model/test_appearance.py +++ b/src/shortcircuit/model/test_appearance.py @@ -74,23 +74,31 @@ def test_choosing_system_hands_scheme_back_to_os(window): assert window.requested[-1] == QtCore.Qt.ColorScheme.Unknown -def _switch_palette(qapp, win, window_colour): - # What a scheme switch does on a real platform: new application palette, - # then PaletteChange to the window (offscreen setPalette skips the latter - # once child stylesheets exist). +def _switch_palette(qapp, window_colour): + # A scheme switch gives a new application palette; its PaletteChange is + # posted to the window, so let it arrive. qapp.setPalette(_palette(window_colour)) - qapp.sendEvent(win, QtCore.QEvent(QtCore.QEvent.Type.PaletteChange)) + qapp.processEvents() def test_dark_palette_darkens_and_light_restores_the_ui_file_background(window, qapp): - _switch_palette(qapp, window, DARK) + _switch_palette(qapp, DARK) assert '#2d4a33' in window.spinBox_prio_hs.styleSheet() - _switch_palette(qapp, window, LIGHT) + _switch_palette(qapp, LIGHT) assert window.spinBox_prio_hs.styleSheet() == LIGHT_HS_SHEET def test_dark_request_kept_light_by_the_style_stays_light(window, monkeypatch): - # windowsvista (Windows 10's default) reports Dark but draws light. + # windowsvista (Windows 10's default) reports the Dark request from + # colorScheme() but keeps drawing a light palette. monkeypatch.setattr(QtGui.QStyleHints, 'colorScheme', lambda _: window.requested[-1]) _choose(window, 'dark') assert window.spinBox_prio_hs.styleSheet() == LIGHT_HS_SHEET + + +def test_dark_palette_at_start_up_applies_without_a_palette_event(window, qapp): + # Under System on a dark OS the palette is already dark at start-up, so no + # PaletteChange comes; set_appearance must apply the colours itself. + qapp.setPalette(_palette(DARK)) + window.set_appearance('system') + assert '#2d4a33' in window.spinBox_prio_hs.styleSheet() From c82c6b9491af6aceb24ca1d9db7e468b7f4012b0 Mon Sep 17 00:00:00 2001 From: Rustam SecondFry Gubaydullin Date: Tue, 24 Nov 2026 01:48:02 +0300 Subject: [PATCH 5/5] Test that shown status messages recolour on a palette switch Copilot noted the appearance tests only looked at spinbox styles, so the loop in _apply_scheme_colors that re-applies stored label states could be deleted with every test still green, leaving OK/ERROR messages in their old colours after a light/dark switch. Co-Authored-By: Claude Opus 5.5 --- src/shortcircuit/model/test_appearance.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/shortcircuit/model/test_appearance.py b/src/shortcircuit/model/test_appearance.py index 2574b2d..7fc21d6 100644 --- a/src/shortcircuit/model/test_appearance.py +++ b/src/shortcircuit/model/test_appearance.py @@ -8,7 +8,7 @@ from PySide6 import QtCore, QtGui from shortcircuit import app as app_module -from shortcircuit.app import APPEARANCE_KEY, MainWindow, read_appearance +from shortcircuit.app import APPEARANCE_KEY, MainWindow, MessageType, read_appearance LIGHT_HS_SHEET = 'QSpinBox { background-color: #DFF0D8; }' DARK, LIGHT = '#1e1e1e', '#f3f3f3' @@ -102,3 +102,20 @@ def test_dark_palette_at_start_up_applies_without_a_palette_event(window, qapp): qapp.setPalette(_palette(DARK)) window.set_appearance('system') assert '#2d4a33' in window.spinBox_prio_hs.styleSheet() + + +def test_status_messages_recolour_on_palette_switch(window, qapp): + # (label, type, light sheet, dark sheet): existing messages must follow + # the palette, not keep the colour they were shown with. + cases = [ + (window.label_status, MessageType.OK, 'QLabel {color: green;}', 'QLabel {color: #81c784;}'), + (window.status_mappers, MessageType.ERROR, 'QLabel {color: red;}', 'QLabel {color: #e57373;}'), + (window.status_eve_connection, MessageType.INFO, '', ''), + ] + for label, message_type, _, _ in cases: + window._label_message(label, message_type.name, message_type) + for colour, dark in ((LIGHT, False), (DARK, True), (LIGHT, False)): + _switch_palette(qapp, colour) + for label, message_type, light_sheet, dark_sheet in cases: + assert label.styleSheet() == (dark_sheet if dark else light_sheet) + assert label.text() == message_type.name