diff options
author | Timothy Flynn <trflynn89@pm.me> | 2023-05-17 09:53:13 -0400 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2023-05-17 19:47:05 +0200 |
commit | d8b14da380f0d35f5197b741c81f84c64cfb6204 (patch) | |
tree | 1a67b0b15771b5feac5cae6eb5a182035e4accea /Userland/Applications | |
parent | 5312a140fedc08f8fd2469b65f5d7b05a172bd62 (diff) | |
download | serenity-d8b14da380f0d35f5197b741c81f84c64cfb6204.zip |
Browser+Ladybird+LibWebView: Move some common functions to LibWebView
The implementations of handle_web_content_process_crash and
take_screenshot are exactly the same across Browser and Ladybird. Let's
reduce some code duplication and move them to LibWebView.
Diffstat (limited to 'Userland/Applications')
-rw-r--r-- | Userland/Applications/Browser/BrowserWindow.cpp | 53 | ||||
-rw-r--r-- | Userland/Applications/Browser/BrowserWindow.h | 10 | ||||
-rw-r--r-- | Userland/Applications/Browser/Tab.cpp | 24 | ||||
-rw-r--r-- | Userland/Applications/Browser/Tab.h | 1 |
4 files changed, 22 insertions, 66 deletions
diff --git a/Userland/Applications/Browser/BrowserWindow.cpp b/Userland/Applications/Browser/BrowserWindow.cpp index 8aaaa84f40..c6bd6e5499 100644 --- a/Userland/Applications/Browser/BrowserWindow.cpp +++ b/Userland/Applications/Browser/BrowserWindow.cpp @@ -14,10 +14,8 @@ #include "CookieJar.h" #include "InspectorWidget.h" #include "Tab.h" -#include <AK/LexicalPath.h> #include <Applications/Browser/BrowserWindowGML.h> #include <LibConfig/Client.h> -#include <LibCore/DateTime.h> #include <LibCore/StandardPaths.h> #include <LibGUI/Application.h> #include <LibGUI/Clipboard.h> @@ -32,7 +30,6 @@ #include <LibGUI/TabWidget.h> #include <LibGUI/ToolbarContainer.h> #include <LibGUI/Widget.h> -#include <LibGfx/ImageFormats/PNGWriter.h> #include <LibJS/Interpreter.h> #include <LibWeb/CSS/PreferredColorScheme.h> #include <LibWeb/Dump.h> @@ -259,22 +256,6 @@ void BrowserWindow::build_menus() this); m_inspect_dom_node_action->set_status_tip("Open inspector for this element"); - m_take_visible_screenshot_action = GUI::Action::create( - "Take &Visible Screenshot"sv, g_icon_bag.filetype_image, [this](auto&) { - if (auto result = take_screenshot(ScreenshotType::Visible); result.is_error()) - GUI::MessageBox::show_error(this, DeprecatedString::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, DeprecatedString::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"_string.release_value_but_fixme_should_propagate_errors()); inspect_menu.add_action(*m_view_source_action); inspect_menu.add_action(*m_inspect_dom_tree_action); @@ -655,10 +636,6 @@ Tab& BrowserWindow::create_new_tab(URL url, Web::HTML::ActivateTab activate) return active_tab().view().get_session_storage_entries(); }; - new_tab.on_take_screenshot = [this]() { - return active_tab().view().take_screenshot(); - }; - new_tab.load(url); dbgln_if(SPAM_DEBUG, "Added new tab {:p}, loading {}", &new_tab, url); @@ -776,36 +753,6 @@ void BrowserWindow::event(Core::Event& event) Window::event(event); } -ErrorOr<void> BrowserWindow::take_screenshot(ScreenshotType type) -{ - if (!active_tab().on_take_screenshot) - return {}; - - 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); - - LexicalPath path { Core::StandardPaths::downloads_directory() }; - path = path.append(Core::DateTime::now().to_deprecated_string("screenshot-%Y-%m-%d-%H-%M-%S.png"sv)); - - auto encoded = TRY(Gfx::PNGWriter::encode(*bitmap.bitmap())); - - auto screenshot_file = TRY(Core::File::open(path.string(), Core::File::OpenMode::Write)); - TRY(screenshot_file->write_until_depleted(encoded)); - - return {}; -} - void BrowserWindow::update_displayed_zoom_level() { VERIFY(m_zoom_menu); diff --git a/Userland/Applications/Browser/BrowserWindow.h b/Userland/Applications/Browser/BrowserWindow.h index 0d19d196a6..ba94bdeb23 100644 --- a/Userland/Applications/Browser/BrowserWindow.h +++ b/Userland/Applications/Browser/BrowserWindow.h @@ -41,8 +41,6 @@ public: GUI::Action& view_source_action() { return *m_view_source_action; } 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 autoplay_allowlist_changed(); @@ -65,12 +63,6 @@ private: void update_displayed_zoom_level(); - enum class ScreenshotType { - Visible, - Full, - }; - ErrorOr<void> take_screenshot(ScreenshotType); - RefPtr<GUI::Action> m_go_back_action; RefPtr<GUI::Action> m_go_forward_action; RefPtr<GUI::Action> m_go_home_action; @@ -80,8 +72,6 @@ private: RefPtr<GUI::Action> m_view_source_action; RefPtr<GUI::Action> m_inspect_dom_tree_action; RefPtr<GUI::Action> m_inspect_dom_node_action; - RefPtr<GUI::Action> m_take_visible_screenshot_action; - RefPtr<GUI::Action> m_take_full_screenshot_action; RefPtr<GUI::Menu> m_zoom_menu; diff --git a/Userland/Applications/Browser/Tab.cpp b/Userland/Applications/Browser/Tab.cpp index f83e24ecb6..af7e7a5c7c 100644 --- a/Userland/Applications/Browser/Tab.cpp +++ b/Userland/Applications/Browser/Tab.cpp @@ -554,6 +554,26 @@ Tab::Tab(BrowserWindow& window) on_tab_close_other_request(*this); })); + auto take_visible_screenshot_action = GUI::Action::create( + "Take &Visible Screenshot"sv, g_icon_bag.filetype_image, [this](auto&) { + if (auto result = view().take_screenshot(WebView::ViewImplementation::ScreenshotType::Visible); result.is_error()) { + auto error = String::formatted("{}", result.error()).release_value_but_fixme_should_propagate_errors(); + GUI::MessageBox::show_error(&this->window(), error); + } + }, + this); + take_visible_screenshot_action->set_status_tip("Save a screenshot of the visible portion of the current tab to the Downloads directory"sv); + + auto take_full_screenshot_action = GUI::Action::create( + "Take &Full Screenshot"sv, g_icon_bag.filetype_image, [this](auto&) { + if (auto result = view().take_screenshot(WebView::ViewImplementation::ScreenshotType::Full); result.is_error()) { + auto error = String::formatted("{}", result.error()).release_value_but_fixme_should_propagate_errors(); + GUI::MessageBox::show_error(&this->window(), error); + } + }, + this); + take_full_screenshot_action->set_status_tip("Save a screenshot of the entirety of the current tab to the Downloads directory"sv); + m_page_context_menu = GUI::Menu::construct(); m_page_context_menu->add_action(window.go_back_action()); m_page_context_menu->add_action(window.go_forward_action()); @@ -562,8 +582,8 @@ Tab::Tab(BrowserWindow& window) m_page_context_menu->add_action(window.copy_selection_action()); m_page_context_menu->add_action(window.select_all_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()); + m_page_context_menu->add_action(move(take_visible_screenshot_action)); + m_page_context_menu->add_action(move(take_full_screenshot_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()); diff --git a/Userland/Applications/Browser/Tab.h b/Userland/Applications/Browser/Tab.h index ecc4f300d0..6fe3589bdb 100644 --- a/Userland/Applications/Browser/Tab.h +++ b/Userland/Applications/Browser/Tab.h @@ -77,7 +77,6 @@ public: Function<Vector<Web::Cookie::Cookie>()> on_get_cookies_entries; Function<OrderedHashMap<DeprecatedString, DeprecatedString>()> on_get_local_storage_entries; Function<OrderedHashMap<DeprecatedString, DeprecatedString>()> on_get_session_storage_entries; - Function<Gfx::ShareableBitmap()> on_take_screenshot; void enable_webdriver_mode(); |