From 497c3df50ec2eb0f2c5d1e7cf324c85a7fb90da1 Mon Sep 17 00:00:00 2001 From: Nicolas Werner Date: Wed, 29 Dec 2021 08:05:29 +0100 Subject: [PATCH] Prevent copies when querying theme --- src/UserSettingsPage.cpp | 2 +- src/ui/NhekoGlobalObject.cpp | 38 +++++++++++++++++++++++++++--------- src/ui/Theme.cpp | 12 ++++++------ src/ui/Theme.h | 4 ++-- 4 files changed, 38 insertions(+), 18 deletions(-) diff --git a/src/UserSettingsPage.cpp b/src/UserSettingsPage.cpp index 63563f3f..8c01a11d 100644 --- a/src/UserSettingsPage.cpp +++ b/src/UserSettingsPage.cpp @@ -663,7 +663,7 @@ UserSettings::applyTheme() } else { stylefile.setFileName(QStringLiteral(":/styles/styles/system.qss")); } - QApplication::setPalette(Theme::paletteFromTheme(this->theme().toStdString())); + QApplication::setPalette(Theme::paletteFromTheme(this->theme())); stylefile.open(QFile::ReadOnly); QString stylesheet = QString(stylefile.readAll()); diff --git a/src/ui/NhekoGlobalObject.cpp b/src/ui/NhekoGlobalObject.cpp index 56c0d9f0..bc578476 100644 --- a/src/ui/NhekoGlobalObject.cpp +++ b/src/ui/NhekoGlobalObject.cpp @@ -38,21 +38,41 @@ Nheko::updateUserProfile() QPalette Nheko::colors() const { - return Theme::paletteFromTheme(UserSettings::instance()->theme().toStdString()); + return Theme::paletteFromTheme(UserSettings::instance()->theme()); } QPalette Nheko::inactiveColors() const { - auto p = colors(); - p.setCurrentColorGroup(QPalette::ColorGroup::Inactive); - return p; + auto theme = UserSettings::instance()->theme(); + if (theme == QLatin1String("light")) { + static QPalette lightInactive = [] { + auto lightInactive = Theme::paletteFromTheme(u"light"); + lightInactive.setCurrentColorGroup(QPalette::ColorGroup::Inactive); + return lightInactive; + }(); + return lightInactive; + } else if (theme == QLatin1String("dark")) { + static QPalette darkInactive = [] { + auto darkInactive = Theme::paletteFromTheme(u"dark"); + darkInactive.setCurrentColorGroup(QPalette::ColorGroup::Inactive); + return darkInactive; + }(); + return darkInactive; + } else { + static QPalette originalInactive = [] { + auto originalInactive = Theme::paletteFromTheme(u"system"); + originalInactive.setCurrentColorGroup(QPalette::ColorGroup::Inactive); + return originalInactive; + }(); + return originalInactive; + } } Theme Nheko::theme() const { - return Theme(UserSettings::instance()->theme().toStdString()); + return Theme(UserSettings::instance()->theme()); } void @@ -61,10 +81,10 @@ Nheko::openLink(QString link) const QUrl url(link); // Open externally if we couldn't handle it internally if (!ChatPage::instance()->handleMatrixUri(url)) { - const QStringList allowedUrlSchemes = { - "http", - "https", - "mailto", + static const QStringList allowedUrlSchemes = { + QStringLiteral("http"), + QStringLiteral("https"), + QStringLiteral("mailto"), }; if (allowedUrlSchemes.contains(url.scheme())) diff --git a/src/ui/Theme.cpp b/src/ui/Theme.cpp index 6301735e..aa2c59b1 100644 --- a/src/ui/Theme.cpp +++ b/src/ui/Theme.cpp @@ -7,11 +7,11 @@ Q_DECLARE_METATYPE(Theme) QPalette -Theme::paletteFromTheme(std::string_view theme) +Theme::paletteFromTheme(QStringView theme) { [[maybe_unused]] static auto meta = qRegisterMetaType("Theme"); static QPalette original; - if (theme == "light") { + if (theme == u"light") { static QPalette lightActive = [] { QPalette lightActive( /*windowText*/ QColor(0x33, 0x33, 0x33), @@ -33,7 +33,7 @@ Theme::paletteFromTheme(std::string_view theme) return lightActive; }(); return lightActive; - } else if (theme == "dark") { + } else if (theme == u"dark") { static QPalette darkActive = [] { QPalette darkActive( /*windowText*/ QColor(0xca, 0xcc, 0xd1), @@ -60,16 +60,16 @@ Theme::paletteFromTheme(std::string_view theme) } } -Theme::Theme(std::string_view theme) +Theme::Theme(QStringView theme) { auto p = paletteFromTheme(theme); separator_ = p.mid().color(); - if (theme == "light") { + if (theme == u"light") { sidebarBackground_ = QColor(0x23, 0x36, 0x49); alternateButton_ = QColor(0xcc, 0xcc, 0xcc); red_ = QColor(0xa8, 0x23, 0x53); orange_ = QColor(0xfc, 0xbe, 0x05); - } else if (theme == "dark") { + } else if (theme == u"dark") { sidebarBackground_ = QColor(0x2d, 0x31, 0x39); alternateButton_ = QColor(0x41, 0x4A, 0x59); red_ = QColor(0xa8, 0x23, 0x53); diff --git a/src/ui/Theme.h b/src/ui/Theme.h index 4dbe03f9..457faf7b 100644 --- a/src/ui/Theme.h +++ b/src/ui/Theme.h @@ -66,8 +66,8 @@ class Theme : public QPalette Q_PROPERTY(QColor orange READ orange CONSTANT) public: Theme() {} - explicit Theme(std::string_view theme); - static QPalette paletteFromTheme(std::string_view theme); + explicit Theme(QStringView theme); + static QPalette paletteFromTheme(QStringView theme); QColor sidebarBackground() const { return sidebarBackground_; } QColor alternateButton() const { return alternateButton_; }