QIOSTheme: Set appearance based on UIScreen if no windows created yet

Set the theme's appearance based on the UIScreen's, which follows the
app's or system's configuration, if no UIWindows are created yet.

The UIWindow and UIScreen will have the same appearance (the
traitCollection change is propagated hierarchically from screen to
window) unless UIWindow's overrideUserStyleInterface is explicitly
set to differ from that of the screen. In that case,
traitCollectionDidChange will be called for the window (but not the
screen) and when we update the palette calling the UIColor API, the
colors will be resolved based on
UITraitCollection.currentTraitCollection property, which follows the
window's appearance. That is why we need to set the theme's appearance
based on the window's.

Change-Id: I43207f351559fb82efc2f281eafb3cd1c96bbf75
Reviewed-by: Tor Arne Vestbø <tor.arne.vestbo@qt.io>
bb10
Doris Verria 2022-10-04 14:21:03 +02:00
parent 9e7f91781d
commit abbd9c91f5
2 changed files with 19 additions and 6 deletions

View File

@ -15,6 +15,7 @@
#include <QtGui/qpointingdevice.h>
#include <QtGui/private/qwindow_p.h>
#include <QtGui/private/qguiapplication_p.h>
#include <private/qcoregraphics_p.h>
#include <qpa/qwindowsysteminterface.h>
@ -177,8 +178,16 @@ static QIOSScreen* qtPlatformScreenFor(UIScreen *uiScreen)
{
[super traitCollectionDidChange:previousTraitCollection];
Qt::Appearance appearance = self.traitCollection.userInterfaceStyle
== UIUserInterfaceStyleDark
? Qt::Appearance::Dark
: Qt::Appearance::Light;
if (self.screen == UIScreen.mainScreen) {
if (previousTraitCollection.userInterfaceStyle != self.traitCollection.userInterfaceStyle) {
// Check if the current userInterfaceStyle reports a different appearance than
// the platformTheme's appearance. We might have set that one based on the UIScreen
if (previousTraitCollection.userInterfaceStyle != self.traitCollection.userInterfaceStyle
|| QGuiApplicationPrivate::platformTheme()->appearance() != appearance) {
QIOSTheme::initializeSystemPalette();
QWindowSystemInterface::handleThemeChange<QWindowSystemInterface::SynchronousDelivery>();
}

View File

@ -134,13 +134,17 @@ QVariant QIOSTheme::themeHint(ThemeHint hint) const
Qt::Appearance QIOSTheme::appearance() const
{
UIUserInterfaceStyle appearance = UIUserInterfaceStyleUnspecified;
// Set the appearance based on the UIWindow
// Fallback to the UIScreen if no window is created yet
if (UIWindow *window = qt_apple_sharedApplication().windows.lastObject) {
return window.traitCollection.userInterfaceStyle
== UIUserInterfaceStyleDark
? Qt::Appearance::Dark
: Qt::Appearance::Light;
appearance = window.traitCollection.userInterfaceStyle;
} else {
appearance = UIScreen.mainScreen.traitCollection.userInterfaceStyle;
}
return Qt::Appearance::Unknown;
return appearance == UIUserInterfaceStyleDark
? Qt::Appearance::Dark
: Qt::Appearance::Light;
}
const QFont *QIOSTheme::font(Font type) const