theme: refactor theme names

This commit is contained in:
Karthik Nishanth 2024-06-23 12:46:35 -07:00
parent 2142a8dd9c
commit 03fcf6a661
3 changed files with 42 additions and 18 deletions

View file

@ -55,21 +55,21 @@ Nheko::inactiveColors() const
auto theme = UserSettings::instance()->theme(); auto theme = UserSettings::instance()->theme();
if (theme == QLatin1String("light")) { if (theme == QLatin1String("light")) {
static QPalette lightInactive = [] { static QPalette lightInactive = [] {
auto lightInactive = Theme::paletteFromTheme(u"light"); auto lightInactive = Theme::paletteFromTheme(Theme::Kind::Light);
lightInactive.setCurrentColorGroup(QPalette::ColorGroup::Inactive); lightInactive.setCurrentColorGroup(QPalette::ColorGroup::Inactive);
return lightInactive; return lightInactive;
}(); }();
return lightInactive; return lightInactive;
} else if (theme == QLatin1String("dark")) { } else if (theme == QLatin1String("dark")) {
static QPalette darkInactive = [] { static QPalette darkInactive = [] {
auto darkInactive = Theme::paletteFromTheme(u"dark"); auto darkInactive = Theme::paletteFromTheme(Theme::Kind::Dark);
darkInactive.setCurrentColorGroup(QPalette::ColorGroup::Inactive); darkInactive.setCurrentColorGroup(QPalette::ColorGroup::Inactive);
return darkInactive; return darkInactive;
}(); }();
return darkInactive; return darkInactive;
} else { } else {
static QPalette originalInactive = [] { static QPalette originalInactive = [] {
auto originalInactive = Theme::paletteFromTheme(u"system"); auto originalInactive = Theme::paletteFromTheme(Theme::Kind::System);
originalInactive.setCurrentColorGroup(QPalette::ColorGroup::Inactive); originalInactive.setCurrentColorGroup(QPalette::ColorGroup::Inactive);
return originalInactive; return originalInactive;
}(); }();
@ -80,7 +80,7 @@ Nheko::inactiveColors() const
Theme Theme
Nheko::theme() const Nheko::theme() const
{ {
return Theme(UserSettings::instance()->theme()); return Theme(Theme::kindFromString(UserSettings::instance()->theme()));
} }
int int

View file

@ -5,10 +5,10 @@
#include "Theme.h" #include "Theme.h"
QPalette QPalette
Theme::paletteFromTheme(QStringView theme) Theme::paletteFromTheme(Theme::Kind theme)
{ {
static QPalette original; static QPalette original;
if (theme == u"light") { if (theme == Kind::Light) {
static QPalette lightActive = [] { static QPalette lightActive = [] {
QPalette lightActive( QPalette lightActive(
/*windowText*/ QColor(0x33, 0x33, 0x33), /*windowText*/ QColor(0x33, 0x33, 0x33),
@ -30,7 +30,7 @@ Theme::paletteFromTheme(QStringView theme)
return lightActive; return lightActive;
}(); }();
return lightActive; return lightActive;
} else if (theme == u"dark") { } else if (theme == Kind::Dark) {
static QPalette darkActive = [] { static QPalette darkActive = [] {
QPalette darkActive( QPalette darkActive(
/*windowText*/ QColor(0xca, 0xcc, 0xd1), /*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); auto p = paletteFromTheme(theme);
separator_ = p.mid().color(); separator_ = p.mid().color();
if (theme == u"light") { if (theme == Kind::Light) {
sidebarBackground_ = QColor(0x23, 0x36, 0x49); sidebarBackground_ = QColor(0x23, 0x36, 0x49);
alternateButton_ = QColor(0xcc, 0xcc, 0xcc);
red_ = QColor(0xa8, 0x23, 0x53); red_ = QColor(0xa8, 0x23, 0x53);
green_ = QColor(QColorConstants::Svg::green); green_ = QColor(QColorConstants::Svg::green);
orange_ = QColor(0xfc, 0xbe, 0x05); orange_ = QColor(0xfc, 0xbe, 0x05);
error_ = QColor(0xdd, 0x3d, 0x3d); error_ = QColor(0xdd, 0x3d, 0x3d);
} else if (theme == u"dark") { } else if (theme == Kind::Dark) {
sidebarBackground_ = QColor(0x2d, 0x31, 0x39); sidebarBackground_ = QColor(0x2d, 0x31, 0x39);
alternateButton_ = QColor(0x41, 0x4A, 0x59);
red_ = QColor(0xa8, 0x23, 0x53); red_ = QColor(0xa8, 0x23, 0x53);
green_ = QColor(QColorConstants::Svg::green); green_ = QColor(QColorConstants::Svg::green);
orange_ = QColor(0xfc, 0xc5, 0x3a); orange_ = QColor(0xfc, 0xc5, 0x3a);
error_ = QColor(0xdd, 0x3d, 0x3d); error_ = QColor(0xdd, 0x3d, 0x3d);
} else { } else {
sidebarBackground_ = p.window().color(); sidebarBackground_ = p.window().color();
alternateButton_ = p.dark().color();
red_ = QColor(QColorConstants::Svg::red); red_ = QColor(QColorConstants::Svg::red);
green_ = QColor(QColorConstants::Svg::green); green_ = QColor(QColorConstants::Svg::green);
orange_ = QColor(QColorConstants::Svg::orange); // SVG orange 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" #include "moc_Theme.cpp"

View file

@ -14,7 +14,6 @@ class Theme final : public QPalette
QML_ANONYMOUS QML_ANONYMOUS
Q_PROPERTY(QColor sidebarBackground READ sidebarBackground CONSTANT) Q_PROPERTY(QColor sidebarBackground READ sidebarBackground CONSTANT)
Q_PROPERTY(QColor alternateButton READ alternateButton CONSTANT)
Q_PROPERTY(QColor separator READ separator CONSTANT) Q_PROPERTY(QColor separator READ separator CONSTANT)
Q_PROPERTY(QColor red READ red CONSTANT) Q_PROPERTY(QColor red READ red CONSTANT)
Q_PROPERTY(QColor green READ green 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 online READ online CONSTANT)
Q_PROPERTY(QColor unavailable READ unavailable CONSTANT) Q_PROPERTY(QColor unavailable READ unavailable CONSTANT)
public: public:
Theme() {} enum class Kind {
explicit Theme(QStringView theme); Light,
Dark,
System,
};
static Kind kindFromString(QStringView kind);
static QPalette paletteFromTheme(Kind theme);
static QPalette paletteFromTheme(QStringView theme); static QPalette paletteFromTheme(QStringView theme);
Theme() {}
explicit Theme(Kind theme);
QColor sidebarBackground() const { return sidebarBackground_; } QColor sidebarBackground() const { return sidebarBackground_; }
QColor alternateButton() const { return alternateButton_; }
QColor separator() const { return separator_; } QColor separator() const { return separator_; }
QColor red() const { return red_; } QColor red() const { return red_; }
QColor green() const { return green_; } QColor green() const { return green_; }
@ -38,5 +45,5 @@ public:
QColor unavailable() const { return QColor(0xff, 0x99, 0x33); } QColor unavailable() const { return QColor(0xff, 0x99, 0x33); }
private: private:
QColor sidebarBackground_, separator_, red_, green_, error_, orange_, alternateButton_; QColor sidebarBackground_, separator_, red_, green_, error_, orange_;
}; };