2022-10-29 22:57:20 +05:30
|
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
|
|
|
/*
|
|
|
|
* Prism 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
|
|
|
|
*
|
|
|
|
* Authors: Andrew Okin
|
|
|
|
* Peterix
|
|
|
|
* Orochimarufan <orochimarufan.x3@gmail.com>
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
2016-11-06 08:59:12 +05:30
|
|
|
#include "CustomTheme.h"
|
|
|
|
#include <Json.h>
|
|
|
|
#include <FileSystem.h>
|
2022-10-29 22:57:20 +05:30
|
|
|
#include "ThemeManager.h"
|
2016-11-06 08:59:12 +05:30
|
|
|
|
|
|
|
const char * themeFile = "theme.json";
|
|
|
|
|
2022-10-29 22:57:20 +05:30
|
|
|
static bool readThemeJson(const QString &path, QPalette &palette, double &fadeAmount, QColor &fadeColor, QString &name, QString &widgets, QString &qssFilePath, bool &dataIncomplete)
|
2016-11-06 08:59:12 +05:30
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
QFileInfo pathInfo(path);
|
|
|
|
if(pathInfo.exists() && pathInfo.isFile())
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto doc = Json::requireDocument(path, "Theme JSON file");
|
|
|
|
const QJsonObject root = doc.object();
|
2022-10-29 22:57:20 +05:30
|
|
|
dataIncomplete = !root.contains("qssFilePath");
|
2018-07-15 18:21:05 +05:30
|
|
|
name = Json::requireString(root, "name", "Theme name");
|
|
|
|
widgets = Json::requireString(root, "widgets", "Qt widget theme");
|
2022-10-29 22:57:20 +05:30
|
|
|
qssFilePath = Json::ensureString(root, "qssFilePath", "themeStyle.css");
|
|
|
|
//auto colorFileList = Json::ensureArray(root, "colorFiles");
|
2018-07-15 18:21:05 +05:30
|
|
|
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())
|
|
|
|
{
|
2022-11-01 20:11:08 +05:30
|
|
|
themeWarningLog() << "Color value" << colorValue << "for" << colorName << "was not recognized.";
|
2018-07-15 18:21:05 +05:30
|
|
|
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
|
|
|
|
{
|
2022-11-01 20:11:08 +05:30
|
|
|
themeDebugLog() << "Color value for" << colorName << "was not present.";
|
2018-07-15 18:21:05 +05:30
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
{
|
2022-11-01 20:11:08 +05:30
|
|
|
themeWarningLog() << "Couldn't load theme json: " << e.cause();
|
2018-07-15 18:21:05 +05:30
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-11-01 20:11:08 +05:30
|
|
|
themeDebugLog() << "No theme json present.";
|
2018-07-15 18:21:05 +05:30
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2016-11-06 08:59:12 +05:30
|
|
|
}
|
|
|
|
|
2022-10-29 22:57:20 +05:30
|
|
|
static bool writeThemeJson(const QString &path, const QPalette &palette, double fadeAmount, QColor fadeColor, QString name, QString widgets, QString qssFilePath)
|
2016-11-06 08:59:12 +05:30
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
QJsonObject rootObj;
|
|
|
|
rootObj.insert("name", name);
|
|
|
|
rootObj.insert("widgets", widgets);
|
2022-10-29 22:57:20 +05:30
|
|
|
rootObj.insert("qssFilePath", qssFilePath);
|
2018-07-15 18:21:05 +05:30
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2022-11-01 20:11:08 +05:30
|
|
|
themeWarningLog() << "Failed to write theme json to" << path;
|
2018-07-15 18:21:05 +05:30
|
|
|
return false;
|
|
|
|
}
|
2016-11-06 08:59:12 +05:30
|
|
|
}
|
|
|
|
|
2022-10-29 22:57:20 +05:30
|
|
|
/// @brief
|
|
|
|
/// @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)
|
2016-11-06 08:59:12 +05:30
|
|
|
{
|
2022-10-29 22:57:20 +05:30
|
|
|
if (isManifest) {
|
|
|
|
m_id = fileInfo.dir().dirName();
|
2018-07-15 18:21:05 +05:30
|
|
|
|
2022-10-29 22:57:20 +05:30
|
|
|
QString path = FS::PathCombine("themes", m_id);
|
|
|
|
QString pathResources = FS::PathCombine("themes", m_id, "resources");
|
2018-07-15 18:21:05 +05:30
|
|
|
|
2022-10-29 22:57:20 +05:30
|
|
|
if(!FS::ensureFolderPathExists(path) || !FS::ensureFolderPathExists(pathResources))
|
|
|
|
{
|
2022-11-01 20:11:08 +05:30
|
|
|
themeWarningLog() << "couldn't create folder for theme!";
|
2022-10-29 22:57:20 +05:30
|
|
|
m_palette = baseTheme->colorScheme();
|
|
|
|
m_styleSheet = baseTheme->appStyleSheet();
|
|
|
|
return;
|
|
|
|
}
|
2018-07-15 18:21:05 +05:30
|
|
|
|
2022-10-29 22:57:20 +05:30
|
|
|
auto themeFilePath = FS::PathCombine(path, themeFile);
|
|
|
|
|
|
|
|
bool jsonDataIncomplete = false;
|
2018-07-15 18:21:05 +05:30
|
|
|
|
|
|
|
m_palette = baseTheme->colorScheme();
|
2022-10-29 22:57:20 +05:30
|
|
|
if (!readThemeJson(themeFilePath, m_palette, m_fadeAmount, m_fadeColor, m_name, m_widgets, m_qssFilePath, jsonDataIncomplete))
|
|
|
|
{
|
2022-11-01 20:11:08 +05:30
|
|
|
themeDebugLog() << "Did not read theme json file correctly, writing new one to: " << themeFilePath;
|
2022-10-29 22:57:20 +05:30
|
|
|
m_name = "Custom";
|
|
|
|
m_palette = baseTheme->colorScheme();
|
|
|
|
m_fadeColor = baseTheme->fadeColor();
|
|
|
|
m_fadeAmount = baseTheme->fadeAmount();
|
|
|
|
m_widgets = baseTheme->qtTheme();
|
|
|
|
m_qssFilePath = "themeStyle.css";
|
2018-07-15 18:21:05 +05:30
|
|
|
|
2022-10-29 22:57:20 +05:30
|
|
|
QFileInfo info(themeFilePath);
|
|
|
|
}
|
|
|
|
else
|
2018-07-15 18:21:05 +05:30
|
|
|
{
|
2022-10-29 22:57:20 +05:30
|
|
|
m_palette = fadeInactive(m_palette, m_fadeAmount, m_fadeColor);
|
2018-07-15 18:21:05 +05:30
|
|
|
}
|
|
|
|
|
2022-10-29 22:57:20 +05:30
|
|
|
if(jsonDataIncomplete)
|
2018-07-15 18:21:05 +05:30
|
|
|
{
|
2022-10-29 22:57:20 +05:30
|
|
|
writeThemeJson(fileInfo.absoluteFilePath(), m_palette, m_fadeAmount, m_fadeColor, m_name, m_widgets, m_qssFilePath);
|
2018-07-15 18:21:05 +05:30
|
|
|
}
|
2022-10-29 22:57:20 +05:30
|
|
|
|
|
|
|
auto cssFilePath = FS::PathCombine(path, m_qssFilePath);
|
|
|
|
QFileInfo info (cssFilePath);
|
|
|
|
if(info.isFile())
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
// TODO: validate css?
|
|
|
|
m_styleSheet = QString::fromUtf8(FS::read(cssFilePath));
|
|
|
|
}
|
|
|
|
catch (const Exception &e)
|
|
|
|
{
|
2022-11-01 20:11:08 +05:30
|
|
|
themeWarningLog() << "Couldn't load css:" << e.cause() << "from" << cssFilePath;
|
2022-10-29 22:57:20 +05:30
|
|
|
m_styleSheet = baseTheme->appStyleSheet();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2018-07-15 18:21:05 +05:30
|
|
|
{
|
2022-11-01 20:11:08 +05:30
|
|
|
themeDebugLog() << "No theme css present.";
|
2018-07-15 18:21:05 +05:30
|
|
|
m_styleSheet = baseTheme->appStyleSheet();
|
2022-10-29 22:57:20 +05:30
|
|
|
try
|
|
|
|
{
|
|
|
|
FS::write(cssFilePath, m_styleSheet.toUtf8());
|
|
|
|
}
|
|
|
|
catch (const Exception &e)
|
|
|
|
{
|
2022-11-01 20:11:08 +05:30
|
|
|
themeWarningLog() << "Couldn't write css:" << e.cause() << "to" << cssFilePath;
|
2022-10-29 22:57:20 +05:30
|
|
|
}
|
2018-07-15 18:21:05 +05:30
|
|
|
}
|
2022-10-29 22:57:20 +05:30
|
|
|
} 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))
|
|
|
|
{
|
2022-11-01 20:11:08 +05:30
|
|
|
themeWarningLog() << m_name << " Theme file path doesn't exist!";
|
2022-10-29 22:57:20 +05:30
|
|
|
m_palette = baseTheme->colorScheme();
|
|
|
|
m_styleSheet = baseTheme->appStyleSheet();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_palette = baseTheme->colorScheme();
|
2018-07-15 18:21:05 +05:30
|
|
|
try
|
|
|
|
{
|
2022-10-29 22:57:20 +05:30
|
|
|
// TODO: validate qss?
|
|
|
|
m_styleSheet = QString::fromUtf8(FS::read(path));
|
2018-07-15 18:21:05 +05:30
|
|
|
}
|
|
|
|
catch (const Exception &e)
|
|
|
|
{
|
2022-11-01 20:11:08 +05:30
|
|
|
themeWarningLog() << "Couldn't load qss:" << e.cause() << "from" << path;
|
2022-10-29 22:57:20 +05:30
|
|
|
m_styleSheet = baseTheme->appStyleSheet();
|
2018-07-15 18:21:05 +05:30
|
|
|
}
|
|
|
|
}
|
2016-11-06 08:59:12 +05:30
|
|
|
}
|
|
|
|
|
2016-11-06 10:18:52 +05:30
|
|
|
QStringList CustomTheme::searchPaths()
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
return { FS::PathCombine("themes", m_id, "resources") };
|
2016-11-06 10:18:52 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-11-06 08:59:12 +05:30
|
|
|
QString CustomTheme::id()
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
return m_id;
|
2016-11-06 08:59:12 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
QString CustomTheme::name()
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
return m_name;
|
2016-11-06 08:59:12 +05:30
|
|
|
}
|
|
|
|
|
2017-01-16 03:26:03 +05:30
|
|
|
bool CustomTheme::hasColorScheme()
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
return true;
|
2017-01-16 03:26:03 +05:30
|
|
|
}
|
|
|
|
|
2016-11-06 08:59:12 +05:30
|
|
|
QPalette CustomTheme::colorScheme()
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
return m_palette;
|
2016-11-06 08:59:12 +05:30
|
|
|
}
|
|
|
|
|
2017-01-16 03:26:03 +05:30
|
|
|
bool CustomTheme::hasStyleSheet()
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
return true;
|
2017-01-16 03:26:03 +05:30
|
|
|
}
|
|
|
|
|
2016-11-06 08:59:12 +05:30
|
|
|
QString CustomTheme::appStyleSheet()
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
return m_styleSheet;
|
2016-11-06 08:59:12 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
double CustomTheme::fadeAmount()
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
return m_fadeAmount;
|
2016-11-06 08:59:12 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
QColor CustomTheme::fadeColor()
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
return m_fadeColor;
|
2016-11-06 08:59:12 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
QString CustomTheme::qtTheme()
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
return m_widgets;
|
2016-11-06 08:59:12 +05:30
|
|
|
}
|