Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

🐛 fix(desktop): add Copy Image to the image context menu (#22067)

Rdmclin24441d89c9ea0
diff --git a/apps/desktop/src/main/core/browser/Browser.ts b/apps/desktop/src/main/core/browser/Browser.ts
index 88e5646efd5..ee37a4c0a81 100644
--- a/apps/desktop/src/main/core/browser/Browser.ts
+++ b/apps/desktop/src/main/core/browser/Browser.ts
@@ -492,18 +492,24 @@ export default class Browser {
logger.debug(`[${this.identifier}] Setting up context menu.`);
browserWindow.webContents.on('context-menu', (_event, params) => {
- const { x, y, selectionText, linkURL, srcURL, mediaType, isEditable } = params;
+ const { x, y, selectionText, linkURL, srcURL, mediaType, isEditable, hasImageContents } =
+ params;
// Use the platform menu system with full context data
- this.app.menuManager.showContextMenu('default', {
- isEditable,
- linkURL: linkURL || undefined,
- mediaType: mediaType as any,
- selectionText: selectionText || undefined,
- srcURL: srcURL || undefined,
- x,
- y,
- });
+ this.app.menuManager.showContextMenu(
+ 'default',
+ {
+ hasImageContents,
+ isEditable,
+ linkURL: linkURL || undefined,
+ mediaType: mediaType as any,
+ selectionText: selectionText || undefined,
+ srcURL: srcURL || undefined,
+ x,
+ y,
+ },
+ browserWindow,
+ );
});
}
diff --git a/apps/desktop/src/main/core/ui/MenuManager.ts b/apps/desktop/src/main/core/ui/MenuManager.ts
index a27b74e8e84..3607f7b695e 100644
--- a/apps/desktop/src/main/core/ui/MenuManager.ts
+++ b/apps/desktop/src/main/core/ui/MenuManager.ts
@@ -38,10 +38,12 @@ export class MenuManager {
/**
* Build and show context menu
*/
- showContextMenu(type: string, data?: any) {
+ showContextMenu(type: string, data?: any, window?: BrowserWindow) {
logger.debug(`Showing context menu of type: ${type}`);
const menu = this.platformImpl.buildContextMenu(type, data);
- menu.popup(); // popup must be called in main process
+ // Bind to the originating window so item clicks (e.g. Copy Image at x/y) act on it,
+ // not on whichever window happens to be focused. Must be called in main process.
+ menu.popup(window ? { window } : undefined);
return { success: true };
}
diff --git a/apps/desktop/src/main/core/ui/__tests__/MenuManager.test.ts b/apps/desktop/src/main/core/ui/__tests__/MenuManager.test.ts
index ab3e6e096d4..ad050289fd6 100644
--- a/apps/desktop/src/main/core/ui/__tests__/MenuManager.test.ts
+++ b/apps/desktop/src/main/core/ui/__tests__/MenuManager.test.ts
@@ -108,6 +108,14 @@ describe('MenuManager', () => {
expect(result).toEqual({ success: true });
});
+ it('binds the popup to the originating window when given', () => {
+ const window = { id: 2 } as any;
+
+ menuManager.showContextMenu('default', { mediaType: 'image' }, window);
+
+ expect(mockMenu.popup).toHaveBeenCalledWith({ window });
+ });
+
it('should build context menu without data', () => {
const type = 'simple-menu';
diff --git a/apps/desktop/src/main/menus/impls/BaseMenuPlatform.ts b/apps/desktop/src/main/menus/impls/BaseMenuPlatform.ts
index 40389a00bae..2804a4eb4b2 100644
--- a/apps/desktop/src/main/menus/impls/BaseMenuPlatform.ts
+++ b/apps/desktop/src/main/menus/impls/BaseMenuPlatform.ts
@@ -5,6 +5,8 @@ import { BrowserWindow } from 'electron';
import type { App } from '@/core/App';
import ZoomService, { type ZoomAction } from '@/services/zoomSrv';
+import type { ContextMenuData } from '../types';
+
// Popup (`/popup/*`) and desktop utility (`/desktop/*`) windows run without the app shell.
const NON_APP_SHELL_PATH = /^\/(?:popup|desktop)(?:\/|$)/;
@@ -59,6 +61,33 @@ export abstract class BaseMenuPlatform {
};
}
+ /**
+ * Context menu > Copy Image: copy the right-clicked image itself to the clipboard.
+ * The standard Copy role only copies a text selection, so it does nothing on an image.
+ * `x`/`y` come from the `context-menu` event of the window the menu popped up over.
+ * Disabled when Electron reports no decoded bitmap (broken or still-loading image),
+ * since `copyImageAt` would silently copy nothing.
+ */
+ protected buildCopyImageMenuItem(
+ label: string,
+ data: ContextMenuData,
+ ): MenuItemConstructorOptions {
+ return {
+ click: (_item, win) => {
+ const target =
+ win && 'webContents' in win
+ ? (win as BrowserWindow)
+ : (BrowserWindow.getFocusedWindow() ??
+ this.app.browserManager.getMainWindow()?.browserWindow);
+ if (!target || target.isDestroyed()) return;
+ if (data.x === undefined || data.y === undefined) return;
+ target.webContents.copyImageAt(data.x, data.y);
+ },
+ enabled: data.hasImageContents !== false,
+ label,
+ };
+ }
+
protected buildDevToolsMenuItem(label: string, accelerator?: string): MenuItemConstructorOptions {
return {
accelerator,
diff --git a/apps/desktop/src/main/menus/impls/linux.test.ts b/apps/desktop/src/main/menus/impls/linux.test.ts
index 75410154b00..bf525ea8980 100644
--- a/apps/desktop/src/main/menus/impls/linux.test.ts
+++ b/apps/desktop/src/main/menus/impls/linux.test.ts
@@ -202,6 +202,42 @@ describe('LinuxMenu', () => {
});
});
+ describe('image context menu', () => {
+ it.each(['default', 'chat'])('copies the right-clicked image itself in the %s menu', (type) => {
+ const imageData = {
+ mediaType: 'image' as const,
+ srcURL: 'https://example.com/a.png',
+ x: 12,
+ y: 34,
+ };
+ linuxMenu.buildContextMenu(type, imageData);
+
+ const template = (Menu.buildFromTemplate as any).mock.calls.at(-1)[0];
+ const copyImage = template.find((item: any) => item.label === 'context.copyImage');
+ expect(copyImage).toBeDefined();
+
+ const copyImageAt = vi.fn();
+ const win = { isDestroyed: () => false, webContents: { copyImageAt } };
+ copyImage.click({}, win);
+
+ expect(copyImageAt).toHaveBeenCalledWith(12, 34);
+ });
+
+ it('disables Copy Image when the image has no decoded contents', () => {
+ linuxMenu.buildContextMenu('default', {
+ hasImageContents: false,
+ mediaType: 'image',
+ srcURL: 'https://example.com/broken.png',
+ x: 12,
+ y: 34,
+ });
+
+ const template = (Menu.buildFromTemplate as any).mock.calls.at(-1)[0];
+ const copyImage = template.find((item: any) => item.label === 'context.copyImage');
+ expect(copyImage.enabled).toBe(false);
+ });
+ });
+
describe('buildTrayMenu', () => {
it('should build tray menu', () => {
const menu = linuxMenu.buildTrayMenu();
diff --git a/apps/desktop/src/main/menus/impls/linux.ts b/apps/desktop/src/main/menus/impls/linux.ts
index a80b9665b6b..80046afdabb 100644
--- a/apps/desktop/src/main/menus/impls/linux.ts
+++ b/apps/desktop/src/main/menus/impls/linux.ts
@@ -361,6 +361,7 @@ export class LinuxMenu extends BaseMenuPlatform implements IMenuPlatform {
},
label: t('context.saveImage'),
});
+ template.push(this.buildCopyImageMenuItem(t('context.copyImage'), data!));
template.push({
click: () => {
clipboard.writeText(data!.srcURL!);
@@ -436,6 +437,7 @@ export class LinuxMenu extends BaseMenuPlatform implements IMenuPlatform {
},
label: t('context.saveImage'),
});
+ template.push(this.buildCopyImageMenuItem(t('context.copyImage'), data!));
template.push({
click: () => {
clipboard.writeText(data!.srcURL!);
diff --git a/apps/desktop/src/main/menus/impls/macOS.test.ts b/apps/desktop/src/main/menus/impls/macOS.test.ts
index 6a4a1a457e6..771c3002a4c 100644
--- a/apps/desktop/src/main/menus/impls/macOS.test.ts
+++ b/apps/desktop/src/main/menus/impls/macOS.test.ts
@@ -172,6 +172,42 @@ describe('MacOSMenu', () => {
});
});
+ describe('image context menu', () => {
+ it.each(['default', 'chat'])('copies the right-clicked image itself in the %s menu', (type) => {
+ const imageData = {
+ mediaType: 'image' as const,
+ srcURL: 'https://example.com/a.png',
+ x: 12,
+ y: 34,
+ };
+ macOSMenu.buildContextMenu(type, imageData);
+
+ const template = (Menu.buildFromTemplate as any).mock.calls.at(-1)[0];
+ const copyImage = template.find((item: any) => item.label === 'Copy Image');
+ expect(copyImage).toBeDefined();
+
+ const copyImageAt = vi.fn();
+ const win = { isDestroyed: () => false, webContents: { copyImageAt } };
+ copyImage.click({}, win);
+
+ expect(copyImageAt).toHaveBeenCalledWith(12, 34);
+ });
+
+ it('disables Copy Image when the image has no decoded contents', () => {
+ macOSMenu.buildContextMenu('default', {
+ hasImageContents: false,
+ mediaType: 'image',
+ srcURL: 'https://example.com/broken.png',
+ x: 12,
+ y: 34,
+ });
+
+ const template = (Menu.buildFromTemplate as any).mock.calls.at(-1)[0];
+ const copyImage = template.find((item: any) => item.label === 'Copy Image');
+ expect(copyImage.enabled).toBe(false);
+ });
+ });
+
describe('buildTrayMenu', () => {
it('should build tray menu', () => {
const menu = macOSMenu.buildTrayMenu();
diff --git a/apps/desktop/src/main/menus/impls/macOS.ts b/apps/desktop/src/main/menus/impls/macOS.ts
index d846f81963c..3fc00c2e40c 100644
--- a/apps/desktop/src/main/menus/impls/macOS.ts
+++ b/apps/desktop/src/main/menus/impls/macOS.ts
@@ -543,6 +543,7 @@ export class MacOSMenu extends BaseMenuPlatform implements IMenuPlatform {
},
label: t('context.saveImage'),
});
+ template.push(this.buildCopyImageMenuItem(t('context.copyImage'), data!));
template.push({
click: () => {
clipboard.writeText(data!.srcURL!);
@@ -629,6 +630,7 @@ export class MacOSMenu extends BaseMenuPlatform implements IMenuPlatform {
},
label: t('context.saveImage'),
});
+ template.push(this.buildCopyImageMenuItem(t('context.copyImage'), data!));
template.push({
click: () => {
clipboard.writeText(data!.srcURL!);
diff --git a/apps/desktop/src/main/menus/impls/windows.test.ts b/apps/desktop/src/main/menus/impls/windows.test.ts
index c135d30c711..21bceeb5b80 100644
--- a/apps/desktop/src/main/menus/impls/windows.test.ts
+++ b/apps/desktop/src/main/menus/impls/windows.test.ts
@@ -182,6 +182,42 @@ describe('WindowsMenu', () => {
});
});
+ describe('image context menu', () => {
+ it.each(['default', 'chat'])('copies the right-clicked image itself in the %s menu', (type) => {
+ const imageData = {
+ mediaType: 'image' as const,
+ srcURL: 'https://example.com/a.png',
+ x: 12,
+ y: 34,
+ };
+ windowsMenu.buildContextMenu(type, imageData);
+
+ const template = (Menu.buildFromTemplate as any).mock.calls.at(-1)[0];
+ const copyImage = template.find((item: any) => item.label === 'context.copyImage');
+ expect(copyImage).toBeDefined();
+
+ const copyImageAt = vi.fn();
+ const win = { isDestroyed: () => false, webContents: { copyImageAt } };
+ copyImage.click({}, win);
+
+ expect(copyImageAt).toHaveBeenCalledWith(12, 34);
+ });
+
+ it('disables Copy Image when the image has no decoded contents', () => {
+ windowsMenu.buildContextMenu('default', {
+ hasImageContents: false,
+ mediaType: 'image',
+ srcURL: 'https://example.com/broken.png',
+ x: 12,
+ y: 34,
+ });
+
+ const template = (Menu.buildFromTemplate as any).mock.calls.at(-1)[0];
+ const copyImage = template.find((item: any) => item.label === 'context.copyImage');
+ expect(copyImage.enabled).toBe(false);
+ });
+ });
+
describe('buildTrayMenu', () => {
it('should build tray menu', () => {
const menu = windowsMenu.buildTrayMenu();
diff --git a/apps/desktop/src/main/menus/impls/windows.ts b/apps/desktop/src/main/menus/impls/windows.ts
index 4e973f6f6c4..fcd8bc2aa88 100644
--- a/apps/desktop/src/main/menus/impls/windows.ts
+++ b/apps/desktop/src/main/menus/impls/windows.ts
@@ -342,6 +342,7 @@ export class WindowsMenu extends BaseMenuPlatform implements IMenuPlatform {
},
label: t('context.saveImage'),
});
+ template.push(this.buildCopyImageMenuItem(t('context.copyImage'), data!));
template.push({
click: () => {
clipboard.writeText(data!.srcURL!);
@@ -417,6 +418,7 @@ export class WindowsMenu extends BaseMenuPlatform implements IMenuPlatform {
},
label: t('context.saveImage'),
});
+ template.push(this.buildCopyImageMenuItem(t('context.copyImage'), data!));
template.push({
click: () => {
clipboard.writeText(data!.srcURL!);
diff --git a/apps/desktop/src/main/menus/types.ts b/apps/desktop/src/main/menus/types.ts
index 6e0c34c243b..0e49a9b7d01 100644
--- a/apps/desktop/src/main/menus/types.ts
+++ b/apps/desktop/src/main/menus/types.ts
@@ -11,6 +11,8 @@ export interface MenuOptions {
* Based on Electron's ContextMenuParams
*/
export interface ContextMenuData {
+ /** Whether the right-clicked element has a decoded image to copy (false for broken/loading images) */
+ hasImageContents?: boolean;
/** Whether the context is editable (input, textarea, contenteditable) */
isEditable?: boolean;
/** URL of the link if right-clicked on a link */