mirror of
https://github.com/YACReader/yacreader
synced 2026-04-12 15:49:53 -04:00
Add more built in themes.
Some checks failed
Build / Initialization (push) Has been cancelled
Build / Code Format Validation (push) Has been cancelled
Build / Linux (Qt6) (push) Has been cancelled
Build / Linux (Qt6 + 7zip) (push) Has been cancelled
Build / macOS (Qt6 Universal) (push) Has been cancelled
Build / Windows x64 (Qt6) (push) Has been cancelled
Build / Windows ARM64 (Qt6) (push) Has been cancelled
Build / Docker amd64 Image (push) Has been cancelled
Build / Docker arm64 Image (push) Has been cancelled
Build / Publish Dev Builds (push) Has been cancelled
Build / Publish Release (push) Has been cancelled
Build / Publish YACReader10 Pre-release Builds (push) Has been cancelled
Some checks failed
Build / Initialization (push) Has been cancelled
Build / Code Format Validation (push) Has been cancelled
Build / Linux (Qt6) (push) Has been cancelled
Build / Linux (Qt6 + 7zip) (push) Has been cancelled
Build / macOS (Qt6 Universal) (push) Has been cancelled
Build / Windows x64 (Qt6) (push) Has been cancelled
Build / Windows ARM64 (Qt6) (push) Has been cancelled
Build / Docker amd64 Image (push) Has been cancelled
Build / Docker arm64 Image (push) Has been cancelled
Build / Publish Dev Builds (push) Has been cancelled
Build / Publish Release (push) Has been cancelled
Build / Publish YACReader10 Pre-release Builds (push) Has been cancelled
This commit is contained in:
@ -1,12 +1,37 @@
|
||||
#include "theme_repository.h"
|
||||
#include "theme_json_utils.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <QDir>
|
||||
#include <QFile>
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonObject>
|
||||
#include <QUuid>
|
||||
|
||||
namespace {
|
||||
QString builtinNameFromFileName(QString fileName)
|
||||
{
|
||||
if (fileName.endsWith(".json"))
|
||||
fileName.chop(5);
|
||||
|
||||
if (fileName.startsWith("builtin_"))
|
||||
fileName.remove(0, 8);
|
||||
|
||||
return fileName;
|
||||
}
|
||||
|
||||
int builtinSortRank(const QString &name)
|
||||
{
|
||||
if (name == QStringLiteral("classic"))
|
||||
return 0;
|
||||
if (name == QStringLiteral("light"))
|
||||
return 1;
|
||||
if (name == QStringLiteral("dark"))
|
||||
return 2;
|
||||
return 3;
|
||||
}
|
||||
}
|
||||
|
||||
ThemeRepository::ThemeRepository(const QString &qrcPrefix, const QString &userThemesDir, const QString &targetApp)
|
||||
: qrcPrefix(qrcPrefix), userThemesDir(userThemesDir), targetApp(targetApp)
|
||||
{
|
||||
@ -43,9 +68,19 @@ bool ThemeRepository::contains(const QString &themeId) const
|
||||
|
||||
QJsonObject ThemeRepository::loadThemeJson(const QString &themeId) const
|
||||
{
|
||||
for (const auto &b : builtins)
|
||||
if (b.id == themeId)
|
||||
return readJsonFile(b.resourcePath);
|
||||
for (const auto &b : builtins) {
|
||||
if (b.id != themeId)
|
||||
continue;
|
||||
|
||||
QJsonObject json = readJsonFile(b.resourcePath);
|
||||
if (json.isEmpty())
|
||||
return { };
|
||||
|
||||
auto meta = json["meta"].toObject();
|
||||
meta["id"] = b.id;
|
||||
json["meta"] = meta;
|
||||
return json;
|
||||
}
|
||||
|
||||
for (const auto &u : userThemes)
|
||||
if (u.id == themeId)
|
||||
@ -145,10 +180,24 @@ void ThemeRepository::scanBuiltins()
|
||||
{
|
||||
builtins.clear();
|
||||
|
||||
static const QStringList builtinNames = { "classic", "light", "dark" };
|
||||
QDir dir(qrcPrefix);
|
||||
QStringList builtinFiles = dir.entryList({ QStringLiteral("builtin_*.json") }, QDir::Files, QDir::Name);
|
||||
std::sort(builtinFiles.begin(), builtinFiles.end(), [](const QString &lhs, const QString &rhs) {
|
||||
const QString lhsName = builtinNameFromFileName(lhs);
|
||||
const QString rhsName = builtinNameFromFileName(rhs);
|
||||
const int lhsRank = builtinSortRank(lhsName);
|
||||
const int rhsRank = builtinSortRank(rhsName);
|
||||
if (lhsRank != rhsRank)
|
||||
return lhsRank < rhsRank;
|
||||
return lhsName < rhsName;
|
||||
});
|
||||
|
||||
for (const auto &name : builtinNames) {
|
||||
const QString resourcePath = qrcPrefix + "/builtin_" + name + ".json";
|
||||
for (const auto &fileName : builtinFiles) {
|
||||
const QString name = builtinNameFromFileName(fileName);
|
||||
if (name.isEmpty())
|
||||
continue;
|
||||
|
||||
const QString resourcePath = dir.absoluteFilePath(fileName);
|
||||
const QJsonObject json = readJsonFile(resourcePath);
|
||||
if (json.isEmpty())
|
||||
continue;
|
||||
|
||||
Reference in New Issue
Block a user