mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 17:47:44 +00:00
Browser: Add a context menu item to take a screenshot of the current tab
This commit is contained in:
parent
43888b848c
commit
ce8c34a8c9
3 changed files with 37 additions and 0 deletions
|
@ -14,8 +14,10 @@
|
|||
#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 <LibCore/Stream.h>
|
||||
#include <LibCore/Version.h>
|
||||
|
@ -33,6 +35,7 @@
|
|||
#include <LibGUI/TabWidget.h>
|
||||
#include <LibGUI/ToolbarContainer.h>
|
||||
#include <LibGUI/Widget.h>
|
||||
#include <LibGfx/PNGWriter.h>
|
||||
#include <LibJS/Interpreter.h>
|
||||
#include <LibWeb/CSS/PreferredColorScheme.h>
|
||||
#include <LibWeb/Dump.h>
|
||||
|
@ -240,6 +243,14 @@ void BrowserWindow::build_menus()
|
|||
this);
|
||||
m_inspect_dom_node_action->set_status_tip("Open inspector for this element");
|
||||
|
||||
m_take_screenshot_action = GUI::Action::create(
|
||||
"&Take Screenshot"sv, g_icon_bag.filetype_image, [this](auto&) {
|
||||
if (auto result = take_screenshot(); result.is_error())
|
||||
GUI::MessageBox::show_error(this, String::formatted("{}", result.error()));
|
||||
},
|
||||
this);
|
||||
m_take_screenshot_action->set_status_tip("Save a screenshot 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);
|
||||
|
@ -718,4 +729,24 @@ void BrowserWindow::event(Core::Event& event)
|
|||
Window::event(event);
|
||||
}
|
||||
|
||||
ErrorOr<void> BrowserWindow::take_screenshot()
|
||||
{
|
||||
if (!active_tab().on_take_screenshot)
|
||||
return {};
|
||||
|
||||
auto bitmap = active_tab().on_take_screenshot();
|
||||
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_string("screenshot-%Y-%m-%d-%H-%M-%S.png"sv));
|
||||
|
||||
auto encoded = Gfx::PNGWriter::encode(*bitmap.bitmap());
|
||||
|
||||
auto screenshot_file = TRY(Core::Stream::File::open(path.string(), Core::Stream::OpenMode::Write));
|
||||
TRY(screenshot_file->write(encoded));
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue