From abbd9c91f5309924ac707706e3483747f44149a0 Mon Sep 17 00:00:00 2001 From: Doris Verria Date: Tue, 4 Oct 2022 14:21:03 +0200 Subject: [PATCH] QIOSTheme: Set appearance based on UIScreen if no windows created yet MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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ø --- src/plugins/platforms/ios/qiosscreen.mm | 11 ++++++++++- src/plugins/platforms/ios/qiostheme.mm | 14 +++++++++----- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/src/plugins/platforms/ios/qiosscreen.mm b/src/plugins/platforms/ios/qiosscreen.mm index ce0dedc88d..beaaa3fbfc 100644 --- a/src/plugins/platforms/ios/qiosscreen.mm +++ b/src/plugins/platforms/ios/qiosscreen.mm @@ -15,6 +15,7 @@ #include #include +#include #include #include @@ -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(); } diff --git a/src/plugins/platforms/ios/qiostheme.mm b/src/plugins/platforms/ios/qiostheme.mm index 62ba4d669b..408c59b2cb 100644 --- a/src/plugins/platforms/ios/qiostheme.mm +++ b/src/plugins/platforms/ios/qiostheme.mm @@ -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