From 03fcf6a6618a82b57f9f76ddb686aeb8c6aa94c4 Mon Sep 17 00:00:00 2001 From: Karthik Nishanth Date: Sun, 23 Jun 2024 12:46:35 -0700 Subject: [PATCH] theme: refactor theme names --- src/ui/NhekoGlobalObject.cpp | 8 ++++---- src/ui/Theme.cpp | 35 ++++++++++++++++++++++++++--------- src/ui/Theme.h | 17 ++++++++++++----- 3 files changed, 42 insertions(+), 18 deletions(-) diff --git a/src/ui/NhekoGlobalObject.cpp b/src/ui/NhekoGlobalObject.cpp index e28b1bc1..98caeae4 100644 --- a/src/ui/NhekoGlobalObject.cpp +++ b/src/ui/NhekoGlobalObject.cpp @@ -55,21 +55,21 @@ Nheko::inactiveColors() const auto theme = UserSettings::instance()->theme(); if (theme == QLatin1String("light")) { static QPalette lightInactive = [] { - auto lightInactive = Theme::paletteFromTheme(u"light"); + auto lightInactive = Theme::paletteFromTheme(Theme::Kind::Light); lightInactive.setCurrentColorGroup(QPalette::ColorGroup::Inactive); return lightInactive; }(); return lightInactive; } else if (theme == QLatin1String("dark")) { static QPalette darkInactive = [] { - auto darkInactive = Theme::paletteFromTheme(u"dark"); + auto darkInactive = Theme::paletteFromTheme(Theme::Kind::Dark); darkInactive.setCurrentColorGroup(QPalette::ColorGroup::Inactive); return darkInactive; }(); return darkInactive; } else { static QPalette originalInactive = [] { - auto originalInactive = Theme::paletteFromTheme(u"system"); + auto originalInactive = Theme::paletteFromTheme(Theme::Kind::System); originalInactive.setCurrentColorGroup(QPalette::ColorGroup::Inactive); return originalInactive; }(); @@ -80,7 +80,7 @@ Nheko::inactiveColors() const Theme Nheko::theme() const { - return Theme(UserSettings::instance()->theme()); + return Theme(Theme::kindFromString(UserSettings::instance()->theme())); } int diff --git a/src/ui/Theme.cpp b/src/ui/Theme.cpp index 4d46db13..11d755d9 100644 --- a/src/ui/Theme.cpp +++ b/src/ui/Theme.cpp @@ -5,10 +5,10 @@ #include "Theme.h" QPalette -Theme::paletteFromTheme(QStringView theme) +Theme::paletteFromTheme(Theme::Kind theme) { static QPalette original; - if (theme == u"light") { + if (theme == Kind::Light) { static QPalette lightActive = [] { QPalette lightActive( /*windowText*/ QColor(0x33, 0x33, 0x33), @@ -30,7 +30,7 @@ Theme::paletteFromTheme(QStringView theme) return lightActive; }(); return lightActive; - } else if (theme == u"dark") { + } else if (theme == Kind::Dark) { static QPalette darkActive = [] { QPalette darkActive( /*windowText*/ QColor(0xca, 0xcc, 0xd1), @@ -57,27 +57,30 @@ Theme::paletteFromTheme(QStringView theme) } } -Theme::Theme(QStringView theme) +QPalette +Theme::paletteFromTheme(QStringView theme) +{ + return paletteFromTheme(kindFromString(theme)); +} + +Theme::Theme(Theme::Kind theme) { auto p = paletteFromTheme(theme); separator_ = p.mid().color(); - if (theme == u"light") { + if (theme == Kind::Light) { sidebarBackground_ = QColor(0x23, 0x36, 0x49); - alternateButton_ = QColor(0xcc, 0xcc, 0xcc); red_ = QColor(0xa8, 0x23, 0x53); green_ = QColor(QColorConstants::Svg::green); orange_ = QColor(0xfc, 0xbe, 0x05); error_ = QColor(0xdd, 0x3d, 0x3d); - } else if (theme == u"dark") { + } else if (theme == Kind::Dark) { sidebarBackground_ = QColor(0x2d, 0x31, 0x39); - alternateButton_ = QColor(0x41, 0x4A, 0x59); red_ = QColor(0xa8, 0x23, 0x53); green_ = QColor(QColorConstants::Svg::green); orange_ = QColor(0xfc, 0xc5, 0x3a); error_ = QColor(0xdd, 0x3d, 0x3d); } else { sidebarBackground_ = p.window().color(); - alternateButton_ = p.dark().color(); red_ = QColor(QColorConstants::Svg::red); green_ = QColor(QColorConstants::Svg::green); orange_ = QColor(QColorConstants::Svg::orange); // SVG orange @@ -85,4 +88,18 @@ Theme::Theme(QStringView theme) } } +Theme::Kind +Theme::kindFromString(QStringView kind) +{ + if (kind == u"light") { + return Kind::Light; + } else if (kind == u"dark") { + return Kind::Dark; + } else if (kind == u"system") { + return Kind::System; + } else { + throw std::invalid_argument("Unknown theme kind: " + kind.toString().toStdString()); + } +} + #include "moc_Theme.cpp" diff --git a/src/ui/Theme.h b/src/ui/Theme.h index d581ffe4..2d3f6b9f 100644 --- a/src/ui/Theme.h +++ b/src/ui/Theme.h @@ -14,7 +14,6 @@ class Theme final : public QPalette QML_ANONYMOUS Q_PROPERTY(QColor sidebarBackground READ sidebarBackground CONSTANT) - Q_PROPERTY(QColor alternateButton READ alternateButton CONSTANT) Q_PROPERTY(QColor separator READ separator CONSTANT) Q_PROPERTY(QColor red READ red CONSTANT) Q_PROPERTY(QColor green READ green CONSTANT) @@ -23,12 +22,20 @@ class Theme final : public QPalette Q_PROPERTY(QColor online READ online CONSTANT) Q_PROPERTY(QColor unavailable READ unavailable CONSTANT) public: - Theme() {} - explicit Theme(QStringView theme); + enum class Kind { + Light, + Dark, + System, + }; + static Kind kindFromString(QStringView kind); + + static QPalette paletteFromTheme(Kind theme); static QPalette paletteFromTheme(QStringView theme); + Theme() {} + explicit Theme(Kind theme); + QColor sidebarBackground() const { return sidebarBackground_; } - QColor alternateButton() const { return alternateButton_; } QColor separator() const { return separator_; } QColor red() const { return red_; } QColor green() const { return green_; } @@ -38,5 +45,5 @@ public: QColor unavailable() const { return QColor(0xff, 0x99, 0x33); } private: - QColor sidebarBackground_, separator_, red_, green_, error_, orange_, alternateButton_; + QColor sidebarBackground_, separator_, red_, green_, error_, orange_; };