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
96 lines
2.7 KiB
TypeScript
96 lines
2.7 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 electron, {ipcMain, BrowserWindow} from 'electron';
|
|
// eslint-disable-next-line flipper/no-relative-imports-across-packages
|
|
import type {
|
|
ElectronIpcAsyncCommands,
|
|
ElectronIpcEvents,
|
|
ElectronIpcSyncCommands,
|
|
} from '../app/src/electronIpc';
|
|
|
|
export class ElectronIpcClientMain {
|
|
constructor(private readonly window: BrowserWindow) {
|
|
this.handleAll();
|
|
}
|
|
|
|
private handleAll() {
|
|
for (const command of Object.keys(this.commandsAsync)) {
|
|
ipcMain.handle(command, (_event, params) =>
|
|
this.commandsAsync[command as keyof ElectronIpcAsyncCommands](params),
|
|
);
|
|
}
|
|
// eslint-disable-next-line node/no-sync
|
|
for (const command of Object.keys(this.commandsSync)) {
|
|
ipcMain.on(command, (event) => {
|
|
event.returnValue =
|
|
// eslint-disable-next-line node/no-sync
|
|
this.commandsSync[command as keyof ElectronIpcSyncCommands]();
|
|
});
|
|
}
|
|
}
|
|
|
|
send<T extends keyof ElectronIpcEvents>(
|
|
eventName: T,
|
|
data: ElectronIpcEvents[T],
|
|
) {
|
|
this.window.webContents.send(eventName, data);
|
|
}
|
|
|
|
private readonly commandsAsync: ElectronIpcAsyncCommands = {
|
|
exit: async () => electron.app.exit(),
|
|
quit: async () => electron.app.quit(),
|
|
getPath: async (pathName) => {
|
|
switch (pathName) {
|
|
case 'app': {
|
|
return electron.app.getAppPath();
|
|
}
|
|
default: {
|
|
return electron.app.getPath(pathName);
|
|
}
|
|
}
|
|
},
|
|
getProcess: async () => {
|
|
const {pid, execPath} = process;
|
|
return {pid, execPath};
|
|
},
|
|
relaunch: async (options) => electron.app.relaunch(options),
|
|
showSaveDialog: (options) => electron.dialog.showSaveDialog(options),
|
|
showOpenDialog: (options) => electron.dialog.showOpenDialog(options),
|
|
getNativeTheme: async () => {
|
|
const {
|
|
themeSource,
|
|
shouldUseDarkColors,
|
|
shouldUseHighContrastColors,
|
|
shouldUseInvertedColorScheme,
|
|
inForcedColorsMode,
|
|
} = electron.nativeTheme;
|
|
return {
|
|
themeSource,
|
|
shouldUseDarkColors,
|
|
shouldUseHighContrastColors,
|
|
shouldUseInvertedColorScheme,
|
|
inForcedColorsMode,
|
|
};
|
|
},
|
|
getApp: async () => {
|
|
const {name} = electron.app;
|
|
return {name};
|
|
},
|
|
};
|
|
|
|
private readonly commandsSync: ElectronIpcSyncCommands = {
|
|
getCurrentWindowState: () => {
|
|
const isFocused = this.window.isFocused();
|
|
return {isFocused};
|
|
},
|
|
};
|
|
}
|