Store Themes as JSON files (#4471)

Co-authored-by: pajlada <rasmus.karlsson@pajlada.com>
This commit is contained in:
nerix
2023-04-08 11:05:55 +02:00
committed by GitHub
parent 7a286480d6
commit 4e3433e966
13 changed files with 1018 additions and 138 deletions
+158 -136
View File
@@ -2,34 +2,153 @@
#include "singletons/Theme.hpp"
#include "Application.hpp"
#include "singletons/Resources.hpp"
#include "common/QLogging.hpp"
#include <QColor>
#include <QFile>
#include <QJsonDocument>
#include <QJsonObject>
#include <QSet>
#include <cmath>
namespace {
double getMultiplierByTheme(const QString &themeName)
void parseInto(const QJsonObject &obj, const QLatin1String &key, QColor &color)
{
if (themeName == "Light")
const auto &jsonValue = obj[key];
if (!jsonValue.isString()) [[unlikely]]
{
return 0.8;
qCWarning(chatterinoTheme) << key
<< "was expected but not found in the "
"current theme - using previous value.";
return;
}
if (themeName == "White")
QColor parsed = {jsonValue.toString()};
if (!parsed.isValid()) [[unlikely]]
{
return 1.0;
qCWarning(chatterinoTheme).nospace()
<< "While parsing " << key << ": '" << jsonValue.toString()
<< "' isn't a valid color.";
return;
}
if (themeName == "Black")
{
return -1.0;
}
if (themeName == "Dark")
{
return -0.8;
}
return -0.8; // default: Dark
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)
{
{
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);
}
{
const auto backgrounds = messages["backgrounds"].toObject();
parseColor(theme.messages, backgrounds, regular);
parseColor(theme.messages, backgrounds, alternate);
}
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);
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);
}
#undef parseColor
QString getThemePath(const QString &name)
{
static QSet<QString> knownThemes = {"White", "Light", "Dark", "Black"};
if (knownThemes.contains(name))
{
return QStringLiteral(":/themes/%1.json").arg(name);
}
return name;
}
} // namespace
namespace chatterino {
@@ -52,142 +171,45 @@ Theme::Theme()
void Theme::update()
{
this->actuallyUpdate(getMultiplierByTheme(this->themeName.getValue()));
this->parse();
this->updated.invoke();
}
// multiplier: 1 = white, 0.8 = light, -0.8 dark, -1 black
void Theme::actuallyUpdate(double multiplier)
void Theme::parse()
{
this->isLight_ = multiplier > 0;
const auto isLight = this->isLightTheme();
auto getGray = [multiplier](double l, double a = 1.0) {
return QColor::fromHslF(0, 0, ((l - 0.5) * multiplier) + 0.5, a);
};
/// WINDOW
#ifdef Q_OS_LINUX
this->window.background = isLight ? "#fff" : QColor(61, 60, 56);
#else
this->window.background = isLight ? "#fff" : "#111";
#endif
this->window.text = isLight ? "#000" : "#eee";
/// TABSs
if (isLight)
QFile file(getThemePath(this->themeName));
if (!file.open(QFile::ReadOnly))
{
this->tabs.regular = {.text = "#444",
.backgrounds = {"#fff", "#eee", "#fff"},
.line = {"#fff", "#fff", "#fff"}};
this->tabs.newMessage = {.text = "#222",
.backgrounds = {"#fff", "#eee", "#fff"},
.line = {"#bbb", "#bbb", "#bbb"}};
this->tabs.highlighted = {.text = "#000",
.backgrounds = {"#fff", "#eee", "#fff"},
.line = {"#f00", "#f00", "#f00"}};
this->tabs.selected = {
.text = "#000",
.backgrounds = {"#b4d7ff", "#b4d7ff", "#b4d7ff"},
.line = {this->accent, this->accent, this->accent}};
}
else
{
this->tabs.regular = {.text = "#aaa",
.backgrounds{"#252525", "#252525", "#252525"},
.line = {"#444", "#444", "#444"}};
this->tabs.newMessage = {.text = "#eee",
.backgrounds{"#252525", "#252525", "#252525"},
.line = {"#888", "#888", "#888"}};
this->tabs.highlighted = {.text = "#eee",
.backgrounds{"#252525", "#252525", "#252525"},
.line = {"#ee6166", "#ee6166", "#ee6166"}};
this->tabs.selected = {
.text = "#fff",
.backgrounds{"#555", "#555", "#555"},
.line = {this->accent, this->accent, this->accent}};
qCWarning(chatterinoTheme) << "Failed to open" << file.fileName();
return;
}
this->tabs.dividerLine = this->tabs.selected.backgrounds.regular;
// Message
this->messages.textColors.caret = isLight ? "#000" : "#fff";
this->messages.textColors.regular = isLight ? "#000" : "#fff";
this->messages.textColors.link = QColor(66, 134, 244);
this->messages.textColors.system = QColor(140, 127, 127);
this->messages.textColors.chatPlaceholder =
isLight ? QColor(175, 159, 159) : QColor(93, 85, 85);
this->messages.backgrounds.regular = getGray(1);
this->messages.backgrounds.alternate = getGray(0.96);
this->messages.disabled = getGray(1, 0.6);
int complementaryGray = isLight ? 20 : 230;
this->messages.highlightAnimationStart =
QColor(complementaryGray, complementaryGray, complementaryGray, 110);
this->messages.highlightAnimationEnd =
QColor(complementaryGray, complementaryGray, complementaryGray, 0);
// Scrollbar
this->scrollbars.background = QColor(0, 0, 0, 0);
this->scrollbars.thumb = getGray(0.70);
this->scrollbars.thumbSelected = getGray(0.65);
// Selection
this->messages.selection =
isLight ? QColor(0, 0, 0, 64) : QColor(255, 255, 255, 64);
// Splits
if (isLight)
QJsonParseError error{};
auto json = QJsonDocument::fromJson(file.readAll(), &error);
if (json.isNull())
{
this->splits.dropTargetRect = QColor(255, 255, 255, 0);
qCWarning(chatterinoTheme) << "Failed to parse" << file.fileName()
<< "error:" << error.errorString();
return;
}
else
{
this->splits.dropTargetRect = QColor(0, 148, 255, 0);
}
this->splits.dropTargetRectBorder = QColor(0, 148, 255, 0);
this->splits.dropPreview = QColor(0, 148, 255, 48);
this->splits.dropPreviewBorder = QColor(0, 148, 255);
this->splits.resizeHandle = QColor(0, 148, 255, isLight ? 255 : 112);
this->splits.resizeHandleBackground =
QColor(0, 148, 255, isLight ? 80 : 32);
this->splits.header.background = getGray(isLight ? 1 : 0.9);
this->splits.header.border = getGray(isLight ? 1 : 0.85);
this->splits.header.text = this->messages.textColors.regular;
this->splits.header.focusedBackground = getGray(isLight ? 0.95 : 0.79);
this->splits.header.focusedBorder = getGray(isLight ? 0.90 : 0.78);
this->splits.header.focusedText = QColor::fromHsvF(
0.58388, isLight ? 1.0 : 0.482, isLight ? 0.6375 : 1.0);
this->parseFrom(json.object());
}
void Theme::parseFrom(const QJsonObject &root)
{
parseColors(root, *this);
this->isLight_ =
root["metadata"]["iconTheme"].toString() == QStringLiteral("dark");
this->splits.input.background = getGray(0.95);
this->splits.input.text = this->messages.textColors.regular;
this->splits.input.styleSheet =
"background:" + this->splits.input.background.name() + ";" +
"border:" + this->tabs.selected.backgrounds.regular.name() + ";" +
"color:" + this->messages.textColors.regular.name() + ";" +
"selection-background-color:" +
(isLight ? "#68B1FF" : this->tabs.selected.backgrounds.regular.name());
this->splits.messageSeperator =
isLight ? QColor(127, 127, 127) : QColor(60, 60, 60);
this->splits.background = getGray(1);
// Copy button
if (isLight)
{
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;
}
(this->isLightTheme() ? "#68B1FF"
: this->tabs.selected.backgrounds.regular.name());
}
void Theme::normalizeColor(QColor &color) const