summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2020-10-02 19:01:51 +0200
committerAndreas Kling <kling@serenityos.org>2020-10-02 19:02:47 +0200
commit91b49dd9d017acb1e979d28a0e2bc212cb297bba (patch)
tree77898e0d2bd9e16c526f1ff69a0e3c84398ce79c
parentd00bfd0eaadb81c6e54e9e601d6ca8861c5acbcd (diff)
downloadserenity-91b49dd9d017acb1e979d28a0e2bc212cb297bba.zip
LibWeb: Add a PageClient callback for image context menu requests
When the user right-clicks on an image, you might want to show a special context menu, separate from the regular link context menu. This patch only implements enough of the functionality to get this working in a single-process context.
-rw-r--r--Libraries/LibWeb/InProcessWebView.cpp11
-rw-r--r--Libraries/LibWeb/InProcessWebView.h1
-rw-r--r--Libraries/LibWeb/Page/EventHandler.cpp8
-rw-r--r--Libraries/LibWeb/Page/Page.h1
-rw-r--r--Libraries/LibWeb/WebViewHooks.h1
5 files changed, 22 insertions, 0 deletions
diff --git a/Libraries/LibWeb/InProcessWebView.cpp b/Libraries/LibWeb/InProcessWebView.cpp
index e0095fb173..e13c902474 100644
--- a/Libraries/LibWeb/InProcessWebView.cpp
+++ b/Libraries/LibWeb/InProcessWebView.cpp
@@ -36,6 +36,7 @@
#include <LibGUI/ScrollBar.h>
#include <LibGUI/Window.h>
#include <LibGfx/ImageDecoder.h>
+#include <LibGfx/ShareableBitmap.h>
#include <LibJS/Runtime/Value.h>
#include <LibWeb/DOM/Element.h>
#include <LibWeb/DOM/ElementFactory.h>
@@ -171,6 +172,16 @@ void InProcessWebView::page_did_request_link_context_menu(const Gfx::IntPoint& c
on_link_context_menu_request(url, screen_relative_rect().location().translated(to_widget_position(content_position)));
}
+void InProcessWebView::page_did_request_image_context_menu(const Gfx::IntPoint& content_position, const URL& url, [[maybe_unused]] const String& target, [[maybe_unused]] unsigned modifiers, const Gfx::Bitmap* bitmap)
+{
+ if (!on_image_context_menu_request)
+ return;
+ Gfx::ShareableBitmap shareable_bitmap;
+ if (bitmap)
+ shareable_bitmap = Gfx::ShareableBitmap(*bitmap);
+ on_image_context_menu_request(url, screen_relative_rect().location().translated(to_widget_position(content_position)), shareable_bitmap);
+}
+
void InProcessWebView::page_did_click_link(const URL& url, const String& target, unsigned modifiers)
{
if (on_link_click)
diff --git a/Libraries/LibWeb/InProcessWebView.h b/Libraries/LibWeb/InProcessWebView.h
index c6931936dc..6122e79f6a 100644
--- a/Libraries/LibWeb/InProcessWebView.h
+++ b/Libraries/LibWeb/InProcessWebView.h
@@ -94,6 +94,7 @@ private:
virtual void page_did_request_cursor_change(Gfx::StandardCursor) override;
virtual void page_did_request_context_menu(const Gfx::IntPoint&) override;
virtual void page_did_request_link_context_menu(const Gfx::IntPoint&, const URL&, const String& target, unsigned modifiers) override;
+ virtual void page_did_request_image_context_menu(const Gfx::IntPoint&, const URL&, const String& target, unsigned modifiers, const Gfx::Bitmap*) override;
virtual void page_did_click_link(const URL&, const String& target, unsigned modifiers) override;
virtual void page_did_middle_click_link(const URL&, const String& target, unsigned modifiers) override;
virtual void page_did_enter_tooltip_area(const Gfx::IntPoint&, const String&) override;
diff --git a/Libraries/LibWeb/Page/EventHandler.cpp b/Libraries/LibWeb/Page/EventHandler.cpp
index cc54cda41f..c75d41c0bb 100644
--- a/Libraries/LibWeb/Page/EventHandler.cpp
+++ b/Libraries/LibWeb/Page/EventHandler.cpp
@@ -31,6 +31,7 @@
#include <LibWeb/DOM/Text.h>
#include <LibWeb/HTML/HTMLAnchorElement.h>
#include <LibWeb/HTML/HTMLIFrameElement.h>
+#include <LibWeb/HTML/HTMLImageElement.h>
#include <LibWeb/InProcessWebView.h>
#include <LibWeb/Layout/LayoutDocument.h>
#include <LibWeb/Page/EventHandler.h>
@@ -154,6 +155,13 @@ bool EventHandler::handle_mousedown(const Gfx::IntPoint& position, unsigned butt
if (!layout_root())
return true;
+ if (button == GUI::MouseButton::Right && is<HTML::HTMLImageElement>(*node)) {
+ auto& image_element = downcast<HTML::HTMLImageElement>(*node);
+ auto image_url = image_element.document().complete_url(image_element.src());
+ page_client.page_did_request_image_context_menu(m_frame.to_main_frame_position(position), image_url, "", modifiers, image_element.bitmap());
+ return true;
+ }
+
if (RefPtr<HTML::HTMLAnchorElement> link = node->enclosing_link_element()) {
auto href = link->href();
auto url = document->complete_url(href);
diff --git a/Libraries/LibWeb/Page/Page.h b/Libraries/LibWeb/Page/Page.h
index 1805c80d79..a9073b016b 100644
--- a/Libraries/LibWeb/Page/Page.h
+++ b/Libraries/LibWeb/Page/Page.h
@@ -87,6 +87,7 @@ public:
virtual void page_did_request_cursor_change(Gfx::StandardCursor) { }
virtual void page_did_request_context_menu(const Gfx::IntPoint&) { }
virtual void page_did_request_link_context_menu(const Gfx::IntPoint&, const URL&, [[maybe_unused]] const String& target, [[maybe_unused]] unsigned modifiers) { }
+ virtual void page_did_request_image_context_menu(const Gfx::IntPoint&, const URL&, [[maybe_unused]] const String& target, [[maybe_unused]] unsigned modifiers, const Gfx::Bitmap*) { }
virtual void page_did_click_link(const URL&, [[maybe_unused]] const String& target, [[maybe_unused]] unsigned modifiers) { }
virtual void page_did_middle_click_link(const URL&, [[maybe_unused]] const String& target, [[maybe_unused]] unsigned modifiers) { }
virtual void page_did_enter_tooltip_area(const Gfx::IntPoint&, const String&) { }
diff --git a/Libraries/LibWeb/WebViewHooks.h b/Libraries/LibWeb/WebViewHooks.h
index 5f261b4b37..d93655bcea 100644
--- a/Libraries/LibWeb/WebViewHooks.h
+++ b/Libraries/LibWeb/WebViewHooks.h
@@ -37,6 +37,7 @@ public:
Function<void(const Gfx::IntPoint& screen_position)> on_context_menu_request;
Function<void(const URL&, const String& target, unsigned modifiers)> on_link_click;
Function<void(const URL&, const Gfx::IntPoint& screen_position)> on_link_context_menu_request;
+ Function<void(const URL&, const Gfx::IntPoint& screen_position, const Gfx::ShareableBitmap&)> on_image_context_menu_request;
Function<void(const URL&, const String& target, unsigned modifiers)> on_link_middle_click;
Function<void(const URL&)> on_link_hover;
Function<void(const String&)> on_title_change;