forked from ElectronNET/Electron.NET
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathipc.ts
More file actions
81 lines (70 loc) · 2.9 KB
/
ipc.ts
File metadata and controls
81 lines (70 loc) · 2.9 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
import { ipcMain, BrowserWindow, BrowserView, Menu } from 'electron';
import { Socket } from 'net';
let electronSocket;
export = (socket: Socket) => {
electronSocket = socket;
socket.on('registerIpcMainChannel', (channel) => {
ipcMain.on(channel, (event, args) => {
electronSocket.emit(channel, [event.preventDefault(), args]);
});
});
socket.on('registerSyncIpcMainChannel', (channel) => {
ipcMain.on(channel, (event, args) => {
const x = <any>socket;
x.removeAllListeners(channel + 'Sync');
socket.on(channel + 'Sync', (result) => {
event.returnValue = result;
});
electronSocket.emit(channel, [event.preventDefault(), args]);
});
});
socket.on('registerOnceIpcMainChannel', (channel) => {
ipcMain.once(channel, (event, args) => {
electronSocket.emit(channel, [event.preventDefault(), args]);
});
});
socket.on('removeAllListenersIpcMainChannel', (channel) => {
ipcMain.removeAllListeners(channel);
});
socket.on('sendToIpcRenderer', (browserWindow, channel, data) => {
const window = BrowserWindow.fromId(browserWindow.id);
if (window) {
window.webContents.send(channel, ...data);
}
});
socket.on('sendToIpcRendererBrowserView', (id, channel, data) => {
const browserViews: BrowserView[] = (global['browserViews'] = global['browserViews'] || []) as BrowserView[];
let view: BrowserView = null;
for (let i = 0; i < browserViews.length; i++) {
if (browserViews[i]['id'] === id) {
view = browserViews[i];
break;
}
}
if (view) {
view.webContents.send(channel, ...data);
}
});
// Integration helpers: programmatically click menu items from renderer tests
ipcMain.on('integration-click-application-menu', (event, id: string) => {
try {
const menu = Menu.getApplicationMenu();
const mi = menu ? menu.getMenuItemById(id) : null;
if (mi && typeof (mi as any).click === 'function') {
const bw = BrowserWindow.fromWebContents(event.sender);
(mi as any).click(undefined, bw, undefined);
}
} catch { /* ignore */ }
});
ipcMain.on('integration-click-context-menu', (event, windowId: number, id: string) => {
try {
const entries = (global as any)['contextMenuItems'] || [];
const entry = entries.find((x: any) => x.browserWindowId === windowId);
const mi = entry?.menu?.items?.find((i: any) => i.id === id);
if (mi && typeof (mi as any).click === 'function') {
const bw = BrowserWindow.fromId(windowId);
(mi as any).click(undefined, bw, undefined);
}
} catch { /* ignore */ }
});
};