From 67a0d6eae6d3e76462ccb1eafe178779b1394ad9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C4=B1rat=20K=C4=B1z=C4=B1lbo=C4=9Fa?= Date: Mon, 19 Jan 2026 17:27:10 +0300 Subject: [PATCH] Browser: Share Find in Page search term across tabs This adds a global search term stored in BrowserWindow that is shared across all tabs. When opening Find in Page (Ctrl+F), the widget now uses the following priority logic: 1. If there's text selected on the page, use that 2. If the search box is empty, use the global search term 3. Otherwise, keep the existing text To imitatates Chromium's logic, the global search term is updated whenever the user types, navigates matches, or when a selection populates the search box. --- Userland/Applications/Browser/BrowserWindow.h | 6 ++++ .../Applications/Browser/FindInPageWidget.cpp | 29 +++++++++++++++++-- .../Applications/Browser/FindInPageWidget.h | 6 +++- Userland/Applications/Browser/Tab.cpp | 11 ++++--- 4 files changed, 43 insertions(+), 9 deletions(-) diff --git a/Userland/Applications/Browser/BrowserWindow.h b/Userland/Applications/Browser/BrowserWindow.h index 7e0d6cb1fc4..6c17db6cf68 100644 --- a/Userland/Applications/Browser/BrowserWindow.h +++ b/Userland/Applications/Browser/BrowserWindow.h @@ -10,6 +10,7 @@ #include "BookmarksBarWidget.h" #include "Tab.h" #include "WindowActions.h" +#include #include #include #include @@ -32,6 +33,9 @@ public: Tab& create_new_tab(URL::URL const&, Web::HTML::ActivateTab activate); void create_new_window(URL::URL const&); + String const& search_term() const { return m_search_term; } + void set_search_term(String const& term) { m_search_term = term; } + GUI::Action& go_back_action() { return *m_go_back_action; } GUI::Action& go_forward_action() { return *m_go_forward_action; } GUI::Action& go_home_action() { return *m_go_home_action; } @@ -96,6 +100,8 @@ private: RefPtr m_disable_user_agent_spoofing; RefPtr m_disable_search_engine_action; RefPtr m_change_homepage_action; + + String m_search_term; }; } diff --git a/Userland/Applications/Browser/FindInPageWidget.cpp b/Userland/Applications/Browser/FindInPageWidget.cpp index 40ec4a7c9b6..bcef3d75826 100644 --- a/Userland/Applications/Browser/FindInPageWidget.cpp +++ b/Userland/Applications/Browser/FindInPageWidget.cpp @@ -38,22 +38,27 @@ void FindInPageWidget::initialize(WebView::OutOfProcessWebView& web_view) m_search_textbox->on_change = [this] { find_text_changed(); + notify_search_text_changed(); }; m_search_textbox->on_return_pressed = [this] { m_web_content_view->find_in_page_next_match(); + notify_search_text_changed(); }; m_search_textbox->on_shift_return_pressed = [this] { m_web_content_view->find_in_page_previous_match(); + notify_search_text_changed(); }; m_next_button->on_click = [this](auto) { m_web_content_view->find_in_page_next_match(); + notify_search_text_changed(); }; m_previous_button->on_click = [this](auto) { m_web_content_view->find_in_page_previous_match(); + notify_search_text_changed(); }; m_close_button->on_click = [this](auto) { @@ -91,10 +96,30 @@ void FindInPageWidget::update_result_label(size_t current_match_index, Optional< } } -void FindInPageWidget::set_search_text(String const& text) +void FindInPageWidget::notify_search_text_changed() { - m_search_textbox->set_text(text); + if (on_search_text_change) { + auto text = MUST(String::from_byte_string(m_search_textbox->text())); + on_search_text_change(text); + } +} + +void FindInPageWidget::show(String const& global_term) +{ + // Priority 1: If there's a selection on the page, use that + if (auto selection = m_web_content_view->selected_text_with_whitespace_collapsed(); selection.has_value() && !selection->is_empty()) { + m_search_textbox->set_text(*selection); + notify_search_text_changed(); + } + // Priority 2: If the textbox is empty, use the global term + else if (m_search_textbox->text().is_empty() && !global_term.is_empty()) { + m_search_textbox->set_text(global_term); + } + // Priority 3: Keep existing text (do nothing) + m_search_textbox->select_all(); + set_visible(true); + set_focus(true); } void FindInPageWidget::keydown_event(GUI::KeyEvent& event) diff --git a/Userland/Applications/Browser/FindInPageWidget.h b/Userland/Applications/Browser/FindInPageWidget.h index e22167f0dc2..087a65afb38 100644 --- a/Userland/Applications/Browser/FindInPageWidget.h +++ b/Userland/Applications/Browser/FindInPageWidget.h @@ -6,6 +6,7 @@ #pragma once +#include #include #include #include @@ -24,13 +25,16 @@ public: virtual ~FindInPageWidget() override = default; void initialize(WebView::OutOfProcessWebView& web_view); - void set_search_text(String const& text); + void show(String const& global_term); + + Function on_search_text_change; private: FindInPageWidget(); virtual void keydown_event(GUI::KeyEvent&) override; void find_text_changed(); + void notify_search_text_changed(); void update_result_label(size_t current_match_index, Optional const& total_match_count); RefPtr m_web_content_view; diff --git a/Userland/Applications/Browser/Tab.cpp b/Userland/Applications/Browser/Tab.cpp index 0f5c42276b7..c0d6c04c21b 100644 --- a/Userland/Applications/Browser/Tab.cpp +++ b/Userland/Applications/Browser/Tab.cpp @@ -126,6 +126,10 @@ Tab::Tab(BrowserWindow& window) m_find_in_page_widget->initialize(*m_web_content_view); + m_find_in_page_widget->on_search_text_change = [this](String const& text) { + this->window().set_search_term(text); + }; + auto preferred_color_scheme = Web::CSS::preferred_color_scheme_from_string(Config::read_string("Browser"sv, "Preferences"sv, "ColorScheme"sv, Browser::default_color_scheme)); m_web_content_view->set_preferred_color_scheme(preferred_color_scheme); @@ -1067,12 +1071,7 @@ void Tab::enable_webdriver_mode() void Tab::show_find_in_page() { - auto selected_text = m_web_content_view->selected_text_with_whitespace_collapsed(); - if (selected_text.has_value()) - m_find_in_page_widget->set_search_text(*selected_text); - - m_find_in_page_widget->set_visible(true); - m_find_in_page_widget->set_focus(true); + m_find_in_page_widget->show(window().search_term()); } }