From 91a82c4db95cd886acc9712f1bab06c9f3eb26db Mon Sep 17 00:00:00 2001 From: Nerixyz Date: Sun, 21 Jan 2024 12:10:26 +0100 Subject: [PATCH 1/5] feat: add system theme on Qt 6.5 --- src/singletons/Theme.cpp | 47 +++++++++++++++++++++++++++++++++++++++- src/singletons/Theme.hpp | 3 +++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/src/singletons/Theme.cpp b/src/singletons/Theme.cpp index ba7cbf63e4a..b16aa7ee0b2 100644 --- a/src/singletons/Theme.cpp +++ b/src/singletons/Theme.cpp @@ -6,6 +6,7 @@ #include "common/QLogging.hpp" #include "singletons/Paths.hpp" #include "singletons/Resources.hpp" +#include "singletons/WindowManager.hpp" #include #include @@ -13,6 +14,9 @@ #include #include #include +#if QT_VERSION >= QT_VERSION_CHECK(6, 5, 0) +# include +#endif #include @@ -208,7 +212,15 @@ const std::vector Theme::builtInThemes{ .key = "Black", .path = ":/themes/Black.json", .name = "Black", + } +#if QT_VERSION >= QT_VERSION_CHECK(6, 5, 0) + , + { + .key = "System", + .path = ":/themes/Dark.json", + .name = "System", }, +#endif }; // Dark is our default & fallback theme @@ -219,6 +231,11 @@ bool Theme::isLightTheme() const return this->isLight_; } +bool Theme::isSystemTheme() const +{ + return this->themeName == u"System"; +} + void Theme::initialize(Settings &settings, const Paths &paths) { this->themeName.connect( @@ -230,12 +247,40 @@ void Theme::initialize(Settings &settings, const Paths &paths) this->loadAvailableThemes(paths); +#if QT_VERSION >= QT_VERSION_CHECK(6, 5, 0) + QObject::connect(qApp->styleHints(), &QStyleHints::colorSchemeChanged, + &this->lifetime_, [this] { + if (this->isSystemTheme()) + { + this->update(); + getIApp()->getWindows()->forceLayoutChannelViews(); + } + }); +#endif + this->update(); } void Theme::update() { - auto oTheme = this->findThemeByKey(this->themeName); + auto currentTheme = [&]() -> QString { +#if QT_VERSION >= QT_VERSION_CHECK(6, 5, 0) + if (this->isSystemTheme()) + { + switch (qApp->styleHints()->colorScheme()) + { + case Qt::ColorScheme::Light: + return u"Light"_s; + case Qt::ColorScheme::Unknown: + case Qt::ColorScheme::Dark: + return u"Dark"_s; + } + } +#endif + return this->themeName; + }; + + auto oTheme = this->findThemeByKey(currentTheme()); constexpr const double nsToMs = 1.0 / 1000000.0; QElapsedTimer timer; diff --git a/src/singletons/Theme.hpp b/src/singletons/Theme.hpp index 034a01b6438..4040dc9eeac 100644 --- a/src/singletons/Theme.hpp +++ b/src/singletons/Theme.hpp @@ -46,6 +46,7 @@ class Theme final : public Singleton void initialize(Settings &settings, const Paths &paths) final; bool isLightTheme() const; + bool isSystemTheme() const; struct TabColors { QColor text; @@ -164,6 +165,8 @@ class Theme final : public Singleton // This will only be populated when auto-reloading themes QJsonObject currentThemeJson_; + QObject lifetime_; + /** * Figure out which themes are available in the Themes directory * From c339fe486c839d57734b8635d22b06c84273255c Mon Sep 17 00:00:00 2001 From: Nerixyz Date: Sun, 21 Jan 2024 12:25:17 +0100 Subject: [PATCH 2/5] chroe: add changelog entry --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index dca7de28804..6cdc20a4510 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,6 +26,7 @@ - Minor: Normalized the input padding between light & dark themes. (#5095) - Minor: Add `--activate ` (or `-a`) command line option to activate or add a Twitch channel. (#5111) - Minor: Chatters from recent-messages are now added to autocompletion. (#5116) +- Minor: Add _System_ theme that updates according to the system's color scheme (requires Qt 6.5). (#5118) - Bugfix: Fixed an issue where certain emojis did not send to Twitch chat correctly. (#4840) - Bugfix: Fixed capitalized channel names in log inclusion list not being logged. (#4848) - Bugfix: Trimmed custom streamlink paths on all platforms making sure you don't accidentally add spaces at the beginning or end of its path. (#4834) From c5a800280fc24c5c137b8a9918bb01057823cdf2 Mon Sep 17 00:00:00 2001 From: Nerixyz Date: Mon, 22 Jan 2024 19:55:43 +0100 Subject: [PATCH 3/5] refactor: add separate settings --- src/singletons/Theme.cpp | 20 +++---- src/singletons/Theme.hpp | 3 ++ src/widgets/settingspages/GeneralPage.cpp | 53 +++++++++++++++---- src/widgets/settingspages/GeneralPageView.hpp | 9 ++++ 4 files changed, 65 insertions(+), 20 deletions(-) diff --git a/src/singletons/Theme.cpp b/src/singletons/Theme.cpp index b16aa7ee0b2..f7fee968546 100644 --- a/src/singletons/Theme.cpp +++ b/src/singletons/Theme.cpp @@ -212,15 +212,7 @@ const std::vector Theme::builtInThemes{ .key = "Black", .path = ":/themes/Black.json", .name = "Black", - } -#if QT_VERSION >= QT_VERSION_CHECK(6, 5, 0) - , - { - .key = "System", - .path = ":/themes/Dark.json", - .name = "System", }, -#endif }; // Dark is our default & fallback theme @@ -244,6 +236,14 @@ void Theme::initialize(Settings &settings, const Paths &paths) this->update(); }, false); + auto updateIfSystem = [this](const auto &) { + if (this->isSystemTheme()) + { + this->update(); + } + }; + this->darkSystemThemeName.connect(updateIfSystem, false); + this->lightSystemThemeName.connect(updateIfSystem, false); this->loadAvailableThemes(paths); @@ -270,10 +270,10 @@ void Theme::update() switch (qApp->styleHints()->colorScheme()) { case Qt::ColorScheme::Light: - return u"Light"_s; + return this->lightSystemThemeName; case Qt::ColorScheme::Unknown: case Qt::ColorScheme::Dark: - return u"Dark"_s; + return this->darkSystemThemeName; } } #endif diff --git a/src/singletons/Theme.hpp b/src/singletons/Theme.hpp index 4040dc9eeac..d67ff8a9eb3 100644 --- a/src/singletons/Theme.hpp +++ b/src/singletons/Theme.hpp @@ -154,6 +154,9 @@ class Theme final : public Singleton pajlada::Signals::NoArgSignal updated; QStringSetting themeName{"/appearance/theme/name", "Dark"}; + QStringSetting lightSystemThemeName{"/appearance/theme/lightSystem", + "Light"}; + QStringSetting darkSystemThemeName{"/appearance/theme/darkSystem", "Dark"}; private: bool isLight_ = false; diff --git a/src/widgets/settingspages/GeneralPage.cpp b/src/widgets/settingspages/GeneralPage.cpp index 7f414e81ba4..3c7938f46a9 100644 --- a/src/widgets/settingspages/GeneralPage.cpp +++ b/src/widgets/settingspages/GeneralPage.cpp @@ -122,16 +122,49 @@ void GeneralPage::initLayout(GeneralPageView &layout) layout.addTitle("Interface"); - layout.addDropdown( - "Theme", getIApp()->getThemes()->availableThemes(), - getIApp()->getThemes()->themeName, - [](const auto *combo, const auto &themeKey) { - return combo->findData(themeKey, Qt::UserRole); - }, - [](const auto &args) { - return args.combobox->itemData(args.index, Qt::UserRole).toString(); - }, - {}, Theme::fallbackTheme.name); + { + auto *themes = getIApp()->getThemes(); + auto available = themes->availableThemes(); +#if QT_VERSION >= QT_VERSION_CHECK(6, 5, 0) + available.emplace_back("System", "System"); +#endif + + auto addThemeDropdown = [&](auto name, auto &setting, + const auto &options, + const QString &tooltip = {}) { + return layout.addDropdown( + name, options, setting, + [](const auto *combo, const auto &themeKey) { + return combo->findData(themeKey, Qt::UserRole); + }, + [](const auto &args) { + return args.combobox->itemData(args.index, Qt::UserRole) + .toString(); + }, + tooltip, Theme::fallbackTheme.name); + }; + + addThemeDropdown("Theme", themes->themeName, available); + +#if QT_VERSION >= QT_VERSION_CHECK(6, 5, 0) + auto *darkDropdown = addThemeDropdown( + "Dark system theme", themes->darkSystemThemeName, + themes->availableThemes(), + "This theme is selected if your system is in a dark theme and you " + "enabled the adaptive 'System' theme."); + auto *lightDropdown = addThemeDropdown( + "Light system theme", themes->lightSystemThemeName, + themes->availableThemes(), + "This theme is selected if your system is in a light theme and you " + "enabled the adaptive 'System' theme."); + + auto isSystem = [](const auto &s) { + return s == "System"; + }; + layout.enableIf(darkDropdown, themes->themeName, isSystem); + layout.enableIf(lightDropdown, themes->themeName, isSystem); +#endif + } layout.addDropdown( "Font", {"Segoe UI", "Arial", "Choose..."}, diff --git a/src/widgets/settingspages/GeneralPageView.hpp b/src/widgets/settingspages/GeneralPageView.hpp index fce52077b54..9e10e5cf36e 100644 --- a/src/widgets/settingspages/GeneralPageView.hpp +++ b/src/widgets/settingspages/GeneralPageView.hpp @@ -306,6 +306,15 @@ class GeneralPageView : public QWidget return combo; } + void enableIf(QComboBox *widget, auto &setting, auto cb) + { + auto updateVisibility = [cb = std::move(cb), &setting, widget]() { + auto enabled = cb(setting.getValue()); + widget->setEnabled(enabled); + }; + setting.connect(updateVisibility, this->managedConnections_); + } + DescriptionLabel *addDescription(const QString &text); void addSeperator(); From 701d60b2fe8d334d66757522aba642ed129d0d90 Mon Sep 17 00:00:00 2001 From: Nerixyz Date: Mon, 22 Jan 2024 19:57:44 +0100 Subject: [PATCH 4/5] fix: qt 5 --- src/singletons/Theme.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/singletons/Theme.cpp b/src/singletons/Theme.cpp index f7fee968546..e89c31f9630 100644 --- a/src/singletons/Theme.cpp +++ b/src/singletons/Theme.cpp @@ -225,7 +225,7 @@ bool Theme::isLightTheme() const bool Theme::isSystemTheme() const { - return this->themeName == u"System"; + return this->themeName == u"System"_s; } void Theme::initialize(Settings &settings, const Paths &paths) From a4a476bd19c6c78773f33db5c04bf81966a70ebf Mon Sep 17 00:00:00 2001 From: Rasmus Karlsson Date: Sat, 27 Jan 2024 11:26:04 +0100 Subject: [PATCH 5/5] Update changelog entry --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6cdc20a4510..adbac1a11fb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,7 +26,7 @@ - Minor: Normalized the input padding between light & dark themes. (#5095) - Minor: Add `--activate ` (or `-a`) command line option to activate or add a Twitch channel. (#5111) - Minor: Chatters from recent-messages are now added to autocompletion. (#5116) -- Minor: Add _System_ theme that updates according to the system's color scheme (requires Qt 6.5). (#5118) +- Minor: Added a _System_ theme that updates according to the system's color scheme (requires Qt 6.5). (#5118) - Bugfix: Fixed an issue where certain emojis did not send to Twitch chat correctly. (#4840) - Bugfix: Fixed capitalized channel names in log inclusion list not being logged. (#4848) - Bugfix: Trimmed custom streamlink paths on all platforms making sure you don't accidentally add spaces at the beginning or end of its path. (#4834)