From bc3e6af3cee79019ddc548eaa6b5172a5fb93d95 Mon Sep 17 00:00:00 2001 From: Volker Hilsheimer Date: Thu, 14 Jul 2022 17:00:16 +0200 Subject: [PATCH] Windows: better handling of darkmode support 43ef22045c6f4fbf76d5cfa4ca32160d919b9984 turned dark mode support on for both styling and window frames. However, the default palette and style support in Qt is too incomplete, resulting in unreadable UIs when using certain styles (e.g. fusion). Also the vista style is not supporting dark mode. If we don't turn on dark style support, then dark frame support doesn't look good either. However, many application developers have implement a dark theme themselves, and we should have a dark frame for those applications. So partially revert 43ef22045c6f4fbf76d5cfa4ca32160d919b9984 so that dark style support is disabled by default, and leave dark frame support on. However, only activate dark frames if the palette is dark, i.e. if the window background color in the default palette is darker than the text color (or if DarkModeStyle is explicitly turned on by running the application with -platform windows:darkmode=2). This way, dark-themed applications get a dark frame on dark Windows, and a light frame on light Windows; and light-themed applications (including default Qt applications) get a light frame all the time. Fixes: QTBUG-72028 Change-Id: I61f1b1e43b2a4ba69848d5d8bec921c0790fe511 Reviewed-by: Marius Kittler Reviewed-by: Oliver Wolff (cherry picked from commit 5ea7e3a8111b2939f0c91b750aa1c62ab16ab715) Reviewed-by: Qt Cherry-pick Bot --- src/plugins/platforms/windows/qwindowscontext.cpp | 8 ++++---- src/plugins/platforms/windows/qwindowsintegration.cpp | 3 +-- src/plugins/platforms/windows/qwindowswindow.cpp | 10 +++++++++- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/plugins/platforms/windows/qwindowscontext.cpp b/src/plugins/platforms/windows/qwindowscontext.cpp index 9397c0213e8..8f1198190fa 100644 --- a/src/plugins/platforms/windows/qwindowscontext.cpp +++ b/src/plugins/platforms/windows/qwindowscontext.cpp @@ -1101,14 +1101,14 @@ bool QWindowsContext::windowsProc(HWND hwnd, UINT message, if (darkMode != QWindowsContextPrivate::m_darkMode) { QWindowsContextPrivate::m_darkMode = darkMode; auto integration = QWindowsIntegration::instance(); - if (integration->darkModeHandling().testFlag(QWindowsApplication::DarkModeWindowFrames)) { - for (QWindowsWindow *w : d->m_windows) - w->setDarkBorder(QWindowsContextPrivate::m_darkMode); - } if (integration->darkModeHandling().testFlag(QWindowsApplication::DarkModeStyle)) { QWindowsTheme::instance()->refresh(); QWindowSystemInterface::handleThemeChange(); } + if (integration->darkModeHandling().testFlag(QWindowsApplication::DarkModeWindowFrames)) { + for (QWindowsWindow *w : d->m_windows) + w->setDarkBorder(QWindowsContextPrivate::m_darkMode); + } } } return d->m_screenManager.handleScreenChanges(); diff --git a/src/plugins/platforms/windows/qwindowsintegration.cpp b/src/plugins/platforms/windows/qwindowsintegration.cpp index 40b264d1864..9e284f23663 100644 --- a/src/plugins/platforms/windows/qwindowsintegration.cpp +++ b/src/plugins/platforms/windows/qwindowsintegration.cpp @@ -212,8 +212,7 @@ void QWindowsIntegrationPrivate::parseOptions(QWindowsIntegration *q, const QStr QtWindows::ProcessDpiAwareness dpiAwareness = QtWindows::ProcessPerMonitorV2DpiAware; int tabletAbsoluteRange = -1; - DarkModeHandling darkModeHandling = DarkModeHandlingFlag::DarkModeWindowFrames - | DarkModeHandlingFlag::DarkModeStyle; + DarkModeHandling darkModeHandling = DarkModeHandlingFlag::DarkModeWindowFrames; m_options = ::parseOptions(paramList, &tabletAbsoluteRange, &dpiAwareness, &darkModeHandling); q->setDarkModeHandling(darkModeHandling); QWindowsFontDatabase::setFontOptions(m_options); diff --git a/src/plugins/platforms/windows/qwindowswindow.cpp b/src/plugins/platforms/windows/qwindowswindow.cpp index 929e468115e..9a9ba369106 100644 --- a/src/plugins/platforms/windows/qwindowswindow.cpp +++ b/src/plugins/platforms/windows/qwindowswindow.cpp @@ -848,7 +848,15 @@ void WindowCreationData::fromWindow(const QWindow *w, const Qt::WindowFlags flag static inline bool shouldApplyDarkFrame(const QWindow *w) { - return w->isTopLevel() && !w->flags().testFlag(Qt::FramelessWindowHint); + if (!w->isTopLevel() || w->flags().testFlag(Qt::FramelessWindowHint)) + return false; + if (QWindowsIntegration::instance()->darkModeHandling().testFlag(QWindowsApplication::DarkModeStyle)) + return true; + // if the application supports a dark border, and the palette is dark (window background color + // is darker than the text), then turn dark-border support on, otherwise use a light border. + const QPalette defaultPalette; + return defaultPalette.color(QPalette::WindowText).lightness() + > defaultPalette.color(QPalette::Window).lightness(); } QWindowsWindowData