From a3489a3bc315387b5f3fa9b88a40f4686c2722f6 Mon Sep 17 00:00:00 2001 From: Doris Verria Date: Thu, 30 Sep 2021 16:46:37 +0200 Subject: QCocoaWindow: Make window key if the app's modal window is hidden MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit On macOS, when showing a window, we decide if it should be made key and therefore active, if the app has no active modal session or if the window's worksWhenModal returns true. However, the window needs to be made key also when a modal window is present, but not visible. Add this condition when checking if the window needs to be made key. This makes the behavior consistent with what happens when a modal is minimized on macOS. The input focus is passed to the next window, and the window appears active, even if it can not be interacted with. Fixes: QTBUG-85574 Change-Id: I204d4f912128f4a46840789fc2ee08e1b2716bfc Reviewed-by: Tor Arne Vestbø Reviewed-by: Volker Hilsheimer (cherry picked from commit 911c97f2b59945093e9e87130d687c6a2a9423ac) Reviewed-by: Doris Verria --- src/plugins/platforms/cocoa/qcocoawindow.mm | 4 +++- tests/auto/widgets/kernel/qwidget/tst_qwidget.cpp | 21 +++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/plugins/platforms/cocoa/qcocoawindow.mm b/src/plugins/platforms/cocoa/qcocoawindow.mm index 2f77fdb47f..3e08cbd655 100644 --- a/src/plugins/platforms/cocoa/qcocoawindow.mm +++ b/src/plugins/platforms/cocoa/qcocoawindow.mm @@ -376,7 +376,9 @@ void QCocoaWindow::setVisible(bool visible) // Show the window as application modal eventDispatcher()->beginModalSession(window()); } else if (m_view.window.canBecomeKeyWindow) { - bool shouldBecomeKeyNow = !NSApp.modalWindow || m_view.window.worksWhenModal; + bool shouldBecomeKeyNow = !NSApp.modalWindow + || m_view.window.worksWhenModal + || !NSApp.modalWindow.visible; // Panels with becomesKeyOnlyIfNeeded set should not activate until a view // with needsPanelToBecomeKey, for example a line edit, is clicked. diff --git a/tests/auto/widgets/kernel/qwidget/tst_qwidget.cpp b/tests/auto/widgets/kernel/qwidget/tst_qwidget.cpp index 431433c7d6..1a72ca298f 100644 --- a/tests/auto/widgets/kernel/qwidget/tst_qwidget.cpp +++ b/tests/auto/widgets/kernel/qwidget/tst_qwidget.cpp @@ -421,6 +421,8 @@ private slots: void receivesLanguageChangeEvent(); void deleteWindowInCloseEvent(); + void activateWhileModalHidden(); + private: bool ensureScreenSize(int width, int height); @@ -11803,5 +11805,24 @@ void tst_QWidget::deleteWindowInCloseEvent() QVERIFY(true); } +void tst_QWidget::activateWhileModalHidden() +{ + QDialog dialog; + dialog.setWindowModality(Qt::ApplicationModal); + dialog.show(); + QVERIFY(QTest::qWaitForWindowActive(&dialog)); + QVERIFY(dialog.isActiveWindow()); + QCOMPARE(QApplication::activeWindow(), &dialog); + + dialog.hide(); + QTRY_VERIFY(!dialog.isVisible()); + + QMainWindow window; + window.show(); + QVERIFY(QTest::qWaitForWindowActive(&window)); + QVERIFY(window.isActiveWindow()); + QCOMPARE(QApplication::activeWindow(), &window); +} + QTEST_MAIN(tst_QWidget) #include "tst_qwidget.moc" -- cgit v1.2.3