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.
This commit is contained in:
Fırat Kızılboğa authored and Nico Weber committed 2026-01-21 22:38:20 -05:00
1 parent a2d32d03c1
commit 67a0d6eae6
4 files changed
+43 -9

No files matched your search

@@ -10,6 +10,7 @@
#include "BookmarksBarWidget.h"
#include "Tab.h"
#include "WindowActions.h"
#include <AK/String.h>
#include <LibConfig/Listener.h>
#include <LibGUI/ActionGroup.h>
#include <LibGUI/Window.h>
@@ -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<GUI::Action> m_disable_user_agent_spoofing;
RefPtr<GUI::Action> m_disable_search_engine_action;
RefPtr<GUI::Action> m_change_homepage_action;
String m_search_term;
};
}
@@ -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)
@@ -6,6 +6,7 @@
#pragma once
#include <AK/Function.h>
#include <LibGUI/Button.h>
#include <LibGUI/CheckBox.h>
#include <LibGUI/Event.h>
@@ -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<void(String const&)> 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<size_t> const& total_match_count);
RefPtr<WebView::OutOfProcessWebView> m_web_content_view;
+5 -6
View File
@@ -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());
}
}