diff options
Diffstat (limited to 'src/kitemviews')
| -rw-r--r-- | src/kitemviews/kitemlistcontroller.cpp | 6 | ||||
| -rw-r--r-- | src/kitemviews/kitemlistcontroller.h | 2 | ||||
| -rw-r--r-- | src/kitemviews/private/kitemlistkeyboardsearchmanager.cpp | 34 | ||||
| -rw-r--r-- | src/kitemviews/private/kitemlistkeyboardsearchmanager.h | 3 |
4 files changed, 32 insertions, 13 deletions
diff --git a/src/kitemviews/kitemlistcontroller.cpp b/src/kitemviews/kitemlistcontroller.cpp index fdde48abc..7971de5b7 100644 --- a/src/kitemviews/kitemlistcontroller.cpp +++ b/src/kitemviews/kitemlistcontroller.cpp @@ -58,7 +58,7 @@ KItemListController::KItemListController(KItemModelBase *model, KItemListView *v , m_keyboardAnchorIndex(-1) , m_keyboardAnchorPos(0) { - connect(m_keyboardManager, &KItemListKeyboardSearchManager::changeCurrentItem, this, &KItemListController::slotChangeCurrentItem); + connect(m_keyboardManager, &KItemListKeyboardSearchManager::changeCurrentItem, this, &KItemListController::slotChangeCurrentItem, Qt::DirectConnection); connect(m_selectionManager, &KItemListSelectionManager::currentChanged, m_keyboardManager, &KItemListKeyboardSearchManager::slotCurrentChanged); connect(m_selectionManager, &KItemListSelectionManager::selectionChanged, m_keyboardManager, &KItemListKeyboardSearchManager::slotSelectionChanged); @@ -515,8 +515,9 @@ bool KItemListController::keyPressEvent(QKeyEvent *event) return true; } -void KItemListController::slotChangeCurrentItem(const QString &text, bool searchFromNextItem) +void KItemListController::slotChangeCurrentItem(const QString &text, bool searchFromNextItem, bool *found) { + *found = false; if (!m_model || m_model->count() == 0) { return; } @@ -542,6 +543,7 @@ void KItemListController::slotChangeCurrentItem(const QString &text, bool search } m_view->scrollToItem(index, KItemListView::ViewItemPosition::Beginning); + *found = true; } } diff --git a/src/kitemviews/kitemlistcontroller.h b/src/kitemviews/kitemlistcontroller.h index 48da07206..48a518610 100644 --- a/src/kitemviews/kitemlistcontroller.h +++ b/src/kitemviews/kitemlistcontroller.h @@ -233,7 +233,7 @@ private Q_SLOTS: */ void slotRubberBandChanged(); - void slotChangeCurrentItem(const QString &text, bool searchFromNextItem); + void slotChangeCurrentItem(const QString &text, bool searchFromNextItem, bool *found); void slotAutoActivationTimeout(); diff --git a/src/kitemviews/private/kitemlistkeyboardsearchmanager.cpp b/src/kitemviews/private/kitemlistkeyboardsearchmanager.cpp index afc3bb071..f2f834c4c 100644 --- a/src/kitemviews/private/kitemlistkeyboardsearchmanager.cpp +++ b/src/kitemviews/private/kitemlistkeyboardsearchmanager.cpp @@ -35,6 +35,7 @@ void KItemListKeyboardSearchManager::addKeys(const QString &keys) { if (shouldClearSearchIfInputTimeReached()) { m_searchedString.clear(); + m_lastSuccessfulSearch.clear(); } const bool newSearch = m_searchedString.isEmpty(); @@ -48,17 +49,32 @@ void KItemListKeyboardSearchManager::addKeys(const QString &keys) if (!keys.isEmpty()) { m_searchedString.append(keys); - // Special case: - // If the same key is pressed repeatedly, the next item matching that key should be highlighted - const QChar firstKey = m_searchedString.length() > 0 ? m_searchedString.at(0) : QChar(); - const bool sameKey = m_searchedString.length() > 1 && m_searchedString.count(firstKey) == m_searchedString.length(); + const QChar firstChar = m_searchedString.at(0); + const bool allSameChars = m_searchedString.length() > 1 && + m_searchedString.count(firstChar) == m_searchedString.length(); - // Searching for a matching item should start from the next item if either - // 1. a new search is started, or - // 2. a 'repeated key' search is done. - const bool searchFromNextItem = newSearch || sameKey; + // Overall strategy: + // 1. First attempt full string matching for exact file names + // 2. If full match fails and user is typing repeating characters, + // fall back to rapid navigation using either: + // - Last successful search string (for extended searches like "444" -> "4444") + // - First character only (original rapid navigation behavior) + bool found = false; + Q_EMIT changeCurrentItem(m_searchedString, newSearch, &found); - Q_EMIT changeCurrentItem(sameKey ? firstKey : m_searchedString, searchFromNextItem); + if (found) { + m_lastSuccessfulSearch = m_searchedString; + } else if (allSameChars && !newSearch) { + QString rapidSearchString; + + if (!m_lastSuccessfulSearch.isEmpty() && m_searchedString.startsWith(m_lastSuccessfulSearch)) { + rapidSearchString = m_lastSuccessfulSearch; + } else { + rapidSearchString = QString(firstChar); + } + + Q_EMIT changeCurrentItem(rapidSearchString, true, &found); + } } m_keyboardInputTime.start(); } diff --git a/src/kitemviews/private/kitemlistkeyboardsearchmanager.h b/src/kitemviews/private/kitemlistkeyboardsearchmanager.h index 5cb8effbf..4437f97b4 100644 --- a/src/kitemviews/private/kitemlistkeyboardsearchmanager.h +++ b/src/kitemviews/private/kitemlistkeyboardsearchmanager.h @@ -66,7 +66,7 @@ Q_SIGNALS: */ // TODO: Think about getting rid of the bool parameter // (see https://doc.qt.io/archives/qq/qq13-apis.html#thebooleanparametertrap) - void changeCurrentItem(const QString &string, bool searchFromNextItem); + void changeCurrentItem(const QString &string, bool searchFromNextItem, bool *found); private: bool shouldClearSearchIfInputTimeReached(); @@ -76,6 +76,7 @@ private: QElapsedTimer m_keyboardInputTime; /** Time in milliseconds in which a key press is considered as a continuation of the previous search input. */ qint64 m_timeout; + QString m_lastSuccessfulSearch; }; #endif |
