Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pressing ENTER in the search bar opens the first suggestion #1230

Closed
veloman-yunkan opened this issue Oct 25, 2024 · 4 comments · Fixed by #1232
Closed

Pressing ENTER in the search bar opens the first suggestion #1230

veloman-yunkan opened this issue Oct 25, 2024 · 4 comments · Fixed by #1232
Assignees
Milestone

Comments

@veloman-yunkan
Copy link
Collaborator

#1224 broke the handling of pressing ENTER in the search bar - it always opens the first suggestion (even if it doesn't fully match the text in the searchbar).

Originally posted by @veloman-yunkan in #1224 (comment)

@ShaopengLin
Copy link
Collaborator

ShaopengLin commented Oct 25, 2024

@veloman-yunkan Since we opened this issue I am continuing discussion here. From #1189 (comment), it is found that QCompleter's activate signal is not sent on the invalid index so it doesn't work. This approach is inherently wrong since during typing, the m
odel is not cleared, which means not checking text match in openCompletion will cause us to open from no longer relevant suggestions.

Regardless, to ensure correctness, openCompletion() needs to check the text content before opening the index. One change could be that we make getDefaultSuggestionIndex more generalized:

QModelIndex SearchBarLineEdit::getRowOrDefaultIndex(int row) const
{
    const auto rowIndex = m_suggestionModel.index(row);
    ...
}

Then, we adapt openCompletion:

/* In .h */
void openCompletion(const QModelIndex& index = QModelIndex());
/* In .h */

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

void SearchBarLineEdit::onInitialSuggestions(int)
{
    if (m_returnPressed) {
        openCompletion();
    ...
}

@veloman-yunkan
Copy link
Collaborator Author

Another approach is to track if suggestions are valid (have already been produced) for the current content of the searchbox. If so, then QLineEdit::returnPressed should immediately result in openCompletion(getDefaulSuggestionIndex());

@ShaopengLin
Copy link
Collaborator

@veloman-yunkan This would result in the issue I described above:

the model is not cleared until typing is finished, which means not checking text match in openCompletion will cause us to open from no longer relevant suggestions.

If we open in returnPressed, then we might open a suggestion during typing which is exactly what the m_returnPressed is trying to avoid.

@veloman-yunkan
Copy link
Collaborator Author

@ShaopengLin That's why my proposal is centered around tracking relevance of suggestions - though I used a somewhat ambiguous adjective valid, which may have confused you, I meant it in the meaning relevant. The parenthesized fragment may have added to the confusion. Please reread my idea as follows

Another approach is to track if suggestions are valid/relevant for the current content of the searchbox

With more detailed outline for a possible implementation:

  1. Add a data member m_suggestionsAreValidFor that holds the searchbox text for which the most recent call of onInitialSuggestions() has taken place
  2. In QLineEdit::returnPressed check if the current content of the searchbox matches m_suggestionsAreValidFor. If yes, call openCompletion(getDefaulSuggestionIndex()); immediately.

That's the main idea. You may have to take into account a couple of edge cases (like the searchbox being empty, etc).

But I would like to see both fixes (as separate commits in one PR) so that we can select the better one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants