Skip to content

Commit

Permalink
Open Default Suggestion When No Selection
Browse files Browse the repository at this point in the history
Search Bar opens the default suggestion when user did not select any suggestions and presses enter.
  • Loading branch information
ShaopengLin committed Oct 31, 2024
1 parent 0585c34 commit 1ea0222
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
18 changes: 14 additions & 4 deletions src/searchbar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,15 @@ SearchBarLineEdit::SearchBarLineEdit(QWidget *parent) :
});
connect(this, &QLineEdit::returnPressed, this, [=]() {
m_returnPressed = true;

/* Open default index when nothing is selected.
Key_Return can be pressed during typing, where suggestions no longer
match the text typed. Hence the suggestionsValid check.
*/
const bool suggestionsValid = m_suggestionsAreValidFor == m_searchbarInput;
if (!m_suggestionView->currentIndex().isValid() && suggestionsValid)
openCompletion(getDefaulSuggestionIndex());
});

auto app = KiwixApp::instance();
Expand Down Expand Up @@ -312,10 +321,9 @@ void SearchBarLineEdit::onInitialSuggestions(int)
} else {
m_completer.complete(getCompleterRect());

/* Make row 0 appear but do not highlight it */
const auto completerFirstIdx = m_suggestionView->model()->index(0, 0);
/* Select nothing by default */
const auto completerSelModel = m_suggestionView->selectionModel();
completerSelModel->setCurrentIndex(completerFirstIdx, QItemSelectionModel::Current);
completerSelModel->setCurrentIndex(QModelIndex(), QItemSelectionModel::Current);
}
}

Expand All @@ -330,14 +338,16 @@ void SearchBarLineEdit::onAdditionalSuggestions(int start)
void SearchBarLineEdit::fetchSuggestions(NewSuggestionHandlerFuncPtr callback)
{
const int start = m_suggestionModel.countOfRegularSuggestions();
const auto suggestionWorker = new SuggestionListWorker(m_searchbarInput, m_token, start, this);
const auto searchText = m_searchbarInput;
const auto suggestionWorker = new SuggestionListWorker(searchText, m_token, start, this);
connect(suggestionWorker, &SuggestionListWorker::searchFinished, this,
[=] (const QList<SuggestionData>& suggestionList, int token) {
if (token != m_token) {
return;
}

m_suggestionModel.append(suggestionList);
m_suggestionsAreValidFor = searchText;
const int listSize = suggestionList.size();
const bool hasFullText = listSize > 0 && suggestionList.back().isFullTextSearchSuggestion();
const int maxFetchSize = SuggestionListWorker::getFetchSize() + hasFullText;
Expand Down
1 change: 1 addition & 0 deletions src/searchbar.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public slots:
QCompleter m_completer;
QString m_title;
QString m_searchbarInput;
QString m_suggestionsAreValidFor;
bool m_returnPressed = false;
QTimer* mp_typingTimer;
int m_token;
Expand Down

0 comments on commit 1ea0222

Please sign in to comment.