]> cloud.milkyroute.net Git - dolphin.git/commitdiff
Fix activating the Selection Mode with a keyboard shortcut
authorEugene Popov <popov895@ukr.net>
Tue, 25 Apr 2023 13:05:39 +0000 (13:05 +0000)
committerFelix Ernst <felixernst@kde.org>
Tue, 25 Apr 2023 13:05:39 +0000 (13:05 +0000)
If a spacebar is used as a keyboard shortcut to activate the Selection Mode, then allow this shortcut to be triggered only if the view has a keyboard focus.

BUG: 465489

src/dolphinmainwindow.cpp
src/dolphinmainwindow.h
src/kitemviews/kitemlistcontroller.cpp
src/kitemviews/kitemlistcontroller.h
src/kitemviews/private/kitemlistkeyboardsearchmanager.cpp
src/kitemviews/private/kitemlistkeyboardsearchmanager.h
src/views/dolphinview.cpp
src/views/dolphinview.h

index 7931bab588fe4cdc0119fed49acca8fdf06c520e..0094bc2e78b0f6d049f852fe505eb1e20832bcde 100644 (file)
@@ -547,6 +547,19 @@ void DolphinMainWindow::showTarget()
     });
 }
 
+bool DolphinMainWindow::event(QEvent *event)
+{
+    if (event->type() == QEvent::ShortcutOverride) {
+        const QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
+        if (keyEvent->key() == Qt::Key_Space && m_activeViewContainer->view()->handleSpaceAsNormalKey()) {
+            event->accept();
+            return true;
+        }
+    }
+
+    return KXmlGuiWindow::event(event);
+}
+
 void DolphinMainWindow::showEvent(QShowEvent *event)
 {
     KXmlGuiWindow::showEvent(event);
@@ -1722,6 +1735,7 @@ void DolphinMainWindow::setupActions()
         "</para>"));
     toggleSelectionModeAction->setIcon(QIcon::fromTheme(QStringLiteral("quickwizard")));
     toggleSelectionModeAction->setCheckable(true);
+    actionCollection()->setDefaultShortcut(toggleSelectionModeAction, Qt::Key_Space );
     connect(toggleSelectionModeAction, &QAction::triggered, this, &DolphinMainWindow::toggleSelectionMode);
 
     // A special version of the toggleSelectionModeAction for the toolbar that also contains a menu
index fe07cbc1740f5038ccf3c755c995dc1b80488b53..92ddb24c37420b1ca0a57a89eef66d931705b6eb 100644 (file)
@@ -238,6 +238,9 @@ Q_SIGNALS:
     void settingsChanged();
 
 protected:
+    /** @see QObject::event() */
+    bool event(QEvent *event) override;
+
     /** @see QWidget::showEvent() */
     void showEvent(QShowEvent *event) override;
 
index 0c9f19f553a3aa49fe1c4a99c38031295ad927d6..2e7d2f057d5fa61b0efe8b9ae32921d2dc78824c 100644 (file)
@@ -228,6 +228,11 @@ bool KItemListController::selectionMode() const
     return m_selectionMode;
 }
 
+bool KItemListController::isSearchAsYouTypeActive() const
+{
+    return m_keyboardManager->isSearchAsYouTypeActive();
+}
+
 bool KItemListController::keyPressEvent(QKeyEvent *event)
 {
     int index = m_selectionManager->currentItem();
@@ -439,10 +444,13 @@ bool KItemListController::keyPressEvent(QKeyEvent *event)
                 m_selectionManager->setSelected(index, 1, KItemListSelectionManager::Toggle);
                 m_selectionManager->beginAnchoredSelection(index);
                 break;
-            } else if (m_keyboardManager->addKeyBeginsNewSearch()) { // File names shouldn't start with a space,
-                // so we can use this press as a keyboard shortcut instead.
-                Q_EMIT selectionModeChangeRequested(!m_selectionMode);
-                break;
+            } else {
+                // Select the current item if it is not selected yet.
+                const int current = m_selectionManager->currentItem();
+                if (!m_selectionManager->isSelected(current)) {
+                    m_selectionManager->setSelected(current);
+                    break;
+                }
             }
         }
         Q_FALLTHROUGH(); // fall through to the default case and add the Space to the current search string.
index d75a8a22d5a7173442e4b469f7ee438dd4f98a29..0969ed21a4b0a4d7a89a000b3e45b91e0118b09f 100644 (file)
@@ -123,6 +123,11 @@ public:
     void setSelectionModeEnabled(bool enabled);
     bool selectionMode() const;
 
+    /**
+     * @return \c true if search as you type is active, or \c false otherwise.
+     */
+    bool isSearchAsYouTypeActive() const;
+
     bool processEvent(QEvent *event, const QTransform &transform);
 
 Q_SIGNALS:
index b7318b34406eeb0f1f42101fa273a8c1f1465b56..c74ef16386e37c9e1786fde6505caf5893965512 100644 (file)
@@ -27,6 +27,11 @@ bool KItemListKeyboardSearchManager::shouldClearSearchIfInputTimeReached()
     return (keyboardInputTimeElapsed > m_timeout) || !keyboardTimeWasValid;
 }
 
+bool KItemListKeyboardSearchManager::isSearchAsYouTypeActive() const
+{
+    return !m_searchedString.isEmpty() && !m_keyboardInputTime.hasExpired(m_timeout);
+}
+
 void KItemListKeyboardSearchManager::addKeys(const QString &keys)
 {
     if (shouldClearSearchIfInputTimeReached()) {
@@ -63,11 +68,6 @@ void KItemListKeyboardSearchManager::addKeys(const QString &keys)
     m_keyboardInputTime.start();
 }
 
-bool KItemListKeyboardSearchManager::addKeyBeginsNewSearch() const
-{
-    return m_keyboardInputTime.hasExpired(m_timeout) || m_searchedString.isEmpty();
-}
-
 void KItemListKeyboardSearchManager::setTimeout(qint64 milliseconds)
 {
     m_timeout = milliseconds;
index d370bc9ba5c6d73e04d8dc6017142d9e591f80b5..981d98cd3c2ed0d840beb8bc21fc8a58c2216488 100644 (file)
@@ -34,11 +34,6 @@ public:
      * Add \a keys to the text buffer used for searching.
      */
     void addKeys(const QString &keys);
-    /**
-     * @returns true if the next call to addKeys() will trigger a new search.
-     *          Returns false if the next added key char will be added to the search string that was used previously.
-     */
-    bool addKeyBeginsNewSearch() const;
 
     /**
      * Sets the delay after which the search is cancelled to \a milliseconds.
@@ -51,6 +46,11 @@ public:
 
     void cancelSearch();
 
+    /**
+     * @return \c true if search as you type is active, or \c false otherwise.
+     */
+    bool isSearchAsYouTypeActive() const;
+
 public Q_SLOTS:
 
     void slotCurrentChanged(int current, int previous);
@@ -71,7 +71,6 @@ Q_SIGNALS:
 private:
     bool shouldClearSearchIfInputTimeReached();
 
-private:
     QString m_searchedString;
     bool m_isSearchRestarted;
     /** Measures the time since the last key press. */
index b33353e3a813b85d5df942d799097a08f33ab53b..186de4fccf59d14d9b4d84fca11538c3e88cff8d 100644 (file)
@@ -1828,6 +1828,11 @@ void DolphinView::hideToolTip(const ToolTipManager::HideBehavior behavior)
     }
 }
 
+bool DolphinView::handleSpaceAsNormalKey() const
+{
+    return !m_container->hasFocus() || m_container->controller()->isSearchAsYouTypeActive();
+}
+
 void DolphinView::slotTwoClicksRenamingTimerTimeout()
 {
     const KItemListSelectionManager *selectionManager = m_container->controller()->selectionManager();
index 3486ee9b45afc05c483b4663d923b1025934983c..705272308bb34e4058b77b85748f2e521c4a5139 100644 (file)
@@ -335,6 +335,17 @@ public:
      */
     void hideToolTip(const ToolTipManager::HideBehavior behavior = ToolTipManager::HideBehavior::Later);
 
+    /**
+     * Check if the space key should be handled as a normal key, even if it's
+     * used as a keyboard shortcut.
+     *
+     * See BUG 465489
+     */
+    bool handleSpaceAsNormalKey() const;
+
+    /** Activates the view if the item list container gets focus. */
+    bool eventFilter(QObject *watched, QEvent *event) override;
+
 public Q_SLOTS:
     /**
      * Changes the directory to \a url. If the current directory is equal to
@@ -426,9 +437,6 @@ public Q_SLOTS:
      */
     void updateViewState();
 
-    /** Activates the view if the item list container gets focus. */
-    bool eventFilter(QObject *watched, QEvent *event) override;
-
 Q_SIGNALS:
     /**
      * Is emitted if the view has been activated by e. g. a mouse click.