From 3c570c61ff9fbec63e1c5647de285b013be1e1bf Mon Sep 17 00:00:00 2001 From: Dean Chen <51218137+locker95@users.noreply.github.com> Date: Thu, 16 Jul 2026 22:25:49 +0500 Subject: [PATCH] fix(desktop): preserve copy image for large remote media --- .../electron/image-context-menu.test.ts | 64 +++++++++++++++++++ apps/desktop/electron/image-context-menu.ts | 40 ++++++++++++ apps/desktop/electron/main.ts | 51 +++------------ 3 files changed, 114 insertions(+), 41 deletions(-) create mode 100644 apps/desktop/electron/image-context-menu.test.ts create mode 100644 apps/desktop/electron/image-context-menu.ts diff --git a/apps/desktop/electron/image-context-menu.test.ts b/apps/desktop/electron/image-context-menu.test.ts new file mode 100644 index 0000000000..ad82c5a125 --- /dev/null +++ b/apps/desktop/electron/image-context-menu.test.ts @@ -0,0 +1,64 @@ +import assert from 'node:assert/strict' + +import { test } from 'vitest' + +import { imageContextMenuItems } from './image-context-menu' + +function createActions() { + const calls = { + copyImageAt: [], + openImage: [], + copyImageAddress: [], + saveImage: [] + } + + return { + calls, + actions: { + copyImageAt: (x, y) => calls.copyImageAt.push([x, y]), + openImage: url => calls.openImage.push(url), + copyImageAddress: url => calls.copyImageAddress.push(url), + saveImage: url => calls.saveImage.push(url) + } + } +} + +test('keeps Copy Image available when Chromium omits a large image srcURL', () => { + const { actions, calls } = createActions() + const items = imageContextMenuItems({ mediaType: 'image', srcURL: '', x: 100, y: 120 }, actions) + + assert.deepEqual( + items.map(item => item.label), + ['Copy Image'] + ) + + items[0].click() + assert.deepEqual(calls.copyImageAt, [[100, 120]]) +}) + +test('keeps URL-dependent image actions when srcURL is available', () => { + const { actions, calls } = createActions() + const url = 'https://example.com/image.png' + const items = imageContextMenuItems({ mediaType: 'image', srcURL: url, x: 5, y: 8 }, actions) + + assert.deepEqual( + items.map(item => item.label), + ['Open Image', 'Copy Image', 'Copy Image Address', 'Save Image As...'] + ) + + items[0].click() + items[1].click() + items[2].click() + items[3].click() + + assert.deepEqual(calls.openImage, [url]) + assert.deepEqual(calls.copyImageAt, [[5, 8]]) + assert.deepEqual(calls.copyImageAddress, [url]) + assert.deepEqual(calls.saveImage, [url]) +}) + +test('does not add image actions for a non-image target', () => { + const { actions } = createActions() + + assert.deepEqual(imageContextMenuItems({ mediaType: 'none', srcURL: '', x: 0, y: 0 }, actions), []) +}) diff --git a/apps/desktop/electron/image-context-menu.ts b/apps/desktop/electron/image-context-menu.ts new file mode 100644 index 0000000000..3d7e20b341 --- /dev/null +++ b/apps/desktop/electron/image-context-menu.ts @@ -0,0 +1,40 @@ +export function imageContextMenuItems(params, actions) { + if (params.mediaType !== 'image') { + return [] + } + + const items = [] + const srcURL = params.srcURL || '' + + if (srcURL) { + items.push({ + label: 'Open Image', + click: () => { + if (!srcURL.startsWith('data:')) { + actions.openImage(srcURL) + } + }, + enabled: !srcURL.startsWith('data:') + }) + } + + items.push({ + label: 'Copy Image', + click: () => actions.copyImageAt(params.x, params.y) + }) + + if (srcURL) { + items.push( + { + label: 'Copy Image Address', + click: () => actions.copyImageAddress(srcURL) + }, + { + label: 'Save Image As...', + click: () => actions.saveImage(srcURL) + } + ) + } + + return items +} diff --git a/apps/desktop/electron/main.ts b/apps/desktop/electron/main.ts index e1f5676a46..212641d92d 100644 --- a/apps/desktop/electron/main.ts +++ b/apps/desktop/electron/main.ts @@ -17,7 +17,6 @@ import { globalShortcut, ipcMain, Menu, - nativeImage, nativeTheme, Notification, powerMonitor, @@ -169,6 +168,7 @@ import { cursorPointInWindow } from './hud-cursor' import { snapHudBounds } from './hud-snap' import { createHudSnapShortcut } from './hud-snap-shortcut' import { buildHudWindowUrl } from './hud-url' +import { imageContextMenuItems } from './image-context-menu' import { createLinkTitleWindow, guardLinkTitleSession, readLinkTitleWindowTitle } from './link-title-window' import { ensureMainWindow } from './main-window-lifecycle' import { @@ -5130,17 +5130,6 @@ async function resourceBufferFromUrl(rawUrl) { }) } -async function copyImageFromUrl(rawUrl) { - const { buffer } = (await resourceBufferFromUrl(rawUrl)) as any - const image = nativeImage.createFromBuffer(buffer) - - if (image.isEmpty()) { - throw new Error('Could not read image') - } - - clipboard.writeImage(image) -} - async function saveImageFromUrl(rawUrl) { const { buffer, mimeType } = (await resourceBufferFromUrl(rawUrl)) as any const extension = extensionForMimeType(mimeType) || '.png' @@ -5992,39 +5981,19 @@ function installContextMenu(window) { window.webContents.on('context-menu', (_event, params) => { const template = [] const hasSelection = Boolean(params.selectionText?.trim()) - const hasImage = params.mediaType === 'image' && Boolean(params.srcURL) const hasLink = Boolean(params.linkURL) const isEditable = Boolean(params.isEditable) - if (hasImage) { - template.push( - { - label: 'Open Image', - click: () => { - if (params.srcURL && !params.srcURL.startsWith('data:')) { - openExternalUrl(params.srcURL) - } - }, - enabled: !params.srcURL.startsWith('data:') - }, - { - label: 'Copy Image', - click: () => { - void copyImageFromUrl(params.srcURL).catch(error => rememberLog(`Copy image failed: ${error.message}`)) - } - }, - { - label: 'Copy Image Address', - click: () => clipboard.writeText(params.srcURL) - }, - { - label: 'Save Image As...', - click: () => { - void saveImageFromUrl(params.srcURL).catch(error => rememberLog(`Save image failed: ${error.message}`)) - } + template.push( + ...imageContextMenuItems(params, { + copyImageAt: (x, y) => window.webContents.copyImageAt(x, y), + openImage: openExternalUrl, + copyImageAddress: url => clipboard.writeText(url), + saveImage: url => { + void saveImageFromUrl(url).catch(error => rememberLog(`Save image failed: ${error.message}`)) } - ) - } + }) + ) if (hasLink) { if (template.length) {