From ea7ce76d208c283d46b0645b823c7f742f712e90 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Sat, 5 Nov 2022 00:10:31 -0400 Subject: [PATCH] Browser: Add a context menu item to take a full document screenshot --- .../Applications/Browser/BrowserWindow.cpp | 24 ++++++++++++++++--- Userland/Applications/Browser/BrowserWindow.h | 8 ++++++- Userland/Applications/Browser/Tab.cpp | 1 + 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/Userland/Applications/Browser/BrowserWindow.cpp b/Userland/Applications/Browser/BrowserWindow.cpp index 729d61dbfc1..31c363d7e40 100644 --- a/Userland/Applications/Browser/BrowserWindow.cpp +++ b/Userland/Applications/Browser/BrowserWindow.cpp @@ -246,12 +246,20 @@ void BrowserWindow::build_menus() m_take_visible_screenshot_action = GUI::Action::create( "Take &Visible Screenshot"sv, g_icon_bag.filetype_image, [this](auto&) { - if (auto result = take_screenshot(); result.is_error()) + if (auto result = take_screenshot(ScreenshotType::Visible); result.is_error()) GUI::MessageBox::show_error(this, String::formatted("{}", result.error())); }, this); m_take_visible_screenshot_action->set_status_tip("Save a screenshot of the visible portion of the current tab to the Downloads directory"sv); + m_take_full_screenshot_action = GUI::Action::create( + "Take &Full Screenshot"sv, g_icon_bag.filetype_image, [this](auto&) { + if (auto result = take_screenshot(ScreenshotType::Full); result.is_error()) + GUI::MessageBox::show_error(this, String::formatted("{}", result.error())); + }, + this); + m_take_full_screenshot_action->set_status_tip("Save a screenshot of the entirety of the current tab to the Downloads directory"sv); + auto& inspect_menu = add_menu("&Inspect"); inspect_menu.add_action(*m_view_source_action); inspect_menu.add_action(*m_inspect_dom_tree_action); @@ -758,12 +766,22 @@ void BrowserWindow::event(Core::Event& event) Window::event(event); } -ErrorOr BrowserWindow::take_screenshot() +ErrorOr BrowserWindow::take_screenshot(ScreenshotType type) { if (!active_tab().on_take_screenshot) return {}; - auto bitmap = active_tab().on_take_screenshot(); + Gfx::ShareableBitmap bitmap; + + switch (type) { + case ScreenshotType::Visible: + bitmap = active_tab().on_take_screenshot(); + break; + case ScreenshotType::Full: + bitmap = active_tab().view().take_document_screenshot(); + break; + } + if (!bitmap.is_valid()) return Error::from_string_view("Failed to take a screenshot of the current tab"sv); diff --git a/Userland/Applications/Browser/BrowserWindow.h b/Userland/Applications/Browser/BrowserWindow.h index 94ee02cafce..ee039d6e612 100644 --- a/Userland/Applications/Browser/BrowserWindow.h +++ b/Userland/Applications/Browser/BrowserWindow.h @@ -40,6 +40,7 @@ public: GUI::Action& inspect_dom_tree_action() { return *m_inspect_dom_tree_action; } GUI::Action& inspect_dom_node_action() { return *m_inspect_dom_node_action; } GUI::Action& take_visible_screenshot_action() { return *m_take_visible_screenshot_action; } + GUI::Action& take_full_screenshot_action() { return *m_take_full_screenshot_action; } void content_filters_changed(); void proxy_mappings_changed(); @@ -59,7 +60,11 @@ private: virtual void event(Core::Event&) override; - ErrorOr take_screenshot(); + enum class ScreenshotType { + Visible, + Full, + }; + ErrorOr take_screenshot(ScreenshotType); RefPtr m_go_back_action; RefPtr m_go_forward_action; @@ -71,6 +76,7 @@ private: RefPtr m_inspect_dom_tree_action; RefPtr m_inspect_dom_node_action; RefPtr m_take_visible_screenshot_action; + RefPtr m_take_full_screenshot_action; CookieJar& m_cookie_jar; WindowActions m_window_actions; diff --git a/Userland/Applications/Browser/Tab.cpp b/Userland/Applications/Browser/Tab.cpp index 4af1f632691..8141f1c0d8a 100644 --- a/Userland/Applications/Browser/Tab.cpp +++ b/Userland/Applications/Browser/Tab.cpp @@ -406,6 +406,7 @@ Tab::Tab(BrowserWindow& window) m_page_context_menu->add_action(window.inspect_dom_node_action()); m_page_context_menu->add_separator(); m_page_context_menu->add_action(window.take_visible_screenshot_action()); + m_page_context_menu->add_action(window.take_full_screenshot_action()); view().on_context_menu_request = [&](auto& screen_position) { m_page_context_menu->popup(screen_position); };