Skip to content

Commit

Permalink
Only Open Suggestion Matching Search Text
Browse files Browse the repository at this point in the history
Consequently will open default suggestion when nothing is selected.
  • Loading branch information
ShaopengLin committed Oct 30, 2024
1 parent caac302 commit e7e8a1f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
23 changes: 12 additions & 11 deletions src/searchbar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -298,17 +298,18 @@ void SearchBarLineEdit::onScroll(int value)

void SearchBarLineEdit::openCompletion(const QModelIndex &index)
{
if (index.isValid())
const auto openIndex = getRowOrDefaultIndex(index.isValid() ? index.row() : 0);
if (openIndex.isValid())
{
const QUrl url = index.data(Qt::UserRole).toUrl();
const QUrl url = openIndex.data(Qt::UserRole).toUrl();
QTimer::singleShot(0, [=](){KiwixApp::instance()->openUrl(url, false);});
}
}

void SearchBarLineEdit::onInitialSuggestions(int)
{
if (m_returnPressed) {
openCompletion(getDefaulSuggestionIndex());
openCompletion();
} else {
m_completer.complete(getCompleterRect());

Expand Down Expand Up @@ -348,17 +349,17 @@ void SearchBarLineEdit::fetchSuggestions(NewSuggestionHandlerFuncPtr callback)
suggestionWorker->start();
}

QModelIndex SearchBarLineEdit::getDefaulSuggestionIndex() const
QModelIndex SearchBarLineEdit::getRowOrDefaultIndex(int row) const
{
const auto firstSuggestionIndex = m_suggestionModel.index(0);
if (!firstSuggestionIndex.isValid())
return firstSuggestionIndex;
const auto rowIndex = m_suggestionModel.index(row);
if (!rowIndex.isValid())
return rowIndex;

/* If the first entry matches the typed text, use it as default, otherwise
/* If the row text matches the typed text, use it as default, otherwise
use the last entry if fulltext search exist. */
const auto firstSuggestionText = firstSuggestionIndex.data().toString();
if (this->text().compare(firstSuggestionText, Qt::CaseInsensitive) == 0)
return firstSuggestionIndex;
const auto rowText = rowIndex.data().toString();
if (this->text().compare(rowText, Qt::CaseInsensitive) == 0)
return rowIndex;
else if (m_suggestionModel.hasFullTextSearchSuggestion())
return m_suggestionModel.index(m_suggestionModel.rowCount() - 1);
return QModelIndex();
Expand Down
4 changes: 2 additions & 2 deletions src/searchbar.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@ private slots:
void updateCompletion();
void fetchMoreSuggestions();
void onScroll(int value);
void openCompletion(const QModelIndex& index);
void openCompletion(const QModelIndex& index = QModelIndex());
void onInitialSuggestions(int);
void onAdditionalSuggestions(int start);
void fetchSuggestions(NewSuggestionHandlerFuncPtr callback);

QModelIndex getDefaulSuggestionIndex() const;
QModelIndex getRowOrDefaultIndex(int row) const;
QRect getCompleterRect() const;
};

Expand Down

0 comments on commit e7e8a1f

Please sign in to comment.