2024-06-20 21:34:32 +02:00
|
|
|
// Copyright 2024, Command Line Inc.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
import { getApi } from "./global";
|
|
|
|
|
|
|
|
class ContextMenuModelType {
|
|
|
|
handlers: Map<string, () => void> = new Map(); // id -> handler
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
getApi().onContextMenuClick(this.handleContextMenuClick.bind(this));
|
|
|
|
}
|
|
|
|
|
2024-07-23 21:40:50 +02:00
|
|
|
handleContextMenuClick(id: string): void {
|
2024-07-23 21:42:31 +02:00
|
|
|
const handler = this.handlers.get(id);
|
2024-06-20 21:34:32 +02:00
|
|
|
if (handler) {
|
|
|
|
handler();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_convertAndRegisterMenu(menu: ContextMenuItem[]): ElectronContextMenuItem[] {
|
2024-07-23 21:42:31 +02:00
|
|
|
const electronMenuItems: ElectronContextMenuItem[] = [];
|
|
|
|
for (const item of menu) {
|
|
|
|
const electronItem: ElectronContextMenuItem = {
|
2024-06-20 21:34:32 +02:00
|
|
|
role: item.role,
|
|
|
|
type: item.type,
|
|
|
|
label: item.label,
|
2024-10-06 22:55:26 +02:00
|
|
|
sublabel: item.sublabel,
|
2024-08-13 06:20:13 +02:00
|
|
|
id: crypto.randomUUID(),
|
2024-09-26 05:53:32 +02:00
|
|
|
checked: item.checked,
|
2024-06-20 21:34:32 +02:00
|
|
|
};
|
2024-10-06 22:55:26 +02:00
|
|
|
if (item.visible === false) {
|
|
|
|
electronItem.visible = false;
|
|
|
|
}
|
|
|
|
if (item.enabled === false) {
|
|
|
|
electronItem.enabled = false;
|
|
|
|
}
|
2024-06-20 21:34:32 +02:00
|
|
|
if (item.click) {
|
|
|
|
this.handlers.set(electronItem.id, item.click);
|
|
|
|
}
|
|
|
|
if (item.submenu) {
|
|
|
|
electronItem.submenu = this._convertAndRegisterMenu(item.submenu);
|
|
|
|
}
|
|
|
|
electronMenuItems.push(electronItem);
|
|
|
|
}
|
|
|
|
return electronMenuItems;
|
|
|
|
}
|
|
|
|
|
|
|
|
showContextMenu(menu: ContextMenuItem[], ev: React.MouseEvent<any>): void {
|
|
|
|
this.handlers.clear();
|
|
|
|
const electronMenuItems = this._convertAndRegisterMenu(menu);
|
2024-08-17 03:01:31 +02:00
|
|
|
getApi().showContextMenu(electronMenuItems);
|
2024-06-20 21:34:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const ContextMenuModel = new ContextMenuModelType();
|
|
|
|
|
|
|
|
export { ContextMenuModel, ContextMenuModelType };
|