macOS: Modernize and clarify transient parent window level inheritance

Task-number: QTBUG-71480
Change-Id: Ia026427844a674f6b36804571a897dc6f16364fa
Reviewed-by: Timur Pocheptsov <timur.pocheptsov@qt.io>
This commit is contained in:
Tor Arne Vestbø 2019-07-18 13:35:35 +02:00
parent 5c351da046
commit c5ab86976b

View File

@ -453,13 +453,35 @@ NSInteger QCocoaWindow::windowLevel(Qt::WindowFlags flags)
if (type == Qt::ToolTip) if (type == Qt::ToolTip)
windowLevel = NSScreenSaverWindowLevel; windowLevel = NSScreenSaverWindowLevel;
// Any "special" window should be in at least the same level as its parent. auto *transientParent = window()->transientParent();
if (type != Qt::Window) { if (transientParent && transientParent->handle()) {
const QWindow * const transientParent = window()->transientParent(); // We try to keep windows in at least the same window level as
const QCocoaWindow * const transientParentWindow = transientParent ? // their transient parent. Unfortunately this only works when the
static_cast<QCocoaWindow *>(transientParent->handle()) : nullptr; // window is created. If the window level changes after that, as
if (transientParentWindow) // a result of a call to setWindowFlags, or by changing the level
windowLevel = qMax([transientParentWindow->nativeWindow() level], windowLevel); // of the native window, we will not pick this up, and the window
// will be left behind (or in a different window level than) its
// parent. We could KVO-observe the window level of our transient
// parent, but that requires us to know when the parent goes away
// so that we can unregister the observation before the parent is
// dealloced, something we can't do for generic NSWindows. Another
// way would be to override [NSWindow setLevel:] and notify child
// windows about the change, but that doesn't work for foreign
// windows, which can still be transient parents via fromWinId().
// One area where this problem is apparent is when AppKit tweaks
// the window level of modal windows during application activation
// and deactivation. Since we don't pick up on these window level
// changes in a generic way, we need to add logic explicitly to
// re-evaluate the window level after AppKit has done its tweaks.
auto *transientCocoaWindow = static_cast<QCocoaWindow *>(transientParent->handle());
auto *nsWindow = transientCocoaWindow->nativeWindow();
// We only upgrade the window level for "special" windows, to work
// around Qt Designer parenting the designer windows to the widget
// palette window (QTBUG-31779). This should be fixed in designer.
if (type != Qt::Window)
windowLevel = qMax(windowLevel, nsWindow.level);
} }
return windowLevel; return windowLevel;