1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 06:48:12 +00:00

LibWebView: Log the result of taking screenshots to Inspector's console

It probably isn't obvious that screenshots are saved to the user's
Downloads folder, so add a console message to inform them.
This commit is contained in:
Timothy Flynn 2023-12-09 16:57:57 -05:00 committed by Andreas Kling
parent e0450301ab
commit 42c0ac9352
5 changed files with 49 additions and 14 deletions

View file

@ -204,6 +204,14 @@ details > :not(:first-child) {
color: cyan; color: cyan;
} }
.console-message {
color: lightskyblue;
}
.console-warning {
color: orange;
}
.console-input { .console-input {
background-color: rgb(57, 57, 57); background-color: rgb(57, 57, 57);
} }
@ -218,6 +226,14 @@ details > :not(:first-child) {
color: blue; color: blue;
} }
.console-message {
color: blue;
}
.console-warning {
color: darkorange;
}
.console-input { .console-input {
background-color: rgb(229, 229, 229); background-color: rgb(229, 229, 229);
} }

View file

@ -249,7 +249,10 @@ void InspectorClient::context_menu_screenshot_dom_node()
{ {
VERIFY(m_context_menu_data.has_value()); VERIFY(m_context_menu_data.has_value());
m_content_web_view.take_dom_node_screenshot(m_context_menu_data->dom_node_id).release_value_but_fixme_should_propagate_errors(); if (auto result = m_content_web_view.take_dom_node_screenshot(m_context_menu_data->dom_node_id); result.is_error())
append_console_warning(MUST(String::formatted("Warning: {}", result.error())));
else
append_console_message(MUST(String::formatted("Screenshot saved to: {}", result.value())));
m_context_menu_data.clear(); m_context_menu_data.clear();
} }
@ -645,13 +648,30 @@ void InspectorClient::handle_console_messages(i32 start_index, ReadonlySpan<Depr
void InspectorClient::append_console_source(StringView source) void InspectorClient::append_console_source(StringView source)
{ {
StringBuilder builder; StringBuilder builder;
builder.append("<span class=\"console-prompt\">&gt;&nbsp;</span>"sv); builder.append("<span class=\"console-prompt\">&gt;&nbsp;</span>"sv);
builder.append(MUST(JS::MarkupGenerator::html_from_source(source))); builder.append(MUST(JS::MarkupGenerator::html_from_source(source)));
append_console_output(builder.string_view()); append_console_output(builder.string_view());
} }
void InspectorClient::append_console_message(StringView message)
{
StringBuilder builder;
builder.append("<span class=\"console-prompt\">#&nbsp;</span>"sv);
builder.appendff("<span class=\"console-message\">{}</span>", message);
append_console_output(builder.string_view());
}
void InspectorClient::append_console_warning(StringView warning)
{
StringBuilder builder;
builder.append("<span class=\"console-prompt\">#&nbsp;</span>"sv);
builder.appendff("<span class=\"console-warning\">{}</span>", warning);
append_console_output(builder.string_view());
}
void InspectorClient::append_console_output(StringView html) void InspectorClient::append_console_output(StringView html)
{ {
auto html_base64 = MUST(encode_base64(html.bytes())); auto html_base64 = MUST(encode_base64(html.bytes()));

View file

@ -53,6 +53,8 @@ private:
void handle_console_messages(i32 start_index, ReadonlySpan<DeprecatedString> message_types, ReadonlySpan<DeprecatedString> messages); void handle_console_messages(i32 start_index, ReadonlySpan<DeprecatedString> message_types, ReadonlySpan<DeprecatedString> messages);
void append_console_source(StringView); void append_console_source(StringView);
void append_console_message(StringView);
void append_console_warning(StringView);
void append_console_output(StringView); void append_console_output(StringView);
void clear_console_output(); void clear_console_output();

View file

@ -5,7 +5,6 @@
*/ */
#include <AK/Error.h> #include <AK/Error.h>
#include <AK/LexicalPath.h>
#include <AK/String.h> #include <AK/String.h>
#include <LibCore/DateTime.h> #include <LibCore/DateTime.h>
#include <LibCore/StandardPaths.h> #include <LibCore/StandardPaths.h>
@ -394,7 +393,7 @@ void ViewImplementation::handle_web_content_process_crash()
load_html(builder.to_deprecated_string()); load_html(builder.to_deprecated_string());
} }
static ErrorOr<void> save_screenshot(Gfx::ShareableBitmap const& bitmap) static ErrorOr<LexicalPath> save_screenshot(Gfx::ShareableBitmap const& bitmap)
{ {
if (!bitmap.is_valid()) if (!bitmap.is_valid())
return Error::from_string_view("Failed to take a screenshot"sv); return Error::from_string_view("Failed to take a screenshot"sv);
@ -407,10 +406,10 @@ static ErrorOr<void> save_screenshot(Gfx::ShareableBitmap const& bitmap)
auto screenshot_file = TRY(Core::File::open(path.string(), Core::File::OpenMode::Write)); auto screenshot_file = TRY(Core::File::open(path.string(), Core::File::OpenMode::Write));
TRY(screenshot_file->write_until_depleted(encoded)); TRY(screenshot_file->write_until_depleted(encoded));
return {}; return path;
} }
ErrorOr<void> ViewImplementation::take_screenshot(ScreenshotType type) ErrorOr<LexicalPath> ViewImplementation::take_screenshot(ScreenshotType type)
{ {
Gfx::ShareableBitmap bitmap; Gfx::ShareableBitmap bitmap;
@ -424,16 +423,13 @@ ErrorOr<void> ViewImplementation::take_screenshot(ScreenshotType type)
break; break;
} }
TRY(save_screenshot(bitmap)); return save_screenshot(bitmap);
return {};
} }
ErrorOr<void> ViewImplementation::take_dom_node_screenshot(i32 node_id) ErrorOr<LexicalPath> ViewImplementation::take_dom_node_screenshot(i32 node_id)
{ {
auto bitmap = client().take_dom_node_screenshot(node_id); auto bitmap = client().take_dom_node_screenshot(node_id);
TRY(save_screenshot(bitmap)); return save_screenshot(bitmap);
return {};
} }
void ViewImplementation::set_user_style_sheet(String source) void ViewImplementation::set_user_style_sheet(String source)

View file

@ -9,6 +9,7 @@
#include <AK/Forward.h> #include <AK/Forward.h>
#include <AK/Function.h> #include <AK/Function.h>
#include <AK/LexicalPath.h>
#include <AK/String.h> #include <AK/String.h>
#include <LibGfx/Forward.h> #include <LibGfx/Forward.h>
#include <LibGfx/StandardCursor.h> #include <LibGfx/StandardCursor.h>
@ -95,8 +96,8 @@ public:
Visible, Visible,
Full, Full,
}; };
ErrorOr<void> take_screenshot(ScreenshotType); ErrorOr<LexicalPath> take_screenshot(ScreenshotType);
ErrorOr<void> take_dom_node_screenshot(i32); ErrorOr<LexicalPath> take_dom_node_screenshot(i32);
void set_user_style_sheet(String source); void set_user_style_sheet(String source);
// Load Native.css as the User style sheet, which attempts to make WebView content look as close to // Load Native.css as the User style sheet, which attempts to make WebView content look as close to