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 <Friedemann.Kleint@qt.io>
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
This commit is contained in:
Tor Arne Vestbø 2022-04-18 16:32:28 +02:00
parent dc4bc1e575
commit e3941facca
6 changed files with 10 additions and 10 deletions

View File

@ -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()

View File

@ -257,7 +257,7 @@ public:
static void handleScreenRefreshRateChange(QScreen *screen, qreal newRefreshRate);
template<typename Delivery = QWindowSystemInterface::DefaultDelivery>
static void handleThemeChange(QWindow *window);
static void handleThemeChange(QWindow *window = nullptr);
static void handleFileOpenEvent(const QString& fileName);
static void handleFileOpenEvent(const QUrl &url);

View File

@ -245,7 +245,7 @@ void QCoreTextFontDatabase::invalidate()
qDeleteAll(m_themeFonts);
m_themeFonts.clear();
QWindowSystemInterface::handleThemeChange<QWindowSystemInterface::SynchronousDelivery>(nullptr);
QWindowSystemInterface::handleThemeChange<QWindowSystemInterface::SynchronousDelivery>();
}
struct FontDescription {

View File

@ -318,7 +318,7 @@ void QCocoaTheme::handleSystemThemeChange()
QFontCache::instance()->clear();
}
QWindowSystemInterface::handleThemeChange<QWindowSystemInterface::SynchronousDelivery>(nullptr);
QWindowSystemInterface::handleThemeChange<QWindowSystemInterface::SynchronousDelivery>();
}
bool QCocoaTheme::usePlatformNativeDialog(DialogType dialogType) const

View File

@ -217,7 +217,7 @@ static QIOSScreen* qtPlatformScreenFor(UIScreen *uiScreen)
if (self.screen == UIScreen.mainScreen) {
if (previousTraitCollection.userInterfaceStyle != self.traitCollection.userInterfaceStyle) {
QIOSTheme::initializeSystemPalette();
QWindowSystemInterface::handleThemeChange<QWindowSystemInterface::SynchronousDelivery>(nullptr);
QWindowSystemInterface::handleThemeChange<QWindowSystemInterface::SynchronousDelivery>();
}
}
}

View File

@ -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();