From a34b0855f160add13d08c21715dd6853094c23e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tor=20Arne=20Vestb=C3=B8?= Date: Tue, 30 Jul 2019 15:41:57 +0200 Subject: macOS: Don't assume NSWindows will be created on the screen we request The user may have assigned the application to start up on a specific display, in which case the window's screen is nil after creation, and the resulting screen will be delivered as a normal screen change once the window is ordered on screen. Fixes: QTBUG-77154 Change-Id: Idade6d833e31654db239243f2430166b5d86eca2 Reviewed-by: Volker Hilsheimer --- src/plugins/platforms/cocoa/qcocoawindow.mm | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/plugins/platforms/cocoa/qcocoawindow.mm b/src/plugins/platforms/cocoa/qcocoawindow.mm index b609fb3995..ab20c7abe9 100644 --- a/src/plugins/platforms/cocoa/qcocoawindow.mm +++ b/src/plugins/platforms/cocoa/qcocoawindow.mm @@ -1569,8 +1569,8 @@ QCocoaNSWindow *QCocoaWindow::createNSWindow(bool shouldBePanel) } rect.translate(-targetScreen->geometry().topLeft()); - QCocoaScreen *cocoaScreen = static_cast(targetScreen->handle()); - NSRect frame = QCocoaScreen::mapToNative(rect, cocoaScreen); + auto *targetCocoaScreen = static_cast(targetScreen->handle()); + NSRect frame = QCocoaScreen::mapToNative(rect, targetCocoaScreen); // Create NSWindow Class windowClass = shouldBePanel ? [QNSPanel class] : [QNSWindow class]; @@ -1579,15 +1579,22 @@ QCocoaNSWindow *QCocoaWindow::createNSWindow(bool shouldBePanel) // Deferring window creation breaks OpenGL (the GL context is // set up before the window is shown and needs a proper window) backing:NSBackingStoreBuffered defer:NO - screen:cocoaScreen->nativeScreen() + screen:targetCocoaScreen->nativeScreen() platformWindow:this]; - Q_ASSERT_X(nsWindow.screen == cocoaScreen->nativeScreen(), "QCocoaWindow", - "Resulting NSScreen should match the requested NSScreen"); + // The resulting screen can be different from the screen requested if + // for example the application has been assigned to a specific display. + auto resultingScreen = QCocoaIntegration::instance()->screenForNSScreen(nsWindow.screen); - if (targetScreen != window()->screen()) { + // But may not always be resolved at this point, in which case we fall back + // to the target screen. The real screen will be delivered as a screen change + // when resolved as part of ordering the window on screen. + if (!resultingScreen) + resultingScreen = targetCocoaScreen; + + if (resultingScreen->screen() != window()->screen()) { QWindowSystemInterface::handleWindowScreenChanged< - QWindowSystemInterface::SynchronousDelivery>(window(), targetScreen); + QWindowSystemInterface::SynchronousDelivery>(window(), resultingScreen->screen()); } static QSharedPointer sharedDelegate([[QNSWindowDelegate alloc] init], -- cgit v1.2.3