-
-
Notifications
You must be signed in to change notification settings - Fork 748
Expand file tree
/
Copy pathmenu.js
More file actions
65 lines (65 loc) · 2.47 KB
/
menu.js
File metadata and controls
65 lines (65 loc) · 2.47 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
"use strict";
const electron_1 = require("electron");
const contextMenuItems = (global["contextMenuItems"] =
global["contextMenuItems"] || []);
let electronSocket;
module.exports = (socket) => {
electronSocket = socket;
socket.on("menu-setContextMenu", (browserWindowId, menuItems) => {
const menu = electron_1.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 = electron_1.BrowserWindow.fromId(browserWindowId);
x.menu.popup(browserWindow);
}
});
});
socket.on("menu-setApplicationMenu", (menuItems) => {
const menu = electron_1.Menu.buildFromTemplate(menuItems);
addMenuItemClickConnector(menu.items, (id) => {
electronSocket.emit("menuItemClicked", id);
});
electron_1.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);
};
}
});
}
};
//# sourceMappingURL=menu.js.map