2021-04-07 19:42:06 +02:00
|
|
|
import { app, dialog, ipcMain, Menu, MenuItem, nativeTheme } from "electron";
|
2018-04-26 21:43:59 +02:00
|
|
|
|
2022-02-22 15:39:11 +01:00
|
|
|
import { MessagingService } from "jslib-common/abstractions/messaging.service";
|
2021-09-29 22:37:36 +02:00
|
|
|
import { ThemeType } from "jslib-common/enums/themeType";
|
|
|
|
|
2022-02-22 15:39:11 +01:00
|
|
|
import { RendererMenuItem } from "../utils";
|
2018-04-26 21:43:59 +02:00
|
|
|
import { WindowMain } from "../window.main";
|
|
|
|
|
|
|
|
export class ElectronMainMessagingService implements MessagingService {
|
2021-04-07 19:42:06 +02:00
|
|
|
constructor(private windowMain: WindowMain, private onMessage: (message: any) => void) {
|
|
|
|
ipcMain.handle("appVersion", () => {
|
|
|
|
return app.getVersion();
|
|
|
|
});
|
|
|
|
|
|
|
|
ipcMain.handle("systemTheme", () => {
|
2021-09-29 22:37:36 +02:00
|
|
|
return nativeTheme.shouldUseDarkColors ? ThemeType.Dark : ThemeType.Light;
|
2021-04-07 19:42:06 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
ipcMain.handle("showMessageBox", (event, options) => {
|
|
|
|
return dialog.showMessageBox(options);
|
|
|
|
});
|
|
|
|
|
|
|
|
ipcMain.handle("openContextMenu", (event, options: { menu: RendererMenuItem[] }) => {
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
const menu = new Menu();
|
|
|
|
options.menu.forEach((m, index) => {
|
|
|
|
menu.append(
|
|
|
|
new MenuItem({
|
|
|
|
label: m.label,
|
|
|
|
type: m.type,
|
|
|
|
click: () => {
|
|
|
|
resolve(index);
|
|
|
|
},
|
|
|
|
})
|
|
|
|
);
|
|
|
|
});
|
2021-05-06 19:41:51 +02:00
|
|
|
menu.popup({
|
|
|
|
window: windowMain.win,
|
|
|
|
callback: () => {
|
|
|
|
resolve(-1);
|
2021-12-16 13:36:21 +01:00
|
|
|
},
|
2021-05-06 19:41:51 +02:00
|
|
|
});
|
2021-12-16 13:36:21 +01:00
|
|
|
});
|
|
|
|
});
|
2021-05-06 19:41:51 +02:00
|
|
|
|
|
|
|
ipcMain.handle("windowVisible", () => {
|
|
|
|
return windowMain.win?.isVisible();
|
2021-12-16 13:36:21 +01:00
|
|
|
});
|
|
|
|
|
2021-04-07 19:42:06 +02:00
|
|
|
nativeTheme.on("updated", () => {
|
2021-09-29 22:37:36 +02:00
|
|
|
windowMain.win?.webContents.send(
|
|
|
|
"systemThemeUpdated",
|
|
|
|
nativeTheme.shouldUseDarkColors ? ThemeType.Dark : ThemeType.Light
|
|
|
|
);
|
2021-04-07 19:42:06 +02:00
|
|
|
});
|
|
|
|
}
|
2018-04-26 21:43:59 +02:00
|
|
|
|
|
|
|
send(subscriber: string, arg: any = {}) {
|
|
|
|
const message = Object.assign({}, { command: subscriber }, arg);
|
|
|
|
this.onMessage(message);
|
|
|
|
if (this.windowMain.win != null) {
|
|
|
|
this.windowMain.win.webContents.send("messagingService", message);
|
|
|
|
}
|
2021-12-16 13:36:21 +01:00
|
|
|
}
|
2018-04-26 21:43:59 +02:00
|
|
|
}
|