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

34 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;
ipcMain.on('top-menu', (event, menu_template: electron.MenuItemConstructorOptions[]) => {
const window = BrowserWindow.fromWebContents(event.sender);
const process_template = (item: electron.MenuItemConstructorOptions) => {
if(typeof(item.icon) === "string" && item.icon.startsWith("data:"))
item.icon = electron.nativeImage.createFromDataURL(item.icon);
item.click = () => window.webContents.send('top-menu', item.id);
for(const i of item.submenu as electron.MenuItemConstructorOptions[] || []) {
process_template(i);
}
};
for(const m of menu_template)
process_template(m);
try {
const menu = new electron.Menu();
for(const m of menu_template) {
try {
menu.append(new electron.MenuItem(m));
} catch(error) {
console.error("Failed to build menu entry: %o\nSource: %o", error, m);
}
}
window.setMenu(menu_template.length == 0 ? undefined : menu);
} catch(error) {
console.error("Failed to set window menu: %o", error);
}
});