mirror of
https://github.com/tiddly-gittly/TidGi-Desktop.git
synced 2026-01-09 10:41:49 -08:00
refactor: modenlize simple menu creation
This commit is contained in:
parent
65e7d223d8
commit
4a3ea0df04
2 changed files with 32 additions and 12 deletions
|
|
@ -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<void> {
|
||||
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();
|
||||
},
|
||||
},
|
||||
],
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<Electron.BrowserWindow>;
|
||||
closeCurrentWindow: () => void;
|
||||
getBaseName: (pathString: string) => Promise<string>;
|
||||
getDirectoryName: (pathString: string) => Promise<string>;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue