TeaSpeak-Client/modules/core/render-backend/menu.ts

26 lines
1.2 KiB
TypeScript
Raw Normal View History

2019-10-25 19:51:40 -04:00
import * as electron from "electron";
import ipcMain = electron.ipcMain;
import BrowserWindow = electron.BrowserWindow;
2020-10-05 09:10:49 -04:00
import {NativeMenuBarEntry} from "../../shared/MenuBarDefinitions";
import {Menu, MenuItemConstructorOptions} from "electron";
2019-10-25 19:51:40 -04:00
2020-10-05 09:10:49 -04:00
ipcMain.on("menu-bar", (event, menuBar: NativeMenuBarEntry[]) => {
2019-10-25 19:51:40 -04:00
const window = BrowserWindow.fromWebContents(event.sender);
try {
2020-10-05 09:10:49 -04:00
const processEntry = (entry: NativeMenuBarEntry): MenuItemConstructorOptions => {
return {
type: entry.type === "separator" ? "separator" : entry.children?.length ? "submenu" : "normal",
label: entry.label,
icon: entry.icon ? electron.nativeImage.createFromDataURL(entry.icon).resize({ height: 16, width: 16 }) : undefined,
enabled: !entry.disabled,
click: entry.uniqueId && (() => event.sender.send("menu-bar", "item-click", entry.uniqueId)),
submenu: entry.children?.map(processEntry)
2019-10-25 19:51:40 -04:00
}
2020-10-05 09:10:49 -04:00
};
window.setMenu(Menu.buildFromTemplate(menuBar.map(processEntry)));
} catch (error) {
console.error("failed to set menu bar for %s: %o", window.getTitle(), error);
2019-10-25 19:51:40 -04:00
}
});