From db6b607cae07d8241e006edffd541caced38c4c2 Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Mon, 20 Feb 2023 08:04:19 +0100 Subject: [PATCH] Windows QPA plugin: Move palette helper function to qwindowstheme.h This avoids the duplication of helper functions. Amends a2518b4140ed88a674bf4a4fcf4576e35c698bb9. Task-number: QTBUG-109394 Change-Id: If969a6497aed3b3662621cf723772c87eb66fd23 Reviewed-by: Volker Hilsheimer (cherry picked from commit 9ac0742d3699bdce7015abeecec8aa23abea47d6) Reviewed-by: Qt Cherry-pick Bot --- .../platforms/windows/qwindowsapplication.cpp | 66 +------------------ .../platforms/windows/qwindowstheme.cpp | 47 +++++++++++-- src/plugins/platforms/windows/qwindowstheme.h | 2 + 3 files changed, 46 insertions(+), 69 deletions(-) diff --git a/src/plugins/platforms/windows/qwindowsapplication.cpp b/src/plugins/platforms/windows/qwindowsapplication.cpp index f823a69552d..00477e2890b 100644 --- a/src/plugins/platforms/windows/qwindowsapplication.cpp +++ b/src/plugins/platforms/windows/qwindowsapplication.cpp @@ -9,17 +9,13 @@ #include "qwindowsopengltester.h" #include "qwindowswindow.h" #include "qwindowsintegration.h" +#include "qwindowstheme.h" #include #include #include -#if QT_CONFIG(cpp_winrt) -# include -# include -#endif // QT_CONFIG(cpp_winrt) - QT_BEGIN_NAMESPACE void QWindowsApplication::setTouchWindowTouchType(QWindowsApplication::TouchWindowTouchTypes type) @@ -147,67 +143,9 @@ QVariant QWindowsApplication::gpuList() const return result; } -static inline QColor getSysColor(int index) -{ - COLORREF cr = GetSysColor(index); - return QColor(GetRValue(cr), GetGValue(cr), GetBValue(cr)); -} - -#if QT_CONFIG(cpp_winrt) -static constexpr QColor getSysColor(winrt::Windows::UI::Color &&color) -{ - return QColor(color.R, color.G, color.B, color.A); -} -#endif - -static inline QColor placeHolderColor(QColor textColor) -{ - textColor.setAlpha(128); - return textColor; -} - void QWindowsApplication::lightSystemPalette(QPalette &result) const { - QColor background = getSysColor(COLOR_BTNFACE); - QColor textColor = getSysColor(COLOR_WINDOWTEXT); - QColor accent = getSysColor(COLOR_HIGHLIGHT); - -#if QT_CONFIG(cpp_winrt) - // respect the Windows 11 accent color - using namespace winrt::Windows::UI::ViewManagement; - const auto settings = UISettings(); - - accent = getSysColor(settings.GetColorValue(UIColorType::Accent)); -#endif - - const QColor btnFace = background; - const QColor btnHighlight = getSysColor(COLOR_BTNHIGHLIGHT); - - result.setColor(QPalette::Highlight, accent); - result.setColor(QPalette::WindowText, getSysColor(COLOR_WINDOWTEXT)); - result.setColor(QPalette::Button, btnFace); - result.setColor(QPalette::Light, btnHighlight); - result.setColor(QPalette::Dark, getSysColor(COLOR_BTNSHADOW)); - result.setColor(QPalette::Mid, result.button().color().darker(150)); - result.setColor(QPalette::Text, textColor); - result.setColor(QPalette::PlaceholderText, placeHolderColor(textColor)); - result.setColor(QPalette::BrightText, btnHighlight); - result.setColor(QPalette::Base, getSysColor(COLOR_WINDOW)); - result.setColor(QPalette::Window, btnFace); - result.setColor(QPalette::ButtonText, getSysColor(COLOR_BTNTEXT)); - result.setColor(QPalette::Midlight, getSysColor(COLOR_3DLIGHT)); - result.setColor(QPalette::Shadow, getSysColor(COLOR_3DDKSHADOW)); - result.setColor(QPalette::HighlightedText, getSysColor(COLOR_HIGHLIGHTTEXT)); - - result.setColor(QPalette::Link, Qt::blue); - result.setColor(QPalette::LinkVisited, Qt::magenta); - result.setColor(QPalette::Inactive, QPalette::Button, result.button().color()); - result.setColor(QPalette::Inactive, QPalette::Window, result.window().color()); - result.setColor(QPalette::Inactive, QPalette::Light, result.light().color()); - result.setColor(QPalette::Inactive, QPalette::Dark, result.dark().color()); - - if (result.midlight() == result.button()) - result.setColor(QPalette::Midlight, result.button().color().lighter(110)); + QWindowsTheme::populateLightSystemBasePalette(result); } QT_END_NAMESPACE diff --git a/src/plugins/platforms/windows/qwindowstheme.cpp b/src/plugins/platforms/windows/qwindowstheme.cpp index 29c4c47c36e..6fba9e55c13 100644 --- a/src/plugins/platforms/windows/qwindowstheme.cpp +++ b/src/plugins/platforms/windows/qwindowstheme.cpp @@ -225,11 +225,48 @@ 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. */ -static void populateLightSystemBasePalette(QPalette &result) +void QWindowsTheme::populateLightSystemBasePalette(QPalette &result) { - using QWindowsApplication = QNativeInterface::Private::QWindowsApplication; - if (auto nativeWindowsApp = dynamic_cast(QGuiApplicationPrivate::platformIntegration())) - nativeWindowsApp->lightSystemPalette(result); + QColor background = getSysColor(COLOR_BTNFACE); + QColor textColor = getSysColor(COLOR_WINDOWTEXT); + QColor accent = getSysColor(COLOR_HIGHLIGHT); + +#if QT_CONFIG(cpp_winrt) + // respect the Windows 11 accent color + using namespace winrt::Windows::UI::ViewManagement; + const auto settings = UISettings(); + + accent = getSysColor(settings.GetColorValue(UIColorType::Accent)); +#endif + + const QColor btnFace = background; + const QColor btnHighlight = getSysColor(COLOR_BTNHIGHLIGHT); + + result.setColor(QPalette::Highlight, accent); + result.setColor(QPalette::WindowText, getSysColor(COLOR_WINDOWTEXT)); + result.setColor(QPalette::Button, btnFace); + result.setColor(QPalette::Light, btnHighlight); + result.setColor(QPalette::Dark, getSysColor(COLOR_BTNSHADOW)); + result.setColor(QPalette::Mid, result.button().color().darker(150)); + result.setColor(QPalette::Text, textColor); + result.setColor(QPalette::PlaceholderText, placeHolderColor(textColor)); + result.setColor(QPalette::BrightText, btnHighlight); + result.setColor(QPalette::Base, getSysColor(COLOR_WINDOW)); + result.setColor(QPalette::Window, btnFace); + result.setColor(QPalette::ButtonText, getSysColor(COLOR_BTNTEXT)); + result.setColor(QPalette::Midlight, getSysColor(COLOR_3DLIGHT)); + result.setColor(QPalette::Shadow, getSysColor(COLOR_3DDKSHADOW)); + result.setColor(QPalette::HighlightedText, getSysColor(COLOR_HIGHLIGHTTEXT)); + + result.setColor(QPalette::Link, Qt::blue); + result.setColor(QPalette::LinkVisited, Qt::magenta); + result.setColor(QPalette::Inactive, QPalette::Button, result.button().color()); + result.setColor(QPalette::Inactive, QPalette::Window, result.window().color()); + result.setColor(QPalette::Inactive, QPalette::Light, result.light().color()); + result.setColor(QPalette::Inactive, QPalette::Dark, result.dark().color()); + + if (result.midlight() == result.button()) + result.setColor(QPalette::Midlight, result.button().color().lighter(110)); } static void populateDarkSystemBasePalette(QPalette &result) @@ -300,7 +337,7 @@ static QPalette systemPalette(bool light) { QPalette result = standardPalette(); if (light) - populateLightSystemBasePalette(result); + QWindowsTheme::populateLightSystemBasePalette(result); else populateDarkSystemBasePalette(result); diff --git a/src/plugins/platforms/windows/qwindowstheme.h b/src/plugins/platforms/windows/qwindowstheme.h index 18b87e40727..7f320da9675 100644 --- a/src/plugins/platforms/windows/qwindowstheme.h +++ b/src/plugins/platforms/windows/qwindowstheme.h @@ -61,6 +61,8 @@ public: static const char *name; + static void populateLightSystemBasePalette(QPalette &result); + private: void clearPalettes(); void refreshPalettes();