]> cloud.milkyroute.net Git - dolphin.git/commitdiff
Make Escape move focus from location bar to view
authorFelix Ernst <felixernst@zohomail.eu>
Mon, 28 Oct 2024 02:58:45 +0000 (03:58 +0100)
committerMéven Car <meven@kde.org>
Thu, 31 Oct 2024 16:28:02 +0000 (16:28 +0000)
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
src/dolphinurlnavigator.h
src/dolphinviewcontainer.cpp
src/tests/dolphinmainwindowtest.cpp

index 3a40eea5c482fbefe777b8f24d5c413909138d82..5c32538b06b27dbbdf12d7556d24166d90ce5926 100644 (file)
@@ -16,6 +16,7 @@
 #include <KUrlComboBox>
 
 #include <QAbstractButton>
+#include <QKeyEvent>
 #include <QLabel>
 #include <QLayout>
 #include <QLineEdit>
@@ -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"
index 1387b567f694aba7c3dd4c3a2d02745c7a91335c..d6da51b47a647601032c13ee1e29b96c8d1f248c 100644 (file)
@@ -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
index 62c29a1fc684eb4fc5a4c4cbeca2c0a9578bb9c9..c6c56ce240deb5a0340a8bdfcf7bbc7ba2bae1b7 100644 (file)
@@ -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;
index 8fd5be7cba8a10a30b18d14331e71bbb2a98cc3d..a57632a1d84f0537d276b6929a1b69fed6270638 100644 (file)
@@ -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);