From 744164e6c92cb721d2a339cee8c465e1685723f9 Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Mon, 10 Jul 2017 16:48:07 +0200 Subject: Fix focus handling for popups When a popup is closed, if there are multiple popups open, restore focus to the next popup in chain instead of transferring focus to the window content. This allows us to cleanup the custom focus handling for sub-menus in dev. There is no more need to manually transfer focus to the parent menu or to a sub-menu, but calling open() or close() is enough and the focus is automatically transferred as expected. Change-Id: I54406c7c6b8dd25af261e00ebb1ae00ccbea8b9f Reviewed-by: Mitch Curtis --- .../data/focusAfterPopupClosed.qml | 15 ++++++++ .../applicationwindow/tst_applicationwindow.cpp | 44 ++++++++++++++++++++-- 2 files changed, 55 insertions(+), 4 deletions(-) (limited to 'tests/auto/applicationwindow') diff --git a/tests/auto/applicationwindow/data/focusAfterPopupClosed.qml b/tests/auto/applicationwindow/data/focusAfterPopupClosed.qml index 015e03bc..f0499a3a 100644 --- a/tests/auto/applicationwindow/data/focusAfterPopupClosed.qml +++ b/tests/auto/applicationwindow/data/focusAfterPopupClosed.qml @@ -57,9 +57,12 @@ ApplicationWindow { visible: true signal focusScopeKeyPressed + signal focusPopupKeyPressed + property alias fileMenu: fileMenu property alias toolButton: toolButton property alias focusScope: focusScope + property alias focusPopup: focusPopup header: ToolBar { ToolButton { @@ -92,5 +95,17 @@ ApplicationWindow { Keys.onSpacePressed: focusScopeKeyPressed() } + + Popup { + id: focusPopup + focus: true + width: parent.width + height: parent.height + + Item { + focus: true + Keys.onSpacePressed: focusPopupKeyPressed() + } + } } diff --git a/tests/auto/applicationwindow/tst_applicationwindow.cpp b/tests/auto/applicationwindow/tst_applicationwindow.cpp index 61f84673..f35d9b7d 100644 --- a/tests/auto/applicationwindow/tst_applicationwindow.cpp +++ b/tests/auto/applicationwindow/tst_applicationwindow.cpp @@ -46,6 +46,8 @@ #include #include #include +#include +#include #include #include #include @@ -664,9 +666,9 @@ void tst_applicationwindow::focusAfterPopupClosed() QVERIFY(focusScope); QVERIFY(focusScope->hasActiveFocus()); - QSignalSpy spy(window.data(), SIGNAL(focusScopeKeyPressed())); + QSignalSpy focusScopeSpy(window.data(), SIGNAL(focusScopeKeyPressed())); QTest::keyClick(window.data(), Qt::Key_Space); - QCOMPARE(spy.count(), 1); + QCOMPARE(focusScopeSpy.count(), 1); // Open the menu. QQuickItem* toolButton = window->property("toolButton").value(); @@ -677,14 +679,48 @@ void tst_applicationwindow::focusAfterPopupClosed() // The FocusScope shouldn't receive any key events while the menu is open. QTest::keyClick(window.data(), Qt::Key_Space); - QCOMPARE(spy.count(), 1); + QCOMPARE(focusScopeSpy.count(), 1); // Close the menu. The FocusScope should regain focus. QTest::keyClick(window.data(), Qt::Key_Escape); QVERIFY(focusScope->hasActiveFocus()); QTest::keyClick(window.data(), Qt::Key_Space); - QCOMPARE(spy.count(), 2); + QCOMPARE(focusScopeSpy.count(), 2); + + QQuickPopup *focusPopup = window->property("focusPopup").value(); + QVERIFY(focusPopup); + QVERIFY(!focusPopup->hasActiveFocus()); + + focusPopup->open(); + QVERIFY(focusPopup->isVisible()); + + QSignalSpy focusPopupSpy(window.data(), SIGNAL(focusPopupKeyPressed())); + QTest::keyClick(window.data(), Qt::Key_Space); + QCOMPARE(focusPopupSpy.count(), 1); + + QQuickMenu *fileMenu = window->property("fileMenu").value(); + QVERIFY(fileMenu); + fileMenu->open(); + QVERIFY(fileMenu->isVisible()); + + // The Popup shouldn't receive any key events while the menu is open. + QTest::keyClick(window.data(), Qt::Key_Space); + QCOMPARE(focusPopupSpy.count(), 1); + + // Close the menu. The Popup should regain focus. + QTest::keyClick(window.data(), Qt::Key_Escape); + QVERIFY(focusPopup->hasActiveFocus()); + + QTest::keyClick(window.data(), Qt::Key_Space); + QCOMPARE(focusPopupSpy.count(), 2); + + // Close the popup. The FocusScope should regain focus. + QTest::keyClick(window.data(), Qt::Key_Escape); + QVERIFY(focusScope->hasActiveFocus()); + + QTest::keyClick(window.data(), Qt::Key_Space); + QCOMPARE(focusScopeSpy.count(), 3); } void tst_applicationwindow::clearFocusOnDestruction() -- cgit v1.2.3