Summary: CHANGELOG: Upgrade electron to 18.2.0. In Electron 18.2.0 we no longer have access to `remote`. Instead, we are recommended to implement IPC communications. We re-implement `remote` methods used before as IPC commands. To support type-safe execution of the commands, we create electron IPC clients on both sides: the main process and renderer process. We also move the main menu creation to the main process and track its usage via sending IPC messages to the renderer process where the logging happens. Reviewed By: mweststrate Differential Revision: D36593625 fbshipit-source-id: 6dcf531461ef2edceb9cac372a650f84f3370953
86 lines
2.2 KiB
TypeScript
86 lines
2.2 KiB
TypeScript
/**
|
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*
|
|
* @format
|
|
*/
|
|
|
|
// eslint-disable-next-line no-restricted-imports
|
|
import type {
|
|
NativeTheme,
|
|
SaveDialogReturnValue,
|
|
SaveDialogOptions,
|
|
OpenDialogOptions,
|
|
OpenDialogReturnValue,
|
|
} from 'electron';
|
|
import {ipcRenderer} from 'electron/renderer';
|
|
|
|
export interface ElectronIpcAsyncCommands {
|
|
getPath: (path: 'app' | 'home' | 'temp' | 'desktop') => Promise<string>;
|
|
getProcess: () => Promise<{execPath: string; pid: number}>;
|
|
relaunch: (options?: {args: string[]}) => Promise<void>;
|
|
showSaveDialog: (
|
|
options: SaveDialogOptions,
|
|
) => Promise<SaveDialogReturnValue>;
|
|
showOpenDialog: (
|
|
options: OpenDialogOptions,
|
|
) => Promise<OpenDialogReturnValue>;
|
|
getNativeTheme: () => Promise<
|
|
Pick<
|
|
NativeTheme,
|
|
| 'inForcedColorsMode'
|
|
| 'shouldUseDarkColors'
|
|
| 'shouldUseHighContrastColors'
|
|
| 'shouldUseInvertedColorScheme'
|
|
| 'themeSource'
|
|
>
|
|
>;
|
|
quit: () => Promise<void>;
|
|
exit: () => Promise<void>;
|
|
getApp: () => Promise<{
|
|
name: string;
|
|
}>;
|
|
}
|
|
|
|
export interface ElectronIpcSyncCommands {
|
|
getCurrentWindowState: () => {
|
|
isFocused: boolean;
|
|
};
|
|
}
|
|
|
|
export interface ElectronIpcEvents {
|
|
menuItemAction: {
|
|
menu: 'view' | 'root';
|
|
label: string;
|
|
action: 'click';
|
|
};
|
|
}
|
|
|
|
export class ElectronIpcClientRenderer {
|
|
on<T extends keyof ElectronIpcEvents>(
|
|
eventName: T,
|
|
cb: (data: ElectronIpcEvents[T]) => void,
|
|
) {
|
|
ipcRenderer.on(eventName, (_event, data) => cb(data));
|
|
}
|
|
|
|
send<T extends keyof ElectronIpcAsyncCommands>(
|
|
eventName: T,
|
|
...args: Parameters<ElectronIpcAsyncCommands[T]>
|
|
): ReturnType<ElectronIpcAsyncCommands[T]> extends Promise<any>
|
|
? ReturnType<ElectronIpcAsyncCommands[T]>
|
|
: never {
|
|
return ipcRenderer.invoke(eventName, ...args) as any;
|
|
}
|
|
|
|
sendSync<T extends keyof ElectronIpcSyncCommands>(
|
|
eventName: T,
|
|
...args: Parameters<ElectronIpcSyncCommands[T]>
|
|
): ReturnType<ElectronIpcSyncCommands[T]> {
|
|
// eslint-disable-next-line node/no-sync
|
|
return ipcRenderer.sendSync(eventName, ...args);
|
|
}
|
|
}
|