Skip to content

Truly independent web browser

LibWebView+UI/Qt: Let the user choose what a screenshot shows

Jelle Raaijmakers90d526fffc4d
diff --git a/Libraries/LibWebView/Menu.h b/Libraries/LibWebView/Menu.h
index 8f31943d603..b8ba34ec896 100644
--- a/Libraries/LibWebView/Menu.h
+++ b/Libraries/LibWebView/Menu.h
@@ -48,8 +48,7 @@ enum class ActionID {
IgnoreSpelling,
LearnSpelling,
- TakeVisibleScreenshot,
- TakeFullScreenshot,
+ TakeScreenshot,
ReportWebsiteIssue,
ToggleVerticalTabsExpanded,
diff --git a/Libraries/LibWebView/ViewImplementation.cpp b/Libraries/LibWebView/ViewImplementation.cpp
index bf41dd1e346..46d6d71e7fd 100644
--- a/Libraries/LibWebView/ViewImplementation.cpp
+++ b/Libraries/LibWebView/ViewImplementation.cpp
@@ -4116,7 +4116,7 @@ void ViewImplementation::update_bookmark_action()
m_toggle_bookmark_action->set_engaged(is_bookmarked);
}
-static ErrorOr<LexicalPath> save_screenshot(Gfx::Bitmap const* bitmap)
+static ErrorOr<LexicalPath> write_screenshot(Gfx::Bitmap const* bitmap)
{
if (!bitmap)
return Error::from_string_literal("Failed to take a screenshot");
@@ -4158,7 +4158,7 @@ NonnullRefPtr<Core::Promise<LexicalPath>> ViewImplementation::take_screenshot(Sc
visible_bitmap = m_backup_shared_image_buffer->bitmap_if_present();
}
if (visible_bitmap) {
- if (auto result = save_screenshot(visible_bitmap.ptr()); result.is_error())
+ if (auto result = write_screenshot(visible_bitmap.ptr()); result.is_error())
promise->reject(result.release_error());
else
promise->resolve(result.release_value());
@@ -4179,6 +4179,30 @@ NonnullRefPtr<Core::Promise<LexicalPath>> ViewImplementation::take_screenshot(Sc
return promise;
}
+void ViewImplementation::take_and_save_screenshot(ScreenshotType type)
+{
+ take_screenshot(type)
+ ->when_resolved([](auto const& path) {
+ Application::the().display_download_confirmation_dialog("Screenshot"sv, path);
+ })
+ .when_rejected([](auto const& error) {
+ if (error.is_errno() && error.code() == ECANCELED)
+ return;
+
+ auto error_message = MUST(String::formatted("{}", error));
+ Application::the().display_error_dialog(error_message);
+ });
+}
+
+// A screenshot the UI took itself, such as of the part of the page the user selected.
+void ViewImplementation::save_screenshot(Gfx::Bitmap const& bitmap)
+{
+ if (auto result = write_screenshot(&bitmap); result.is_error())
+ Application::the().display_error_dialog(MUST(String::formatted("{}", result.error())));
+ else
+ Application::the().display_download_confirmation_dialog("Screenshot"sv, result.value());
+}
+
NonnullRefPtr<Core::Promise<LexicalPath>> ViewImplementation::take_dom_node_screenshot(Web::UniqueNodeID node_id)
{
auto promise = Core::Promise<LexicalPath>::construct();
@@ -4204,7 +4228,7 @@ void ViewImplementation::did_receive_screenshot(Badge<WebContentPage>, Gfx::Shar
return;
}
- if (auto result = save_screenshot(screenshot.bitmap()); result.is_error())
+ if (auto result = write_screenshot(screenshot.bitmap()); result.is_error())
m_pending_screenshot->reject(result.release_error());
else
m_pending_screenshot->resolve(result.release_value());
@@ -4316,25 +4340,9 @@ void ViewImplementation::initialize_context_menus()
});
m_look_up_selected_text_action->set_visible(false);
- auto take_and_save_screenshot = [this](auto type) {
- take_screenshot(type)
- ->when_resolved([](auto const& path) {
- Application::the().display_download_confirmation_dialog("Screenshot"sv, path);
- })
- .when_rejected([](auto const& error) {
- if (error.is_errno() && error.code() == ECANCELED)
- return;
-
- auto error_message = MUST(String::formatted("{}", error));
- Application::the().display_error_dialog(error_message);
- });
- };
-
- m_take_visible_screenshot_action = Action::create("Take Visible Screenshot"sv, ActionID::TakeVisibleScreenshot, [take_and_save_screenshot]() {
- take_and_save_screenshot(ScreenshotType::Visible);
- });
- m_take_full_screenshot_action = Action::create("Take Full Screenshot"sv, ActionID::TakeFullScreenshot, [take_and_save_screenshot]() {
- take_and_save_screenshot(ScreenshotType::Full);
+ m_take_screenshot_action = Action::create("Take Screenshot..."sv, ActionID::TakeScreenshot, [this]() {
+ if (on_request_screenshot)
+ on_request_screenshot();
});
m_report_website_issue_action = Action::create("Report Website Issue..."sv, ActionID::ReportWebsiteIssue, [this]() {
@@ -4492,8 +4500,7 @@ void ViewImplementation::initialize_context_menus()
m_page_context_menu->add_action(*m_search_selected_text_action);
m_page_context_menu->add_action(*m_copy_link_to_highlight_action);
m_page_context_menu->add_separator();
- m_page_context_menu->add_action(*m_take_visible_screenshot_action);
- m_page_context_menu->add_action(*m_take_full_screenshot_action);
+ m_page_context_menu->add_action(*m_take_screenshot_action);
m_page_context_menu->add_separator();
m_page_context_menu->add_action(application.view_source_action());
m_page_context_menu->add_action(*m_report_website_issue_action);
diff --git a/Libraries/LibWebView/ViewImplementation.h b/Libraries/LibWebView/ViewImplementation.h
index 6056c0eb061..72feafdf6ba 100644
--- a/Libraries/LibWebView/ViewImplementation.h
+++ b/Libraries/LibWebView/ViewImplementation.h
@@ -462,6 +462,8 @@ public:
Full,
};
NonnullRefPtr<Core::Promise<LexicalPath>> take_screenshot(ScreenshotType);
+ void take_and_save_screenshot(ScreenshotType);
+ void save_screenshot(Gfx::Bitmap const&);
NonnullRefPtr<Core::Promise<LexicalPath>> take_dom_node_screenshot(Web::UniqueNodeID);
virtual void did_receive_screenshot(Badge<WebContentPage>, Gfx::ShareableBitmap const&);
@@ -517,6 +519,7 @@ public:
Function<void(URL::URL const&, URL::Origin const&, ExternalURLHandler const&, Function<void(bool)>)> on_request_external_url_confirmation;
Function<void()> on_request_accept_dialog;
Function<void()> on_request_website_issue_report;
+ Function<void()> on_request_screenshot;
Function<void()> on_request_dismiss_dialog;
Function<void(JsonObject)> on_received_dom_tree;
Function<void(DOMNodeProperties)> on_received_dom_node_properties;
@@ -844,8 +847,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;
- RefPtr<Action> m_take_visible_screenshot_action;
- RefPtr<Action> m_take_full_screenshot_action;
+ RefPtr<Action> m_take_screenshot_action;
RefPtr<Action> m_report_website_issue_action;
RefPtr<Action> m_open_in_new_tab_action;
diff --git a/UI/Qt/ScreenshotSelectionOverlay.cpp b/UI/Qt/ScreenshotSelectionOverlay.cpp
index d4313deb008..b8f630ab039 100644
--- a/UI/Qt/ScreenshotSelectionOverlay.cpp
+++ b/UI/Qt/ScreenshotSelectionOverlay.cpp
@@ -56,7 +56,7 @@ public:
// Shows the page as it was when the selection started or the user last scrolled it, so the user selects what they saw,
// not what the page has changed to since. The rest of the frame dims around the selection.
-ScreenshotSelectionOverlay::ScreenshotSelectionOverlay(QImage frame, QWidget* parent)
+ScreenshotSelectionOverlay::ScreenshotSelectionOverlay(QImage frame, QWidget* parent, Purpose purpose)
: QWidget(parent)
, m_frame(AK::move(frame))
{
@@ -80,7 +80,8 @@ ScreenshotSelectionOverlay::ScreenshotSelectionOverlay(QImage frame, QWidget* pa
auto* layout = new QHBoxLayout(instructions);
layout->setContentsMargins(INSTRUCTIONS_TEXT_INSET, INSTRUCTIONS_BUTTON_INSET, INSTRUCTIONS_BUTTON_INSET, INSTRUCTIONS_BUTTON_INSET);
layout->setSpacing(INSTRUCTIONS_BUTTON_SPACING);
- layout->addWidget(new InstructionsLabel(tr("Drag to select the part of the page that shows the problem."), instructions), 1);
+ auto text = purpose == Purpose::ReportIssue ? tr("Drag to select the part of the page that shows the problem.") : tr("Drag to select the part of the page to save.");
+ layout->addWidget(new InstructionsLabel(text, instructions), 1);
layout->addSpacing(INSTRUCTIONS_TEXT_SPACING - INSTRUCTIONS_BUTTON_SPACING);
auto add_button = [&](QString const& text, char const* object_name, auto on_click) {
@@ -96,8 +97,12 @@ ScreenshotSelectionOverlay::ScreenshotSelectionOverlay(QImage frame, QWidget* pa
m_buttons.append(button);
return button;
};
- add_button(tr("Use Visible Page"), "ScreenshotSelectionWholeFrameButton", [this] { use_whole_frame(); });
- add_button(tr("Continue Without Screenshot"), "ScreenshotSelectionSkipButton", [this] { skip(); });
+ add_button(tr("Capture Visible Page"), "ScreenshotSelectionWholeFrameButton", [this] { use_whole_frame(); });
+ // A report can go without a screenshot, and a saved screenshot can show all of the page rather than what is visible.
+ if (purpose == Purpose::ReportIssue)
+ add_button(tr("Continue Without Screenshot"), "ScreenshotSelectionSkipButton", [this] { skip(); });
+ else
+ add_button(tr("Capture Full Page"), "ScreenshotSelectionFullPageButton", [this] { use_full_page(); });
m_close_button = add_button({}, "ScreenshotSelectionCancelButton", [this] { dismiss(); });
m_close_button->setToolTip(tr("Close"));
m_close_button->setAccessibleName(tr("Close"));
@@ -155,6 +160,13 @@ void ScreenshotSelectionOverlay::use_whole_frame()
finish(m_frame);
}
+void ScreenshotSelectionOverlay::use_full_page()
+{
+ dismiss();
+ if (on_full_page_requested)
+ on_full_page_requested();
+}
+
void ScreenshotSelectionOverlay::skip()
{
finish({});
diff --git a/UI/Qt/ScreenshotSelectionOverlay.h b/UI/Qt/ScreenshotSelectionOverlay.h
index 816c9e5b0dc..8523a08457b 100644
--- a/UI/Qt/ScreenshotSelectionOverlay.h
+++ b/UI/Qt/ScreenshotSelectionOverlay.h
@@ -28,10 +28,16 @@ public:
static constexpr int minimum_selection_size = 8;
- ScreenshotSelectionOverlay(QImage frame, QWidget* parent);
+ enum class Purpose {
+ ReportIssue,
+ SaveScreenshot,
+ };
+
+ ScreenshotSelectionOverlay(QImage frame, QWidget* parent, Purpose = Purpose::ReportIssue);
void select(QRect);
void use_whole_frame();
+ void use_full_page();
void skip();
void dismiss();
@@ -41,6 +47,7 @@ public:
Function<void()> on_close;
Function<void(QImage)> on_finished;
+ Function<void()> on_full_page_requested;
Function<QImage()> on_request_page_frame;
Function<void()> on_page_visibility_change;
diff --git a/UI/Qt/WebContentView.cpp b/UI/Qt/WebContentView.cpp
index c0d8f132516..42dd332cbbd 100644
--- a/UI/Qt/WebContentView.cpp
+++ b/UI/Qt/WebContentView.cpp
@@ -38,7 +38,6 @@
#include <UI/Qt/ChromeStyle.h>
#include <UI/Qt/CrashReportReviewWidget.h>
#include <UI/Qt/ReportReview.h>
-#include <UI/Qt/ScreenshotSelectionOverlay.h>
#include <UI/Qt/ScreenshotViewer.h>
#include <UI/Qt/WebCompatReportReview.h>
#ifdef AK_OS_MACOS
@@ -339,6 +338,10 @@ WebContentView::WebContentView(QWidget* window, Optional<WebView::CanonicalTrave
start_web_compat_report();
};
+ on_request_screenshot = [this] {
+ start_screenshot();
+ };
+
#ifdef AK_OS_MACOS
on_request_dictionary_lookup = [this](auto const& lookup, auto position) {
show_appkit_dictionary_lookup(*this, lookup, position);
@@ -1523,7 +1526,7 @@ void WebContentView::select_web_compat_report_screenshot(Function<void(QImage)>
return;
}
- select_screenshot(frame, AK::move(on_close)).on_finished = AK::move(on_selected);
+ select_screenshot(frame, ScreenshotSelectionOverlay::Purpose::ReportIssue, AK::move(on_close)).on_finished = AK::move(on_selected);
}
// The tab was opened only for the review, so no web content runs behind it, and leaving the review closes it.
@@ -1587,11 +1590,11 @@ void WebContentView::review_web_compat_report(WebCompatReportCapture capture)
}
// The selection shows a frame taken earlier, so the user selects what they saw, even while the page underneath changes.
-ScreenshotSelectionOverlay& WebContentView::select_screenshot(QImage const& frame, Function<void()> on_close)
+ScreenshotSelectionOverlay& WebContentView::select_screenshot(QImage const& frame, ScreenshotSelectionOverlay::Purpose purpose, Function<void()> on_close)
{
VERIFY(!m_screenshot_selection_overlay);
- auto* overlay = new ScreenshotSelectionOverlay(frame, this);
+ auto* overlay = new ScreenshotSelectionOverlay(frame, this, purpose);
m_screenshot_selection_overlay = overlay;
auto focus_to_restore = show_transient_overlay(*overlay);
overlay->on_close = [this, overlay, focus_to_restore, on_close = AK::move(on_close)] {
@@ -1604,6 +1607,30 @@ ScreenshotSelectionOverlay& WebContentView::select_screenshot(QImage const& fram
return *overlay;
}
+// The user selects the part of the page to save, or saves all of the page they see or the whole page. Without a frame
+// to select from, what the page shows is saved.
+void WebContentView::start_screenshot()
+{
+ if (m_screenshot_selection_overlay)
+ return;
+
+ auto frame = visible_snapshot();
+ if (frame.isNull()) {
+ take_and_save_screenshot(ScreenshotType::Visible);
+ return;
+ }
+
+ auto& overlay = select_screenshot(frame, ScreenshotSelectionOverlay::Purpose::SaveScreenshot);
+ overlay.on_finished = [this](QImage screenshot) {
+ auto image = screenshot.convertToFormat(QImage::Format_ARGB32);
+ auto bitmap = MUST(Gfx::Bitmap::create_wrapper(Gfx::BitmapFormat::BGRA8888, Gfx::AlphaType::Unpremultiplied, { image.width(), image.height() }, image.bytesPerLine(), image.bits()));
+ save_screenshot(*bitmap);
+ };
+ overlay.on_full_page_requested = [this] {
+ take_and_save_screenshot(ScreenshotType::Full);
+ };
+}
+
// An overlay shown for a moment, such as to select or view a screenshot, hands the focus back once it is done.
QPointer<QWidget> WebContentView::show_transient_overlay(QWidget& overlay)
{
diff --git a/UI/Qt/WebContentView.h b/UI/Qt/WebContentView.h
index b5ea4deb949..a1611496de1 100644
--- a/UI/Qt/WebContentView.h
+++ b/UI/Qt/WebContentView.h
@@ -27,6 +27,7 @@
#include <LibWebView/BrowsingSession.h>
#include <LibWebView/ViewImplementation.h>
#include <LibWebView/WebCompatReport.h>
+#include <UI/Qt/ScreenshotSelectionOverlay.h>
#include <LibWebCommon/Page/QueuedInputEvent.h>
#include <QImage>
@@ -87,7 +88,6 @@ using WebContentViewBase = QWidget;
class CrashOverlayUrlLabel;
class CrashReportReviewWidget;
class PageOverlay;
-class ScreenshotSelectionOverlay;
class ScreenshotViewer;
class SelectDropdown;
@@ -238,7 +238,8 @@ private:
void show_crash_report_review(Optional<ByteString> const& hidden_crash_report_name = {});
void start_web_compat_report();
- ScreenshotSelectionOverlay& select_screenshot(QImage const& frame, Function<void()> on_close = {});
+ ScreenshotSelectionOverlay& select_screenshot(QImage const& frame, ScreenshotSelectionOverlay::Purpose, Function<void()> on_close = {});
+ void start_screenshot();
QPointer<QWidget> show_transient_overlay(QWidget&);
void close_transient_overlay(QWidget&, QPointer<QWidget> const& focus_to_restore);