From 07b6edf9df1f5ababe53cf8ec35b9699fbee1634 Mon Sep 17 00:00:00 2001 From: linonetwo Date: Fri, 23 Jun 2023 21:50:31 +0800 Subject: [PATCH] fix: no context menu on new window --- src/services/menu/index.ts | 4 ++++ src/services/view/handleNewWindow.ts | 16 +++++++++------- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/services/menu/index.ts b/src/services/menu/index.ts index 39a22b8c..16b97645 100644 --- a/src/services/menu/index.ts +++ b/src/services/menu/index.ts @@ -264,8 +264,12 @@ export class MenuService implements IMenuService { /** Register `on('context-menu', openContextMenuForWindow)` for a window, return an unregister function */ public async initContextMenuForWindowWebContents(webContents: WebContents): Promise<() => void> { const openContextMenuForWindow = async (event: Electron.Event, parameters: ContextMenuParams): Promise => { + // DEBUG: console event + console.log(`event`, event); await this.buildContextMenuAndPopup([], parameters, webContents); }; + // DEBUG: console webContents + console.log(`webContents`, webContents); webContents.on('context-menu', openContextMenuForWindow); return () => { diff --git a/src/services/view/handleNewWindow.ts b/src/services/view/handleNewWindow.ts index e0a251d4..a0e8e88a 100644 --- a/src/services/view/handleNewWindow.ts +++ b/src/services/view/handleNewWindow.ts @@ -57,9 +57,9 @@ export function handleNewWindow( action: 'deny', }; } - logger.debug('handleNewWindow()', { newWindowContext }); - const { view, workspace, sharedWebPreferences, meta } = newWindowContext; - const currentUrl = view.webContents.getURL(); + logger.debug('Allowing creating new window', { newWindowContext, function: 'handleNewWindow' }); + const { view: parentWindowView, workspace, sharedWebPreferences, meta } = newWindowContext; + const currentUrl = parentWindowView.webContents.getURL(); /** Conditions are listed by order of priority if global.forceNewWindow = true or regular new-window event @@ -115,7 +115,7 @@ export function handleNewWindow( } parentWebContents.once('did-create-window', (childWindow) => { childWindow.setMenuBarVisibility(false); - childWindow.webContents.setWindowOpenHandler((details: Electron.HandlerDetails) => handleNewWindow(details, newWindowContext, parentWebContents)); + childWindow.webContents.setWindowOpenHandler((details: Electron.HandlerDetails) => handleNewWindow(details, newWindowContext, childWindow.webContents)); childWindow.webContents.once('will-navigate', async (_event, url) => { // if the window is used for the current app, then use default behavior const appUrl = (await workspaceService.get(workspace.id))?.homeUrl; @@ -133,9 +133,11 @@ export function handleNewWindow( }); windowWithBrowserViewState.manage(childWindow); const menuService = container.get(serviceIdentifier.MenuService); - void menuService.initContextMenuForWindowWebContents(view.webContents).then((unregisterContextMenu) => { - childWindow.webContents.on('destroyed', () => { - unregisterContextMenu(); + childWindow.webContents.once('dom-ready', async () => { + await menuService.initContextMenuForWindowWebContents(childWindow.webContents).then((unregisterContextMenu) => { + childWindow.webContents.on('destroyed', () => { + unregisterContextMenu(); + }); }); }); });