-
-
Notifications
You must be signed in to change notification settings - Fork 748
Expand file tree
/
Copy pathmenu.ts
More file actions
92 lines (77 loc) · 2.26 KB
/
menu.ts
File metadata and controls
92 lines (77 loc) · 2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import type { Socket } from "net";
import { Menu, BrowserWindow } from "electron";
const contextMenuItems = (global["contextMenuItems"] =
global["contextMenuItems"] || []);
let electronSocket: Socket;
export = (socket: Socket) => {
electronSocket = socket;
socket.on("menu-setContextMenu", (browserWindowId, menuItems) => {
const menu = Menu.buildFromTemplate(menuItems);
addContextMenuItemClickConnector(
menu.items,
browserWindowId,
(id, windowId) => {
electronSocket.emit("contextMenuItemClicked", [id, windowId]);
},
);
const index = contextMenuItems.findIndex(
(contextMenu) => contextMenu.browserWindowId === browserWindowId,
);
const contextMenuItem = {
menu: menu,
browserWindowId: browserWindowId,
};
if (index === -1) {
contextMenuItems.push(contextMenuItem);
} else {
contextMenuItems[index] = contextMenuItem;
}
});
function addContextMenuItemClickConnector(
menuItems,
browserWindowId,
callback,
) {
menuItems.forEach((item) => {
if (item.submenu && item.submenu.items.length > 0) {
addContextMenuItemClickConnector(
item.submenu.items,
browserWindowId,
callback,
);
}
if ("id" in item && item.id) {
item.click = () => {
callback(item.id, browserWindowId);
};
}
});
}
socket.on("menu-contextMenuPopup", (browserWindowId) => {
contextMenuItems.forEach((x) => {
if (x.browserWindowId === browserWindowId) {
const browserWindow = BrowserWindow.fromId(browserWindowId);
x.menu.popup(browserWindow);
}
});
});
socket.on("menu-setApplicationMenu", (menuItems) => {
const menu = Menu.buildFromTemplate(menuItems);
addMenuItemClickConnector(menu.items, (id) => {
electronSocket.emit("menuItemClicked", id);
});
Menu.setApplicationMenu(menu);
});
function addMenuItemClickConnector(menuItems, callback) {
menuItems.forEach((item) => {
if (item.submenu && item.submenu.items.length > 0) {
addMenuItemClickConnector(item.submenu.items, callback);
}
if ("id" in item && item.id) {
item.click = () => {
callback(item.id);
};
}
});
}
};