forked from imodeljs/simple-viewer-app
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.ts
More file actions
75 lines (65 loc) · 3.19 KB
/
Copy pathmain.ts
File metadata and controls
75 lines (65 loc) · 3.19 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
/*---------------------------------------------------------------------------------------------
* Copyright (c) 2019 Bentley Systems, Incorporated. All rights reserved.
* Licensed under the MIT License. See LICENSE.md in the project root for license terms.
*--------------------------------------------------------------------------------------------*/
import * as path from "path";
import { app, protocol, BrowserWindow } from "electron";
import { RpcInterfaceDefinition, ElectronRpcManager } from "@bentley/imodeljs-common";
/**
* Initializes Electron backend
*/
export default function initialize(rpcs: RpcInterfaceDefinition[]) {
// tell ElectronRpcManager which RPC interfaces to handle
ElectronRpcManager.initializeImpl({}, rpcs);
// in order to preserve the platform standard behavior on MacOS,
// the application needs to continue running even if the "main" window closes
// so we'll keep a reference to the currently open "main" window here
let mainWindow: BrowserWindow | undefined;
/**
* Converts an "electron://" URL to an absolute file path.
*
* We use this protocol in production builds because our frontend must be built with absolute URLs,
* however, since we're loading everything directly from the install directory, we cannot know the
* absolute path at build time.
*/
function parseElectronUrl(requestedUrl: string): string {
let assetPath = requestedUrl.substr("electron://".length);
assetPath = assetPath.replace(/#.*$/, "");
return path.normalize(`${__dirname}/../../webresources/${assetPath}`);
}
/**
* Creates the "main" electron BrowserWindow with the application's frontend.
*/
function createWindow() {
// in dev builds (npm start), we don't copy the public folder to lib/public,
// so we'll need to access the original public dir for our app icon
const isDevBuild = (process.env.NODE_ENV === "development");
const iconPath = (isDevBuild) ? path.join(__dirname, "../../webresources/appicon.ico") : path.join(__dirname, "../../webresources/appicon.ico");
// configure and create the main window
mainWindow = new BrowserWindow({
autoHideMenuBar: true,
icon: iconPath,
});
mainWindow.on("closed", () => mainWindow = undefined);
// load the frontend
// in development builds, the frontend assets are served by the webpack devserver
// in production builds, load the built frontend assets directly from the filesystem
mainWindow.loadURL(isDevBuild ? "http://localhost:3000" : parseElectronUrl("electron://index.html"));
}
// open the "frontend" window when the application starts up
app.on("ready", () => {
createWindow();
// also handle any "electron://" requests and redirect them to "file://" URLs
protocol.registerFileProtocol("electron", (request, callback) => callback(parseElectronUrl(request.url)));
});
// quit the application when all windows are closed (unless we're running on MacOS)
app.on("window-all-closed", () => {
if (process.platform !== "darwin")
app.quit();
});
// re-open the main window if it was closed and the app is re-activated (this is the normal MacOS behavior)
app.on("activate", () => {
if (!mainWindow)
createWindow();
});
}