From c8c3c44064881e277abd58b6ef8f7a31ba9fc03a Mon Sep 17 00:00:00 2001 From: Santhosh Kumar Date: Mon, 26 Jun 2023 11:37:21 +0200 Subject: [PATCH] Fix disabled and inactive group palettes for windows native style MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The dark mode related changes for windows didn't update disabled and inactive group palettes, and this makes text in certain context (such as menu bar) appear with enabled color. The windows theme API populateLightSystemPalette() is responsible for this and it updates palettes for color roles and certain inactive scenarios but not for disabled. This patch set makes existing systemPalette(Qt::ColorScheme) to be available in QWindowsTheme and it updates palette depending on color scheme. From now on, this API updates palettes for windows native style. Its to be noted that window native style use light palette irrespective of color scheme. Fixes: QTBUG-114821 Change-Id: Iff4f35900293b8e7030ec121ca21856daa094dc0 Reviewed-by: Axel Spoerl Reviewed-by: Tor Arne Vestbø (cherry picked from commit 491534006ec0b648e70269d33ccd7b79faddef7f) Reviewed-by: Qt Cherry-pick Bot --- src/gui/kernel/qguiapplication_p.h | 2 +- .../platforms/windows/qwindowsapplication.cpp | 4 +- .../platforms/windows/qwindowsapplication.h | 2 +- .../platforms/windows/qwindowstheme.cpp | 78 ++++++++++--------- src/plugins/platforms/windows/qwindowstheme.h | 2 +- .../windowsvista/qwindowsvistastyle.cpp | 2 +- 6 files changed, 49 insertions(+), 41 deletions(-) diff --git a/src/gui/kernel/qguiapplication_p.h b/src/gui/kernel/qguiapplication_p.h index ec638760586..14a81daed52 100644 --- a/src/gui/kernel/qguiapplication_p.h +++ b/src/gui/kernel/qguiapplication_p.h @@ -420,7 +420,7 @@ struct Q_GUI_EXPORT QWindowsApplication virtual QVariant gpu() const = 0; // internal, used by qtdiag virtual QVariant gpuList() const = 0; - virtual void lightSystemPalette(QPalette &pal) const = 0; + virtual void populateLightSystemPalette(QPalette &pal) const = 0; }; #endif // Q_OS_WIN diff --git a/src/plugins/platforms/windows/qwindowsapplication.cpp b/src/plugins/platforms/windows/qwindowsapplication.cpp index 00477e2890b..60cbf1f7ba0 100644 --- a/src/plugins/platforms/windows/qwindowsapplication.cpp +++ b/src/plugins/platforms/windows/qwindowsapplication.cpp @@ -143,9 +143,9 @@ QVariant QWindowsApplication::gpuList() const return result; } -void QWindowsApplication::lightSystemPalette(QPalette &result) const +void QWindowsApplication::populateLightSystemPalette(QPalette &result) const { - QWindowsTheme::populateLightSystemBasePalette(result); + result = QWindowsTheme::systemPalette(Qt::ColorScheme::Light); } QT_END_NAMESPACE diff --git a/src/plugins/platforms/windows/qwindowsapplication.h b/src/plugins/platforms/windows/qwindowsapplication.h index 8b74b47f3d1..efacd74e184 100644 --- a/src/plugins/platforms/windows/qwindowsapplication.h +++ b/src/plugins/platforms/windows/qwindowsapplication.h @@ -43,7 +43,7 @@ public: QVariant gpu() const override; QVariant gpuList() const override; - void lightSystemPalette(QPalette &palette) const override; + void populateLightSystemPalette(QPalette &palette) const override; private: WindowActivationBehavior m_windowActivationBehavior = DefaultActivateWindow; diff --git a/src/plugins/platforms/windows/qwindowstheme.cpp b/src/plugins/platforms/windows/qwindowstheme.cpp index 2d97b718bab..872c78bb8ef 100644 --- a/src/plugins/platforms/windows/qwindowstheme.cpp +++ b/src/plugins/platforms/windows/qwindowstheme.cpp @@ -241,7 +241,7 @@ static QColor placeHolderColor(QColor textColor) This is used when the theme is light mode, and when the theme is dark but the application doesn't support dark mode. In the latter case, we need to check. */ -void QWindowsTheme::populateLightSystemBasePalette(QPalette &result) +static void populateLightSystemBasePalette(QPalette &result) { const QColor background = getSysColor(COLOR_BTNFACE); const QColor textColor = getSysColor(COLOR_WINDOWTEXT); @@ -354,39 +354,6 @@ static void populateDarkSystemBasePalette(QPalette &result) result.setColor(QPalette::All, QPalette::AccentColor, accent); } -static QPalette systemPalette(bool light) -{ - QPalette result = standardPalette(); - if (light) - QWindowsTheme::populateLightSystemBasePalette(result); - else - populateDarkSystemBasePalette(result); - - if (result.window() != result.base()) { - result.setColor(QPalette::Inactive, QPalette::Highlight, - result.color(QPalette::Inactive, QPalette::Window)); - result.setColor(QPalette::Inactive, QPalette::HighlightedText, - result.color(QPalette::Inactive, QPalette::Text)); - result.setColor(QPalette::Inactive, QPalette::AccentColor, - result.color(QPalette::Inactive, QPalette::Window)); - } - - const QColor disabled = mixColors(result.windowText().color(), result.button().color()); - - result.setColorGroup(QPalette::Disabled, result.windowText(), result.button(), - result.light(), result.dark(), result.mid(), - result.text(), result.brightText(), result.base(), - result.window()); - result.setColor(QPalette::Disabled, QPalette::WindowText, disabled); - result.setColor(QPalette::Disabled, QPalette::Text, disabled); - result.setColor(QPalette::Disabled, QPalette::ButtonText, disabled); - result.setColor(QPalette::Disabled, QPalette::Highlight, result.color(QPalette::Highlight)); - result.setColor(QPalette::Disabled, QPalette::HighlightedText, result.color(QPalette::HighlightedText)); - result.setColor(QPalette::Disabled, QPalette::AccentColor, disabled); - result.setColor(QPalette::Disabled, QPalette::Base, result.window().color()); - return result; -} - static inline QPalette toolTipPalette(const QPalette &systemPalette, bool light) { QPalette result(systemPalette); @@ -579,7 +546,7 @@ void QWindowsTheme::refreshPalettes() !QWindowsContext::isDarkMode() || !QWindowsIntegration::instance()->darkModeHandling().testFlag(QWindowsApplication::DarkModeStyle); clearPalettes(); - m_palettes[SystemPalette] = new QPalette(systemPalette(light)); + m_palettes[SystemPalette] = new QPalette(QWindowsTheme::systemPalette(light ? Qt::ColorScheme::Light : Qt::ColorScheme::Dark)); m_palettes[ToolTipPalette] = new QPalette(toolTipPalette(*m_palettes[SystemPalette], light)); m_palettes[MenuPalette] = new QPalette(menuPalette(*m_palettes[SystemPalette], light)); m_palettes[MenuBarPalette] = menuBarPalette(*m_palettes[MenuPalette], light); @@ -603,6 +570,47 @@ void QWindowsTheme::refreshPalettes() } } +QPalette QWindowsTheme::systemPalette(Qt::ColorScheme colorScheme) +{ + QPalette result = standardPalette(); + + switch (colorScheme) { + case Qt::ColorScheme::Light: + populateLightSystemBasePalette(result); + break; + case Qt::ColorScheme::Dark: + populateDarkSystemBasePalette(result); + break; + default: + qFatal("Unknown color scheme"); + break; + } + + if (result.window() != result.base()) { + result.setColor(QPalette::Inactive, QPalette::Highlight, + result.color(QPalette::Inactive, QPalette::Window)); + result.setColor(QPalette::Inactive, QPalette::HighlightedText, + result.color(QPalette::Inactive, QPalette::Text)); + result.setColor(QPalette::Inactive, QPalette::AccentColor, + result.color(QPalette::Inactive, QPalette::Window)); + } + + const QColor disabled = mixColors(result.windowText().color(), result.button().color()); + + result.setColorGroup(QPalette::Disabled, result.windowText(), result.button(), + result.light(), result.dark(), result.mid(), + result.text(), result.brightText(), result.base(), + result.window()); + result.setColor(QPalette::Disabled, QPalette::WindowText, disabled); + result.setColor(QPalette::Disabled, QPalette::Text, disabled); + result.setColor(QPalette::Disabled, QPalette::ButtonText, disabled); + result.setColor(QPalette::Disabled, QPalette::Highlight, result.color(QPalette::Highlight)); + result.setColor(QPalette::Disabled, QPalette::HighlightedText, result.color(QPalette::HighlightedText)); + result.setColor(QPalette::Disabled, QPalette::AccentColor, disabled); + result.setColor(QPalette::Disabled, QPalette::Base, result.window().color()); + return result; +} + void QWindowsTheme::clearFonts() { qDeleteAll(m_fonts, m_fonts + NFonts); diff --git a/src/plugins/platforms/windows/qwindowstheme.h b/src/plugins/platforms/windows/qwindowstheme.h index 7f320da9675..6a44db8aa76 100644 --- a/src/plugins/platforms/windows/qwindowstheme.h +++ b/src/plugins/platforms/windows/qwindowstheme.h @@ -61,7 +61,7 @@ public: static const char *name; - static void populateLightSystemBasePalette(QPalette &result); + static QPalette systemPalette(Qt::ColorScheme); private: void clearPalettes(); diff --git a/src/plugins/styles/windowsvista/qwindowsvistastyle.cpp b/src/plugins/styles/windowsvista/qwindowsvistastyle.cpp index 14ef72ad565..4f7786c0346 100644 --- a/src/plugins/styles/windowsvista/qwindowsvistastyle.cpp +++ b/src/plugins/styles/windowsvista/qwindowsvistastyle.cpp @@ -4815,7 +4815,7 @@ void QWindowsVistaStyle::polish(QPalette &pal) // Overwrite with the light system palette. using QWindowsApplication = QNativeInterface::Private::QWindowsApplication; if (auto nativeWindowsApp = dynamic_cast(QGuiApplicationPrivate::platformIntegration())) - nativeWindowsApp->lightSystemPalette(pal); + nativeWindowsApp->populateLightSystemPalette(pal); } QPixmapCache::clear();