From d33c4c751f3b69d9371e20234478b845f6cc0da4 Mon Sep 17 00:00:00 2001 From: Tim Ledbetter Date: Sun, 9 Jun 2024 18:35:32 +0100 Subject: [PATCH] LibWeb+WebContent: Provide feedback on find in page requests This change allows the results of a find in page query to be reported back to the user interface. Currently, the number of results found and the current match index are reported. --- Userland/Libraries/LibWeb/Page/Page.cpp | 27 ++++++++++++++----- Userland/Libraries/LibWeb/Page/Page.h | 10 ++++--- .../Libraries/LibWebView/ViewImplementation.h | 1 + .../Libraries/LibWebView/WebContentClient.cpp | 8 ++++++ .../Libraries/LibWebView/WebContentClient.h | 1 + .../WebContent/ConnectionFromClient.cpp | 9 ++++--- .../Services/WebContent/WebContentClient.ipc | 2 ++ 7 files changed, 46 insertions(+), 12 deletions(-) diff --git a/Userland/Libraries/LibWeb/Page/Page.cpp b/Userland/Libraries/LibWeb/Page/Page.cpp index 887bbbe66e6..ab6ade80591 100644 --- a/Userland/Libraries/LibWeb/Page/Page.cpp +++ b/Userland/Libraries/LibWeb/Page/Page.cpp @@ -548,14 +548,14 @@ void Page::clear_selection() } } -void Page::find_in_page(String const& query, CaseSensitivity case_sensitivity) +Page::FindInPageResult Page::find_in_page(String const& query, CaseSensitivity case_sensitivity) { m_find_in_page_match_index = 0; if (query.is_empty()) { m_find_in_page_matches = {}; update_find_in_page_selection(); - return; + return {}; } auto documents = HTML::main_thread_event_loop().documents_in_this_event_loop(); @@ -573,12 +573,17 @@ void Page::find_in_page(String const& query, CaseSensitivity case_sensitivity) m_find_in_page_matches.append(*match); update_find_in_page_selection(); + + return Page::FindInPageResult { + .current_match_index = m_find_in_page_match_index, + .total_match_count = m_find_in_page_matches.size(), + }; } -void Page::find_in_page_next_match() +Page::FindInPageResult Page::find_in_page_next_match() { if (m_find_in_page_matches.is_empty()) - return; + return {}; if (m_find_in_page_match_index == m_find_in_page_matches.size() - 1) { m_find_in_page_match_index = 0; @@ -587,12 +592,17 @@ void Page::find_in_page_next_match() } update_find_in_page_selection(); + + return Page::FindInPageResult { + .current_match_index = m_find_in_page_match_index, + .total_match_count = m_find_in_page_matches.size(), + }; } -void Page::find_in_page_previous_match() +Page::FindInPageResult Page::find_in_page_previous_match() { if (m_find_in_page_matches.is_empty()) - return; + return {}; if (m_find_in_page_match_index == 0) { m_find_in_page_match_index = m_find_in_page_matches.size() - 1; @@ -601,6 +611,11 @@ void Page::find_in_page_previous_match() } update_find_in_page_selection(); + + return Page::FindInPageResult { + .current_match_index = m_find_in_page_match_index, + .total_match_count = m_find_in_page_matches.size(), + }; } void Page::update_find_in_page_selection() diff --git a/Userland/Libraries/LibWeb/Page/Page.h b/Userland/Libraries/LibWeb/Page/Page.h index be1b12f0733..55c14f99ee0 100644 --- a/Userland/Libraries/LibWeb/Page/Page.h +++ b/Userland/Libraries/LibWeb/Page/Page.h @@ -181,9 +181,13 @@ public: void clear_selection(); - void find_in_page(String const& query, CaseSensitivity); - void find_in_page_next_match(); - void find_in_page_previous_match(); + struct FindInPageResult { + size_t current_match_index { 0 }; + Optional total_match_count {}; + }; + FindInPageResult find_in_page(String const& query, CaseSensitivity); + FindInPageResult find_in_page_next_match(); + FindInPageResult find_in_page_previous_match(); private: explicit Page(JS::NonnullGCPtr); diff --git a/Userland/Libraries/LibWebView/ViewImplementation.h b/Userland/Libraries/LibWebView/ViewImplementation.h index f5f0650408a..80ffe0174c2 100644 --- a/Userland/Libraries/LibWebView/ViewImplementation.h +++ b/Userland/Libraries/LibWebView/ViewImplementation.h @@ -189,6 +189,7 @@ public: Function items)> on_request_select_dropdown; Function on_finish_handling_key_event; Function on_text_test_finish; + Function const& total_match_count)> on_find_in_page; Function on_theme_color_change; Function on_insert_clipboard_entry; Function on_audio_play_state_changed; diff --git a/Userland/Libraries/LibWebView/WebContentClient.cpp b/Userland/Libraries/LibWebView/WebContentClient.cpp index 39f2356579f..c7d2da0a49c 100644 --- a/Userland/Libraries/LibWebView/WebContentClient.cpp +++ b/Userland/Libraries/LibWebView/WebContentClient.cpp @@ -78,6 +78,14 @@ void WebContentClient::did_finish_text_test(u64 page_id) } } +void WebContentClient::did_find_in_page(u64 page_id, size_t current_match_index, Optional const& total_match_count) +{ + if (auto view = view_for_page_id(page_id); view.has_value()) { + if (view->on_find_in_page) + view->on_find_in_page(current_match_index, total_match_count); + } +} + void WebContentClient::did_request_navigate_back(u64 page_id) { if (auto view = view_for_page_id(page_id); view.has_value()) { diff --git a/Userland/Libraries/LibWebView/WebContentClient.h b/Userland/Libraries/LibWebView/WebContentClient.h index 44380cfa6a8..bdf172da81b 100644 --- a/Userland/Libraries/LibWebView/WebContentClient.h +++ b/Userland/Libraries/LibWebView/WebContentClient.h @@ -95,6 +95,7 @@ private: virtual void did_request_select_dropdown(u64 page_id, Gfx::IntPoint content_position, i32 minimum_width, Vector const& items) override; virtual void did_finish_handling_input_event(u64 page_id, bool event_was_accepted) override; virtual void did_finish_text_test(u64 page_id) override; + virtual void did_find_in_page(u64 page_id, size_t current_match_index, Optional const& total_match_count) override; virtual void did_change_theme_color(u64 page_id, Gfx::Color color) override; virtual void did_insert_clipboard_entry(u64 page_id, String const& data, String const& presentation_style, String const& mime_type) override; virtual void did_change_audio_play_state(u64 page_id, Web::HTML::AudioPlayState) override; diff --git a/Userland/Services/WebContent/ConnectionFromClient.cpp b/Userland/Services/WebContent/ConnectionFromClient.cpp index 47a98f73ca0..db3e6322084 100644 --- a/Userland/Services/WebContent/ConnectionFromClient.cpp +++ b/Userland/Services/WebContent/ConnectionFromClient.cpp @@ -826,7 +826,8 @@ void ConnectionFromClient::find_in_page(u64 page_id, String const& query, CaseSe if (!page.has_value()) return; - page->page().find_in_page(query, case_sensitivity); + auto result = page->page().find_in_page(query, case_sensitivity); + async_did_find_in_page(page_id, result.current_match_index, result.total_match_count); } void ConnectionFromClient::find_in_page_next_match(u64 page_id) @@ -835,7 +836,8 @@ void ConnectionFromClient::find_in_page_next_match(u64 page_id) if (!page.has_value()) return; - page->page().find_in_page_next_match(); + auto result = page->page().find_in_page_next_match(); + async_did_find_in_page(page_id, result.current_match_index, result.total_match_count); } void ConnectionFromClient::find_in_page_previous_match(u64 page_id) @@ -844,7 +846,8 @@ void ConnectionFromClient::find_in_page_previous_match(u64 page_id) if (!page.has_value()) return; - page->page().find_in_page_previous_match(); + auto result = page->page().find_in_page_previous_match(); + async_did_find_in_page(page_id, result.current_match_index, result.total_match_count); } void ConnectionFromClient::paste(u64 page_id, String const& text) diff --git a/Userland/Services/WebContent/WebContentClient.ipc b/Userland/Services/WebContent/WebContentClient.ipc index c8c9a4f47cd..8bc6b078582 100644 --- a/Userland/Services/WebContent/WebContentClient.ipc +++ b/Userland/Services/WebContent/WebContentClient.ipc @@ -88,6 +88,8 @@ endpoint WebContentClient did_finish_text_test(u64 page_id) =| + did_find_in_page(u64 page_id, size_t current_match_index, Optional total_match_count) =| + request_worker_agent(u64 page_id) => (IPC::File socket) // FIXME: Add required attributes to select a SharedWorker Agent inspector_did_load(u64 page_id) =|