Snapshot system palette.

Took 8 minutes


Took 11 seconds
This commit is contained in:
Lukas Brübach 2026-06-20 01:26:04 +02:00
parent 19b3e8b286
commit 3de441a823
2 changed files with 24 additions and 1 deletions

View file

@ -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) {

View file

@ -42,6 +42,7 @@ public:
};
private:
QPalette systemPalette;
QString defaultStyleName;
QString currentThemePath;
std::array<QBrush, Role::MaxRole + 1> brushes;
@ -89,6 +90,7 @@ public:
QBrush getExtraBgBrush(Role zone, int zoneId = 0);
protected slots:
void themeChangedSlot();
void onColorSchemeChanged();
signals:
void themeChanged();
};