2017-01-18 01:04:54 +01:00
|
|
|
|
2018-07-15 14:03:41 +02:00
|
|
|
#include "singletons/Theme.hpp"
|
2021-07-25 13:15:38 +02:00
|
|
|
|
2018-11-25 15:02:48 +01:00
|
|
|
#include "Application.hpp"
|
2023-04-08 11:05:55 +02:00
|
|
|
#include "common/QLogging.hpp"
|
2023-05-19 14:26:51 +02:00
|
|
|
#include "singletons/Paths.hpp"
|
2023-04-10 11:20:27 +02:00
|
|
|
#include "singletons/Resources.hpp"
|
2017-01-18 04:52:47 +01:00
|
|
|
|
|
|
|
#include <QColor>
|
2023-05-19 14:26:51 +02:00
|
|
|
#include <QDir>
|
2023-04-08 11:05:55 +02:00
|
|
|
#include <QFile>
|
|
|
|
#include <QJsonDocument>
|
|
|
|
#include <QSet>
|
2016-12-30 19:16:48 +01:00
|
|
|
|
2018-04-03 02:55:32 +02:00
|
|
|
#include <cmath>
|
2017-08-05 23:38:49 +02:00
|
|
|
|
2022-11-10 10:07:50 +01:00
|
|
|
namespace {
|
2023-05-19 14:26:51 +02:00
|
|
|
|
|
|
|
using namespace chatterino;
|
|
|
|
|
2023-04-08 11:05:55 +02:00
|
|
|
void parseInto(const QJsonObject &obj, const QLatin1String &key, QColor &color)
|
2022-11-10 10:07:50 +01:00
|
|
|
{
|
2023-04-08 11:05:55 +02:00
|
|
|
const auto &jsonValue = obj[key];
|
|
|
|
if (!jsonValue.isString()) [[unlikely]]
|
2022-11-10 10:07:50 +01:00
|
|
|
{
|
2023-04-08 11:05:55 +02:00
|
|
|
qCWarning(chatterinoTheme) << key
|
|
|
|
<< "was expected but not found in the "
|
|
|
|
"current theme - using previous value.";
|
|
|
|
return;
|
2022-11-10 10:07:50 +01:00
|
|
|
}
|
2023-04-08 11:05:55 +02:00
|
|
|
QColor parsed = {jsonValue.toString()};
|
|
|
|
if (!parsed.isValid()) [[unlikely]]
|
2022-11-10 10:07:50 +01:00
|
|
|
{
|
2023-04-08 11:05:55 +02:00
|
|
|
qCWarning(chatterinoTheme).nospace()
|
|
|
|
<< "While parsing " << key << ": '" << jsonValue.toString()
|
|
|
|
<< "' isn't a valid color.";
|
|
|
|
return;
|
2022-11-10 10:07:50 +01:00
|
|
|
}
|
2023-04-08 11:05:55 +02:00
|
|
|
color = parsed;
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOLINTBEGIN(cppcoreguidelines-macro-usage)
|
|
|
|
#define parseColor(to, from, key) \
|
|
|
|
parseInto(from, QLatin1String(#key), (to).from.key)
|
|
|
|
// NOLINTEND(cppcoreguidelines-macro-usage)
|
|
|
|
|
|
|
|
void parseWindow(const QJsonObject &window, chatterino::Theme &theme)
|
|
|
|
{
|
|
|
|
parseColor(theme, window, background);
|
|
|
|
parseColor(theme, window, text);
|
|
|
|
}
|
|
|
|
|
|
|
|
void parseTabs(const QJsonObject &tabs, chatterino::Theme &theme)
|
|
|
|
{
|
|
|
|
const auto parseTabColors = [](auto json, auto &tab) {
|
|
|
|
parseInto(json, QLatin1String("text"), tab.text);
|
|
|
|
{
|
|
|
|
const auto backgrounds = json["backgrounds"].toObject();
|
|
|
|
parseColor(tab, backgrounds, regular);
|
|
|
|
parseColor(tab, backgrounds, hover);
|
|
|
|
parseColor(tab, backgrounds, unfocused);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
const auto line = json["line"].toObject();
|
|
|
|
parseColor(tab, line, regular);
|
|
|
|
parseColor(tab, line, hover);
|
|
|
|
parseColor(tab, line, unfocused);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
parseColor(theme, tabs, dividerLine);
|
|
|
|
parseTabColors(tabs["regular"].toObject(), theme.tabs.regular);
|
|
|
|
parseTabColors(tabs["newMessage"].toObject(), theme.tabs.newMessage);
|
|
|
|
parseTabColors(tabs["highlighted"].toObject(), theme.tabs.highlighted);
|
|
|
|
parseTabColors(tabs["selected"].toObject(), theme.tabs.selected);
|
|
|
|
}
|
|
|
|
|
|
|
|
void parseMessages(const QJsonObject &messages, chatterino::Theme &theme)
|
|
|
|
{
|
2022-11-10 10:07:50 +01:00
|
|
|
{
|
2023-04-08 11:05:55 +02:00
|
|
|
const auto textColors = messages["textColors"].toObject();
|
|
|
|
parseColor(theme.messages, textColors, regular);
|
|
|
|
parseColor(theme.messages, textColors, caret);
|
|
|
|
parseColor(theme.messages, textColors, link);
|
|
|
|
parseColor(theme.messages, textColors, system);
|
|
|
|
parseColor(theme.messages, textColors, chatPlaceholder);
|
2022-11-10 10:07:50 +01:00
|
|
|
}
|
|
|
|
{
|
2023-04-08 11:05:55 +02:00
|
|
|
const auto backgrounds = messages["backgrounds"].toObject();
|
|
|
|
parseColor(theme.messages, backgrounds, regular);
|
|
|
|
parseColor(theme.messages, backgrounds, alternate);
|
2022-11-10 10:07:50 +01:00
|
|
|
}
|
2023-04-08 11:05:55 +02:00
|
|
|
parseColor(theme, messages, disabled);
|
|
|
|
parseColor(theme, messages, selection);
|
|
|
|
parseColor(theme, messages, highlightAnimationStart);
|
|
|
|
parseColor(theme, messages, highlightAnimationEnd);
|
|
|
|
}
|
|
|
|
|
|
|
|
void parseScrollbars(const QJsonObject &scrollbars, chatterino::Theme &theme)
|
|
|
|
{
|
|
|
|
parseColor(theme, scrollbars, background);
|
|
|
|
parseColor(theme, scrollbars, thumb);
|
|
|
|
parseColor(theme, scrollbars, thumbSelected);
|
|
|
|
}
|
|
|
|
|
|
|
|
void parseSplits(const QJsonObject &splits, chatterino::Theme &theme)
|
|
|
|
{
|
|
|
|
parseColor(theme, splits, messageSeperator);
|
|
|
|
parseColor(theme, splits, background);
|
|
|
|
parseColor(theme, splits, dropPreview);
|
|
|
|
parseColor(theme, splits, dropPreviewBorder);
|
|
|
|
parseColor(theme, splits, dropTargetRect);
|
|
|
|
parseColor(theme, splits, dropTargetRectBorder);
|
|
|
|
parseColor(theme, splits, resizeHandle);
|
|
|
|
parseColor(theme, splits, resizeHandleBackground);
|
|
|
|
|
|
|
|
{
|
|
|
|
const auto header = splits["header"].toObject();
|
|
|
|
parseColor(theme.splits, header, border);
|
|
|
|
parseColor(theme.splits, header, focusedBorder);
|
|
|
|
parseColor(theme.splits, header, background);
|
|
|
|
parseColor(theme.splits, header, focusedBackground);
|
|
|
|
parseColor(theme.splits, header, text);
|
|
|
|
parseColor(theme.splits, header, focusedText);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
const auto input = splits["input"].toObject();
|
|
|
|
parseColor(theme.splits, input, background);
|
|
|
|
parseColor(theme.splits, input, text);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void parseColors(const QJsonObject &root, chatterino::Theme &theme)
|
|
|
|
{
|
|
|
|
const auto colors = root["colors"].toObject();
|
|
|
|
|
|
|
|
parseInto(colors, QLatin1String("accent"), theme.accent);
|
2022-11-10 10:07:50 +01:00
|
|
|
|
2023-04-08 11:05:55 +02:00
|
|
|
parseWindow(colors["window"].toObject(), theme);
|
|
|
|
parseTabs(colors["tabs"].toObject(), theme);
|
|
|
|
parseMessages(colors["messages"].toObject(), theme);
|
|
|
|
parseScrollbars(colors["scrollbars"].toObject(), theme);
|
|
|
|
parseSplits(colors["splits"].toObject(), theme);
|
2022-11-10 10:07:50 +01:00
|
|
|
}
|
2023-04-08 11:05:55 +02:00
|
|
|
#undef parseColor
|
|
|
|
|
2023-05-19 14:26:51 +02:00
|
|
|
/**
|
|
|
|
* Load the given theme descriptor from its path
|
|
|
|
*
|
|
|
|
* Returns a JSON object containing theme data if the theme is valid, otherwise nullopt
|
|
|
|
*
|
|
|
|
* NOTE: No theme validation is done by this function
|
|
|
|
**/
|
|
|
|
std::optional<QJsonObject> loadTheme(const ThemeDescriptor &theme)
|
2023-04-08 11:05:55 +02:00
|
|
|
{
|
2023-05-19 14:26:51 +02:00
|
|
|
QFile file(theme.path);
|
|
|
|
if (!file.open(QFile::ReadOnly))
|
|
|
|
{
|
|
|
|
qCWarning(chatterinoTheme)
|
|
|
|
<< "Failed to open" << file.fileName() << "at" << theme.path;
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
2023-04-08 11:05:55 +02:00
|
|
|
|
2023-05-19 14:26:51 +02:00
|
|
|
QJsonParseError error{};
|
|
|
|
auto json = QJsonDocument::fromJson(file.readAll(), &error);
|
|
|
|
if (!json.isObject())
|
2023-04-08 11:05:55 +02:00
|
|
|
{
|
2023-05-19 14:26:51 +02:00
|
|
|
qCWarning(chatterinoTheme) << "Failed to parse" << file.fileName()
|
|
|
|
<< "error:" << error.errorString();
|
|
|
|
return std::nullopt;
|
2023-04-08 11:05:55 +02:00
|
|
|
}
|
2023-05-19 14:26:51 +02:00
|
|
|
|
|
|
|
// TODO: Validate JSON schema?
|
|
|
|
|
|
|
|
return json.object();
|
2023-04-08 11:05:55 +02:00
|
|
|
}
|
|
|
|
|
2022-11-10 10:07:50 +01:00
|
|
|
} // namespace
|
|
|
|
|
2017-04-14 17:52:22 +02:00
|
|
|
namespace chatterino {
|
2017-01-18 21:30:23 +01:00
|
|
|
|
2023-05-19 14:26:51 +02:00
|
|
|
const std::vector<ThemeDescriptor> Theme::builtInThemes{
|
|
|
|
{
|
|
|
|
.key = "White",
|
|
|
|
.path = ":/themes/White.json",
|
|
|
|
.name = "White",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.key = "Light",
|
|
|
|
.path = ":/themes/Light.json",
|
|
|
|
.name = "Light",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.key = "Dark",
|
|
|
|
.path = ":/themes/Dark.json",
|
|
|
|
.name = "Dark",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.key = "Black",
|
|
|
|
.path = ":/themes/Black.json",
|
|
|
|
.name = "Black",
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
// Dark is our default & fallback theme
|
|
|
|
const ThemeDescriptor Theme::fallbackTheme = Theme::builtInThemes.at(2);
|
|
|
|
|
2022-11-10 10:07:50 +01:00
|
|
|
bool Theme::isLightTheme() const
|
|
|
|
{
|
|
|
|
return this->isLight_;
|
|
|
|
}
|
|
|
|
|
2023-05-19 14:26:51 +02:00
|
|
|
void Theme::initialize(Settings &settings, Paths &paths)
|
2017-06-26 16:41:20 +02:00
|
|
|
{
|
2023-05-19 14:26:51 +02:00
|
|
|
this->themeName.connect(
|
|
|
|
[this](auto themeName) {
|
|
|
|
qCInfo(chatterinoTheme) << "Theme updated to" << themeName;
|
2020-11-08 12:02:19 +01:00
|
|
|
this->update();
|
|
|
|
},
|
|
|
|
false);
|
2023-05-19 14:26:51 +02:00
|
|
|
|
|
|
|
this->loadAvailableThemes();
|
|
|
|
|
|
|
|
this->update();
|
2017-02-02 01:23:26 +01:00
|
|
|
}
|
|
|
|
|
2022-11-10 10:07:50 +01:00
|
|
|
void Theme::update()
|
|
|
|
{
|
2023-05-19 14:26:51 +02:00
|
|
|
auto oTheme = this->findThemeByKey(this->themeName);
|
|
|
|
|
|
|
|
std::optional<QJsonObject> themeJSON;
|
|
|
|
if (!oTheme)
|
|
|
|
{
|
|
|
|
qCWarning(chatterinoTheme)
|
|
|
|
<< "Theme" << this->themeName
|
|
|
|
<< "not found, falling back to the fallback theme";
|
|
|
|
|
|
|
|
themeJSON = loadTheme(fallbackTheme);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
const auto &theme = *oTheme;
|
|
|
|
|
|
|
|
themeJSON = loadTheme(theme);
|
|
|
|
|
|
|
|
if (!themeJSON)
|
|
|
|
{
|
|
|
|
qCWarning(chatterinoTheme)
|
|
|
|
<< "Theme" << this->themeName
|
|
|
|
<< "not valid, falling back to the fallback theme";
|
|
|
|
|
|
|
|
// Parsing the theme failed, fall back
|
|
|
|
themeJSON = loadTheme(fallbackTheme);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!themeJSON)
|
|
|
|
{
|
|
|
|
qCWarning(chatterinoTheme)
|
|
|
|
<< "Failed to load" << this->themeName << "or the fallback theme";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this->parseFrom(*themeJSON);
|
|
|
|
|
2022-11-10 10:07:50 +01:00
|
|
|
this->updated.invoke();
|
|
|
|
}
|
|
|
|
|
2023-05-19 14:26:51 +02:00
|
|
|
std::vector<std::pair<QString, QVariant>> Theme::availableThemes() const
|
2016-12-30 19:16:48 +01:00
|
|
|
{
|
2023-05-19 14:26:51 +02:00
|
|
|
std::vector<std::pair<QString, QVariant>> packagedThemes;
|
|
|
|
|
|
|
|
for (const auto &theme : this->availableThemes_)
|
2023-03-17 20:53:03 +01:00
|
|
|
{
|
2023-05-19 14:26:51 +02:00
|
|
|
if (theme.custom)
|
|
|
|
{
|
|
|
|
auto p = std::make_pair(
|
|
|
|
QStringLiteral("Custom: %1").arg(theme.name), theme.key);
|
|
|
|
|
|
|
|
packagedThemes.emplace_back(p);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
auto p = std::make_pair(theme.name, theme.key);
|
|
|
|
|
|
|
|
packagedThemes.emplace_back(p);
|
|
|
|
}
|
2022-11-10 10:07:50 +01:00
|
|
|
}
|
2023-04-08 11:05:55 +02:00
|
|
|
|
2023-05-19 14:26:51 +02:00
|
|
|
return packagedThemes;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Theme::loadAvailableThemes()
|
|
|
|
{
|
|
|
|
this->availableThemes_ = Theme::builtInThemes;
|
|
|
|
|
|
|
|
auto dir = QDir(getPaths()->themesDirectory);
|
|
|
|
for (const auto &info :
|
|
|
|
dir.entryInfoList(QDir::Files | QDir::NoDotAndDotDot, QDir::Name))
|
2023-03-17 20:53:03 +01:00
|
|
|
{
|
2023-05-19 14:26:51 +02:00
|
|
|
if (!info.isFile())
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!info.fileName().endsWith(".json"))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto themeName = info.baseName();
|
|
|
|
|
|
|
|
auto themeDescriptor = ThemeDescriptor{
|
|
|
|
info.fileName(), info.absoluteFilePath(), themeName, true};
|
|
|
|
|
|
|
|
auto theme = loadTheme(themeDescriptor);
|
|
|
|
if (!theme)
|
|
|
|
{
|
|
|
|
qCWarning(chatterinoTheme) << "Failed to parse theme at" << info;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
this->availableThemes_.emplace_back(std::move(themeDescriptor));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<ThemeDescriptor> Theme::findThemeByKey(const QString &key)
|
|
|
|
{
|
|
|
|
for (const auto &theme : this->availableThemes_)
|
|
|
|
{
|
|
|
|
if (theme.key == key)
|
|
|
|
{
|
|
|
|
return theme;
|
|
|
|
}
|
2023-03-17 20:53:03 +01:00
|
|
|
}
|
|
|
|
|
2023-05-19 14:26:51 +02:00
|
|
|
return std::nullopt;
|
2023-04-08 11:05:55 +02:00
|
|
|
}
|
2022-11-10 10:07:50 +01:00
|
|
|
|
2023-04-08 11:05:55 +02:00
|
|
|
void Theme::parseFrom(const QJsonObject &root)
|
|
|
|
{
|
|
|
|
parseColors(root, *this);
|
2022-11-10 10:07:50 +01:00
|
|
|
|
2023-04-08 11:05:55 +02:00
|
|
|
this->isLight_ =
|
|
|
|
root["metadata"]["iconTheme"].toString() == QStringLiteral("dark");
|
2022-11-10 10:07:50 +01:00
|
|
|
|
2018-01-02 02:15:11 +01:00
|
|
|
this->splits.input.styleSheet =
|
|
|
|
"background:" + this->splits.input.background.name() + ";" +
|
2023-03-17 20:53:03 +01:00
|
|
|
"border:" + this->tabs.selected.backgrounds.regular.name() + ";" +
|
|
|
|
"color:" + this->messages.textColors.regular.name() + ";" +
|
2018-06-07 17:43:21 +02:00
|
|
|
"selection-background-color:" +
|
2023-04-08 11:05:55 +02:00
|
|
|
(this->isLightTheme() ? "#68B1FF"
|
|
|
|
: this->tabs.selected.backgrounds.regular.name());
|
2023-04-10 11:20:27 +02:00
|
|
|
|
|
|
|
// Usercard buttons
|
|
|
|
if (this->isLightTheme())
|
|
|
|
{
|
|
|
|
this->buttons.copy = getResources().buttons.copyDark;
|
|
|
|
this->buttons.pin = getResources().buttons.pinDisabledDark;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this->buttons.copy = getResources().buttons.copyLight;
|
|
|
|
this->buttons.pin = getResources().buttons.pinDisabledLight;
|
|
|
|
}
|
2017-09-12 19:06:16 +02:00
|
|
|
}
|
|
|
|
|
2023-03-17 20:53:03 +01:00
|
|
|
void Theme::normalizeColor(QColor &color) const
|
2017-01-18 01:04:54 +01:00
|
|
|
{
|
2018-11-25 15:02:48 +01:00
|
|
|
if (this->isLightTheme())
|
2018-10-21 13:43:02 +02:00
|
|
|
{
|
|
|
|
if (color.lightnessF() > 0.5)
|
|
|
|
{
|
2018-06-01 14:20:46 +02:00
|
|
|
color.setHslF(color.hueF(), color.saturationF(), 0.5);
|
2017-12-19 02:23:17 +01:00
|
|
|
}
|
|
|
|
|
2018-08-06 21:17:03 +02:00
|
|
|
if (color.lightnessF() > 0.4 && color.hueF() > 0.1 &&
|
2018-10-21 13:43:02 +02:00
|
|
|
color.hueF() < 0.33333)
|
|
|
|
{
|
2018-08-06 21:17:03 +02:00
|
|
|
color.setHslF(color.hueF(), color.saturationF(),
|
|
|
|
color.lightnessF() - sin((color.hueF() - 0.1) /
|
|
|
|
(0.3333 - 0.1) * 3.14159) *
|
|
|
|
color.saturationF() * 0.4);
|
2017-12-19 02:23:17 +01:00
|
|
|
}
|
2018-10-21 13:43:02 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (color.lightnessF() < 0.5)
|
|
|
|
{
|
2018-06-01 14:20:46 +02:00
|
|
|
color.setHslF(color.hueF(), color.saturationF(), 0.5);
|
2017-08-12 12:09:26 +02:00
|
|
|
}
|
|
|
|
|
2018-08-06 21:17:03 +02:00
|
|
|
if (color.lightnessF() < 0.6 && color.hueF() > 0.54444 &&
|
2018-10-21 13:43:02 +02:00
|
|
|
color.hueF() < 0.83333)
|
|
|
|
{
|
2018-01-12 23:09:05 +01:00
|
|
|
color.setHslF(
|
|
|
|
color.hueF(), color.saturationF(),
|
2018-08-06 21:17:03 +02:00
|
|
|
color.lightnessF() + sin((color.hueF() - 0.54444) /
|
|
|
|
(0.8333 - 0.54444) * 3.14159) *
|
2018-01-12 23:09:05 +01:00
|
|
|
color.saturationF() * 0.4);
|
2017-08-12 12:09:26 +02:00
|
|
|
}
|
|
|
|
}
|
2016-12-30 19:16:48 +01:00
|
|
|
}
|
2017-06-07 10:09:24 +02:00
|
|
|
|
2018-11-25 15:02:48 +01:00
|
|
|
Theme *getTheme()
|
|
|
|
{
|
|
|
|
return getApp()->themes;
|
|
|
|
}
|
|
|
|
|
2017-04-14 17:52:22 +02:00
|
|
|
} // namespace chatterino
|