iOS: Support message box at startup without relying on screen's window

Task-number: QTBUG-121781
Change-Id: Iafc911dad6c668799383f423e38ab389c29688b4
Reviewed-by: Doris Verria <doris.verria@qt.io>
This commit is contained in:
Tor Arne Vestbø 2024-04-05 16:28:31 +02:00
parent e48a9aca5d
commit eb9923c67e
2 changed files with 17 additions and 20 deletions

View File

@ -88,10 +88,15 @@ int infoPlistValue(NSString* key, int defaultValue)
UIWindow *presentationWindow(QWindow *window) UIWindow *presentationWindow(QWindow *window)
{ {
UIWindow *uiWindow = window ? reinterpret_cast<UIView *>(window->winId()).window : nullptr; UIWindow *uiWindow = window ? reinterpret_cast<UIView *>(window->winId()).window : nullptr;
#if !defined(Q_OS_VISIONOS) if (!uiWindow) {
if (!uiWindow) auto *scenes = [qt_apple_sharedApplication().connectedScenes allObjects];
uiWindow = qt_apple_sharedApplication().keyWindow; if (scenes.count > 0) {
#endif auto *windowScene = static_cast<UIWindowScene*>(scenes[0]);
uiWindow = windowScene.keyWindow;
if (!uiWindow && windowScene.windows.count)
uiWindow = windowScene.windows[0];
}
}
return uiWindow; return uiWindow;
} }

View File

@ -117,25 +117,17 @@ bool QIOSMessageDialog::show(Qt::WindowFlags windowFlags, Qt::WindowModality win
} }
UIWindow *window = presentationWindow(parent); UIWindow *window = presentationWindow(parent);
if (!window) {
qCDebug(lcQpaWindow, "Attempting to exec a dialog without any window/widget visible.");
auto *primaryScreen = static_cast<QIOSScreen*>(QGuiApplication::primaryScreen()->handle());
Q_ASSERT(primaryScreen);
window = primaryScreen->uiWindow();
if (window.hidden) {
// With a window hidden, an attempt to present view controller
// below fails with a warning, that a view "is not a part of
// any view hierarchy". The UIWindow is initially hidden,
// as unhiding it is what hides the splash screen.
window.hidden = NO;
}
}
if (!window) if (!window)
return false; return false;
if (window.hidden) {
// With a window hidden, an attempt to present view controller
// below fails with a warning, that a view "is not a part of
// any view hierarchy". The UIWindow is initially hidden,
// as unhiding it is what hides the splash screen.
window.hidden = NO;
}
[window.rootViewController presentViewController:m_alertController animated:YES completion:nil]; [window.rootViewController presentViewController:m_alertController animated:YES completion:nil];
return true; return true;
} }