diff --git a/src/kitemviews/kitemlistcontroller.cpp b/src/kitemviews/kitemlistcontroller.cpp --- a/src/kitemviews/kitemlistcontroller.cpp +++ b/src/kitemviews/kitemlistcontroller.cpp @@ -61,6 +61,8 @@ this, &KItemListController::slotChangeCurrentItem); connect(m_selectionManager, &KItemListSelectionManager::currentChanged, m_keyboardManager, &KItemListKeyboardSearchManager::slotCurrentChanged); + connect(m_selectionManager, &KItemListSelectionManager::selectionChanged, + m_keyboardManager, &KItemListKeyboardSearchManager::slotSelectionChanged); m_autoActivationTimer = new QTimer(this); m_autoActivationTimer->setSingleShot(true); @@ -472,12 +474,12 @@ if (!m_model || m_model->count() == 0) { return; } - const int currentIndex = m_selectionManager->currentItem(); int index; if (searchFromNextItem) { + const int currentIndex = m_selectionManager->currentItem(); index = m_model->indexForKeyboardSearch(text, (currentIndex + 1) % m_model->count()); } else { - index = m_model->indexForKeyboardSearch(text, currentIndex); + index = m_model->indexForKeyboardSearch(text, 0); } if (index >= 0) { m_selectionManager->setCurrentItem(index); diff --git a/src/kitemviews/private/kitemlistkeyboardsearchmanager.h b/src/kitemviews/private/kitemlistkeyboardsearchmanager.h --- a/src/kitemviews/private/kitemlistkeyboardsearchmanager.h +++ b/src/kitemviews/private/kitemlistkeyboardsearchmanager.h @@ -24,6 +24,7 @@ #define KITEMLISTKEYBOARDSEARCHMANAGER_H #include "dolphin_export.h" +#include "kitemviews/kitemset.h" #include #include @@ -59,10 +60,12 @@ qint64 timeout() const; void cancelSearch(); + bool shouldClearSearchIfInputTimeReached(); public slots: void slotCurrentChanged(int current, int previous); + void slotSelectionChanged(const KItemSet& current, const KItemSet& previous); signals: /** @@ -78,6 +81,7 @@ private: QString m_searchedString; + bool m_isSearchRestarted; QElapsedTimer m_keyboardInputTime; qint64 m_timeout; }; diff --git a/src/kitemviews/private/kitemlistkeyboardsearchmanager.cpp b/src/kitemviews/private/kitemlistkeyboardsearchmanager.cpp --- a/src/kitemviews/private/kitemlistkeyboardsearchmanager.cpp +++ b/src/kitemviews/private/kitemlistkeyboardsearchmanager.cpp @@ -22,9 +22,9 @@ #include "kitemlistkeyboardsearchmanager.h" - KItemListKeyboardSearchManager::KItemListKeyboardSearchManager(QObject* parent) : QObject(parent), + m_isSearchRestarted(false), m_timeout(1000) { m_keyboardInputTime.invalidate(); @@ -34,11 +34,16 @@ { } -void KItemListKeyboardSearchManager::addKeys(const QString& keys) +bool KItemListKeyboardSearchManager::shouldClearSearchIfInputTimeReached() { const bool keyboardTimeWasValid = m_keyboardInputTime.isValid(); const qint64 keyboardInputTimeElapsed = m_keyboardInputTime.restart(); - if (keyboardInputTimeElapsed > m_timeout || !keyboardTimeWasValid) { + return (keyboardInputTimeElapsed > m_timeout) || !keyboardTimeWasValid; +} + +void KItemListKeyboardSearchManager::addKeys(const QString& keys) +{ + if (shouldClearSearchIfInputTimeReached()) { m_searchedString.clear(); } @@ -59,9 +64,13 @@ const bool sameKey = m_searchedString.length() > 1 && m_searchedString.count(firstKey) == m_searchedString.length(); // Searching for a matching item should start from the next item if either - // 1. a new search is started, or + // 1. a new search is started and a search has not been restarted or // 2. a 'repeated key' search is done. - const bool searchFromNextItem = newSearch || sameKey; + const bool searchFromNextItem = (!m_isSearchRestarted && newSearch) || sameKey; + + // to remember not to searchFromNextItem if selection was deselected + // loosing keyboard search context basically + m_isSearchRestarted = false; emit changeCurrentItem(sameKey ? firstKey : m_searchedString, searchFromNextItem); } @@ -80,6 +89,7 @@ void KItemListKeyboardSearchManager::cancelSearch() { + m_isSearchRestarted = true; m_searchedString.clear(); } @@ -92,3 +102,12 @@ cancelSearch(); } } + +void KItemListKeyboardSearchManager::slotSelectionChanged(const KItemSet& current, const KItemSet& previous) +{ + if (!previous.isEmpty() && current.isEmpty() && previous.count() > 0 && current.count() == 0 && shouldClearSearchIfInputTimeReached()) { + // The selection has been emptied and the input time was reached. + // We should cancel the search and force the search restart + cancelSearch(); + } +} diff --git a/src/tests/kitemlistcontrollertest.cpp b/src/tests/kitemlistcontrollertest.cpp --- a/src/tests/kitemlistcontrollertest.cpp +++ b/src/tests/kitemlistcontrollertest.cpp @@ -326,6 +326,12 @@ << qMakePair(KeyPress(Qt::Key_E), ViewState(13, KItemSet() << 13)) << qMakePair(KeyPress(Qt::Key_Space), ViewState(14, KItemSet() << 14)) << qMakePair(KeyPress(Qt::Key_3), ViewState(15, KItemSet() << 15)) + << qMakePair(KeyPress(Qt::Key_Escape), ViewState(15, KItemSet())) + << qMakePair(KeyPress(Qt::Key_E), ViewState(13, KItemSet() << 13)) + << qMakePair(KeyPress(Qt::Key_E), ViewState(14, KItemSet() << 14)) + << qMakePair(KeyPress(Qt::Key_E), ViewState(15, KItemSet() << 15)) + << qMakePair(KeyPress(Qt::Key_Escape), ViewState(15, KItemSet())) + << qMakePair(KeyPress(Qt::Key_E), ViewState(13, KItemSet() << 13)) << qMakePair(KeyPress(Qt::Key_Home), ViewState(0, KItemSet() << 0)) << qMakePair(KeyPress(Qt::Key_Escape), ViewState(0, KItemSet()));