fix: restore native text context menu
This commit is contained in:
52
electron/main/context-menu.ts
Normal file
52
electron/main/context-menu.ts
Normal file
@@ -0,0 +1,52 @@
|
||||
import { Menu } from 'electron';
|
||||
import type { BrowserWindow } from 'electron';
|
||||
|
||||
export type TextContextMenuContext = Pick<
|
||||
Electron.ContextMenuParams,
|
||||
'isEditable' | 'selectionText' | 'editFlags'
|
||||
>;
|
||||
|
||||
export function createTextContextMenuTemplate(
|
||||
context: TextContextMenuContext,
|
||||
): Electron.MenuItemConstructorOptions[] | null {
|
||||
if (!context.isEditable && context.selectionText.length === 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return [
|
||||
{
|
||||
label: '剪切',
|
||||
role: 'cut',
|
||||
enabled: context.isEditable && context.editFlags.canCut,
|
||||
},
|
||||
{
|
||||
label: '复制',
|
||||
role: 'copy',
|
||||
enabled: context.editFlags.canCopy,
|
||||
},
|
||||
{
|
||||
label: '粘贴',
|
||||
role: 'paste',
|
||||
enabled: context.isEditable && context.editFlags.canPaste,
|
||||
},
|
||||
{ type: 'separator' },
|
||||
{
|
||||
label: '全选',
|
||||
role: 'selectAll',
|
||||
enabled: context.editFlags.canSelectAll,
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
export function registerTextContextMenu(win: BrowserWindow): void {
|
||||
win.webContents.on('context-menu', (_event, params) => {
|
||||
const template = createTextContextMenuTemplate(params);
|
||||
if (!template) return;
|
||||
|
||||
Menu.buildFromTemplate(template).popup({
|
||||
window: win,
|
||||
frame: params.frame ?? undefined,
|
||||
sourceType: params.menuSourceType,
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -9,6 +9,7 @@ import { join } from 'node:path';
|
||||
import { registerIpcHandlers } from './ipc-handlers';
|
||||
import { createTray } from './tray';
|
||||
import { createMenu } from './menu';
|
||||
import { registerTextContextMenu } from './context-menu';
|
||||
import { registerZoomShortcuts } from './zoom-shortcuts';
|
||||
import { getNativeWindowMaterialOptions } from './window-material';
|
||||
|
||||
@@ -272,6 +273,7 @@ function createWindow(): BrowserWindow {
|
||||
});
|
||||
|
||||
installMediaPermissionHandler(win);
|
||||
registerTextContextMenu(win);
|
||||
registerZoomShortcuts(win);
|
||||
|
||||
// Handle external links — only allow safe protocols to prevent arbitrary
|
||||
|
||||
Reference in New Issue
Block a user