From b4dedc0d0df2c8b191192476d7787630180e51c7 Mon Sep 17 00:00:00 2001 From: Yifan Zhu Date: Thu, 20 Feb 2025 10:35:06 -0800 Subject: kitemlistkeyboardsearchmanager: smarter search start position Search from the next position for new search and special repeated key search. Otherwise search from the current position, which is current selected item if something is selected or in selection mode, and 0 (the beginning) otherwise. Test plan: - create directory with files ab1, ab2, and ab3 - click ab2, and press escape to deselect - type ab; verify that ab1 is selected - wait a while, type ab again, verify that ab2 is selected - wait a while, type ab again, verify that ab3 is selected - click ab1, type ab, verify that ab2 is selected BUG: 422951 --- src/kitemviews/kitemlistcontroller.cpp | 6 +++--- src/kitemviews/private/kitemlistkeyboardsearchmanager.cpp | 10 ++-------- src/kitemviews/private/kitemlistkeyboardsearchmanager.h | 1 - 3 files changed, 5 insertions(+), 12 deletions(-) (limited to 'src/kitemviews') diff --git a/src/kitemviews/kitemlistcontroller.cpp b/src/kitemviews/kitemlistcontroller.cpp index 5a396de61..2f1bdc551 100644 --- a/src/kitemviews/kitemlistcontroller.cpp +++ b/src/kitemviews/kitemlistcontroller.cpp @@ -521,9 +521,9 @@ void KItemListController::slotChangeCurrentItem(const QString &text, bool search return; } int index; - if (searchFromNextItem) { - const int currentIndex = m_selectionManager->currentItem(); - index = m_model->indexForKeyboardSearch(text, (currentIndex + 1) % m_model->count()); + // In selection mode, always use the current (underlined) item, or the next item, for search start position. + if (m_selectionBehavior == NoSelection || m_selectionMode || m_selectionManager->hasSelection()) { + index = m_model->indexForKeyboardSearch(text, searchFromNextItem ? m_selectionManager->currentItem() + 1 : m_selectionManager->currentItem()); } else { index = m_model->indexForKeyboardSearch(text, 0); } diff --git a/src/kitemviews/private/kitemlistkeyboardsearchmanager.cpp b/src/kitemviews/private/kitemlistkeyboardsearchmanager.cpp index c18f87b7d..afc3bb071 100644 --- a/src/kitemviews/private/kitemlistkeyboardsearchmanager.cpp +++ b/src/kitemviews/private/kitemlistkeyboardsearchmanager.cpp @@ -10,7 +10,6 @@ KItemListKeyboardSearchManager::KItemListKeyboardSearchManager(QObject *parent) : QObject(parent) - , m_isSearchRestarted(false) , m_timeout(1000) { m_keyboardInputTime.invalidate(); @@ -55,13 +54,9 @@ void KItemListKeyboardSearchManager::addKeys(const QString &keys) 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 and a search has not been restarted or + // 1. a new search is started, or // 2. a 'repeated key' search is done. - const bool searchFromNextItem = (!m_isSearchRestarted && newSearch) || sameKey; - - // to remember not to searchFromNextItem if selection was deselected - // losing keyboard search context basically - m_isSearchRestarted = false; + const bool searchFromNextItem = newSearch || sameKey; Q_EMIT changeCurrentItem(sameKey ? firstKey : m_searchedString, searchFromNextItem); } @@ -80,7 +75,6 @@ qint64 KItemListKeyboardSearchManager::timeout() const void KItemListKeyboardSearchManager::cancelSearch() { - m_isSearchRestarted = true; m_searchedString.clear(); } diff --git a/src/kitemviews/private/kitemlistkeyboardsearchmanager.h b/src/kitemviews/private/kitemlistkeyboardsearchmanager.h index 981d98cd3..5cb8effbf 100644 --- a/src/kitemviews/private/kitemlistkeyboardsearchmanager.h +++ b/src/kitemviews/private/kitemlistkeyboardsearchmanager.h @@ -72,7 +72,6 @@ private: bool shouldClearSearchIfInputTimeReached(); QString m_searchedString; - bool m_isSearchRestarted; /** Measures the time since the last key press. */ QElapsedTimer m_keyboardInputTime; /** Time in milliseconds in which a key press is considered as a continuation of the previous search input. */ -- cgit v1.3