From 3de441a823d0cfe7a47cc1d881e5fba7ca51014a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20Br=C3=BCbach?= Date: Sat, 20 Jun 2026 01:26:04 +0200 Subject: [PATCH] Snapshot system palette. Took 8 minutes Took 11 seconds --- cockatrice/src/interface/theme_manager.cpp | 23 +++++++++++++++++++++- cockatrice/src/interface/theme_manager.h | 2 ++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/cockatrice/src/interface/theme_manager.cpp b/cockatrice/src/interface/theme_manager.cpp index 2c56a03ff..47b921945 100644 --- a/cockatrice/src/interface/theme_manager.cpp +++ b/cockatrice/src/interface/theme_manager.cpp @@ -97,6 +97,13 @@ ThemeManager::ThemeManager(QObject *parent) : QObject(parent) defaultStyleName = "windowsvista"; } ensureThemeDirectoryExists(); + + // Capture the QPA-initialised palette before we ever call qApp->setPalette(). + // Once setPalette() is called, is_app_palette is locked true and neither + // setStyle() nor colorSchemeChanged will update it automatically. + // This snapshot is our guaranteed-clean base for applyStyleAndPalette. + systemPalette = qApp->palette(); + #if (QT_VERSION >= QT_VERSION_CHECK(6, 5, 0)) connect(QGuiApplication::styleHints(), &QStyleHints::colorSchemeChanged, this, &ThemeManager::themeChangedSlot); #endif @@ -344,7 +351,7 @@ void ThemeManager::applyStyleAndPalette(const QString &themeName, } #endif } else { - base = qApp->palette(); + base = systemPalette; } // Overlay custom palette colours @@ -434,6 +441,20 @@ void ThemeManager::themeChangedSlot() emit themeChanged(); } +void ThemeManager::onColorSchemeChanged() +{ + // qApp->palette() is locked (is_app_palette = true), so the QPA won't push + // the new OS palette through automatically. style->polish(QPalette&) queries + // GetSysColor on Windows — adequate for light mode, imperfect for Win11 dark + // mode (which uses a different API), but better than a stale light snapshot. + QPalette fresh; + qApp->style()->polish(fresh); + if (fresh.color(QPalette::Window).isValid()) { + systemPalette = fresh; + } + themeChangedSlot(); +} + static QString roleBgName(ThemeManager::Role role) { switch (role) { diff --git a/cockatrice/src/interface/theme_manager.h b/cockatrice/src/interface/theme_manager.h index 64a021bfc..191c5927e 100644 --- a/cockatrice/src/interface/theme_manager.h +++ b/cockatrice/src/interface/theme_manager.h @@ -42,6 +42,7 @@ public: }; private: + QPalette systemPalette; QString defaultStyleName; QString currentThemePath; std::array brushes; @@ -89,6 +90,7 @@ public: QBrush getExtraBgBrush(Role zone, int zoneId = 0); protected slots: void themeChangedSlot(); + void onColorSchemeChanged(); signals: void themeChanged(); };