import {ContextMenuEntry, ContextMenuFactory, setGlobalContextMenuFactory} from "tc-shared/ui/ContextMenu"; import * as electron from "electron"; import {MenuItemConstructorOptions} from "electron"; import {clientIconClassToImage} from "./IconHelper"; const {Menu} = electron.remote; let currentMenu: electron.Menu; function mapMenuEntry(entry: ContextMenuEntry) : MenuItemConstructorOptions { switch (entry.type) { case "normal": return { type: "normal", label: typeof entry.label === "string" ? entry.label : entry.label.text, enabled: entry.enabled, visible: entry.visible, click: entry.click, icon: typeof entry.icon === "string" ? clientIconClassToImage(entry.icon) : undefined, id: entry.uniqueId, submenu: entry.subMenu ? entry.subMenu.map(mapMenuEntry).filter(e => !!e) : undefined }; case "checkbox": return { type: "normal", label: typeof entry.label === "string" ? entry.label : entry.label.text, enabled: entry.enabled, visible: entry.visible, click: entry.click, id: entry.uniqueId, checked: entry.checked }; case "separator": return { type: "separator" }; default: return undefined; } } setGlobalContextMenuFactory(new class implements ContextMenuFactory { closeContextMenu() { currentMenu?.closePopup(); currentMenu = undefined; } spawnContextMenu(position: { pageX: number; pageY: number }, entries: ContextMenuEntry[], callbackClose?: () => void) { this.closeContextMenu(); currentMenu = Menu.buildFromTemplate(entries.map(mapMenuEntry).filter(e => !!e)); currentMenu.popup({ callback: () => { callbackClose(); currentMenu = undefined; }, x: position.pageX, y: position.pageY, window: electron.remote.BrowserWindow.getFocusedWindow() }); } });