Truly independent web browser
LibWebView: Let Enter go to the inline completion while rows load
Andreas Klingaa4f588caeec
diff --git a/Documentation/Omnibox.md b/Documentation/Omnibox.mdindex a0237fbb17b..e0234b1a52f 100644--- a/Documentation/Omnibox.md+++ b/Documentation/Omnibox.md@@ -259,8 +259,10 @@ An explicitly selected row remains selected across a refresh of the same generatwith the same destination still exists. If it disappears, its preview is removed. Automaticselection is recalculated separately.-Enter never waits for suggestions. If the displayed rows belong to an older query generation, the-current input is immediately committed through normal URL-or-search resolution.+Enter never waits for suggestions. If the displayed rows belong to an older query generation, Enter+goes to the inline completion on display, since every key press checks that it still extends the+text as typed. Without one, the current input is immediately committed through normal+URL-or-search resolution. Either way, Enter goes where the field shows, however soon it is pressed.## Persistence and deletiondiff --git a/Libraries/LibWebView/Omnibox.cpp b/Libraries/LibWebView/Omnibox.cppindex a9a6be22d4f..4022b8754ea 100644--- a/Libraries/LibWebView/Omnibox.cpp+++ b/Libraries/LibWebView/Omnibox.cpp@@ -262,18 +262,25 @@ void Omnibox::text_edited(String text, bool cursor_at_end)if (had_preview)m_user_rejected_completion = true;+ auto kept_completion = false;if (!completion_is_suppressed() && !m_is_suspended && previous_completion_suggestion.has_value()) {if (suggestion_matches_query_exactly(m_query, *previous_completion_suggestion)) {// The user typed out the whole suggestion; there is nothing left to complete.} else if (auto completion = inline_completion_for_suggestion(m_query, *previous_completion_suggestion); !completion.is_empty()) {// The user typed further into the completion; keep it.apply_completion(previous_completion_suggestion.release_value(), move(completion));+ kept_completion = true;} else {// The user typed over the completion; Enter must now submit the text as typed.m_user_rejected_completion = true;}}+ // NB: Editing drops a row the user chose, unless they typed further into its completion. This comes last, since the+ // chrome reads where Return goes when told.+ if (selection_is_explicit() && !kept_completion)+ set_selection({});+start_query();}@@ -537,8 +544,9 @@ void Omnibox::return_pressed()return;}- if (suggestion_index_for_return().has_value()) {- activate_selected_suggestion();+ if (auto index = suggestion_index_for_return(); index.has_value()) {+ auto was_explicit = selection_is_explicit() && m_selection->index == *index;+ commit_suggestion(*index, true, was_explicit);return;}@@ -550,9 +558,20 @@ void Omnibox::return_pressed()Optional<size_t> Omnibox::suggestion_index_for_return() const{- if (!m_popup_visible || !m_selection.has_value())+ if (!m_popup_visible)return {};+ // NB: Every key press checks that a completion on display still extends the text, so Return goes to it even+ // while rows load.+ auto index_of_completion_on_display = [&] -> Optional<size_t> {+ if (is_inline_completed() && m_completion_suggestion.has_value())+ return index_of_suggestion(*m_completion_suggestion, m_suggestions);+ return {};+ };++ if (!m_selection.has_value())+ return index_of_completion_on_display();+if (selection_is_explicit())return m_selection->index;@@ -572,7 +591,7 @@ Optional<size_t> Omnibox::suggestion_index_for_return() const// stale, commit the exact current input through the browser's normal URL-or-search resolution.if (m_popup_query_id == m_active_query_id)return m_selection->index;- return {};+ return index_of_completion_on_display();}Optional<AutocompleteSuggestion const&> Omnibox::suggestion_for_return() const@@ -678,19 +697,6 @@ void Omnibox::display_suggestion(size_t suggestion_index)apply_preview(suggestion_text);}-void Omnibox::activate_selected_suggestion()-{- if (m_selection.has_value()) {- auto was_explicit = m_selection->origin == Selection::Origin::ExplicitChoice;- commit_suggestion(m_selection->index, true, was_explicit);- return;- }-- restore_query_display();- close_popup();- commit_verbatim(m_query);-}-void Omnibox::commit_suggestion(size_t suggestion_index, bool should_record_engagement, bool was_explicit){auto suggestion = m_suggestions[suggestion_index];@@ -781,12 +787,6 @@ Omnibox::EditingIcon Omnibox::editing_icon() constreturn { .kind = EditingIcon::Kind::Page, .favicon_png = {} };}-bool Omnibox::should_keep_editing_icon() const-{- // NB: After the user chose a row, or typed over or deleted a completion, the icon already shows where Return goes.- return has_pending_suggestions() && !m_display_text.is_empty() && !selection_is_explicit() && !m_user_rejected_completion;-}-Optional<String> Omnibox::completion_title() const{if (m_provenance != TextProvenance::InlineCompleted || !m_completion_suggestion.has_value())diff --git a/Libraries/LibWebView/Omnibox.h b/Libraries/LibWebView/Omnibox.hindex 4587093842c..db9f4270b56 100644--- a/Libraries/LibWebView/Omnibox.h+++ b/Libraries/LibWebView/Omnibox.h@@ -48,11 +48,12 @@ public:// selected. The query is retained and restored when the popup goes away.//// 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. 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.+// current query generation, the user selected it explicitly, or it is the inline completion on display.+// 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.+// When results lag behind fast typing, Enter otherwise 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.class WEBVIEW_API Omnibox {AK_MAKE_NONCOPYABLE(Omnibox);AK_MAKE_NONMOVABLE(Omnibox);@@ -128,9 +129,6 @@ public:Optional<ByteBuffer const&> favicon_png;};EditingIcon editing_icon() const;- // Each key press asks for new suggestions, and until they arrive, Return would go to the text as typed. The icon- // stays as it is meanwhile, rather than flickering on each key, unless Return goes to the text as typed anyway.- bool should_keep_editing_icon() const;// Callbacks to the chrome:Function<void(Display const&)> on_display_change;@@ -174,7 +172,6 @@ private:bool select_adjacent_suggestion(StepDirection);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;diff --git a/Tests/LibWebView/TestOmnibox.cpp b/Tests/LibWebView/TestOmnibox.cppindex 029ff7b6618..fcc0c1e8831 100644--- a/Tests/LibWebView/TestOmnibox.cpp+++ b/Tests/LibWebView/TestOmnibox.cpp@@ -168,12 +168,25 @@ struct Harness {if (omnibox.is_popup_visible() && !rows_match(visible_rows, omnibox.suggestions()))fail("the chrome shows different rows than the model"sv);- if (!omnibox.is_editing() || !omnibox.is_popup_visible() || omnibox.has_pending_suggestions())+ if (!omnibox.is_editing() || !omnibox.is_popup_visible())+ return;++ auto return_target = omnibox.suggestion_for_return();++ // Text completed inline goes where Return goes, and only the completion is selected, even while new rows are+ // on their way.+ if (omnibox.is_inline_completed()) {+ if (!return_target.has_value() || !return_target->text.bytes_as_string_view().contains(display_text, CaseSensitivity::CaseInsensitive))+ fail("Return ignores the inline completion on display"sv);+ if (!selection_start.has_value() || !display_text.bytes_as_string_view().starts_with(omnibox.query().bytes_as_string_view(), CaseSensitivity::CaseInsensitive))+ fail("the inline completion does not extend the text as typed"sv);+ }++ if (omnibox.has_pending_suggestions())return;auto const& rows = omnibox.suggestions();auto selection = omnibox.selected_suggestion();- auto return_target = omnibox.suggestion_for_return();if (selection.has_value()) {if (*selection >= rows.size()) {@@ -195,14 +208,6 @@ struct Harness {if (!omnibox.selection_is_explicit() && *selection != 0)fail("an automatically highlighted row is not the top row"sv);}-- // Text completed inline goes where Return goes, and only the completion is selected.- if (omnibox.is_inline_completed()) {- if (!return_target.has_value())- fail("Return ignores the inline completion on display"sv);- if (!selection_start.has_value() || !display_text.bytes_as_string_view().starts_with(omnibox.query().bytes_as_string_view(), CaseSensitivity::CaseInsensitive))- fail("the inline completion does not extend the text as typed"sv);- }}bool is_search_for_query(AutocompleteSuggestion const& suggestion) const@@ -618,6 +623,26 @@ TEST_CASE(explicit_selection_survives_same_generation_reordering)EXPECT(harness.provider->engagements.last().was_explicit);}+TEST_CASE(typing_into_a_chosen_completion_keeps_it_chosen)+{+ Harness harness;+ harness.begin_editing();++ harness.press_key('g');+ harness.provider->deliver({ search_row("g"sv), history_row("https://gitlab.example/"sv) });+ EXPECT(harness.omnibox.select_next_suggestion());+ EXPECT_EQ(harness.display_text, "gitlab.example"sv);++ // The user typed further into the row they chose, so it is still their choice until the rows for the text arrive.+ harness.press_key('i');+ EXPECT_EQ(harness.display_text, "gitlab.example"sv);+ EXPECT(harness.omnibox.selection_is_explicit());++ harness.omnibox.return_pressed();+ EXPECT_EQ(harness.commits.last(), "https://gitlab.example/"sv);+ EXPECT(harness.provider->engagements.last().was_explicit);+}+TEST_CASE(search_selection_survives_equivalent_text_normalization){Harness harness;@@ -643,7 +668,7 @@ TEST_CASE(search_selection_survives_equivalent_text_normalization)EXPECT(harness.provider->engagements.last().was_explicit);}-TEST_CASE(enter_with_stale_results_commits_the_current_input_immediately)+TEST_CASE(enter_with_stale_results_goes_to_the_completion_on_display_immediately){Harness harness;harness.begin_editing();@@ -653,11 +678,11 @@ TEST_CASE(enter_with_stale_results_commits_the_current_input_immediately)harness.press_key('h');EXPECT_EQ(harness.display_text, "thee.example"sv);- // Enter with a stale popup never waits for a provider response that may arrive after the user's- // intent has changed.+ // Enter with a stale popup never waits for a provider response. It goes where the field shows, as it would once+ // the rows arrive.harness.omnibox.return_pressed();EXPECT_EQ(harness.commits.size(), 1u);- EXPECT_EQ(harness.commits.last(), "th"sv);+ EXPECT_EQ(harness.commits.last(), "https://www.thee.example/"sv);// Later deliveries may update the popup if editing is still active, but cannot trigger a second// navigation.@@ -666,6 +691,41 @@ TEST_CASE(enter_with_stale_results_commits_the_current_input_immediately)EXPECT_EQ(harness.commits.size(), 1u);}+TEST_CASE(enter_with_stale_results_and_no_completion_commits_the_current_input_immediately)+{+ Harness harness;+ harness.begin_editing();++ harness.press_key('t');+ harness.provider->deliver({ history_row("https://www.thee.example/"sv), search_row("t"sv) });+ harness.press_key('x');+ EXPECT_EQ(harness.display_text, "tx"sv);++ harness.omnibox.return_pressed();+ EXPECT_EQ(harness.commits.size(), 1u);+ EXPECT_EQ(harness.commits.last(), "tx"sv);++ harness.provider->deliver({ search_row("tx"sv) });+ EXPECT_EQ(harness.commits.size(), 1u);+}++TEST_CASE(enter_with_stale_results_after_deleting_the_completion_commits_the_current_input)+{+ Harness harness;+ harness.begin_editing();++ harness.press_key('t');+ harness.press_key('h');+ harness.provider->deliver({ history_row("https://www.thee.example/"sv), search_row("th"sv) });+ EXPECT_EQ(harness.display_text, "thee.example"sv);++ harness.press_backspace();+ EXPECT_EQ(harness.display_text, "th"sv);+ harness.omnibox.return_pressed();+ EXPECT_EQ(harness.commits.size(), 1u);+ EXPECT_EQ(harness.commits.last(), "th"sv);+}+TEST_CASE(escape_rejects_an_automatic_completion_and_closes_the_popup){Harness harness;@@ -1177,7 +1237,7 @@ TEST_CASE(provider_results_after_a_stale_enter_do_not_navigate)harness.press_key('h');harness.omnibox.return_pressed();EXPECT_EQ(harness.commits.size(), 1u);- EXPECT_EQ(harness.commits.last(), "th"sv);+ EXPECT_EQ(harness.commits.last(), "https://www.thee.example/"sv);// Provider updates may continue until the chrome's commit handler ends editing, but no response is// armed to perform another navigation.@@ -1209,9 +1269,11 @@ TEST_CASE(deleting_from_a_previewed_row_does_not_highlight_rows_for_older_text)if (auto selection = harness.omnibox.selected_suggestion(); selection.has_value())EXPECT_NE(rows[*selection].text, "ex"sv);+ // Return goes to the edited text, not to the row it was edited from.+ auto edited_text = harness.display_text;harness.omnibox.return_pressed();EXPECT_EQ(harness.commits.size(), 1u);- EXPECT_NE(harness.commits.last(), "ex"sv);+ EXPECT_EQ(harness.commits.last(), edited_text);}TEST_CASE(a_completion_for_fewer_rows_than_the_chosen_row_does_not_read_past_them)@@ -1854,23 +1916,23 @@ TEST_CASE(scenario_without_a_search_engine_offers_to_choose_one)EXPECT(scenario.icon_is_search());}-TEST_CASE(scenario_the_icon_holds_while_typing_through_a_completion)+TEST_CASE(scenario_the_icon_stays_while_typing_through_a_completion){Scenario scenario(reddit_profile());scenario.type("red"sv);EXPECT(scenario.icon_is_page_with_favicon());- // The next key press keeps the completion, and its suggestions have yet to arrive.+ // The next key press keeps the completion, and Return still goes to it before its suggestions arrive.scenario.harness.press_key('d');EXPECT_EQ(scenario.harness.display_text, "reddit.com"sv);- EXPECT(scenario.harness.omnibox.should_keep_editing_icon());+ EXPECT(scenario.harness.omnibox.has_pending_suggestions());+ EXPECT(scenario.icon_is_page_with_favicon());scenario.deliver();- EXPECT(!scenario.harness.omnibox.should_keep_editing_icon());EXPECT(scenario.icon_is_page_with_favicon());- // Backspacing over the completion does not keep the page's icon while the search's suggestions are on their way.+ // Backspacing over the completion shows the search at once, while its suggestions are on their way.scenario.harness.press_backspace();- EXPECT(!scenario.harness.omnibox.should_keep_editing_icon());+ EXPECT(scenario.harness.omnibox.has_pending_suggestions());EXPECT(scenario.icon_is_search());}diff --git a/UI/Qt/LocationEdit.cpp b/UI/Qt/LocationEdit.cppindex 7e38b3fb044..c287378f064 100644--- a/UI/Qt/LocationEdit.cpp+++ b/UI/Qt/LocationEdit.cpp@@ -1154,8 +1154,6 @@ void LocationEdit::update_trailing_item_positions()void LocationEdit::search_engine_settings_changed(){- // NB: The change cancels the suggestions on their way, so the editing icon cannot wait for them.- m_editing_icon_kind = {};update_location_icon();}@@ -1250,8 +1248,7 @@ void LocationEdit::update_location_icon()// OPTIMIZATION: This runs several times per key press. While the editing icon is up and set for the current// appearance, only the icon itself can change, so the button is not set up and laid out again.if (m_is_editing_location && m_shows_editing_icon && m_editing_icon_kind.has_value()) {- if (!m_omnibox.should_keep_editing_icon())- update_editing_icon();+ update_editing_icon();return;}