Skip to content

Truly independent web browser

LibWebView: Keep the omnibox's highlighted row at the top

Andreas Klingf6456a55afed
diff --git a/Libraries/LibWebView/Omnibox.cpp b/Libraries/LibWebView/Omnibox.cpp
index c01ef9e1f3b..3c4a05f0ef7 100644
--- a/Libraries/LibWebView/Omnibox.cpp
+++ b/Libraries/LibWebView/Omnibox.cpp
@@ -139,6 +139,14 @@ static Optional<size_t> index_of_exact_search_suggestion(String const& query, Ve
});
}
+// The row for the text as typed: the verbatim row, or else the search for the text.
+static Optional<size_t> index_of_typed_text_suggestion(String const& query, Vector<AutocompleteSuggestion> const& suggestions)
+{
+ if (auto index = suggestions.find_first_index_if([](auto const& suggestion) { return suggestion.is_verbatim; }); index.has_value())
+ return index;
+ return index_of_exact_search_suggestion(query, suggestions);
+}
+
Omnibox::Omnibox(NonnullOwnPtr<OmniboxSuggestionProvider> provider)
: m_provider(move(provider))
{
@@ -169,6 +177,7 @@ void Omnibox::begin_editing(String text)
close_popup();
m_suggestions.clear();
m_popup_query_id = {};
+ m_popup_query = {};
}
void Omnibox::end_editing()
@@ -199,6 +208,7 @@ void Omnibox::show_all_suggestions()
adopt_display_text_as_query();
m_completion_suppression = m_query;
m_popup_query_id = {};
+ m_popup_query = {};
start_query();
}
@@ -220,9 +230,19 @@ void Omnibox::text_edited(String text, bool cursor_at_end)
if (m_completion_suppression.has<SuppressionArmedByDelete>()) {
// The user deleted part of the text; leave it alone until the query changes again.
- if (had_completion || had_preview)
- m_user_rejected_completion = true;
m_completion_suppression = m_query;
+ if (had_completion || had_preview) {
+ m_user_rejected_completion = true;
+ // NB: Return now goes to the text as typed, so its row leads and is highlighted.
+ if (m_popup_visible && m_popup_query_id.has_value() && m_popup_query == m_query) {
+ move_typed_text_row_to_front(m_suggestions);
+ m_selection = index_of_typed_text_suggestion(m_query, m_suggestions).map([](auto index) {
+ return Selection { index, Selection::Origin::Automatic };
+ });
+ if (on_suggestions_change)
+ on_suggestions_change();
+ }
+ }
} else if (auto const* suppressed_query = m_completion_suppression.get_pointer<String>(); suppressed_query && *suppressed_query != m_query) {
m_completion_suppression = Empty {};
}
@@ -322,12 +342,14 @@ void Omnibox::received_suggestions(AutocompleteQueryID query_id, Vector<Autocomp
}
stabilize_automatic_default(suggestions, is_same_generation_refresh);
+ move_typed_text_row_to_front(suggestions);
Optional<size_t> preserved_selection;
if (previous_explicit_selection)
preserved_selection = index_of_suggestion(*previous_explicit_selection, suggestions);
m_popup_query_id = query_id;
+ m_popup_query = m_query;
m_suggestions = move(suggestions);
if (preserved_selection.has_value()) {
@@ -362,6 +384,22 @@ void Omnibox::received_suggestions(AutocompleteQueryID query_id, Vector<Autocomp
}
}
+// When Return goes to the text as typed, because the user rejected a completion or because the top row may not be
+// selected automatically, the row for that text comes first. The highlighted row is then always the top row.
+void Omnibox::move_typed_text_row_to_front(Vector<AutocompleteSuggestion>& suggestions) const
+{
+ auto top_row_is_not_automatic = !suggestions.is_empty() && !suggestions.first().can_be_automatically_selected;
+ if (!return_goes_to_typed_text_regardless_of_suggestions() && !top_row_is_not_automatic)
+ return;
+ if (auto index = index_of_typed_text_suggestion(m_query, suggestions); index.has_value())
+ move_suggestion_to_front(suggestions, *index);
+}
+
+bool Omnibox::return_goes_to_typed_text_regardless_of_suggestions() const
+{
+ return m_user_rejected_completion && completion_is_suppressed();
+}
+
void Omnibox::stabilize_automatic_default(Vector<AutocompleteSuggestion>& suggestions, bool is_same_generation_refresh) const
{
auto challenger_index = index_of_automatic_default(suggestions);
@@ -421,9 +459,10 @@ Optional<size_t> Omnibox::update_completion_for_suggestions(Vector<AutocompleteS
if (suggestions.is_empty())
return {};
- if (!suggestions.first().can_be_automatically_selected) {
+ // NB: Return goes to the text as typed here, so its row is highlighted, if there is one.
+ if (return_goes_to_typed_text_regardless_of_suggestions() || !suggestions.first().can_be_automatically_selected) {
restore_query_display();
- return index_of_exact_search_suggestion(m_query, suggestions);
+ return index_of_typed_text_suggestion(m_query, suggestions);
}
// A literal URL always wins: no completion, restore the typed text.
@@ -496,7 +535,11 @@ Optional<size_t> Omnibox::suggestion_index_for_return() const
// The user edited or deleted a completion, so submit the text as typed instead of the popup's
// automatically selected row. Rows the user chose deliberately still win.
if (m_user_rejected_completion) {
- if (m_popup_query_id == m_active_query_id && !completion_is_suppressed() && m_suggestions[m_selection->index].text != m_query)
+ auto const& selected = m_suggestions[m_selection->index];
+ // NB: The row for the text as typed is where Return goes, such as to choose a search engine when there is none.
+ if (selected.is_verbatim && m_popup_query_id.has_value() && m_popup_query == m_query)
+ return m_selection->index;
+ if (m_popup_query_id == m_active_query_id && !completion_is_suppressed() && selected.text != m_query)
return m_selection->index;
return {};
}
diff --git a/Libraries/LibWebView/Omnibox.h b/Libraries/LibWebView/Omnibox.h
index ac28b0eafe3..ceee35df7f4 100644
--- a/Libraries/LibWebView/Omnibox.h
+++ b/Libraries/LibWebView/Omnibox.h
@@ -49,7 +49,9 @@ public:
// The invariant that makes Enter trustworthy: a selected row is activated only when it belongs to the
// current query generation or the user selected it explicitly. When results lag behind fast typing, Enter
// immediately commits the current input through the browser's normal URL-or-search resolution. It never
-// waits for a provider response that may arrive after the user's intent has changed.
+// waits for a provider response that may arrive after the user's intent has changed. After the user rejects
+// a completion, the row for the text as typed is activated even from older rows for the same text, as it
+// goes where that text goes.
class WEBVIEW_API Omnibox {
AK_MAKE_NONCOPYABLE(Omnibox);
AK_MAKE_NONMOVABLE(Omnibox);
@@ -155,6 +157,8 @@ private:
void highlight_suggestion(size_t suggestion_index, Selection::Origin);
void display_suggestion(size_t suggestion_index);
void activate_selected_suggestion();
+ void move_typed_text_row_to_front(Vector<AutocompleteSuggestion>&) const;
+ bool return_goes_to_typed_text_regardless_of_suggestions() const;
Optional<size_t> suggestion_index_for_return() const;
void commit_suggestion(size_t suggestion_index, bool record_engagement, bool was_explicit);
void commit_suggestion_text(String, OmniboxDestinationKind);
@@ -214,6 +218,8 @@ private:
u64 m_next_query_id { 0 };
Optional<AutocompleteQueryID> m_active_query_id;
OmniboxDestinationKind m_destination_kind_for_last_commit { OmniboxDestinationKind::URL };
+ // The text the rows in the popup were made for.
+ String m_popup_query;
};
}
diff --git a/Tests/LibWebView/TestOmnibox.cpp b/Tests/LibWebView/TestOmnibox.cpp
index 4f453afb97b..de5c20fbb21 100644
--- a/Tests/LibWebView/TestOmnibox.cpp
+++ b/Tests/LibWebView/TestOmnibox.cpp
@@ -313,8 +313,13 @@ TEST_CASE(backspacing_the_completion_submits_the_typed_text)
// Backspace deletes the selected completion; the suggestion must not come back for the same query.
harness.press_backspace();
EXPECT_EQ(harness.display_text, "t"sv);
+ // Return goes to the search for the text as typed, so that row leads and is highlighted.
+ EXPECT_EQ(harness.omnibox.suggestions().first().text, "t"sv);
+ EXPECT_EQ(harness.omnibox.selected_suggestion(), 0u);
harness.provider->deliver({ history_row("https://www.thev.example/"sv), search_row("t"sv) });
EXPECT_EQ(harness.display_text, "t"sv);
+ EXPECT_EQ(harness.omnibox.suggestions().first().text, "t"sv);
+ EXPECT_EQ(harness.omnibox.selected_suggestion(), 0u);
harness.omnibox.return_pressed();
EXPECT_EQ(harness.commits.size(), 1u);
@@ -342,6 +347,24 @@ TEST_CASE(typing_after_backspace_lifts_the_suppression)
EXPECT_EQ(harness.commits.last(), "https://www.thev.example/"sv);
}
+TEST_CASE(rows_without_one_for_the_typed_text_highlight_none_after_rejecting_a_completion)
+{
+ Harness harness;
+ harness.begin_editing();
+
+ harness.press_key('t');
+ harness.provider->deliver({ history_row("https://www.thee.example/"sv), search_row("t"sv) });
+ EXPECT(harness.omnibox.is_inline_completed());
+ harness.press_backspace();
+ EXPECT_EQ(harness.display_text, "t"sv);
+
+ // Return goes to the text as typed, and the rows have no row for it, such as while the row for the text waits on
+ // the check for an app that handles it. So none of them is highlighted.
+ harness.provider->deliver({ non_inline_history_row("https://www.tango.example/"sv) });
+ EXPECT(!harness.omnibox.selected_suggestion().has_value());
+ EXPECT(!harness.omnibox.suggestion_for_return().has_value());
+}
+
TEST_CASE(the_first_intermediate_result_repaints_for_each_generation)
{
Harness harness;
@@ -723,7 +746,8 @@ TEST_CASE(a_top_row_that_does_not_match_the_prefix_is_not_automatically_selected
harness.press_key('t');
harness.provider->deliver({ non_automatic_history_row("https://odd.example/t"sv, "Odd"sv), search_row("t"sv) });
EXPECT_EQ(harness.display_text, "t"sv);
- EXPECT_EQ(harness.omnibox.selected_suggestion(), 1u);
+ EXPECT_EQ(harness.omnibox.selected_suggestion(), 0u);
+ EXPECT_EQ(harness.omnibox.suggestions().first().text, "t"sv);
harness.omnibox.return_pressed();
EXPECT_EQ(harness.commits.last(), "t"sv);
@@ -739,7 +763,8 @@ TEST_CASE(a_title_only_history_row_is_not_automatically_selected)
harness.provider->deliver({ non_automatic_history_row("https://example.com/fragrance"sv, "Eau de Parfum"sv), search_row("eau de"sv) });
EXPECT_EQ(harness.display_text, "eau de"sv);
- EXPECT_EQ(harness.omnibox.selected_suggestion(), 1u);
+ EXPECT_EQ(harness.omnibox.selected_suggestion(), 0u);
+ EXPECT_EQ(harness.omnibox.suggestions().first().text, "eau de"sv);
EXPECT(harness.omnibox.is_popup_visible());
harness.omnibox.return_pressed();
@@ -758,7 +783,7 @@ TEST_CASE(a_user_chosen_title_only_history_row_is_activated)
harness.provider->deliver({ non_automatic_history_row("https://example.com/fragrance"sv, "Eau de Parfum"sv), search_row("eau de"sv) });
EXPECT(harness.omnibox.select_next_suggestion());
- EXPECT_EQ(harness.omnibox.selected_suggestion(), 0u);
+ EXPECT_EQ(harness.omnibox.selected_suggestion(), 1u);
EXPECT_EQ(harness.display_text, "https://example.com/fragrance"sv);
harness.omnibox.return_pressed();
@@ -781,7 +806,8 @@ TEST_CASE(a_fresh_non_prefix_url_top_row_does_not_win_after_editing_a_completion
harness.press_key(code_point);
harness.provider->deliver({ non_automatic_history_row("https://news.ycombinator.com/title-match"sv, "Title Match"sv), search_row("title match"sv) });
EXPECT_EQ(harness.display_text, "title match"sv);
- EXPECT_EQ(harness.omnibox.selected_suggestion(), 1u);
+ EXPECT_EQ(harness.omnibox.selected_suggestion(), 0u);
+ EXPECT_EQ(harness.omnibox.suggestions().first().text, "title match"sv);
harness.omnibox.return_pressed();
EXPECT_EQ(harness.commits.size(), 1u);
@@ -1054,6 +1080,34 @@ TEST_CASE(provider_results_after_a_stale_enter_do_not_navigate)
EXPECT_EQ(harness.commits.size(), 1u);
}
+TEST_CASE(deleting_from_a_previewed_row_does_not_highlight_rows_for_older_text)
+{
+ Harness harness;
+ harness.begin_editing();
+
+ harness.press_key('e');
+ harness.press_key('x');
+ auto search = search_row("ex"sv);
+ search.is_verbatim = true;
+ harness.provider->deliver({ move(search), non_automatic_history_row("https://other.example/foo"sv, "Ex"sv) });
+ EXPECT(harness.omnibox.select_next_suggestion());
+ auto previewed_text = harness.display_text;
+ EXPECT_NE(previewed_text, "ex"sv);
+
+ // The user moves the cursor to the end of the previewed text, and deletes a character from it.
+ harness.selection_start = {};
+ harness.omnibox.cursor_moved(true);
+ harness.press_backspace();
+ EXPECT_NE(harness.omnibox.query(), "ex"sv);
+ auto const& rows = harness.omnibox.suggestions();
+ if (auto selection = harness.omnibox.selected_suggestion(); selection.has_value())
+ EXPECT_NE(rows[*selection].text, "ex"sv);
+
+ harness.omnibox.return_pressed();
+ EXPECT_EQ(harness.commits.size(), 1u);
+ EXPECT_NE(harness.commits.last(), "ex"sv);
+}
+
TEST_CASE(restarting_the_session_cancels_in_flight_queries)
{
Harness harness;