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
|
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { BrowserWindow, ipcMain, IpcMainEvent, Menu, MenuItem } from 'electron';
import { withNullAsUndefined } from 'vs/base/common/types';
import { CONTEXT_MENU_CHANNEL, CONTEXT_MENU_CLOSE_CHANNEL, IPopupOptions, ISerializableContextMenuItem } from 'vs/base/parts/contextmenu/common/contextmenu';
export function registerContextMenuListener(): void {
ipcMain.on(CONTEXT_MENU_CHANNEL, (event: IpcMainEvent, contextMenuId: number, items: ISerializableContextMenuItem[], onClickChannel: string, options?: IPopupOptions) => {
const menu = createMenu(event, onClickChannel, items);
menu.popup({
window: withNullAsUndefined(BrowserWindow.fromWebContents(event.sender)),
x: options ? options.x : undefined,
y: options ? options.y : undefined,
positioningItem: options ? options.positioningItem : undefined,
callback: () => {
// Workaround for https://github.com/microsoft/vscode/issues/72447
// It turns out that the menu gets GC'ed if not referenced anymore
// As such we drag it into this scope so that it is not being GC'ed
if (menu) {
event.sender.send(CONTEXT_MENU_CLOSE_CHANNEL, contextMenuId);
}
}
});
});
}
function createMenu(event: IpcMainEvent, onClickChannel: string, items: ISerializableContextMenuItem[]): Menu {
const menu = new Menu();
items.forEach(item => {
let menuitem: MenuItem;
// Separator
if (item.type === 'separator') {
menuitem = new MenuItem({
type: item.type,
});
}
// Sub Menu
else if (Array.isArray(item.submenu)) {
menuitem = new MenuItem({
submenu: createMenu(event, onClickChannel, item.submenu),
label: item.label
});
}
// Normal Menu Item
else {
menuitem = new MenuItem({
label: item.label,
type: item.type,
accelerator: item.accelerator,
checked: item.checked,
enabled: item.enabled,
visible: item.visible,
click: (menuItem, win, contextmenuEvent) => event.sender.send(onClickChannel, item.id, contextmenuEvent)
});
}
menu.append(menuitem);
});
return menu;
}
|