import {class_to_image} from "./icon-helper"; window["require_setup"](module); import * as electron from "electron"; //import {top_menu as dtop_menu, Icon} from "./imports/imports_shared"; /// import dtop_menu = top_menu; namespace _top_menu { import ipcRenderer = electron.ipcRenderer; namespace native { import ipcRenderer = electron.ipcRenderer; let _item_index = 1; abstract class NativeMenuBase { protected _handle: NativeMenuBar; protected _click: () => any; id: string; protected constructor(handle: NativeMenuBar, id?: string) { this._handle = handle; this.id = id || ("item_" + (_item_index++)); } abstract build() : electron.MenuItemConstructorOptions; abstract items(): (dtop_menu.MenuItem | dtop_menu.HRItem)[]; trigger_click() { if(this._click) this._click(); } } class NativeMenuItem extends NativeMenuBase implements dtop_menu.MenuItem { private _items: (NativeMenuItem | NativeHrItem)[] = []; private _label: string; private _enabled: boolean = true; private _visible: boolean = true; private _icon_data: string; constructor(handle: NativeMenuBar) { super(handle); } append_hr(): dtop_menu.HRItem { const item = new NativeHrItem(this._handle); this._items.push(item); return item; } append_item(label: string): dtop_menu.MenuItem { const item = new NativeMenuItem(this._handle); item.label(label); this._items.push(item); return item; } click(callback: () => any): this { this._click = callback; return this; } delete_item(item: dtop_menu.MenuItem | dtop_menu.HRItem) { const i_index = this._items.indexOf(item as any); if(i_index < 0) return; this._items.splice(i_index, 1); } disabled(value?: boolean): boolean { if(typeof(value) === "boolean") this._enabled = !value; return !this._enabled; } icon(klass?: string | Promise | Icon): string { if(typeof(klass) === "string") { const buffer = class_to_image(klass); if(buffer) this._icon_data = buffer.toDataURL(); } return ""; } items(): (dtop_menu.MenuItem | dtop_menu.HRItem)[] { return this._items; } label(value?: string): string { if(typeof(value) === "string") this._label = value; return this._label; } visible(value?: boolean): boolean { if(typeof(value) === "boolean") this._visible = value; return this._visible; } build(): Electron.MenuItemConstructorOptions { return { id: this.id, label: this._label || "", submenu: this._items.length > 0 ? this._items.map(e => e.build()) : undefined, enabled: this._enabled, visible: this._visible, icon: this._icon_data } } } class NativeHrItem extends NativeMenuBase implements dtop_menu.HRItem { constructor(handle: NativeMenuBar) { super(handle); } build(): Electron.MenuItemConstructorOptions { return { type: 'separator', id: this.id } } items(): (dtop_menu.MenuItem | dtop_menu.HRItem)[] { return []; } } function is_similar_deep(a, b) { if(typeof(a) !== typeof(b)) return false; if(typeof(a) !== "object") return a === b; const aProps = Object.keys(a); const bProps = Object.keys(b); if (aProps.length != bProps.length) return false; for (let i = 0; i < aProps.length; i++) { const propName = aProps[i]; if(!is_similar_deep(a[propName], b[propName])) return false; } return true; } export class NativeMenuBar implements dtop_menu.MenuBarDriver { private static _instance: NativeMenuBar; private menu: electron.Menu; private _items: NativeMenuItem[] = []; private _current_menu: electron.MenuItemConstructorOptions[]; public static instance() : NativeMenuBar { if(!this._instance) this._instance = new NativeMenuBar(); return this._instance; } append_item(label: string): dtop_menu.MenuItem { const item = new NativeMenuItem(this); item.label(label); this._items.push(item); return item; } delete_item(item: dtop_menu.MenuItem) { const i_index = this._items.indexOf(item as any); if(i_index < 0) return; this._items.splice(i_index, 1); } flush_changes() { const target_menu = this.build_menu(); if(is_similar_deep(target_menu, this._current_menu)) return; this._current_menu = target_menu; ipcRenderer.send('top-menu', target_menu); } private build_menu() : electron.MenuItemConstructorOptions[] { return this._items.map(e => e.build()); } items(): dtop_menu.MenuItem[] { return this._items; } initialize() { this.menu = new electron.remote.Menu(); ipcRenderer.on('top-menu', (event, clicked_item) => { console.log("Item %o clicked", clicked_item); const check_item = (item: NativeMenuBase) => { if(item.id == clicked_item) { item.trigger_click(); return true; } for(const child of item.items()) if(check_item(child as NativeMenuBase)) return true; }; for(const item of this._items) if(check_item(item)) return; }); } } } //Global variable // @ts-ignore top_menu.set_driver(native.NativeMenuBar.instance()); const call_basic_action = (name: string, ...args: any[]) => ipcRenderer.send('basic-action', name, ...args); top_menu.native_actions = { open_change_log() { call_basic_action("open-changelog"); }, check_native_update() { call_basic_action("check-native-update"); }, quit() { call_basic_action("quit"); }, open_dev_tools() { call_basic_action("open-dev-tools"); }, reload_page() { call_basic_action("reload-window") } }; } export {};