mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-11-05 09:33:25 -08:00
[Fonts] Wire FontConfig into ThemeManager with appFont and preview
This commit is contained in:
parent
3def6b00ef
commit
d854bdfd83
2 changed files with 89 additions and 0 deletions
|
|
@ -440,6 +440,34 @@ PaletteConfig ThemeManager::loadDefaultPaletteConfig(const QString &themeDirPath
|
||||||
return cfg;
|
return cfg;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
FontConfig ThemeManager::loadFontConfig(const QString &themeDirPath)
|
||||||
|
{
|
||||||
|
if (themeDirPath.isEmpty()) {
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
return FontConfig::fromThemeDir(themeDirPath);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ThemeManager::saveFontConfig(const QString &themeDirPath, const FontConfig &cfg)
|
||||||
|
{
|
||||||
|
if (themeDirPath.isEmpty()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
QDir dir(themeDirPath);
|
||||||
|
if (!dir.exists()) {
|
||||||
|
dir.mkpath(".");
|
||||||
|
}
|
||||||
|
|
||||||
|
QFile f(dir.absoluteFilePath(FontConfig::fileName()));
|
||||||
|
if (!f.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
QTextStream(&f) << cfg.toToml();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
bool ThemeManager::commitPalette(const QString &themeDirPath, const QString &colorScheme, const PaletteConfig &cfg)
|
bool ThemeManager::commitPalette(const QString &themeDirPath, const QString &colorScheme, const PaletteConfig &cfg)
|
||||||
{
|
{
|
||||||
if (!savePaletteConfig(themeDirPath, colorScheme, cfg)) {
|
if (!savePaletteConfig(themeDirPath, colorScheme, cfg)) {
|
||||||
|
|
@ -488,6 +516,27 @@ void ThemeManager::previewPalette(const PaletteConfig &cfg, const QString &schem
|
||||||
applyStyleAndPalette(themeName, themeCfg, cfg, scheme);
|
applyStyleAndPalette(themeName, themeCfg, cfg, scheme);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ThemeManager::previewFonts(const FontConfig &cfg)
|
||||||
|
{
|
||||||
|
currentFonts = cfg;
|
||||||
|
|
||||||
|
const auto it = cfg.fonts.constFind(AppFont::InterfaceDefault);
|
||||||
|
if (it != cfg.fonts.constEnd()) {
|
||||||
|
qApp->setFont(it.value());
|
||||||
|
|
||||||
|
// Repolish every widget immediately so the preview applies without
|
||||||
|
// waiting for natural expose events, mirroring applyStyleAndPalette.
|
||||||
|
QStyle *style = qApp->style();
|
||||||
|
for (QWidget *widget : qApp->allWidgets()) {
|
||||||
|
style->unpolish(widget);
|
||||||
|
style->polish(widget);
|
||||||
|
widget->update();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
emit fontsChanged();
|
||||||
|
}
|
||||||
|
|
||||||
void ThemeManager::applyStyleAndPalette(const QString &themeName,
|
void ThemeManager::applyStyleAndPalette(const QString &themeName,
|
||||||
const ThemeConfig &themeCfg,
|
const ThemeConfig &themeCfg,
|
||||||
const PaletteConfig &palCfg,
|
const PaletteConfig &palCfg,
|
||||||
|
|
@ -591,6 +640,18 @@ QColor ThemeManager::appColor(AppColor::Role role) const
|
||||||
return accent;
|
return accent;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QFont ThemeManager::appFont(AppFont::Role role) const
|
||||||
|
{
|
||||||
|
const auto it = currentFonts.fonts.constFind(role);
|
||||||
|
if (it != currentFonts.fonts.constEnd()) {
|
||||||
|
return it.value();
|
||||||
|
}
|
||||||
|
|
||||||
|
// No per-role scaling: every unset role resolves to the application font,
|
||||||
|
// so with no fonts.toml present the result is identical to before.
|
||||||
|
return qApp->font();
|
||||||
|
}
|
||||||
|
|
||||||
void ThemeManager::themeChangedSlot()
|
void ThemeManager::themeChangedSlot()
|
||||||
{
|
{
|
||||||
QString themeName = SettingsCache::instance().getThemeName();
|
QString themeName = SettingsCache::instance().getThemeName();
|
||||||
|
|
@ -642,6 +703,24 @@ void ThemeManager::themeChangedSlot()
|
||||||
|
|
||||||
applyStyleAndPalette(themeName, themeCfg, palette, activeScheme);
|
applyStyleAndPalette(themeName, themeCfg, palette, activeScheme);
|
||||||
|
|
||||||
|
// ── Load fonts: shared per theme, no light/dark split ───────────────────
|
||||||
|
FontConfig fonts = loadFontConfig(dirPath);
|
||||||
|
if (!fonts.hasFonts()) {
|
||||||
|
// The shipped fonts.toml may live in the system theme directory rather
|
||||||
|
// than the resolved (user) theme directory, so built-in themes still
|
||||||
|
// get their curated fonts.
|
||||||
|
const QString sysFontPath = QDir(systemThemesBasePath()).absoluteFilePath(themeName);
|
||||||
|
if (sysFontPath != dirPath) {
|
||||||
|
fonts = loadFontConfig(sysFontPath);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
currentFonts = fonts;
|
||||||
|
const auto fontIt = currentFonts.fonts.constFind(AppFont::InterfaceDefault);
|
||||||
|
if (fontIt != currentFonts.fonts.constEnd()) {
|
||||||
|
qApp->setFont(fontIt.value());
|
||||||
|
}
|
||||||
|
emit fontsChanged();
|
||||||
|
|
||||||
QStringList resources;
|
QStringList resources;
|
||||||
if (!dirPath.isEmpty()) {
|
if (!dirPath.isEmpty()) {
|
||||||
resources << dir.absolutePath();
|
resources << dir.absolutePath();
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,7 @@
|
||||||
|
|
||||||
#include <QBrush>
|
#include <QBrush>
|
||||||
#include <QColor>
|
#include <QColor>
|
||||||
|
#include <QFont>
|
||||||
#include <QLoggingCategory>
|
#include <QLoggingCategory>
|
||||||
#include <QMap>
|
#include <QMap>
|
||||||
#include <QObject>
|
#include <QObject>
|
||||||
|
|
@ -51,6 +52,7 @@ private:
|
||||||
std::array<QBrush, Role::MaxRole + 1> brushes;
|
std::array<QBrush, Role::MaxRole + 1> brushes;
|
||||||
QStringMap availableThemes;
|
QStringMap availableThemes;
|
||||||
QMap<AppColor::Role, QColor> currentAppColors;
|
QMap<AppColor::Role, QColor> currentAppColors;
|
||||||
|
FontConfig currentFonts;
|
||||||
/*
|
/*
|
||||||
Internal cache for multiple backgrounds
|
Internal cache for multiple backgrounds
|
||||||
*/
|
*/
|
||||||
|
|
@ -105,6 +107,9 @@ 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/save shared per-theme fonts (fonts.toml, not split by scheme)
|
||||||
|
static FontConfig loadFontConfig(const QString &themeDirPath);
|
||||||
|
static bool saveFontConfig(const QString &themeDirPath, const FontConfig &cfg);
|
||||||
// Resolve prefix to a scheme-qualified "theme:" path. Existence is probed
|
// Resolve prefix to a scheme-qualified "theme:" path. Existence is probed
|
||||||
// internally across the formats themes may ship (.png/.jpg/.svg), so
|
// internally across the formats themes may ship (.png/.jpg/.svg), so
|
||||||
// callers load the returned path directly. Prefers "<prefix>-<dark|light>"
|
// callers load the returned path directly. Prefers "<prefix>-<dark|light>"
|
||||||
|
|
@ -132,10 +137,14 @@ public:
|
||||||
|
|
||||||
void reloadCurrentTheme();
|
void reloadCurrentTheme();
|
||||||
void previewPalette(const PaletteConfig &cfg, const QString &scheme);
|
void previewPalette(const PaletteConfig &cfg, const QString &scheme);
|
||||||
|
void previewFonts(const FontConfig &cfg);
|
||||||
|
|
||||||
// Resolves an application color role: the theme's stored [AppColors] value
|
// Resolves an application color role: the theme's stored [AppColors] value
|
||||||
// when present, otherwise a palette-accent-derived fallback.
|
// when present, otherwise a palette-accent-derived fallback.
|
||||||
QColor appColor(AppColor::Role role) const;
|
QColor appColor(AppColor::Role role) const;
|
||||||
|
/** @brief Resolves an application font role: the theme's stored [Fonts]
|
||||||
|
* value when present, otherwise the application font. */
|
||||||
|
QFont appFont(AppFont::Role role) const;
|
||||||
|
|
||||||
QBrush &getBgBrush(Role zone);
|
QBrush &getBgBrush(Role zone);
|
||||||
QBrush getExtraBgBrush(Role zone, int zoneId = 0);
|
QBrush getExtraBgBrush(Role zone, int zoneId = 0);
|
||||||
|
|
@ -144,6 +153,7 @@ protected slots:
|
||||||
signals:
|
signals:
|
||||||
void themeChanged();
|
void themeChanged();
|
||||||
void paletteChanged();
|
void paletteChanged();
|
||||||
|
void fontsChanged();
|
||||||
};
|
};
|
||||||
|
|
||||||
extern ThemeManager *themeManager;
|
extern ThemeManager *themeManager;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue