Overwrite dark system palette in Vista style

The Vista style uses system assets for controls, and those are never
dark. Because of that we cannot support dark appearance with that style,
and applications using the Vista style should always use the light
system palette. Override QStyle::polish(QPalette &) in the vista style
to do that.

To make that palette available, move the code reading the light palette
into QWindowsApplication, and call that method from both the platform
theme (if the system is running in light mode) and from the Vista style
(only if the system is running in dark mode).

If the system is dark mode and another style is used (e.g. Fusion, which
works well with a dark palette), then the palette returned by the
platform theme gets used without any modifications.

This requires duplicating some small inline helper functions in
QWindowsTheme and QWindowsApplication. We can clean this up once the
implementation is complete for both Qt Widgets and Qt Quick.

Change-Id: Ia13f59a2d8414642603f9708926718daf9e8954d
Reviewed-by: Tor Arne Vestbø <tor.arne.vestbo@qt.io>
Reviewed-by: Santhosh Kumar <santhosh.kumar.selvaraj@qt.io>
This commit is contained in:
Volker Hilsheimer 2022-12-07 17:37:09 +01:00
parent c62c2aac75
commit a2518b4140
5 changed files with 101 additions and 54 deletions

View File

@ -396,6 +396,8 @@ 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;
};
#endif // Q_OS_WIN

View File

@ -8,8 +8,25 @@
#include "qwin10helpers.h"
#include "qwindowsopengltester.h"
#include "qwindowswindow.h"
#include "qwindowsintegration.h"
#include <QtCore/QVariant>
#include <QtCore/qvariant.h>
#include <QtCore/private/qfunctions_win_p.h>
#include <QtGui/qpalette.h>
#if QT_CONFIG(cpp_winrt)
# include <winrt/base.h>
# include <QtCore/private/qfactorycacheregistration_p.h>
// Workaround for Windows SDK bug.
// See https://github.com/microsoft/Windows.UI.Composition-Win32-Samples/issues/47
namespace winrt::impl
{
template <typename Async>
auto wait_for(Async const& async, Windows::Foundation::TimeSpan const& timeout);
}
# include <winrt/Windows.UI.ViewManagement.h>
#endif // QT_CONFIG(cpp_winrt)
QT_BEGIN_NAMESPACE
@ -138,4 +155,67 @@ 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));
}
QT_END_NAMESPACE

View File

@ -43,6 +43,8 @@ public:
QVariant gpu() const override;
QVariant gpuList() const override;
void lightSystemPalette(QPalette &palette) const override;
private:
WindowActivationBehavior m_windowActivationBehavior = DefaultActivateWindow;
TouchWindowTouchTypes m_touchWindowTouchTypes = NormalTouch;

View File

@ -63,11 +63,6 @@ QT_BEGIN_NAMESPACE
using namespace Qt::StringLiterals;
static inline QColor COLORREFToQColor(COLORREF cr)
{
return QColor(GetRValue(cr), GetGValue(cr), GetBValue(cr));
}
static inline bool booleanSystemParametersInfo(UINT what, bool defaultValue)
{
BOOL result;
@ -93,7 +88,8 @@ static inline QColor mixColors(const QColor &c1, const QColor &c2)
static inline QColor getSysColor(int index)
{
return COLORREFToQColor(GetSysColor(index));
COLORREF cr = GetSysColor(index);
return QColor(GetRValue(cr), GetGValue(cr), GetBValue(cr));
}
#if QT_CONFIG(cpp_winrt)
@ -238,49 +234,9 @@ static QColor placeHolderColor(QColor textColor)
*/
static void populateLightSystemBasePalette(QPalette &result)
{
QColor background = getSysColor(COLOR_BTNFACE);
QColor textColor = getSysColor(COLOR_WINDOWTEXT);
QColor accent = getSysColor(COLOR_HIGHLIGHT);
#if QT_CONFIG(cpp_winrt)
if (QWindowsIntegration::instance()->darkModeHandling().testFlag(QWindowsApplication::DarkModeStyle)) {
using namespace winrt::Windows::UI::ViewManagement;
const auto settings = UISettings();
background = getSysColor(settings.GetColorValue(UIColorType::Background));
textColor = getSysColor(settings.GetColorValue(UIColorType::Foreground));
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));
using QWindowsApplication = QNativeInterface::Private::QWindowsApplication;
if (auto nativeWindowsApp = dynamic_cast<QWindowsApplication *>(QGuiApplicationPrivate::platformIntegration()))
nativeWindowsApp->lightSystemPalette(result);
}
static void populateDarkSystemBasePalette(QPalette &result)

View File

@ -6,6 +6,7 @@
#include "qwindowsvistaanimation_p.h"
#include <qoperatingsystemversion.h>
#include <qscreen.h>
#include <qstylehints.h>
#include <qwindow.h>
#include <private/qstyleanimation_p.h>
#include <private/qstylehelper_p.h>
@ -172,10 +173,8 @@ static QRegion scaleRegion(const QRegion &region, qreal factor)
*/
bool QWindowsVistaStylePrivate::useVista(bool update)
{
if (update) {
useVistaTheme = IsThemeActive() && (IsAppThemed() || !QCoreApplication::instance())
&& !QWindowsStylePrivate::isDarkMode();
}
if (update)
useVistaTheme = IsThemeActive() && (IsAppThemed() || !QCoreApplication::instance());
return useVistaTheme;
}
@ -4791,6 +4790,14 @@ void QWindowsVistaStyle::polish(QPalette &pal)
{
Q_D(QWindowsVistaStyle);
if (qApp->styleHints()->appearance() == Qt::Appearance::Dark) {
// System runs in dark mode, but the Vista style cannot use a dark palette.
// Overwrite with the light system palette.
using QWindowsApplication = QNativeInterface::Private::QWindowsApplication;
if (auto nativeWindowsApp = dynamic_cast<QWindowsApplication *>(QGuiApplicationPrivate::platformIntegration()))
nativeWindowsApp->lightSystemPalette(pal);
}
QPixmapCache::clear();
d->alphaCache.clear();
d->hasInitColors = false;