From 67695c1b01ea241b359e456dad44488e5cd9fdce Mon Sep 17 00:00:00 2001 From: Felix Ernst Date: Mon, 28 Oct 2024 03:58:45 +0100 Subject: [PATCH] Make Escape move focus from location bar to view Pressing Escape on the location bar while in breadcrumb mode has no effect at all. This commit changes this to instead move the focus to the active view. This is more logical because a user pressing escape while having focus on the location bar is no longer interested in interacting and changing the location in the location bar. They most likely want to act on the current location instead. --- src/dolphinurlnavigator.cpp | 10 ++++++++++ src/dolphinurlnavigator.h | 12 +++++++++++ src/dolphinviewcontainer.cpp | 4 ++++ src/tests/dolphinmainwindowtest.cpp | 31 +++++++++++++++++++++++++++++ 4 files changed, 57 insertions(+) diff --git a/src/dolphinurlnavigator.cpp b/src/dolphinurlnavigator.cpp index 3a40eea5c..5c32538b0 100644 --- a/src/dolphinurlnavigator.cpp +++ b/src/dolphinurlnavigator.cpp @@ -16,6 +16,7 @@ #include #include +#include #include #include #include @@ -145,4 +146,13 @@ void DolphinUrlNavigator::slotReturnPressed() } } +void DolphinUrlNavigator::keyPressEvent(QKeyEvent *keyEvent) +{ + if (keyEvent->key() == Qt::Key_Escape && !isUrlEditable()) { + Q_EMIT requestToLoseFocus(); + return; + } + KUrlNavigator::keyPressEvent(keyEvent); +} + #include "moc_dolphinurlnavigator.cpp" diff --git a/src/dolphinurlnavigator.h b/src/dolphinurlnavigator.h index 1387b567f..d6da51b47 100644 --- a/src/dolphinurlnavigator.h +++ b/src/dolphinurlnavigator.h @@ -92,6 +92,18 @@ public Q_SLOTS: * preferred in the Dolphin settings. */ void slotReturnPressed(); + +Q_SIGNALS: + /** + * Escape was pressed, and the focus should return to the view. + */ + void requestToLoseFocus(); + +protected: + /** + * Return focus back to the view when pressing Escape and this would have no other effect (e.g. deselecting or changing edit mode). + */ + void keyPressEvent(QKeyEvent *keyEvent) override; }; #endif // DOLPHINURLNAVIGATOR_H diff --git a/src/dolphinviewcontainer.cpp b/src/dolphinviewcontainer.cpp index 62c29a1fc..c6c56ce24 100644 --- a/src/dolphinviewcontainer.cpp +++ b/src/dolphinviewcontainer.cpp @@ -309,6 +309,9 @@ void DolphinViewContainer::connectUrlNavigator(DolphinUrlNavigator *urlNavigator // Aside from these, only visual things need to be connected. connect(m_view, &DolphinView::urlChanged, urlNavigator, &DolphinUrlNavigator::setLocationUrl); connect(urlNavigator, &DolphinUrlNavigator::activated, this, &DolphinViewContainer::activate); + connect(urlNavigator, &DolphinUrlNavigator::requestToLoseFocus, m_view, [this]() { + m_view->setFocus(); + }); urlNavigator->setReadOnlyBadgeVisible(rootItem().isLocalFile() && !rootItem().isWritable()); @@ -325,6 +328,7 @@ void DolphinViewContainer::disconnectUrlNavigator() disconnect(m_urlNavigatorConnected, &DolphinUrlNavigator::urlsDropped, this, nullptr); disconnect(m_view, &DolphinView::urlChanged, m_urlNavigatorConnected, &DolphinUrlNavigator::setLocationUrl); disconnect(m_urlNavigatorConnected, &DolphinUrlNavigator::activated, this, &DolphinViewContainer::activate); + disconnect(m_urlNavigatorConnected, &DolphinUrlNavigator::requestToLoseFocus, m_view, nullptr); m_urlNavigatorVisualState = m_urlNavigatorConnected->visualState(); m_urlNavigatorConnected = nullptr; diff --git a/src/tests/dolphinmainwindowtest.cpp b/src/tests/dolphinmainwindowtest.cpp index 8fd5be7cb..a57632a1d 100644 --- a/src/tests/dolphinmainwindowtest.cpp +++ b/src/tests/dolphinmainwindowtest.cpp @@ -48,6 +48,7 @@ private Q_SLOTS: void testNewFileMenuEnabled(); void testWindowTitle_data(); void testWindowTitle(); + void testFocusLocationBar(); void testFocusPlacesPanel(); void testPlacesPanelWidthResistance(); void testGoActions(); @@ -389,6 +390,36 @@ void DolphinMainWindowTest::testWindowTitle() QCOMPARE(m_mainWindow->windowTitle(), expectedWindowTitle); } +void DolphinMainWindowTest::testFocusLocationBar() +{ + const QUrl homePathUrl{QUrl::fromLocalFile(QDir::homePath())}; + m_mainWindow->openDirectories({homePathUrl}, false); + m_mainWindow->show(); + QVERIFY(QTest::qWaitForWindowExposed(m_mainWindow.data())); + QVERIFY(m_mainWindow->isVisible()); + + QAction *replaceLocationAction = m_mainWindow->actionCollection()->action(QStringLiteral("replace_location")); + replaceLocationAction->trigger(); + QVERIFY(m_mainWindow->activeViewContainer()->urlNavigator()->isAncestorOf(QApplication::focusWidget())); + replaceLocationAction->trigger(); + QVERIFY(m_mainWindow->activeViewContainer()->view()->hasFocus()); + + QAction *editableLocationAction = m_mainWindow->actionCollection()->action(QStringLiteral("editable_location")); + editableLocationAction->trigger(); + QVERIFY(m_mainWindow->activeViewContainer()->urlNavigator()->isAncestorOf(QApplication::focusWidget())); + QVERIFY(m_mainWindow->activeViewContainer()->urlNavigator()->isUrlEditable()); + editableLocationAction->trigger(); + QVERIFY(!m_mainWindow->activeViewContainer()->urlNavigator()->isUrlEditable()); + + replaceLocationAction->trigger(); + QVERIFY(m_mainWindow->activeViewContainer()->urlNavigator()->isAncestorOf(QApplication::focusWidget())); + + // Pressing Escape multiple times should eventually move the focus back to the active view. + QTest::keyClick(QApplication::focusWidget(), Qt::Key_Escape); // Focus might not go the view yet because it toggles the editable state of the location bar. + QTest::keyClick(QApplication::focusWidget(), Qt::Key_Escape); + QVERIFY(m_mainWindow->activeViewContainer()->view()->hasFocus()); +} + void DolphinMainWindowTest::testFocusPlacesPanel() { m_mainWindow->openDirectories({QUrl::fromLocalFile(QDir::homePath())}, false); -- 2.47.3