Skip to content

Truly independent web browser

LibWebView+Hub+UI/Qt: Build the actions and menus in the UI

Andreas Kling76009673551a
diff --git a/Libraries/LibWebView/Application.cpp b/Libraries/LibWebView/Application.cpp
index 034629fd614..15de0c8632b 100644
--- a/Libraries/LibWebView/Application.cpp
+++ b/Libraries/LibWebView/Application.cpp
@@ -62,7 +62,6 @@
#include <LibWebView/HelperProcess.h>
#include <LibWebView/HistoryStore.h>
#include <LibWebView/ImageDecoderManager.h>
-#include <LibWebView/Menu.h>
#include <LibWebView/NoOpMicrophoneAuthorizer.h>
#include <LibWebView/NoOpNotificationBackend.h>
#include <LibWebView/PermissionManager.h>
@@ -127,11 +126,6 @@ struct ApplicationSettingsObserver final : public SettingsObserver {
Application::the().appearance_changed({});
}
- virtual void content_settings_changed() override
- {
- Application::the().update_force_dark_action();
- }
-
virtual void tab_settings_changed() override
{
Application::the().tab_settings_changed({});
@@ -152,18 +146,6 @@ struct ApplicationSettingsObserver final : public SettingsObserver {
Application::request_server_manager().dns_settings_changed();
}
- virtual void config_variable_changed(ConfigVariableID variable) override
- {
- if (variable == ConfigVariableID::ShowAdvancedDebugMenu) {
- auto enabled = Application::settings().config_variable_as_bool(ConfigVariableID::ShowAdvancedDebugMenu);
- Application::the().debug_menu().set_visible(enabled);
- }
- if (variable == ConfigVariableID::ShowTabPerformanceMonitor) {
- auto enabled = Application::settings().config_variable_as_bool(ConfigVariableID::ShowTabPerformanceMonitor);
- Application::the().show_tab_performance_monitor_action().set_checked(enabled);
- }
- }
-
virtual void background_networking_settings_changed() override
{
Application::the().background_networking_settings_changed({});
@@ -724,7 +706,7 @@ ErrorOr<void> Application::initialize(Main::Arguments const& arguments)
start_content_blocker_list_update(ContentBlockerListUpdateTrigger::Automatic);
}
- initialize_actions();
+ initialize_page_options();
return {};
}
@@ -2555,298 +2537,99 @@ NonnullRefPtr<Core::Promise<Empty>> Application::clear_browsing_data(ClearBrowsi
return promise.release_nonnull();
}
-void Application::initialize_actions()
+void Application::initialize_page_options()
{
- auto debug_request = [this](auto request) {
- return [this, request]() {
- if (auto view = active_web_view(); view.has_value())
- view->debug_request(request);
- };
- };
-
- auto check = [](auto& action, auto request) {
- return [&action, request]() {
- ViewImplementation::for_each_view([checked = action->checked(), request](ViewImplementation& view) {
- view.debug_request(request, checked ? "on"sv : "off"sv);
- return IterationDecision::Continue;
- });
- };
- };
-
- auto add_spoofed_value = [](auto& menu, auto name, auto value, auto& cached_value, auto request, auto notify_workers) {
- auto action = Action::create_checkable(name, ActionID::SpoofUserAgent, [value, &cached_value, request, notify_workers]() {
- cached_value = value;
- // Workers take no debug requests; the worker agent manager hands the new value to every live worker.
- notify_workers();
-
- ViewImplementation::for_each_view([&](ViewImplementation& view) {
- view.debug_request(request, cached_value);
- view.debug_request("clear-cache"sv); // Clear the cache to ensure requests are re-done with the new value.
- return IterationDecision::Continue;
- });
- });
-
- action->set_checked(value == cached_value);
- menu->add_action(move(action));
- };
-
- m_reload_action = Action::create("Reload"sv, ActionID::Reload, [this]() {
- if (auto view = active_web_view(); view.has_value())
- view->reload();
- });
- m_reload_bypassing_cache_action = Action::create("Reload Without Cache"sv, ActionID::ReloadBypassingCache, [this]() {
- if (auto view = active_web_view(); view.has_value())
- view->reload(BypassCache::Yes);
- });
-
- m_undo_action = Action::create("Undo"sv, ActionID::Undo, [this]() {
- if (auto view = active_web_view(); view.has_value())
- view->undo();
- });
- m_redo_action = Action::create("Redo"sv, ActionID::Redo, [this]() {
- if (auto view = active_web_view(); view.has_value())
- view->redo();
- });
- update_editing_history_actions();
- m_copy_selection_action = Action::create("Copy"sv, ActionID::CopySelection, [this]() {
- if (auto view = active_web_view(); view.has_value())
- view->selected_text()->when_resolved([this](auto& text) {
- if (!text.is_empty())
- insert_clipboard_entry({ "text/plain"_string, text });
- });
- });
- m_cut_selection_action = Action::create("Cut"sv, ActionID::CutSelection, [this]() {
- if (auto view = active_web_view(); view.has_value())
- view->cut_selected_text()->when_resolved([this](auto& text) {
- if (!text.is_empty())
- insert_clipboard_entry({ "text/plain"_string, text });
- });
- });
- m_paste_action = Action::create("Paste"sv, ActionID::Paste, [this]() {
- if (auto view = active_web_view(); view.has_value())
- view->paste_from_clipboard();
- });
- m_select_all_action = Action::create("Select All"sv, ActionID::SelectAll, [this]() {
- if (auto view = active_web_view(); view.has_value())
- view->select_all();
- });
+ m_user_agent_string = MUST(String::from_utf8(m_web_content_options.user_agent_preset.has_value()
+ ? *WebView::user_agents.get(*m_web_content_options.user_agent_preset)
+ : Web::default_user_agent));
+ m_scripting_enabled = m_browser_options.disable_scripting == DisableScripting::No;
+ m_blocks_pop_ups = m_browser_options.allow_popups == AllowPopups::No;
+ update_website_color_scheme();
- m_open_about_page_action = Action::create("About Ladybird"sv, ActionID::OpenAboutPage, [this]() {
- activate_or_open_tab_with_url(URL::about_version());
- });
- m_open_downloads_page_action = Action::create("Downloads"sv, ActionID::ViewDownloads, [this]() {
- activate_or_open_tab_with_url(URL::about_downloads());
- });
- m_open_settings_page_action = Action::create("Settings"sv, ActionID::OpenSettingsPage, [this]() {
- activate_or_open_tab_with_url(URL::about_settings());
- });
+ create_platform_actions();
+}
- m_zoom_in_action = Action::create("Zoom In"sv, ActionID::ZoomIn, [this]() {
- if (auto view = active_web_view(); view.has_value())
- view->zoom_in();
- });
- m_zoom_out_action = Action::create("Zoom Out"sv, ActionID::ZoomOut, [this]() {
- if (auto view = active_web_view(); view.has_value())
- view->zoom_out();
- });
- m_reset_zoom_action = Action::create("Reset Zoom"sv, ActionID::ResetZoom, [this]() {
- if (auto view = active_web_view(); view.has_value())
- view->reset_zoom();
+void Application::set_preferred_contrast(Web::CSS::PreferredContrast contrast)
+{
+ m_contrast = contrast;
+ ViewImplementation::for_each_view([&](ViewImplementation& view) {
+ view.set_preferred_contrast(m_contrast);
+ return IterationDecision::Continue;
});
+}
- m_zoom_menu = Menu::create_group("Zoom"sv);
- m_zoom_menu->add_action(zoom_in_action());
- m_zoom_menu->add_action(zoom_out_action());
- m_zoom_menu->add_action(reset_zoom_action());
-
- auto set_website_color_scheme = [this](auto color_scheme) {
- return [this, color_scheme]() {
- auto appearance = m_settings->appearance();
- appearance.website_color_scheme = color_scheme;
- m_settings->set_appearance(appearance);
- };
- };
-
- m_force_dark_action = Action::create_checkable("Force Dark Mode"sv, ActionID::ForceDark, [this] {
- if (auto view = active_web_view(); view.has_value())
- view->set_force_dark_enabled(!view->force_dark_enabled());
- update_force_dark_action();
+void Application::set_preferred_motion(Web::CSS::PreferredMotion motion)
+{
+ m_motion = motion;
+ ViewImplementation::for_each_view([&](ViewImplementation& view) {
+ view.set_preferred_motion(m_motion);
+ return IterationDecision::Continue;
});
- update_force_dark_action();
-
- m_color_scheme_menu = Menu::create_group("Color Scheme"sv);
- m_color_scheme_menu->add_action(Action::create_checkable("Auto"sv, ActionID::PreferredColorScheme, set_website_color_scheme(ThemePreference::System)));
- m_color_scheme_menu->add_action(Action::create_checkable("Dark"sv, ActionID::PreferredColorScheme, set_website_color_scheme(ThemePreference::Dark)));
- m_color_scheme_menu->add_action(Action::create_checkable("Light"sv, ActionID::PreferredColorScheme, set_website_color_scheme(ThemePreference::Light)));
- update_website_color_scheme();
-
- auto set_contrast = [this](auto contrast) {
- return [this, contrast]() {
- m_contrast = contrast;
-
- ViewImplementation::for_each_view([&](ViewImplementation& view) {
- view.set_preferred_contrast(m_contrast);
- return IterationDecision::Continue;
- });
- };
- };
-
- m_contrast_menu = Menu::create_group("Contrast"sv);
- m_contrast_menu->add_action(Action::create_checkable("Auto"sv, ActionID::PreferredContrast, set_contrast(Web::CSS::PreferredContrast::Auto)));
- m_contrast_menu->add_action(Action::create_checkable("Less"sv, ActionID::PreferredContrast, set_contrast(Web::CSS::PreferredContrast::Less)));
- m_contrast_menu->add_action(Action::create_checkable("More"sv, ActionID::PreferredContrast, set_contrast(Web::CSS::PreferredContrast::More)));
- m_contrast_menu->add_action(Action::create_checkable("No Preference"sv, ActionID::PreferredContrast, set_contrast(Web::CSS::PreferredContrast::NoPreference)));
- m_contrast_menu->items().first().get<NonnullRefPtr<Action>>()->set_checked(true);
-
- auto set_motion = [this](auto motion) {
- return [this, motion]() {
- m_motion = motion;
+}
- ViewImplementation::for_each_view([&](ViewImplementation& view) {
- view.set_preferred_motion(m_motion);
- return IterationDecision::Continue;
- });
- };
- };
+void Application::set_user_agent_string(String user_agent)
+{
+ m_user_agent_string = move(user_agent);
- m_motion_menu = Menu::create_group("Motion"sv);
- m_motion_menu->add_action(Action::create_checkable("Auto"sv, ActionID::PreferredMotion, set_motion(Web::CSS::PreferredMotion::Auto)));
- m_motion_menu->add_action(Action::create_checkable("Reduce"sv, ActionID::PreferredMotion, set_motion(Web::CSS::PreferredMotion::Reduce)));
- m_motion_menu->add_action(Action::create_checkable("No Preference"sv, ActionID::PreferredMotion, set_motion(Web::CSS::PreferredMotion::NoPreference)));
- m_motion_menu->items().first().get<NonnullRefPtr<Action>>()->set_checked(true);
+ // Workers take no debug requests; the worker process manager hands the new value to every live worker.
+ WorkerAgentManager::the().update_user_agent(m_user_agent_string);
- m_toggle_vertical_tabs_expanded_action = Action::create("Toggle Vertical Tabs Expanded"sv, ActionID::ToggleVerticalTabsExpanded, [this]() {
- auto tab_settings = m_settings->tab_settings();
- tab_settings.vertical_tabs_expanded = !tab_settings.vertical_tabs_expanded;
- m_settings->set_tab_settings(tab_settings);
+ ViewImplementation::for_each_view([&](ViewImplementation& view) {
+ view.debug_request("spoof-user-agent"sv, m_user_agent_string.to_byte_string());
+ view.debug_request("clear-cache"sv); // Clear the cache to ensure requests are re-done with the new value.
+ return IterationDecision::Continue;
});
- update_vertical_tabs_action();
+}
- m_toggle_menu_bar_action = Action::create_checkable("Show Menu Bar"sv, ActionID::ToggleMenuBar, [this]() {
- auto appearance = m_settings->appearance();
- appearance.show_menu_bar = !appearance.show_menu_bar;
- m_settings->set_appearance(appearance);
- });
- m_toggle_menu_bar_action->set_checked(m_settings->appearance().show_menu_bar);
-
- m_history_menu = Menu::create("History"sv);
- m_history_menu->add_action(Action::create("View History"sv, ActionID::ViewHistory, [this]() {
- activate_or_open_tab_with_url(URL::about_history());
- }));
- m_history_menu->add_separator();
- m_history_menu->add_action(Action::create("Clear Browsing Data"sv, ActionID::ClearBrowsingData, [this]() {
- auto url = URL::about_settings();
- url.set_fragment("clearBrowsingData"sv);
- activate_or_open_tab_with_url(url);
- }));
-
- m_inspect_menu = Menu::create("Inspect"sv);
-
- m_view_source_action = Action::create("View Page Source"sv, ActionID::ViewSource, [this]() {
- if (auto view = active_web_view(); view.has_value())
- view->get_source();
- });
- m_inspect_menu->add_action(*m_view_source_action);
+void Application::set_navigator_compatibility_mode(Web::NavigatorCompatibilityMode mode)
+{
+ m_navigator_compatibility_mode = mode;
- m_toggle_devtools_action = Action::create("Enable DevTools"sv, ActionID::ToggleDevTools, [this]() {
- if (auto result = toggle_devtools_enabled(); result.is_error())
- display_error_dialog(MUST(String::formatted("Unable to start DevTools: {}", result.error())));
- });
- m_inspect_menu->add_action(*m_toggle_devtools_action);
+ // Workers take no debug requests; the worker process manager hands the new value to every live worker.
+ WorkerAgentManager::the().update_navigator_compatibility_mode(m_navigator_compatibility_mode);
- m_report_website_issue_action = Action::create("Report Website Issue…"sv, ActionID::ReportWebsiteIssue, [this]() {
- if (auto view = active_web_view(); view.has_value())
- view->report_website_issue();
+ ViewImplementation::for_each_view([&](ViewImplementation& view) {
+ view.debug_request("navigator-compatibility-mode"sv, navigator_compatibility_mode_to_string(m_navigator_compatibility_mode));
+ view.debug_request("clear-cache"sv); // Clear the cache to ensure requests are re-done with the new value.
+ return IterationDecision::Continue;
});
- m_report_website_issue_action->set_enabled(false);
-
- m_debug_menu = Menu::create("Debug"sv);
- m_debug_menu->set_visible(m_settings->config_variable_as_bool(ConfigVariableID::ShowAdvancedDebugMenu));
- m_debug_menu->add_action(Action::create("Dump Session History Tree"sv, ActionID::DumpSessionHistoryTree, debug_request("dump-session-history"sv)));
- m_debug_menu->add_action(Action::create("Dump DOM Tree"sv, ActionID::DumpDOMTree, debug_request("dump-dom-tree"sv)));
- m_debug_menu->add_action(Action::create("Dump Layout Tree"sv, ActionID::DumpLayoutTree, debug_request("dump-layout-tree"sv)));
- m_debug_menu->add_action(Action::create("Dump Stacking Context Tree"sv, ActionID::DumpStackingContextTree, debug_request("dump-stacking-context-tree"sv)));
- m_debug_menu->add_action(Action::create("Dump Site Isolation Process Tree"sv, ActionID::DumpSiteIsolationProcessTree, debug_request("dump-site-isolation-process-tree"sv)));
- m_debug_menu->add_action(Action::create("Dump Display List"sv, ActionID::DumpDisplayList, debug_request("dump-display-list"sv)));
- m_debug_menu->add_action(Action::create("Dump Style Sheets"sv, ActionID::DumpStyleSheets, debug_request("dump-style-sheets"sv)));
- m_debug_menu->add_action(Action::create("Dump All Resolved Styles"sv, ActionID::DumpStyles, debug_request("dump-all-resolved-styles"sv)));
- m_debug_menu->add_action(Action::create("Dump CSS Errors"sv, ActionID::DumpCSSErrors, debug_request("dump-all-css-errors"sv)));
- m_debug_menu->add_action(Action::create("Dump Cookies"sv, ActionID::DumpCookies, [this]() { m_default_session->cookie_jar->dump_cookies(); }));
- m_debug_menu->add_action(Action::create("Dump Local Storage"sv, ActionID::DumpLocalStorage, debug_request("dump-local-storage"sv)));
- m_debug_menu->add_action(Action::create("Dump Session Storage"sv, ActionID::DumpSessionStorage, debug_request("dump-session-storage"sv)));
- m_debug_menu->add_action(Action::create("Dump GC Graph"sv, ActionID::DumpGCGraph, [this]() {
- if (auto view = active_web_view(); view.has_value()) {
- auto gc_graph_path = view->dump_gc_graph();
- if (gc_graph_path.is_error()) {
- warnln("\033[31;1mFailed to dump GC graph: {}\033[0m", gc_graph_path.error());
- } else {
- warnln("\033[33;1mDumped GC graph into {}\033[0m", gc_graph_path.value());
- if (auto source_dir = Core::Environment::get("LADYBIRD_SOURCE_DIR"sv); source_dir.has_value())
- warnln("\033[33;1mGC graph explorer: file://{}/Meta/gc-heap-explorer.html?script=file://{}\033[0m", *source_dir, gc_graph_path.value());
- }
- }
- }));
- m_debug_menu->add_separator();
-
- m_show_line_box_borders_action = Action::create_checkable("Show Line Box Borders"sv, ActionID::ShowLineBoxBorders, check(m_show_line_box_borders_action, "set-line-box-borders"sv));
- m_debug_menu->add_action(*m_show_line_box_borders_action);
-
- m_show_caret_hit_test_debug_overlay_action = Action::create_checkable("Show Caret Hit Test Debug Overlay"sv, ActionID::ShowCaretHitTestDebugOverlay, check(m_show_caret_hit_test_debug_overlay_action, "set-caret-hit-test-debug-overlay"sv));
- m_debug_menu->add_action(*m_show_caret_hit_test_debug_overlay_action);
+}
- m_show_tab_performance_monitor_action = Action::create_checkable("Show Tab Performance Monitor"sv, ActionID::ShowTabPerformanceMonitor, [this]() {
- m_settings->set_config_variable(ConfigVariableID::ShowTabPerformanceMonitor, m_show_tab_performance_monitor_action->checked());
+static void send_debug_switch_to_every_view(StringView request, bool on)
+{
+ ViewImplementation::for_each_view([&](ViewImplementation& view) {
+ view.debug_request(request, on ? "on"sv : "off"sv);
+ return IterationDecision::Continue;
});
- m_show_tab_performance_monitor_action->set_checked(m_settings->config_variable_as_bool(ConfigVariableID::ShowTabPerformanceMonitor));
- m_debug_menu->add_action(*m_show_tab_performance_monitor_action);
- m_debug_menu->add_separator();
-
- m_debug_menu->add_action(Action::create("Report Leaked Documents"sv, ActionID::ReportLeakedDocuments, debug_request("report-leaked-documents"sv)));
- m_debug_menu->add_action(Action::create("Collect Garbage"sv, ActionID::CollectGarbage, debug_request("collect-garbage"sv)));
- m_debug_menu->add_action(Action::create("Crash Current Page"sv, ActionID::CrashCurrentPage, debug_request("crash-current-page"sv)));
- m_debug_menu->add_action(Action::create("Crash Compositor Process"sv, ActionID::CrashCompositorProcess, [this]() { crash_compositor_process(); }));
- m_debug_menu->add_separator();
-
- auto spoof_user_agent_menu = Menu::create_group("Spoof User Agent"sv);
- m_user_agent_string = m_web_content_options.user_agent_preset.has_value()
- ? *WebView::user_agents.get(*m_web_content_options.user_agent_preset)
- : Web::default_user_agent;
-
- auto notify_workers_of_user_agent = [this] { WorkerAgentManager::the().update_user_agent(user_agent_string()); };
- add_spoofed_value(spoof_user_agent_menu, "Disabled"sv, Web::default_user_agent, m_user_agent_string, "spoof-user-agent"sv, notify_workers_of_user_agent);
- for (auto const& user_agent : WebView::user_agents) {
- add_spoofed_value(spoof_user_agent_menu, user_agent.key, user_agent.value, m_user_agent_string, "spoof-user-agent"sv, notify_workers_of_user_agent);
- }
-
- auto navigator_compatibility_mode_menu = Menu::create_group("Navigator Compatibility Mode"sv);
- m_navigator_compatibility_mode = navigator_compatibility_mode_to_string(Web::default_navigator_compatibility_mode);
-
- auto notify_workers_of_navigator_compatibility_mode = [this] { WorkerAgentManager::the().update_navigator_compatibility_mode(navigator_compatibility_mode()); };
- auto add_navigator_compatibility_mode = [&](StringView name, Web::NavigatorCompatibilityMode mode) {
- add_spoofed_value(navigator_compatibility_mode_menu, name, navigator_compatibility_mode_to_string(mode), m_navigator_compatibility_mode, "navigator-compatibility-mode"sv, notify_workers_of_navigator_compatibility_mode);
- };
- add_navigator_compatibility_mode("Chrome"sv, Web::NavigatorCompatibilityMode::Chrome);
- add_navigator_compatibility_mode("Gecko"sv, Web::NavigatorCompatibilityMode::Gecko);
- add_navigator_compatibility_mode("WebKit"sv, Web::NavigatorCompatibilityMode::WebKit);
+}
- m_debug_menu->add_submenu(move(spoof_user_agent_menu));
- m_debug_menu->add_submenu(move(navigator_compatibility_mode_menu));
- m_debug_menu->add_separator();
+void Application::set_scripting_enabled(bool enabled)
+{
+ m_scripting_enabled = enabled;
+ send_debug_switch_to_every_view("scripting"sv, enabled);
+}
- m_enable_scripting_action = Action::create_checkable("Enable Scripting"sv, ActionID::EnableScripting, check(m_enable_scripting_action, "scripting"sv));
- m_enable_scripting_action->set_checked(m_browser_options.disable_scripting == WebView::DisableScripting::No);
- m_debug_menu->add_action(*m_enable_scripting_action);
+void Application::set_blocks_pop_ups(bool blocks)
+{
+ m_blocks_pop_ups = blocks;
+ send_debug_switch_to_every_view("block-pop-ups"sv, blocks);
+}
- m_block_pop_ups_action = Action::create_checkable("Block Pop-ups"sv, ActionID::BlockPopUps, check(m_block_pop_ups_action, "block-pop-ups"sv));
- m_block_pop_ups_action->set_checked(m_browser_options.allow_popups == AllowPopups::No);
- m_debug_menu->add_action(*m_block_pop_ups_action);
+void Application::set_shows_line_box_borders(bool shows)
+{
+ m_shows_line_box_borders = shows;
+ send_debug_switch_to_every_view("set-line-box-borders"sv, shows);
+}
- create_platform_actions();
+void Application::set_shows_caret_hit_test_debug_overlay(bool shows)
+{
+ m_shows_caret_hit_test_debug_overlay = shows;
+ send_debug_switch_to_every_view("set-caret-hit-test-debug-overlay"sv, shows);
}
-bool Application::blocks_pop_ups() const
+void Application::dump_cookies() const
{
- return m_block_pop_ups_action->checked();
+ m_default_session->cookie_jar->dump_cookies();
}
// The options every page hosting the tab's document runs with.
@@ -2858,12 +2641,12 @@ void Application::apply_view_options(Badge<ViewImplementation>, ViewImplementati
apply_content_blocking_policy(page);
- page.async_debug_request("set-line-box-borders"sv, m_show_line_box_borders_action->checked() ? "on"sv : "off"sv);
- page.async_debug_request("set-caret-hit-test-debug-overlay"sv, m_show_caret_hit_test_debug_overlay_action->checked() ? "on"sv : "off"sv);
- page.async_debug_request("scripting"sv, m_enable_scripting_action->checked() ? "on"sv : "off"sv);
- page.async_debug_request("block-pop-ups"sv, m_block_pop_ups_action->checked() ? "on"sv : "off"sv);
+ page.async_debug_request("set-line-box-borders"sv, m_shows_line_box_borders ? "on"sv : "off"sv);
+ page.async_debug_request("set-caret-hit-test-debug-overlay"sv, m_shows_caret_hit_test_debug_overlay ? "on"sv : "off"sv);
+ page.async_debug_request("scripting"sv, m_scripting_enabled ? "on"sv : "off"sv);
+ page.async_debug_request("block-pop-ups"sv, m_blocks_pop_ups ? "on"sv : "off"sv);
page.async_debug_request("spoof-user-agent"sv, m_user_agent_string);
- page.async_debug_request("navigator-compatibility-mode"sv, m_navigator_compatibility_mode);
+ page.async_debug_request("navigator-compatibility-mode"sv, navigator_compatibility_mode_to_string(m_navigator_compatibility_mode));
}
bool Application::sites_can_ask_for_location() const
@@ -2986,20 +2769,12 @@ void Application::languages_changed(Badge<ApplicationSettingsObserver>)
String Application::user_agent_string() const
{
- return MUST(String::from_utf8(m_user_agent_string));
+ return m_user_agent_string;
}
Web::NavigatorCompatibilityMode Application::navigator_compatibility_mode() const
{
- return navigator_compatibility_mode_from_string(m_navigator_compatibility_mode).value_or(Web::default_navigator_compatibility_mode);
-}
-
-void Application::update_vertical_tabs_action()
-{
- auto const& settings = m_settings->tab_settings();
- m_toggle_vertical_tabs_expanded_action->set_visible(settings.vertical_tabs_enabled);
- m_toggle_vertical_tabs_expanded_action->set_engaged(settings.vertical_tabs_expanded);
- m_toggle_vertical_tabs_expanded_action->set_tooltip(settings.vertical_tabs_expanded ? "Hide Sidebar"sv : "Show Sidebar"sv);
+ return m_navigator_compatibility_mode;
}
static Web::CSS::PreferredColorScheme preferred_color_scheme_for(ThemePreference preference)
@@ -3022,13 +2797,7 @@ Web::CSS::PreferredColorScheme Application::interface_color_scheme() const
void Application::update_website_color_scheme()
{
- auto preference = m_settings->appearance().website_color_scheme;
- auto actions = m_color_scheme_menu->items();
- auto action_index = preference == ThemePreference::System ? 0 : preference == ThemePreference::Dark ? 1
- : 2;
- actions[action_index].get<NonnullRefPtr<Action>>()->set_checked(true);
-
- auto color_scheme = preferred_color_scheme_for(preference);
+ auto color_scheme = preferred_color_scheme_for(m_settings->appearance().website_color_scheme);
if (color_scheme == m_color_scheme)
return;
m_color_scheme = color_scheme;
@@ -3041,13 +2810,11 @@ void Application::update_website_color_scheme()
void Application::appearance_changed(Badge<ApplicationSettingsObserver>)
{
- m_toggle_menu_bar_action->set_checked(m_settings->appearance().show_menu_bar);
update_website_color_scheme();
}
void Application::tab_settings_changed(Badge<ApplicationSettingsObserver>)
{
- update_vertical_tabs_action();
update_tabs_display();
}
@@ -3130,14 +2897,6 @@ void Application::download_content_blocker_list_if_needed(Badge<Application, Set
}
// The shared actions that depend on the page follow the active view, whichever tab or window made it the active one.
-void Application::update_actions_for_active_web_view()
-{
- update_editing_history_actions();
- update_report_website_issue_action();
- update_force_dark_action();
- update_view_source_action();
-}
-
// NB: Only the application knows which favicons it keeps for itself, so without one telling us, nothing is removed.
void Application::remove_unreferenced_favicons(HashTable<String> referenced_hashes)
{
@@ -3232,16 +2991,6 @@ ErrorOr<void> Application::toggle_devtools_enabled()
return {};
}
-void Application::on_devtools_enabled() const
-{
- m_toggle_devtools_action->set_text("Disable DevTools"sv);
-}
-
-void Application::on_devtools_disabled() const
-{
- m_toggle_devtools_action->set_text("Enable DevTools"sv);
-}
-
void Application::refresh_tab_list()
{
if (!m_devtools)
@@ -3481,34 +3230,6 @@ void Application::inspect_indexed_database_objects(DevTools::TabDescription cons
view->inspect_indexed_database_objects(host, move(names), move(options), move(on_complete));
}
-void Application::update_force_dark_action()
-{
- if (!m_force_dark_action)
- return;
- auto view = active_web_view();
- m_force_dark_action->set_enabled(view.has_value() && view->can_set_force_dark());
- m_force_dark_action->set_checked(view.has_value() ? view->force_dark_enabled() : settings().content_settings().enable_force_dark);
-}
-
-void Application::update_report_website_issue_action()
-{
- auto view = active_web_view();
- m_report_website_issue_action->set_enabled(view.has_value() && view->can_report_website_issue());
-}
-
-void Application::update_view_source_action()
-{
- auto view = active_web_view();
- m_view_source_action->set_enabled(!view.has_value() || view->can_view_source());
-}
-
-void Application::update_editing_history_actions()
-{
- auto view = active_web_view();
- m_undo_action->set_enabled(view.has_value() && view->can_undo());
- m_redo_action->set_enabled(view.has_value() && view->can_redo());
-}
-
void Application::delete_indexed_database(DevTools::TabDescription const& description, String const& host, String const& name, OnIndexedDBInspectionComplete on_complete) const
{
auto view = ViewImplementation::find_view_by_id(description.id);
diff --git a/Libraries/LibWebView/Application.h b/Libraries/LibWebView/Application.h
index 66daf1e46d0..1aa184f0ffb 100644
--- a/Libraries/LibWebView/Application.h
+++ b/Libraries/LibWebView/Application.h
@@ -43,6 +43,7 @@
#include <LibWebCommon/HTML/SessionHistoryEntryDescriptor.h>
#include <LibWebCommon/HTML/VisibilityState.h>
#include <LibWebCommon/Loader/NavigatorCompatibilityMode.h>
+#include <LibWebCommon/Loader/UserAgent.h>
#include <LibWebCommon/Page/InputEvent.h>
#include <LibWebCommon/Page/PageId.h>
#include <LibWebView/BlobURLStore.h>
@@ -115,7 +116,7 @@ public:
static FontService& font_service() { return *the().m_font_service; }
static FontServiceHost& font_service_host() { return *the().m_font_service_host; }
JsonValue const& site_compatibility_data() const { return m_site_compatibility_data; }
- bool blocks_pop_ups() const;
+ bool blocks_pop_ups() const { return m_blocks_pop_ups; }
ErrorOr<void> reload_site_compatibility_data();
bool claim_cpu_profiler(ProcessType);
@@ -180,8 +181,6 @@ public:
String user_agent_string() const;
Web::NavigatorCompatibilityMode navigator_compatibility_mode() const;
- void update_actions_for_active_web_view();
-
void background_networking_settings_changed(Badge<ApplicationSettingsObserver>);
// True unless the user has turned location off, or there's no way to find it.
@@ -357,50 +356,20 @@ public:
};
NonnullRefPtr<Core::Promise<Empty>> clear_browsing_data(ClearBrowsingDataOptions const&);
- Action& reload_action() { return *m_reload_action; }
- Action& reload_bypassing_cache_action() { return *m_reload_bypassing_cache_action; }
- Action& copy_selection_action() { return *m_copy_selection_action; }
- Action& undo_action() { return *m_undo_action; }
- Action& redo_action() { return *m_redo_action; }
-
- // Reflects the active view's undo/redo availability in the shared actions; call when the
- // active view changes.
- void update_editing_history_actions();
- Action& cut_selection_action() { return *m_cut_selection_action; }
- Action& paste_action() { return *m_paste_action; }
- Action& select_all_action() { return *m_select_all_action; }
-
- Action& open_about_page_action() { return *m_open_about_page_action; }
- Action& open_downloads_page_action() { return *m_open_downloads_page_action; }
- Action& open_settings_page_action() { return *m_open_settings_page_action; }
-
- Menu& zoom_menu() { return *m_zoom_menu; }
- Action& zoom_in_action() { return *m_zoom_in_action; }
- Action& zoom_out_action() { return *m_zoom_out_action; }
- Action& reset_zoom_action() { return *m_reset_zoom_action; }
-
- Action& force_dark_action() { return *m_force_dark_action; }
- void update_force_dark_action();
-
- Menu& color_scheme_menu() { return *m_color_scheme_menu; }
- Menu& contrast_menu() { return *m_contrast_menu; }
- Menu& motion_menu() { return *m_motion_menu; }
-
- Action& toggle_vertical_tabs_expanded_action() { return *m_toggle_vertical_tabs_expanded_action; }
-
- Action& toggle_menu_bar_action() { return *m_toggle_menu_bar_action; }
-
- Menu& history_menu() { return *m_history_menu; }
-
- Menu& inspect_menu() { return *m_inspect_menu; }
- Action& view_source_action() { return *m_view_source_action; }
- void update_view_source_action();
-
- Action& report_website_issue_action() { return *m_report_website_issue_action; }
- void update_report_website_issue_action();
-
- Menu& debug_menu() { return *m_debug_menu; }
- Action& show_tab_performance_monitor_action() { return *m_show_tab_performance_monitor_action; }
+ // Options every page runs with, which the user can change while browsing.
+ void set_preferred_contrast(Web::CSS::PreferredContrast);
+ void set_preferred_motion(Web::CSS::PreferredMotion);
+ void set_user_agent_string(String);
+ void set_navigator_compatibility_mode(Web::NavigatorCompatibilityMode);
+ bool scripting_enabled() const { return m_scripting_enabled; }
+ void set_scripting_enabled(bool);
+ void set_blocks_pop_ups(bool);
+ void set_shows_line_box_borders(bool);
+ void set_shows_caret_hit_test_debug_overlay(bool);
+
+ bool devtools_enabled() const { return m_devtools; }
+ void dump_cookies() const;
+ void crash_compositor_process();
FileDownloader& file_downloader() { return m_file_downloader; }
@@ -446,8 +415,8 @@ protected:
virtual void on_recently_closed_entries_changed() const { }
- virtual void on_devtools_enabled() const;
- virtual void on_devtools_disabled() const;
+ virtual void on_devtools_enabled() const { }
+ virtual void on_devtools_disabled() const { }
Main::Arguments& arguments() { return m_arguments; }
@@ -465,7 +434,6 @@ private:
void compositor_reported_gpu_device_identity(Optional<Gfx::GpuDeviceIdentity>);
void handle_compositor_process_death();
void recover_compositor_process();
- void crash_compositor_process();
ErrorOr<void> launch_request_server();
ErrorOr<void> launch_devtools_server();
// Returns whether the combined rules changed.
@@ -488,8 +456,7 @@ private:
void update_os_location_authorization();
void schedule_permission_expiry();
- void initialize_actions();
- void update_vertical_tabs_action();
+ void initialize_page_options();
WebDriverBrowserConnection* webdriver_browser_connection();
@@ -681,54 +648,15 @@ private:
#endif
OwnPtr<ProcessManager> m_process_manager;
- RefPtr<Action> m_reload_action;
- RefPtr<Action> m_reload_bypassing_cache_action;
- RefPtr<Action> m_copy_selection_action;
- RefPtr<Action> m_undo_action;
- RefPtr<Action> m_redo_action;
- RefPtr<Action> m_cut_selection_action;
- RefPtr<Action> m_paste_action;
- RefPtr<Action> m_select_all_action;
-
- RefPtr<Action> m_open_about_page_action;
- RefPtr<Action> m_open_downloads_page_action;
- RefPtr<Action> m_open_settings_page_action;
-
- RefPtr<Menu> m_zoom_menu;
- RefPtr<Action> m_zoom_in_action;
- RefPtr<Action> m_zoom_out_action;
- RefPtr<Action> m_reset_zoom_action;
-
- RefPtr<Action> m_force_dark_action;
- RefPtr<Menu> m_color_scheme_menu;
Web::CSS::PreferredColorScheme m_color_scheme { Web::CSS::PreferredColorScheme::Auto };
-
- RefPtr<Menu> m_contrast_menu;
Web::CSS::PreferredContrast m_contrast { Web::CSS::PreferredContrast::Auto };
-
- RefPtr<Menu> m_motion_menu;
Web::CSS::PreferredMotion m_motion { Web::CSS::PreferredMotion::Auto };
-
- RefPtr<Action> m_toggle_vertical_tabs_expanded_action;
-
- RefPtr<Action> m_toggle_menu_bar_action;
-
- RefPtr<Menu> m_history_menu;
-
- RefPtr<Menu> m_inspect_menu;
- RefPtr<Action> m_view_source_action;
- RefPtr<Action> m_toggle_devtools_action;
-
- RefPtr<Action> m_report_website_issue_action;
-
- RefPtr<Menu> m_debug_menu;
- RefPtr<Action> m_show_line_box_borders_action;
- RefPtr<Action> m_show_caret_hit_test_debug_overlay_action;
- RefPtr<Action> m_show_tab_performance_monitor_action;
- RefPtr<Action> m_enable_scripting_action;
- RefPtr<Action> m_block_pop_ups_action;
- StringView m_user_agent_string;
- StringView m_navigator_compatibility_mode;
+ String m_user_agent_string;
+ Web::NavigatorCompatibilityMode m_navigator_compatibility_mode { Web::default_navigator_compatibility_mode };
+ bool m_scripting_enabled { true };
+ bool m_blocks_pop_ups { true };
+ bool m_shows_line_box_borders { false };
+ bool m_shows_caret_hit_test_debug_overlay { false };
Web::Clipboard::SystemClipboardItem m_clipboard;
diff --git a/Libraries/LibWebView/ContextMenuRequest.h b/Libraries/LibWebView/ContextMenuRequest.h
new file mode 100644
index 00000000000..630ba94d340
--- /dev/null
+++ b/Libraries/LibWebView/ContextMenuRequest.h
@@ -0,0 +1,60 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include <AK/Optional.h>
+#include <AK/String.h>
+#include <AK/Utf16String.h>
+#include <AK/Vector.h>
+#include <LibGfx/Point.h>
+#include <LibGfx/ShareableBitmap.h>
+#include <LibWebCommon/HTML/PreparedNavigationDescriptor.h>
+#include <LibWebCommon/Page/MediaContextMenu.h>
+#include <LibWebCommon/WebView/DictionaryLookup.h>
+
+namespace WebView {
+
+enum class ContextMenuKind : u8 {
+ Page,
+ Link,
+ Image,
+ Media,
+};
+
+// What the user asked for a context menu on. The view finds this out, and the application decides what to offer.
+struct ContextMenuRequest {
+ ContextMenuKind kind { ContextMenuKind::Page };
+ Gfx::IntPoint widget_position;
+
+ bool is_editable { false };
+ Optional<String> selected_text;
+ Optional<DictionaryLookup> dictionary_lookup;
+ Gfx::IntPoint dictionary_lookup_position;
+ Optional<Utf16String> misspelled_word;
+ Vector<Utf16String> spelling_guesses;
+
+ // A link to the selected text is being made, and arrives later.
+ bool makes_link_to_highlight { false };
+ bool can_inspect_element { false };
+
+ Optional<Web::HTML::PreparedNavigationDescriptor> link_navigation;
+ Optional<Web::HTML::PreparedNavigationDescriptor> navigation;
+ Optional<Gfx::ShareableBitmap> image_bitmap;
+ Optional<String> image_mime_type;
+ Optional<Web::MediaContextMenu> media;
+};
+
+enum class ContextMenuMediaAction : u8 {
+ TogglePlay,
+ ToggleMute,
+ ToggleControls,
+ ToggleLoop,
+ TogglePictureInPicture,
+ ToggleFullscreen,
+};
+
+}
diff --git a/Libraries/LibWebView/PageTranslation.cpp b/Libraries/LibWebView/PageTranslation.cpp
index 5c3fb71a18a..09b60d268f7 100644
--- a/Libraries/LibWebView/PageTranslation.cpp
+++ b/Libraries/LibWebView/PageTranslation.cpp
@@ -5,12 +5,9 @@
*/
#include <AK/AnyOf.h>
-#include <LibURL/InternalURLs.h>
#include <LibURL/URL.h>
-#include <LibUnicode/DisplayNames.h>
#include <LibWebView/Application.h>
#include <LibWebView/CanonicalTraversable.h>
-#include <LibWebView/Menu.h>
#include <LibWebView/PageTranslation.h>
#include <LibWebView/Settings.h>
#include <LibWebView/URL.h>
@@ -19,15 +16,6 @@
namespace WebView {
-// The browser's own strings are English, so languages are named in English.
-static String language_display_name(StringView language)
-{
- auto primary_language = primary_language_subtag(language);
- if (auto name = Unicode::language_display_name("en"sv, primary_language, Unicode::LanguageDisplay::Standard); name.has_value())
- return name->to_utf8();
- return MUST(String::from_utf8(primary_language));
-}
-
// The languages the user reads, most preferred first, one per language whatever its regions.
static Vector<String> languages_the_user_reads()
{
@@ -42,37 +30,6 @@ static Vector<String> languages_the_user_reads()
PageTranslation::PageTranslation(ViewImplementation& view)
: m_view(view)
{
- m_translate_action = Action::create("Translate"sv, ActionID::TranslatePage, [] { });
- m_translate_action->set_tooltip("Translate this page"sv);
- m_translate_action->set_visible(false);
-
- m_translation_menu = Menu::create("Translation"sv);
- for (size_t index = 0; index < MAXIMUM_OFFERED_LANGUAGES; ++index) {
- auto action = Action::create_checkable("Translate"sv, ActionID::TranslateTo, [this, index] {
- if (auto target_language = ready_target_language(index); target_language.has_value())
- translate_to(*target_language);
- // NB: The check mark shows the language the page is in, which activating an action doesn't change by itself.
- update_actions();
- });
- action->set_visible(false);
- m_translation_menu->add_action(action);
- m_translate_to_actions.append(move(action));
- }
- m_show_original_action = Action::create("Show Original"sv, ActionID::ShowOriginal, [this] { show_original(); });
- m_translation_menu->add_action(*m_show_original_action);
- m_translation_menu->add_separator();
- m_download_languages_action = Action::create("Download Languages…"sv, ActionID::DownloadTranslationLanguages, [] {
- if (auto* translator = Application::the().translator())
- translator->open_language_download_settings();
- });
- m_translation_menu->add_action(*m_download_languages_action);
- m_translation_menu->add_action(Action::create("Preferred Languages…"sv, ActionID::EditPreferredLanguages, [] {
- auto url = URL::about_settings();
- url.set_fragment("editLanguages"sv);
- Application::the().activate_or_open_tab_with_url(url);
- }));
-
- update_actions();
}
PageTranslation::~PageTranslation()
@@ -111,7 +68,7 @@ void PageTranslation::translate_to(String const& target_language)
page.async_start_translation(*m_page_language, target_language);
});
- update_actions();
+ did_change_state();
}
void PageTranslation::show_original()
@@ -163,7 +120,7 @@ void PageTranslation::did_change_active_document()
m_page_language.clear();
m_target_languages.clear();
m_target_language.clear();
- update_actions();
+ did_change_state();
}
void PageTranslation::did_create_page(WebContentPage& page)
@@ -240,7 +197,7 @@ void PageTranslation::did_detect_page_language(u64 generation, Optional<String>
m_page_language = move(language);
m_target_languages.clear();
- update_actions();
+ did_change_state();
// A page in a language the user reads isn't offered for translation.
auto languages = languages_the_user_reads();
@@ -256,23 +213,12 @@ void PageTranslation::did_detect_page_language(u64 generation, Optional<String>
if (!weak_this || generation != weak_this->m_generation)
return;
weak_this->m_target_languages = move(target_languages);
- weak_this->update_actions();
+ weak_this->did_change_state();
if (on_complete)
on_complete();
});
}
-Optional<String> PageTranslation::ready_target_language(size_t index) const
-{
- for (auto const& target : m_target_languages) {
- if (target.needs_download)
- continue;
- if (index-- == 0)
- return target.language;
- }
- return {};
-}
-
// The pages forget the translation, so a document they load next isn't translated unless it's asked for.
void PageTranslation::stop_translating_pages()
{
@@ -292,26 +238,27 @@ void PageTranslation::close_session()
m_session_id.clear();
}
-void PageTranslation::update_actions()
+Vector<String> PageTranslation::offered_target_languages() const
{
- m_translate_action->set_visible(is_offered());
- m_translate_action->set_engaged(is_translated());
- m_translate_action->set_tooltip(is_translated() ? "Showing translation"sv : "Translate this page"sv);
-
- size_t slot = 0;
+ Vector<String> languages;
for (auto const& target : m_target_languages) {
- if (target.needs_download || slot == m_translate_to_actions.size())
+ if (target.needs_download)
continue;
- auto& action = m_translate_to_actions[slot++];
- action->set_text(MUST(String::formatted("Translate to {}", language_display_name(target.language))));
- action->set_checked(m_target_language.has_value() && is_same_language(*m_target_language, target.language));
- action->set_visible(true);
+ if (languages.size() == MAXIMUM_OFFERED_LANGUAGES)
+ break;
+ languages.append(target.language);
}
- for (; slot < m_translate_to_actions.size(); ++slot)
- m_translate_to_actions[slot]->set_visible(false);
+ return languages;
+}
- m_show_original_action->set_enabled(is_translated());
- m_download_languages_action->set_visible(any_of(m_target_languages, [](auto const& target) { return target.needs_download; }));
+bool PageTranslation::needs_language_download() const
+{
+ return any_of(m_target_languages, [](auto const& target) { return target.needs_download; });
+}
+
+void PageTranslation::did_change_state()
+{
+ m_view.did_change_state();
}
Optional<String> PageTranslation::current_site() const
diff --git a/Libraries/LibWebView/PageTranslation.h b/Libraries/LibWebView/PageTranslation.h
index 27ca94348ee..6d2bee2ec43 100644
--- a/Libraries/LibWebView/PageTranslation.h
+++ b/Libraries/LibWebView/PageTranslation.h
@@ -14,6 +14,7 @@
#include <AK/Vector.h>
#include <AK/Weakable.h>
#include <LibWebCommon/Page/Translation.h>
+#include <LibWebView/Export.h>
#include <LibWebView/Forward.h>
#include <LibWebView/Translator.h>
@@ -30,7 +31,7 @@ class PageTranslation : public Weakable<PageTranslation> {
AK_ALLOC_WITH_KMALLOC;
public:
- // How many languages the translation menu offers at most.
+ // How many languages are offered at most.
static constexpr size_t MAXIMUM_OFFERED_LANGUAGES = 5;
// A page sends a few units at a time, of a paragraph's text each. One that sends more is answered without them being
// translated, so a misbehaving WebContent process can't queue up unbounded work for the translator.
@@ -49,14 +50,16 @@ public:
bool is_translated() const { return m_target_language.has_value(); }
// Whether the page can be translated into a language the user reads, or already is.
- bool is_offered() const;
+ WEBVIEW_API bool is_offered() const;
- Action& translate_action() { return *m_translate_action; }
- Menu& translation_menu() { return *m_translation_menu; }
+ // The languages the page can be translated into right away, most preferred first.
+ WEBVIEW_API Vector<String> offered_target_languages() const;
+ // Whether the user reads a language that the translator needs to download first.
+ WEBVIEW_API bool needs_language_download() const;
- void translate_to(String const& target_language);
+ WEBVIEW_API void translate_to(String const& target_language);
// Shows the page as it was before it was translated, by loading it again.
- void show_original();
+ WEBVIEW_API void show_original();
void did_finish_loading();
void did_change_active_document();
@@ -75,13 +78,11 @@ private:
Optional<String> site;
};
- // The target the n-th of the translation menu's languages stands for; only targets ready to translate get one.
- Optional<String> ready_target_language(size_t index) const;
void stop_translating_pages();
void detect_page_language(Function<void()> on_complete = {});
void did_detect_page_language(u64 generation, Optional<String> language, Function<void()> on_complete);
void close_session();
- void update_actions();
+ void did_change_state();
Optional<String> current_site() const;
ViewImplementation& m_view;
@@ -103,12 +104,6 @@ private:
u64 m_generation { 0 };
u64 m_next_sample_request_id { 0 };
HashMap<u64, Function<void(Web::TranslationLanguageSample)>> m_pending_samples;
-
- RefPtr<Action> m_translate_action;
- RefPtr<Menu> m_translation_menu;
- Vector<NonnullRefPtr<Action>> m_translate_to_actions;
- RefPtr<Action> m_show_original_action;
- RefPtr<Action> m_download_languages_action;
};
}
diff --git a/Libraries/LibWebView/UserAgent.cpp b/Libraries/LibWebView/UserAgent.cpp
index 76be0e433bb..c13441d8956 100644
--- a/Libraries/LibWebView/UserAgent.cpp
+++ b/Libraries/LibWebView/UserAgent.cpp
@@ -42,15 +42,4 @@ StringView navigator_compatibility_mode_to_string(Web::NavigatorCompatibilityMod
VERIFY_NOT_REACHED();
}
-Optional<Web::NavigatorCompatibilityMode> navigator_compatibility_mode_from_string(StringView mode)
-{
- if (mode == "chrome"sv)
- return Web::NavigatorCompatibilityMode::Chrome;
- if (mode == "gecko"sv)
- return Web::NavigatorCompatibilityMode::Gecko;
- if (mode == "webkit"sv)
- return Web::NavigatorCompatibilityMode::WebKit;
- return {};
-}
-
}
diff --git a/Libraries/LibWebView/UserAgent.h b/Libraries/LibWebView/UserAgent.h
index f9445f39fe7..514eb69bbab 100644
--- a/Libraries/LibWebView/UserAgent.h
+++ b/Libraries/LibWebView/UserAgent.h
@@ -20,6 +20,5 @@ WEBVIEW_API Optional<StringView> normalize_user_agent_name(StringView);
// The names WebContent's navigator-compatibility-mode debug request takes.
WEBVIEW_API StringView navigator_compatibility_mode_to_string(Web::NavigatorCompatibilityMode);
-WEBVIEW_API Optional<Web::NavigatorCompatibilityMode> navigator_compatibility_mode_from_string(StringView);
}
diff --git a/Libraries/LibWebView/ViewImplementation.cpp b/Libraries/LibWebView/ViewImplementation.cpp
index 8ff8d3ef24f..df160ad41ac 100644
--- a/Libraries/LibWebView/ViewImplementation.cpp
+++ b/Libraries/LibWebView/ViewImplementation.cpp
@@ -20,7 +20,6 @@
#include <LibFileSystem/FileSystem.h>
#include <LibGfx/ImageFormats/PNGWriter.h>
#include <LibGfx/SharedImageBuffer.h>
-#include <LibURL/Parser.h>
#include <LibWebCommon/CSS/SystemColor.h>
#include <LibWebCommon/Geolocation/GeolocationPositionErrorCode.h>
#include <LibWebCommon/Infra/Strings.h>
@@ -37,7 +36,6 @@
#include <LibWebView/HelperProcess.h>
#include <LibWebView/HistoryDebug.h>
#include <LibWebView/HistoryStore.h>
-#include <LibWebView/Menu.h>
#include <LibWebView/PageTranslation.h>
#include <LibWebView/PermissionManager.h>
#include <LibWebView/SessionHistory.h>
@@ -113,7 +111,6 @@ ViewImplementation::ViewImplementation(IsPrivate is_private)
{
all_views().set(m_view_id, this);
- initialize_context_menus();
m_page_translation = make<PageTranslation>(*this);
m_repeated_crash_timer = Core::Timer::create_single_shot(AK::Duration::from_milliseconds(1000), [this] {
@@ -235,9 +232,7 @@ void ViewImplementation::did_update_url()
}
}
- update_report_website_issue_action();
- Application::the().update_force_dark_action();
- update_view_source_action();
+ did_change_state();
if (auto page = traversable().display_page(); page && page->routed_connection())
page->routed_connection()->update_audio_capture_permission();
update_displayed_url();
@@ -1867,7 +1862,7 @@ void ViewImplementation::set_editing_history_state(bool can_undo, bool can_redo)
{
m_can_undo = can_undo;
m_can_redo = can_redo;
- Application::the().update_editing_history_actions();
+ did_change_state();
}
void ViewImplementation::redo()
@@ -1916,7 +1911,7 @@ void ViewImplementation::did_start_showing_page_source()
{
m_page_source_pending = true;
m_page_source_document_state.clear();
- update_view_source_action();
+ did_change_state();
}
// Only a website the page is showing can be reported, so not a browser page or a page that crashed.
@@ -1932,16 +1927,6 @@ void ViewImplementation::report_website_issue()
on_request_website_issue_report();
}
-void ViewImplementation::update_report_website_issue_action()
-{
- Application::the().update_report_website_issue_action();
-}
-
-void ViewImplementation::update_view_source_action()
-{
- Application::the().update_view_source_action();
-}
-
void ViewImplementation::inspect_dom_tree()
{
page().async_inspect_dom_tree();
@@ -3093,13 +3078,6 @@ void ViewImplementation::install_backing_stores(Vector<i32> bitmap_ids, Vector<G
void ViewImplementation::update_zoom()
{
- if (m_zoom_level != 1.0) {
- m_reset_zoom_action->set_text(MUST(String::formatted("{}%", round_to<int>(m_zoom_level * 100))));
- m_reset_zoom_action->set_visible(true);
- } else {
- m_reset_zoom_action->set_visible(false);
- }
-
// Every process showing part of the tab lays out and converts input at the tab's zoom level.
traversable().for_each_hosting_page([&](WebContentPage& page) {
page.async_set_zoom_level(m_zoom_level);
@@ -4037,8 +4015,8 @@ JsonValue ViewImplementation::webdriver_session_history() const
serialized.set("displayedURL"sv, m_displayed_url.serialize());
serialized.set("title"sv, m_title.to_utf8());
serialized.set("webContentProcessID"sv, page().client().pid());
- serialized.set("backButtonEnabled"sv, m_navigate_back_action->enabled());
- serialized.set("forwardButtonEnabled"sv, m_navigate_forward_action->enabled());
+ serialized.set("backButtonEnabled"sv, m_can_navigate_back);
+ serialized.set("forwardButtonEnabled"sv, m_can_navigate_forward);
serialized.set("crashOverlayActive"sv, crash_overlay_active());
serialized.set("hasOnlyTopLevelUsedSteps"sv, traversable().session_history().has_only_top_level_used_steps());
@@ -4068,8 +4046,13 @@ String ViewImplementation::ui_process_session_history_for_testing(Badge<WebConte
void ViewImplementation::update_navigation_action_state()
{
auto effective_current_index = traversable().effective_current_session_history_step_index();
- m_navigate_back_action->set_enabled(effective_current_index > 0);
- m_navigate_forward_action->set_enabled(effective_current_index + 1 < traversable().session_history().used_step_count());
+ auto can_navigate_back = effective_current_index > 0;
+ auto can_navigate_forward = effective_current_index + 1 < traversable().session_history().used_step_count();
+ if (can_navigate_back == m_can_navigate_back && can_navigate_forward == m_can_navigate_forward)
+ return;
+ m_can_navigate_back = can_navigate_back;
+ m_can_navigate_forward = can_navigate_forward;
+ did_change_state();
}
void ViewImplementation::recover_current_session_history_entry_with_history_operation()
@@ -4236,8 +4219,8 @@ void ViewImplementation::dump_session_history(StringView reason, SessionHistoryD
traversal.has_value() ? Optional<i32> { traversal->target_step } : Optional<i32> {},
traversal.has_value() ? CanonicalTraversable::browser_history_traversal_stage_to_string(traversal->stage) : "none"sv,
traversable().queued_same_document_session_history_entries_for_debug(),
- m_navigate_back_action->enabled(),
- m_navigate_forward_action->enabled(),
+ m_can_navigate_back,
+ m_can_navigate_forward,
history_log_entries(traversable().session_history()));
}
@@ -4385,7 +4368,7 @@ void ViewImplementation::set_crash_state(Optional<CrashState> state)
auto active = state.has_value();
m_crash_state = move(state);
notify_content_blocking_changed();
- update_report_website_issue_action();
+ did_change_state();
if (on_crash_overlay_state_change)
on_crash_overlay_state_change(active);
}
@@ -4473,6 +4456,7 @@ void ViewImplementation::languages_changed()
void ViewImplementation::content_settings_changed()
{
apply_zoom_for_current_host();
+ did_change_state();
// The page displaying the tab, and any page chosen to host its next document, host the tab's traversable.
send_force_dark(page());
@@ -4905,198 +4889,53 @@ ErrorOr<LexicalPath> ViewImplementation::dump_gc_graph()
return path;
}
-void ViewImplementation::initialize_context_menus()
-{
- m_navigate_back_action = Action::create("Back"sv, ActionID::NavigateBack, [this]() {
- traverse_the_history_by_delta(-1);
- });
- m_navigate_forward_action = Action::create("Forward"sv, ActionID::NavigateForward, [this]() {
- traverse_the_history_by_delta(+1);
- });
- m_navigate_back_action->set_enabled(false);
- m_navigate_forward_action->set_enabled(false);
-
- m_reset_zoom_action = Action::create("100%"sv, ActionID::ResetZoomViaToolbar, [this]() {
- reset_zoom();
- });
- m_reset_zoom_action->set_tooltip("Reset zoom level"sv);
- m_reset_zoom_action->set_visible(false);
-
- m_copy_link_to_highlight_action = Action::create("Copy Link to Highlight"sv, ActionID::CopyLinkToHighlight, [this] {
- if (!m_text_fragment_url.has_value())
- return;
- Application::the().insert_clipboard_entry({ "text/plain"_string, url_text_to_copy(*m_text_fragment_url) });
- });
- m_copy_link_to_highlight_action->set_visible(false);
- m_copy_link_to_highlight_action->set_enabled(false);
-
- // Shown only while a DevTools client is attached to this tab — the element is the one under the cursor when the
- // context menu opened, so a page that changes while the menu is open still has the element the user clicked on.
- m_inspect_element_action = Action::create("Inspect Element"sv, ActionID::InspectElement, [this]() {
- if (m_inspect_element_hit_test_request_id.has_value())
- m_inspect_element_activated_before_hit_test = true;
- else if (m_inspect_element_node_id.has_value() && on_inspect_element_request)
- on_inspect_element_request(*m_inspect_element_node_id);
- });
- m_inspect_element_action->set_visible(false);
-}
-
-static NonnullRefPtr<Action> create_copy_url_action(StringView text, URL::URL const& url)
-{
- return Action::create(text, ActionID::CopyURL, [url]() {
- Application::the().insert_clipboard_entry({ "text/plain"_string, url_text_to_copy(url) });
- });
-}
-
-static void add_search_action(ViewImplementation& view, Menu& menu, Optional<String> const& selected_text)
+void ViewImplementation::send_context_menu_request(ContextMenuRequest request, Gfx::IntPoint content_position)
{
- auto const& search_engine = Application::settings().search_engine_settings().engine;
- if (!search_engine.has_value() || !selected_text.has_value())
- return;
-
- auto url = URL::Parser::basic_parse(search_engine->format_search_query_for_navigation(*selected_text));
- VERIFY(url.has_value());
-
- menu.add_action(Action::create(search_engine->format_search_query_for_display(*selected_text), ActionID::SearchSelectedText, [&view, url = url.release_value()]() {
- view.open_url_in_new_tab(url, Web::HTML::ActivateTab::Yes);
- }));
-}
-
-void ViewImplementation::open_context_menu(NonnullRefPtr<Menu> menu, Gfx::IntPoint content_position)
-{
- m_context_menu = move(menu);
- show_context_menu(*m_context_menu, to_widget_position(content_position));
-}
-
-NonnullRefPtr<Menu> ViewImplementation::create_page_context_menu(bool is_editable, Optional<String> const& selected_text, Optional<DictionaryLookup> const& lookup, Gfx::IntPoint content_position)
-{
- auto& application = Application::the();
-
- // The page's own items are left out where the menu is about the text.
- auto is_about_page = !is_editable && !selected_text.has_value();
-
- auto menu = Menu::create("Page Context Menu"sv);
- add_spelling_actions(*menu);
- menu->add_separator();
- add_look_up_action(*menu, lookup, content_position);
- if (is_about_page) {
- menu->add_action(*m_navigate_back_action);
- menu->add_action(*m_navigate_forward_action);
- menu->add_action(application.reload_action());
- }
- menu->add_separator();
- add_text_edit_actions(*menu, is_editable, selected_text.has_value());
- menu->add_separator();
- add_search_action(*this, *menu, selected_text);
- menu->add_action(*m_copy_link_to_highlight_action);
- menu->add_separator();
- if (is_about_page)
- menu->add_action(application.view_source_action());
- menu->add_action(application.report_website_issue_action());
- menu->add_separator();
- if (is_about_page) {
- menu->add_action(Action::create("Take Screenshot..."sv, ActionID::TakeScreenshot, [this]() {
- if (on_request_screenshot)
- on_request_screenshot();
- }));
- }
- menu->add_separator();
- menu->add_action(*m_inspect_element_action);
- return menu;
-}
-
-NonnullRefPtr<Menu> ViewImplementation::create_selected_text_link_context_menu(bool is_editable, String const& selected_text, URL::URL const& url, Optional<DictionaryLookup> const& lookup, Gfx::IntPoint content_position)
-{
- auto menu = Menu::create("Selected Text Link Context Menu"sv);
- add_look_up_action(*menu, lookup, content_position);
- add_open_url_actions(*menu, url, {});
- menu->add_separator();
- add_text_edit_actions(*menu, is_editable, true);
- menu->add_separator();
- add_search_action(*this, *menu, selected_text);
- menu->add_action(*m_copy_link_to_highlight_action);
- menu->add_separator();
- menu->add_action(*m_inspect_element_action);
- return menu;
-}
-
-// A URL from the context menu is the page's — a link's or an image's — so it's navigated to from the page, unless
-// the user selected it as text. Blink also starts a selected URL with no initiator (IDC_CONTENT_CONTEXT_GOTOURL).
-void ViewImplementation::add_open_in_new_tab_action(Menu& menu, StringView text, URL::URL const& url, Optional<Web::HTML::PreparedNavigationDescriptor> const& navigation)
-{
- menu.add_action(Action::create(text, ActionID::OpenInNewTab, [this, url, navigation]() {
- if (navigation.has_value())
- open_navigation_in_new_tab(*navigation, Web::HTML::ActivateTab::No);
- else
- open_url_in_new_tab(url, Web::HTML::ActivateTab::No);
- }));
-}
-
-void ViewImplementation::add_open_url_actions(Menu& menu, URL::URL const& url, Optional<Web::HTML::PreparedNavigationDescriptor> const& navigation)
-{
- add_open_in_new_tab_action(menu, "Open Link in New Tab"sv, url, navigation);
- if (m_is_private == IsPrivate::No) {
- menu.add_action(Action::create("Open Link in New Window"sv, ActionID::OpenInNewWindow, [this, url, navigation]() {
- if (navigation.has_value())
- open_navigation_in_new_window(*navigation, IsPrivate::No);
- else
- open_url_in_new_window(url, IsPrivate::No);
- }));
- }
- if (Application::the().supports_private_browsing_windows()) {
- menu.add_action(Action::create("Open Link in Private Window"sv, ActionID::OpenInNewPrivateWindow, [this, url, navigation]() {
- if (!navigation.has_value()) {
- open_url_in_new_window(url, IsPrivate::Yes);
- return;
- }
- // A private window still gets the page as the navigation's initiator, but never the page's URL as its
- // referrer — as in Gecko/Blink (Gecko URILoadingHelper, Blink IDC_CONTENT_CONTEXT_OPENLINKOFFTHERECORD).
- auto private_navigation = *navigation;
- private_navigation.referrer_policy = Web::ReferrerPolicy::ReferrerPolicy::NoReferrer;
- open_navigation_in_new_window(move(private_navigation), IsPrivate::Yes);
- }));
- }
+ request.widget_position = to_widget_position(content_position);
+ request.can_inspect_element = static_cast<bool>(on_inspect_element_request);
+ did_request_context_menu(move(request));
}
-static void insert_text_clipboard_entry(ByteString const& text)
+void ViewImplementation::inspect_context_menu_element()
{
- if (!text.is_empty())
- Application::the().insert_clipboard_entry({ "text/plain"_string, text });
+ if (m_inspect_element_hit_test_request_id.has_value())
+ m_inspect_element_activated_before_hit_test = true;
+ else if (m_inspect_element_node_id.has_value() && on_inspect_element_request)
+ on_inspect_element_request(*m_inspect_element_node_id);
}
-// Text can be edited only in a field, and copied only where there is some.
-void ViewImplementation::add_text_edit_actions(Menu& menu, bool is_editable, bool has_selected_text)
+void ViewImplementation::replace_context_menu_misspelling(Utf16String const& replacement)
{
- if (is_editable) {
- menu.add_action(Action::create("Cut"sv, ActionID::CutSelection, [this]() {
- cut_selected_text()->when_resolved(insert_text_clipboard_entry);
- }));
- }
- if (is_editable || has_selected_text) {
- menu.add_action(Action::create("Copy"sv, ActionID::CopySelection, [this]() {
- selected_text()->when_resolved(insert_text_clipboard_entry);
- }));
- }
- if (is_editable) {
- menu.add_action(Action::create("Paste"sv, ActionID::Paste, [this]() {
- paste_from_clipboard();
- }));
- menu.add_action(Action::create("Select All"sv, ActionID::SelectAll, [this]() {
- select_all();
- }));
- }
+ if (m_spelling_context_menu_page && m_spelling_context_menu_page->is_open())
+ m_spelling_context_menu_page->async_replace_context_menu_misspelling(replacement);
}
-void ViewImplementation::add_look_up_action(Menu& menu, Optional<DictionaryLookup> const& lookup, Gfx::IntPoint content_position)
+void ViewImplementation::do_context_menu_media_action(ContextMenuMediaAction action)
{
- if (!lookup.has_value() || !on_request_dictionary_lookup)
+ if (!m_media_context_menu_page || !m_media_context_menu_page->is_open())
return;
- auto position = to_widget_position(lookup->baseline_origin.value_or(content_position));
- menu.add_action(Action::create("Look Up"sv, ActionID::LookUpSelectedText, [this, lookup = *lookup, position]() {
- if (on_request_dictionary_lookup)
- on_request_dictionary_lookup(lookup, position);
- }));
+ auto& page = *m_media_context_menu_page;
+ switch (action) {
+ case ContextMenuMediaAction::TogglePlay:
+ page.async_toggle_media_play_state();
+ break;
+ case ContextMenuMediaAction::ToggleMute:
+ page.async_toggle_media_mute_state();
+ break;
+ case ContextMenuMediaAction::ToggleControls:
+ page.async_toggle_media_controls_state();
+ break;
+ case ContextMenuMediaAction::ToggleLoop:
+ page.async_toggle_media_loop_state();
+ break;
+ case ContextMenuMediaAction::TogglePictureInPicture:
+ page.async_toggle_media_picture_in_picture_state();
+ break;
+ case ContextMenuMediaAction::ToggleFullscreen:
+ page.async_toggle_media_fullscreen_state();
+ break;
+ }
}
void ViewImplementation::request_context_menu_dictionary_lookup(Function<void(Optional<DictionaryLookup> const&)> on_complete)
@@ -5188,27 +5027,6 @@ void ViewImplementation::update_context_menu_misspelling(Optional<Utf16String> m
m_spelling_guesses.shrink(MAX_SPELLING_GUESSES);
}
-void ViewImplementation::add_spelling_actions(Menu& menu)
-{
- if (!m_context_menu_misspelled_word.has_value())
- return;
-
- for (auto const& guess : m_spelling_guesses) {
- menu.add_action(Action::create(MUST(guess.utf16_view().to_utf8()), ActionID::SpellingGuess, [page = m_spelling_context_menu_page, guess]() {
- if (page && page->is_open())
- page->async_replace_context_menu_misspelling(guess);
- }));
- }
- if (m_spelling_guesses.is_empty()) {
- auto no_guesses_action = Action::create("No Guesses Found"sv, ActionID::NoSpellingGuesses, [] { });
- no_guesses_action->set_enabled(false);
- menu.add_action(move(no_guesses_action));
- }
- menu.add_separator();
- menu.add_action(Action::create("Ignore Spelling"sv, ActionID::IgnoreSpelling, [this, word = *m_context_menu_misspelled_word] { ignore_misspelling(word); }));
- menu.add_action(Action::create("Learn Spelling"sv, ActionID::LearnSpelling, [this, word = *m_context_menu_misspelled_word] { learn_misspelling(word); }));
-}
-
void ViewImplementation::ignore_misspelling(Utf16String const& word)
{
auto* spell_checker = Application::the().spell_checker();
@@ -5247,42 +5065,35 @@ void ViewImplementation::learn_misspelling(Utf16String const& word)
void ViewImplementation::activate_spelling_menu_action_for_testing(Badge<WebContentPage>, WebContentPage& requesting_page, Web::SpellingMenuActionForTesting action, u32 guess_index)
{
- auto menu = Menu::create("Spelling Menu"sv);
- add_spelling_actions(*menu);
-
+ // NB: This describes the spelling items a context menu offers, as the application shows them.
StringBuilder description;
- Vector<NonnullRefPtr<Action>> guess_actions;
- RefPtr<Action> ignore_action;
- RefPtr<Action> learn_action;
- menu->for_each_action([&](Action& menu_action) {
- if (!description.is_empty())
- description.append(" | "sv);
- description.append(menu_action.text());
-
- if (menu_action.id() == ActionID::SpellingGuess)
- guess_actions.append(menu_action);
- else if (menu_action.id() == ActionID::IgnoreSpelling)
- ignore_action = menu_action;
- else if (menu_action.id() == ActionID::LearnSpelling)
- learn_action = menu_action;
- });
- if (description.is_empty())
+ if (m_context_menu_misspelled_word.has_value()) {
+ for (auto const& guess : m_spelling_guesses) {
+ if (!description.is_empty())
+ description.append(" | "sv);
+ description.append(guess);
+ }
+ if (m_spelling_guesses.is_empty())
+ description.append("No Guesses Found"sv);
+ description.append(" | Ignore Spelling | Learn Spelling"sv);
+ } else {
description.append("(no spelling items)"sv);
+ }
switch (action) {
case Web::SpellingMenuActionForTesting::None:
break;
case Web::SpellingMenuActionForTesting::Guess:
- if (guess_index < guess_actions.size())
- guess_actions[guess_index]->activate();
+ if (m_context_menu_misspelled_word.has_value() && guess_index < m_spelling_guesses.size())
+ replace_context_menu_misspelling(m_spelling_guesses[guess_index]);
break;
case Web::SpellingMenuActionForTesting::IgnoreSpelling:
- if (ignore_action)
- ignore_action->activate();
+ if (m_context_menu_misspelled_word.has_value())
+ ignore_misspelling(*m_context_menu_misspelled_word);
break;
case Web::SpellingMenuActionForTesting::LearnSpelling:
- if (learn_action)
- learn_action->activate();
+ if (m_context_menu_misspelled_word.has_value())
+ learn_misspelling(*m_context_menu_misspelled_word);
break;
}
// The page gets this after whatever the action sent it, on the same connection.
@@ -5291,10 +5102,9 @@ void ViewImplementation::activate_spelling_menu_action_for_testing(Badge<WebCont
void ViewImplementation::request_text_fragment_url(Optional<u64> generation_id)
{
- m_text_fragment_url.clear();
m_pending_text_fragment_url_request_id.clear();
- m_copy_link_to_highlight_action->set_enabled(false);
- m_copy_link_to_highlight_action->set_visible(generation_id.has_value());
+ // NB: The link of an earlier context menu goes before the new menu is asked for, as the link can come first.
+ did_make_link_to_highlight({});
if (!generation_id.has_value())
return;
@@ -5308,17 +5118,15 @@ void ViewImplementation::did_generate_text_fragment_url(u64 request_id, Optional
if (!m_pending_text_fragment_url_request_id.has_value() || *m_pending_text_fragment_url_request_id != request_id)
return;
m_pending_text_fragment_url_request_id.clear();
- m_text_fragment_url = move(url);
- m_copy_link_to_highlight_action->set_enabled(m_text_fragment_url.has_value());
+ did_make_link_to_highlight(move(url));
}
-void ViewImplementation::prepare_inspect_element_action(Gfx::IntPoint content_position)
+void ViewImplementation::prepare_inspect_element(Gfx::IntPoint content_position)
{
m_inspect_element_node_id.clear();
m_inspect_element_hit_test_request_id.clear();
m_inspect_element_activated_before_hit_test = false;
- m_inspect_element_action->set_visible(static_cast<bool>(on_inspect_element_request));
if (!on_inspect_element_request)
return;
@@ -5330,29 +5138,34 @@ void ViewImplementation::prepare_inspect_element_action(Gfx::IntPoint content_po
void ViewImplementation::did_request_page_context_menu(Badge<WebContentPage>, WebContentPage& requesting_page, Gfx::IntPoint content_position, Web::ContextMenuForInputEventsTarget for_input_events_target, Optional<Utf16String> misspelled_word, Optional<u64> text_fragment_generation_id)
{
request_text_fragment_url(text_fragment_generation_id);
- prepare_inspect_element_action(content_position);
+ prepare_inspect_element(content_position);
m_spelling_context_menu_page = requesting_page;
update_context_menu_misspelling(move(misspelled_word));
auto request_id = ++m_context_menu_request_id;
auto weak_this = make_weak_ptr();
- request_context_menu_dictionary_lookup([weak_this, request_id, content_position, for_input_events_target](auto const& lookup) {
+ auto makes_link_to_highlight = text_fragment_generation_id.has_value();
+ request_context_menu_dictionary_lookup([weak_this, request_id, content_position, for_input_events_target, makes_link_to_highlight](auto const& lookup) {
if (!weak_this || request_id != weak_this->m_context_menu_request_id)
return;
- auto open_menu = [weak_this, request_id, content_position, for_input_events_target, lookup](Optional<String> selected_text) {
+ auto open_menu = [weak_this, request_id, content_position, for_input_events_target, lookup, makes_link_to_highlight](Optional<String> selected_text) {
if (!weak_this || request_id != weak_this->m_context_menu_request_id)
return;
- auto is_editable = for_input_events_target == Web::ContextMenuForInputEventsTarget::Yes;
- auto selected_text_url = selected_text.has_value() ? url_from_text(*selected_text) : OptionalNone {};
-
- if (selected_text_url.has_value()) {
- weak_this->open_context_menu(weak_this->create_selected_text_link_context_menu(is_editable, *selected_text, *selected_text_url, lookup, content_position), content_position);
- return;
- }
-
- weak_this->open_context_menu(weak_this->create_page_context_menu(is_editable, selected_text, lookup, content_position), content_position);
+ ContextMenuRequest request;
+ request.kind = ContextMenuKind::Page;
+ request.is_editable = for_input_events_target == Web::ContextMenuForInputEventsTarget::Yes;
+ request.selected_text = move(selected_text);
+ request.dictionary_lookup = lookup;
+ if (lookup.has_value() && weak_this->on_request_dictionary_lookup)
+ request.dictionary_lookup_position = weak_this->to_widget_position(lookup->baseline_origin.value_or(content_position));
+ else
+ request.dictionary_lookup.clear();
+ request.misspelled_word = weak_this->m_context_menu_misspelled_word;
+ request.spelling_guesses = weak_this->m_spelling_guesses;
+ request.makes_link_to_highlight = makes_link_to_highlight;
+ weak_this->send_context_menu_request(move(request), content_position);
};
if (lookup.has_value()) {
@@ -5366,44 +5179,17 @@ void ViewImplementation::did_request_page_context_menu(Badge<WebContentPage>, We
});
}
-void ViewImplementation::add_link_actions(Menu& menu, Web::HTML::PreparedNavigationDescriptor const& navigation)
-{
- auto url = navigation.url;
- auto copy_url_text = [&] {
- switch (url_type(url)) {
- case URLType::Email:
- return "Copy Email Address"sv;
- case URLType::Telephone:
- return "Copy Phone Number"sv;
- case URLType::Other:
- return "Copy Link"sv;
- }
- VERIFY_NOT_REACHED();
- }();
-
- add_open_url_actions(menu, url, navigation);
- menu.add_separator();
- menu.add_action(create_copy_url_action(copy_url_text, url));
- menu.add_action(Action::create("Download Linked File"sv, ActionID::DownloadLinkedFile, [this, url]() {
- download_url(url, PromptForPath::No);
- }));
- menu.add_action(Action::create("Save Link As…"sv, ActionID::DownloadLinkedFileAs, [this, url]() {
- download_url(url, PromptForPath::Yes);
- }));
-}
-
void ViewImplementation::did_request_link_context_menu(Badge<WebContentPage>, Gfx::IntPoint content_position, Web::HTML::PreparedNavigationDescriptor navigation, Optional<u64> text_fragment_generation_id)
{
request_text_fragment_url(text_fragment_generation_id);
- prepare_inspect_element_action(content_position);
+ prepare_inspect_element(content_position);
++m_context_menu_request_id;
- auto menu = Menu::create("Link Context Menu"sv);
- add_link_actions(*menu, navigation);
- menu->add_action(*m_copy_link_to_highlight_action);
- menu->add_separator();
- menu->add_action(*m_inspect_element_action);
- open_context_menu(move(menu), content_position);
+ ContextMenuRequest request;
+ request.kind = ContextMenuKind::Link;
+ request.link_navigation = move(navigation);
+ request.makes_link_to_highlight = text_fragment_generation_id.has_value();
+ send_context_menu_request(move(request), content_position);
}
void ViewImplementation::download_url(URL::URL const& url, PromptForPath prompt_for_path, Optional<String> const& image_mime_type)
@@ -5431,100 +5217,30 @@ void ViewImplementation::download_url(URL::URL const& url, PromptForPath prompt_
void ViewImplementation::did_request_image_context_menu(Badge<WebContentPage>, Gfx::IntPoint content_position, Web::HTML::PreparedNavigationDescriptor navigation, Optional<Gfx::ShareableBitmap> bitmap, Optional<Web::HTML::PreparedNavigationDescriptor> link_navigation, Optional<String> image_mime_type)
{
- prepare_inspect_element_action(content_position);
+ prepare_inspect_element(content_position);
++m_context_menu_request_id;
- auto url = navigation.url;
- auto copy_image_action = Action::create("Copy Image"sv, ActionID::CopyImage, [bitmap]() {
- if (!bitmap.has_value() || !bitmap->is_valid())
- return;
-
- auto encoded = Gfx::PNGWriter::encode(*bitmap->bitmap());
- if (encoded.is_error())
- return;
-
- Application::the().insert_clipboard_entry({ "image/png"_string, ByteString { encoded.value().bytes() } });
- });
- copy_image_action->set_enabled(bitmap.has_value());
-
- auto menu = Menu::create("Image Context Menu"sv);
- if (link_navigation.has_value()) {
- add_link_actions(*menu, *link_navigation);
- menu->add_separator();
- }
- add_open_in_new_tab_action(*menu, "Open Image in New Tab"sv, url, navigation);
- menu->add_action(Action::create("Open Image"sv, ActionID::OpenImage, [this, navigation]() {
- load(navigation);
- }));
- menu->add_separator();
- menu->add_action(move(copy_image_action));
- menu->add_action(create_copy_url_action("Copy Image Address"sv, url));
- menu->add_action(Action::create("Save Image As…"sv, ActionID::SaveImage, [this, url, image_mime_type = move(image_mime_type)]() {
- download_url(url, PromptForPath::Yes, image_mime_type);
- }));
- menu->add_separator();
- menu->add_action(*m_inspect_element_action);
- open_context_menu(move(menu), content_position);
+ ContextMenuRequest request;
+ request.kind = ContextMenuKind::Image;
+ request.navigation = move(navigation);
+ request.image_bitmap = move(bitmap);
+ request.link_navigation = move(link_navigation);
+ request.image_mime_type = move(image_mime_type);
+ send_context_menu_request(move(request), content_position);
}
void ViewImplementation::did_request_media_context_menu(Badge<WebContentPage>, WebContentPage& requesting_page, Gfx::IntPoint content_position, Web::MediaContextMenu media, Optional<Web::HTML::PreparedNavigationDescriptor> navigation)
{
- prepare_inspect_element_action(content_position);
+ prepare_inspect_element(content_position);
++m_context_menu_request_id;
- auto send_to_page = [page = NonnullRefPtr { requesting_page }](void (*send)(WebContentPage&)) {
- return [page, send]() {
- if (page->is_open())
- send(*page);
- };
- };
- auto toggle_play_state = [](WebContentPage& page) { page.async_toggle_media_play_state(); };
- auto toggle_mute_state = [](WebContentPage& page) { page.async_toggle_media_mute_state(); };
- auto toggle_controls_state = [](WebContentPage& page) { page.async_toggle_media_controls_state(); };
- auto toggle_picture_in_picture_state = [](WebContentPage& page) { page.async_toggle_media_picture_in_picture_state(); };
- auto toggle_fullscreen_state = [](WebContentPage& page) { page.async_toggle_media_fullscreen_state(); };
-
- auto menu = Menu::create("Media Context Menu"sv);
- if (media.is_playing)
- menu->add_action(Action::create("Pause"sv, ActionID::PauseMedia, send_to_page(toggle_play_state)));
- else
- menu->add_action(Action::create("Play"sv, ActionID::PlayMedia, send_to_page(toggle_play_state)));
- if (media.is_muted)
- menu->add_action(Action::create("Unmute"sv, ActionID::UnmuteMedia, send_to_page(toggle_mute_state)));
- else
- menu->add_action(Action::create("Mute"sv, ActionID::MuteMedia, send_to_page(toggle_mute_state)));
- if (media.has_user_agent_controls)
- menu->add_action(Action::create("Hide Controls"sv, ActionID::HideControls, send_to_page(toggle_controls_state)));
- else
- menu->add_action(Action::create("Show Controls"sv, ActionID::ShowControls, send_to_page(toggle_controls_state)));
- auto loop_action = Action::create_checkable("Loop"sv, ActionID::ToggleMediaLoopState, send_to_page([](WebContentPage& page) {
- page.async_toggle_media_loop_state();
- }));
- loop_action->set_checked(media.is_looping);
- menu->add_action(move(loop_action));
- menu->add_separator();
- if (media.is_picture_in_picture)
- menu->add_action(Action::create("Exit Picture in Picture"sv, ActionID::ExitPictureInPicture, send_to_page(toggle_picture_in_picture_state)));
- else if (media.can_enter_picture_in_picture)
- menu->add_action(Action::create("Picture in Picture"sv, ActionID::EnterPictureInPicture, send_to_page(toggle_picture_in_picture_state)));
- if (media.is_video && media.is_fullscreen)
- menu->add_action(Action::create("Exit Full Screen"sv, ActionID::ExitFullscreen, send_to_page(toggle_fullscreen_state)));
- else if (media.is_video)
- menu->add_action(Action::create("Full Screen"sv, ActionID::EnterFullscreen, send_to_page(toggle_fullscreen_state)));
- menu->add_separator();
- if (media.media_url.has_value()) {
- auto url = *media.media_url;
- add_open_in_new_tab_action(*menu, media.is_video ? "Open Video in New Tab"sv : "Open Audio in New Tab"sv, url, navigation);
- if (navigation.has_value()) {
- menu->add_action(Action::create(media.is_video ? "Open Video"sv : "Open Audio"sv, media.is_video ? ActionID::OpenVideo : ActionID::OpenAudio, [this, navigation = *navigation]() {
- load(navigation);
- }));
- }
- menu->add_action(create_copy_url_action(media.is_video ? "Copy Video Address"sv : "Copy Audio Address"sv, url));
- }
- menu->add_separator();
- menu->add_action(*m_inspect_element_action);
- open_context_menu(move(menu), content_position);
+ m_media_context_menu_page = requesting_page;
+
+ ContextMenuRequest request;
+ request.kind = ContextMenuKind::Media;
+ request.media = move(media);
+ request.navigation = move(navigation);
+ send_context_menu_request(move(request), content_position);
}
u64 ViewImplementation::add_navigation_listener(NavigationListener listener)
diff --git a/Libraries/LibWebView/ViewImplementation.h b/Libraries/LibWebView/ViewImplementation.h
index d6acf7951dc..a617e788dc6 100644
--- a/Libraries/LibWebView/ViewImplementation.h
+++ b/Libraries/LibWebView/ViewImplementation.h
@@ -72,6 +72,7 @@
#include <LibWebView/BrowsingSession.h>
#include <LibWebView/CanonicalTraversable.h>
#include <LibWebView/ContentBlocking.h>
+#include <LibWebView/ContextMenuRequest.h>
#include <LibWebView/ExternalURLHandler.h>
#include <LibWebView/Forward.h>
#include <LibWebView/HistoryVisitTransition.h>
@@ -713,9 +714,23 @@ public:
void did_request_file_picker(Badge<WebContentPage>, WebContentPage& requesting_page, Web::HTML::FileFilter const& accepted_file_types, Web::HTML::AllowMultipleFiles);
void did_request_select_dropdown(Badge<WebContentPage>, WebContentPage& requesting_page, Gfx::IntPoint content_position, i32 minimum_width, Vector<Web::HTML::SelectItem> items);
- Action& navigate_back_action() { return *m_navigate_back_action; }
- Action& navigate_forward_action() { return *m_navigate_forward_action; }
- Action& reset_zoom_action() { return *m_reset_zoom_action; }
+ // Something the UI shows about this view may have changed, like whether it can undo or show its source.
+ virtual void did_change_state() { }
+
+ bool can_navigate_back() const { return m_can_navigate_back; }
+ bool can_navigate_forward() const { return m_can_navigate_forward; }
+
+ // What the application offers in a context menu, it asks the view to do.
+ void inspect_context_menu_element();
+ void replace_context_menu_misspelling(Utf16String const&);
+ void ignore_misspelling(Utf16String const&);
+ void learn_misspelling(Utf16String const&);
+ void do_context_menu_media_action(ContextMenuMediaAction);
+ enum class PromptForPath : u8 {
+ No,
+ Yes,
+ };
+ void download_url(URL::URL const&, PromptForPath, Optional<String> const& image_mime_type = {});
PageTranslation& page_translation() { return *m_page_translation; }
@@ -726,7 +741,8 @@ public:
virtual Gfx::IntPoint to_widget_position(Gfx::IntPoint content_position) const = 0;
protected:
- virtual void show_context_menu(Menu&, [[maybe_unused]] Gfx::IntPoint widget_position) { }
+ virtual void did_request_context_menu(ContextMenuRequest) { }
+ virtual void did_make_link_to_highlight(Optional<URL::URL>) { }
void will_apply_history_traversal_step(Web::HTML::CrossProcessId operation_id);
void did_apply_top_level_history_traversal_step(Web::HTML::CrossProcessId operation_id);
@@ -852,17 +868,7 @@ protected:
virtual void geolocation_settings_changed() override;
virtual void global_privacy_control_changed() override;
- void initialize_context_menus();
- void open_context_menu(NonnullRefPtr<Menu>, Gfx::IntPoint content_position);
- NonnullRefPtr<Menu> create_page_context_menu(bool is_editable, Optional<String> const& selected_text, Optional<DictionaryLookup> const&, Gfx::IntPoint content_position);
- NonnullRefPtr<Menu> create_selected_text_link_context_menu(bool is_editable, String const& selected_text, URL::URL const&, Optional<DictionaryLookup> const&, Gfx::IntPoint content_position);
- void add_open_in_new_tab_action(Menu&, StringView text, URL::URL const&, Optional<Web::HTML::PreparedNavigationDescriptor> const&);
- void add_open_url_actions(Menu&, URL::URL const&, Optional<Web::HTML::PreparedNavigationDescriptor> const&);
- void add_link_actions(Menu&, Web::HTML::PreparedNavigationDescriptor const&);
- void add_text_edit_actions(Menu&, bool is_editable, bool has_selected_text);
- void add_look_up_action(Menu&, Optional<DictionaryLookup> const&, Gfx::IntPoint content_position);
- void update_report_website_issue_action();
- void update_view_source_action();
+ void send_context_menu_request(ContextMenuRequest, Gfx::IntPoint content_position);
void handle_external_url(URL::URL, URL::Origin, bool has_transient_activation, Function<void()> on_handler_unavailable = {});
void handle_external_url_from_user_input(URL::URL const&, Function<void()> on_handler_unavailable = {});
void complete_external_url_request();
@@ -871,11 +877,6 @@ protected:
void request_context_menu_dictionary_lookup(Function<void(Optional<DictionaryLookup> const&)> on_complete);
NonnullRefPtr<Core::Promise<bool>> select_word_for_dictionary_lookup(Gfx::IntPoint widget_position);
void reject_pending_selection_requests();
- enum class PromptForPath : u8 {
- No,
- Yes,
- };
- void download_url(URL::URL const&, PromptForPath, Optional<String> const& image_mime_type = {});
String m_window_handle;
NonnullOwnPtr<PageBackingStores> m_backing_stores { make<PageBackingStores>() };
@@ -915,22 +916,17 @@ protected:
bool m_frame_rate_capped { false };
bool m_scales_frames_to_fit { false };
- // NB: The UI holds a shown menu's actions weakly, so the view keeps the last menu until it shows another.
- RefPtr<Menu> m_context_menu;
u64 m_context_menu_request_id { 0 };
+ RefPtr<WebContentPage> m_media_context_menu_page;
SpellChecker::DocumentTag spell_document_tag();
Optional<SpellChecker::DocumentTag> m_spell_document_tag;
- RefPtr<Action> m_navigate_back_action;
- RefPtr<Action> m_navigate_forward_action;
+ bool m_can_navigate_back { false };
+ bool m_can_navigate_forward { false };
OwnPtr<PageTranslation> m_page_translation;
- RefPtr<Action> m_reset_zoom_action;
-
- RefPtr<Action> m_copy_link_to_highlight_action;
- Optional<URL::URL> m_text_fragment_url;
u64 m_next_text_fragment_url_request_id { 1 };
Optional<u64> m_pending_text_fragment_url_request_id;
@@ -940,8 +936,7 @@ protected:
HashMap<u64, NonnullRefPtr<Core::Promise<bool>>> m_pending_select_word_for_dictionary_lookup_requests;
HashMap<u64, NonnullRefPtr<Core::Promise<ByteString>>> m_pending_cut_selected_text_requests;
- void prepare_inspect_element_action(Gfx::IntPoint content_position);
- RefPtr<Action> m_inspect_element_action;
+ void prepare_inspect_element(Gfx::IntPoint content_position);
Optional<u64> m_inspect_element_hit_test_request_id;
Optional<Web::UniqueNodeID> m_inspect_element_node_id;
bool m_inspect_element_activated_before_hit_test { false };
@@ -1133,9 +1128,6 @@ protected:
static constexpr size_t MAX_SPELLING_GUESSES = 5;
void update_context_menu_misspelling(Optional<Utf16String> misspelled_word);
- void add_spelling_actions(Menu&);
- void ignore_misspelling(Utf16String const&);
- void learn_misspelling(Utf16String const&);
Optional<Utf16String> m_context_menu_misspelled_word;
Vector<Utf16String> m_spelling_guesses;
diff --git a/Services/Hub/ActionMirror.cpp b/Services/Hub/ActionMirror.cpp
deleted file mode 100644
index dfb0b6f0425..00000000000
--- a/Services/Hub/ActionMirror.cpp
+++ /dev/null
@@ -1,190 +0,0 @@
-/*
- * Copyright (c) 2026-present, the Ladybird developers.
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
-
-#include <Hub/ActionMirror.h>
-#include <Hub/Application.h>
-#include <Hub/ConnectionFromUI.h>
-#include <LibWebView/PageTranslation.h>
-#include <LibWebView/ViewImplementation.h>
-
-namespace Hub {
-
-ActionMirror& ActionMirror::the()
-{
- // NB: Actions can go away while the process exits, and they tell this object when they do.
- static auto& s_the = *new ActionMirror;
- return s_the;
-}
-
-struct ActionObserver final : public WebView::Action::Observer {
- ActionObserver(u64 handle, WebView::Action const& action)
- : m_handle(handle)
- , m_action(&action)
- {
- }
-
- // NB: An action owns its observers, so this runs as the action goes away.
- virtual ~ActionObserver() override { ActionMirror::the().retire(m_handle, m_action); }
-
- virtual void on_text_changed(WebView::Action& action) override { did_change(action); }
- virtual void on_tooltip_changed(WebView::Action& action) override { did_change(action); }
- virtual void on_enabled_state_changed(WebView::Action& action) override { did_change(action); }
- virtual void on_visible_state_changed(WebView::Action& action) override { did_change(action); }
- virtual void on_engaged_state_changed(WebView::Action& action) override { did_change(action); }
- virtual void on_checked_state_changed(WebView::Action& action) override { did_change(action); }
-
- static void did_change(WebView::Action& action)
- {
- if (auto* ui = ConnectionFromUI::the())
- ui->async_action_changed(ActionMirror::the().state_of(action));
- }
-
- u64 m_handle { 0 };
- WebView::Action const* m_action { nullptr };
-};
-
-u64 ActionMirror::handle_of(WebView::Action& action)
-{
- if (auto handle = m_handles.get(&action); handle.has_value())
- return *handle;
-
- auto handle = m_next_handle++;
- m_handles.set(&action, handle);
- m_actions.set(handle, action.make_weak_ptr<WebView::Action>());
- action.add_observer(make<ActionObserver>(handle, action));
- return handle;
-}
-
-void ActionMirror::retire(u64 handle, WebView::Action const* action)
-{
- m_actions.remove(handle);
- m_handles.remove(action);
- if (auto* ui = ConnectionFromUI::the())
- ui->async_action_retired(handle);
-}
-
-template<typename MenuOrAction>
-static void copy_properties(MenuOrAction const& menu_or_action, Vector<String>& names, Vector<String>& values)
-{
- for (auto const& [name, value] : menu_or_action.properties()) {
- names.append(MUST(String::from_utf8(name)));
- values.append(value);
- }
-}
-
-ActionState ActionMirror::state_of(WebView::Action& action)
-{
- ActionState state;
- state.handle = handle_of(action);
- state.id = action.id();
- state.text = MUST(String::from_utf8(action.text()));
- if (action.has_tooltip())
- state.tooltip = MUST(String::from_utf8(action.tooltip()));
- state.enabled = action.enabled();
- state.visible = action.visible();
- state.engaged = action.engaged();
- if (action.is_checkable())
- state.checked = action.checked();
- if (auto icon = action.png_icon(); icon.has_value())
- state.png_icon = *icon;
- copy_properties(action, state.property_names, state.property_values);
- return state;
-}
-
-static MenuEntry menu_entry(MenuEntryKind kind)
-{
- MenuEntry entry;
- entry.kind = kind;
- return entry;
-}
-
-Vector<MenuEntry> ActionMirror::entries_of(WebView::Menu& menu)
-{
- Vector<MenuEntry> entries;
- auto add_menu = [&](auto& add_menu, WebView::Menu& menu) -> void {
- auto entry = menu_entry(MenuEntryKind::Submenu);
- entry.title = MUST(String::from_utf8(menu.title()));
- entry.visible = menu.visible();
- entry.is_group = menu.is_group();
- entry.render_group_icon = menu.render_group_icon();
- copy_properties(menu, entry.property_names, entry.property_values);
- entries.append(move(entry));
-
- for (auto& item : menu.items()) {
- item.visit(
- [&](NonnullRefPtr<WebView::Action>& action) {
- auto action_entry = menu_entry(MenuEntryKind::Action);
- action_entry.action = state_of(*action);
- entries.append(move(action_entry));
- },
- [&](NonnullRefPtr<WebView::Menu>& submenu) {
- add_menu(add_menu, *submenu);
- },
- [&](WebView::Separator) {
- entries.append(menu_entry(MenuEntryKind::Separator));
- });
- }
- entries.append(menu_entry(MenuEntryKind::EndOfSubmenu));
- };
- add_menu(add_menu, menu);
- return entries;
-}
-
-ApplicationActions ActionMirror::application_actions()
-{
- auto& application = Application::the();
- ApplicationActions actions;
- actions.reload_action = state_of(application.reload_action());
- actions.reload_bypassing_cache_action = state_of(application.reload_bypassing_cache_action());
- actions.copy_selection_action = state_of(application.copy_selection_action());
- actions.undo_action = state_of(application.undo_action());
- actions.redo_action = state_of(application.redo_action());
- actions.cut_selection_action = state_of(application.cut_selection_action());
- actions.paste_action = state_of(application.paste_action());
- actions.select_all_action = state_of(application.select_all_action());
- actions.open_about_page_action = state_of(application.open_about_page_action());
- actions.open_downloads_page_action = state_of(application.open_downloads_page_action());
- actions.open_settings_page_action = state_of(application.open_settings_page_action());
- actions.zoom_in_action = state_of(application.zoom_in_action());
- actions.zoom_out_action = state_of(application.zoom_out_action());
- actions.reset_zoom_action = state_of(application.reset_zoom_action());
- actions.force_dark_action = state_of(application.force_dark_action());
- actions.toggle_vertical_tabs_expanded_action = state_of(application.toggle_vertical_tabs_expanded_action());
- actions.toggle_menu_bar_action = state_of(application.toggle_menu_bar_action());
- actions.view_source_action = state_of(application.view_source_action());
- actions.report_website_issue_action = state_of(application.report_website_issue_action());
- actions.zoom_menu = entries_of(application.zoom_menu());
- actions.color_scheme_menu = entries_of(application.color_scheme_menu());
- actions.contrast_menu = entries_of(application.contrast_menu());
- actions.motion_menu = entries_of(application.motion_menu());
- actions.history_menu = entries_of(application.history_menu());
- actions.inspect_menu = entries_of(application.inspect_menu());
- actions.debug_menu = entries_of(application.debug_menu());
- return actions;
-}
-
-ViewActions ActionMirror::view_actions(WebView::ViewImplementation& view)
-{
- ViewActions actions;
- actions.navigate_back_action = state_of(view.navigate_back_action());
- actions.navigate_forward_action = state_of(view.navigate_forward_action());
- actions.reset_zoom_action = state_of(view.reset_zoom_action());
- actions.translate_action = state_of(view.page_translation().translate_action());
- return actions;
-}
-
-void ActionMirror::activate(u64 handle, Optional<bool> checked)
-{
- auto* action = m_actions.get(handle).value_or(nullptr);
- if (!action)
- return;
- // NB: The UI has flipped its copy already, and what an action does often depends on its state.
- if (checked.has_value())
- action->set_checked(*checked);
- action->activate();
-}
-
-}
diff --git a/Services/Hub/ActionMirror.h b/Services/Hub/ActionMirror.h
deleted file mode 100644
index 3ee00549afd..00000000000
--- a/Services/Hub/ActionMirror.h
+++ /dev/null
@@ -1,38 +0,0 @@
-/*
- * Copyright (c) 2026-present, the Ladybird developers.
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
-
-#pragma once
-
-#include <AK/HashMap.h>
-#include <AK/WeakPtr.h>
-#include <Hub/HubClientEndpoint.h>
-#include <LibWebView/Forward.h>
-#include <LibWebView/Menu.h>
-
-namespace Hub {
-
-// Lets the UI show the Hub's actions and menus. The UI knows an action by a handle, sees it change, and activates it.
-class ActionMirror {
-public:
- static ActionMirror& the();
-
- ActionState state_of(WebView::Action&);
- Vector<MenuEntry> entries_of(WebView::Menu&);
- ApplicationActions application_actions();
- ViewActions view_actions(WebView::ViewImplementation&);
-
- void activate(u64 handle, Optional<bool> checked);
- void retire(u64 handle, WebView::Action const*);
-
-private:
- u64 handle_of(WebView::Action&);
-
- HashMap<u64, WeakPtr<WebView::Action>> m_actions;
- HashMap<WebView::Action const*, u64> m_handles;
- u64 m_next_handle { 1 };
-};
-
-}
diff --git a/Services/Hub/Application.cpp b/Services/Hub/Application.cpp
index 2d6f8ebf471..c584e7ae4be 100644
--- a/Services/Hub/Application.cpp
+++ b/Services/Hub/Application.cpp
@@ -7,7 +7,6 @@
#include <AK/Hex.h>
#include <AK/MemoryStream.h>
#include <AK/Queue.h>
-#include <Hub/ActionMirror.h>
#include <Hub/Application.h>
#include <Hub/ConnectionFromUI.h>
#include <Hub/Proxies.h>
@@ -101,7 +100,6 @@ void Application::ui_did_start()
void Application::set_active_view(Optional<u64> view_id)
{
m_active_view_id = view_id;
- update_actions_for_active_web_view();
}
Optional<WebView::ViewImplementation&> Application::active_web_view() const
diff --git a/Services/Hub/CMakeLists.txt b/Services/Hub/CMakeLists.txt
index 7d7b2e5fc59..8c4584e2d81 100644
--- a/Services/Hub/CMakeLists.txt
+++ b/Services/Hub/CMakeLists.txt
@@ -13,7 +13,6 @@ target_include_directories(hubipc PRIVATE ${LADYBIRD_SOURCE_DIR}/Services/)
target_link_libraries(hubipc PRIVATE LibCore LibGfx LibHTTP LibIPC LibURL LibWebView)
set(SOURCES
- ActionMirror.cpp
Application.cpp
ConnectionFromUI.cpp
Proxies.cpp
diff --git a/Services/Hub/ConnectionFromUI.cpp b/Services/Hub/ConnectionFromUI.cpp
index 46ce7aab12c..887978bd7a3 100644
--- a/Services/Hub/ConnectionFromUI.cpp
+++ b/Services/Hub/ConnectionFromUI.cpp
@@ -4,10 +4,10 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
-#include <Hub/ActionMirror.h>
#include <Hub/Application.h>
#include <Hub/ConnectionFromUI.h>
#include <Hub/Proxies.h>
+#include <LibCore/Environment.h>
#include <LibCore/EventLoop.h>
#include <LibHTTP/HeaderList.h>
#include <LibRequests/RequestClient.h>
@@ -555,7 +555,11 @@ Messages::HubServer::InitializeResponse ConnectionFromUI::initialize()
InitialState state;
state.settings_json = Application::settings().serialize_json().serialized().to_byte_string();
- state.actions = ActionMirror::the().application_actions();
+ state.user_agent = Application::the().user_agent_string();
+ state.navigator_compatibility_mode = Application::the().navigator_compatibility_mode();
+ state.scripting_enabled = Application::the().scripting_enabled();
+ state.blocks_pop_ups = Application::the().blocks_pop_ups();
+ state.devtools_enabled = Application::the().devtools_enabled();
state.downloads = downloads();
state.urls = options.urls;
state.raw_urls = options.raw_urls;
@@ -582,7 +586,7 @@ Messages::HubServer::CreateViewResponse ConnectionFromUI::create_view(Hub::NewVi
.websites_prefer_dark = request.websites_prefer_dark,
.is_tab = request.is_tab,
});
- return { view.view_id(), view.current_state(), ActionMirror::the().view_actions(view) };
+ return { view.view_id(), view.current_state() };
}
Messages::HubServer::ViewDescriptionResponse ConnectionFromUI::view_description(u64 view_id)
@@ -590,8 +594,8 @@ Messages::HubServer::ViewDescriptionResponse ConnectionFromUI::view_description(
// NB: The UI can ask about a view that the Hub has let go of already, and must not take the Hub down by that.
auto* view = find_view(view_id);
if (!view)
- return { Hub::ViewState {}, Hub::ViewActions {} };
- return { view->current_state(), ActionMirror::the().view_actions(*view) };
+ return Hub::ViewState {};
+ return { view->current_state() };
}
void ConnectionFromUI::crash_reports_changed()
@@ -639,7 +643,57 @@ void ConnectionFromUI::cancel_report_post(u64 post_id)
void ConnectionFromUI::toggle_devtools_enabled()
{
if (auto result = Application::the().toggle_devtools_enabled(); result.is_error())
- dbgln("Unable to toggle DevTools: {}", result.error());
+ static_cast<WebView::Application&>(Application::the()).display_error_dialog(MUST(String::formatted("Unable to start DevTools: {}", result.error())));
+}
+
+void ConnectionFromUI::set_preferred_contrast(Web::CSS::PreferredContrast contrast)
+{
+ Application::the().set_preferred_contrast(contrast);
+}
+
+void ConnectionFromUI::set_preferred_motion(Web::CSS::PreferredMotion motion)
+{
+ Application::the().set_preferred_motion(motion);
+}
+
+void ConnectionFromUI::set_user_agent_string(String user_agent)
+{
+ Application::the().set_user_agent_string(move(user_agent));
+}
+
+void ConnectionFromUI::set_navigator_compatibility_mode(Web::NavigatorCompatibilityMode mode)
+{
+ Application::the().set_navigator_compatibility_mode(mode);
+}
+
+void ConnectionFromUI::set_scripting_enabled(bool enabled)
+{
+ Application::the().set_scripting_enabled(enabled);
+}
+
+void ConnectionFromUI::set_blocks_pop_ups(bool blocks)
+{
+ Application::the().set_blocks_pop_ups(blocks);
+}
+
+void ConnectionFromUI::set_shows_line_box_borders(bool shows)
+{
+ Application::the().set_shows_line_box_borders(shows);
+}
+
+void ConnectionFromUI::set_shows_caret_hit_test_debug_overlay(bool shows)
+{
+ Application::the().set_shows_caret_hit_test_debug_overlay(shows);
+}
+
+void ConnectionFromUI::dump_cookies()
+{
+ Application::the().dump_cookies();
+}
+
+void ConnectionFromUI::crash_compositor_process()
+{
+ Application::the().crash_compositor_process();
}
void ConnectionFromUI::launch_devtools_client()
@@ -739,14 +793,6 @@ Messages::HubServer::ViewPageDescriptionResponse ConnectionFromUI::view_page_des
return { move(page.url), move(page.user_agent), page.viewport_size, page.device_pixel_ratio, page.zoom_level };
}
-Messages::HubServer::ViewTranslationMenuResponse ConnectionFromUI::view_translation_menu(u64 view_id)
-{
- auto* view = find_view(view_id);
- if (!view)
- return Vector<MenuEntry> {};
- return ActionMirror::the().entries_of(view->page_translation().translation_menu());
-}
-
Messages::HubServer::SessionWindowOpenedResponse ConnectionFromUI::session_window_opened(WebView::IsPrivate is_private)
{
auto id = session_store_of(is_private).window_opened();
@@ -878,11 +924,6 @@ void ConnectionFromUI::update_settings(u64 change_id, ByteString changes_json)
Application::settings().replace_with_json(settings, WebView::Settings::PersistChange::Yes);
}
-void ConnectionFromUI::activate_action(u64 handle, Optional<bool> checked)
-{
- ActionMirror::the().activate(handle, checked);
-}
-
void ConnectionFromUI::destroy_view(u64 view_id)
{
Application::the().destroy_view(view_id);
@@ -1042,6 +1083,122 @@ void ConnectionFromUI::view_reload(u64 view_id, WebView::BypassCache bypass_cach
}
}
+void ConnectionFromUI::view_undo(u64 view_id)
+{
+ if (auto* view = find_view(view_id))
+ view->undo();
+}
+
+void ConnectionFromUI::view_redo(u64 view_id)
+{
+ if (auto* view = find_view(view_id))
+ view->redo();
+}
+
+void ConnectionFromUI::view_cut_selection(u64 view_id)
+{
+ if (auto* view = find_view(view_id)) {
+ view->cut_selected_text()->when_resolved([](auto& text) {
+ if (!text.is_empty())
+ Application::the().insert_clipboard_entry({ "text/plain"_string, text });
+ });
+ }
+}
+
+void ConnectionFromUI::view_paste(u64 view_id)
+{
+ if (auto* view = find_view(view_id))
+ view->paste_from_clipboard();
+}
+
+void ConnectionFromUI::view_select_all(u64 view_id)
+{
+ if (auto* view = find_view(view_id))
+ view->select_all();
+}
+
+void ConnectionFromUI::view_get_source(u64 view_id)
+{
+ if (auto* view = find_view(view_id))
+ view->get_source();
+}
+
+void ConnectionFromUI::view_traverse_the_history_by_delta(u64 view_id, i32 delta)
+{
+ if (auto* view = find_view(view_id))
+ view->traverse_the_history_by_delta(delta);
+}
+
+void ConnectionFromUI::view_translate_to(u64 view_id, String target_language)
+{
+ if (auto* view = find_view(view_id); view && view->has_traversable())
+ view->page_translation().translate_to(target_language);
+}
+
+void ConnectionFromUI::view_show_original(u64 view_id)
+{
+ if (auto* view = find_view(view_id); view && view->has_traversable())
+ view->page_translation().show_original();
+}
+
+void ConnectionFromUI::view_download_url(u64 view_id, URL::URL url, bool prompt_for_path, Optional<String> image_mime_type)
+{
+ if (auto* view = find_view(view_id))
+ view->download_url(url, prompt_for_path ? WebView::ViewImplementation::PromptForPath::Yes : WebView::ViewImplementation::PromptForPath::No, image_mime_type);
+}
+
+void ConnectionFromUI::view_inspect_context_menu_element(u64 view_id)
+{
+ if (auto* view = find_view(view_id))
+ view->inspect_context_menu_element();
+}
+
+void ConnectionFromUI::view_replace_context_menu_misspelling(u64 view_id, Utf16String replacement)
+{
+ if (auto* view = find_view(view_id))
+ view->replace_context_menu_misspelling(replacement);
+}
+
+void ConnectionFromUI::view_ignore_misspelling(u64 view_id, Utf16String word)
+{
+ if (auto* view = find_view(view_id))
+ view->ignore_misspelling(word);
+}
+
+void ConnectionFromUI::view_learn_misspelling(u64 view_id, Utf16String word)
+{
+ if (auto* view = find_view(view_id))
+ view->learn_misspelling(word);
+}
+
+void ConnectionFromUI::view_do_context_menu_media_action(u64 view_id, WebView::ContextMenuMediaAction action)
+{
+ if (auto* view = find_view(view_id))
+ view->do_context_menu_media_action(action);
+}
+
+void ConnectionFromUI::view_debug_request(u64 view_id, ByteString request, ByteString argument)
+{
+ if (auto* view = find_view(view_id))
+ view->debug_request(request, argument);
+}
+
+void ConnectionFromUI::view_dump_gc_graph(u64 view_id)
+{
+ auto* view = find_view(view_id);
+ if (!view)
+ return;
+
+ auto gc_graph_path = view->dump_gc_graph();
+ if (gc_graph_path.is_error()) {
+ warnln("\033[31;1mFailed to dump GC graph: {}\033[0m", gc_graph_path.error());
+ return;
+ }
+ warnln("\033[33;1mDumped GC graph into {}\033[0m", gc_graph_path.value());
+ if (auto source_dir = Core::Environment::get("LADYBIRD_SOURCE_DIR"sv); source_dir.has_value())
+ warnln("\033[33;1mGC graph explorer: file://{}/Meta/gc-heap-explorer.html?script=file://{}\033[0m", *source_dir, gc_graph_path.value());
+}
+
void ConnectionFromUI::view_stop_loading(u64 view_id)
{
if (auto* view = find_view(view_id)) {
diff --git a/Services/Hub/ConnectionFromUI.h b/Services/Hub/ConnectionFromUI.h
index a55079b668a..657febaa862 100644
--- a/Services/Hub/ConnectionFromUI.h
+++ b/Services/Hub/ConnectionFromUI.h
@@ -72,6 +72,16 @@ private:
virtual void post_report(u64 post_id, URL::URL url, Vector<HTTP::Header> headers, ByteString body) override;
virtual void cancel_report_post(u64 post_id) override;
virtual void toggle_devtools_enabled() override;
+ virtual void set_preferred_contrast(Web::CSS::PreferredContrast) override;
+ virtual void set_preferred_motion(Web::CSS::PreferredMotion) override;
+ virtual void set_user_agent_string(String user_agent) override;
+ virtual void set_navigator_compatibility_mode(Web::NavigatorCompatibilityMode) override;
+ virtual void set_scripting_enabled(bool enabled) override;
+ virtual void set_blocks_pop_ups(bool blocks) override;
+ virtual void set_shows_line_box_borders(bool shows) override;
+ virtual void set_shows_caret_hit_test_debug_overlay(bool shows) override;
+ virtual void dump_cookies() override;
+ virtual void crash_compositor_process() override;
virtual void launch_devtools_client() override;
virtual void reset_private_browsing_session() override;
virtual void notify_compositor_gpu_presentation_unavailable() override;
@@ -86,7 +96,6 @@ private:
virtual Messages::HubServer::ViewHistoryStepAtDeltaResponse view_history_step_at_delta(u64 view_id, i32 delta) override;
virtual Messages::HubServer::ViewLookUpSelectedTextAtResponse view_look_up_selected_text_at(u64 view_id, Gfx::IntPoint position) override;
virtual Messages::HubServer::ViewPageDescriptionResponse view_page_description(u64 view_id) override;
- virtual Messages::HubServer::ViewTranslationMenuResponse view_translation_menu(u64 view_id) override;
virtual Messages::HubServer::SessionWindowOpenedResponse session_window_opened(WebView::IsPrivate is_private) override;
virtual Messages::HubServer::SessionWindowClosingResponse session_window_closing(WebView::IsPrivate is_private, WebView::SessionStore::WindowClosing closing) override;
virtual void session_window_activated(WebView::IsPrivate is_private, WebView::SessionWindowId id) override;
@@ -156,7 +165,6 @@ private:
virtual void answer_clipboard_item(u64 request_id, Web::Clipboard::SystemClipboardItem item) override;
virtual void answer_download_path(u64 request_id, Optional<ByteString> path) override;
virtual void update_settings(u64 change_id, ByteString changes_json) override;
- virtual void activate_action(u64 handle, Optional<bool> checked) override;
virtual void view_answer(u64 view_id, u64 answer_id, bool answer) override;
virtual void view_answer_permission_request(u64 view_id, u64 answer_id, Web::Permissions::PromptOutcome outcome) override;
virtual void destroy_view(u64 view_id) override;
@@ -185,6 +193,23 @@ private:
virtual void view_set_next_history_visit_transition(u64 view_id, WebView::HistoryVisitTransition transition) override;
virtual void view_reload(u64 view_id, WebView::BypassCache bypass_cache) override;
virtual void view_stop_loading(u64 view_id) override;
+ virtual void view_undo(u64 view_id) override;
+ virtual void view_redo(u64 view_id) override;
+ virtual void view_cut_selection(u64 view_id) override;
+ virtual void view_paste(u64 view_id) override;
+ virtual void view_select_all(u64 view_id) override;
+ virtual void view_get_source(u64 view_id) override;
+ virtual void view_traverse_the_history_by_delta(u64 view_id, i32 delta) override;
+ virtual void view_translate_to(u64 view_id, String target_language) override;
+ virtual void view_show_original(u64 view_id) override;
+ virtual void view_download_url(u64 view_id, URL::URL url, bool prompt_for_path, Optional<String> image_mime_type) override;
+ virtual void view_inspect_context_menu_element(u64 view_id) override;
+ virtual void view_replace_context_menu_misspelling(u64 view_id, Utf16String replacement) override;
+ virtual void view_ignore_misspelling(u64 view_id, Utf16String word) override;
+ virtual void view_learn_misspelling(u64 view_id, Utf16String word) override;
+ virtual void view_do_context_menu_media_action(u64 view_id, WebView::ContextMenuMediaAction action) override;
+ virtual void view_debug_request(u64 view_id, ByteString request, ByteString argument) override;
+ virtual void view_dump_gc_graph(u64 view_id) override;
virtual void view_zoom_in(u64 view_id) override;
virtual void view_zoom_out(u64 view_id) override;
virtual void view_reset_zoom(u64 view_id) override;
diff --git a/Services/Hub/HubClient.ipc b/Services/Hub/HubClient.ipc
index b9242703068..b344821f6b1 100644
--- a/Services/Hub/HubClient.ipc
+++ b/Services/Hub/HubClient.ipc
@@ -14,14 +14,15 @@
#include <LibWebCommon/NotificationsAPI/NotificationData.h>
#include <LibURL/URL.h>
#include <LibWebCommon/CSS/PreferredColorScheme.h>
+#include <LibWebCommon/Loader/NavigatorCompatibilityMode.h>
#include <LibWebCommon/Page/Translation.h>
#include <LibWebCommon/WebView/DictionaryLookup.h>
#include <LibWebCommon/HTML/FileFilter.h>
#include <LibWebCommon/HTML/SelectItem.h>
#include <LibWebView/AccessibilityTreeManager.h>
#include <LibWebView/Autocomplete.h>
+#include <LibWebView/ContextMenuRequest.h>
#include <LibWebView/FileDownloader.h>
-#include <LibWebView/Menu.h>
#include <LibWebView/TabPerformanceStats.h>
#include <LibWebCommon/Clipboard/SystemClipboard.h>
#include <LibWebCommon/HTML/PreparedNavigationDescriptor.h>
@@ -79,77 +80,6 @@ struct WebView::ViewImplementation::InputMethodState {
Optional<Web::DevicePixelRect> caret_rect;
}
-enum Hub::MenuEntryKind : u8 {
- Action,
- Separator,
- Submenu,
- EndOfSubmenu,
-}
-
-struct Hub::ActionState {
- u64 handle;
- WebView::ActionID id;
- String text;
- Optional<String> tooltip;
- bool enabled;
- bool visible;
- bool engaged;
- Optional<bool> checked;
- Optional<ByteBuffer> png_icon;
- Vector<String> property_names;
- Vector<String> property_values;
-}
-
-// A menu goes as a flat list, in which a submenu's entries follow its own, up to its end.
-struct Hub::MenuEntry {
- Hub::MenuEntryKind kind;
- Optional<Hub::ActionState> action;
- String title;
- bool visible;
- bool is_group;
- bool render_group_icon;
- Vector<String> property_names;
- Vector<String> property_values;
-}
-
-// The actions and menus of the application, which the UI shows in its windows.
-struct Hub::ApplicationActions {
- Hub::ActionState reload_action;
- Hub::ActionState reload_bypassing_cache_action;
- Hub::ActionState copy_selection_action;
- Hub::ActionState undo_action;
- Hub::ActionState redo_action;
- Hub::ActionState cut_selection_action;
- Hub::ActionState paste_action;
- Hub::ActionState select_all_action;
- Hub::ActionState open_about_page_action;
- Hub::ActionState open_downloads_page_action;
- Hub::ActionState open_settings_page_action;
- Hub::ActionState zoom_in_action;
- Hub::ActionState zoom_out_action;
- Hub::ActionState reset_zoom_action;
- Hub::ActionState force_dark_action;
- Hub::ActionState toggle_vertical_tabs_expanded_action;
- Hub::ActionState toggle_menu_bar_action;
- Hub::ActionState view_source_action;
- Hub::ActionState report_website_issue_action;
- Vector<Hub::MenuEntry> zoom_menu;
- Vector<Hub::MenuEntry> color_scheme_menu;
- Vector<Hub::MenuEntry> contrast_menu;
- Vector<Hub::MenuEntry> motion_menu;
- Vector<Hub::MenuEntry> history_menu;
- Vector<Hub::MenuEntry> inspect_menu;
- Vector<Hub::MenuEntry> debug_menu;
-}
-
-// The actions and menus of a view.
-struct Hub::ViewActions {
- Hub::ActionState navigate_back_action;
- Hub::ActionState navigate_forward_action;
- Hub::ActionState reset_zoom_action;
- Hub::ActionState translate_action;
-}
-
struct Hub::DownloadState {
u64 id;
WebView::IsPrivate is_private;
@@ -211,7 +141,11 @@ struct Hub::UIInfo {
struct Hub::InitialState {
ByteString settings_json;
ByteString config_directory;
- Hub::ApplicationActions actions;
+ String user_agent;
+ Web::NavigatorCompatibilityMode navigator_compatibility_mode;
+ bool scripting_enabled;
+ bool blocks_pop_ups;
+ bool devtools_enabled;
Vector<Hub::DownloadState> downloads;
Vector<URL::URL> urls;
Vector<ByteString> raw_urls;
@@ -222,6 +156,25 @@ struct Hub::InitialState {
bool profile_is_temporary;
}
+[ExistingCpp]
+struct WebView::ContextMenuRequest {
+ WebView::ContextMenuKind kind;
+ Gfx::IntPoint widget_position;
+ bool is_editable;
+ Optional<String> selected_text;
+ Optional<WebView::DictionaryLookup> dictionary_lookup;
+ Gfx::IntPoint dictionary_lookup_position;
+ Optional<Utf16String> misspelled_word;
+ Vector<Utf16String> spelling_guesses;
+ bool makes_link_to_highlight;
+ bool can_inspect_element;
+ Optional<Web::HTML::PreparedNavigationDescriptor> link_navigation;
+ Optional<Web::HTML::PreparedNavigationDescriptor> navigation;
+ Optional<Gfx::ShareableBitmap> image_bitmap;
+ Optional<String> image_mime_type;
+ Optional<Web::MediaContextMenu> media;
+}
+
// What the UI needs to know about a view at any time, without having to ask.
struct Hub::ViewState {
URL::URL url;
@@ -238,8 +191,17 @@ struct Hub::ViewState {
String force_dark_host;
i32 web_content_pid;
bool can_report_website_issue;
+ bool can_undo;
+ bool can_redo;
+ bool can_view_source;
+ bool can_navigate_back;
+ bool can_navigate_forward;
bool is_node_picker_active;
bool is_translated;
+ bool translation_offered;
+ Vector<String> translation_languages;
+ Optional<String> translation_target_language;
+ bool translation_needs_language_download;
WebView::ContentBlockingSnapshot content_blocking_snapshot;
WebView::ViewImplementation::InputMethodState input_method_state;
Web::HTML::MuteState page_mute_state;
@@ -312,9 +274,8 @@ endpoint HubClient
web_ui_opened(u64 web_ui_id, String host) =|
web_ui_message(u64 web_ui_id, String name, JsonValue data) =|
web_ui_closed(u64 web_ui_id) =|
- action_changed(Hub::ActionState state) =|
- action_retired(u64 handle) =|
- view_show_context_menu(u64 view_id, Vector<Hub::MenuEntry> entries, Gfx::IntPoint widget_position) =|
+ view_request_context_menu(u64 view_id, WebView::ContextMenuRequest request) =|
+ view_did_make_link_to_highlight(u64 view_id, Optional<URL::URL> url) =|
view_favicon_change(u64 view_id, Optional<Gfx::ShareableBitmap> favicon) =|
view_request_before_unload(u64 view_id, u64 answer_id, String source) =|
view_request_external_url_confirmation(u64 view_id, u64 answer_id, URL::URL url, URL::Origin initiator_origin, String application_name) =|
diff --git a/Services/Hub/HubServer.ipc b/Services/Hub/HubServer.ipc
index 9b16778c067..8bbad611db6 100644
--- a/Services/Hub/HubServer.ipc
+++ b/Services/Hub/HubServer.ipc
@@ -11,7 +11,10 @@
#include <LibGfx/Size.h>
#include <LibURL/Origin.h>
#include <LibURL/URL.h>
+#include <LibWebCommon/CSS/PreferredContrast.h>
+#include <LibWebCommon/CSS/PreferredMotion.h>
#include <LibWebCommon/HTML/ActivateTab.h>
+#include <LibWebCommon/Loader/NavigatorCompatibilityMode.h>
#include <LibWebCommon/HTML/VisibilityState.h>
#include <LibWebCommon/Clipboard/SystemClipboard.h>
#include <LibWebView/OmniboxEngagement.h>
@@ -230,8 +233,8 @@ struct Hub::NewViewRequest {
endpoint HubServer
{
initialize() => (Hub::InitialState state)
- create_view(Hub::NewViewRequest request) => (u64 view_id, Hub::ViewState state, Hub::ViewActions actions)
- view_description(u64 view_id) => (Hub::ViewState state, Hub::ViewActions actions)
+ create_view(Hub::NewViewRequest request) => (u64 view_id, Hub::ViewState state)
+ view_description(u64 view_id) => (Hub::ViewState state)
ui_did_start() =|
favicons_in_use(Vector<String> favicon_hashes) =|
answer_keeps_omnibox_engagements(u64 request_id, bool keeps_engagements) =|
@@ -242,6 +245,16 @@ endpoint HubServer
cancel_report_post(u64 post_id) =|
toggle_devtools_enabled() =|
+ set_preferred_contrast(Web::CSS::PreferredContrast contrast) =|
+ set_preferred_motion(Web::CSS::PreferredMotion motion) =|
+ set_user_agent_string(String user_agent) =|
+ set_navigator_compatibility_mode(Web::NavigatorCompatibilityMode mode) =|
+ set_scripting_enabled(bool enabled) =|
+ set_blocks_pop_ups(bool blocks) =|
+ set_shows_line_box_borders(bool shows) =|
+ set_shows_caret_hit_test_debug_overlay(bool shows) =|
+ dump_cookies() =|
+ crash_compositor_process() =|
launch_devtools_client() =|
reset_private_browsing_session() =|
notify_compositor_gpu_presentation_unavailable() =|
@@ -258,7 +271,6 @@ endpoint HubServer
view_history_step_at_delta(u64 view_id, i32 delta) => (Optional<i32> step)
view_look_up_selected_text_at(u64 view_id, Gfx::IntPoint position) => (bool looked_up)
view_page_description(u64 view_id) => (URL::URL url, String user_agent, Gfx::IntSize viewport_size, double device_pixel_ratio, double zoom_level)
- view_translation_menu(u64 view_id) => (Vector<Hub::MenuEntry> entries)
session_window_opened(WebView::IsPrivate is_private) => (Optional<WebView::SessionWindowId> id)
session_window_closing(WebView::IsPrivate is_private, WebView::SessionStore::WindowClosing closing) => (bool succeeded)
@@ -287,6 +299,15 @@ endpoint HubServer
view_open_navigation_in_new_tab(u64 view_id, Web::HTML::PreparedNavigationDescriptor navigation, Web::HTML::ActivateTab activate_tab) =|
view_open_navigation_in_new_window(u64 view_id, Web::HTML::PreparedNavigationDescriptor navigation, WebView::IsPrivate is_private) =|
view_traverse_the_history_to_step(u64 view_id, i32 step) =|
+ view_traverse_the_history_by_delta(u64 view_id, i32 delta) =|
+ view_translate_to(u64 view_id, String target_language) =|
+ view_show_original(u64 view_id) =|
+ view_download_url(u64 view_id, URL::URL url, bool prompt_for_path, Optional<String> image_mime_type) =|
+ view_inspect_context_menu_element(u64 view_id) =|
+ view_replace_context_menu_misspelling(u64 view_id, Utf16String replacement) =|
+ view_ignore_misspelling(u64 view_id, Utf16String word) =|
+ view_learn_misspelling(u64 view_id, Utf16String word) =|
+ view_do_context_menu_media_action(u64 view_id, WebView::ContextMenuMediaAction action) =|
view_set_session_tab_id(u64 view_id, Optional<WebView::SessionTabId> session_tab_id) =|
view_file_picker_closed(u64 view_id, Vector<Web::HTML::SelectedFile> selected_files) =|
view_save_screenshot(u64 view_id, Gfx::ShareableBitmap bitmap) =|
@@ -329,7 +350,6 @@ endpoint HubServer
answer_clipboard_item(u64 request_id, Web::Clipboard::SystemClipboardItem item) =|
answer_download_path(u64 request_id, Optional<ByteString> path) =|
update_settings(u64 change_id, ByteString changes_json) =|
- activate_action(u64 handle, Optional<bool> checked) =|
view_answer(u64 view_id, u64 answer_id, bool answer) =|
view_answer_permission_request(u64 view_id, u64 answer_id, Web::Permissions::PromptOutcome outcome) =|
destroy_view(u64 view_id) =|
@@ -358,6 +378,14 @@ endpoint HubServer
view_set_next_history_visit_transition(u64 view_id, WebView::HistoryVisitTransition transition) =|
view_reload(u64 view_id, WebView::BypassCache bypass_cache) =|
view_stop_loading(u64 view_id) =|
+ view_undo(u64 view_id) =|
+ view_redo(u64 view_id) =|
+ view_cut_selection(u64 view_id) =|
+ view_paste(u64 view_id) =|
+ view_select_all(u64 view_id) =|
+ view_get_source(u64 view_id) =|
+ view_debug_request(u64 view_id, ByteString request, ByteString argument) =|
+ view_dump_gc_graph(u64 view_id) =|
view_zoom_in(u64 view_id) =|
view_zoom_out(u64 view_id) =|
view_reset_zoom(u64 view_id) =|
diff --git a/Services/Hub/View.cpp b/Services/Hub/View.cpp
index e50f66365f1..1c4643fddf6 100644
--- a/Services/Hub/View.cpp
+++ b/Services/Hub/View.cpp
@@ -4,7 +4,6 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
-#include <Hub/ActionMirror.h>
#include <Hub/Application.h>
#include <Hub/ConnectionFromUI.h>
#include <Hub/View.h>
@@ -617,8 +616,17 @@ ViewState View::current_state() const
state.handle = handle();
state.is_loading = is_loading();
state.zoom_level = zoom_level();
+ state.can_navigate_back = can_navigate_back();
+ state.can_navigate_forward = can_navigate_forward();
state.is_node_picker_active = is_node_picker_active();
- state.is_translated = has_traversable() && const_cast<View&>(*this).page_translation().is_translated();
+ if (has_traversable()) {
+ auto& translation = const_cast<View&>(*this).page_translation();
+ state.is_translated = translation.is_translated();
+ state.translation_offered = translation.is_offered();
+ state.translation_languages = translation.offered_target_languages();
+ state.translation_target_language = translation.target_language();
+ state.translation_needs_language_download = translation.needs_language_download();
+ }
state.input_method_state = input_method_state();
state.page_mute_state = page_mute_state();
state.plays_audio_indicator_visible = tab_indicator_visible(WebView::TabIndicator::PlayingAudio);
@@ -631,10 +639,13 @@ ViewState View::current_state() const
state.crash_overlay_failed_url = crash_overlay_failed_url();
state.force_dark_host = force_dark_host();
state.can_report_website_issue = can_report_website_issue();
+ state.can_undo = can_undo();
+ state.can_redo = can_redo();
state.permission_origin = permission_origin();
if (has_traversable()) {
state.content_blocking_snapshot = content_blocking_snapshot();
state.site_permission_rows = site_permission_rows();
+ state.can_view_source = can_view_source();
if (auto page = traversable().display_page(); page && page->is_open())
state.web_content_pid = web_content_pid();
}
@@ -677,6 +688,7 @@ void View::prepare_page_for_navigable(WebView::WebContentPage& page)
void View::update_zoom()
{
ViewImplementation::update_zoom();
+ push_state_to_ui();
if (auto* ui = ConnectionFromUI::the())
ui->async_view_update_zoom(view_id());
}
@@ -686,12 +698,18 @@ void View::read_clipboard_item(Function<void(Web::Clipboard::SystemClipboardItem
WebView::Application::the().read_clipboard_item(move(on_read));
}
-void View::show_context_menu(WebView::Menu& menu, Gfx::IntPoint widget_position)
+void View::did_request_context_menu(WebView::ContextMenuRequest request)
{
if (auto* ui = ConnectionFromUI::the()) {
push_state_to_ui();
- ui->async_view_show_context_menu(view_id(), ActionMirror::the().entries_of(menu), widget_position);
+ ui->async_view_request_context_menu(view_id(), move(request));
}
}
+void View::did_make_link_to_highlight(Optional<URL::URL> url)
+{
+ if (auto* ui = ConnectionFromUI::the())
+ ui->async_view_did_make_link_to_highlight(view_id(), move(url));
+}
+
}
diff --git a/Services/Hub/View.h b/Services/Hub/View.h
index 02e55271c34..14646117dca 100644
--- a/Services/Hub/View.h
+++ b/Services/Hub/View.h
@@ -59,10 +59,12 @@ private:
virtual Core::AnonymousBuffer system_theme() override { return m_system_theme; }
virtual Optional<ScreenRects> screen_rects() const override { return m_screen_rects; }
virtual void update_zoom() override;
+ virtual void did_change_state() override { push_state_to_ui(); }
virtual Web::DevicePixelSize viewport_size() const override { return m_viewport_size; }
virtual Gfx::IntPoint to_content_position(Gfx::IntPoint widget_position) const override { return widget_position; }
virtual Gfx::IntPoint to_widget_position(Gfx::IntPoint content_position) const override { return content_position; }
- virtual void show_context_menu(WebView::Menu&, Gfx::IntPoint widget_position) override;
+ virtual void did_request_context_menu(WebView::ContextMenuRequest) override;
+ virtual void did_make_link_to_highlight(Optional<URL::URL>) override;
virtual void read_clipboard_item(Function<void(Web::Clipboard::SystemClipboardItem)>) const override;
virtual void did_accept_presented_backing_store(i32 bitmap_id, Gfx::IntRect damage_rect) override;
// NB: The UI hands a backing store back, once it no longer shows what is in it.
diff --git a/Tests/LibWebView/TestViewSource.cpp b/Tests/LibWebView/TestViewSource.cpp
index c65872f8965..7f109766cf7 100644
--- a/Tests/LibWebView/TestViewSource.cpp
+++ b/Tests/LibWebView/TestViewSource.cpp
@@ -13,7 +13,6 @@
#include <LibURL/Parser.h>
#include <LibWebView/Application.h>
#include <LibWebView/HeadlessWebView.h>
-#include <LibWebView/Menu.h>
#include <LibWebView/Utilities.h>
namespace {
@@ -53,7 +52,6 @@ public:
void activate(WebView::ViewImplementation& view)
{
m_active_view = &view;
- update_actions_for_active_web_view();
}
size_t opened_tab_count() const { return m_opened_tabs.size(); }
@@ -88,16 +86,16 @@ TEST_CASE(view_source_of_source_page)
app->activate(*view);
VERIFY(view->can_view_source());
- VERIFY(app->view_source_action().enabled());
+ VERIFY(app->active_web_view()->can_view_source());
- app->view_source_action().activate();
+ view->get_source();
Core::EventLoop::current().spin_until([&]() {
return app->opened_tab_count() == 1 && app->opened_tab(0).url() == URL::about_srcdoc();
});
auto& source_tab = app->opened_tab(0);
VERIFY(!source_tab.can_view_source());
- VERIFY(!app->view_source_action().enabled());
+ VERIFY(!app->active_web_view()->can_view_source());
source_tab.get_source();
VERIFY(app->opened_tab_count() == 1);
@@ -108,28 +106,28 @@ TEST_CASE(view_source_of_source_page)
VERIFY(!source_tab.can_view_source());
app->activate(*view);
- VERIFY(app->view_source_action().enabled());
+ VERIFY(app->active_web_view()->can_view_source());
app->activate(source_tab);
- VERIFY(!app->view_source_action().enabled());
+ VERIFY(!app->active_web_view()->can_view_source());
auto loads_before_navigation = app->opened_tab_loads();
source_tab.load(URL::Parser::basic_parse("data:text/html,<p>Another page</p>"sv).release_value());
Core::EventLoop::current().spin_until([&]() { return app->opened_tab_loads() > loads_before_navigation; });
VERIFY(source_tab.can_view_source());
- VERIFY(app->view_source_action().enabled());
+ VERIFY(app->active_web_view()->can_view_source());
// History traversal brings the source page back, and with it the restriction.
source_tab.traverse_the_history_by_delta(-1);
Core::EventLoop::current().spin_until([&]() { return source_tab.url().equals(URL::about_srcdoc(), URL::ExcludeFragment::Yes); });
VERIFY(!source_tab.can_view_source());
- VERIFY(!app->view_source_action().enabled());
+ VERIFY(!app->active_web_view()->can_view_source());
source_tab.traverse_the_history_by_delta(1);
Core::EventLoop::current().spin_until([&]() { return !source_tab.url().equals(URL::about_srcdoc(), URL::ExcludeFragment::Yes); });
VERIFY(source_tab.can_view_source());
- VERIFY(app->view_source_action().enabled());
+ VERIFY(app->active_web_view()->can_view_source());
outln("PASS: A tab showing the source of a page offers no source of its own, while it shows that source");
}
diff --git a/UI/Qt/Application.cpp b/UI/Qt/Application.cpp
index aeb4107a262..869ac59b649 100644
--- a/UI/Qt/Application.cpp
+++ b/UI/Qt/Application.cpp
@@ -36,7 +36,6 @@
#elif defined(AK_OS_LINUX) || defined(AK_OS_FREEBSD)
# include <LibWebView/NotificationBackendFreedesktop.h>
#endif
-#include <Hub/ActionMirror.h>
#include <LibWebView/NoOpNotificationBackend.h>
#include <LibWebView/SessionStore.h>
#include <LibWebView/Translator.h>
@@ -53,7 +52,6 @@
#include <UI/Qt/PictureInPictureWindow.h>
#include <UI/Qt/PlainText.h>
#include <UI/Qt/ProcessManagerWindow.h>
-#include <UI/Qt/RemoteActions.h>
#include <UI/Qt/RemoteDownloads.h>
#include <UI/Qt/RemoteSession.h>
#include <UI/Qt/StringUtils.h>
@@ -345,7 +343,7 @@ public:
void create_application_actions()
{
- auto& application = RemoteActions::the();
+ auto& application = ApplicationActions::the();
auto make_action = [&](QString const& text) {
auto* action = new QAction(text, m_application_widget);
@@ -813,7 +811,13 @@ ErrorOr<void> Application::connect_to_hub(Main::Arguments const& arguments)
# if defined(AK_OS_MACOS)
set_profile_application_icon(state.profile_identifier, state.profile_identity, state.profile_is_temporary);
# endif
- RemoteActions::the().set_application_actions(state.actions);
+ m_application_actions = make<ApplicationActions>(ApplicationActions::PageOptions {
+ .user_agent = move(state.user_agent),
+ .navigator_compatibility_mode = state.navigator_compatibility_mode,
+ .scripting_enabled = state.scripting_enabled,
+ .blocks_pop_ups = state.blocks_pop_ups,
+ .devtools_enabled = state.devtools_enabled,
+ });
RemoteDownloads::the().set_downloads(state.downloads);
create_platform_actions();
@@ -967,7 +971,14 @@ void Application::create_platform_actions()
#ifndef LADYBIRD_HUB_IN_OWN_PROCESS
m_settings = make<WebView::Settings>(WebView::Settings::create_from_json(WebView::Application::settings().serialize_json()));
m_bookmark_store = make<BookmarkStore>(BookmarkStore::create(LexicalPath::join(WebView::Application::profile().paths().config, "Bookmarks.json"sv).string()));
- RemoteActions::the().set_application_actions(Hub::ActionMirror::the().application_actions());
+ auto& hub = WebView::Application::the();
+ m_application_actions = make<ApplicationActions>(ApplicationActions::PageOptions {
+ .user_agent = hub.user_agent_string(),
+ .navigator_compatibility_mode = hub.navigator_compatibility_mode(),
+ .scripting_enabled = hub.scripting_enabled(),
+ .blocks_pop_ups = hub.blocks_pop_ups(),
+ .devtools_enabled = hub.devtools_enabled(),
+ });
RemoteDownloads::the().set_downloads(Hub::ConnectionFromUI::downloads());
#endif
if (auto* hub = HubConnection::the_if_connected())
@@ -978,9 +989,10 @@ void Application::create_platform_actions()
};
m_bookmarks = make<Bookmarks>(*m_bookmark_store);
- RemoteActions::the().inspect_menu().add_action(WebView::Action::create("Open Task Manager"sv, WebView::ActionID::OpenTaskManager, [this]() {
+ m_open_task_manager_action = WebView::Action::create("Open Task Manager"sv, WebView::ActionID::OpenTaskManager, [this]() {
show_process_manager();
- }));
+ });
+ m_application_actions->inspect_menu().add_action(*m_open_task_manager_action);
if (m_application) {
auto& qapplication = static_cast<LadybirdQApplication&>(*m_application);
@@ -2293,6 +2305,8 @@ void Application::report_active_view() const
{
if (m_bookmarks)
m_bookmarks->update_toggle_bookmark_action();
+ if (m_application_actions)
+ m_application_actions->update_for_active_view();
auto* hub = HubConnection::the_if_connected();
if (!hub)
@@ -2303,12 +2317,14 @@ void Application::report_active_view() const
void Application::did_enable_devtools() const
{
+ m_application_actions->devtools_enabled_changed(true);
if (m_active_window)
m_active_window->on_devtools_enabled();
}
void Application::did_disable_devtools() const
{
+ m_application_actions->devtools_enabled_changed(false);
if (m_active_window)
m_active_window->on_devtools_disabled();
}
@@ -2320,7 +2336,7 @@ void Application::on_recently_closed_entries_changed() const
QMenuBar* Application::create_application_menu_bar(ForBrowserWindow for_browser_window)
{
- auto& application = RemoteActions::the();
+ auto& application = ApplicationActions::the();
auto* menu_bar = new QMenuBar();
auto* file_menu = menu_bar->addMenu("&File");
@@ -2392,6 +2408,11 @@ QMenuBar* Application::create_application_menu_bar(ForBrowserWindow for_browser_
if (for_browser_window == ForBrowserWindow::Yes) {
menu_bar->addMenu(inspect_menu());
menu_bar->addMenu(debug_menu());
+ } else {
+ // NB: The Task Manager needs no browser window, so it can be opened while none is focused, such as right after
+ // it was closed.
+ auto* inspect_menu = menu_bar->addMenu("&Inspect");
+ inspect_menu->addAction(create_application_action(*inspect_menu, *m_open_task_manager_action, IncludeActionIcon::No));
}
menu_bar->addMenu(help_menu());
diff --git a/UI/Qt/Application.h b/UI/Qt/Application.h
index 8f86fd7f290..5bc27a6bb7e 100644
--- a/UI/Qt/Application.h
+++ b/UI/Qt/Application.h
@@ -18,6 +18,7 @@
#include <LibWebView/BrowsingSession.h>
#include <LibWebView/CrashReportOutbox.h>
#include <LibWebView/ProcessSnapshot.h>
+#include <UI/Qt/ApplicationActions.h>
#include <UI/Qt/BrowserWindow.h>
#include <UI/Qt/HubConnection.h>
@@ -50,6 +51,7 @@ enum class ShowWindow {
class Application final
: public Hub::ApplicationHost
, public WebView::CrashReportOutboxObserver {
+ friend class ApplicationActions;
friend class Bookmarks;
friend class BookmarksPage;
friend class RemoteView;
@@ -163,6 +165,10 @@ public:
QAction* reset_zoom_action();
QAction* quit_action();
+ void set_clipboard_text(String, ClipboardType = ClipboardType::Text);
+ void insert_clipboard_item(Web::Clipboard::SystemClipboardItem);
+ virtual bool supports_private_browsing_windows() const override { return true; }
+
private:
void present_window(BrowserWindow&, WindowConfiguration const&, ShowWindow);
explicit Application();
@@ -223,14 +229,11 @@ private:
virtual bool supports_clipboard_type(ClipboardType) const override;
Utf16String clipboard_text(ClipboardType) const;
- void set_clipboard_text(String, ClipboardType = ClipboardType::Text);
Web::Clipboard::SystemClipboardItem clipboard_item() const;
- void insert_clipboard_item(Web::Clipboard::SystemClipboardItem);
virtual bool supports_system_menu_bar() const override;
virtual bool supports_vertical_tabs() const override { return true; }
- virtual bool supports_private_browsing_windows() const override { return true; }
virtual bool supports_client_side_window_decorations() const override
{
#if defined(AK_OS_MACOS)
@@ -287,6 +290,7 @@ private:
OwnPtr<WebView::Settings> m_settings;
OwnPtr<BookmarkStore> m_bookmark_store;
OwnPtr<Bookmarks> m_bookmarks;
+ OwnPtr<ApplicationActions> m_application_actions;
RefPtr<HubConnection> m_hub_connection;
OwnPtr<QApplication> m_application;
HashMap<u64, NonnullOwnPtr<WebView::PictureInPictureWindow>> m_picture_in_picture_windows;
@@ -295,6 +299,7 @@ private:
u64 m_next_external_url_handler_id { 1 };
BrowserWindow* m_active_window { nullptr };
OwnPtr<ProcessManagerWindow> m_process_manager_window;
+ RefPtr<WebView::Action> m_open_task_manager_action;
bool m_private_session_restart_in_progress { false };
bool m_quit_in_progress { false };
diff --git a/UI/Qt/ApplicationActions.cpp b/UI/Qt/ApplicationActions.cpp
new file mode 100644
index 00000000000..0389315ad9f
--- /dev/null
+++ b/UI/Qt/ApplicationActions.cpp
@@ -0,0 +1,356 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <LibURL/InternalURLs.h>
+#include <LibWebCommon/CSS/PreferredContrast.h>
+#include <LibWebCommon/CSS/PreferredMotion.h>
+#include <LibWebCommon/Loader/UserAgent.h>
+#include <LibWebView/UserAgent.h>
+#include <UI/Qt/Application.h>
+#include <UI/Qt/ApplicationActions.h>
+#include <UI/Qt/HubConnection.h>
+#include <UI/Qt/Tab.h>
+
+namespace Ladybird {
+
+static ApplicationActions* s_the;
+
+ApplicationActions& ApplicationActions::the()
+{
+ return *s_the;
+}
+
+static RemoteView* active_view()
+{
+ auto* tab = Application::the().active_tab();
+ if (!tab || !tab->is_loaded())
+ return nullptr;
+ return &tab->view();
+}
+
+template<typename Callback>
+static Function<void()> on_active_view(Callback callback)
+{
+ return [callback = move(callback)] {
+ if (auto* view = active_view())
+ callback(*view);
+ };
+}
+
+static Function<void()> debug_request(StringView request)
+{
+ return on_active_view([request](RemoteView& view) {
+ HubConnection::the().async_view_debug_request(view.view_id(), request, {});
+ });
+}
+
+ApplicationActions::ApplicationActions(PageOptions const& options)
+ : WebView::SettingsObserver(Application::settings())
+{
+ VERIFY(!s_the);
+ s_the = this;
+
+ m_reload_action = WebView::Action::create("Reload"sv, WebView::ActionID::Reload, on_active_view([](RemoteView& view) {
+ view.reload();
+ }));
+ m_reload_bypassing_cache_action = WebView::Action::create("Reload Without Cache"sv, WebView::ActionID::ReloadBypassingCache, on_active_view([](RemoteView& view) {
+ view.reload(WebView::BypassCache::Yes);
+ }));
+
+ m_undo_action = WebView::Action::create("Undo"sv, WebView::ActionID::Undo, on_active_view([](RemoteView& view) {
+ HubConnection::the().async_view_undo(view.view_id());
+ }));
+ m_redo_action = WebView::Action::create("Redo"sv, WebView::ActionID::Redo, on_active_view([](RemoteView& view) {
+ HubConnection::the().async_view_redo(view.view_id());
+ }));
+ m_copy_selection_action = WebView::Action::create("Copy"sv, WebView::ActionID::CopySelection, on_active_view([](RemoteView& view) {
+ view.selected_text()->when_resolved([](ByteString const& text) {
+ if (!text.is_empty())
+ Application::the().set_clipboard_text(MUST(String::from_byte_string(text)));
+ });
+ }));
+ m_cut_selection_action = WebView::Action::create("Cut"sv, WebView::ActionID::CutSelection, on_active_view([](RemoteView& view) {
+ HubConnection::the().async_view_cut_selection(view.view_id());
+ }));
+ m_paste_action = WebView::Action::create("Paste"sv, WebView::ActionID::Paste, on_active_view([](RemoteView& view) {
+ HubConnection::the().async_view_paste(view.view_id());
+ }));
+ m_select_all_action = WebView::Action::create("Select All"sv, WebView::ActionID::SelectAll, on_active_view([](RemoteView& view) {
+ HubConnection::the().async_view_select_all(view.view_id());
+ }));
+
+ m_open_about_page_action = WebView::Action::create("About Ladybird"sv, WebView::ActionID::OpenAboutPage, [] {
+ Application::the().activate_or_open_tab_with_url(URL::about_version());
+ });
+ m_open_downloads_page_action = WebView::Action::create("Downloads"sv, WebView::ActionID::ViewDownloads, [] {
+ Application::the().activate_or_open_tab_with_url(URL::about_downloads());
+ });
+ m_open_settings_page_action = WebView::Action::create("Settings"sv, WebView::ActionID::OpenSettingsPage, [] {
+ Application::the().activate_or_open_tab_with_url(URL::about_settings());
+ });
+
+ m_zoom_in_action = WebView::Action::create("Zoom In"sv, WebView::ActionID::ZoomIn, on_active_view([](RemoteView& view) {
+ view.zoom_in();
+ }));
+ m_zoom_out_action = WebView::Action::create("Zoom Out"sv, WebView::ActionID::ZoomOut, on_active_view([](RemoteView& view) {
+ view.zoom_out();
+ }));
+ m_reset_zoom_action = WebView::Action::create("Reset Zoom"sv, WebView::ActionID::ResetZoom, on_active_view([](RemoteView& view) {
+ view.reset_zoom();
+ }));
+
+ m_zoom_menu = WebView::Menu::create_group("Zoom"sv);
+ m_zoom_menu->add_action(*m_zoom_in_action);
+ m_zoom_menu->add_action(*m_zoom_out_action);
+ m_zoom_menu->add_action(*m_reset_zoom_action);
+
+ m_force_dark_action = WebView::Action::create_checkable("Force Dark Mode"sv, WebView::ActionID::ForceDark, on_active_view([](RemoteView& view) {
+ if (view.is_private() == WebView::IsPrivate::Yes || view.force_dark_host().is_empty())
+ return;
+ auto& settings = Application::settings();
+ settings.set_force_dark_for_host(view.force_dark_host(), !settings.force_dark_for_host(view.force_dark_host()));
+ }));
+
+ auto set_website_color_scheme = [](WebView::ThemePreference color_scheme) {
+ return [color_scheme] {
+ auto appearance = Application::settings().appearance();
+ appearance.website_color_scheme = color_scheme;
+ Application::settings().set_appearance(appearance);
+ };
+ };
+
+ m_color_scheme_menu = WebView::Menu::create_group("Color Scheme"sv);
+ m_color_scheme_menu->add_action(WebView::Action::create_checkable("Auto"sv, WebView::ActionID::PreferredColorScheme, set_website_color_scheme(WebView::ThemePreference::System)));
+ m_color_scheme_menu->add_action(WebView::Action::create_checkable("Dark"sv, WebView::ActionID::PreferredColorScheme, set_website_color_scheme(WebView::ThemePreference::Dark)));
+ m_color_scheme_menu->add_action(WebView::Action::create_checkable("Light"sv, WebView::ActionID::PreferredColorScheme, set_website_color_scheme(WebView::ThemePreference::Light)));
+ update_website_color_scheme_actions();
+
+ auto set_contrast = [](Web::CSS::PreferredContrast contrast) {
+ return [contrast] { HubConnection::the().async_set_preferred_contrast(contrast); };
+ };
+
+ m_contrast_menu = WebView::Menu::create_group("Contrast"sv);
+ m_contrast_menu->add_action(WebView::Action::create_checkable("Auto"sv, WebView::ActionID::PreferredContrast, set_contrast(Web::CSS::PreferredContrast::Auto)));
+ m_contrast_menu->add_action(WebView::Action::create_checkable("Less"sv, WebView::ActionID::PreferredContrast, set_contrast(Web::CSS::PreferredContrast::Less)));
+ m_contrast_menu->add_action(WebView::Action::create_checkable("More"sv, WebView::ActionID::PreferredContrast, set_contrast(Web::CSS::PreferredContrast::More)));
+ m_contrast_menu->add_action(WebView::Action::create_checkable("No Preference"sv, WebView::ActionID::PreferredContrast, set_contrast(Web::CSS::PreferredContrast::NoPreference)));
+ m_contrast_menu->items().first().get<NonnullRefPtr<WebView::Action>>()->set_checked(true);
+
+ auto set_motion = [](Web::CSS::PreferredMotion motion) {
+ return [motion] { HubConnection::the().async_set_preferred_motion(motion); };
+ };
+
+ m_motion_menu = WebView::Menu::create_group("Motion"sv);
+ m_motion_menu->add_action(WebView::Action::create_checkable("Auto"sv, WebView::ActionID::PreferredMotion, set_motion(Web::CSS::PreferredMotion::Auto)));
+ m_motion_menu->add_action(WebView::Action::create_checkable("Reduce"sv, WebView::ActionID::PreferredMotion, set_motion(Web::CSS::PreferredMotion::Reduce)));
+ m_motion_menu->add_action(WebView::Action::create_checkable("No Preference"sv, WebView::ActionID::PreferredMotion, set_motion(Web::CSS::PreferredMotion::NoPreference)));
+ m_motion_menu->items().first().get<NonnullRefPtr<WebView::Action>>()->set_checked(true);
+
+ m_toggle_vertical_tabs_expanded_action = WebView::Action::create("Toggle Vertical Tabs Expanded"sv, WebView::ActionID::ToggleVerticalTabsExpanded, [] {
+ auto tab_settings = Application::settings().tab_settings();
+ tab_settings.vertical_tabs_expanded = !tab_settings.vertical_tabs_expanded;
+ Application::settings().set_tab_settings(tab_settings);
+ });
+ update_vertical_tabs_action();
+
+ m_toggle_menu_bar_action = WebView::Action::create_checkable("Show Menu Bar"sv, WebView::ActionID::ToggleMenuBar, [] {
+ auto appearance = Application::settings().appearance();
+ appearance.show_menu_bar = !appearance.show_menu_bar;
+ Application::settings().set_appearance(appearance);
+ });
+ m_toggle_menu_bar_action->set_checked(Application::settings().appearance().show_menu_bar);
+
+ m_history_menu = WebView::Menu::create("History"sv);
+ m_history_menu->add_action(WebView::Action::create("View History"sv, WebView::ActionID::ViewHistory, [] {
+ Application::the().activate_or_open_tab_with_url(URL::about_history());
+ }));
+ m_history_menu->add_separator();
+ m_history_menu->add_action(WebView::Action::create("Clear Browsing Data"sv, WebView::ActionID::ClearBrowsingData, [] {
+ auto url = URL::about_settings();
+ url.set_fragment("clearBrowsingData"sv);
+ Application::the().activate_or_open_tab_with_url(url);
+ }));
+
+ m_inspect_menu = WebView::Menu::create("Inspect"sv);
+
+ m_view_source_action = WebView::Action::create("View Page Source"sv, WebView::ActionID::ViewSource, on_active_view([](RemoteView& view) {
+ HubConnection::the().async_view_get_source(view.view_id());
+ }));
+ m_inspect_menu->add_action(*m_view_source_action);
+
+ m_toggle_devtools_action = WebView::Action::create("Enable DevTools"sv, WebView::ActionID::ToggleDevTools, [] {
+ HubConnection::the().async_toggle_devtools_enabled();
+ });
+ m_inspect_menu->add_action(*m_toggle_devtools_action);
+ devtools_enabled_changed(options.devtools_enabled);
+
+ m_report_website_issue_action = WebView::Action::create("Report Website Issue…"sv, WebView::ActionID::ReportWebsiteIssue, on_active_view([](RemoteView& view) {
+ view.report_website_issue();
+ }));
+
+ create_debug_menu(options);
+ update_for_active_view();
+}
+
+ApplicationActions::~ApplicationActions()
+{
+ s_the = nullptr;
+}
+
+void ApplicationActions::create_debug_menu(PageOptions const& options)
+{
+ auto& settings = Application::settings();
+
+ m_debug_menu = WebView::Menu::create("Debug"sv);
+ m_debug_menu->set_visible(settings.config_variable_as_bool(WebView::ConfigVariableID::ShowAdvancedDebugMenu));
+ m_debug_menu->add_action(WebView::Action::create("Dump Session History Tree"sv, WebView::ActionID::DumpSessionHistoryTree, debug_request("dump-session-history"sv)));
+ m_debug_menu->add_action(WebView::Action::create("Dump DOM Tree"sv, WebView::ActionID::DumpDOMTree, debug_request("dump-dom-tree"sv)));
+ m_debug_menu->add_action(WebView::Action::create("Dump Layout Tree"sv, WebView::ActionID::DumpLayoutTree, debug_request("dump-layout-tree"sv)));
+ m_debug_menu->add_action(WebView::Action::create("Dump Stacking Context Tree"sv, WebView::ActionID::DumpStackingContextTree, debug_request("dump-stacking-context-tree"sv)));
+ m_debug_menu->add_action(WebView::Action::create("Dump Site Isolation Process Tree"sv, WebView::ActionID::DumpSiteIsolationProcessTree, debug_request("dump-site-isolation-process-tree"sv)));
+ m_debug_menu->add_action(WebView::Action::create("Dump Display List"sv, WebView::ActionID::DumpDisplayList, debug_request("dump-display-list"sv)));
+ m_debug_menu->add_action(WebView::Action::create("Dump Style Sheets"sv, WebView::ActionID::DumpStyleSheets, debug_request("dump-style-sheets"sv)));
+ m_debug_menu->add_action(WebView::Action::create("Dump All Resolved Styles"sv, WebView::ActionID::DumpStyles, debug_request("dump-all-resolved-styles"sv)));
+ m_debug_menu->add_action(WebView::Action::create("Dump CSS Errors"sv, WebView::ActionID::DumpCSSErrors, debug_request("dump-all-css-errors"sv)));
+ m_debug_menu->add_action(WebView::Action::create("Dump Cookies"sv, WebView::ActionID::DumpCookies, [] {
+ HubConnection::the().async_dump_cookies();
+ }));
+ m_debug_menu->add_action(WebView::Action::create("Dump Local Storage"sv, WebView::ActionID::DumpLocalStorage, debug_request("dump-local-storage"sv)));
+ m_debug_menu->add_action(WebView::Action::create("Dump Session Storage"sv, WebView::ActionID::DumpSessionStorage, debug_request("dump-session-storage"sv)));
+ m_debug_menu->add_action(WebView::Action::create("Dump GC Graph"sv, WebView::ActionID::DumpGCGraph, on_active_view([](RemoteView& view) {
+ HubConnection::the().async_view_dump_gc_graph(view.view_id());
+ })));
+ m_debug_menu->add_separator();
+
+ auto add_switch = [&](RefPtr<WebView::Action>& action, StringView name, WebView::ActionID id, bool checked, void (*set)(bool)) {
+ action = WebView::Action::create_checkable(name, id, [&action, set] { set(action->checked()); });
+ action->set_checked(checked);
+ m_debug_menu->add_action(*action);
+ };
+
+ add_switch(m_show_line_box_borders_action, "Show Line Box Borders"sv, WebView::ActionID::ShowLineBoxBorders, false, [](bool shows) {
+ HubConnection::the().async_set_shows_line_box_borders(shows);
+ });
+ add_switch(m_show_caret_hit_test_debug_overlay_action, "Show Caret Hit Test Debug Overlay"sv, WebView::ActionID::ShowCaretHitTestDebugOverlay, false, [](bool shows) {
+ HubConnection::the().async_set_shows_caret_hit_test_debug_overlay(shows);
+ });
+
+ m_show_tab_performance_monitor_action = WebView::Action::create_checkable("Show Tab Performance Monitor"sv, WebView::ActionID::ShowTabPerformanceMonitor, [this] {
+ Application::settings().set_config_variable(WebView::ConfigVariableID::ShowTabPerformanceMonitor, m_show_tab_performance_monitor_action->checked());
+ });
+ m_show_tab_performance_monitor_action->set_checked(settings.config_variable_as_bool(WebView::ConfigVariableID::ShowTabPerformanceMonitor));
+ m_debug_menu->add_action(*m_show_tab_performance_monitor_action);
+ m_debug_menu->add_separator();
+
+ m_debug_menu->add_action(WebView::Action::create("Report Leaked Documents"sv, WebView::ActionID::ReportLeakedDocuments, debug_request("report-leaked-documents"sv)));
+ m_debug_menu->add_action(WebView::Action::create("Collect Garbage"sv, WebView::ActionID::CollectGarbage, debug_request("collect-garbage"sv)));
+ m_debug_menu->add_action(WebView::Action::create("Crash Current Page"sv, WebView::ActionID::CrashCurrentPage, debug_request("crash-current-page"sv)));
+ m_debug_menu->add_action(WebView::Action::create("Crash Compositor Process"sv, WebView::ActionID::CrashCompositorProcess, [] {
+ HubConnection::the().async_crash_compositor_process();
+ }));
+ m_debug_menu->add_separator();
+
+ auto spoof_user_agent_menu = WebView::Menu::create_group("Spoof User Agent"sv);
+ auto add_user_agent = [&](StringView name, StringView user_agent) {
+ auto action = WebView::Action::create_checkable(name, WebView::ActionID::SpoofUserAgent, [user_agent] {
+ HubConnection::the().async_set_user_agent_string(MUST(String::from_utf8(user_agent)));
+ });
+ action->set_checked(options.user_agent == user_agent);
+ spoof_user_agent_menu->add_action(move(action));
+ };
+ add_user_agent("Disabled"sv, Web::default_user_agent);
+ for (auto const& user_agent : WebView::user_agents)
+ add_user_agent(user_agent.key, user_agent.value);
+
+ auto navigator_compatibility_mode_menu = WebView::Menu::create_group("Navigator Compatibility Mode"sv);
+ auto add_navigator_compatibility_mode = [&](StringView name, Web::NavigatorCompatibilityMode mode) {
+ auto action = WebView::Action::create_checkable(name, WebView::ActionID::SpoofUserAgent, [mode] {
+ HubConnection::the().async_set_navigator_compatibility_mode(mode);
+ });
+ action->set_checked(options.navigator_compatibility_mode == mode);
+ navigator_compatibility_mode_menu->add_action(move(action));
+ };
+ add_navigator_compatibility_mode("Chrome"sv, Web::NavigatorCompatibilityMode::Chrome);
+ add_navigator_compatibility_mode("Gecko"sv, Web::NavigatorCompatibilityMode::Gecko);
+ add_navigator_compatibility_mode("WebKit"sv, Web::NavigatorCompatibilityMode::WebKit);
+
+ m_debug_menu->add_submenu(move(spoof_user_agent_menu));
+ m_debug_menu->add_submenu(move(navigator_compatibility_mode_menu));
+ m_debug_menu->add_separator();
+
+ add_switch(m_enable_scripting_action, "Enable Scripting"sv, WebView::ActionID::EnableScripting, options.scripting_enabled, [](bool enabled) {
+ HubConnection::the().async_set_scripting_enabled(enabled);
+ });
+ add_switch(m_block_pop_ups_action, "Block Pop-ups"sv, WebView::ActionID::BlockPopUps, options.blocks_pop_ups, [](bool blocks) {
+ HubConnection::the().async_set_blocks_pop_ups(blocks);
+ });
+}
+
+void ApplicationActions::update_for_active_view()
+{
+ auto* view = active_view();
+ m_undo_action->set_enabled(view && view->can_undo());
+ m_redo_action->set_enabled(view && view->can_redo());
+ m_view_source_action->set_enabled(!view || view->can_view_source());
+ m_report_website_issue_action->set_enabled(view && view->can_report_website_issue());
+ update_force_dark_action();
+}
+
+void ApplicationActions::update_force_dark_action()
+{
+ auto& settings = Application::settings();
+ auto* view = active_view();
+ m_force_dark_action->set_enabled(view && view->is_private() == WebView::IsPrivate::No && !view->force_dark_host().is_empty());
+ m_force_dark_action->set_checked(view ? settings.force_dark_for_host(view->force_dark_host()) : settings.content_settings().enable_force_dark);
+}
+
+void ApplicationActions::devtools_enabled_changed(bool enabled)
+{
+ m_toggle_devtools_action->set_text(enabled ? "Disable DevTools"sv : "Enable DevTools"sv);
+}
+
+void ApplicationActions::update_website_color_scheme_actions()
+{
+ auto preference = Application::settings().appearance().website_color_scheme;
+ auto index = preference == WebView::ThemePreference::System ? 0 : preference == WebView::ThemePreference::Dark ? 1
+ : 2;
+ m_color_scheme_menu->items()[index].get<NonnullRefPtr<WebView::Action>>()->set_checked(true);
+}
+
+void ApplicationActions::update_vertical_tabs_action()
+{
+ auto const& tab_settings = Application::settings().tab_settings();
+ m_toggle_vertical_tabs_expanded_action->set_visible(tab_settings.vertical_tabs_enabled);
+ m_toggle_vertical_tabs_expanded_action->set_engaged(tab_settings.vertical_tabs_expanded);
+ m_toggle_vertical_tabs_expanded_action->set_tooltip(tab_settings.vertical_tabs_expanded ? "Hide Sidebar"sv : "Show Sidebar"sv);
+}
+
+void ApplicationActions::appearance_changed()
+{
+ m_toggle_menu_bar_action->set_checked(Application::settings().appearance().show_menu_bar);
+ update_website_color_scheme_actions();
+}
+
+void ApplicationActions::content_settings_changed()
+{
+ update_force_dark_action();
+}
+
+void ApplicationActions::tab_settings_changed()
+{
+ update_vertical_tabs_action();
+}
+
+void ApplicationActions::config_variable_changed(WebView::ConfigVariableID variable)
+{
+ auto& settings = Application::settings();
+ if (variable == WebView::ConfigVariableID::ShowAdvancedDebugMenu)
+ m_debug_menu->set_visible(settings.config_variable_as_bool(WebView::ConfigVariableID::ShowAdvancedDebugMenu));
+ if (variable == WebView::ConfigVariableID::ShowTabPerformanceMonitor)
+ m_show_tab_performance_monitor_action->set_checked(settings.config_variable_as_bool(WebView::ConfigVariableID::ShowTabPerformanceMonitor));
+}
+
+}
diff --git a/UI/Qt/RemoteActions.h b/UI/Qt/ApplicationActions.h
similarity index 66%
rename from UI/Qt/RemoteActions.h
rename to UI/Qt/ApplicationActions.h
index 0782f8376c5..da65bede697 100644
--- a/UI/Qt/RemoteActions.h
+++ b/UI/Qt/ApplicationActions.h
@@ -6,25 +6,37 @@
#pragma once
-#include <AK/HashMap.h>
-#include <AK/WeakPtr.h>
-#include <Hub/HubClientEndpoint.h>
+#include <AK/String.h>
+#include <LibWebCommon/Loader/NavigatorCompatibilityMode.h>
+#include <LibWebCommon/Loader/UserAgent.h>
#include <LibWebView/Menu.h>
+#include <LibWebView/Settings.h>
namespace Ladybird {
-// The Hub's actions and menus, as the UI shows them. Each stands in for one in the Hub, which it follows, and which it
-// activates when the user picks it.
-class RemoteActions {
+class RemoteView;
+
+// The actions and menus of the application, which act on the active view or on the whole browser.
+class ApplicationActions final : public WebView::SettingsObserver {
+ AK_ALLOC_WITH_KMALLOC;
+
public:
- static RemoteActions& the();
+ // What the Hub had in effect for every page when this process started.
+ struct PageOptions {
+ String user_agent;
+ Web::NavigatorCompatibilityMode navigator_compatibility_mode { Web::default_navigator_compatibility_mode };
+ bool scripting_enabled { true };
+ bool blocks_pop_ups { true };
+ bool devtools_enabled { false };
+ };
+
+ static ApplicationActions& the();
- void set_application_actions(Hub::ApplicationActions const&);
+ explicit ApplicationActions(PageOptions const&);
+ virtual ~ApplicationActions() override;
- NonnullRefPtr<WebView::Action> action_for(Hub::ActionState const&);
- NonnullRefPtr<WebView::Menu> menu_for(ReadonlySpan<Hub::MenuEntry>);
- void update(Hub::ActionState const&);
- void retire(u64 handle);
+ void update_for_active_view();
+ void devtools_enabled_changed(bool enabled);
WebView::Action& reload_action() { return *m_reload_action; }
WebView::Action& reload_bypassing_cache_action() { return *m_reload_bypassing_cache_action; }
@@ -55,9 +67,15 @@ public:
WebView::Menu& debug_menu() { return *m_debug_menu; }
private:
- static void apply_state(WebView::Action&, Hub::ActionState const&);
+ virtual void appearance_changed() override;
+ virtual void content_settings_changed() override;
+ virtual void tab_settings_changed() override;
+ virtual void config_variable_changed(WebView::ConfigVariableID) override;
- HashMap<u64, WeakPtr<WebView::Action>> m_actions;
+ void create_debug_menu(PageOptions const&);
+ void update_force_dark_action();
+ void update_website_color_scheme_actions();
+ void update_vertical_tabs_action();
RefPtr<WebView::Action> m_reload_action;
RefPtr<WebView::Action> m_reload_bypassing_cache_action;
@@ -77,7 +95,13 @@ private:
RefPtr<WebView::Action> m_toggle_vertical_tabs_expanded_action;
RefPtr<WebView::Action> m_toggle_menu_bar_action;
RefPtr<WebView::Action> m_view_source_action;
+ RefPtr<WebView::Action> m_toggle_devtools_action;
RefPtr<WebView::Action> m_report_website_issue_action;
+ RefPtr<WebView::Action> m_show_tab_performance_monitor_action;
+ RefPtr<WebView::Action> m_show_line_box_borders_action;
+ RefPtr<WebView::Action> m_show_caret_hit_test_debug_overlay_action;
+ RefPtr<WebView::Action> m_enable_scripting_action;
+ RefPtr<WebView::Action> m_block_pop_ups_action;
RefPtr<WebView::Menu> m_zoom_menu;
RefPtr<WebView::Menu> m_color_scheme_menu;
diff --git a/UI/Qt/BookmarksBar.cpp b/UI/Qt/BookmarksBar.cpp
index 8f12c9c2922..ab458387042 100644
--- a/UI/Qt/BookmarksBar.cpp
+++ b/UI/Qt/BookmarksBar.cpp
@@ -18,7 +18,6 @@
#include <UI/Qt/Icon.h>
#include <UI/Qt/Menu.h>
#include <UI/Qt/Motion.h>
-#include <UI/Qt/RemoteActions.h>
#include <UI/Qt/StringUtils.h>
#include <UI/Qt/SystemAppearance.h>
#include <UI/Qt/Tab.h>
diff --git a/UI/Qt/CMakeLists.txt b/UI/Qt/CMakeLists.txt
index 916a3c7061f..39e3f0cf897 100644
--- a/UI/Qt/CMakeLists.txt
+++ b/UI/Qt/CMakeLists.txt
@@ -15,6 +15,7 @@ endif()
add_library(ladybird_impl STATIC
AllTabsPopover.cpp
Application.cpp
+ ApplicationActions.cpp
Autocomplete.cpp
BookmarkDialog.cpp
BookmarkStore.cpp
@@ -24,6 +25,7 @@ add_library(ladybird_impl STATIC
BookmarksPage.cpp
BrowserWindow.cpp
ChromeStyle.cpp
+ ContextMenus.cpp
CrashReportReviewWidget.cpp
DevToolsBanner.cpp
EventLoopImplementationQt.cpp
@@ -46,7 +48,6 @@ add_library(ladybird_impl STATIC
PlainText.cpp
Popover.cpp
ProcessManagerWindow.cpp
- RemoteActions.cpp
RemoteDownloads.cpp
RemoteSession.cpp
RemoteSuggestionProvider.cpp
@@ -77,7 +78,7 @@ if (NOT APPLE AND NOT WIN32 AND NOT ANDROID)
endif()
target_link_libraries(ladybird_impl PUBLIC Qt::Core Qt::Gui Qt::Widgets)
-target_link_libraries(ladybird_impl PRIVATE LibFileSystem)
+target_link_libraries(ladybird_impl PRIVATE LibFileSystem LibUnicode)
if (TARGET Qt6::Positioning)
target_sources(ladybird_impl PRIVATE GeolocationProviderQt.cpp GeolocationProviderQt.h)
diff --git a/UI/Qt/ContextMenus.cpp b/UI/Qt/ContextMenus.cpp
new file mode 100644
index 00000000000..5532e6fa304
--- /dev/null
+++ b/UI/Qt/ContextMenus.cpp
@@ -0,0 +1,387 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <LibGfx/ImageFormats/PNGWriter.h>
+#include <LibURL/Parser.h>
+#include <LibWebView/SearchEngine.h>
+#include <LibWebView/Settings.h>
+#include <LibWebView/URL.h>
+#include <UI/Qt/Application.h>
+#include <UI/Qt/ContextMenus.h>
+#include <UI/Qt/HubConnection.h>
+#include <UI/Qt/RemoteView.h>
+
+namespace Ladybird {
+
+using WebView::Action;
+using WebView::ActionID;
+using WebView::ContextMenuKind;
+using WebView::ContextMenuMediaAction;
+using WebView::ContextMenuRequest;
+using WebView::Menu;
+
+// NB: A shown menu can outlive its view, so its actions find the view again when the user picks them.
+template<typename Callback>
+static Function<void()> on_view(u64 view_id, Callback callback)
+{
+ return [view_id, callback = move(callback)]() mutable {
+ if (auto* view = RemoteView::find(view_id))
+ callback(*view);
+ };
+}
+
+static void copy_text(ByteString const& text)
+{
+ if (!text.is_empty())
+ Application::the().set_clipboard_text(MUST(String::from_byte_string(text)));
+}
+
+static NonnullRefPtr<Action> create_copy_url_action(StringView text, URL::URL const& url)
+{
+ return Action::create(text, ActionID::CopyURL, [url]() {
+ copy_text(WebView::url_text_to_copy(url));
+ });
+}
+
+// A URL from the context menu is the page's, a link's or an image's, so it's navigated to from the page, unless the
+// user selected it as text.
+static void add_open_in_new_tab_action(Menu& menu, u64 view_id, StringView text, URL::URL const& url, Optional<Web::HTML::PreparedNavigationDescriptor> const& navigation)
+{
+ menu.add_action(Action::create(text, ActionID::OpenInNewTab, on_view(view_id, [url, navigation](RemoteView& view) {
+ if (navigation.has_value())
+ view.open_navigation_in_new_tab(*navigation, Web::HTML::ActivateTab::No);
+ else
+ view.open_url_in_new_tab(url, Web::HTML::ActivateTab::No);
+ })));
+}
+
+static void add_open_url_actions(Menu& menu, RemoteView& view, URL::URL const& url, Optional<Web::HTML::PreparedNavigationDescriptor> const& navigation)
+{
+ auto view_id = view.view_id();
+ add_open_in_new_tab_action(menu, view_id, "Open Link in New Tab"sv, url, navigation);
+ if (view.is_private() == WebView::IsPrivate::No) {
+ menu.add_action(Action::create("Open Link in New Window"sv, ActionID::OpenInNewWindow, on_view(view_id, [url, navigation](RemoteView& view) {
+ if (navigation.has_value())
+ view.open_navigation_in_new_window(*navigation, WebView::IsPrivate::No);
+ else
+ view.open_url_in_new_window(url, WebView::IsPrivate::No);
+ })));
+ }
+ if (Application::the().supports_private_browsing_windows()) {
+ menu.add_action(Action::create("Open Link in Private Window"sv, ActionID::OpenInNewPrivateWindow, on_view(view_id, [url, navigation](RemoteView& view) {
+ if (!navigation.has_value()) {
+ view.open_url_in_new_window(url, WebView::IsPrivate::Yes);
+ return;
+ }
+ // A private window still gets the page as the navigation's initiator, but never the page's URL as its
+ // referrer.
+ auto private_navigation = *navigation;
+ private_navigation.referrer_policy = Web::ReferrerPolicy::ReferrerPolicy::NoReferrer;
+ view.open_navigation_in_new_window(move(private_navigation), WebView::IsPrivate::Yes);
+ })));
+ }
+}
+
+static void add_link_actions(Menu& menu, RemoteView& view, Web::HTML::PreparedNavigationDescriptor const& navigation)
+{
+ auto url = navigation.url;
+ auto copy_url_text = [&] {
+ switch (WebView::url_type(url)) {
+ case WebView::URLType::Email:
+ return "Copy Email Address"sv;
+ case WebView::URLType::Telephone:
+ return "Copy Phone Number"sv;
+ case WebView::URLType::Other:
+ return "Copy Link"sv;
+ }
+ VERIFY_NOT_REACHED();
+ }();
+
+ add_open_url_actions(menu, view, url, navigation);
+ menu.add_separator();
+ menu.add_action(create_copy_url_action(copy_url_text, url));
+ menu.add_action(Action::create("Download Linked File"sv, ActionID::DownloadLinkedFile, on_view(view.view_id(), [url](RemoteView& view) {
+ view.download_url(url, RemoteView::PromptForPath::No);
+ })));
+ menu.add_action(Action::create("Save Link As…"sv, ActionID::DownloadLinkedFileAs, on_view(view.view_id(), [url](RemoteView& view) {
+ view.download_url(url, RemoteView::PromptForPath::Yes);
+ })));
+}
+
+// Text can be edited only in a field, and copied only where there is some.
+static void add_text_edit_actions(Menu& menu, u64 view_id, bool is_editable, bool has_selected_text)
+{
+ if (is_editable) {
+ menu.add_action(Action::create("Cut"sv, ActionID::CutSelection, on_view(view_id, [](RemoteView& view) {
+ HubConnection::the().async_view_cut_selection(view.view_id());
+ })));
+ }
+ if (is_editable || has_selected_text) {
+ menu.add_action(Action::create("Copy"sv, ActionID::CopySelection, on_view(view_id, [](RemoteView& view) {
+ view.selected_text()->when_resolved(copy_text);
+ })));
+ }
+ if (is_editable) {
+ menu.add_action(Action::create("Paste"sv, ActionID::Paste, on_view(view_id, [](RemoteView& view) {
+ HubConnection::the().async_view_paste(view.view_id());
+ })));
+ menu.add_action(Action::create("Select All"sv, ActionID::SelectAll, on_view(view_id, [](RemoteView& view) {
+ HubConnection::the().async_view_select_all(view.view_id());
+ })));
+ }
+}
+
+static void add_look_up_action(Menu& menu, u64 view_id, ContextMenuRequest const& request)
+{
+ if (!request.dictionary_lookup.has_value())
+ return;
+
+ menu.add_action(Action::create("Look Up"sv, ActionID::LookUpSelectedText, on_view(view_id, [lookup = *request.dictionary_lookup, position = request.dictionary_lookup_position](RemoteView& view) {
+ if (view.on_request_dictionary_lookup)
+ view.on_request_dictionary_lookup(lookup, position);
+ })));
+}
+
+static void add_search_action(Menu& menu, u64 view_id, Optional<String> const& selected_text)
+{
+ auto const& search_engine = Application::settings().search_engine_settings().engine;
+ if (!search_engine.has_value() || !selected_text.has_value())
+ return;
+
+ auto url = URL::Parser::basic_parse(search_engine->format_search_query_for_navigation(*selected_text));
+ VERIFY(url.has_value());
+
+ menu.add_action(Action::create(search_engine->format_search_query_for_display(*selected_text), ActionID::SearchSelectedText, on_view(view_id, [url = url.release_value()](RemoteView& view) {
+ view.open_url_in_new_tab(url, Web::HTML::ActivateTab::Yes);
+ })));
+}
+
+static void add_spelling_actions(Menu& menu, u64 view_id, ContextMenuRequest const& request)
+{
+ if (!request.misspelled_word.has_value())
+ return;
+
+ for (auto const& guess : request.spelling_guesses) {
+ menu.add_action(Action::create(MUST(guess.utf16_view().to_utf8()), ActionID::SpellingGuess, [view_id, guess]() {
+ HubConnection::the().async_view_replace_context_menu_misspelling(view_id, guess);
+ }));
+ }
+ if (request.spelling_guesses.is_empty()) {
+ auto no_guesses_action = Action::create("No Guesses Found"sv, ActionID::NoSpellingGuesses, [] { });
+ no_guesses_action->set_enabled(false);
+ menu.add_action(move(no_guesses_action));
+ }
+ menu.add_separator();
+ menu.add_action(Action::create("Ignore Spelling"sv, ActionID::IgnoreSpelling, [view_id, word = *request.misspelled_word]() {
+ HubConnection::the().async_view_ignore_misspelling(view_id, word);
+ }));
+ menu.add_action(Action::create("Learn Spelling"sv, ActionID::LearnSpelling, [view_id, word = *request.misspelled_word]() {
+ HubConnection::the().async_view_learn_misspelling(view_id, word);
+ }));
+}
+
+static void add_copy_link_to_highlight_action(Menu& menu, RemoteView& view, ContextMenuRequest const& request)
+{
+ if (request.makes_link_to_highlight)
+ menu.add_action(view.copy_link_to_highlight_action());
+}
+
+// The element is the one under the cursor when the context menu opened, so a page that changes while the menu is open
+// still has the element the user clicked on.
+static void add_inspect_element_action(Menu& menu, u64 view_id, ContextMenuRequest const& request)
+{
+ if (!request.can_inspect_element)
+ return;
+ menu.add_separator();
+ menu.add_action(Action::create("Inspect Element"sv, ActionID::InspectElement, [view_id]() {
+ HubConnection::the().async_view_inspect_context_menu_element(view_id);
+ }));
+}
+
+static NonnullRefPtr<Menu> create_page_context_menu(RemoteView& view, ContextMenuRequest const& request)
+{
+ auto view_id = view.view_id();
+
+ // The page's own items are left out where the menu is about the text.
+ auto is_about_page = !request.is_editable && !request.selected_text.has_value();
+
+ auto menu = Menu::create("Page Context Menu"sv);
+ add_spelling_actions(*menu, view_id, request);
+ menu->add_separator();
+ add_look_up_action(*menu, view_id, request);
+ if (is_about_page) {
+ menu->add_action(view.navigate_back_action());
+ menu->add_action(view.navigate_forward_action());
+ menu->add_action(Action::create("Reload"sv, ActionID::Reload, on_view(view_id, [](RemoteView& view) { view.reload(); })));
+ }
+ menu->add_separator();
+ add_text_edit_actions(*menu, view_id, request.is_editable, request.selected_text.has_value());
+ menu->add_separator();
+ add_search_action(*menu, view_id, request.selected_text);
+ add_copy_link_to_highlight_action(*menu, view, request);
+ menu->add_separator();
+ if (is_about_page) {
+ auto view_source_action = Action::create("View Page Source"sv, ActionID::ViewSource, [view_id] {
+ HubConnection::the().async_view_get_source(view_id);
+ });
+ view_source_action->set_enabled(view.can_view_source());
+ menu->add_action(move(view_source_action));
+ }
+ auto report_website_issue_action = Action::create("Report Website Issue…"sv, ActionID::ReportWebsiteIssue, on_view(view_id, [](RemoteView& view) {
+ view.report_website_issue();
+ }));
+ report_website_issue_action->set_enabled(view.can_report_website_issue());
+ menu->add_action(move(report_website_issue_action));
+ menu->add_separator();
+ if (is_about_page) {
+ menu->add_action(Action::create("Take Screenshot..."sv, ActionID::TakeScreenshot, on_view(view_id, [](RemoteView& view) {
+ if (view.on_request_screenshot)
+ view.on_request_screenshot();
+ })));
+ }
+ add_inspect_element_action(*menu, view_id, request);
+ return menu;
+}
+
+static NonnullRefPtr<Menu> create_selected_text_link_context_menu(RemoteView& view, ContextMenuRequest const& request, URL::URL const& url)
+{
+ auto view_id = view.view_id();
+
+ auto menu = Menu::create("Selected Text Link Context Menu"sv);
+ add_look_up_action(*menu, view_id, request);
+ add_open_url_actions(*menu, view, url, {});
+ menu->add_separator();
+ add_text_edit_actions(*menu, view_id, request.is_editable, true);
+ menu->add_separator();
+ add_search_action(*menu, view_id, request.selected_text);
+ add_copy_link_to_highlight_action(*menu, view, request);
+ add_inspect_element_action(*menu, view_id, request);
+ return menu;
+}
+
+static NonnullRefPtr<Menu> create_link_context_menu(RemoteView& view, ContextMenuRequest const& request)
+{
+ auto menu = Menu::create("Link Context Menu"sv);
+ add_link_actions(*menu, view, *request.link_navigation);
+ add_copy_link_to_highlight_action(*menu, view, request);
+ add_inspect_element_action(*menu, view.view_id(), request);
+ return menu;
+}
+
+static NonnullRefPtr<Menu> create_image_context_menu(RemoteView& view, ContextMenuRequest const& request)
+{
+ auto view_id = view.view_id();
+ auto const& navigation = *request.navigation;
+ auto url = navigation.url;
+
+ auto copy_image_action = Action::create("Copy Image"sv, ActionID::CopyImage, [bitmap = request.image_bitmap]() {
+ if (!bitmap.has_value() || !bitmap->is_valid())
+ return;
+
+ auto encoded = Gfx::PNGWriter::encode(*bitmap->bitmap());
+ if (encoded.is_error())
+ return;
+
+ Application::the().insert_clipboard_item({ { { "image/png"_string, ByteString { encoded.value().bytes() } } } });
+ });
+ copy_image_action->set_enabled(request.image_bitmap.has_value());
+
+ auto menu = Menu::create("Image Context Menu"sv);
+ if (request.link_navigation.has_value()) {
+ add_link_actions(*menu, view, *request.link_navigation);
+ menu->add_separator();
+ }
+ add_open_in_new_tab_action(*menu, view_id, "Open Image in New Tab"sv, url, navigation);
+ menu->add_action(Action::create("Open Image"sv, ActionID::OpenImage, on_view(view_id, [navigation](RemoteView& view) {
+ view.load(navigation);
+ })));
+ menu->add_separator();
+ menu->add_action(move(copy_image_action));
+ menu->add_action(create_copy_url_action("Copy Image Address"sv, url));
+ menu->add_action(Action::create("Save Image As…"sv, ActionID::SaveImage, on_view(view_id, [url, image_mime_type = request.image_mime_type](RemoteView& view) {
+ view.download_url(url, RemoteView::PromptForPath::Yes, image_mime_type);
+ })));
+ add_inspect_element_action(*menu, view_id, request);
+ return menu;
+}
+
+static NonnullRefPtr<Menu> create_media_context_menu(RemoteView& view, ContextMenuRequest const& request)
+{
+ auto view_id = view.view_id();
+ auto const& media = *request.media;
+
+ auto send = [view_id](ContextMenuMediaAction action) {
+ return [view_id, action]() {
+ HubConnection::the().async_view_do_context_menu_media_action(view_id, action);
+ };
+ };
+
+ auto menu = Menu::create("Media Context Menu"sv);
+ if (media.is_playing)
+ menu->add_action(Action::create("Pause"sv, ActionID::PauseMedia, send(ContextMenuMediaAction::TogglePlay)));
+ else
+ menu->add_action(Action::create("Play"sv, ActionID::PlayMedia, send(ContextMenuMediaAction::TogglePlay)));
+ if (media.is_muted)
+ menu->add_action(Action::create("Unmute"sv, ActionID::UnmuteMedia, send(ContextMenuMediaAction::ToggleMute)));
+ else
+ menu->add_action(Action::create("Mute"sv, ActionID::MuteMedia, send(ContextMenuMediaAction::ToggleMute)));
+ if (media.has_user_agent_controls)
+ menu->add_action(Action::create("Hide Controls"sv, ActionID::HideControls, send(ContextMenuMediaAction::ToggleControls)));
+ else
+ menu->add_action(Action::create("Show Controls"sv, ActionID::ShowControls, send(ContextMenuMediaAction::ToggleControls)));
+ auto loop_action = Action::create_checkable("Loop"sv, ActionID::ToggleMediaLoopState, send(ContextMenuMediaAction::ToggleLoop));
+ loop_action->set_checked(media.is_looping);
+ menu->add_action(move(loop_action));
+ menu->add_separator();
+ if (media.is_picture_in_picture)
+ menu->add_action(Action::create("Exit Picture in Picture"sv, ActionID::ExitPictureInPicture, send(ContextMenuMediaAction::TogglePictureInPicture)));
+ else if (media.can_enter_picture_in_picture)
+ menu->add_action(Action::create("Picture in Picture"sv, ActionID::EnterPictureInPicture, send(ContextMenuMediaAction::TogglePictureInPicture)));
+ if (media.is_video && media.is_fullscreen)
+ menu->add_action(Action::create("Exit Full Screen"sv, ActionID::ExitFullscreen, send(ContextMenuMediaAction::ToggleFullscreen)));
+ else if (media.is_video)
+ menu->add_action(Action::create("Full Screen"sv, ActionID::EnterFullscreen, send(ContextMenuMediaAction::ToggleFullscreen)));
+ menu->add_separator();
+ if (media.media_url.has_value()) {
+ auto url = *media.media_url;
+ add_open_in_new_tab_action(*menu, view_id, media.is_video ? "Open Video in New Tab"sv : "Open Audio in New Tab"sv, url, request.navigation);
+ if (request.navigation.has_value()) {
+ menu->add_action(Action::create(media.is_video ? "Open Video"sv : "Open Audio"sv, media.is_video ? ActionID::OpenVideo : ActionID::OpenAudio, on_view(view_id, [navigation = *request.navigation](RemoteView& view) {
+ view.load(navigation);
+ })));
+ }
+ menu->add_action(create_copy_url_action(media.is_video ? "Copy Video Address"sv : "Copy Audio Address"sv, url));
+ }
+ add_inspect_element_action(*menu, view_id, request);
+ return menu;
+}
+
+NonnullRefPtr<Menu> create_context_menu(RemoteView& view, ContextMenuRequest const& request)
+{
+ switch (request.kind) {
+ case ContextMenuKind::Page:
+ if (request.selected_text.has_value()) {
+ if (auto url = WebView::url_from_text(*request.selected_text); url.has_value())
+ return create_selected_text_link_context_menu(view, request, *url);
+ }
+ return create_page_context_menu(view, request);
+ case ContextMenuKind::Link:
+ if (request.link_navigation.has_value())
+ return create_link_context_menu(view, request);
+ break;
+ case ContextMenuKind::Image:
+ if (request.navigation.has_value())
+ return create_image_context_menu(view, request);
+ break;
+ case ContextMenuKind::Media:
+ if (request.media.has_value())
+ return create_media_context_menu(view, request);
+ break;
+ }
+ return create_page_context_menu(view, request);
+}
+
+}
diff --git a/UI/Qt/ContextMenus.h b/UI/Qt/ContextMenus.h
new file mode 100644
index 00000000000..2bd1820372f
--- /dev/null
+++ b/UI/Qt/ContextMenus.h
@@ -0,0 +1,20 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include <AK/NonnullRefPtr.h>
+#include <LibWebView/ContextMenuRequest.h>
+#include <LibWebView/Menu.h>
+
+namespace Ladybird {
+
+class RemoteView;
+
+// Makes the menu for what the user asked a context menu on in a view.
+NonnullRefPtr<WebView::Menu> create_context_menu(RemoteView&, WebView::ContextMenuRequest const&);
+
+}
diff --git a/UI/Qt/HubConnection.cpp b/UI/Qt/HubConnection.cpp
index e070f4d684a..3160d2d1d09 100644
--- a/UI/Qt/HubConnection.cpp
+++ b/UI/Qt/HubConnection.cpp
@@ -8,13 +8,14 @@
#include <LibCore/Process.h>
#include <LibWebView/CrashReportOutbox.h>
#include <UI/Qt/Application.h>
+#include <UI/Qt/ApplicationActions.h>
#include <UI/Qt/Bookmarks.h>
#include <UI/Qt/BookmarksPage.h>
#include <UI/Qt/HubConnection.h>
-#include <UI/Qt/RemoteActions.h>
#include <UI/Qt/RemoteDownloads.h>
#include <UI/Qt/RemoteSuggestionProvider.h>
#include <UI/Qt/RemoteView.h>
+#include <UI/Qt/Tab.h>
namespace Ladybird {
@@ -646,20 +647,16 @@ void HubConnection::settings_changed(ByteString settings_json, u64 last_applied_
Application::settings().replace_with_json(settings.value(), WebView::Settings::PersistChange::No);
}
-void HubConnection::action_retired(u64 handle)
+void HubConnection::view_request_context_menu(u64 view_id, WebView::ContextMenuRequest request)
{
- RemoteActions::the().retire(handle);
-}
-
-void HubConnection::action_changed(Hub::ActionState state)
-{
- RemoteActions::the().update(state);
+ if (auto* view = RemoteView::find(view_id))
+ view->did_request_context_menu(request);
}
-void HubConnection::view_show_context_menu(u64 view_id, Vector<Hub::MenuEntry> entries, Gfx::IntPoint widget_position)
+void HubConnection::view_did_make_link_to_highlight(u64 view_id, Optional<URL::URL> url)
{
if (auto* view = RemoteView::find(view_id))
- view->did_request_context_menu(entries, widget_position);
+ view->did_make_link_to_highlight(move(url));
}
void HubConnection::view_update_zoom(u64 view_id)
@@ -688,6 +685,9 @@ void HubConnection::view_state_changed(u64 view_id, Hub::ViewState state)
view->set_state(move(state));
if (url_changed)
view->update_bookmark_action();
+ // NB: The application actions follow the active view only, and background tabs change state often.
+ if (auto* tab = Application::the().active_tab(); tab && tab->is_loaded() && &tab->view() == view)
+ ApplicationActions::the().update_for_active_view();
}
}
diff --git a/UI/Qt/HubConnection.h b/UI/Qt/HubConnection.h
index be9d7b182ff..86747fe2b4e 100644
--- a/UI/Qt/HubConnection.h
+++ b/UI/Qt/HubConnection.h
@@ -118,9 +118,8 @@ private:
virtual void web_ui_opened(u64 web_ui_id, String host) override;
virtual void web_ui_message(u64 web_ui_id, String name, JsonValue data) override;
virtual void web_ui_closed(u64 web_ui_id) override;
- virtual void action_changed(Hub::ActionState state) override;
- virtual void action_retired(u64 handle) override;
- virtual void view_show_context_menu(u64 view_id, Vector<Hub::MenuEntry> entries, Gfx::IntPoint widget_position) override;
+ virtual void view_request_context_menu(u64 view_id, WebView::ContextMenuRequest request) override;
+ virtual void view_did_make_link_to_highlight(u64 view_id, Optional<URL::URL> url) override;
virtual void view_favicon_change(u64 view_id, Optional<Gfx::ShareableBitmap> favicon) override;
virtual void view_request_before_unload(u64 view_id, u64 answer_id, String source) override;
virtual void view_request_external_url_confirmation(u64 view_id, u64 answer_id, URL::URL url, URL::Origin initiator_origin, String application_name) override;
diff --git a/UI/Qt/RemoteActions.cpp b/UI/Qt/RemoteActions.cpp
deleted file mode 100644
index 366e5113f49..00000000000
--- a/UI/Qt/RemoteActions.cpp
+++ /dev/null
@@ -1,149 +0,0 @@
-/*
- * Copyright (c) 2026-present, the Ladybird developers.
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
-
-#include <AK/ByteString.h>
-#include <AK/HashTable.h>
-#include <UI/Qt/HubConnection.h>
-#include <UI/Qt/RemoteActions.h>
-
-namespace Ladybird {
-
-RemoteActions& RemoteActions::the()
-{
- static RemoteActions s_the;
- return s_the;
-}
-
-// NB: Menus and actions hold their property names as views of string literals, which these stand in for.
-static StringView interned_property_name(String const& name)
-{
- static HashTable<ByteString> s_names;
- auto byte_string = name.to_byte_string();
- s_names.set(byte_string);
- return s_names.find(byte_string)->view();
-}
-
-template<typename MenuOrAction>
-static void apply_properties(MenuOrAction& menu_or_action, ReadonlySpan<String> names, ReadonlySpan<String> values)
-{
- for (size_t i = 0; i < names.size(); ++i)
- menu_or_action.add_property(interned_property_name(names[i]), values[i]);
-}
-
-void RemoteActions::apply_state(WebView::Action& action, Hub::ActionState const& state)
-{
- if (action.text() != state.text)
- action.set_text(state.text);
- if (state.tooltip.has_value() && (!action.has_tooltip() || action.tooltip() != *state.tooltip))
- action.set_tooltip(*state.tooltip);
- action.set_png_icon(state.png_icon);
- if (action.enabled() != state.enabled)
- action.set_enabled(state.enabled);
- if (action.visible() != state.visible)
- action.set_visible(state.visible);
- if (action.engaged() != state.engaged)
- action.set_engaged(state.engaged);
- if (state.checked.has_value() && action.is_checkable() && action.checked() != *state.checked)
- action.set_checked(*state.checked);
- apply_properties(action, state.property_names, state.property_values);
-}
-
-NonnullRefPtr<WebView::Action> RemoteActions::action_for(Hub::ActionState const& state)
-{
- if (auto* action = m_actions.get(state.handle).value_or(nullptr)) {
- apply_state(*action, state);
- return *action;
- }
-
- auto activate = [this, handle = state.handle] {
- Optional<bool> checked;
- if (auto* action = m_actions.get(handle).value_or(nullptr); action && action->is_checkable())
- checked = action->checked();
- HubConnection::the().async_activate_action(handle, checked);
- };
- auto action = state.checked.has_value()
- ? WebView::Action::create_checkable(state.text, state.id, move(activate))
- : WebView::Action::create(state.text, state.id, move(activate));
- apply_state(*action, state);
- m_actions.set(state.handle, action->make_weak_ptr<WebView::Action>());
- return action;
-}
-
-void RemoteActions::update(Hub::ActionState const& state)
-{
- if (auto* action = m_actions.get(state.handle).value_or(nullptr))
- apply_state(*action, state);
-}
-
-void RemoteActions::retire(u64 handle)
-{
- m_actions.remove(handle);
-}
-
-NonnullRefPtr<WebView::Menu> RemoteActions::menu_for(ReadonlySpan<Hub::MenuEntry> entries)
-{
- Vector<NonnullRefPtr<WebView::Menu>> open_menus;
- RefPtr<WebView::Menu> outermost_menu;
-
- for (auto const& entry : entries) {
- switch (entry.kind) {
- case Hub::MenuEntryKind::Submenu: {
- auto menu = entry.is_group ? WebView::Menu::create_group(entry.title) : WebView::Menu::create(entry.title);
- menu->set_render_group_icon(entry.render_group_icon);
- menu->set_visible(entry.visible);
- apply_properties(*menu, entry.property_names, entry.property_values);
- if (!open_menus.is_empty())
- open_menus.last()->add_submenu(menu);
- else
- outermost_menu = menu;
- open_menus.append(menu);
- break;
- }
- case Hub::MenuEntryKind::EndOfSubmenu:
- (void)open_menus.take_last();
- break;
- case Hub::MenuEntryKind::Action:
- open_menus.last()->add_action(action_for(*entry.action));
- break;
- case Hub::MenuEntryKind::Separator:
- open_menus.last()->add_separator();
- break;
- }
- }
- return outermost_menu.release_nonnull();
-}
-
-void RemoteActions::set_application_actions(Hub::ApplicationActions const& actions)
-{
- m_reload_action = action_for(actions.reload_action);
- m_reload_bypassing_cache_action = action_for(actions.reload_bypassing_cache_action);
- m_copy_selection_action = action_for(actions.copy_selection_action);
- m_undo_action = action_for(actions.undo_action);
- m_redo_action = action_for(actions.redo_action);
- m_cut_selection_action = action_for(actions.cut_selection_action);
- m_paste_action = action_for(actions.paste_action);
- m_select_all_action = action_for(actions.select_all_action);
- m_open_about_page_action = action_for(actions.open_about_page_action);
- m_open_downloads_page_action = action_for(actions.open_downloads_page_action);
- m_open_settings_page_action = action_for(actions.open_settings_page_action);
- m_zoom_in_action = action_for(actions.zoom_in_action);
- m_zoom_out_action = action_for(actions.zoom_out_action);
- m_reset_zoom_action = action_for(actions.reset_zoom_action);
- m_force_dark_action = action_for(actions.force_dark_action);
- m_toggle_vertical_tabs_expanded_action = action_for(actions.toggle_vertical_tabs_expanded_action);
- m_toggle_menu_bar_action = action_for(actions.toggle_menu_bar_action);
- m_view_source_action = action_for(actions.view_source_action);
- m_report_website_issue_action = action_for(actions.report_website_issue_action);
- m_zoom_menu = menu_for(actions.zoom_menu);
- m_color_scheme_menu = menu_for(actions.color_scheme_menu);
- m_contrast_menu = menu_for(actions.contrast_menu);
- m_motion_menu = menu_for(actions.motion_menu);
- m_history_menu = menu_for(actions.history_menu);
- m_inspect_menu = menu_for(actions.inspect_menu);
- m_debug_menu = menu_for(actions.debug_menu);
-}
-
-}
diff --git a/UI/Qt/RemoteView.cpp b/UI/Qt/RemoteView.cpp
index 1e2196f420f..847c5d5945e 100644
--- a/UI/Qt/RemoteView.cpp
+++ b/UI/Qt/RemoteView.cpp
@@ -6,13 +6,17 @@
#include <AK/HashMap.h>
#ifndef LADYBIRD_HUB_IN_OWN_PROCESS
-# include <Hub/ActionMirror.h>
# include <Hub/Application.h>
# include <LibWebView/PageTranslation.h>
#endif
+#include <LibURL/InternalURLs.h>
+#include <LibUnicode/DisplayNames.h>
+#include <LibWebView/PageTranslation.h>
+#include <LibWebView/Translator.h>
+#include <LibWebView/URL.h>
#include <UI/Qt/Application.h>
#include <UI/Qt/Bookmarks.h>
-#include <UI/Qt/RemoteActions.h>
+#include <UI/Qt/ContextMenus.h>
#include <UI/Qt/RemoteView.h>
namespace Ladybird {
@@ -26,12 +30,9 @@ RemoteView::RemoteView(Optional<u64> view_id, Hub::View::InitialState initial_st
, m_display_id(initial_state.display_id)
{
#ifdef LADYBIRD_HUB_IN_OWN_PROCESS
- Hub::ViewActions actions;
if (view_id.has_value()) {
- auto description = HubConnection::the().view_description(*view_id);
m_view_id = *view_id;
- m_state = description.take_state();
- actions = description.take_actions();
+ m_state = HubConnection::the().view_description(*view_id);
} else {
auto view = HubConnection::the().create_view({
.is_private = initial_state.is_private,
@@ -46,7 +47,6 @@ RemoteView::RemoteView(Optional<u64> view_id, Hub::View::InitialState initial_st
});
m_view_id = view.view_id();
m_state = view.take_state();
- actions = view.take_actions();
}
#else
if (view_id.has_value()) {
@@ -58,18 +58,28 @@ RemoteView::RemoteView(Optional<u64> view_id, Hub::View::InitialState initial_st
}
m_view_id = m_view->view_id();
m_state = m_view->current_state();
- auto actions = Hub::ActionMirror::the().view_actions(*m_view);
#endif
- auto& remote_actions = RemoteActions::the();
- m_navigate_back_action = remote_actions.action_for(actions.navigate_back_action);
- m_navigate_forward_action = remote_actions.action_for(actions.navigate_forward_action);
+ m_navigate_back_action = WebView::Action::create("Back"sv, WebView::ActionID::NavigateBack, [this] {
+ traverse_the_history_by_delta(-1);
+ });
+ m_navigate_forward_action = WebView::Action::create("Forward"sv, WebView::ActionID::NavigateForward, [this] {
+ traverse_the_history_by_delta(+1);
+ });
m_toggle_bookmark_action = WebView::Action::create("Toggle Bookmark"sv, WebView::ActionID::ToggleBookmarkViaToolbar, [] {
Application::the().show_bookmark_popover();
});
- m_reset_zoom_action = remote_actions.action_for(actions.reset_zoom_action);
- m_translate_action = remote_actions.action_for(actions.translate_action);
+ m_reset_zoom_action = WebView::Action::create("100%"sv, WebView::ActionID::ResetZoomViaToolbar, [this] {
+ reset_zoom();
+ });
+ m_reset_zoom_action->set_tooltip("Reset zoom level"sv);
+ m_copy_link_to_highlight_action = WebView::Action::create("Copy Link to Highlight"sv, WebView::ActionID::CopyLinkToHighlight, [this] {
+ if (m_link_to_highlight.has_value())
+ Application::the().set_clipboard_text(MUST(String::from_byte_string(WebView::url_text_to_copy(*m_link_to_highlight))));
+ });
+ create_translation_actions();
+ update_view_actions();
update_bookmark_action();
s_views.set(m_view_id, this);
@@ -302,12 +312,6 @@ WebView::WebCompatReport::Page RemoteView::describe_page() const
return { .url = page.take_url(), .user_agent = page.take_user_agent(), .viewport_size = page.viewport_size(), .device_pixel_ratio = page.device_pixel_ratio(), .zoom_level = page.zoom_level() };
}
-WebView::Menu& RemoteView::translation_menu()
-{
- m_translation_menu = RemoteActions::the().menu_for(HubConnection::the().view_translation_menu(view_id()));
- return *m_translation_menu;
-}
-
Vector<WebView::HistoryEntry> RemoteView::recent_history_entries(size_t limit) const
{
return HubConnection::the().view_recent_history_entries(view_id(), limit);
@@ -355,12 +359,6 @@ WebView::WebCompatReport::Page RemoteView::describe_page() const
return WebView::WebCompatReport::describe_page(*m_view);
}
-WebView::Menu& RemoteView::translation_menu()
-{
- m_translation_menu = RemoteActions::the().menu_for(Hub::ActionMirror::the().entries_of(m_view->page_translation().translation_menu()));
- return *m_translation_menu;
-}
-
Vector<WebView::HistoryEntry> RemoteView::recent_history_entries(size_t limit) const
{
return m_view->recent_history_entries(limit);
@@ -397,11 +395,98 @@ RemoteView* RemoteView::find(u64 view_id)
return s_views.get(view_id).value_or(nullptr);
}
-void RemoteView::did_request_context_menu(Vector<Hub::MenuEntry> const& entries, Gfx::IntPoint widget_position)
+// The browser's own strings are English, so languages are named in English.
+static String language_display_name(StringView language)
+{
+ auto primary_language = WebView::primary_language_subtag(language);
+ if (auto name = Unicode::language_display_name("en"sv, primary_language, Unicode::LanguageDisplay::Standard); name.has_value())
+ return name->to_utf8();
+ return MUST(String::from_utf8(primary_language));
+}
+
+void RemoteView::create_translation_actions()
+{
+ m_translate_action = WebView::Action::create("Translate"sv, WebView::ActionID::TranslatePage, [] { });
+
+ m_translation_menu = WebView::Menu::create("Translation"sv);
+ for (size_t index = 0; index < WebView::PageTranslation::MAXIMUM_OFFERED_LANGUAGES; ++index) {
+ auto action = WebView::Action::create_checkable("Translate"sv, WebView::ActionID::TranslateTo, [this, index] {
+ if (index < m_state.translation_languages.size())
+ HubConnection::the().async_view_translate_to(view_id(), m_state.translation_languages[index]);
+ // NB: The check mark shows the language the page is in, which activating an action doesn't change by itself.
+ update_view_actions();
+ });
+ m_translation_menu->add_action(action);
+ m_translate_to_actions.append(move(action));
+ }
+ m_show_original_action = WebView::Action::create("Show Original"sv, WebView::ActionID::ShowOriginal, [this] {
+ HubConnection::the().async_view_show_original(view_id());
+ });
+ m_translation_menu->add_action(*m_show_original_action);
+ m_translation_menu->add_separator();
+ m_download_languages_action = WebView::Action::create("Download Languages…"sv, WebView::ActionID::DownloadTranslationLanguages, [] {
+ if (auto* translator = Application::the().translator())
+ translator->open_language_download_settings();
+ });
+ m_translation_menu->add_action(*m_download_languages_action);
+ m_translation_menu->add_action(WebView::Action::create("Preferred Languages…"sv, WebView::ActionID::EditPreferredLanguages, [] {
+ auto url = URL::about_settings();
+ url.set_fragment("editLanguages"sv);
+ Application::the().activate_or_open_tab_with_url(url);
+ }));
+}
+
+void RemoteView::update_translation_actions()
+{
+ m_translate_action->set_visible(m_state.translation_offered);
+ m_translate_action->set_engaged(m_state.is_translated);
+ m_translate_action->set_tooltip(m_state.is_translated ? "Showing translation"sv : "Translate this page"sv);
+
+ auto const& target_language = m_state.translation_target_language;
+ for (size_t slot = 0; slot < m_translate_to_actions.size(); ++slot) {
+ auto& action = m_translate_to_actions[slot];
+ if (slot >= m_state.translation_languages.size()) {
+ action->set_visible(false);
+ continue;
+ }
+ auto const& language = m_state.translation_languages[slot];
+ action->set_text(MUST(String::formatted("Translate to {}", language_display_name(language))));
+ action->set_checked(target_language.has_value() && WebView::is_same_language(*target_language, language));
+ action->set_visible(true);
+ }
+
+ m_show_original_action->set_enabled(m_state.is_translated);
+ m_download_languages_action->set_visible(m_state.translation_needs_language_download);
+}
+
+void RemoteView::update_view_actions()
+{
+ // NB: The state can come before the view has made its actions.
+ if (!m_navigate_back_action)
+ return;
+
+ m_navigate_back_action->set_enabled(m_state.can_navigate_back);
+ m_navigate_forward_action->set_enabled(m_state.can_navigate_forward);
+ if (m_state.zoom_level != 1.0) {
+ m_reset_zoom_action->set_text(MUST(String::formatted("{}%", round_to<int>(m_state.zoom_level * 100))));
+ m_reset_zoom_action->set_visible(true);
+ } else {
+ m_reset_zoom_action->set_visible(false);
+ }
+ update_translation_actions();
+}
+
+void RemoteView::did_request_context_menu(WebView::ContextMenuRequest const& request)
{
// NB: The menu has to outlive this call, since the view may show it later.
- m_context_menu = RemoteActions::the().menu_for(entries);
- show_context_menu(*m_context_menu, widget_position);
+ m_context_menu = create_context_menu(*this, request);
+ show_context_menu(*m_context_menu, request.widget_position);
+}
+
+void RemoteView::did_make_link_to_highlight(Optional<URL::URL> url)
+{
+ m_link_to_highlight = move(url);
+ m_copy_link_to_highlight_action->set_enabled(m_link_to_highlight.has_value());
}
}
diff --git a/UI/Qt/RemoteView.h b/UI/Qt/RemoteView.h
index 80221c60942..1dbf34a95a2 100644
--- a/UI/Qt/RemoteView.h
+++ b/UI/Qt/RemoteView.h
@@ -8,6 +8,7 @@
#include <AK/NonnullOwnPtr.h>
#include <Hub/View.h>
+#include <LibWebView/ContextMenuRequest.h>
#include <LibWebView/HistoryStore.h>
#include <LibWebView/WebCompatReport.h>
#include <UI/Qt/BookmarkStore.h>
@@ -31,10 +32,15 @@ public:
static RemoteView* find(u64 view_id);
- void did_request_context_menu(Vector<Hub::MenuEntry> const&, Gfx::IntPoint widget_position);
+ void did_request_context_menu(WebView::ContextMenuRequest const&);
+ void did_make_link_to_highlight(Optional<URL::URL>);
- void set_state(Hub::ViewState state) { m_state = move(state); }
- void set_initial_state(Hub::ViewState state) { m_state = move(state); }
+ void set_state(Hub::ViewState state)
+ {
+ m_state = move(state);
+ update_view_actions();
+ }
+ void set_initial_state(Hub::ViewState state) { set_state(move(state)); }
void did_update_zoom() { update_zoom(); }
void did_get_selected_text(u64 request_id, Optional<ByteString>);
@@ -92,6 +98,14 @@ public:
Optional<ByteString> crash_report_name() const { return m_state.crash_report_name; }
Optional<String> crash_report_website() const { return m_state.crash_report_website; }
void traverse_the_history_to_step(i32 step) { HubConnection::the().async_view_traverse_the_history_to_step(view_id(), step); }
+ void traverse_the_history_by_delta(i32 delta) { HubConnection::the().async_view_traverse_the_history_by_delta(view_id(), delta); }
+ bool can_navigate_back() const { return m_state.can_navigate_back; }
+ bool can_navigate_forward() const { return m_state.can_navigate_forward; }
+ enum class PromptForPath : u8 {
+ No,
+ Yes,
+ };
+ void download_url(URL::URL const& url, PromptForPath prompt_for_path, Optional<String> const& image_mime_type = {}) { HubConnection::the().async_view_download_url(view_id(), url, prompt_for_path == PromptForPath::Yes, image_mime_type); }
Vector<WebView::HistoryEntry> recent_history_entries(size_t limit) const;
Optional<WebView::SessionTabId> session_tab_id() const { return m_session_tab_id; }
void set_session_tab_id(WebView::SessionTabId session_tab_id)
@@ -133,6 +147,9 @@ public:
void find_in_page_previous_match() { HubConnection::the().async_view_find_in_page_previous_match(view_id()); }
void find_in_page_end() { HubConnection::the().async_view_find_in_page_end(view_id()); }
bool can_report_website_issue() const { return m_state.can_report_website_issue; }
+ bool can_undo() const { return m_state.can_undo; }
+ bool can_redo() const { return m_state.can_redo; }
+ bool can_view_source() const { return m_state.can_view_source; }
void report_website_issue() { HubConnection::the().async_view_report_website_issue(view_id()); }
void request_accessibility_tree() { HubConnection::the().async_view_request_accessibility_tree(view_id()); }
void perform_accessibility_action(i64 node_id, String action) { HubConnection::the().async_view_perform_accessibility_action(view_id(), node_id, move(action)); }
@@ -181,9 +198,10 @@ public:
WebView::Action& toggle_bookmark_action() { return *m_toggle_bookmark_action; }
void update_bookmark_action();
WebView::Action& reset_zoom_action() { return *m_reset_zoom_action; }
+ WebView::Action& copy_link_to_highlight_action() { return *m_copy_link_to_highlight_action; }
WebView::Action& translate_action() { return *m_translate_action; }
bool is_translated() const { return m_state.is_translated; }
- WebView::Menu& translation_menu();
+ WebView::Menu& translation_menu() { return *m_translation_menu; }
Vector<SessionHistoryTraversalMenuItem> session_history_traversal_menu_items(int direction) const;
WebView::SessionHistorySnapshot session_history_snapshot() const;
@@ -225,6 +243,10 @@ protected:
private:
virtual void bookmarks_changed() override { update_bookmark_action(); }
+ void update_view_actions();
+ void create_translation_actions();
+ void update_translation_actions();
+
#ifndef LADYBIRD_HUB_IN_OWN_PROCESS
Hub::View* m_view { nullptr };
#endif
@@ -233,8 +255,13 @@ private:
RefPtr<WebView::Action> m_navigate_forward_action;
RefPtr<WebView::Action> m_toggle_bookmark_action;
RefPtr<WebView::Action> m_reset_zoom_action;
+ RefPtr<WebView::Action> m_copy_link_to_highlight_action;
+ Optional<URL::URL> m_link_to_highlight;
RefPtr<WebView::Action> m_translate_action;
RefPtr<WebView::Menu> m_translation_menu;
+ Vector<NonnullRefPtr<WebView::Action>> m_translate_to_actions;
+ RefPtr<WebView::Action> m_show_original_action;
+ RefPtr<WebView::Action> m_download_languages_action;
RefPtr<WebView::Menu> m_context_menu;
u64 m_view_id { 0 };
diff --git a/UI/Qt/Tab.cpp b/UI/Qt/Tab.cpp
index 2d9d17bb06c..6b6b1b76257 100644
--- a/UI/Qt/Tab.cpp
+++ b/UI/Qt/Tab.cpp
@@ -20,6 +20,7 @@
#include <LibWebView/Utilities.h>
#include <LibWebView/WebContentClient.h>
#include <UI/Qt/Application.h>
+#include <UI/Qt/ApplicationActions.h>
#include <UI/Qt/BookmarkPopover.h>
#include <UI/Qt/BrowserWindow.h>
#include <UI/Qt/ChromeStyle.h>
@@ -27,7 +28,6 @@
#include <UI/Qt/Icon.h>
#include <UI/Qt/JavaScriptDialog.h>
#include <UI/Qt/LadybirdButton.h>
-#include <UI/Qt/RemoteActions.h>
#include <UI/Qt/RemoteSession.h>
#if defined(AK_OS_MACOS)
# include <UI/Qt/MacWindow.h>
@@ -875,7 +875,7 @@ void Tab::initialize(Optional<u64> hub_view_id)
void Tab::create_tab_context_menu()
{
- auto& application = RemoteActions::the();
+ auto& application = ApplicationActions::the();
auto* duplicate_tab_action = new QAction("&Duplicate Tab", this);
QObject::connect(duplicate_tab_action, &QAction::triggered, this, [this]() {
@@ -1535,7 +1535,7 @@ void Tab::show_site_settings_popover()
m_window->new_tab_from_url(URL::Parser::basic_parse("about:settings#permissions"sv).release_value(), Web::HTML::ActivateTab::Yes, BrowserWindow::TabLocation::after_current_tab());
};
m_site_settings_popover->on_report_broken_site = [] {
- RemoteActions::the().report_website_issue_action().activate();
+ ApplicationActions::the().report_website_issue_action().activate();
};
m_site_settings_popover->on_open_filter_lists = [this] {
m_window->new_tab_from_url(URL::Parser::basic_parse("about:settings#blocking"sv).release_value(), Web::HTML::ActivateTab::Yes, BrowserWindow::TabLocation::after_current_tab());
diff --git a/UI/Qt/TabBar.cpp b/UI/Qt/TabBar.cpp
index cf87f0ae7a3..4fd81a3724d 100644
--- a/UI/Qt/TabBar.cpp
+++ b/UI/Qt/TabBar.cpp
@@ -16,12 +16,12 @@
#include <LibWebView/SessionStore.h>
#include <UI/Qt/AllTabsPopover.h>
#include <UI/Qt/Application.h>
+#include <UI/Qt/ApplicationActions.h>
#include <UI/Qt/BrowserWindow.h>
#include <UI/Qt/ChromeLayout.h>
#include <UI/Qt/ChromeStyle.h>
#include <UI/Qt/Icon.h>
#include <UI/Qt/LadybirdButton.h>
-#include <UI/Qt/RemoteActions.h>
#include <UI/Qt/RemoteSession.h>
#if defined(AK_OS_MACOS)
# include <UI/Qt/MacWindow.h>
@@ -3401,7 +3401,7 @@ TabWidget::TabWidget(WebView::IsPrivate is_private, QWidget* parent)
m_vertical_tabs_toggle_button = new ChromeHeaderButton(m_vertical_tab_bar_column);
m_vertical_tabs_toggle_button->setObjectName("LadybirdVerticalTabsToggleButton");
- m_vertical_tabs_toggle_button->setDefaultAction(create_application_action(*this, RemoteActions::the().toggle_vertical_tabs_expanded_action()));
+ m_vertical_tabs_toggle_button->setDefaultAction(create_application_action(*this, ApplicationActions::the().toggle_vertical_tabs_expanded_action()));
m_vertical_tabs_toggle_button->setAutoRaise(true);
m_vertical_tabs_toggle_button->setFocusPolicy(Qt::NoFocus);
m_vertical_tabs_toggle_button->setIconSize({ 16, 16 });
diff --git a/UI/Qt/WindowToolbar.cpp b/UI/Qt/WindowToolbar.cpp
index 45339611ba1..7ead6db6604 100644
--- a/UI/Qt/WindowToolbar.cpp
+++ b/UI/Qt/WindowToolbar.cpp
@@ -14,6 +14,7 @@
#include <LibWebView/DownloadPresentation.h>
#include <LibWebView/WebCompatReport.h>
#include <UI/Qt/Application.h>
+#include <UI/Qt/ApplicationActions.h>
#include <UI/Qt/BrowserWindow.h>
#include <UI/Qt/ChromeLayout.h>
#include <UI/Qt/ChromeStyle.h>
@@ -28,7 +29,6 @@
#include <UI/Qt/Motion.h>
#include <UI/Qt/PerformanceMonitorWidget.h>
#include <UI/Qt/Popover.h>
-#include <UI/Qt/RemoteActions.h>
#include <UI/Qt/RemoteDownloads.h>
#include <UI/Qt/StringUtils.h>
#include <UI/Qt/SystemAppearance.h>
@@ -1157,7 +1157,7 @@ WindowToolbar::WindowToolbar(WebView::IsPrivate is_private, QWidget* parent)
, WebView::SettingsObserver(Application::settings())
, m_is_private(is_private)
{
- auto& application = RemoteActions::the();
+ auto& application = ApplicationActions::the();
setObjectName("LadybirdToolbarContainer");
// NB: The style sheet paints the toolbar's background, which a QWidget subclass only draws when asked to.