/
index.tsx
152 lines (127 loc) · 4.25 KB
/
index.tsx
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/**
* Copyright (c) 2020 BlockDev AG
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import * as path from "path"
import { format as formatUrl } from "url"
import { app, BrowserWindow, ipcMain, Menu, Tray } from "electron"
import { autoUpdater } from "electron-updater"
import * as packageJson from "../../package.json"
import { winSize } from "../config"
import { supervisor } from "../supervisor/supervisor"
import {
initialize as initializeAnalytics,
setupApp as setupAnalyticsForApp,
setupGlobals as setupAnalyticsGlobals,
setupWindow as setupAnalyticsForWindow,
} from "../analytics/analytics-main"
import { initialize as initializeSentry } from "../errors/sentry"
import { createTray, refreshTrayIcon } from "./tray"
import { MainIpcListenChannels, WebIpcListenChannels } from "./ipc"
import { createMenu } from "./menu"
initializeSentry()
const isDevelopment = process.env.NODE_ENV !== "production"
global.supervisor = supervisor
// global reference to win (necessary to prevent window from being garbage collected)
let win: BrowserWindow | null
// eslint-disable-next-line @typescript-eslint/no-unused-vars
let tray: Tray | null
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const installExtensions = async (): Promise<void | any[]> => {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const installer = require("electron-devtools-installer")
const forceDownload = !!process.env.UPGRADE_EXTENSIONS
const extensions = ["REACT_DEVELOPER_TOOLS"]
// eslint-disable-next-line prettier/prettier
return Promise.all(extensions.map((name) => installer.default(installer[name], forceDownload))).catch(console.log) // eslint-disable-line no-console
}
const createWindow = async (): Promise<BrowserWindow> => {
if (isDevelopment) {
await installExtensions()
}
const window = new BrowserWindow({
title: packageJson.productName,
width: winSize.width,
height: winSize.height,
useContentSize: true,
resizable: false,
maximizable: false,
webPreferences: { nodeIntegration: true },
})
if (!isDevelopment) {
Menu.setApplicationMenu(createMenu())
}
if (isDevelopment) {
window.webContents.once("dom-ready", () => {
window.webContents.openDevTools()
})
}
if (isDevelopment) {
window.loadURL(`http://localhost:${process.env.ELECTRON_WEBPACK_WDS_PORT}`)
} else {
window.loadURL(
formatUrl({
pathname: path.join(__dirname, "index.html"),
protocol: "file",
slashes: true,
}),
)
}
window.on("close", (event) => {
if (app.quitting) {
win = null
} else {
event.preventDefault()
win?.hide()
}
})
window.on("closed", () => {
win = null
})
window.webContents.on("devtools-opened", () => {
window.focus()
setImmediate(() => {
window.focus()
})
})
setupAnalyticsForWindow(window)
return window
}
// create main BrowserWindow when electron is ready
app.on("ready", async () => {
win = await createWindow()
tray = createTray(app, win)
initializeAnalytics()
setupAnalyticsGlobals()
setupAnalyticsForApp(app)
autoUpdater.checkForUpdatesAndNotify()
})
// quit application when all windows are closed
app.on("window-all-closed", () => {
// on macOS it is common for applications to stay open until the user explicitly quits
if (process.platform !== "darwin") {
app.quit()
}
})
app.on("activate", async () => {
// on macOS it is common to re-create a window even after all windows have been closed
if (win === null) {
win = await createWindow()
}
})
app.on("before-quit", () => (app.quitting = true))
app.on("will-quit", async () => {
await supervisor.connect()
await supervisor.killMyst()
})
ipcMain.on(MainIpcListenChannels.ConnectionStatus, (event, status) => {
if (!tray || !status) {
return
}
refreshTrayIcon(tray, status)
})
export const ipcWebDisconnect = (): void => {
win?.webContents.send(WebIpcListenChannels.Disconnect)
}