1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.preload = void 0;
|
4 | const disposable_1 = require("../common/disposable");
|
5 | const electron_api_1 = require("../electron-common/electron-api");
|
6 |
|
7 | const { ipcRenderer, contextBridge } = require('electron');
|
8 |
|
9 | const commandHandlers = new Map();
|
10 | let nextHandlerId = 0;
|
11 | const mainMenuId = 0;
|
12 | let nextMenuId = mainMenuId + 1;
|
13 | function convertMenu(menu, handlerMap) {
|
14 | if (!menu) {
|
15 | return undefined;
|
16 | }
|
17 | return menu.map(item => {
|
18 | let handlerId = undefined;
|
19 | if (item.execute) {
|
20 | handlerId = nextHandlerId++;
|
21 | handlerMap.set(handlerId, item.execute);
|
22 | }
|
23 | return {
|
24 | id: item.id,
|
25 | submenu: convertMenu(item.submenu, handlerMap),
|
26 | accelerator: item.accelerator,
|
27 | label: item.label,
|
28 | handlerId: handlerId,
|
29 | checked: item.checked,
|
30 | enabled: item.enabled,
|
31 | role: item.role,
|
32 | type: item.type,
|
33 | visible: item.visible
|
34 | };
|
35 | });
|
36 | }
|
37 | const api = {
|
38 | setMenuBarVisible: (visible, windowName) => ipcRenderer.send(electron_api_1.CHANNEL_SET_MENU_BAR_VISIBLE, visible, windowName),
|
39 | setMenu: (menu) => {
|
40 | commandHandlers.delete(mainMenuId);
|
41 | const handlers = new Map();
|
42 | commandHandlers.set(mainMenuId, handlers);
|
43 | ipcRenderer.send(electron_api_1.CHANNEL_SET_MENU, mainMenuId, convertMenu(menu, handlers));
|
44 | },
|
45 | getSecurityToken: () => ipcRenderer.sendSync(electron_api_1.CHANNEL_GET_SECURITY_TOKEN),
|
46 | focusWindow: (name) => ipcRenderer.send(electron_api_1.CHANNEL_FOCUS_WINDOW, name),
|
47 | showItemInFolder: fsPath => {
|
48 | ipcRenderer.send(electron_api_1.CHANNEL_SHOW_ITEM_IN_FOLDER, fsPath);
|
49 | },
|
50 | attachSecurityToken: (endpoint) => ipcRenderer.invoke(electron_api_1.CHANNEL_ATTACH_SECURITY_TOKEN, endpoint),
|
51 | popup: async function (menu, x, y, onClosed) {
|
52 | const menuId = nextMenuId++;
|
53 | const handlers = new Map();
|
54 | commandHandlers.set(menuId, handlers);
|
55 | const handle = await ipcRenderer.invoke(electron_api_1.CHANNEL_OPEN_POPUP, menuId, convertMenu(menu, handlers), x, y);
|
56 | const closeListener = () => {
|
57 | ipcRenderer.removeListener(electron_api_1.CHANNEL_ON_CLOSE_POPUP, closeListener);
|
58 | commandHandlers.delete(menuId);
|
59 | onClosed();
|
60 | };
|
61 | ipcRenderer.on(electron_api_1.CHANNEL_ON_CLOSE_POPUP, closeListener);
|
62 | return handle;
|
63 | },
|
64 | closePopup: function (handle) {
|
65 | ipcRenderer.send(electron_api_1.CHANNEL_CLOSE_POPUP, handle);
|
66 | },
|
67 | getTitleBarStyleAtStartup: function () {
|
68 | return ipcRenderer.invoke(electron_api_1.CHANNEL_GET_TITLE_STYLE_AT_STARTUP);
|
69 | },
|
70 | setTitleBarStyle: function (style) {
|
71 | ipcRenderer.send(electron_api_1.CHANNEL_SET_TITLE_STYLE, style);
|
72 | },
|
73 | setBackgroundColor: function (backgroundColor) {
|
74 | ipcRenderer.send(electron_api_1.CHANNEL_SET_BACKGROUND_COLOR, backgroundColor);
|
75 | },
|
76 | minimize: function () {
|
77 | ipcRenderer.send(electron_api_1.CHANNEL_MINIMIZE);
|
78 | },
|
79 | isMaximized: function () {
|
80 | return ipcRenderer.sendSync(electron_api_1.CHANNEL_IS_MAXIMIZED);
|
81 | },
|
82 | maximize: function () {
|
83 | ipcRenderer.send(electron_api_1.CHANNEL_MAXIMIZE);
|
84 | },
|
85 | unMaximize: function () {
|
86 | ipcRenderer.send(electron_api_1.CHANNEL_UNMAXIMIZE);
|
87 | },
|
88 | close: function () {
|
89 | ipcRenderer.send(electron_api_1.CHANNEL_CLOSE);
|
90 | },
|
91 | onWindowEvent: function (event, handler) {
|
92 | const h = (_event, evt) => {
|
93 | if (event === evt) {
|
94 | handler();
|
95 | }
|
96 | };
|
97 | ipcRenderer.on(electron_api_1.CHANNEL_ON_WINDOW_EVENT, h);
|
98 | return disposable_1.Disposable.create(() => ipcRenderer.off(electron_api_1.CHANNEL_ON_WINDOW_EVENT, h));
|
99 | },
|
100 | setCloseRequestHandler: function (handler) {
|
101 | ipcRenderer.on(electron_api_1.CHANNEL_REQUEST_CLOSE, async (event, stopReason, confirmChannel, cancelChannel) => {
|
102 | try {
|
103 | if (await handler(stopReason)) {
|
104 | event.sender.send(confirmChannel);
|
105 | return;
|
106 | }
|
107 | ;
|
108 | }
|
109 | catch (e) {
|
110 | console.warn('exception in close handler ', e);
|
111 | }
|
112 | event.sender.send(cancelChannel);
|
113 | });
|
114 | },
|
115 | setSecondaryWindowCloseRequestHandler(windowName, handler) {
|
116 |
|
117 | const listener = async (event, name, confirmChannel, cancelChannel) => {
|
118 | if (name === windowName) {
|
119 | try {
|
120 | if (await handler()) {
|
121 | event.sender.send(confirmChannel);
|
122 | ipcRenderer.removeListener(electron_api_1.CHANNEL_REQUEST_SECONDARY_CLOSE, listener);
|
123 | return;
|
124 | }
|
125 | ;
|
126 | }
|
127 | catch (e) {
|
128 | console.warn('exception in close handler ', e);
|
129 | }
|
130 | event.sender.send(cancelChannel);
|
131 | }
|
132 | };
|
133 | ipcRenderer.on(electron_api_1.CHANNEL_REQUEST_SECONDARY_CLOSE, listener);
|
134 | },
|
135 | toggleDevTools: function () {
|
136 | ipcRenderer.send(electron_api_1.CHANNEL_TOGGLE_DEVTOOLS);
|
137 | },
|
138 | getZoomLevel: function () {
|
139 | return ipcRenderer.invoke(electron_api_1.CHANNEL_GET_ZOOM_LEVEL);
|
140 | },
|
141 | setZoomLevel: function (desired) {
|
142 | ipcRenderer.send(electron_api_1.CHANNEL_SET_ZOOM_LEVEL, desired);
|
143 | },
|
144 | isFullScreenable: function () {
|
145 | return ipcRenderer.sendSync(electron_api_1.CHANNEL_IS_FULL_SCREENABLE);
|
146 | },
|
147 | isFullScreen: function () {
|
148 | return ipcRenderer.sendSync(electron_api_1.CHANNEL_IS_FULL_SCREEN);
|
149 | },
|
150 | toggleFullScreen: function () {
|
151 | ipcRenderer.send(electron_api_1.CHANNEL_TOGGLE_FULL_SCREEN);
|
152 | },
|
153 | requestReload: () => ipcRenderer.send(electron_api_1.CHANNEL_REQUEST_RELOAD),
|
154 | restart: () => ipcRenderer.send(electron_api_1.CHANNEL_RESTART),
|
155 | applicationStateChanged: state => {
|
156 | ipcRenderer.send(electron_api_1.CHANNEL_APP_STATE_CHANGED, state);
|
157 | },
|
158 | readClipboard() {
|
159 | return ipcRenderer.sendSync(electron_api_1.CHANNEL_READ_CLIPBOARD);
|
160 | },
|
161 | writeClipboard(text) {
|
162 | ipcRenderer.send(electron_api_1.CHANNEL_WRITE_CLIPBOARD, text);
|
163 | },
|
164 | onKeyboardLayoutChanged(handler) {
|
165 | return createDisposableListener(electron_api_1.CHANNEL_KEYBOARD_LAYOUT_CHANGED, (event, layout) => { handler(layout); });
|
166 | },
|
167 | onData: handler => createDisposableListener(electron_api_1.CHANNEL_IPC_CONNECTION, (event, data) => { handler(data); }),
|
168 | sendData: data => {
|
169 | ipcRenderer.send(electron_api_1.CHANNEL_IPC_CONNECTION, data);
|
170 | },
|
171 | };
|
172 |
|
173 | function createDisposableListener(channel, handler) {
|
174 | ipcRenderer.on(channel, handler);
|
175 | return disposable_1.Disposable.create(() => ipcRenderer.off(channel, handler));
|
176 | }
|
177 | function preload() {
|
178 | console.log('exposing theia core electron api');
|
179 | ipcRenderer.on(electron_api_1.CHANNEL_INVOKE_MENU, (_, menuId, handlerId) => {
|
180 | const map = commandHandlers.get(menuId);
|
181 | if (map) {
|
182 | const handler = map.get(handlerId);
|
183 | if (handler) {
|
184 | handler();
|
185 | }
|
186 | }
|
187 | });
|
188 | contextBridge.exposeInMainWorld('electronTheiaCore', api);
|
189 | }
|
190 | exports.preload = preload;
|
191 |
|
\ | No newline at end of file |