mirror of
https://github.com/fergalmoran/ladybird.git
synced 2025-12-22 09:19:03 +00:00
UI: Prevent crash when right clicking on an unloaded image
This commit is contained in:
@@ -33,6 +33,7 @@ static constexpr NSInteger CONTEXT_MENU_CONTROLS_TAG = 3;
|
||||
static constexpr NSInteger CONTEXT_MENU_LOOP_TAG = 4;
|
||||
static constexpr NSInteger CONTEXT_MENU_SEARCH_SELECTED_TEXT_TAG = 5;
|
||||
static constexpr NSInteger CONTEXT_MENU_COPY_LINK_TAG = 6;
|
||||
static constexpr NSInteger CONTEXT_MENU_COPY_IMAGE_TAG = 7;
|
||||
|
||||
// Calls to [NSCursor hide] and [NSCursor unhide] must be balanced. We use this struct to ensure
|
||||
// we only call [NSCursor hide] once and to ensure that we do call [NSCursor unhide].
|
||||
@@ -54,7 +55,7 @@ struct HideCursor {
|
||||
OwnPtr<Ladybird::WebViewBridge> m_web_view_bridge;
|
||||
|
||||
URL::URL m_context_menu_url;
|
||||
Gfx::ShareableBitmap m_context_menu_bitmap;
|
||||
Optional<Gfx::ShareableBitmap> m_context_menu_bitmap;
|
||||
Optional<String> m_context_menu_search_text;
|
||||
|
||||
Optional<HideCursor> m_hidden_cursor;
|
||||
@@ -669,6 +670,9 @@ static void copy_data_to_clipboard(StringView data, NSPasteboardType pasteboard_
|
||||
TemporaryChange change_url { m_context_menu_url, url };
|
||||
TemporaryChange change_bitmap { m_context_menu_bitmap, bitmap };
|
||||
|
||||
auto* copy_image_menu_item = [self.image_context_menu itemWithTag:CONTEXT_MENU_COPY_IMAGE_TAG];
|
||||
[copy_image_menu_item setEnabled:bitmap.has_value()];
|
||||
|
||||
auto* event = Ladybird::create_context_menu_mouse_event(self, position);
|
||||
[NSMenu popUpContextMenu:self.image_context_menu withEvent:event forView:self];
|
||||
};
|
||||
@@ -1186,7 +1190,10 @@ static void copy_data_to_clipboard(StringView data, NSPasteboardType pasteboard_
|
||||
|
||||
- (void)copyImage:(id)sender
|
||||
{
|
||||
auto* bitmap = m_context_menu_bitmap.bitmap();
|
||||
if (!m_context_menu_bitmap.has_value()) {
|
||||
return;
|
||||
}
|
||||
auto* bitmap = m_context_menu_bitmap.value().bitmap();
|
||||
if (bitmap == nullptr) {
|
||||
return;
|
||||
}
|
||||
@@ -1310,6 +1317,7 @@ static void copy_data_to_clipboard(StringView data, NSPasteboardType pasteboard_
|
||||
{
|
||||
if (!_image_context_menu) {
|
||||
_image_context_menu = [[NSMenu alloc] initWithTitle:@"Image Context Menu"];
|
||||
[_image_context_menu setAutoenablesItems:NO];
|
||||
|
||||
[_image_context_menu addItem:[[NSMenuItem alloc] initWithTitle:@"Open Image"
|
||||
action:@selector(openLink:)
|
||||
@@ -1319,9 +1327,12 @@ static void copy_data_to_clipboard(StringView data, NSPasteboardType pasteboard_
|
||||
keyEquivalent:@""]];
|
||||
[_image_context_menu addItem:[NSMenuItem separatorItem]];
|
||||
|
||||
[_image_context_menu addItem:[[NSMenuItem alloc] initWithTitle:@"Copy Image"
|
||||
auto* copy_image_menu_item = [[NSMenuItem alloc] initWithTitle:@"Copy Image"
|
||||
action:@selector(copyImage:)
|
||||
keyEquivalent:@""]];
|
||||
keyEquivalent:@""];
|
||||
[copy_image_menu_item setTag:CONTEXT_MENU_COPY_IMAGE_TAG];
|
||||
[_image_context_menu addItem:copy_image_menu_item];
|
||||
|
||||
[_image_context_menu addItem:[[NSMenuItem alloc] initWithTitle:@"Copy Image URL"
|
||||
action:@selector(copyLink:)
|
||||
keyEquivalent:@""]];
|
||||
|
||||
@@ -592,10 +592,13 @@ Tab::Tab(BrowserWindow* window, RefPtr<WebView::WebContentClient> parent_client,
|
||||
open_link_in_new_tab(m_image_context_menu_url);
|
||||
});
|
||||
|
||||
auto* copy_image_action = new QAction("&Copy Image", this);
|
||||
copy_image_action->setIcon(load_icon_from_uri("resource://icons/16x16/edit-copy.png"sv));
|
||||
QObject::connect(copy_image_action, &QAction::triggered, this, [this]() {
|
||||
auto* bitmap = m_image_context_menu_bitmap.bitmap();
|
||||
m_image_context_menu_copy_image_action = new QAction("&Copy Image", this);
|
||||
m_image_context_menu_copy_image_action->setIcon(load_icon_from_uri("resource://icons/16x16/edit-copy.png"sv));
|
||||
QObject::connect(m_image_context_menu_copy_image_action, &QAction::triggered, this, [this]() {
|
||||
if (!m_image_context_menu_bitmap.has_value())
|
||||
return;
|
||||
|
||||
auto* bitmap = m_image_context_menu_bitmap.value().bitmap();
|
||||
if (bitmap == nullptr)
|
||||
return;
|
||||
|
||||
@@ -621,14 +624,15 @@ Tab::Tab(BrowserWindow* window, RefPtr<WebView::WebContentClient> parent_client,
|
||||
m_image_context_menu->addAction(open_image_action);
|
||||
m_image_context_menu->addAction(open_image_in_new_tab_action);
|
||||
m_image_context_menu->addSeparator();
|
||||
m_image_context_menu->addAction(copy_image_action);
|
||||
m_image_context_menu->addAction(m_image_context_menu_copy_image_action);
|
||||
m_image_context_menu->addAction(copy_image_url_action);
|
||||
m_image_context_menu->addSeparator();
|
||||
m_image_context_menu->addAction(&m_window->inspect_dom_node_action());
|
||||
|
||||
view().on_image_context_menu_request = [this](auto& image_url, Gfx::IntPoint content_position, Gfx::ShareableBitmap const& shareable_bitmap) {
|
||||
view().on_image_context_menu_request = [this](auto& image_url, Gfx::IntPoint content_position, Optional<Gfx::ShareableBitmap> const& shareable_bitmap) {
|
||||
m_image_context_menu_url = image_url;
|
||||
m_image_context_menu_bitmap = shareable_bitmap;
|
||||
m_image_context_menu_copy_image_action->setEnabled(shareable_bitmap.has_value());
|
||||
|
||||
m_image_context_menu->exec(view().map_point_to_global_position(content_position));
|
||||
};
|
||||
|
||||
@@ -151,7 +151,8 @@ private:
|
||||
URL::URL m_link_context_menu_url;
|
||||
|
||||
QMenu* m_image_context_menu { nullptr };
|
||||
Gfx::ShareableBitmap m_image_context_menu_bitmap;
|
||||
QAction* m_image_context_menu_copy_image_action { nullptr };
|
||||
Optional<Gfx::ShareableBitmap> m_image_context_menu_bitmap;
|
||||
URL::URL m_image_context_menu_url;
|
||||
|
||||
QMenu* m_audio_context_menu { nullptr };
|
||||
|
||||
Reference in New Issue
Block a user