From e3941facca2e365bb2eb961edd7085d081c64220 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tor=20Arne=20Vestb=C3=B8?= Date: Mon, 18 Apr 2022 16:32:28 +0200 Subject: [PATCH] Send ThemeChange event to all windows when system theme changes The QWSI event for theme change has an optional window parameter to specify the window affected, but most platform react to global theme changes, and end up passing nullptr into the event. The reasonable thing to do in QGuiApplication in that case is send a theme change event to every QWindow, so that they are all notified about the situation. This approach is what the Windows platform plugin was doing already, but did so by iterating manually over the windows, resulting in multiple calls to QGuiApplicationPrivate::handleThemeChanged -- one for each QWSI event. Change-Id: Ifb27b6c31231377c0df389a592cafd0075d3d8bb Reviewed-by: Friedemann Kleint Reviewed-by: Volker Hilsheimer --- src/gui/kernel/qguiapplication.cpp | 9 +++++---- src/gui/kernel/qwindowsysteminterface.h | 2 +- src/gui/text/coretext/qcoretextfontdatabase.mm | 2 +- src/plugins/platforms/cocoa/qcocoatheme.mm | 2 +- src/plugins/platforms/ios/qiosscreen.mm | 2 +- src/plugins/platforms/windows/qwindowscontext.cpp | 3 +-- 6 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/gui/kernel/qguiapplication.cpp b/src/gui/kernel/qguiapplication.cpp index 2ccaf2565b..1f53aa2593 100644 --- a/src/gui/kernel/qguiapplication.cpp +++ b/src/gui/kernel/qguiapplication.cpp @@ -2586,10 +2586,11 @@ void QGuiApplicationPrivate::processThemeChanged(QWindowSystemInterfacePrivate:: { if (self) self->handleThemeChanged(); - if (QWindow *window = tce->window.data()) { - QEvent e(QEvent::ThemeChange); - QGuiApplication::sendSpontaneousEvent(window, &e); - } + + QEvent themeChangeEvent(QEvent::ThemeChange); + const QWindowList windows = tce->window ? QWindowList{tce->window} : window_list; + for (auto *window : windows) + QGuiApplication::sendSpontaneousEvent(window, &themeChangeEvent); } void QGuiApplicationPrivate::handleThemeChanged() diff --git a/src/gui/kernel/qwindowsysteminterface.h b/src/gui/kernel/qwindowsysteminterface.h index cb368d489a..b5a6c45381 100644 --- a/src/gui/kernel/qwindowsysteminterface.h +++ b/src/gui/kernel/qwindowsysteminterface.h @@ -257,7 +257,7 @@ public: static void handleScreenRefreshRateChange(QScreen *screen, qreal newRefreshRate); template - static void handleThemeChange(QWindow *window); + static void handleThemeChange(QWindow *window = nullptr); static void handleFileOpenEvent(const QString& fileName); static void handleFileOpenEvent(const QUrl &url); diff --git a/src/gui/text/coretext/qcoretextfontdatabase.mm b/src/gui/text/coretext/qcoretextfontdatabase.mm index b64a8aefc0..a47f7a64f5 100644 --- a/src/gui/text/coretext/qcoretextfontdatabase.mm +++ b/src/gui/text/coretext/qcoretextfontdatabase.mm @@ -245,7 +245,7 @@ void QCoreTextFontDatabase::invalidate() qDeleteAll(m_themeFonts); m_themeFonts.clear(); - QWindowSystemInterface::handleThemeChange(nullptr); + QWindowSystemInterface::handleThemeChange(); } struct FontDescription { diff --git a/src/plugins/platforms/cocoa/qcocoatheme.mm b/src/plugins/platforms/cocoa/qcocoatheme.mm index 3d329f312e..15004a0b05 100644 --- a/src/plugins/platforms/cocoa/qcocoatheme.mm +++ b/src/plugins/platforms/cocoa/qcocoatheme.mm @@ -318,7 +318,7 @@ void QCocoaTheme::handleSystemThemeChange() QFontCache::instance()->clear(); } - QWindowSystemInterface::handleThemeChange(nullptr); + QWindowSystemInterface::handleThemeChange(); } bool QCocoaTheme::usePlatformNativeDialog(DialogType dialogType) const diff --git a/src/plugins/platforms/ios/qiosscreen.mm b/src/plugins/platforms/ios/qiosscreen.mm index 2a9968c841..69ec393b12 100644 --- a/src/plugins/platforms/ios/qiosscreen.mm +++ b/src/plugins/platforms/ios/qiosscreen.mm @@ -217,7 +217,7 @@ static QIOSScreen* qtPlatformScreenFor(UIScreen *uiScreen) if (self.screen == UIScreen.mainScreen) { if (previousTraitCollection.userInterfaceStyle != self.traitCollection.userInterfaceStyle) { QIOSTheme::initializeSystemPalette(); - QWindowSystemInterface::handleThemeChange(nullptr); + QWindowSystemInterface::handleThemeChange(); } } } diff --git a/src/plugins/platforms/windows/qwindowscontext.cpp b/src/plugins/platforms/windows/qwindowscontext.cpp index fe5b017b27..f32b82bb11 100644 --- a/src/plugins/platforms/windows/qwindowscontext.cpp +++ b/src/plugins/platforms/windows/qwindowscontext.cpp @@ -1164,8 +1164,7 @@ bool QWindowsContext::windowsProc(HWND hwnd, UINT message, } if (integration->darkModeHandling().testFlag(QWindowsApplication::DarkModeStyle)) { QWindowsTheme::instance()->refresh(); - for (QWindowsWindow *w : d->m_windows) - QWindowSystemInterface::handleThemeChange(w->window()); + QWindowSystemInterface::handleThemeChange(); } } return d->m_screenManager.handleScreenChanges();