summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTimothy Flynn <trflynn89@pm.me>2021-07-14 08:33:35 -0400
committerAndreas Kling <kling@serenityos.org>2021-07-14 17:16:39 +0200
commit58cb37f986dbabd2973d5b975a1166a6f240647e (patch)
treec91275459b9e11047eb79b6ab35de1b4a5c671da
parent2fda6ce1595f093c770063f6b597ebd30bd3db34 (diff)
downloadserenity-58cb37f986dbabd2973d5b975a1166a6f240647e.zip
Browser: Add context menu item for copying selected text
-rw-r--r--Userland/Applications/Browser/BrowserWindow.cpp14
-rw-r--r--Userland/Applications/Browser/BrowserWindow.h2
-rw-r--r--Userland/Applications/Browser/Tab.cpp2
3 files changed, 18 insertions, 0 deletions
diff --git a/Userland/Applications/Browser/BrowserWindow.cpp b/Userland/Applications/Browser/BrowserWindow.cpp
index eb02fb5955..b45fc44a0b 100644
--- a/Userland/Applications/Browser/BrowserWindow.cpp
+++ b/Userland/Applications/Browser/BrowserWindow.cpp
@@ -17,6 +17,7 @@
#include <LibCore/StandardPaths.h>
#include <LibGUI/AboutDialog.h>
#include <LibGUI/Application.h>
+#include <LibGUI/Clipboard.h>
#include <LibGUI/Icon.h>
#include <LibGUI/InputBox.h>
#include <LibGUI/Menu.h>
@@ -175,6 +176,19 @@ void BrowserWindow::build_menus()
go_menu.add_separator();
go_menu.add_action(*m_reload_action);
+ m_copy_selection_action = GUI::CommonActions::make_copy_action([this](auto&) {
+ auto& tab = active_tab();
+ String selected_text;
+
+ if (tab.m_type == Tab::Type::InProcessWebView)
+ selected_text = tab.m_page_view->selected_text();
+ else
+ selected_text = tab.m_web_content_view->selected_text();
+
+ if (!selected_text.is_empty())
+ GUI::Clipboard::the().set_plain_text(selected_text);
+ });
+
m_view_source_action = GUI::Action::create(
"View &Source", { Mod_Ctrl, Key_U }, [this](auto&) {
auto& tab = active_tab();
diff --git a/Userland/Applications/Browser/BrowserWindow.h b/Userland/Applications/Browser/BrowserWindow.h
index ec149ca7e3..45bd69c5d1 100644
--- a/Userland/Applications/Browser/BrowserWindow.h
+++ b/Userland/Applications/Browser/BrowserWindow.h
@@ -30,6 +30,7 @@ public:
GUI::Action& go_forward_action() { return *m_go_forward_action; }
GUI::Action& go_home_action() { return *m_go_home_action; }
GUI::Action& reload_action() { return *m_reload_action; }
+ GUI::Action& copy_selection_action() { return *m_copy_selection_action; }
GUI::Action& view_source_action() { return *m_view_source_action; }
GUI::Action& inspect_dom_tree_action() { return *m_inspect_dom_tree_action; }
@@ -43,6 +44,7 @@ private:
RefPtr<GUI::Action> m_go_forward_action;
RefPtr<GUI::Action> m_go_home_action;
RefPtr<GUI::Action> m_reload_action;
+ RefPtr<GUI::Action> m_copy_selection_action;
RefPtr<GUI::Action> m_view_source_action;
RefPtr<GUI::Action> m_inspect_dom_tree_action;
diff --git a/Userland/Applications/Browser/Tab.cpp b/Userland/Applications/Browser/Tab.cpp
index 49fc92c881..9cc555dad1 100644
--- a/Userland/Applications/Browser/Tab.cpp
+++ b/Userland/Applications/Browser/Tab.cpp
@@ -332,6 +332,8 @@ Tab::Tab(BrowserWindow& window, Type type)
m_page_context_menu->add_action(window.go_forward_action());
m_page_context_menu->add_action(window.reload_action());
m_page_context_menu->add_separator();
+ m_page_context_menu->add_action(window.copy_selection_action());
+ m_page_context_menu->add_separator();
m_page_context_menu->add_action(window.view_source_action());
m_page_context_menu->add_action(window.inspect_dom_tree_action());
hooks().on_context_menu_request = [&](auto& screen_position) {