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

Took 8 minutes
This commit is contained in:
Lukas Brübach 2026-07-20 21:43:14 +02:00
parent 6e41e3502a
commit d70349da4a
3 changed files with 38 additions and 14 deletions

View file

@ -176,8 +176,8 @@ void PaletteEditorDialog::retranslateUi()
titleLabel->setText(tr("<b>Palette Editor</b> &nbsp;·&nbsp; %1").arg(themeName)); titleLabel->setText(tr("<b>Palette Editor</b> &nbsp;·&nbsp; %1").arg(themeName));
// Revert button only makes sense when the theme ships default palette files // Revert button only makes sense when the theme ships default palette files
const bool hasDefault = PaletteConfig::fromDefault(themeDirPath, "Light").hasPalette() || const bool hasDefault = ThemeManager::loadDefaultPaletteConfig(themeDirPath, themeName, "Light").hasPalette() ||
PaletteConfig::fromDefault(themeDirPath, "Dark").hasPalette(); ThemeManager::loadDefaultPaletteConfig(themeDirPath, themeName, "Dark").hasPalette();
revertButton->setEnabled(hasDefault); revertButton->setEnabled(hasDefault);
if (!hasDefault) { if (!hasDefault) {
revertButton->setToolTip(tr("This theme ships no default palette files")); revertButton->setToolTip(tr("This theme ships no default palette files"));
@ -211,7 +211,7 @@ void PaletteEditorDialog::loadSchemes()
PaletteConfig cfg = PaletteConfig::fromScheme(themeDirPath, scheme); PaletteConfig cfg = PaletteConfig::fromScheme(themeDirPath, scheme);
if (!cfg.hasPalette()) { if (!cfg.hasPalette()) {
cfg = PaletteConfig::fromDefault(themeDirPath, scheme); cfg = ThemeManager::loadDefaultPaletteConfig(themeDirPath, themeName, scheme);
} }
if (!cfg.hasPalette()) { if (!cfg.hasPalette()) {
@ -309,7 +309,7 @@ void PaletteEditorDialog::onReset()
void PaletteEditorDialog::onRevertToDefault() void PaletteEditorDialog::onRevertToDefault()
{ {
PaletteConfig def = PaletteConfig::fromDefault(themeDirPath, loadedScheme); PaletteConfig def = ThemeManager::loadDefaultPaletteConfig(themeDirPath, themeName, loadedScheme);
if (!def.hasPalette()) { if (!def.hasPalette()) {
QMessageBox::information(this, tr("No default found"), QMessageBox::information(this, tr("No default found"),
tr("No default palette file found for the \"%1\" scheme.").arg(loadedScheme)); tr("No default palette file found for the \"%1\" scheme.").arg(loadedScheme));

View file

@ -137,6 +137,20 @@ bool ThemeManager::isBuiltInTheme()
return themeName == NONE_THEME_NAME || themeName == FUSION_THEME_NAME; 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() QStringMap &ThemeManager::getAvailableThemes()
{ {
QDir dir; QDir dir;
@ -157,15 +171,7 @@ QStringMap &ThemeManager::getAvailableThemes()
} }
// load themes from cockatrice system dir // load themes from cockatrice system dir
dir.setPath(qApp->applicationDirPath() + dir.setPath(systemThemesBasePath());
#ifdef Q_OS_MAC
"/../Resources/themes"
#elif defined(Q_OS_WIN)
"/themes"
#else // linux
"/../share/cockatrice/themes"
#endif
);
for (QString themeName : dir.entryList(QDir::AllDirs | QDir::NoDotAndDotDot, QDir::Name)) { for (QString themeName : dir.entryList(QDir::AllDirs | QDir::NoDotAndDotDot, QDir::Name)) {
if (!availableThemes.contains(themeName)) { if (!availableThemes.contains(themeName)) {
@ -242,6 +248,20 @@ bool ThemeManager::savePaletteConfig(const QString &themeDirPath, const QString
return true; 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) void ThemeManager::setColorScheme(const QString &scheme)
{ {
const QString dirPath = getAvailableThemes().value(SettingsCache::instance().getThemeName()); const QString dirPath = getAvailableThemes().value(SettingsCache::instance().getThemeName());
@ -353,7 +373,7 @@ void ThemeManager::themeChangedSlot()
// ── Load palette: custom first, then theme default ──────────────────── // ── Load palette: custom first, then theme default ────────────────────
PaletteConfig palette = PaletteConfig::fromScheme(dirPath, activeScheme); PaletteConfig palette = PaletteConfig::fromScheme(dirPath, activeScheme);
if (!palette.hasPalette()) { if (!palette.hasPalette()) {
palette = PaletteConfig::fromDefault(dirPath, activeScheme); palette = ThemeManager::loadDefaultPaletteConfig(dirPath, themeName, activeScheme);
} }
applyStyleAndPalette(themeName, themeCfg, palette, activeScheme); applyStyleAndPalette(themeName, themeCfg, palette, activeScheme);

View file

@ -76,6 +76,10 @@ public:
// Load/save per-scheme palette colors // Load/save per-scheme palette colors
static PaletteConfig loadPaletteConfig(const QString &themeDirPath, const QString &colorScheme); static PaletteConfig loadPaletteConfig(const QString &themeDirPath, const QString &colorScheme);
static bool savePaletteConfig(const QString &themeDirPath, const QString &colorScheme, const PaletteConfig &cfg); static bool savePaletteConfig(const QString &themeDirPath, const QString &colorScheme, const PaletteConfig &cfg);
// Load the theme's shipped default palette, falling back to the system
// theme directory when it is absent from the resolved (user) directory.
static PaletteConfig
loadDefaultPaletteConfig(const QString &themeDirPath, const QString &themeName, const QString &colorScheme);
void setColorScheme(const QString &scheme); void setColorScheme(const QString &scheme);
void reloadCurrentTheme(); void reloadCurrentTheme();