Skip to content

Commit 44f9637

Browse files
committed
Browser: Add a special context menu for images
Now, right-clicking on an image allows you to open that image in this tab or a new tab. You can also copy the image URL, and even copy the image itself to the clipboard! :^) Copying to the clipboard will not work in a multi-process context yet, since we need to send the image bitmap across the IPC boundary and this patch does not do that.
1 parent 91b49dd commit 44f9637

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

Applications/Browser/Tab.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,32 @@ Tab::Tab(Type type)
187187
m_link_context_menu->popup(screen_position, m_link_context_menu_default_action);
188188
};
189189

190+
m_image_context_menu = GUI::Menu::construct();
191+
m_image_context_menu->add_action(GUI::Action::create("Open image", [this](auto&) {
192+
hooks().on_link_click(m_image_context_menu_url, "", 0);
193+
}));
194+
m_image_context_menu->add_action(GUI::Action::create("Open image in new tab", [this](auto&) {
195+
hooks().on_link_click(m_image_context_menu_url, "_blank", 0);
196+
}));
197+
m_image_context_menu->add_separator();
198+
m_image_context_menu->add_action(GUI::Action::create("Copy image", [this](auto&) {
199+
if (m_image_context_menu_bitmap.is_valid())
200+
GUI::Clipboard::the().set_bitmap(*m_image_context_menu_bitmap.bitmap());
201+
}));
202+
m_image_context_menu->add_action(GUI::Action::create("Copy image URL", [this](auto&) {
203+
GUI::Clipboard::the().set_plain_text(m_image_context_menu_url.to_string());
204+
}));
205+
m_image_context_menu->add_separator();
206+
m_image_context_menu->add_action(GUI::Action::create("Download", [this](auto&) {
207+
start_download(m_image_context_menu_url);
208+
}));
209+
210+
hooks().on_image_context_menu_request = [this](auto& image_url, auto& screen_position, const Gfx::ShareableBitmap& shareable_bitmap) {
211+
m_image_context_menu_url = image_url;
212+
m_image_context_menu_bitmap = shareable_bitmap;
213+
m_image_context_menu->popup(screen_position);
214+
};
215+
190216
hooks().on_link_middle_click = [this](auto& href, auto&, auto) {
191217
hooks().on_link_click(href, "_blank", 0);
192218
};

Applications/Browser/Tab.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "History.h"
3030
#include <AK/URL.h>
3131
#include <LibGUI/Widget.h>
32+
#include <LibGfx/ShareableBitmap.h>
3233
#include <LibHTTP/HttpJob.h>
3334
#include <LibWeb/Forward.h>
3435

@@ -104,6 +105,10 @@ class Tab final : public GUI::Widget {
104105
RefPtr<GUI::Action> m_link_context_menu_default_action;
105106
URL m_link_context_menu_url;
106107

108+
RefPtr<GUI::Menu> m_image_context_menu;
109+
Gfx::ShareableBitmap m_image_context_menu_bitmap;
110+
URL m_image_context_menu_url;
111+
107112
RefPtr<GUI::Menu> m_tab_context_menu;
108113
RefPtr<GUI::Menu> m_page_context_menu;
109114

0 commit comments

Comments
 (0)