From 9a5fdd10ef0f700081c484847bd73148cdf7cd63 Mon Sep 17 00:00:00 2001 From: Nerixyz Date: Fri, 7 Jul 2023 16:38:24 +0200 Subject: [PATCH 01/11] fix: use L1 literals --- src/singletons/Theme.cpp | 48 ++++++++++++++++++++++------------------ 1 file changed, 26 insertions(+), 22 deletions(-) diff --git a/src/singletons/Theme.cpp b/src/singletons/Theme.cpp index 98510662bae..07f6f798bb6 100644 --- a/src/singletons/Theme.cpp +++ b/src/singletons/Theme.cpp @@ -2,6 +2,7 @@ #include "singletons/Theme.hpp" #include "Application.hpp" +#include "common/Literals.hpp" #include "common/QLogging.hpp" #include "singletons/Paths.hpp" #include "singletons/Resources.hpp" @@ -17,8 +18,9 @@ namespace { using namespace chatterino; +using namespace literals; -void parseInto(const QJsonObject &obj, const QLatin1String &key, QColor &color) +void parseInto(const QJsonObject &obj, QLatin1String key, QColor &color) { const auto &jsonValue = obj[key]; if (!jsonValue.isString()) [[unlikely]] @@ -40,8 +42,9 @@ void parseInto(const QJsonObject &obj, const QLatin1String &key, QColor &color) } // NOLINTBEGIN(cppcoreguidelines-macro-usage) +#define _c2StringLit(s, ty) s##ty #define parseColor(to, from, key) \ - parseInto(from, QLatin1String(#key), (to).from.key) + parseInto(from, _c2StringLit(#key, _L1), (to).from.key) // NOLINTEND(cppcoreguidelines-macro-usage) void parseWindow(const QJsonObject &window, chatterino::Theme &theme) @@ -52,32 +55,32 @@ void parseWindow(const QJsonObject &window, chatterino::Theme &theme) void parseTabs(const QJsonObject &tabs, chatterino::Theme &theme) { - const auto parseTabColors = [](auto json, auto &tab) { - parseInto(json, QLatin1String("text"), tab.text); + const auto parseTabColors = [](const auto &json, auto &tab) { + parseInto(json, "text"_L1, tab.text); { - const auto backgrounds = json["backgrounds"].toObject(); + const auto backgrounds = json["backgrounds"_L1].toObject(); parseColor(tab, backgrounds, regular); parseColor(tab, backgrounds, hover); parseColor(tab, backgrounds, unfocused); } { - const auto line = json["line"].toObject(); + const auto line = json["line"_L1].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); + parseTabColors(tabs["regular"_L1].toObject(), theme.tabs.regular); + parseTabColors(tabs["newMessage"_L1].toObject(), theme.tabs.newMessage); + parseTabColors(tabs["highlighted"_L1].toObject(), theme.tabs.highlighted); + parseTabColors(tabs["selected"_L1].toObject(), theme.tabs.selected); } void parseMessages(const QJsonObject &messages, chatterino::Theme &theme) { { - const auto textColors = messages["textColors"].toObject(); + const auto textColors = messages["textColors"_L1].toObject(); parseColor(theme.messages, textColors, regular); parseColor(theme.messages, textColors, caret); parseColor(theme.messages, textColors, link); @@ -85,7 +88,7 @@ void parseMessages(const QJsonObject &messages, chatterino::Theme &theme) parseColor(theme.messages, textColors, chatPlaceholder); } { - const auto backgrounds = messages["backgrounds"].toObject(); + const auto backgrounds = messages["backgrounds"_L1].toObject(); parseColor(theme.messages, backgrounds, regular); parseColor(theme.messages, backgrounds, alternate); } @@ -114,7 +117,7 @@ void parseSplits(const QJsonObject &splits, chatterino::Theme &theme) parseColor(theme, splits, resizeHandleBackground); { - const auto header = splits["header"].toObject(); + const auto header = splits["header"_L1].toObject(); parseColor(theme.splits, header, border); parseColor(theme.splits, header, focusedBorder); parseColor(theme.splits, header, background); @@ -123,7 +126,7 @@ void parseSplits(const QJsonObject &splits, chatterino::Theme &theme) parseColor(theme.splits, header, focusedText); } { - const auto input = splits["input"].toObject(); + const auto input = splits["input"_L1].toObject(); parseColor(theme.splits, input, background); parseColor(theme.splits, input, text); } @@ -131,17 +134,18 @@ void parseSplits(const QJsonObject &splits, chatterino::Theme &theme) void parseColors(const QJsonObject &root, chatterino::Theme &theme) { - const auto colors = root["colors"].toObject(); + const auto colors = root["colors"_L1].toObject(); - parseInto(colors, QLatin1String("accent"), theme.accent); + parseInto(colors, "accent"_L1, 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); + parseWindow(colors["window"_L1].toObject(), theme); + parseTabs(colors["tabs"_L1].toObject(), theme); + parseMessages(colors["messages"_L1].toObject(), theme); + parseScrollbars(colors["scrollbars"_L1].toObject(), theme); + parseSplits(colors["splits"_L1].toObject(), theme); } #undef parseColor +#undef _c2StringLit /** * Load the given theme descriptor from its path @@ -341,7 +345,7 @@ void Theme::parseFrom(const QJsonObject &root) parseColors(root, *this); this->isLight_ = - root["metadata"]["iconTheme"].toString() == QStringLiteral("dark"); + root["metadata"_L1]["iconTheme"_L1].toString() == u"dark"_s; this->splits.input.styleSheet = "background:" + this->splits.input.background.name() + ";" + From 942e3f2a29fa47a62f9500c1090d076fc70961d9 Mon Sep 17 00:00:00 2001 From: Nerixyz Date: Fri, 7 Jul 2023 16:50:18 +0200 Subject: [PATCH 02/11] fix: use stringliteral for input stylesheet --- src/singletons/Theme.cpp | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/singletons/Theme.cpp b/src/singletons/Theme.cpp index 07f6f798bb6..68b9c34b01c 100644 --- a/src/singletons/Theme.cpp +++ b/src/singletons/Theme.cpp @@ -347,13 +347,18 @@ void Theme::parseFrom(const QJsonObject &root) this->isLight_ = root["metadata"_L1]["iconTheme"_L1].toString() == u"dark"_s; - 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:" + - (this->isLightTheme() ? "#68B1FF" - : this->tabs.selected.backgrounds.regular.name()); + this->splits.input.styleSheet = uR"( + background: %1; + border: %2; + color: %3; + selection-background-color: %4; + )"_s.arg( + this->splits.input.background.name(QColor::HexArgb), + this->tabs.selected.backgrounds.regular.name(QColor::HexArgb), + this->messages.textColors.regular.name(QColor::HexArgb), + this->isLightTheme() + ? u"#68B1FF"_s + : this->tabs.selected.backgrounds.regular.name(QColor::HexArgb)); // Usercard buttons if (this->isLightTheme()) From 1f8fb501c6f50b1f0f834663e04498312cef8db3 Mon Sep 17 00:00:00 2001 From: Nerixyz Date: Fri, 7 Jul 2023 19:34:40 +0200 Subject: [PATCH 03/11] fix: super annoying typo --- docs/ChatterinoTheme.schema.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/ChatterinoTheme.schema.json b/docs/ChatterinoTheme.schema.json index a91de0129d0..f10374230d9 100644 --- a/docs/ChatterinoTheme.schema.json +++ b/docs/ChatterinoTheme.schema.json @@ -30,7 +30,7 @@ }, { "title": "SVG Color", - "description": "This is stricter than Qt. You could theoretically put tabs an spaces between characters in a named color and capitalize the color.", + "description": "This is stricter than Qt. You could theoretically put tabs and spaces between characters in a named color and capitalize the color.", "$comment": "https://www.w3.org/TR/SVG11/types.html#ColorKeywords", "enum": [ "aliceblue", From 8f4770aa0ffc41e199d3ea29dc551efc1499d733 Mon Sep 17 00:00:00 2001 From: Nerixyz Date: Fri, 7 Jul 2023 19:36:31 +0200 Subject: [PATCH 04/11] fix: clarify `This` --- docs/ChatterinoTheme.schema.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/ChatterinoTheme.schema.json b/docs/ChatterinoTheme.schema.json index f10374230d9..a0c7972dd37 100644 --- a/docs/ChatterinoTheme.schema.json +++ b/docs/ChatterinoTheme.schema.json @@ -30,7 +30,7 @@ }, { "title": "SVG Color", - "description": "This is stricter than Qt. You could theoretically put tabs and spaces between characters in a named color and capitalize the color.", + "description": "This enum is stricter than Qt. You could theoretically put tabs and spaces between characters in a named color and capitalize the color.", "$comment": "https://www.w3.org/TR/SVG11/types.html#ColorKeywords", "enum": [ "aliceblue", From 45a7b7d53bdcf7909a08b9d79c9e2e25e4631791 Mon Sep 17 00:00:00 2001 From: Nerixyz Date: Fri, 7 Jul 2023 19:37:51 +0200 Subject: [PATCH 05/11] feat: add session only checkbox --- src/widgets/settingspages/GeneralPageView.cpp | 22 +++++++++++++++++++ src/widgets/settingspages/GeneralPageView.hpp | 4 ++++ 2 files changed, 26 insertions(+) diff --git a/src/widgets/settingspages/GeneralPageView.cpp b/src/widgets/settingspages/GeneralPageView.cpp index 0d1ab0b25a4..486a58a34b9 100644 --- a/src/widgets/settingspages/GeneralPageView.cpp +++ b/src/widgets/settingspages/GeneralPageView.cpp @@ -125,6 +125,28 @@ QCheckBox *GeneralPageView::addCheckbox(const QString &text, return check; } +QCheckBox *GeneralPageView::addSessionCheckbox( + const QString &text, bool initialValue, std::function onUpdated, + QString toolTipText) +{ + auto *check = new QCheckBox(text); + this->addToolTip(*check, std::move(toolTipText)); + check->setChecked(initialValue); + + // update setting on toggle + QObject::connect(check, &QCheckBox::toggled, this, + [onUpdated = std::move(onUpdated)](bool state) { + onUpdated(state); + }); + + this->addWidget(check); + + // groups + this->groups_.back().widgets.push_back({check, {text}}); + + return check; +} + ComboBox *GeneralPageView::addDropdown(const QString &text, const QStringList &list, QString toolTipText) diff --git a/src/widgets/settingspages/GeneralPageView.hpp b/src/widgets/settingspages/GeneralPageView.hpp index 101b0b7b9cd..5153ab822e0 100644 --- a/src/widgets/settingspages/GeneralPageView.hpp +++ b/src/widgets/settingspages/GeneralPageView.hpp @@ -103,6 +103,10 @@ class GeneralPageView : public QWidget /// @param inverse Inverses true to false and vice versa QCheckBox *addCheckbox(const QString &text, BoolSetting &setting, bool inverse = false, QString toolTipText = {}); + /// Adds a checkbox that only keeps its state for the current session + QCheckBox *addSessionCheckbox(const QString &text, bool initialValue, + std::function onUpdated, + QString toolTipText = {}); ComboBox *addDropdown(const QString &text, const QStringList &items, QString toolTipText = {}); ComboBox *addDropdown(const QString &text, const QStringList &items, From b087d5dffbd6fa582468bad92d7b8f7c62cd40a4 Mon Sep 17 00:00:00 2001 From: Nerixyz Date: Fri, 7 Jul 2023 19:41:08 +0200 Subject: [PATCH 06/11] feat: add support for reloading the current theme --- src/singletons/Theme.cpp | 130 ++++++++++++++++++++-- src/singletons/Theme.hpp | 10 ++ src/widgets/settingspages/GeneralPage.cpp | 9 ++ 3 files changed, 139 insertions(+), 10 deletions(-) diff --git a/src/singletons/Theme.cpp b/src/singletons/Theme.cpp index 68b9c34b01c..23e5d25cb8a 100644 --- a/src/singletons/Theme.cpp +++ b/src/singletons/Theme.cpp @@ -9,6 +9,7 @@ #include #include +#include #include #include #include @@ -147,20 +148,13 @@ void parseColors(const QJsonObject &root, chatterino::Theme &theme) #undef parseColor #undef _c2StringLit -/** - * 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 loadTheme(const ThemeDescriptor &theme) +std::optional loadThemeFromPath(const QString &path) { - QFile file(theme.path); + QFile file(path); if (!file.open(QFile::ReadOnly)) { qCWarning(chatterinoTheme) - << "Failed to open" << file.fileName() << "at" << theme.path; + << "Failed to open" << file.fileName() << "at" << path; return std::nullopt; } @@ -178,6 +172,18 @@ std::optional loadTheme(const ThemeDescriptor &theme) return json.object(); } +/** + * 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 loadTheme(const ThemeDescriptor &theme) +{ + return loadThemeFromPath(theme.path); +} + } // namespace namespace chatterino { @@ -232,6 +238,7 @@ void Theme::update() auto oTheme = this->findThemeByKey(this->themeName); std::optional themeJSON; + QString themePath; if (!oTheme) { qCWarning(chatterinoTheme) @@ -239,12 +246,14 @@ void Theme::update() << "not found, falling back to the fallback theme"; themeJSON = loadTheme(fallbackTheme); + themePath = fallbackTheme.path; } else { const auto &theme = *oTheme; themeJSON = loadTheme(theme); + themePath = theme.path; if (!themeJSON) { @@ -254,6 +263,7 @@ void Theme::update() // Parsing the theme failed, fall back themeJSON = loadTheme(fallbackTheme); + themePath = fallbackTheme.path; } } @@ -265,8 +275,10 @@ void Theme::update() } this->parseFrom(*themeJSON); + this->currentThemePath_ = themePath; this->updated.invoke(); + this->updateCurrentWatchPath(); } std::vector> Theme::availableThemes() const @@ -373,6 +385,104 @@ void Theme::parseFrom(const QJsonObject &root) } } +bool Theme::isAutoReloading() const +{ + return this->themeWatcher_ != nullptr; +} + +void Theme::setAutoReload(bool autoReload) +{ + // autoReload <=> themeWatcher != nullptr + if (autoReload == this->isAutoReloading()) + { + return; + } + + if (!autoReload) + { + this->themeWatcher_.reset(); + return; + } + + this->themeWatcher_ = std::make_unique(); + + QObject::connect(this->themeWatcher_.get(), + &QFileSystemWatcher::fileChanged, + [this](const auto &path) { + this->watchedFileChanged(path); + }); + this->updateCurrentWatchPath(); + qCDebug(chatterinoTheme) << "Enabled theme watcher"; +} + +void Theme::updateCurrentWatchPath() +{ + if (this->themeWatcher_ == nullptr) + { + return; + } + auto files = this->themeWatcher_->files(); + + if (files.empty()) + { + this->themeWatcher_->addPath(this->currentThemePath_); + return; + } + + if (files.length() == 1) + { + auto watched = files[0]; + if (watched == this->currentThemePath_) + { + return; + } + this->themeWatcher_->addPath(this->currentThemePath_); + this->themeWatcher_->removePath(watched); + return; + } + + this->themeWatcher_->removePaths(files); + this->themeWatcher_->addPath(this->currentThemePath_); +} + +void Theme::watchedFileChanged(const QString &path) +{ + QFile target(path); + if (!target.exists()) + { + return; + } + if (target.size() < 2) + { + return; + } + + constexpr const double nsToMs = 1.0 / 1000000.0; + QElapsedTimer timer; + timer.start(); + + auto json = loadThemeFromPath(path); + auto loadTs = double(timer.nsecsElapsed()) * nsToMs; + + if (!json) + { + qCWarning(chatterinoTheme) << "Failed to load theme JSON from" << path; + return; + } + this->parseFrom(*json); + auto parseTs = double(timer.nsecsElapsed()) * nsToMs; + + this->updated.invoke(); + auto updateTs = double(timer.nsecsElapsed()) * nsToMs; + + qCDebug(chatterinoTheme).nospace().noquote() + << "Reloaded theme in " << QString::number(updateTs, 'f', 2) + << "ms (load: " << QString::number(loadTs, 'f', 2) + << "ms, parse: " << QString::number(parseTs - loadTs, 'f', 2) + << "ms, update: " << QString::number(updateTs - parseTs, 'f', 2) + << "ms)"; +} + void Theme::normalizeColor(QColor &color) const { if (this->isLightTheme()) diff --git a/src/singletons/Theme.hpp b/src/singletons/Theme.hpp index 3a7e1c9841e..b6137e67f4c 100644 --- a/src/singletons/Theme.hpp +++ b/src/singletons/Theme.hpp @@ -6,11 +6,13 @@ #include #include +#include #include #include #include #include +#include #include #include @@ -138,6 +140,9 @@ class Theme final : public Singleton void normalizeColor(QColor &color) const; void update(); + bool isAutoReloading() const; + void setAutoReload(bool autoReload); + /** * Return a list of available themes **/ @@ -152,6 +157,11 @@ class Theme final : public Singleton std::vector availableThemes_; + QString currentThemePath_; + std::unique_ptr themeWatcher_; + void watchedFileChanged(const QString &path); + void updateCurrentWatchPath(); + /** * Figure out which themes are available in the Themes directory * diff --git a/src/widgets/settingspages/GeneralPage.cpp b/src/widgets/settingspages/GeneralPage.cpp index d55dc1f4d27..4106c88f0d3 100644 --- a/src/widgets/settingspages/GeneralPage.cpp +++ b/src/widgets/settingspages/GeneralPage.cpp @@ -1002,6 +1002,15 @@ void GeneralPage::initLayout(GeneralPageView &layout) layout.addIntInput("Usercard scrollback limit (requires restart)", s.scrollbackUsercardLimit, 100, 100000, 100); + layout.addSessionCheckbox( + "Automatically reload custom theme (session only)", + getTheme()->isAutoReloading(), + [](bool on) { + getTheme()->setAutoReload(on); + }, + "Automatically reloads the theme when the custom theme file is " + "updated. This only applies to the current session to save resources."); + layout.addCheckbox("Enable experimental IRC support (requires restart)", s.enableExperimentalIrc, false, "When enabled, attempting to join a channel will " From 66fefd7214a6a53a05d0dada5763063ff9f3ea3b Mon Sep 17 00:00:00 2001 From: Nerixyz Date: Fri, 7 Jul 2023 20:03:22 +0200 Subject: [PATCH 07/11] chore: add changelog entry --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7a64364ee2d..0fd2eeb0136 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ - Minor: Added a message for when Chatterino joins a channel (#4616) - Minor: Add pin action to usercards and reply threads. (#4692) - Minor: Stream status requests are now batched. (#4713) +- Minor: Added option to automatically reload a custom theme. (#4718) - Bugfix: Fixed generation of crashdumps by the browser-extension process when the browser was closed. (#4667) - Bugfix: Fix spacing issue with mentions inside RTL text. (#4677) - Bugfix: Fixed a crash when opening and closing a reply thread and switching the user. (#4675) From 8388fe8518c9a635c64939add622300375193bd2 Mon Sep 17 00:00:00 2001 From: Nerixyz Date: Sat, 22 Jul 2023 15:09:35 +0200 Subject: [PATCH 08/11] revert: ui checkbox --- src/widgets/settingspages/GeneralPage.cpp | 9 -------- src/widgets/settingspages/GeneralPageView.cpp | 22 ------------------- src/widgets/settingspages/GeneralPageView.hpp | 4 ---- 3 files changed, 35 deletions(-) diff --git a/src/widgets/settingspages/GeneralPage.cpp b/src/widgets/settingspages/GeneralPage.cpp index 4106c88f0d3..d55dc1f4d27 100644 --- a/src/widgets/settingspages/GeneralPage.cpp +++ b/src/widgets/settingspages/GeneralPage.cpp @@ -1002,15 +1002,6 @@ void GeneralPage::initLayout(GeneralPageView &layout) layout.addIntInput("Usercard scrollback limit (requires restart)", s.scrollbackUsercardLimit, 100, 100000, 100); - layout.addSessionCheckbox( - "Automatically reload custom theme (session only)", - getTheme()->isAutoReloading(), - [](bool on) { - getTheme()->setAutoReload(on); - }, - "Automatically reloads the theme when the custom theme file is " - "updated. This only applies to the current session to save resources."); - layout.addCheckbox("Enable experimental IRC support (requires restart)", s.enableExperimentalIrc, false, "When enabled, attempting to join a channel will " diff --git a/src/widgets/settingspages/GeneralPageView.cpp b/src/widgets/settingspages/GeneralPageView.cpp index 486a58a34b9..0d1ab0b25a4 100644 --- a/src/widgets/settingspages/GeneralPageView.cpp +++ b/src/widgets/settingspages/GeneralPageView.cpp @@ -125,28 +125,6 @@ QCheckBox *GeneralPageView::addCheckbox(const QString &text, return check; } -QCheckBox *GeneralPageView::addSessionCheckbox( - const QString &text, bool initialValue, std::function onUpdated, - QString toolTipText) -{ - auto *check = new QCheckBox(text); - this->addToolTip(*check, std::move(toolTipText)); - check->setChecked(initialValue); - - // update setting on toggle - QObject::connect(check, &QCheckBox::toggled, this, - [onUpdated = std::move(onUpdated)](bool state) { - onUpdated(state); - }); - - this->addWidget(check); - - // groups - this->groups_.back().widgets.push_back({check, {text}}); - - return check; -} - ComboBox *GeneralPageView::addDropdown(const QString &text, const QStringList &list, QString toolTipText) diff --git a/src/widgets/settingspages/GeneralPageView.hpp b/src/widgets/settingspages/GeneralPageView.hpp index 5153ab822e0..101b0b7b9cd 100644 --- a/src/widgets/settingspages/GeneralPageView.hpp +++ b/src/widgets/settingspages/GeneralPageView.hpp @@ -103,10 +103,6 @@ class GeneralPageView : public QWidget /// @param inverse Inverses true to false and vice versa QCheckBox *addCheckbox(const QString &text, BoolSetting &setting, bool inverse = false, QString toolTipText = {}); - /// Adds a checkbox that only keeps its state for the current session - QCheckBox *addSessionCheckbox(const QString &text, bool initialValue, - std::function onUpdated, - QString toolTipText = {}); ComboBox *addDropdown(const QString &text, const QStringList &items, QString toolTipText = {}); ComboBox *addDropdown(const QString &text, const QStringList &items, From 35c0ce5c616511e358bd41d707bd26d6bd708a69 Mon Sep 17 00:00:00 2001 From: Nerixyz Date: Sat, 22 Jul 2023 15:10:27 +0200 Subject: [PATCH 09/11] refactor: use a QTimer --- src/singletons/Theme.cpp | 112 ++++++++++++--------------------------- src/singletons/Theme.hpp | 10 ++-- 2 files changed, 39 insertions(+), 83 deletions(-) diff --git a/src/singletons/Theme.cpp b/src/singletons/Theme.cpp index 23e5d25cb8a..e8a110d538f 100644 --- a/src/singletons/Theme.cpp +++ b/src/singletons/Theme.cpp @@ -237,6 +237,10 @@ void Theme::update() { auto oTheme = this->findThemeByKey(this->themeName); + constexpr const double nsToMs = 1.0 / 1000000.0; + QElapsedTimer timer; + timer.start(); + std::optional themeJSON; QString themePath; if (!oTheme) @@ -266,6 +270,7 @@ void Theme::update() themePath = fallbackTheme.path; } } + auto loadTs = double(timer.nsecsElapsed()) * nsToMs; if (!themeJSON) { @@ -274,11 +279,29 @@ void Theme::update() return; } + if (this->isAutoReloading() && this->currentThemeJson_ == *themeJSON) + { + return; + } + this->parseFrom(*themeJSON); this->currentThemePath_ = themePath; + auto parseTs = double(timer.nsecsElapsed()) * nsToMs; + this->updated.invoke(); - this->updateCurrentWatchPath(); + auto updateTs = double(timer.nsecsElapsed()) * nsToMs; + qCDebug(chatterinoTheme).nospace().noquote() + << "Updated theme in " << QString::number(updateTs, 'f', 2) + << "ms (load: " << QString::number(loadTs, 'f', 2) + << "ms, parse: " << QString::number(parseTs - loadTs, 'f', 2) + << "ms, update: " << QString::number(updateTs - parseTs, 'f', 2) + << "ms)"; + + if (this->isAutoReloading()) + { + this->currentThemeJson_ = *themeJSON; + } } std::vector> Theme::availableThemes() const @@ -387,12 +410,11 @@ void Theme::parseFrom(const QJsonObject &root) bool Theme::isAutoReloading() const { - return this->themeWatcher_ != nullptr; + return this->themeReloadTimer_ != nullptr; } void Theme::setAutoReload(bool autoReload) { - // autoReload <=> themeWatcher != nullptr if (autoReload == this->isAutoReloading()) { return; @@ -400,89 +422,21 @@ void Theme::setAutoReload(bool autoReload) if (!autoReload) { - this->themeWatcher_.reset(); + this->themeReloadTimer_.reset(); + this->currentThemeJson_ = {}; return; } - this->themeWatcher_ = std::make_unique(); + this->themeReloadTimer_ = std::make_unique(); + QObject::connect(this->themeReloadTimer_.get(), &QTimer::timeout, [this]() { + this->update(); + }); + this->themeReloadTimer_->setInterval(Theme::AUTO_RELOAD_INTERVAL_MS); + this->themeReloadTimer_->start(); - QObject::connect(this->themeWatcher_.get(), - &QFileSystemWatcher::fileChanged, - [this](const auto &path) { - this->watchedFileChanged(path); - }); - this->updateCurrentWatchPath(); qCDebug(chatterinoTheme) << "Enabled theme watcher"; } -void Theme::updateCurrentWatchPath() -{ - if (this->themeWatcher_ == nullptr) - { - return; - } - auto files = this->themeWatcher_->files(); - - if (files.empty()) - { - this->themeWatcher_->addPath(this->currentThemePath_); - return; - } - - if (files.length() == 1) - { - auto watched = files[0]; - if (watched == this->currentThemePath_) - { - return; - } - this->themeWatcher_->addPath(this->currentThemePath_); - this->themeWatcher_->removePath(watched); - return; - } - - this->themeWatcher_->removePaths(files); - this->themeWatcher_->addPath(this->currentThemePath_); -} - -void Theme::watchedFileChanged(const QString &path) -{ - QFile target(path); - if (!target.exists()) - { - return; - } - if (target.size() < 2) - { - return; - } - - constexpr const double nsToMs = 1.0 / 1000000.0; - QElapsedTimer timer; - timer.start(); - - auto json = loadThemeFromPath(path); - auto loadTs = double(timer.nsecsElapsed()) * nsToMs; - - if (!json) - { - qCWarning(chatterinoTheme) << "Failed to load theme JSON from" << path; - return; - } - this->parseFrom(*json); - auto parseTs = double(timer.nsecsElapsed()) * nsToMs; - - this->updated.invoke(); - auto updateTs = double(timer.nsecsElapsed()) * nsToMs; - - qCDebug(chatterinoTheme).nospace().noquote() - << "Reloaded theme in " << QString::number(updateTs, 'f', 2) - << "ms (load: " << QString::number(loadTs, 'f', 2) - << "ms, parse: " << QString::number(parseTs - loadTs, 'f', 2) - << "ms, update: " << QString::number(updateTs - parseTs, 'f', 2) - << "ms)"; -} - void Theme::normalizeColor(QColor &color) const { if (this->isLightTheme()) diff --git a/src/singletons/Theme.hpp b/src/singletons/Theme.hpp index b6137e67f4c..d2165543c4a 100644 --- a/src/singletons/Theme.hpp +++ b/src/singletons/Theme.hpp @@ -6,10 +6,10 @@ #include #include -#include #include #include #include +#include #include #include @@ -41,6 +41,8 @@ class Theme final : public Singleton // The built in theme that will be used if some theme parsing fails static const ThemeDescriptor fallbackTheme; + static const int AUTO_RELOAD_INTERVAL_MS = 500; + void initialize(Settings &settings, Paths &paths) final; bool isLightTheme() const; @@ -158,9 +160,9 @@ class Theme final : public Singleton std::vector availableThemes_; QString currentThemePath_; - std::unique_ptr themeWatcher_; - void watchedFileChanged(const QString &path); - void updateCurrentWatchPath(); + std::unique_ptr themeReloadTimer_; + // This will only be populated when auto-reloading themes + QJsonObject currentThemeJson_; /** * Figure out which themes are available in the Themes directory From ccd46cc0ce83e8504684d2a197af03406a69059f Mon Sep 17 00:00:00 2001 From: Nerixyz Date: Sat, 22 Jul 2023 15:11:21 +0200 Subject: [PATCH 10/11] refactor: use a command --- CHANGELOG.md | 2 +- .../commands/CommandController.cpp | 1 + .../commands/builtin/chatterino/Debugging.cpp | 21 +++++++++++++++++++ .../commands/builtin/chatterino/Debugging.hpp | 2 ++ 4 files changed, 25 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0fd2eeb0136..8380b8da22e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,7 +12,7 @@ - Minor: Added a message for when Chatterino joins a channel (#4616) - Minor: Add pin action to usercards and reply threads. (#4692) - Minor: Stream status requests are now batched. (#4713) -- Minor: Added option to automatically reload a custom theme. (#4718) +- Minor: Added `/c2-theme-autoreload` command to automatically reload a custom theme. (#4718) - Bugfix: Fixed generation of crashdumps by the browser-extension process when the browser was closed. (#4667) - Bugfix: Fix spacing issue with mentions inside RTL text. (#4677) - Bugfix: Fixed a crash when opening and closing a reply thread and switching the user. (#4675) diff --git a/src/controllers/commands/CommandController.cpp b/src/controllers/commands/CommandController.cpp index bd282224c5c..62da0b83b1e 100644 --- a/src/controllers/commands/CommandController.cpp +++ b/src/controllers/commands/CommandController.cpp @@ -3241,6 +3241,7 @@ void CommandController::initialize(Settings &, Paths &paths) this->registerCommand("/shoutout", &commands::sendShoutout); this->registerCommand("/c2-set-logging-rules", &commands::setLoggingRules); + this->registerCommand("/c2-theme-autoreload", &commands::toggleThemeReload); } void CommandController::save() diff --git a/src/controllers/commands/builtin/chatterino/Debugging.cpp b/src/controllers/commands/builtin/chatterino/Debugging.cpp index 7482d68ce99..7ae1ce9476c 100644 --- a/src/controllers/commands/builtin/chatterino/Debugging.cpp +++ b/src/controllers/commands/builtin/chatterino/Debugging.cpp @@ -1,14 +1,18 @@ #include "controllers/commands/builtin/chatterino/Debugging.hpp" #include "common/Channel.hpp" +#include "common/Literals.hpp" #include "controllers/commands/CommandContext.hpp" #include "messages/MessageBuilder.hpp" +#include "singletons/Theme.hpp" #include #include namespace chatterino::commands { +using namespace literals; + QString setLoggingRules(const CommandContext &ctx) { if (ctx.words.size() < 2) @@ -42,4 +46,21 @@ QString setLoggingRules(const CommandContext &ctx) return {}; } +QString toggleThemeReload(const CommandContext &ctx) +{ + if (getTheme()->isAutoReloading()) + { + getTheme()->setAutoReload(false); + ctx.channel->addMessage( + makeSystemMessage(u"Disabled theme auto reloading."_s)); + return {}; + } + + getTheme()->setAutoReload(true); + ctx.channel->addMessage( + makeSystemMessage(u"Auto reloading theme every %1 ms."_s.arg( + Theme::AUTO_RELOAD_INTERVAL_MS))); + return {}; +} + } // namespace chatterino::commands diff --git a/src/controllers/commands/builtin/chatterino/Debugging.hpp b/src/controllers/commands/builtin/chatterino/Debugging.hpp index 8cd2330ba4a..8b531455f5e 100644 --- a/src/controllers/commands/builtin/chatterino/Debugging.hpp +++ b/src/controllers/commands/builtin/chatterino/Debugging.hpp @@ -12,4 +12,6 @@ namespace chatterino::commands { QString setLoggingRules(const CommandContext &ctx); +QString toggleThemeReload(const CommandContext &ctx); + } // namespace chatterino::commands From ffb3c0a89df1f3185eade8e55a67d8c11e5823e6 Mon Sep 17 00:00:00 2001 From: Rasmus Karlsson Date: Sun, 23 Jul 2023 12:52:36 +0200 Subject: [PATCH 11/11] Update the changelog entry --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1d043dafe07..c6071edf163 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,7 +13,7 @@ - Minor: Add accelerators to the right click menu for messages (#4705) - Minor: Add pin action to usercards and reply threads. (#4692) - Minor: Stream status requests are now batched. (#4713) -- Minor: Added `/c2-theme-autoreload` command to automatically reload a custom theme. (#4718) +- Minor: Added `/c2-theme-autoreload` command to automatically reload a custom theme. This is useful for when you're developing your own theme. (#4718) - Bugfix: Fixed generation of crashdumps by the browser-extension process when the browser was closed. (#4667) - Bugfix: Fix spacing issue with mentions inside RTL text. (#4677) - Bugfix: Fixed a crash when opening and closing a reply thread and switching the user. (#4675)