update theme category indicator (#387)

This commit is contained in:
Cyano Hao 2024-04-17 12:48:47 +08:00 committed by GitHub
parent 9650393db7
commit 5fce5cead7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 50 additions and 52 deletions

View File

@ -92,7 +92,7 @@ void EnvironmentAppearanceWidget::init()
ThemeManager themeManager; ThemeManager themeManager;
QList<PAppTheme> appThemes = themeManager.getThemes(); QList<PAppTheme> appThemes = themeManager.getThemes();
foreach(const PAppTheme& appTheme, appThemes) { foreach(const PAppTheme& appTheme, appThemes) {
ui->cbTheme->addItem(appTheme->displayName(),appTheme->name()); ui->cbTheme->addItem(appTheme->categoryIcon() + " " + appTheme->displayName(), appTheme->name());
} }
ui->cbLanguage->addItem(tr("English"),"en"); ui->cbLanguage->addItem(tr("English"),"en");
ui->cbLanguage->addItem(tr("Portuguese"),"pt_BR"); ui->cbLanguage->addItem(tr("Portuguese"),"pt_BR");
@ -109,7 +109,6 @@ void EnvironmentAppearanceWidget::on_cbTheme_currentIndexChanged(int /* index */
{ {
ThemeManager themeManager; ThemeManager themeManager;
PAppTheme appTheme = themeManager.theme(ui->cbTheme->currentData().toString()); PAppTheme appTheme = themeManager.theme(ui->cbTheme->currentData().toString());
ui->lblThemeCategory->setText(appTheme->category());
if(!appTheme->defaultIconSet().isEmpty()) { if(!appTheme->defaultIconSet().isEmpty()) {
for (int i=0; i<ui->cbIconSet->count();i++) { for (int i=0; i<ui->cbIconSet->count();i++) {
if (ui->cbIconSet->itemData(i) == appTheme->defaultIconSet()) { if (ui->cbIconSet->itemData(i) == appTheme->defaultIconSet()) {

View File

@ -135,13 +135,6 @@
<item> <item>
<widget class="QComboBox" name="cbTheme"/> <widget class="QComboBox" name="cbTheme"/>
</item> </item>
<item>
<widget class="QLabel" name="lblThemeCategory">
<property name="text">
<string>theme category</string>
</property>
</widget>
</item>
<item> <item>
<spacer name="horizontalSpacer"> <spacer name="horizontalSpacer">
<property name="orientation"> <property name="orientation">

View File

@ -45,9 +45,9 @@ PAppTheme ThemeManager::theme(const QString &themeName)
PAppTheme appTheme = nullptr; PAppTheme appTheme = nullptr;
// custom overrides built-in // custom overrides built-in
if (tryLoadThemeFromDir(customThemeDir, tr("custom"), themeName, appTheme)) if (tryLoadThemeFromDir(customThemeDir, AppTheme::ThemeCategory::Custom, themeName, appTheme))
return appTheme; return appTheme;
if (tryLoadThemeFromDir(builtInThemeDir, tr("built-in"), themeName, appTheme)) if (tryLoadThemeFromDir(builtInThemeDir, AppTheme::ThemeCategory::BuiltIn, themeName, appTheme))
return appTheme; return appTheme;
return AppTheme::fallbackTheme(); return AppTheme::fallbackTheme();
} }
@ -59,8 +59,8 @@ QList<PAppTheme> ThemeManager::getThemes()
std::set<PAppTheme, ThemeCompare> themes; std::set<PAppTheme, ThemeCompare> themes;
// custom overrides built-in // custom overrides built-in
loadThemesFromDir(customThemeDir, tr("custom"), themes); loadThemesFromDir(customThemeDir, AppTheme::ThemeCategory::Custom, themes);
loadThemesFromDir(builtInThemeDir, tr("built-in"), themes); loadThemesFromDir(builtInThemeDir, AppTheme::ThemeCategory::BuiltIn, themes);
QList<PAppTheme> result(themes.begin(), themes.end()); QList<PAppTheme> result(themes.begin(), themes.end());
return result; return result;
@ -71,18 +71,14 @@ bool ThemeManager::ThemeCompare::operator()(const PAppTheme &lhs, const PAppThem
return QFileInfo(lhs->filename()).baseName() < QFileInfo(rhs->filename()).baseName(); return QFileInfo(lhs->filename()).baseName() < QFileInfo(rhs->filename()).baseName();
} }
bool ThemeManager::tryLoadThemeFromDir(const QString &dir, const QString &dirType, const QString &themeName, PAppTheme &theme) bool ThemeManager::tryLoadThemeFromDir(const QString &dir, AppTheme::ThemeCategory category, const QString &themeName, PAppTheme &theme)
{ {
for (const auto &[extension, type] : searchTypes) { for (const auto &[extension, type] : searchTypes) {
QString filename = QString("%2.%3").arg(themeName, extension); QString filename = QString("%2.%3").arg(themeName, extension);
QString fullPath = QString("%1/%2").arg(dir, filename); QString fullPath = QString("%1/%2").arg(dir, filename);
if (QFile::exists(fullPath)) { if (QFile::exists(fullPath)) {
try { try {
theme = std::make_shared<AppTheme>(fullPath, type); theme = std::make_shared<AppTheme>(fullPath, type, category);
if (theme->displayName().isEmpty()) {
theme->setDisplayName(themeName);
}
theme->setCategory(QString("%1 %2").arg(dirType, filename));
return true; return true;
} catch(FileError e) { } catch(FileError e) {
//just skip it //just skip it
@ -97,7 +93,7 @@ bool ThemeManager::tryLoadThemeFromDir(const QString &dir, const QString &dirTyp
return false; return false;
} }
void ThemeManager::loadThemesFromDir(const QString &dir, const QString &dirType, std::set<PAppTheme, ThemeCompare> &themes) void ThemeManager::loadThemesFromDir(const QString &dir, AppTheme::ThemeCategory category, std::set<PAppTheme, ThemeCompare> &themes)
{ {
for (const auto &[extension, type] : searchTypes) { for (const auto &[extension, type] : searchTypes) {
QDirIterator it(dir); QDirIterator it(dir);
@ -106,12 +102,7 @@ void ThemeManager::loadThemesFromDir(const QString &dir, const QString &dirType,
QFileInfo fileInfo = it.fileInfo(); QFileInfo fileInfo = it.fileInfo();
if (fileInfo.suffix().compare(extension, PATH_SENSITIVITY) == 0) { if (fileInfo.suffix().compare(extension, PATH_SENSITIVITY) == 0) {
try { try {
PAppTheme appTheme = std::make_shared<AppTheme>(fileInfo.absoluteFilePath(), type); PAppTheme appTheme = std::make_shared<AppTheme>(fileInfo.absoluteFilePath(), type, category);
if (appTheme->displayName().isEmpty()) {
QString baseName = fileInfo.baseName();
appTheme->setDisplayName(baseName);
}
appTheme->setCategory(QString("%1 %2").arg(dirType, fileInfo.fileName()));
themes.insert(appTheme); themes.insert(appTheme);
} catch(FileError e) { } catch(FileError e) {
//just skip it //just skip it
@ -198,9 +189,11 @@ QPalette AppTheme::palette() const
return pal; return pal;
} }
AppTheme::AppTheme(const QString &filename, ThemeType type, QObject *parent):QObject(parent) AppTheme::AppTheme(const QString &filename, ThemeType type, ThemeCategory category, QObject *parent):QObject(parent)
{ {
mFilename = filename; mFilename = filename;
mType = type;
mCategory = category;
QFile file(filename); QFile file(filename);
if (!file.exists()) { if (!file.exists()) {
throw FileError(tr("Theme file '%1' doesn't exist!") throw FileError(tr("Theme file '%1' doesn't exist!")
@ -213,6 +206,8 @@ AppTheme::AppTheme(const QString &filename, ThemeType type, QObject *parent):QOb
QJsonObject obj; QJsonObject obj;
switch (type) { switch (type) {
case ThemeType::HardCoded:
__builtin_unreachable();
case ThemeType::JSON: { case ThemeType::JSON: {
QJsonParseError error; QJsonParseError error;
QJsonDocument doc(QJsonDocument::fromJson(content, &error)); QJsonDocument doc(QJsonDocument::fromJson(content, &error));
@ -255,6 +250,8 @@ AppTheme::AppTheme(const QString &filename, ThemeType type, QObject *parent):QOb
QFileInfo fileInfo(filename); QFileInfo fileInfo(filename);
mName = fileInfo.baseName(); mName = fileInfo.baseName();
mDisplayName = obj["name"].toString(); mDisplayName = obj["name"].toString();
if (mDisplayName.isEmpty())
mDisplayName = mName;
mStyle = obj["style"].toString(); mStyle = obj["style"].toString();
mDefaultColorScheme = obj["default scheme"].toString(); mDefaultColorScheme = obj["default scheme"].toString();
mDefaultIconSet = obj["default iconset"].toString(); mDefaultIconSet = obj["default iconset"].toString();
@ -316,6 +313,22 @@ const QString &AppTheme::filename() const
return mFilename; return mFilename;
} }
const QString AppTheme::categoryIcon() const
{
switch (mCategory) {
case ThemeCategory::FailSafe:
return "";
case ThemeCategory::BuiltIn:
return "📦";
case ThemeCategory::Custom:
return "📄";
case ThemeCategory::Shared:
return "🌐";
default:
return "";
}
}
const QString &AppTheme::defaultIconSet() const const QString &AppTheme::defaultIconSet() const
{ {
return mDefaultIconSet; return mDefaultIconSet;
@ -336,21 +349,6 @@ const QString &AppTheme::displayName() const
return mDisplayName; return mDisplayName;
} }
void AppTheme::setDisplayName(const QString &newDisplayName)
{
mDisplayName = newDisplayName;
}
const QString &AppTheme::category() const
{
return mCategory;
}
void AppTheme::setCategory(const QString &newCategory)
{
mCategory = newCategory;
}
const QString &AppTheme::defaultColorScheme() const const QString &AppTheme::defaultColorScheme() const
{ {
return mDefaultColorScheme; return mDefaultColorScheme;
@ -369,10 +367,11 @@ const QString &AppTheme::style() const
AppTheme::AppTheme() : AppTheme::AppTheme() :
mName("__failsafe__theme__"), mName("__failsafe__theme__"),
mDisplayName("Fusion"), mDisplayName("Fusion"),
mCategory("fail-safe hard-coded"),
mStyle("fusion"), mStyle("fusion"),
mDefaultColorScheme("Adaptive"), mDefaultColorScheme("Adaptive"),
mDefaultIconSet("newlook") mDefaultIconSet("newlook"),
mType(ThemeType::HardCoded),
mCategory(ThemeCategory::FailSafe)
{ {
} }

View File

@ -78,13 +78,21 @@ public:
Q_ENUM(ColorRole) Q_ENUM(ColorRole)
enum class ThemeType { enum class ThemeType {
HardCoded,
JSON, JSON,
#ifdef ENABLE_LUA_ADDON #ifdef ENABLE_LUA_ADDON
Lua, Lua,
#endif #endif
}; };
AppTheme(const QString& filename, ThemeType type, QObject* parent=nullptr); enum class ThemeCategory {
FailSafe,
BuiltIn,
Custom,
Shared,
};
AppTheme(const QString& filename, ThemeType type, ThemeCategory category, QObject* parent=nullptr);
QColor color(ColorRole role) const; QColor color(ColorRole role) const;
QPalette palette() const; QPalette palette() const;
@ -95,10 +103,6 @@ public:
void setDefaultColorScheme(const QString &newDefaultColorScheme); void setDefaultColorScheme(const QString &newDefaultColorScheme);
const QString &displayName() const; const QString &displayName() const;
void setDisplayName(const QString &newDisplayName);
const QString &category() const;
void setCategory(const QString &newCategory);
const QString &name() const; const QString &name() const;
@ -110,6 +114,8 @@ public:
const QString& filename() const; const QString& filename() const;
const QString categoryIcon() const;
public: public:
static PAppTheme fallbackTheme(); static PAppTheme fallbackTheme();
@ -122,11 +128,12 @@ private:
QHash<int,QColor> mColors; QHash<int,QColor> mColors;
QString mName; QString mName;
QString mDisplayName; QString mDisplayName;
QString mCategory;
QString mStyle; QString mStyle;
QString mDefaultColorScheme; QString mDefaultColorScheme;
QString mDefaultIconSet; QString mDefaultIconSet;
QString mFilename; QString mFilename;
ThemeType mType;
ThemeCategory mCategory;
}; };
class ThemeManager : public QObject class ThemeManager : public QObject
@ -143,8 +150,8 @@ private:
}; };
private: private:
bool tryLoadThemeFromDir(const QString &dir, const QString &dirType, const QString &themeName, PAppTheme &theme); bool tryLoadThemeFromDir(const QString &dir, AppTheme::ThemeCategory category, const QString &themeName, PAppTheme &theme);
void loadThemesFromDir(const QString &dir, const QString &dirType, std::set<PAppTheme, ThemeCompare> &themes); void loadThemesFromDir(const QString &dir, AppTheme::ThemeCategory category, std::set<PAppTheme, ThemeCompare> &themes);
// lua overrides json // lua overrides json
inline static const std::pair<QString, AppTheme::ThemeType> searchTypes[] = { inline static const std::pair<QString, AppTheme::ThemeType> searchTypes[] = {