1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
|
/* SPDX-FileCopyrightText: 2026 Project Tick
* SPDX-FileContributor: Project Tick
* SPDX-License-Identifier: GPL-3.0-or-later
*
* MeshMC - A Custom Launcher for Minecraft
* Copyright (C) 2026 Project Tick
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "ThemeManager.h"
#include "ITheme.h"
#include "SystemTheme.h"
#include "DarkTheme.h"
#include "BrightTheme.h"
#include "CustomTheme.h"
#include "CatPack.h"
#include "Application.h"
#include "Exception.h"
#include <QApplication>
#include <QDebug>
#include <QDirIterator>
#include <QImageReader>
#include <QSet>
#include <QStyle>
#include <QStyleFactory>
#include <QSysInfo>
#include <xdgicon.h>
ThemeManager::ThemeManager()
{
const auto& style = QApplication::style();
m_defaultStyle = style->objectName();
m_defaultPalette = QApplication::palette();
// Default "System" theme
addTheme(
std::make_unique<SystemTheme>(m_defaultStyle, m_defaultPalette, true));
// Built-in Fusion themes
auto darkTheme = new DarkTheme();
addTheme(std::unique_ptr<ITheme>(darkTheme));
addTheme(std::make_unique<BrightTheme>());
// System widget themes from QStyleFactory
QStringList styles = QStyleFactory::keys();
for (auto& st : styles) {
#ifdef Q_OS_WINDOWS
if (QSysInfo::productVersion() != "11" && st == "windows11") {
continue;
}
#endif
addTheme(std::make_unique<SystemTheme>(st, m_defaultPalette, false));
}
// Custom theme
addTheme(std::make_unique<CustomTheme>(darkTheme, "custom"));
initIconThemes();
initializeCatPacks();
}
void ThemeManager::addTheme(std::unique_ptr<ITheme> theme)
{
QString id = theme->id();
m_themes.insert(std::make_pair(id, std::move(theme)));
}
ITheme* ThemeManager::getTheme(const QString& id)
{
auto it = m_themes.find(id);
if (it != m_themes.end()) {
return it->second.get();
}
return nullptr;
}
void ThemeManager::setApplicationTheme(const QString& id, bool initial)
{
auto theme = getTheme(id);
if (theme) {
theme->apply(initial);
} else {
qWarning() << "Tried to set invalid theme:" << id;
}
}
void ThemeManager::setIconTheme(const QString& name)
{
XdgIcon::setThemeName(name);
}
void ThemeManager::applyCurrentlySelectedTheme(bool initial)
{
auto settings = APPLICATION->settings();
// Apply widget theme first (sets palette)
auto applicationTheme = settings->get("ApplicationTheme").toString();
if (applicationTheme.isEmpty()) {
applicationTheme = "system";
}
setApplicationTheme(applicationTheme, initial);
// Auto-resolve icon variant based on the now-active palette brightness
auto iconTheme = settings->get("IconTheme").toString();
if (!iconTheme.isEmpty()) {
auto resolved = bestIconThemeForPalette(iconTheme);
if (resolved != iconTheme) {
settings->set("IconTheme", resolved);
}
setIconTheme(resolved);
}
}
std::vector<ITheme*> ThemeManager::allThemes()
{
std::vector<ITheme*> ret;
for (auto& pair : m_themes) {
ret.push_back(pair.second.get());
}
return ret;
}
QStringList ThemeManager::families()
{
QStringList ret;
QSet<QString> seen;
for (auto& pair : m_themes) {
QString fam = pair.second->family();
if (!seen.contains(fam)) {
seen.insert(fam);
ret.append(fam);
}
}
return ret;
}
std::vector<ITheme*> ThemeManager::themesInFamily(const QString& family)
{
std::vector<ITheme*> ret;
for (auto& pair : m_themes) {
if (pair.second->family() == family) {
ret.push_back(pair.second.get());
}
}
return ret;
}
QList<IconThemeEntry> ThemeManager::iconThemes() const
{
return m_iconThemes;
}
QStringList ThemeManager::iconThemeFamilies() const
{
QStringList ret;
QSet<QString> seen;
for (const auto& entry : m_iconThemes) {
const QString& fam = entry.family;
if (!seen.contains(fam)) {
seen.insert(fam);
ret.append(fam);
}
}
return ret;
}
QList<IconThemeEntry>
ThemeManager::iconThemesInFamily(const QString& family) const
{
QList<IconThemeEntry> ret;
for (const auto& entry : m_iconThemes) {
if (entry.family == family) {
ret.append(entry);
}
}
return ret;
}
QString ThemeManager::resolveIconTheme(const QString& family) const
{
auto entries = iconThemesInFamily(family);
if (entries.size() <= 1) {
return entries.isEmpty() ? QString() : entries[0].id;
}
// Check if family has variants
bool hasVariants = false;
for (const auto& entry : entries) {
if (!entry.variant.isEmpty()) {
hasVariants = true;
break;
}
}
if (!hasVariants) {
return entries[0].id;
}
// Auto-detect based on current palette brightness
auto windowColor = QApplication::palette().color(QPalette::Window);
bool isDark = windowColor.lightnessF() < 0.5;
for (const auto& entry : entries) {
QString v = entry.variant.toLower();
if (isDark && v == "dark")
return entry.id;
if (!isDark && v == "light")
return entry.id;
}
return entries[0].id;
}
QString
ThemeManager::bestIconThemeForPalette(const QString& currentIconId) const
{
// Find the family of the current icon theme
QString family;
for (const auto& entry : m_iconThemes) {
if (entry.id == currentIconId) {
family = entry.family;
break;
}
}
if (family.isEmpty()) {
return currentIconId;
}
// Resolve the best variant for that family based on current palette
QString resolved = resolveIconTheme(family);
return resolved.isEmpty() ? currentIconId : resolved;
}
void ThemeManager::initIconThemes()
{
m_iconThemes = {
{"pe_colored", QObject::tr("Default"), QObject::tr("Default"),
QString()},
{"multimc", QStringLiteral("MultiMC"), QStringLiteral("MultiMC"),
QString()},
{"pe_dark", QObject::tr("Simple (Dark Icons)"),
QObject::tr("Simple (Dark Icons)"), QString()},
{"pe_light", QObject::tr("Simple (Light Icons)"),
QObject::tr("Simple (Light Icons)"), QString()},
{"pe_blue", QObject::tr("Simple (Blue Icons)"),
QObject::tr("Simple (Blue Icons)"), QString()},
{"pe_colored", QObject::tr("Simple (Colored Icons)"),
QObject::tr("Simple (Colored Icons)"), QString()},
{"OSX", QStringLiteral("OSX"), QStringLiteral("OSX"), QString()},
{"iOS", QStringLiteral("iOS"), QStringLiteral("iOS"), QString()},
{"flat", QStringLiteral("Flat Light"), QStringLiteral("Flat"),
QObject::tr("Light")},
{"flat_white", QStringLiteral("Flat Dark"), QStringLiteral("Flat"),
QObject::tr("Dark")},
{"breeze_dark", QStringLiteral("Breeze Dark"), QStringLiteral("Breeze"),
QObject::tr("Dark")},
{"breeze_light", QStringLiteral("Breeze Light"),
QStringLiteral("Breeze"), QObject::tr("Light")},
{"custom", QObject::tr("Custom"), QObject::tr("Custom"), QString()},
};
}
void ThemeManager::initializeCatPacks()
{
QList<std::pair<QString, QString>> defaultCats{
{"kitteh", QObject::tr("Background Cat (from MultiMC)")},
{"rory", QObject::tr("Rory ID 11 (drawn by Ashtaka)")},
{"rory-flat",
QObject::tr("Rory ID 11 (flat edition, drawn by Ashtaka)")},
{"teawie", QObject::tr("Teawie (drawn by SympathyTea)")}};
for (const auto& [id, name] : defaultCats) {
addCatPack(std::make_unique<BasicCatPack>(id, name));
}
// Create catpacks folder in data directory
m_catPacksFolder = QDir("catpacks");
if (!m_catPacksFolder.mkpath("."))
qWarning() << "Couldn't create catpacks folder";
QStringList supportedImageFormats;
for (const auto& format : QImageReader::supportedImageFormats()) {
supportedImageFormats.append("*." + format);
}
auto loadFiles = [this, &supportedImageFormats](const QDir& dir) {
QDirIterator it(dir.absolutePath(), supportedImageFormats, QDir::Files);
while (it.hasNext()) {
QFileInfo info(it.next());
addCatPack(std::make_unique<FileCatPack>(info));
}
};
// Load image files in catpacks folder root
loadFiles(m_catPacksFolder);
// Load subdirectories
QDirIterator directoryIterator(m_catPacksFolder.path(),
QDir::Dirs | QDir::NoDotAndDotDot);
while (directoryIterator.hasNext()) {
QDir dir(directoryIterator.next());
QFileInfo manifest(dir.absoluteFilePath("catpack.json"));
if (manifest.isFile()) {
try {
addCatPack(std::make_unique<JsonCatPack>(manifest));
} catch (const Exception& e) {
qWarning() << "Couldn't load catpack json:" << e.cause();
}
} else {
loadFiles(dir);
}
}
}
void ThemeManager::addCatPack(std::unique_ptr<CatPack> catPack)
{
QString id = catPack->id();
if (m_catPacks.find(id) == m_catPacks.end())
m_catPacks.emplace(id, std::move(catPack));
else
qWarning() << "CatPack" << id << "not added to prevent id duplication";
}
QString ThemeManager::getCatPack(const QString& catName)
{
QString id = catName.isEmpty()
? APPLICATION->settings()->get("BackgroundCat").toString()
: catName;
auto it = m_catPacks.find(id);
if (it != m_catPacks.end())
return it->second->path();
// Fallback to first available
if (!m_catPacks.empty())
return m_catPacks.begin()->second->path();
return QString();
}
QList<CatPack*> ThemeManager::getValidCatPacks()
{
QList<CatPack*> ret;
ret.reserve(m_catPacks.size());
for (auto& [id, pack] : m_catPacks) {
ret.append(pack.get());
}
return ret;
}
QDir ThemeManager::getCatPacksFolder()
{
return m_catPacksFolder;
}
|