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
|
// SPDX-License-Identifier: GPL-3.0-only
/*
* Prism Launcher - Minecraft Launcher
* Copyright (C) 2022 Tayou <tayou@gmx.net>
*
* 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, version 3.
*
* 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/>.
*
* This file incorporates work covered by the following copyright and
* permission notice:
*
* Copyright 2013-2021 MultiMC Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "CustomTheme.h"
#include <FileSystem.h>
#include <Json.h>
#include "ThemeManager.h"
const char* themeFile = "theme.json";
static bool readThemeJson(const QString& path,
QPalette& palette,
double& fadeAmount,
QColor& fadeColor,
QString& name,
QString& widgets,
QString& qssFilePath,
bool& dataIncomplete)
{
QFileInfo pathInfo(path);
if (pathInfo.exists() && pathInfo.isFile()) {
try {
auto doc = Json::requireDocument(path, "Theme JSON file");
const QJsonObject root = doc.object();
dataIncomplete = !root.contains("qssFilePath");
name = Json::requireString(root, "name", "Theme name");
widgets = Json::requireString(root, "widgets", "Qt widget theme");
qssFilePath = Json::ensureString(root, "qssFilePath", "themeStyle.css");
auto colorsRoot = Json::requireObject(root, "colors", "colors object");
auto readColor = [&](QString colorName) -> QColor {
auto colorValue = Json::ensureString(colorsRoot, colorName, QString());
if (!colorValue.isEmpty()) {
QColor color(colorValue);
if (!color.isValid()) {
themeWarningLog() << "Color value" << colorValue << "for" << colorName << "was not recognized.";
return QColor();
}
return color;
}
return QColor();
};
auto readAndSetColor = [&](QPalette::ColorRole role, QString colorName) {
auto color = readColor(colorName);
if (color.isValid()) {
palette.setColor(role, color);
} else {
themeDebugLog() << "Color value for" << colorName << "was not present.";
}
};
// palette
readAndSetColor(QPalette::Window, "Window");
readAndSetColor(QPalette::WindowText, "WindowText");
readAndSetColor(QPalette::Base, "Base");
readAndSetColor(QPalette::AlternateBase, "AlternateBase");
readAndSetColor(QPalette::ToolTipBase, "ToolTipBase");
readAndSetColor(QPalette::ToolTipText, "ToolTipText");
readAndSetColor(QPalette::Text, "Text");
readAndSetColor(QPalette::Button, "Button");
readAndSetColor(QPalette::ButtonText, "ButtonText");
readAndSetColor(QPalette::BrightText, "BrightText");
readAndSetColor(QPalette::Link, "Link");
readAndSetColor(QPalette::Highlight, "Highlight");
readAndSetColor(QPalette::HighlightedText, "HighlightedText");
// fade
fadeColor = readColor("fadeColor");
fadeAmount = Json::ensureDouble(colorsRoot, "fadeAmount", 0.5, "fade amount");
} catch (const Exception& e) {
themeWarningLog() << "Couldn't load theme json: " << e.cause();
return false;
}
} else {
themeDebugLog() << "No theme json present.";
return false;
}
return true;
}
static bool writeThemeJson(const QString& path,
const QPalette& palette,
double fadeAmount,
QColor fadeColor,
QString name,
QString widgets,
QString qssFilePath)
{
QJsonObject rootObj;
rootObj.insert("name", name);
rootObj.insert("widgets", widgets);
rootObj.insert("qssFilePath", qssFilePath);
QJsonObject colorsObj;
auto insertColor = [&](QPalette::ColorRole role, QString colorName) { colorsObj.insert(colorName, palette.color(role).name()); };
// palette
insertColor(QPalette::Window, "Window");
insertColor(QPalette::WindowText, "WindowText");
insertColor(QPalette::Base, "Base");
insertColor(QPalette::AlternateBase, "AlternateBase");
insertColor(QPalette::ToolTipBase, "ToolTipBase");
insertColor(QPalette::ToolTipText, "ToolTipText");
insertColor(QPalette::Text, "Text");
insertColor(QPalette::Button, "Button");
insertColor(QPalette::ButtonText, "ButtonText");
insertColor(QPalette::BrightText, "BrightText");
insertColor(QPalette::Link, "Link");
insertColor(QPalette::Highlight, "Highlight");
insertColor(QPalette::HighlightedText, "HighlightedText");
// fade
colorsObj.insert("fadeColor", fadeColor.name());
colorsObj.insert("fadeAmount", fadeAmount);
rootObj.insert("colors", colorsObj);
try {
Json::write(rootObj, path);
return true;
} catch (const Exception& e) {
themeWarningLog() << "Failed to write theme json to" << path;
return false;
}
}
/// @param baseTheme Base Theme
/// @param fileInfo FileInfo object for file to load
/// @param isManifest whether to load a theme manifest or a qss file
CustomTheme::CustomTheme(ITheme* baseTheme, QFileInfo& fileInfo, bool isManifest)
{
if (isManifest) {
m_id = fileInfo.dir().dirName();
QString path = FS::PathCombine("themes", m_id);
QString pathResources = FS::PathCombine("themes", m_id, "resources");
if (!FS::ensureFolderPathExists(path) || !FS::ensureFolderPathExists(pathResources)) {
themeWarningLog() << "couldn't create folder for theme!";
m_palette = baseTheme->colorScheme();
m_styleSheet = baseTheme->appStyleSheet();
return;
}
auto themeFilePath = FS::PathCombine(path, themeFile);
bool jsonDataIncomplete = false;
m_palette = baseTheme->colorScheme();
if (!readThemeJson(themeFilePath, m_palette, m_fadeAmount, m_fadeColor, m_name, m_widgets, m_qssFilePath, jsonDataIncomplete)) {
themeDebugLog() << "Did not read theme json file correctly, writing new one to: " << themeFilePath;
m_name = "Custom";
m_palette = baseTheme->colorScheme();
m_fadeColor = baseTheme->fadeColor();
m_fadeAmount = baseTheme->fadeAmount();
m_widgets = baseTheme->qtTheme();
m_qssFilePath = "themeStyle.css";
} else {
m_palette = fadeInactive(m_palette, m_fadeAmount, m_fadeColor);
}
if (jsonDataIncomplete) {
writeThemeJson(fileInfo.absoluteFilePath(), m_palette, m_fadeAmount, m_fadeColor, m_name, m_widgets, m_qssFilePath);
}
auto qssFilePath = FS::PathCombine(path, m_qssFilePath);
QFileInfo info(qssFilePath);
if (info.isFile()) {
try {
// TODO: validate css?
m_styleSheet = QString::fromUtf8(FS::read(qssFilePath));
} catch (const Exception& e) {
themeWarningLog() << "Couldn't load css:" << e.cause() << "from" << qssFilePath;
m_styleSheet = baseTheme->appStyleSheet();
}
} else {
themeDebugLog() << "No theme css present.";
m_styleSheet = baseTheme->appStyleSheet();
try {
FS::write(qssFilePath, m_styleSheet.toUtf8());
} catch (const Exception& e) {
themeWarningLog() << "Couldn't write css:" << e.cause() << "to" << qssFilePath;
}
}
} else {
m_id = fileInfo.fileName();
m_name = fileInfo.baseName();
QString path = fileInfo.filePath();
// themeDebugLog << "Theme ID: " << m_id;
// themeDebugLog << "Theme Name: " << m_name;
// themeDebugLog << "Theme Path: " << path;
if (!FS::ensureFilePathExists(path)) {
themeWarningLog() << m_name << " Theme file path doesn't exist!";
m_palette = baseTheme->colorScheme();
m_styleSheet = baseTheme->appStyleSheet();
return;
}
m_palette = baseTheme->colorScheme();
try {
// TODO: validate qss?
m_styleSheet = QString::fromUtf8(FS::read(path));
} catch (const Exception& e) {
themeWarningLog() << "Couldn't load qss:" << e.cause() << "from" << path;
m_styleSheet = baseTheme->appStyleSheet();
}
}
}
QStringList CustomTheme::searchPaths()
{
return { FS::PathCombine("themes", m_id, "resources") };
}
QString CustomTheme::id()
{
return m_id;
}
QString CustomTheme::name()
{
return m_name;
}
bool CustomTheme::hasColorScheme()
{
return true;
}
QPalette CustomTheme::colorScheme()
{
return m_palette;
}
bool CustomTheme::hasStyleSheet()
{
return true;
}
QString CustomTheme::appStyleSheet()
{
return m_styleSheet;
}
double CustomTheme::fadeAmount()
{
return m_fadeAmount;
}
QColor CustomTheme::fadeColor()
{
return m_fadeColor;
}
QString CustomTheme::qtTheme()
{
return m_widgets;
}
|