diff --git a/src/preload/common/simple-context-menu.ts b/src/preload/common/simple-context-menu.ts index 2778c0f0..4f0510e8 100644 --- a/src/preload/common/simple-context-menu.ts +++ b/src/preload/common/simple-context-menu.ts @@ -1,13 +1,24 @@ -import { remote } from 'electron'; -import contextMenu from 'electron-context-menu'; +import { WindowNames } from '@services/windows/WindowProperties'; +import { ContextMenuParams, BrowserWindow, WebviewTag, WebContents } from 'electron'; +import contextMenu, { Actions } from 'electron-context-menu'; +import { window } from './services'; // A much simpler version of public/libs/context-menu-builder.js -contextMenu({ - window: remote.getCurrentWindow(), - prepend: (_: any, __: any, browserWindow: any) => [ - { - label: 'Developer Tools', - click: () => browserWindow.webContents.openDevTools(), - }, - ], -}); +// A fallback basic version +export async function initSimpleContextMenu(windowName: WindowNames): Promise { + const currentWindow = await window.get(windowName); + if (currentWindow === undefined) { + throw new Error('currentWindow is undefined when initSimpleContextMenu()'); + } + contextMenu({ + window: currentWindow, + prepend: (_defaultActions: Actions, _parameters: ContextMenuParams, browserWindow: BrowserWindow | WebviewTag | WebContents) => [ + { + label: 'Developer Tools', + click: () => { + if ('openDevTools' in browserWindow) browserWindow.openDevTools(); + }, + }, + ], + }); +} diff --git a/src/preload/index.ts b/src/preload/index.ts index 3ad2376c..05c873cf 100644 --- a/src/preload/index.ts +++ b/src/preload/index.ts @@ -2,7 +2,7 @@ import 'reflect-metadata'; import { contextBridge, ipcRenderer } from 'electron'; import './common/i18n'; -import './common/simple-context-menu'; +import { initSimpleContextMenu } from './common/simple-context-menu'; import './common/authing-postmessage'; import * as service from './common/services'; import { MetaDataChannel, ViewChannel, ContextChannel } from '@/constants/channels'; @@ -11,6 +11,7 @@ import { WindowNames, WindowMeta, IPossibleWindowMeta } from '@services/windows/ const extraMetaJSONString = process.argv.pop() as string; const windowName = process.argv.pop() as WindowNames; const extraMeta = JSON.parse(extraMetaJSONString) as WindowMeta[WindowNames]; +void initSimpleContextMenu(windowName); const browserViewMetaData = { windowName, ...extraMeta }; contextBridge.exposeInMainWorld('meta', browserViewMetaData); @@ -27,6 +28,13 @@ declare global { } } contextBridge.exposeInMainWorld('remote', { + getCurrentWindow: async () => { + const currentWindow = await service.window.get(windowName); + if (currentWindow === undefined) { + throw new Error(`currentWindow is undefined when getCurrentWindow() in preload script with windowName: ${windowName}`); + } + return currentWindow; + }, closeCurrentWindow: async () => { await service.window.close(windowName); }, @@ -46,6 +54,7 @@ contextBridge.exposeInMainWorld('remote', { declare global { interface Window { remote: { + getCurrentWindow: () => Promise; closeCurrentWindow: () => void; getBaseName: (pathString: string) => Promise; getDirectoryName: (pathString: string) => Promise;