[PaletteEditor] Ensure directory is writeable, don't fall back to alternate palette (#7048)

* [PaletteEditor] Ensure directory is writeable, don't fall back to alternate palette.

Took 17 minutes

* [ThemeManager] Extract system theme dir path resolution to helper, use it in default palette fetching

Took 8 minutes

* [ThemeManager] Expose styling again

Took 3 minutes

* [ThemeManager] Capture app palette before theme is applied for reverting

* [ThemeManager] Simply delete custom palette instead of reverting to default

* [ThemeManager] Generate and apply immediately on change.

* [PaletteEditor] Block grid signals during initial loading.

Took 5 minutes

* Address comments

Took 8 minutes

* Seed accent from scheme on reset and revert

Took 2 minutes

---------

Co-authored-by: Lukas Brübach <Bruebach.Lukas@bdosecurity.de>
This commit is contained in:
BruebachL 2026-07-27 11:23:41 +02:00 committed by GitHub
parent 3f9dbdb33b
commit cf0b453e75
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 229 additions and 81 deletions

View file

@ -96,9 +96,14 @@ ThemeManager::ThemeManager(QObject *parent) : QObject(parent)
if (defaultStyleName == "windows11") {
defaultStyleName = "windowsvista";
}
// Capture the untouched application palette before any theme is applied.
defaultPalette = qApp->palette();
ensureThemeDirectoryExists();
#if (QT_VERSION >= QT_VERSION_CHECK(6, 5, 0))
connect(QGuiApplication::styleHints(), &QStyleHints::colorSchemeChanged, this, &ThemeManager::themeChangedSlot);
connect(QGuiApplication::styleHints(), &QStyleHints::colorSchemeChanged, this, [this] {
defaultPalette = qApp->palette();
themeChangedSlot();
});
#endif
connect(&SettingsCache::instance(), &SettingsCache::themeChanged, this, &ThemeManager::themeChangedSlot);
themeChangedSlot();
@ -137,6 +142,20 @@ bool ThemeManager::isBuiltInTheme()
return themeName == NONE_THEME_NAME || themeName == FUSION_THEME_NAME;
}
// System (read-only) themes location, relative to the application binary.
static QString systemThemesBasePath()
{
QString base = qApp->applicationDirPath();
#ifdef Q_OS_MAC
base += "/../Resources/themes";
#elif defined(Q_OS_WIN)
base += "/themes";
#else // linux
base += "/../share/cockatrice/themes";
#endif
return base;
}
QStringMap &ThemeManager::getAvailableThemes()
{
QDir dir;
@ -157,15 +176,7 @@ QStringMap &ThemeManager::getAvailableThemes()
}
// load themes from cockatrice system dir
dir.setPath(qApp->applicationDirPath() +
#ifdef Q_OS_MAC
"/../Resources/themes"
#elif defined(Q_OS_WIN)
"/themes"
#else // linux
"/../share/cockatrice/themes"
#endif
);
dir.setPath(systemThemesBasePath());
for (QString themeName : dir.entryList(QDir::AllDirs | QDir::NoDotAndDotDot, QDir::Name)) {
if (!availableThemes.contains(themeName)) {
@ -242,6 +253,20 @@ bool ThemeManager::savePaletteConfig(const QString &themeDirPath, const QString
return true;
}
PaletteConfig ThemeManager::loadDefaultPaletteConfig(const QString &themeDirPath,
const QString &themeName,
const QString &colorScheme)
{
PaletteConfig cfg = PaletteConfig::fromDefault(themeDirPath, colorScheme);
if (!cfg.hasPalette()) {
// The shipped default may live in the system theme directory rather
// than the resolved (user) theme directory, so built-in themes still
// get their curated defaults.
cfg = PaletteConfig::fromDefault(QDir(systemThemesBasePath()).absoluteFilePath(themeName), colorScheme);
}
return cfg;
}
void ThemeManager::setColorScheme(const QString &scheme)
{
const QString dirPath = getAvailableThemes().value(SettingsCache::instance().getThemeName());
@ -253,6 +278,17 @@ void ThemeManager::setColorScheme(const QString &scheme)
reloadCurrentTheme();
}
void ThemeManager::setStyleName(const QString &styleName)
{
const QString dirPath = getAvailableThemes().value(SettingsCache::instance().getThemeName());
ThemeConfig cfg = ThemeConfig::fromThemeDir(dirPath);
cfg.styleName = styleName;
cfg.save(dirPath);
reloadCurrentTheme();
}
void ThemeManager::reloadCurrentTheme()
{
themeChangedSlot();
@ -298,7 +334,11 @@ void ThemeManager::applyStyleAndPalette(const QString &themeName,
}
#endif
} else {
base = qApp->palette();
// Use the pristine startup palette rather than qApp->palette(): the
// latter may already carry a previously-applied custom (e.g. dark)
// palette, which would otherwise persist when switching to a scheme
// that supplies no palette of its own.
base = defaultPalette;
}
// Overlay custom palette colours
@ -353,7 +393,7 @@ void ThemeManager::themeChangedSlot()
// ── Load palette: custom first, then theme default ────────────────────
PaletteConfig palette = PaletteConfig::fromScheme(dirPath, activeScheme);
if (!palette.hasPalette()) {
palette = PaletteConfig::fromDefault(dirPath, activeScheme);
palette = ThemeManager::loadDefaultPaletteConfig(dirPath, themeName, activeScheme);
}
applyStyleAndPalette(themeName, themeCfg, palette, activeScheme);
@ -362,6 +402,16 @@ void ThemeManager::themeChangedSlot()
if (!dirPath.isEmpty()) {
resources << dir.absolutePath();
}
// When the resolved dir is a user copy (e.g. user/<theme>), also
// include the system theme dir as a fallback so shipped assets like
// zones/*.png and style.css still resolve for themes that ship only
// those files (e.g. Leather, Plasma, Fabric, VelvetMarble).
const QString sysPath = QDir(systemThemesBasePath()).absoluteFilePath(themeName);
if (sysPath != dirPath && QDir(sysPath).exists()) {
resources << sysPath;
}
resources << DEFAULT_RESOURCE_PATHS;
QDir::setSearchPaths("theme", resources);