@itwin/core-electron 4.0.0-dev.52 → 4.0.0-dev.55

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. package/lib/cjs/ElectronBackend.d.ts +1 -1
  2. package/lib/cjs/ElectronBackend.js +21 -17
  3. package/lib/cjs/ElectronBackend.js.map +1 -1
  4. package/lib/cjs/ElectronFrontend.d.ts +1 -1
  5. package/lib/cjs/ElectronFrontend.js +21 -17
  6. package/lib/cjs/ElectronFrontend.js.map +1 -1
  7. package/lib/cjs/__DOC_ONLY__.d.ts +3 -3
  8. package/lib/cjs/__DOC_ONLY__.js +26 -22
  9. package/lib/cjs/__DOC_ONLY__.js.map +1 -1
  10. package/lib/cjs/backend/ElectronHost.d.ts +104 -104
  11. package/lib/cjs/backend/ElectronHost.js +240 -240
  12. package/lib/cjs/backend/ElectronHost.js.map +1 -1
  13. package/lib/cjs/backend/ElectronPreload.d.ts +1 -1
  14. package/lib/cjs/backend/ElectronPreload.js +39 -39
  15. package/lib/cjs/common/ElectronIpcInterface.d.ts +7 -7
  16. package/lib/cjs/common/ElectronIpcInterface.d.ts.map +1 -1
  17. package/lib/cjs/common/ElectronIpcInterface.js +5 -5
  18. package/lib/cjs/common/ElectronIpcTransport.d.ts +44 -44
  19. package/lib/cjs/common/ElectronIpcTransport.js +186 -186
  20. package/lib/cjs/common/ElectronIpcTransport.js.map +1 -1
  21. package/lib/cjs/common/ElectronManagerLoggerCategory.d.ts +10 -10
  22. package/lib/cjs/common/ElectronManagerLoggerCategory.js +18 -18
  23. package/lib/cjs/common/ElectronPush.d.ts +20 -20
  24. package/lib/cjs/common/ElectronPush.js +48 -48
  25. package/lib/cjs/common/ElectronPush.js.map +1 -1
  26. package/lib/cjs/common/ElectronRpcManager.d.ts +26 -26
  27. package/lib/cjs/common/ElectronRpcManager.js +65 -65
  28. package/lib/cjs/common/ElectronRpcProtocol.d.ts +34 -34
  29. package/lib/cjs/common/ElectronRpcProtocol.js +58 -58
  30. package/lib/cjs/common/ElectronRpcProtocol.js.map +1 -1
  31. package/lib/cjs/common/ElectronRpcRequest.d.ts +19 -19
  32. package/lib/cjs/common/ElectronRpcRequest.js +56 -56
  33. package/lib/cjs/common/ITwinElectronApi.d.ts +10 -10
  34. package/lib/cjs/common/ITwinElectronApi.d.ts.map +1 -1
  35. package/lib/cjs/common/ITwinElectronApi.js +6 -6
  36. package/lib/cjs/frontend/ElectronApp.d.ts +35 -35
  37. package/lib/cjs/frontend/ElectronApp.d.ts.map +1 -1
  38. package/lib/cjs/frontend/ElectronApp.js +73 -73
  39. package/lib/cjs/frontend/ElectronApp.js.map +1 -1
  40. package/package.json +15 -15
@@ -1,241 +1,241 @@
1
- "use strict";
2
- /*---------------------------------------------------------------------------------------------
3
- * Copyright (c) Bentley Systems, Incorporated. All rights reserved.
4
- * See LICENSE.md in the project root for license terms and full copyright notice.
5
- *--------------------------------------------------------------------------------------------*/
6
- Object.defineProperty(exports, "__esModule", { value: true });
7
- exports.ElectronHost = void 0;
8
- const fs = require("fs");
9
- const path = require("path");
10
- const core_bentley_1 = require("@itwin/core-bentley");
11
- const core_backend_1 = require("@itwin/core-backend");
12
- const core_common_1 = require("@itwin/core-common");
13
- const ElectronRpcManager_1 = require("../common/ElectronRpcManager");
14
- const ElectronIpcInterface_1 = require("../common/ElectronIpcInterface");
15
- // cSpell:ignore signin devserver webcontents copyfile unmaximize eopt
16
- class ElectronIpc {
17
- addListener(channel, listener) {
18
- ElectronHost.ipcMain.addListener(channel, listener);
19
- return () => ElectronHost.ipcMain.removeListener(channel, listener);
20
- }
21
- removeListener(channel, listener) {
22
- ElectronHost.ipcMain.removeListener(channel, listener);
23
- }
24
- send(channel, ...args) {
25
- const window = ElectronHost.mainWindow ?? ElectronHost.electron.BrowserWindow.getAllWindows()[0];
26
- window?.webContents.send(channel, ...args);
27
- }
28
- handle(channel, listener) {
29
- ElectronHost.ipcMain.removeHandler(channel); // make sure there's not already a handler registered
30
- ElectronHost.ipcMain.handle(channel, listener);
31
- return () => ElectronHost.ipcMain.removeHandler(channel);
32
- }
33
- }
34
- /**
35
- * The backend for Electron-based desktop applications
36
- * @beta
37
- */
38
- class ElectronHost {
39
- constructor() { }
40
- static get ipcMain() { return this._electron?.ipcMain; }
41
- static get app() { return this._electron?.app; }
42
- static get electron() { return this._electron; }
43
- /**
44
- * Converts an "electron://frontend/" URL to an absolute file path.
45
- *
46
- * We use this protocol in production builds because our frontend must be built with absolute URLs,
47
- * however, since we're loading everything directly from the install directory, we cannot know the
48
- * absolute path at build time.
49
- */
50
- static parseElectronUrl(requestedUrl) {
51
- // Note that the "frontend/" path is arbitrary - this is just so we can handle *some* relative URLs...
52
- let assetPath = requestedUrl.substring(this._electronFrontend.length);
53
- if (assetPath.length === 0)
54
- assetPath = "index.html";
55
- assetPath = path.normalize(`${this.webResourcesPath}/${assetPath}`);
56
- // File protocols don't follow symlinks, so we need to resolve this to a real path.
57
- // However, if the file doesn't exist, it's fine to return an invalid path here - the request will just fail with net::ERR_FILE_NOT_FOUND
58
- try {
59
- assetPath = fs.realpathSync(assetPath);
60
- }
61
- catch (error) {
62
- // eslint-disable-next-line no-console
63
- // console.warn(`WARNING: Frontend requested "${requestedUrl}", but ${assetPath} does not exist`);
64
- }
65
- if (!assetPath.startsWith(this.webResourcesPath))
66
- throw new Error(`Access to files outside installation directory (${this.webResourcesPath}) is prohibited`);
67
- return assetPath;
68
- }
69
- static _openWindow(options) {
70
- const webPreferences = {
71
- ...options?.webPreferences,
72
- // These web preference variables should not be overriden by the ElectronHostWindowOptions
73
- preload: require.resolve(/* webpack: copyfile */ "./ElectronPreload.js"),
74
- experimentalFeatures: false,
75
- nodeIntegration: false,
76
- contextIsolation: true,
77
- sandbox: true,
78
- nodeIntegrationInWorker: false,
79
- nodeIntegrationInSubFrames: false,
80
- };
81
- const opts = {
82
- ...options,
83
- autoHideMenuBar: true,
84
- icon: this.appIconPath,
85
- webPreferences,
86
- };
87
- this._mainWindow = new (this.electron.BrowserWindow)(opts);
88
- ElectronRpcManager_1.ElectronRpcConfiguration.targetWindowId = this._mainWindow.id;
89
- this._mainWindow.on("closed", () => this._mainWindow = undefined);
90
- this._mainWindow.loadURL(this.frontendURL); // eslint-disable-line @typescript-eslint/no-floating-promises
91
- /** Monitors and saves main window size, position and maximized state */
92
- if (options?.storeWindowName) {
93
- const mainWindow = this._mainWindow;
94
- const name = options.storeWindowName;
95
- const saveWindowPosition = (key) => {
96
- const bounds = mainWindow.getBounds();
97
- core_backend_1.NativeHost.settingsStore.setData(`${key}-${name}`, JSON.stringify(bounds));
98
- };
99
- const saveMaximized = (maximized) => {
100
- if (!maximized)
101
- saveWindowPosition(this._deprecatedSizeAndPosStoreKey);
102
- core_backend_1.NativeHost.settingsStore.setData(`windowMaximized-${name}`, maximized);
103
- };
104
- mainWindow.on("maximize", () => saveMaximized(true));
105
- mainWindow.on("unmaximize", () => saveMaximized(false));
106
- saveMaximized(mainWindow.isMaximized());
107
- mainWindow.on("resized", () => saveWindowPosition(this._deprecatedSizeAndPosStoreKey));
108
- mainWindow.on("moved", () => saveWindowPosition(this._deprecatedSizeAndPosStoreKey));
109
- const debouncedSaveWindowSizeAndPos = debounce(() => saveWindowPosition(this._sizeAndPosStoreKey));
110
- mainWindow.on("resize", () => debouncedSaveWindowSizeAndPos());
111
- mainWindow.on("move", () => debouncedSaveWindowSizeAndPos());
112
- saveWindowPosition(this._sizeAndPosStoreKey);
113
- }
114
- }
115
- /** The "main" BrowserWindow for this application. */
116
- static get mainWindow() { return this._mainWindow; }
117
- /**
118
- * Gets window size and position for a window, by name, from settings file, if present.
119
- * @note Size and position values in the settings file will be updated differently depending on platform.
120
- * On Linux values are only updated on window "unmaximize".
121
- * On Windows and MacOS values are also updated on window manual resize or move.
122
- * To get consistent behavior across different platforms, use [[ElectronHost.getWindowSizeAndPositionSetting]].
123
- * @deprecated in 3.6. Use [[ElectronHost.getWindowSizeAndPositionSetting]].
124
- */
125
- static getWindowSizeSetting(windowName) {
126
- const saved = core_backend_1.NativeHost.settingsStore.getString(`${this._deprecatedSizeAndPosStoreKey}-${windowName}`);
127
- return saved ? JSON.parse(saved) : undefined;
128
- }
129
- /**
130
- * Gets window size and position for a window, by name, from settings file, if present.
131
- */
132
- static getWindowSizeAndPositionSetting(windowName) {
133
- const saved = core_backend_1.NativeHost.settingsStore.getString(`${this._sizeAndPosStoreKey}-${windowName}`);
134
- return saved ? JSON.parse(saved) : undefined;
135
- }
136
- /** Gets "window maximized" flag for a window, by name, from settings file if present */
137
- static getWindowMaximizedSetting(windowName) {
138
- return core_backend_1.NativeHost.settingsStore.getBoolean(`windowMaximized-${windowName}`);
139
- }
140
- /**
141
- * Open the main Window when the app is ready.
142
- * @param windowOptions Options for constructing the main BrowserWindow. See: https://electronjs.org/docs/api/browser-window#new-browserwindowoptions
143
- */
144
- static async openMainWindow(windowOptions) {
145
- const app = this.app;
146
- // quit the application when all windows are closed (unless we're running on MacOS)
147
- app.on("window-all-closed", () => {
148
- if (process.platform !== "darwin")
149
- app.quit();
150
- });
151
- // re-open the main window if it was closed and the app is re-activated (this is the normal MacOS behavior)
152
- app.on("activate", () => {
153
- if (!this._mainWindow)
154
- this._openWindow(windowOptions);
155
- });
156
- if (this._developmentServer) {
157
- // Occasionally, the electron backend may start before the webpack devserver has even started.
158
- // If this happens, we'll just retry and keep reloading the page.
159
- app.on("web-contents-created", (_e, webcontents) => {
160
- webcontents.on("did-fail-load", async (_event, errorCode, _errorDescription, _validatedURL, isMainFrame) => {
161
- // errorCode -102 is CONNECTION_REFUSED - see https://cs.chromium.org/chromium/src/net/base/net_error_list.h
162
- if (isMainFrame && errorCode === -102) {
163
- await core_bentley_1.BeDuration.wait(100);
164
- webcontents.reload();
165
- }
166
- });
167
- });
168
- }
169
- await app.whenReady();
170
- if (!this._developmentServer) {
171
- // handle any "electron://" requests and redirect them to "file://" URLs
172
- this.electron.protocol.registerFileProtocol("electron", (request, callback) => callback(this.parseElectronUrl(request.url))); // eslint-disable-line @typescript-eslint/no-var-requires
173
- }
174
- this._openWindow(windowOptions);
175
- }
176
- static get isValid() { return this._ipc !== undefined; }
177
- /**
178
- * Initialize the backend of an Electron app.
179
- * This method configures the backend for all of the inter-process communication (RPC and IPC) for an
180
- * Electron app. It should be called from your Electron main function.
181
- * @param opts Options that control aspects of your backend.
182
- * @note This method must only be called from the backend of an Electron app (i.e. when [ProcessDetector.isElectronAppBackend]($bentley) is `true`).
183
- */
184
- static async startup(opts) {
185
- if (!core_bentley_1.ProcessDetector.isElectronAppBackend)
186
- throw new Error("Not running under Electron");
187
- if (!this.isValid) {
188
- this._electron = require("electron");
189
- this._ipc = new ElectronIpc();
190
- const app = this.app;
191
- if (!app.isReady())
192
- this.electron.protocol.registerSchemesAsPrivileged([{
193
- scheme: "electron",
194
- privileges: {
195
- standard: true,
196
- secure: true,
197
- supportFetchAPI: true,
198
- },
199
- }]);
200
- const eopt = opts?.electronHost;
201
- this._developmentServer = eopt?.developmentServer ?? false;
202
- const frontendPort = eopt?.frontendPort ?? 3000;
203
- this.webResourcesPath = eopt?.webResourcesPath ?? "";
204
- this.frontendURL = eopt?.frontendURL ?? (this._developmentServer ? `http://localhost:${frontendPort}` : `${this._electronFrontend}index.html`);
205
- this.appIconPath = path.join(this.webResourcesPath, eopt?.iconName ?? "appicon.ico");
206
- this.rpcConfig = ElectronRpcManager_1.ElectronRpcManager.initializeBackend(this._ipc, eopt?.rpcInterfaces);
207
- }
208
- opts = opts ?? {};
209
- opts.ipcHost = opts.ipcHost ?? {};
210
- opts.ipcHost.socket = this._ipc;
211
- await core_backend_1.NativeHost.startup(opts);
212
- if (core_backend_1.IpcHost.isValid) {
213
- ElectronDialogHandler.register();
214
- opts.electronHost?.ipcHandlers?.forEach((ipc) => ipc.register());
215
- }
216
- }
217
- }
218
- exports.ElectronHost = ElectronHost;
219
- ElectronHost._deprecatedSizeAndPosStoreKey = "windowPos";
220
- ElectronHost._sizeAndPosStoreKey = "windowSizeAndPos";
221
- ElectronHost._electronFrontend = "electron://frontend/";
222
- class ElectronDialogHandler extends core_backend_1.IpcHandler {
223
- get channelName() { return ElectronIpcInterface_1.dialogChannel; }
224
- async callDialog(method, ...args) {
225
- const dialog = ElectronHost.electron.dialog;
226
- const dialogMethod = dialog[method];
227
- if (typeof dialogMethod !== "function")
228
- throw new core_common_1.IModelError(core_bentley_1.IModelStatus.FunctionNotFound, `illegal electron dialog method`);
229
- return dialogMethod.call(dialog, ...args);
230
- }
231
- }
232
- function debounce(func, ms = 200) {
233
- let timeout;
234
- return function (...args) {
235
- clearTimeout(timeout);
236
- timeout = setTimeout(() => {
237
- func.apply(this, args);
238
- }, ms);
239
- };
240
- }
1
+ "use strict";
2
+ /*---------------------------------------------------------------------------------------------
3
+ * Copyright (c) Bentley Systems, Incorporated. All rights reserved.
4
+ * See LICENSE.md in the project root for license terms and full copyright notice.
5
+ *--------------------------------------------------------------------------------------------*/
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ exports.ElectronHost = void 0;
8
+ const fs = require("fs");
9
+ const path = require("path");
10
+ const core_bentley_1 = require("@itwin/core-bentley");
11
+ const core_backend_1 = require("@itwin/core-backend");
12
+ const core_common_1 = require("@itwin/core-common");
13
+ const ElectronRpcManager_1 = require("../common/ElectronRpcManager");
14
+ const ElectronIpcInterface_1 = require("../common/ElectronIpcInterface");
15
+ // cSpell:ignore signin devserver webcontents copyfile unmaximize eopt
16
+ class ElectronIpc {
17
+ addListener(channel, listener) {
18
+ ElectronHost.ipcMain.addListener(channel, listener);
19
+ return () => ElectronHost.ipcMain.removeListener(channel, listener);
20
+ }
21
+ removeListener(channel, listener) {
22
+ ElectronHost.ipcMain.removeListener(channel, listener);
23
+ }
24
+ send(channel, ...args) {
25
+ const window = ElectronHost.mainWindow ?? ElectronHost.electron.BrowserWindow.getAllWindows()[0];
26
+ window?.webContents.send(channel, ...args);
27
+ }
28
+ handle(channel, listener) {
29
+ ElectronHost.ipcMain.removeHandler(channel); // make sure there's not already a handler registered
30
+ ElectronHost.ipcMain.handle(channel, listener);
31
+ return () => ElectronHost.ipcMain.removeHandler(channel);
32
+ }
33
+ }
34
+ /**
35
+ * The backend for Electron-based desktop applications
36
+ * @beta
37
+ */
38
+ class ElectronHost {
39
+ static get ipcMain() { return this._electron?.ipcMain; }
40
+ static get app() { return this._electron?.app; }
41
+ static get electron() { return this._electron; }
42
+ constructor() { }
43
+ /**
44
+ * Converts an "electron://frontend/" URL to an absolute file path.
45
+ *
46
+ * We use this protocol in production builds because our frontend must be built with absolute URLs,
47
+ * however, since we're loading everything directly from the install directory, we cannot know the
48
+ * absolute path at build time.
49
+ */
50
+ static parseElectronUrl(requestedUrl) {
51
+ // Note that the "frontend/" path is arbitrary - this is just so we can handle *some* relative URLs...
52
+ let assetPath = requestedUrl.substring(this._electronFrontend.length);
53
+ if (assetPath.length === 0)
54
+ assetPath = "index.html";
55
+ assetPath = path.normalize(`${this.webResourcesPath}/${assetPath}`);
56
+ // File protocols don't follow symlinks, so we need to resolve this to a real path.
57
+ // However, if the file doesn't exist, it's fine to return an invalid path here - the request will just fail with net::ERR_FILE_NOT_FOUND
58
+ try {
59
+ assetPath = fs.realpathSync(assetPath);
60
+ }
61
+ catch (error) {
62
+ // eslint-disable-next-line no-console
63
+ // console.warn(`WARNING: Frontend requested "${requestedUrl}", but ${assetPath} does not exist`);
64
+ }
65
+ if (!assetPath.startsWith(this.webResourcesPath))
66
+ throw new Error(`Access to files outside installation directory (${this.webResourcesPath}) is prohibited`);
67
+ return assetPath;
68
+ }
69
+ static _openWindow(options) {
70
+ const webPreferences = {
71
+ ...options?.webPreferences,
72
+ // These web preference variables should not be overriden by the ElectronHostWindowOptions
73
+ preload: require.resolve(/* webpack: copyfile */ "./ElectronPreload.js"),
74
+ experimentalFeatures: false,
75
+ nodeIntegration: false,
76
+ contextIsolation: true,
77
+ sandbox: true,
78
+ nodeIntegrationInWorker: false,
79
+ nodeIntegrationInSubFrames: false,
80
+ };
81
+ const opts = {
82
+ ...options,
83
+ autoHideMenuBar: true,
84
+ icon: this.appIconPath,
85
+ webPreferences,
86
+ };
87
+ this._mainWindow = new (this.electron.BrowserWindow)(opts);
88
+ ElectronRpcManager_1.ElectronRpcConfiguration.targetWindowId = this._mainWindow.id;
89
+ this._mainWindow.on("closed", () => this._mainWindow = undefined);
90
+ this._mainWindow.loadURL(this.frontendURL); // eslint-disable-line @typescript-eslint/no-floating-promises
91
+ /** Monitors and saves main window size, position and maximized state */
92
+ if (options?.storeWindowName) {
93
+ const mainWindow = this._mainWindow;
94
+ const name = options.storeWindowName;
95
+ const saveWindowPosition = (key) => {
96
+ const bounds = mainWindow.getBounds();
97
+ core_backend_1.NativeHost.settingsStore.setData(`${key}-${name}`, JSON.stringify(bounds));
98
+ };
99
+ const saveMaximized = (maximized) => {
100
+ if (!maximized)
101
+ saveWindowPosition(this._deprecatedSizeAndPosStoreKey);
102
+ core_backend_1.NativeHost.settingsStore.setData(`windowMaximized-${name}`, maximized);
103
+ };
104
+ mainWindow.on("maximize", () => saveMaximized(true));
105
+ mainWindow.on("unmaximize", () => saveMaximized(false));
106
+ saveMaximized(mainWindow.isMaximized());
107
+ mainWindow.on("resized", () => saveWindowPosition(this._deprecatedSizeAndPosStoreKey));
108
+ mainWindow.on("moved", () => saveWindowPosition(this._deprecatedSizeAndPosStoreKey));
109
+ const debouncedSaveWindowSizeAndPos = debounce(() => saveWindowPosition(this._sizeAndPosStoreKey));
110
+ mainWindow.on("resize", () => debouncedSaveWindowSizeAndPos());
111
+ mainWindow.on("move", () => debouncedSaveWindowSizeAndPos());
112
+ saveWindowPosition(this._sizeAndPosStoreKey);
113
+ }
114
+ }
115
+ /** The "main" BrowserWindow for this application. */
116
+ static get mainWindow() { return this._mainWindow; }
117
+ /**
118
+ * Gets window size and position for a window, by name, from settings file, if present.
119
+ * @note Size and position values in the settings file will be updated differently depending on platform.
120
+ * On Linux values are only updated on window "unmaximize".
121
+ * On Windows and MacOS values are also updated on window manual resize or move.
122
+ * To get consistent behavior across different platforms, use [[ElectronHost.getWindowSizeAndPositionSetting]].
123
+ * @deprecated in 3.6. Use [[ElectronHost.getWindowSizeAndPositionSetting]].
124
+ */
125
+ static getWindowSizeSetting(windowName) {
126
+ const saved = core_backend_1.NativeHost.settingsStore.getString(`${this._deprecatedSizeAndPosStoreKey}-${windowName}`);
127
+ return saved ? JSON.parse(saved) : undefined;
128
+ }
129
+ /**
130
+ * Gets window size and position for a window, by name, from settings file, if present.
131
+ */
132
+ static getWindowSizeAndPositionSetting(windowName) {
133
+ const saved = core_backend_1.NativeHost.settingsStore.getString(`${this._sizeAndPosStoreKey}-${windowName}`);
134
+ return saved ? JSON.parse(saved) : undefined;
135
+ }
136
+ /** Gets "window maximized" flag for a window, by name, from settings file if present */
137
+ static getWindowMaximizedSetting(windowName) {
138
+ return core_backend_1.NativeHost.settingsStore.getBoolean(`windowMaximized-${windowName}`);
139
+ }
140
+ /**
141
+ * Open the main Window when the app is ready.
142
+ * @param windowOptions Options for constructing the main BrowserWindow. See: https://electronjs.org/docs/api/browser-window#new-browserwindowoptions
143
+ */
144
+ static async openMainWindow(windowOptions) {
145
+ const app = this.app;
146
+ // quit the application when all windows are closed (unless we're running on MacOS)
147
+ app.on("window-all-closed", () => {
148
+ if (process.platform !== "darwin")
149
+ app.quit();
150
+ });
151
+ // re-open the main window if it was closed and the app is re-activated (this is the normal MacOS behavior)
152
+ app.on("activate", () => {
153
+ if (!this._mainWindow)
154
+ this._openWindow(windowOptions);
155
+ });
156
+ if (this._developmentServer) {
157
+ // Occasionally, the electron backend may start before the webpack devserver has even started.
158
+ // If this happens, we'll just retry and keep reloading the page.
159
+ app.on("web-contents-created", (_e, webcontents) => {
160
+ webcontents.on("did-fail-load", async (_event, errorCode, _errorDescription, _validatedURL, isMainFrame) => {
161
+ // errorCode -102 is CONNECTION_REFUSED - see https://cs.chromium.org/chromium/src/net/base/net_error_list.h
162
+ if (isMainFrame && errorCode === -102) {
163
+ await core_bentley_1.BeDuration.wait(100);
164
+ webcontents.reload();
165
+ }
166
+ });
167
+ });
168
+ }
169
+ await app.whenReady();
170
+ if (!this._developmentServer) {
171
+ // handle any "electron://" requests and redirect them to "file://" URLs
172
+ this.electron.protocol.registerFileProtocol("electron", (request, callback) => callback(this.parseElectronUrl(request.url))); // eslint-disable-line @typescript-eslint/no-var-requires
173
+ }
174
+ this._openWindow(windowOptions);
175
+ }
176
+ static get isValid() { return this._ipc !== undefined; }
177
+ /**
178
+ * Initialize the backend of an Electron app.
179
+ * This method configures the backend for all of the inter-process communication (RPC and IPC) for an
180
+ * Electron app. It should be called from your Electron main function.
181
+ * @param opts Options that control aspects of your backend.
182
+ * @note This method must only be called from the backend of an Electron app (i.e. when [ProcessDetector.isElectronAppBackend]($bentley) is `true`).
183
+ */
184
+ static async startup(opts) {
185
+ if (!core_bentley_1.ProcessDetector.isElectronAppBackend)
186
+ throw new Error("Not running under Electron");
187
+ if (!this.isValid) {
188
+ this._electron = require("electron");
189
+ this._ipc = new ElectronIpc();
190
+ const app = this.app;
191
+ if (!app.isReady())
192
+ this.electron.protocol.registerSchemesAsPrivileged([{
193
+ scheme: "electron",
194
+ privileges: {
195
+ standard: true,
196
+ secure: true,
197
+ supportFetchAPI: true,
198
+ },
199
+ }]);
200
+ const eopt = opts?.electronHost;
201
+ this._developmentServer = eopt?.developmentServer ?? false;
202
+ const frontendPort = eopt?.frontendPort ?? 3000;
203
+ this.webResourcesPath = eopt?.webResourcesPath ?? "";
204
+ this.frontendURL = eopt?.frontendURL ?? (this._developmentServer ? `http://localhost:${frontendPort}` : `${this._electronFrontend}index.html`);
205
+ this.appIconPath = path.join(this.webResourcesPath, eopt?.iconName ?? "appicon.ico");
206
+ this.rpcConfig = ElectronRpcManager_1.ElectronRpcManager.initializeBackend(this._ipc, eopt?.rpcInterfaces);
207
+ }
208
+ opts = opts ?? {};
209
+ opts.ipcHost = opts.ipcHost ?? {};
210
+ opts.ipcHost.socket = this._ipc;
211
+ await core_backend_1.NativeHost.startup(opts);
212
+ if (core_backend_1.IpcHost.isValid) {
213
+ ElectronDialogHandler.register();
214
+ opts.electronHost?.ipcHandlers?.forEach((ipc) => ipc.register());
215
+ }
216
+ }
217
+ }
218
+ ElectronHost._deprecatedSizeAndPosStoreKey = "windowPos";
219
+ ElectronHost._sizeAndPosStoreKey = "windowSizeAndPos";
220
+ ElectronHost._electronFrontend = "electron://frontend/";
221
+ exports.ElectronHost = ElectronHost;
222
+ class ElectronDialogHandler extends core_backend_1.IpcHandler {
223
+ get channelName() { return ElectronIpcInterface_1.dialogChannel; }
224
+ async callDialog(method, ...args) {
225
+ const dialog = ElectronHost.electron.dialog;
226
+ const dialogMethod = dialog[method];
227
+ if (typeof dialogMethod !== "function")
228
+ throw new core_common_1.IModelError(core_bentley_1.IModelStatus.FunctionNotFound, `illegal electron dialog method`);
229
+ return dialogMethod.call(dialog, ...args);
230
+ }
231
+ }
232
+ function debounce(func, ms = 200) {
233
+ let timeout;
234
+ return function (...args) {
235
+ clearTimeout(timeout);
236
+ timeout = setTimeout(() => {
237
+ func.apply(this, args);
238
+ }, ms);
239
+ };
240
+ }
241
241
  //# sourceMappingURL=ElectronHost.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"ElectronHost.js","sourceRoot":"","sources":["../../../src/backend/ElectronHost.ts"],"names":[],"mappings":";AAAA;;;+FAG+F;;;AAM/F,yBAAyB;AACzB,6BAA6B;AAC7B,sDAAgF;AAChF,sDAAsF;AACtF,oDAA0I;AAC1I,qEAA4F;AAC5F,yEAAmF;AAEnF,sEAAsE;AAEtE,MAAM,WAAW;IACR,WAAW,CAAC,OAAe,EAAE,QAAqB;QACvD,YAAY,CAAC,OAAO,CAAC,WAAW,CAAC,OAAO,EAAE,QAAQ,CAAC,CAAC;QACpD,OAAO,GAAG,EAAE,CAAC,YAAY,CAAC,OAAO,CAAC,cAAc,CAAC,OAAO,EAAE,QAAQ,CAAC,CAAC;IACtE,CAAC;IACM,cAAc,CAAC,OAAe,EAAE,QAAqB;QAC1D,YAAY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YAAY,CAAC,OAAO,CAAC,CAAC;QACtB,OAAO,GAAG,UAAU,CAAC,GAAG,EAAE;YACxB,IAAI,CAAC,KAAK,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;QACzB,CAAC,EAAE,EAAE,CAAC,CAAC;IACT,CAAC,CAAC;AACJ,CAAC","sourcesContent":["/*---------------------------------------------------------------------------------------------\r\n* Copyright (c) Bentley Systems, Incorporated. All rights reserved.\r\n* See LICENSE.md in the project root for license terms and full copyright notice.\r\n*--------------------------------------------------------------------------------------------*/\r\n\r\n// Note: only import *types* from electron so this file can be imported by apps that sometimes use Electron and sometimes not.\r\nimport type { BrowserWindow, BrowserWindowConstructorOptions, WebPreferences } from \"electron\";\r\nimport type * as ElectronModule from \"electron\";\r\n\r\nimport * as fs from \"fs\";\r\nimport * as path from \"path\";\r\nimport { BeDuration, IModelStatus, ProcessDetector } from \"@itwin/core-bentley\";\r\nimport { IpcHandler, IpcHost, NativeHost, NativeHostOpts } from \"@itwin/core-backend\";\r\nimport { IModelError, IpcListener, IpcSocketBackend, RemoveFunction, RpcConfiguration, RpcInterfaceDefinition } from \"@itwin/core-common\";\r\nimport { ElectronRpcConfiguration, ElectronRpcManager } from \"../common/ElectronRpcManager\";\r\nimport { dialogChannel, DialogModuleMethod } from \"../common/ElectronIpcInterface\";\r\n\r\n// cSpell:ignore signin devserver webcontents copyfile unmaximize eopt\r\n\r\nclass ElectronIpc implements IpcSocketBackend {\r\n public addListener(channel: string, listener: IpcListener): RemoveFunction {\r\n ElectronHost.ipcMain.addListener(channel, listener);\r\n return () => ElectronHost.ipcMain.removeListener(channel, listener);\r\n }\r\n public removeListener(channel: string, listener: IpcListener) {\r\n ElectronHost.ipcMain.removeListener(channel, listener);\r\n }\r\n public send(channel: string, ...args: any[]): void {\r\n const window = ElectronHost.mainWindow ?? ElectronHost.electron.BrowserWindow.getAllWindows()[0];\r\n window?.webContents.send(channel, ...args);\r\n }\r\n public handle(channel: string, listener: (evt: any, ...args: any[]) => Promise<any>): RemoveFunction {\r\n ElectronHost.ipcMain.removeHandler(channel); // make sure there's not already a handler registered\r\n ElectronHost.ipcMain.handle(channel, listener);\r\n return () => ElectronHost.ipcMain.removeHandler(channel);\r\n }\r\n}\r\n\r\n/**\r\n * Options for [[ElectronHost.startup]]\r\n * @beta\r\n */\r\nexport interface ElectronHostOptions {\r\n /** the path to find web resources */\r\n webResourcesPath?: string;\r\n /** filename for the app's icon, relative to [[webResourcesPath]] */\r\n iconName?: string;\r\n /** name of frontend url to open. */\r\n frontendURL?: string;\r\n /** use a development server rather than the \"electron\" protocol for loading frontend (see https://www.electronjs.org/docs/api/protocol) */\r\n developmentServer?: boolean;\r\n /** port number for development server. Default is 3000 */\r\n frontendPort?: number;\r\n /** list of RPC interface definitions to register */\r\n rpcInterfaces?: RpcInterfaceDefinition[];\r\n /** list of [IpcHandler]($common) classes to register */\r\n ipcHandlers?: (typeof IpcHandler)[];\r\n}\r\n\r\n/** @beta */\r\nexport interface ElectronHostOpts extends NativeHostOpts {\r\n electronHost?: ElectronHostOptions;\r\n}\r\n\r\n/** @beta */\r\nexport interface ElectronHostWindowOptions extends BrowserWindowConstructorOptions {\r\n storeWindowName?: string;\r\n /** The style of window title bar. Default is `default`. */\r\n titleBarStyle?: (\"default\" | \"hidden\" | \"hiddenInset\" | \"customButtonsOnHover\");\r\n}\r\n\r\n/** the size and position of a window as stored in the settings file.\r\n * @beta\r\n */\r\nexport interface WindowSizeAndPositionProps {\r\n width: number;\r\n height: number;\r\n x: number;\r\n y: number;\r\n}\r\n\r\n/**\r\n * The backend for Electron-based desktop applications\r\n * @beta\r\n */\r\nexport class ElectronHost {\r\n private static readonly _deprecatedSizeAndPosStoreKey = \"windowPos\";\r\n private static readonly _sizeAndPosStoreKey = \"windowSizeAndPos\";\r\n\r\n private static _ipc: ElectronIpc;\r\n private static _developmentServer: boolean;\r\n private static _electron: typeof ElectronModule;\r\n private static _electronFrontend = \"electron://frontend/\";\r\n private static _mainWindow?: BrowserWindow;\r\n public static webResourcesPath: string;\r\n public static appIconPath: string;\r\n public static frontendURL: string;\r\n public static rpcConfig: RpcConfiguration;\r\n public static get ipcMain() { return this._electron?.ipcMain; }\r\n public static get app() { return this._electron?.app; }\r\n public static get electron() { return this._electron; }\r\n\r\n private constructor() { }\r\n\r\n /**\r\n * Converts an \"electron://frontend/\" URL to an absolute file path.\r\n *\r\n * We use this protocol in production builds because our frontend must be built with absolute URLs,\r\n * however, since we're loading everything directly from the install directory, we cannot know the\r\n * absolute path at build time.\r\n */\r\n private static parseElectronUrl(requestedUrl: string): string {\r\n // Note that the \"frontend/\" path is arbitrary - this is just so we can handle *some* relative URLs...\r\n let assetPath = requestedUrl.substring(this._electronFrontend.length);\r\n if (assetPath.length === 0)\r\n assetPath = \"index.html\";\r\n assetPath = path.normalize(`${this.webResourcesPath}/${assetPath}`);\r\n // File protocols don't follow symlinks, so we need to resolve this to a real path.\r\n // However, if the file doesn't exist, it's fine to return an invalid path here - the request will just fail with net::ERR_FILE_NOT_FOUND\r\n try {\r\n assetPath = fs.realpathSync(assetPath);\r\n } catch (error) {\r\n // eslint-disable-next-line no-console\r\n // console.warn(`WARNING: Frontend requested \"${requestedUrl}\", but ${assetPath} does not exist`);\r\n }\r\n if (!assetPath.startsWith(this.webResourcesPath))\r\n throw new Error(`Access to files outside installation directory (${this.webResourcesPath}) is prohibited`);\r\n return assetPath;\r\n }\r\n\r\n private static _openWindow(options?: ElectronHostWindowOptions) {\r\n const webPreferences: WebPreferences = {\r\n ...options?.webPreferences,\r\n\r\n // These web preference variables should not be overriden by the ElectronHostWindowOptions\r\n preload: require.resolve(/* webpack: copyfile */\"./ElectronPreload.js\"),\r\n experimentalFeatures: false,\r\n nodeIntegration: false,\r\n contextIsolation: true,\r\n sandbox: true,\r\n nodeIntegrationInWorker: false,\r\n nodeIntegrationInSubFrames: false,\r\n };\r\n\r\n const opts: BrowserWindowConstructorOptions = {\r\n ...options,\r\n autoHideMenuBar: true,\r\n icon: this.appIconPath,\r\n webPreferences,\r\n };\r\n\r\n this._mainWindow = new (this.electron.BrowserWindow)(opts);\r\n ElectronRpcConfiguration.targetWindowId = this._mainWindow.id;\r\n this._mainWindow.on(\"closed\", () => this._mainWindow = undefined);\r\n this._mainWindow.loadURL(this.frontendURL); // eslint-disable-line @typescript-eslint/no-floating-promises\r\n\r\n /** Monitors and saves main window size, position and maximized state */\r\n if (options?.storeWindowName) {\r\n const mainWindow = this._mainWindow;\r\n const name = options.storeWindowName;\r\n const saveWindowPosition = (key: string) => {\r\n const bounds: WindowSizeAndPositionProps = mainWindow.getBounds();\r\n NativeHost.settingsStore.setData(`${key}-${name}`, JSON.stringify(bounds));\r\n };\r\n const saveMaximized = (maximized: boolean) => {\r\n if (!maximized)\r\n saveWindowPosition(this._deprecatedSizeAndPosStoreKey);\r\n NativeHost.settingsStore.setData(`windowMaximized-${name}`, maximized);\r\n };\r\n\r\n mainWindow.on(\"maximize\", () => saveMaximized(true));\r\n mainWindow.on(\"unmaximize\", () => saveMaximized(false));\r\n saveMaximized(mainWindow.isMaximized());\r\n\r\n mainWindow.on(\"resized\", () => saveWindowPosition(this._deprecatedSizeAndPosStoreKey));\r\n mainWindow.on(\"moved\", () => saveWindowPosition(this._deprecatedSizeAndPosStoreKey));\r\n\r\n const debouncedSaveWindowSizeAndPos = debounce(() => saveWindowPosition(this._sizeAndPosStoreKey));\r\n mainWindow.on(\"resize\", () => debouncedSaveWindowSizeAndPos());\r\n mainWindow.on(\"move\", () => debouncedSaveWindowSizeAndPos());\r\n saveWindowPosition(this._sizeAndPosStoreKey);\r\n }\r\n }\r\n\r\n /** The \"main\" BrowserWindow for this application. */\r\n public static get mainWindow() { return this._mainWindow; }\r\n\r\n /**\r\n * Gets window size and position for a window, by name, from settings file, if present.\r\n * @note Size and position values in the settings file will be updated differently depending on platform.\r\n * On Linux values are only updated on window \"unmaximize\".\r\n * On Windows and MacOS values are also updated on window manual resize or move.\r\n * To get consistent behavior across different platforms, use [[ElectronHost.getWindowSizeAndPositionSetting]].\r\n * @deprecated in 3.6. Use [[ElectronHost.getWindowSizeAndPositionSetting]].\r\n */\r\n public static getWindowSizeSetting(windowName: string): WindowSizeAndPositionProps | undefined {\r\n const saved = NativeHost.settingsStore.getString(`${this._deprecatedSizeAndPosStoreKey}-${windowName}`);\r\n return saved ? JSON.parse(saved) as WindowSizeAndPositionProps : undefined;\r\n }\r\n\r\n /**\r\n * Gets window size and position for a window, by name, from settings file, if present.\r\n */\r\n public static getWindowSizeAndPositionSetting(windowName: string): WindowSizeAndPositionProps | undefined {\r\n const saved = NativeHost.settingsStore.getString(`${this._sizeAndPosStoreKey}-${windowName}`);\r\n return saved ? JSON.parse(saved) as WindowSizeAndPositionProps : undefined;\r\n }\r\n\r\n /** Gets \"window maximized\" flag for a window, by name, from settings file if present */\r\n public static getWindowMaximizedSetting(windowName: string): boolean | undefined {\r\n return NativeHost.settingsStore.getBoolean(`windowMaximized-${windowName}`);\r\n }\r\n\r\n /**\r\n * Open the main Window when the app is ready.\r\n * @param windowOptions Options for constructing the main BrowserWindow. See: https://electronjs.org/docs/api/browser-window#new-browserwindowoptions\r\n */\r\n public static async openMainWindow(windowOptions?: ElectronHostWindowOptions): Promise<void> {\r\n const app = this.app;\r\n // quit the application when all windows are closed (unless we're running on MacOS)\r\n app.on(\"window-all-closed\", () => {\r\n if (process.platform !== \"darwin\")\r\n app.quit();\r\n });\r\n\r\n // re-open the main window if it was closed and the app is re-activated (this is the normal MacOS behavior)\r\n app.on(\"activate\", () => {\r\n if (!this._mainWindow)\r\n this._openWindow(windowOptions);\r\n });\r\n\r\n if (this._developmentServer) {\r\n // Occasionally, the electron backend may start before the webpack devserver has even started.\r\n // If this happens, we'll just retry and keep reloading the page.\r\n app.on(\"web-contents-created\", (_e, webcontents) => {\r\n webcontents.on(\"did-fail-load\", async (_event, errorCode, _errorDescription, _validatedURL, isMainFrame) => {\r\n // errorCode -102 is CONNECTION_REFUSED - see https://cs.chromium.org/chromium/src/net/base/net_error_list.h\r\n if (isMainFrame && errorCode === -102) {\r\n await BeDuration.wait(100);\r\n webcontents.reload();\r\n }\r\n });\r\n });\r\n }\r\n\r\n await app.whenReady();\r\n\r\n if (!this._developmentServer) {\r\n // handle any \"electron://\" requests and redirect them to \"file://\" URLs\r\n this.electron.protocol.registerFileProtocol(\"electron\", (request, callback) => callback(this.parseElectronUrl(request.url))); // eslint-disable-line @typescript-eslint/no-var-requires\r\n }\r\n\r\n this._openWindow(windowOptions);\r\n }\r\n\r\n public static get isValid() { return this._ipc !== undefined; }\r\n\r\n /**\r\n * Initialize the backend of an Electron app.\r\n * This method configures the backend for all of the inter-process communication (RPC and IPC) for an\r\n * Electron app. It should be called from your Electron main function.\r\n * @param opts Options that control aspects of your backend.\r\n * @note This method must only be called from the backend of an Electron app (i.e. when [ProcessDetector.isElectronAppBackend]($bentley) is `true`).\r\n */\r\n public static async startup(opts?: ElectronHostOpts) {\r\n if (!ProcessDetector.isElectronAppBackend)\r\n throw new Error(\"Not running under Electron\");\r\n\r\n if (!this.isValid) {\r\n this._electron = require(\"electron\");\r\n this._ipc = new ElectronIpc();\r\n const app = this.app;\r\n if (!app.isReady())\r\n this.electron.protocol.registerSchemesAsPrivileged([{\r\n scheme: \"electron\",\r\n privileges: {\r\n standard: true,\r\n secure: true,\r\n supportFetchAPI: true,\r\n },\r\n }]);\r\n const eopt = opts?.electronHost;\r\n this._developmentServer = eopt?.developmentServer ?? false;\r\n const frontendPort = eopt?.frontendPort ?? 3000;\r\n this.webResourcesPath = eopt?.webResourcesPath ?? \"\";\r\n this.frontendURL = eopt?.frontendURL ?? (this._developmentServer ? `http://localhost:${frontendPort}` : `${this._electronFrontend}index.html`);\r\n this.appIconPath = path.join(this.webResourcesPath, eopt?.iconName ?? \"appicon.ico\");\r\n this.rpcConfig = ElectronRpcManager.initializeBackend(this._ipc, eopt?.rpcInterfaces);\r\n }\r\n\r\n opts = opts ?? {};\r\n opts.ipcHost = opts.ipcHost ?? {};\r\n opts.ipcHost.socket = this._ipc;\r\n await NativeHost.startup(opts);\r\n if (IpcHost.isValid) {\r\n ElectronDialogHandler.register();\r\n opts.electronHost?.ipcHandlers?.forEach((ipc) => ipc.register());\r\n }\r\n }\r\n}\r\n\r\nclass ElectronDialogHandler extends IpcHandler {\r\n public get channelName() { return dialogChannel; }\r\n public async callDialog(method: DialogModuleMethod, ...args: any) {\r\n const dialog = ElectronHost.electron.dialog;\r\n const dialogMethod = dialog[method] as Function;\r\n if (typeof dialogMethod !== \"function\")\r\n throw new IModelError(IModelStatus.FunctionNotFound, `illegal electron dialog method`);\r\n\r\n return dialogMethod.call(dialog, ...args);\r\n }\r\n}\r\n\r\nfunction debounce(func: Function, ms: number = 200) {\r\n let timeout: NodeJS.Timeout;\r\n return function (this: any, ...args: any[]) {\r\n clearTimeout(timeout);\r\n timeout = setTimeout(() => {\r\n func.apply(this, args);\r\n }, ms);\r\n };\r\n}\r\n"]}
1
+ {"version":3,"file":"ElectronHost.js","sourceRoot":"","sources":["../../../src/backend/ElectronHost.ts"],"names":[],"mappings":";AAAA;;;+FAG+F;;;AAM/F,yBAAyB;AACzB,6BAA6B;AAC7B,sDAAgF;AAChF,sDAAsF;AACtF,oDAA0I;AAC1I,qEAA4F;AAC5F,yEAAmF;AAEnF,sEAAsE;AAEtE,MAAM,WAAW;IACR,WAAW,CAAC,OAAe,EAAE,QAAqB;QACvD,YAAY,CAAC,OAAO,CAAC,WAAW,CAAC,OAAO,EAAE,QAAQ,CAAC,CAAC;QACpD,OAAO,GAAG,EAAE,CAAC,YAAY,CAAC,OAAO,CAAC,cAAc,CAAC,OAAO,EAAE,QAAQ,CAAC,CAAC;IACtE,CAAC;IACM,cAAc,CAAC,OAAe,EAAE,QAAqB;QAC1D,YAAY,CAAC,OAAO,CAAC,cAAc,CAAC,OAAO,EAAE,QAAQ,CAAC,CAAC;IACzD,CAAC;IACM,IAAI,CAAC,OAAe,EAAE,GAAG,IAAW;QACzC,MAAM,MAAM,GAAG,YAAY,CAAC,UAAU,IA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CAAC;IACT,CAAC,CAAC;AACJ,CAAC","sourcesContent":["/*---------------------------------------------------------------------------------------------\r\n* Copyright (c) Bentley Systems, Incorporated. All rights reserved.\r\n* See LICENSE.md in the project root for license terms and full copyright notice.\r\n*--------------------------------------------------------------------------------------------*/\r\n\r\n// Note: only import *types* from electron so this file can be imported by apps that sometimes use Electron and sometimes not.\r\nimport type { BrowserWindow, BrowserWindowConstructorOptions, WebPreferences } from \"electron\";\r\nimport type * as ElectronModule from \"electron\";\r\n\r\nimport * as fs from \"fs\";\r\nimport * as path from \"path\";\r\nimport { BeDuration, IModelStatus, ProcessDetector } from \"@itwin/core-bentley\";\r\nimport { IpcHandler, IpcHost, NativeHost, NativeHostOpts } from \"@itwin/core-backend\";\r\nimport { IModelError, IpcListener, IpcSocketBackend, RemoveFunction, RpcConfiguration, RpcInterfaceDefinition } from \"@itwin/core-common\";\r\nimport { ElectronRpcConfiguration, ElectronRpcManager } from \"../common/ElectronRpcManager\";\r\nimport { dialogChannel, DialogModuleMethod } from \"../common/ElectronIpcInterface\";\r\n\r\n// cSpell:ignore signin devserver webcontents copyfile unmaximize eopt\r\n\r\nclass ElectronIpc implements IpcSocketBackend {\r\n public addListener(channel: string, listener: IpcListener): RemoveFunction {\r\n ElectronHost.ipcMain.addListener(channel, listener);\r\n return () => ElectronHost.ipcMain.removeListener(channel, listener);\r\n }\r\n public removeListener(channel: string, listener: IpcListener) {\r\n ElectronHost.ipcMain.removeListener(channel, listener);\r\n }\r\n public send(channel: string, ...args: any[]): void {\r\n const window = ElectronHost.mainWindow ?? ElectronHost.electron.BrowserWindow.getAllWindows()[0];\r\n window?.webContents.send(channel, ...args);\r\n }\r\n public handle(channel: string, listener: (evt: any, ...args: any[]) => Promise<any>): RemoveFunction {\r\n ElectronHost.ipcMain.removeHandler(channel); // make sure there's not already a handler registered\r\n ElectronHost.ipcMain.handle(channel, listener);\r\n return () => ElectronHost.ipcMain.removeHandler(channel);\r\n }\r\n}\r\n\r\n/**\r\n * Options for [[ElectronHost.startup]]\r\n * @beta\r\n */\r\nexport interface ElectronHostOptions {\r\n /** the path to find web resources */\r\n webResourcesPath?: string;\r\n /** filename for the app's icon, relative to [[webResourcesPath]] */\r\n iconName?: string;\r\n /** name of frontend url to open. */\r\n frontendURL?: string;\r\n /** use a development server rather than the \"electron\" protocol for loading frontend (see https://www.electronjs.org/docs/api/protocol) */\r\n developmentServer?: boolean;\r\n /** port number for development server. Default is 3000 */\r\n frontendPort?: number;\r\n /** list of RPC interface definitions to register */\r\n rpcInterfaces?: RpcInterfaceDefinition[];\r\n /** list of [IpcHandler]($common) classes to register */\r\n ipcHandlers?: (typeof IpcHandler)[];\r\n}\r\n\r\n/** @beta */\r\nexport interface ElectronHostOpts extends NativeHostOpts {\r\n electronHost?: ElectronHostOptions;\r\n}\r\n\r\n/** @beta */\r\nexport interface ElectronHostWindowOptions extends BrowserWindowConstructorOptions {\r\n storeWindowName?: string;\r\n /** The style of window title bar. Default is `default`. */\r\n titleBarStyle?: (\"default\" | \"hidden\" | \"hiddenInset\" | \"customButtonsOnHover\");\r\n}\r\n\r\n/** the size and position of a window as stored in the settings file.\r\n * @beta\r\n */\r\nexport interface WindowSizeAndPositionProps {\r\n width: number;\r\n height: number;\r\n x: number;\r\n y: number;\r\n}\r\n\r\n/**\r\n * The backend for Electron-based desktop applications\r\n * @beta\r\n */\r\nexport class ElectronHost {\r\n private static readonly _deprecatedSizeAndPosStoreKey = \"windowPos\";\r\n private static readonly _sizeAndPosStoreKey = \"windowSizeAndPos\";\r\n\r\n private static _ipc: ElectronIpc;\r\n private static _developmentServer: boolean;\r\n private static _electron: typeof ElectronModule;\r\n private static _electronFrontend = \"electron://frontend/\";\r\n private static _mainWindow?: BrowserWindow;\r\n public static webResourcesPath: string;\r\n public static appIconPath: string;\r\n public static frontendURL: string;\r\n public static rpcConfig: RpcConfiguration;\r\n public static get ipcMain() { return this._electron?.ipcMain; }\r\n public static get app() { return this._electron?.app; }\r\n public static get electron() { return this._electron; }\r\n\r\n private constructor() { }\r\n\r\n /**\r\n * Converts an \"electron://frontend/\" URL to an absolute file path.\r\n *\r\n * We use this protocol in production builds because our frontend must be built with absolute URLs,\r\n * however, since we're loading everything directly from the install directory, we cannot know the\r\n * absolute path at build time.\r\n */\r\n private static parseElectronUrl(requestedUrl: string): string {\r\n // Note that the \"frontend/\" path is arbitrary - this is just so we can handle *some* relative URLs...\r\n let assetPath = requestedUrl.substring(this._electronFrontend.length);\r\n if (assetPath.length === 0)\r\n assetPath = \"index.html\";\r\n assetPath = path.normalize(`${this.webResourcesPath}/${assetPath}`);\r\n // File protocols don't follow symlinks, so we need to resolve this to a real path.\r\n // However, if the file doesn't exist, it's fine to return an invalid path here - the request will just fail with net::ERR_FILE_NOT_FOUND\r\n try {\r\n assetPath = fs.realpathSync(assetPath);\r\n } catch (error) {\r\n // eslint-disable-next-line no-console\r\n // console.warn(`WARNING: Frontend requested \"${requestedUrl}\", but ${assetPath} does not exist`);\r\n }\r\n if (!assetPath.startsWith(this.webResourcesPath))\r\n throw new Error(`Access to files outside installation directory (${this.webResourcesPath}) is prohibited`);\r\n return assetPath;\r\n }\r\n\r\n private static _openWindow(options?: ElectronHostWindowOptions) {\r\n const webPreferences: WebPreferences = {\r\n ...options?.webPreferences,\r\n\r\n // These web preference variables should not be overriden by the ElectronHostWindowOptions\r\n preload: require.resolve(/* webpack: copyfile */\"./ElectronPreload.js\"),\r\n experimentalFeatures: false,\r\n nodeIntegration: false,\r\n contextIsolation: true,\r\n sandbox: true,\r\n nodeIntegrationInWorker: false,\r\n nodeIntegrationInSubFrames: false,\r\n };\r\n\r\n const opts: BrowserWindowConstructorOptions = {\r\n ...options,\r\n autoHideMenuBar: true,\r\n icon: this.appIconPath,\r\n webPreferences,\r\n };\r\n\r\n this._mainWindow = new (this.electron.BrowserWindow)(opts);\r\n ElectronRpcConfiguration.targetWindowId = this._mainWindow.id;\r\n this._mainWindow.on(\"closed\", () => this._mainWindow = undefined);\r\n this._mainWindow.loadURL(this.frontendURL); // eslint-disable-line @typescript-eslint/no-floating-promises\r\n\r\n /** Monitors and saves main window size, position and maximized state */\r\n if (options?.storeWindowName) {\r\n const mainWindow = this._mainWindow;\r\n const name = options.storeWindowName;\r\n const saveWindowPosition = (key: string) => {\r\n const bounds: WindowSizeAndPositionProps = mainWindow.getBounds();\r\n NativeHost.settingsStore.setData(`${key}-${name}`, JSON.stringify(bounds));\r\n };\r\n const saveMaximized = (maximized: boolean) => {\r\n if (!maximized)\r\n saveWindowPosition(this._deprecatedSizeAndPosStoreKey);\r\n NativeHost.settingsStore.setData(`windowMaximized-${name}`, maximized);\r\n };\r\n\r\n mainWindow.on(\"maximize\", () => saveMaximized(true));\r\n mainWindow.on(\"unmaximize\", () => saveMaximized(false));\r\n saveMaximized(mainWindow.isMaximized());\r\n\r\n mainWindow.on(\"resized\", () => saveWindowPosition(this._deprecatedSizeAndPosStoreKey));\r\n mainWindow.on(\"moved\", () => saveWindowPosition(this._deprecatedSizeAndPosStoreKey));\r\n\r\n const debouncedSaveWindowSizeAndPos = debounce(() => saveWindowPosition(this._sizeAndPosStoreKey));\r\n mainWindow.on(\"resize\", () => debouncedSaveWindowSizeAndPos());\r\n mainWindow.on(\"move\", () => debouncedSaveWindowSizeAndPos());\r\n saveWindowPosition(this._sizeAndPosStoreKey);\r\n }\r\n }\r\n\r\n /** The \"main\" BrowserWindow for this application. */\r\n public static get mainWindow() { return this._mainWindow; }\r\n\r\n /**\r\n * Gets window size and position for a window, by name, from settings file, if present.\r\n * @note Size and position values in the settings file will be updated differently depending on platform.\r\n * On Linux values are only updated on window \"unmaximize\".\r\n * On Windows and MacOS values are also updated on window manual resize or move.\r\n * To get consistent behavior across different platforms, use [[ElectronHost.getWindowSizeAndPositionSetting]].\r\n * @deprecated in 3.6. Use [[ElectronHost.getWindowSizeAndPositionSetting]].\r\n */\r\n public static getWindowSizeSetting(windowName: string): WindowSizeAndPositionProps | undefined {\r\n const saved = NativeHost.settingsStore.getString(`${this._deprecatedSizeAndPosStoreKey}-${windowName}`);\r\n return saved ? JSON.parse(saved) as WindowSizeAndPositionProps : undefined;\r\n }\r\n\r\n /**\r\n * Gets window size and position for a window, by name, from settings file, if present.\r\n */\r\n public static getWindowSizeAndPositionSetting(windowName: string): WindowSizeAndPositionProps | undefined {\r\n const saved = NativeHost.settingsStore.getString(`${this._sizeAndPosStoreKey}-${windowName}`);\r\n return saved ? JSON.parse(saved) as WindowSizeAndPositionProps : undefined;\r\n }\r\n\r\n /** Gets \"window maximized\" flag for a window, by name, from settings file if present */\r\n public static getWindowMaximizedSetting(windowName: string): boolean | undefined {\r\n return NativeHost.settingsStore.getBoolean(`windowMaximized-${windowName}`);\r\n }\r\n\r\n /**\r\n * Open the main Window when the app is ready.\r\n * @param windowOptions Options for constructing the main BrowserWindow. See: https://electronjs.org/docs/api/browser-window#new-browserwindowoptions\r\n */\r\n public static async openMainWindow(windowOptions?: ElectronHostWindowOptions): Promise<void> {\r\n const app = this.app;\r\n // quit the application when all windows are closed (unless we're running on MacOS)\r\n app.on(\"window-all-closed\", () => {\r\n if (process.platform !== \"darwin\")\r\n app.quit();\r\n });\r\n\r\n // re-open the main window if it was closed and the app is re-activated (this is the normal MacOS behavior)\r\n app.on(\"activate\", () => {\r\n if (!this._mainWindow)\r\n this._openWindow(windowOptions);\r\n });\r\n\r\n if (this._developmentServer) {\r\n // Occasionally, the electron backend may start before the webpack devserver has even started.\r\n // If this happens, we'll just retry and keep reloading the page.\r\n app.on(\"web-contents-created\", (_e, webcontents) => {\r\n webcontents.on(\"did-fail-load\", async (_event, errorCode, _errorDescription, _validatedURL, isMainFrame) => {\r\n // errorCode -102 is CONNECTION_REFUSED - see https://cs.chromium.org/chromium/src/net/base/net_error_list.h\r\n if (isMainFrame && errorCode === -102) {\r\n await BeDuration.wait(100);\r\n webcontents.reload();\r\n }\r\n });\r\n });\r\n }\r\n\r\n await app.whenReady();\r\n\r\n if (!this._developmentServer) {\r\n // handle any \"electron://\" requests and redirect them to \"file://\" URLs\r\n this.electron.protocol.registerFileProtocol(\"electron\", (request, callback) => callback(this.parseElectronUrl(request.url))); // eslint-disable-line @typescript-eslint/no-var-requires\r\n }\r\n\r\n this._openWindow(windowOptions);\r\n }\r\n\r\n public static get isValid() { return this._ipc !== undefined; }\r\n\r\n /**\r\n * Initialize the backend of an Electron app.\r\n * This method configures the backend for all of the inter-process communication (RPC and IPC) for an\r\n * Electron app. It should be called from your Electron main function.\r\n * @param opts Options that control aspects of your backend.\r\n * @note This method must only be called from the backend of an Electron app (i.e. when [ProcessDetector.isElectronAppBackend]($bentley) is `true`).\r\n */\r\n public static async startup(opts?: ElectronHostOpts) {\r\n if (!ProcessDetector.isElectronAppBackend)\r\n throw new Error(\"Not running under Electron\");\r\n\r\n if (!this.isValid) {\r\n this._electron = require(\"electron\");\r\n this._ipc = new ElectronIpc();\r\n const app = this.app;\r\n if (!app.isReady())\r\n this.electron.protocol.registerSchemesAsPrivileged([{\r\n scheme: \"electron\",\r\n privileges: {\r\n standard: true,\r\n secure: true,\r\n supportFetchAPI: true,\r\n },\r\n }]);\r\n const eopt = opts?.electronHost;\r\n this._developmentServer = eopt?.developmentServer ?? false;\r\n const frontendPort = eopt?.frontendPort ?? 3000;\r\n this.webResourcesPath = eopt?.webResourcesPath ?? \"\";\r\n this.frontendURL = eopt?.frontendURL ?? (this._developmentServer ? `http://localhost:${frontendPort}` : `${this._electronFrontend}index.html`);\r\n this.appIconPath = path.join(this.webResourcesPath, eopt?.iconName ?? \"appicon.ico\");\r\n this.rpcConfig = ElectronRpcManager.initializeBackend(this._ipc, eopt?.rpcInterfaces);\r\n }\r\n\r\n opts = opts ?? {};\r\n opts.ipcHost = opts.ipcHost ?? {};\r\n opts.ipcHost.socket = this._ipc;\r\n await NativeHost.startup(opts);\r\n if (IpcHost.isValid) {\r\n ElectronDialogHandler.register();\r\n opts.electronHost?.ipcHandlers?.forEach((ipc) => ipc.register());\r\n }\r\n }\r\n}\r\n\r\nclass ElectronDialogHandler extends IpcHandler {\r\n public get channelName() { return dialogChannel; }\r\n public async callDialog(method: DialogModuleMethod, ...args: any) {\r\n const dialog = ElectronHost.electron.dialog;\r\n const dialogMethod = dialog[method] as Function;\r\n if (typeof dialogMethod !== \"function\")\r\n throw new IModelError(IModelStatus.FunctionNotFound, `illegal electron dialog method`);\r\n\r\n return dialogMethod.call(dialog, ...args);\r\n }\r\n}\r\n\r\nfunction debounce(func: Function, ms: number = 200) {\r\n let timeout: NodeJS.Timeout;\r\n return function (this: any, ...args: any[]) {\r\n clearTimeout(timeout);\r\n timeout = setTimeout(() => {\r\n func.apply(this, args);\r\n }, ms);\r\n };\r\n}\r\n"]}
@@ -1,2 +1,2 @@
1
- export {};
1
+ export {};
2
2
  //# sourceMappingURL=ElectronPreload.d.ts.map