fix: 修复 mini-app 中无法使用 context-menu 的问题

This commit is contained in:
gyuannn 2025-01-28 17:20:53 +08:00 committed by 亢奋猫
parent 790caae2ab
commit 250aa7154a

View File

@ -17,6 +17,7 @@ export class WindowService {
private wasFullScreen: boolean = false private wasFullScreen: boolean = false
private selectionMenuWindow: BrowserWindow | null = null private selectionMenuWindow: BrowserWindow | null = null
private lastSelectedText: string = '' private lastSelectedText: string = ''
private contextMenu: Menu | null = null
public static getInstance(): WindowService { public static getInstance(): WindowService {
if (!WindowService.instance) { if (!WindowService.instance) {
@ -110,15 +111,25 @@ export class WindowService {
} }
private setupContextMenu(mainWindow: BrowserWindow) { private setupContextMenu(mainWindow: BrowserWindow) {
mainWindow.webContents.on('context-menu', () => { if (!this.contextMenu) {
const locale = locales[configManager.getLanguage()] const locale = locales[configManager.getLanguage()]
const { common } = locale.translation const { common } = locale.translation
const menu = new Menu() this.contextMenu = new Menu()
menu.append(new MenuItem({ label: common.copy, role: 'copy' })) this.contextMenu.append(new MenuItem({ label: common.copy, role: 'copy' }))
menu.append(new MenuItem({ label: common.paste, role: 'paste' })) this.contextMenu.append(new MenuItem({ label: common.paste, role: 'paste' }))
menu.append(new MenuItem({ label: common.cut, role: 'cut' })) this.contextMenu.append(new MenuItem({ label: common.cut, role: 'cut' }))
menu.popup() }
mainWindow.webContents.on('context-menu', () => {
this.contextMenu?.popup()
})
// Handle webview context menu
mainWindow.webContents.on('did-attach-webview', (_, webContents) => {
webContents.on('context-menu', () => {
this.contextMenu?.popup()
})
}) })
} }