[PATCH] KFileWidget: Fix key navigation escaping in save dialogs
authorAkseli Lahtinen <akselmo@akselmo.dev>
Wed, 7 May 2025 13:35:19 +0000 (16:35 +0300)
committerAurélien COUDERC <coucouf@debian.org>
Sun, 8 Jun 2025 12:42:29 +0000 (14:42 +0200)
In save dialogs the keyboard navigation would escape during file
highlighting, since the fileHighlight sets the focus for fileName bar
for mouse operations.

This makes sure the user has to press Tab to explicitly escape the
keyboard navigation mode. For clicking the items, it should not affect
at all.

CCBUG: 466206
FIXED-IN: 6.14
(cherry picked from commit 8e4e84f045b7459c0b02b1b1b51a9df73cea068a)

Gbp-Pq: Name upstream_719e0b00_KFileWidget-Fix-key-navigation-escaping-in-save-dialogs.patch

src/filewidgets/kdiroperator.cpp
src/filewidgets/kdiroperator.h
src/filewidgets/kfilewidget.cpp

index e1d3afeeaa330428d6e9eaff33ddda841f28a0bb..4a2e400c7bf36063fc6cafeb3dd8fbb23c90061a 100644 (file)
@@ -221,6 +221,7 @@ public:
     bool m_showOpenWithActions = false;
     bool m_isTouchEvent = false;
     bool m_isTouchDrag = false;
+    bool m_keyNavigation = false;
 
     QList<QUrl> m_itemsToBeSetAsCurrent;
     QStringList m_supportedSchemes;
@@ -1244,6 +1245,11 @@ void KDirOperator::showOpenWithActions(bool enable)
     d->m_showOpenWithActions = enable;
 }
 
+bool KDirOperator::usingKeyNavigation()
+{
+    return d->m_keyNavigation;
+}
+
 void KDirOperator::changeEvent(QEvent *event)
 {
     QWidget::changeEvent(event);
@@ -1429,6 +1435,19 @@ bool KDirOperator::eventFilter(QObject *watched, QEvent *event)
                 return true;
             }
         }
+        // Only use tab key to escape the view navigation
+        if (evt->key() == Qt::Key_Tab) {
+            d->m_keyNavigation = false;
+            d->slotSelectionChanged();
+            // When saving we need to return here,
+            // otherwise we skip over the next item with our tab press
+            // since we focus on that item in slotSelectionChanged
+            if (d->m_isSaving) {
+                return true;
+            }
+        } else {
+            d->m_keyNavigation = true;
+        }
         break;
     }
     case QEvent::Resize: {
@@ -1833,6 +1852,7 @@ void KDirOperator::selectFile(const KFileItem &item)
     QApplication::restoreOverrideCursor();
 
     Q_EMIT fileSelected(item);
+    d->m_keyNavigation = false;
 }
 
 void KDirOperator::highlightFile(const KFileItem &item)
@@ -1842,6 +1862,7 @@ void KDirOperator::highlightFile(const KFileItem &item)
     }
 
     Q_EMIT fileHighlighted(item);
+    d->m_keyNavigation = false;
 }
 
 void KDirOperator::setCurrentItem(const QUrl &url)
index c34880564eca702f2030837179004a178006fea4..71a20ed2e24071d700e26ed0e12a1271b6c37247 100644 (file)
@@ -744,6 +744,13 @@ public:
      */
     void showOpenWithActions(bool enable);
 
+    /*!
+     * @returns true if the user was using keys to navigate.
+     *
+     * \since 6.14
+     */
+    bool usingKeyNavigation();
+
 protected:
     /**
      * A view factory for creating predefined fileviews. Called internally by setView,
index 37f5e874ac878b81a646886a97e884e5faf42cbe..8170f32d87ef23d49bc5df457e33ad4fec3fc5aa 100644 (file)
@@ -166,7 +166,7 @@ public:
     void enterUrl(const QString &);
     void locationAccepted(const QString &);
     void slotFilterChanged();
-    void fileHighlighted(const KFileItem &);
+    void fileHighlighted(const KFileItem &, bool);
     void fileSelected(const KFileItem &);
     void slotLoadingFinished();
     void togglePlacesPanel(bool show, QObject *sender = nullptr);
@@ -908,7 +908,7 @@ void KFileWidget::accept()
     d->m_ops->close();
 }
 
-void KFileWidgetPrivate::fileHighlighted(const KFileItem &i)
+void KFileWidgetPrivate::fileHighlighted(const KFileItem &i, bool isKeyNavigation)
 {
     if ((m_locationEdit->hasFocus() && !m_locationEdit->currentText().isEmpty())) { // don't disturb
         return;
@@ -951,7 +951,7 @@ void KFileWidgetPrivate::fileHighlighted(const KFileItem &i)
     // rename it if desired
     // Note that double-clicking will override this and overwrite regardless of
     // single/double click mouse setting (see slotViewDoubleClicked() )
-    if (m_operationMode == KFileWidget::Saving) {
+    if (!isKeyNavigation && m_operationMode == KFileWidget::Saving) {
         m_locationEdit->setFocus();
     }
 }
@@ -1106,7 +1106,7 @@ void KFileWidgetPrivate::initDirOpWidgets()
         urlEntered(url);
     });
     q->connect(m_ops, &KDirOperator::fileHighlighted, q, [this](const KFileItem &item) {
-        fileHighlighted(item);
+        fileHighlighted(item, m_ops->usingKeyNavigation());
     });
     q->connect(m_ops, &KDirOperator::fileSelected, q, [this](const KFileItem &item) {
         fileSelected(item);