From 19a18e4b5d3a6efe903f553f6524e4e74556170f Mon Sep 17 00:00:00 2001 From: Vlad Zahorodnii Date: Fri, 18 Mar 2022 11:00:30 +0200 Subject: [PATCH] Clean up QPalette handling in DecorationPalette When using a "legacy" colorscheme, the QPalette will be loaded twice. With this change, the QPalette is going to be created only once. It also cleans up variable naming. --- src/decorations/decorationpalette.cpp | 64 +++++++++++++-------------- src/decorations/decorationpalette.h | 13 +++--- 2 files changed, 36 insertions(+), 41 deletions(-) diff --git a/src/decorations/decorationpalette.cpp b/src/decorations/decorationpalette.cpp index e69df95b13..cbbefa5465 100644 --- a/src/decorations/decorationpalette.cpp +++ b/src/decorations/decorationpalette.cpp @@ -48,34 +48,34 @@ QColor DecorationPalette::color(KDecoration2::ColorGroup group, KDecoration2::Co using KDecoration2::ColorGroup; using KDecoration2::ColorRole; - if (m_legacyPalette.has_value()) { + if (m_legacyColors.has_value()) { switch (role) { case ColorRole::Frame: switch (group) { case ColorGroup::Active: - return m_legacyPalette->activeFrameColor; + return m_legacyColors->activeFrameColor; case ColorGroup::Inactive: - return m_legacyPalette->inactiveFrameColor; + return m_legacyColors->inactiveFrameColor; default: return QColor(); } case ColorRole::TitleBar: switch (group) { case ColorGroup::Active: - return m_legacyPalette->activeTitleBarColor; + return m_legacyColors->activeTitleBarColor; case ColorGroup::Inactive: - return m_legacyPalette->inactiveTitleBarColor; + return m_legacyColors->inactiveTitleBarColor; default: return QColor(); } case ColorRole::Foreground: switch (group) { case ColorGroup::Active: - return m_legacyPalette->activeForegroundColor; + return m_legacyColors->activeForegroundColor; case ColorGroup::Inactive: - return m_legacyPalette->inactiveForegroundColor; + return m_legacyColors->inactiveForegroundColor; case ColorGroup::Warning: - return m_legacyPalette->warningForegroundColor; + return m_legacyColors->warningForegroundColor; default: return QColor(); } @@ -88,29 +88,29 @@ QColor DecorationPalette::color(KDecoration2::ColorGroup group, KDecoration2::Co case ColorRole::Frame: switch (group) { case ColorGroup::Active: - return m_palette.active.background().color(); + return m_colors.active.background().color(); case ColorGroup::Inactive: - return m_palette.inactive.background().color(); + return m_colors.inactive.background().color(); default: return QColor(); } case ColorRole::TitleBar: switch (group) { case ColorGroup::Active: - return m_palette.active.background().color(); + return m_colors.active.background().color(); case ColorGroup::Inactive: - return m_palette.inactive.background().color(); + return m_colors.inactive.background().color(); default: return QColor(); } case ColorRole::Foreground: switch (group) { case ColorGroup::Active: - return m_palette.active.foreground().color(); + return m_colors.active.foreground().color(); case ColorGroup::Inactive: - return m_palette.inactive.foreground().color(); + return m_colors.inactive.foreground().color(); case ColorGroup::Warning: - return m_palette.inactive.foreground(KColorScheme::ForegroundRole::NegativeText).color(); + return m_colors.inactive.foreground(KColorScheme::ForegroundRole::NegativeText).color(); default: return QColor(); } @@ -121,40 +121,38 @@ QColor DecorationPalette::color(KDecoration2::ColorGroup group, KDecoration2::Co QPalette DecorationPalette::palette() const { - return m_legacyPalette ? m_legacyPalette->palette : m_applicationPalette; + return m_palette; } void DecorationPalette::update() { m_colorSchemeConfig->sync(); - - m_applicationPalette = KColorScheme::createApplicationPalette(m_colorSchemeConfig); + m_palette = KColorScheme::createApplicationPalette(m_colorSchemeConfig); if (KColorScheme::isColorSetSupported(m_colorSchemeConfig, KColorScheme::Header)) { - m_palette.active = KColorScheme(QPalette::Normal, KColorScheme::Header, m_colorSchemeConfig); - m_palette.inactive = KColorScheme(QPalette::Inactive, KColorScheme::Header, m_colorSchemeConfig); - m_legacyPalette.reset(); + m_colors.active = KColorScheme(QPalette::Normal, KColorScheme::Header, m_colorSchemeConfig); + m_colors.inactive = KColorScheme(QPalette::Inactive, KColorScheme::Header, m_colorSchemeConfig); + m_legacyColors.reset(); } else { KConfigGroup wmConfig(m_colorSchemeConfig, QStringLiteral("WM")); if (!wmConfig.exists()) { - m_palette.active = KColorScheme(QPalette::Normal, KColorScheme::Window, m_colorSchemeConfig); - m_palette.inactive = KColorScheme(QPalette::Inactive, KColorScheme::Window, m_colorSchemeConfig); - m_legacyPalette.reset(); + m_colors.active = KColorScheme(QPalette::Normal, KColorScheme::Window, m_colorSchemeConfig); + m_colors.inactive = KColorScheme(QPalette::Inactive, KColorScheme::Window, m_colorSchemeConfig); + m_legacyColors.reset(); return; } - m_legacyPalette = LegacyPalette{}; - m_legacyPalette->palette = KColorScheme::createApplicationPalette(m_colorSchemeConfig); - m_legacyPalette->activeFrameColor = wmConfig.readEntry("frame", m_legacyPalette->palette.color(QPalette::Active, QPalette::Window)); - m_legacyPalette->inactiveFrameColor = wmConfig.readEntry("inactiveFrame", m_legacyPalette->activeFrameColor); - m_legacyPalette->activeTitleBarColor = wmConfig.readEntry("activeBackground", m_legacyPalette->palette.color(QPalette::Active, QPalette::Highlight)); - m_legacyPalette->inactiveTitleBarColor = wmConfig.readEntry("inactiveBackground", m_legacyPalette->inactiveTitleBarColor); - m_legacyPalette->activeForegroundColor = wmConfig.readEntry("activeForeground", m_legacyPalette->palette.color(QPalette::Active, QPalette::HighlightedText)); - m_legacyPalette->inactiveForegroundColor = wmConfig.readEntry("inactiveForeground", m_legacyPalette->activeForegroundColor.darker()); + m_legacyColors = LegacyColors{}; + m_legacyColors->activeFrameColor = wmConfig.readEntry("frame", m_palette.color(QPalette::Active, QPalette::Window)); + m_legacyColors->inactiveFrameColor = wmConfig.readEntry("inactiveFrame", m_legacyColors->activeFrameColor); + m_legacyColors->activeTitleBarColor = wmConfig.readEntry("activeBackground", m_palette.color(QPalette::Active, QPalette::Highlight)); + m_legacyColors->inactiveTitleBarColor = wmConfig.readEntry("inactiveBackground", m_legacyColors->inactiveTitleBarColor); + m_legacyColors->activeForegroundColor = wmConfig.readEntry("activeForeground", m_palette.color(QPalette::Active, QPalette::HighlightedText)); + m_legacyColors->inactiveForegroundColor = wmConfig.readEntry("inactiveForeground", m_legacyColors->activeForegroundColor.darker()); KConfigGroup windowColorsConfig(m_colorSchemeConfig, QStringLiteral("Colors:Window")); - m_legacyPalette->warningForegroundColor = windowColorsConfig.readEntry("ForegroundNegative", QColor(237, 21, 2)); + m_legacyColors->warningForegroundColor = windowColorsConfig.readEntry("ForegroundNegative", QColor(237, 21, 2)); } Q_EMIT changed(); diff --git a/src/decorations/decorationpalette.h b/src/decorations/decorationpalette.h index c6af060777..57ed4b7fc1 100644 --- a/src/decorations/decorationpalette.h +++ b/src/decorations/decorationpalette.h @@ -46,10 +46,8 @@ private: QString m_colorScheme; KConfigWatcher::Ptr m_watcher; - struct LegacyPalette + struct LegacyColors { - QPalette palette; - QColor activeTitleBarColor; QColor inactiveTitleBarColor; @@ -61,17 +59,16 @@ private: QColor warningForegroundColor; }; - struct ModernPalette + struct ModernColors { KColorScheme active; KColorScheme inactive; }; - std::optional m_legacyPalette; KSharedConfig::Ptr m_colorSchemeConfig; - - ModernPalette m_palette; - QPalette m_applicationPalette; + QPalette m_palette; + ModernColors m_colors; + std::optional m_legacyColors; }; }