Ladybird+LibWebView: Add an Inspector action to screenshot a DOM node

This commit is contained in:
Timothy Flynn 2023-12-06 11:56:16 -05:00 committed by Andreas Kling
parent e3df035c5d
commit 71fdf0860e
7 changed files with 31 additions and 0 deletions

View file

@ -171,6 +171,11 @@ static constexpr NSInteger CONTEXT_MENU_COPY_ATTRIBUTE_VALUE_TAG = 3;
m_inspector_client->context_menu_copy_dom_node();
}
- (void)screenshotDOMNode:(id)sender
{
m_inspector_client->context_menu_screenshot_dom_node();
}
- (void)deleteDOMNode:(id)sender
{
m_inspector_client->context_menu_remove_dom_node();
@ -240,6 +245,9 @@ static constexpr NSInteger CONTEXT_MENU_COPY_ATTRIBUTE_VALUE_TAG = 3;
[_dom_node_tag_context_menu addItem:[[NSMenuItem alloc] initWithTitle:@"Copy HTML"
action:@selector(copyDOMNode:)
keyEquivalent:@""]];
[_dom_node_tag_context_menu addItem:[[NSMenuItem alloc] initWithTitle:@"Take node screenshot"
action:@selector(screenshotDOMNode:)
keyEquivalent:@""]];
}
return _dom_node_tag_context_menu;
@ -282,6 +290,9 @@ static constexpr NSInteger CONTEXT_MENU_COPY_ATTRIBUTE_VALUE_TAG = 3;
[_dom_node_attribute_context_menu addItem:[[NSMenuItem alloc] initWithTitle:@"Copy HTML"
action:@selector(copyDOMNode:)
keyEquivalent:@""]];
[_dom_node_attribute_context_menu addItem:[[NSMenuItem alloc] initWithTitle:@"Take node screenshot"
action:@selector(screenshotDOMNode:)
keyEquivalent:@""]];
}
return _dom_node_attribute_context_menu;

View file

@ -33,6 +33,9 @@ InspectorWidget::InspectorWidget(QWidget* tab, WebContentView& content_view)
m_copy_node_action = new QAction("&Copy HTML", this);
connect(m_copy_node_action, &QAction::triggered, [this]() { m_inspector_client->context_menu_copy_dom_node(); });
m_screenshot_node_action = new QAction("Take node &screenshot", this);
connect(m_screenshot_node_action, &QAction::triggered, [this]() { m_inspector_client->context_menu_screenshot_dom_node(); });
m_delete_node_action = new QAction("&Delete node", this);
connect(m_delete_node_action, &QAction::triggered, [this]() { m_inspector_client->context_menu_remove_dom_node(); });
@ -58,6 +61,7 @@ InspectorWidget::InspectorWidget(QWidget* tab, WebContentView& content_view)
m_dom_node_tag_context_menu->addAction(m_delete_node_action);
m_dom_node_tag_context_menu->addSeparator();
m_dom_node_tag_context_menu->addAction(m_copy_node_action);
m_dom_node_tag_context_menu->addAction(m_screenshot_node_action);
m_dom_node_attribute_context_menu = new QMenu("DOM attribute context menu", this);
m_dom_node_attribute_context_menu->addAction(m_edit_node_action);
@ -68,6 +72,7 @@ InspectorWidget::InspectorWidget(QWidget* tab, WebContentView& content_view)
m_dom_node_attribute_context_menu->addAction(m_delete_node_action);
m_dom_node_attribute_context_menu->addSeparator();
m_dom_node_attribute_context_menu->addAction(m_copy_node_action);
m_dom_node_attribute_context_menu->addAction(m_screenshot_node_action);
m_inspector_client->on_requested_dom_node_text_context_menu = [this](auto position) {
m_edit_node_action->setText("&Edit text");

View file

@ -45,6 +45,7 @@ private:
QAction* m_edit_node_action { nullptr };
QAction* m_copy_node_action { nullptr };
QAction* m_screenshot_node_action { nullptr };
QAction* m_delete_node_action { nullptr };
QAction* m_add_attribute_action { nullptr };
QAction* m_remove_attribute_action { nullptr };

View file

@ -31,6 +31,7 @@ InspectorWidget::InspectorWidget(WebView::OutOfProcessWebView& content_view)
m_edit_node_action = GUI::Action::create("&Edit node"sv, [this](auto&) { m_inspector_client->context_menu_edit_dom_node(); });
m_copy_node_action = GUI::Action::create("&Copy HTML"sv, [this](auto&) { m_inspector_client->context_menu_copy_dom_node(); });
m_screenshot_node_action = GUI::Action::create("Take node &screenshot"sv, [this](auto&) { m_inspector_client->context_menu_screenshot_dom_node(); });
m_delete_node_action = GUI::Action::create("&Delete node"sv, [this](auto&) { m_inspector_client->context_menu_remove_dom_node(); });
m_add_attribute_action = GUI::Action::create("&Add attribute"sv, [this](auto&) { m_inspector_client->context_menu_add_dom_node_attribute(); });
m_remove_attribute_action = GUI::Action::create("&Remove attribute"sv, [this](auto&) { m_inspector_client->context_menu_remove_dom_node_attribute(); });
@ -49,6 +50,7 @@ InspectorWidget::InspectorWidget(WebView::OutOfProcessWebView& content_view)
m_dom_node_tag_context_menu->add_action(*m_delete_node_action);
m_dom_node_tag_context_menu->add_separator();
m_dom_node_tag_context_menu->add_action(*m_copy_node_action);
m_dom_node_tag_context_menu->add_action(*m_screenshot_node_action);
m_dom_node_attribute_context_menu = GUI::Menu::construct();
m_dom_node_attribute_context_menu->add_action(*m_edit_node_action);
@ -59,6 +61,7 @@ InspectorWidget::InspectorWidget(WebView::OutOfProcessWebView& content_view)
m_dom_node_attribute_context_menu->add_action(*m_delete_node_action);
m_dom_node_attribute_context_menu->add_separator();
m_dom_node_attribute_context_menu->add_action(*m_copy_node_action);
m_dom_node_attribute_context_menu->add_action(*m_screenshot_node_action);
m_inspector_client->on_requested_dom_node_text_context_menu = [this](auto position) {
m_edit_node_action->set_text("&Edit text");

View file

@ -41,6 +41,7 @@ private:
RefPtr<GUI::Action> m_edit_node_action;
RefPtr<GUI::Action> m_copy_node_action;
RefPtr<GUI::Action> m_screenshot_node_action;
RefPtr<GUI::Action> m_delete_node_action;
RefPtr<GUI::Action> m_add_attribute_action;
RefPtr<GUI::Action> m_remove_attribute_action;

View file

@ -245,6 +245,15 @@ void InspectorClient::context_menu_copy_dom_node()
m_context_menu_data.clear();
}
void InspectorClient::context_menu_screenshot_dom_node()
{
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();
m_context_menu_data.clear();
}
void InspectorClient::context_menu_remove_dom_node()
{
VERIFY(m_context_menu_data.has_value());

View file

@ -28,6 +28,7 @@ public:
void context_menu_edit_dom_node();
void context_menu_copy_dom_node();
void context_menu_screenshot_dom_node();
void context_menu_remove_dom_node();
void context_menu_add_dom_node_attribute();
void context_menu_remove_dom_node_attribute();