From 3f3b2be870c34ef72034d4b853ef202e478c7cb7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tor=20Arne=20Vestb=C3=B8?= Date: Tue, 1 Aug 2023 18:47:45 +0200 Subject: [PATCH] macOS: Ensure foreign windows can be reparented via QWindow::setParent() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In b64b0c7947f9f2b0ab4ed33fe526880f54e3981f we bailed out from QCocoaWindow::recreateWindowIfNeeded() for foreign windows, as we should not manage any NSWindows on their behalf. Unfortunately QCocoaWindow::recreateWindowIfNeeded() also took care of adding the view as a subview to the potential non top level view, which we do want for foreign views. Ideally we'd move the reparenting out of recreateWindowIfNeeded() and into QCocoaWindow::setParent(), but this is a more intrusive change, so for now just restore the original missing logic. Pick-to: 6.6 6.5 Change-Id: Ic35ebf94d4adc2f19cedb1cb6a5d0215a1c9c2b4 Reviewed-by: Friedemann Kleint Reviewed-by: Christian Strømme --- src/plugins/platforms/cocoa/qcocoawindow.mm | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/plugins/platforms/cocoa/qcocoawindow.mm b/src/plugins/platforms/cocoa/qcocoawindow.mm index 3342f29614..470b3f4614 100644 --- a/src/plugins/platforms/cocoa/qcocoawindow.mm +++ b/src/plugins/platforms/cocoa/qcocoawindow.mm @@ -1448,16 +1448,22 @@ void QCocoaWindow::recreateWindowIfNeeded() { QMacAutoReleasePool pool; + QPlatformWindow *parentWindow = QPlatformWindow::parent(); + auto *parentCocoaWindow = static_cast(parentWindow); + if (isForeignWindow()) { // A foreign window is created as such, and can never move between being // foreign and not, so we don't need to get rid of any existing NSWindows, // nor create new ones, as a foreign window is a single simple NSView. qCDebug(lcQpaWindow) << "Skipping NSWindow management for foreign window" << this; + + // We do however need to manage the parent relationship + if (parentCocoaWindow) + [parentCocoaWindow->m_view addSubview:m_view]; + return; } - QPlatformWindow *parentWindow = QPlatformWindow::parent(); - const bool isEmbeddedView = isEmbedded(); RecreationReasons recreateReason = RecreationNotNeeded; @@ -1495,8 +1501,6 @@ void QCocoaWindow::recreateWindowIfNeeded() if (recreateReason == RecreationNotNeeded) return; - QCocoaWindow *parentCocoaWindow = static_cast(parentWindow); - // Remove current window (if any) if ((isContentView() && !shouldBeContentView) || (recreateReason & PanelChanged)) { if (m_nsWindow) {