From da0c74550f0e8a21239896d6aead6e05f85eb695 Mon Sep 17 00:00:00 2001 From: Timur Pocheptsov Date: Tue, 1 Jul 2014 10:31:07 +0200 Subject: OS X: main window doesn't become key. When ordering a key window out Cocoa tries to find a new KEY window. Looks like it prefers the current MAIN window and since QNSPanel is never a main window, Cocoa is breaking the stack order. To avoid this - try to change the key window BEFORE ordering out a window. The application has a stack of all open windows (visible and hidden), we iterate through this stack starting from our current key window and look for the nearest window below, that can become a new key window. Most probably, it will be our transient parent :) This code will change (potentially) the key window _only_ if there is a transient parent. Task-number: QTBUG-39809 Change-Id: I96b630799341875fc7e38dabf1ff6416338e687b Reviewed-by: Gabriel de Dietrich --- src/plugins/platforms/cocoa/qcocoawindow.mm | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'src/plugins/platforms/cocoa') diff --git a/src/plugins/platforms/cocoa/qcocoawindow.mm b/src/plugins/platforms/cocoa/qcocoawindow.mm index 4d0458a4aa..19d0f7a987 100644 --- a/src/plugins/platforms/cocoa/qcocoawindow.mm +++ b/src/plugins/platforms/cocoa/qcocoawindow.mm @@ -80,6 +80,32 @@ static bool isMouseEvent(NSEvent *ev) } } +static void selectNextKeyWindow(NSWindow *currentKeyWindow) +{ + if (!currentKeyWindow) + return; + + const QCocoaAutoReleasePool pool; + + if ([[NSApplication sharedApplication] keyWindow] != currentKeyWindow) + return;//currentKeyWindow is not a key window actually. + + NSArray *const windows = [[NSApplication sharedApplication] windows]; + bool startLookup = false; + for (NSWindow *candidate in [windows reverseObjectEnumerator]) { + if (!startLookup) { + if (candidate == currentKeyWindow) + startLookup = true; + } else { + if ([candidate isVisible] && [candidate canBecomeKeyWindow]) { + [candidate makeKeyWindow]; + break; + } + } + } +} + + @interface NSWindow (CocoaWindowCategory) - (NSRect) legacyConvertRectFromScreen:(NSRect) rect; @end @@ -588,6 +614,9 @@ void QCocoaWindow::hide(bool becauseOfAncestor) foreach (QCocoaWindow *childWindow, m_childWindows) childWindow->hide(true); + if (window()->transientParent() && m_nsWindow == [[NSApplication sharedApplication] keyWindow]) + selectNextKeyWindow(m_nsWindow); // Otherwise, Cocoa can do it wrong. + [m_nsWindow orderOut:nil]; } -- cgit v1.2.3