-
Notifications
You must be signed in to change notification settings - Fork 90
/
main.ts
341 lines (292 loc) · 8.84 KB
/
main.ts
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
import type { ConfigData } from "./app";
import {
app as App,
BrowserWindow,
shell,
ipcMain,
nativeImage,
Tray,
Menu,
MenuItem,
} from "electron";
import { execFile } from "child_process";
import windowStateKeeper from "electron-window-state";
import { RelaunchOptions } from "electron/main";
import { URL } from "url";
import path from "path";
import { firstRun, getConfig, store, onStart, getBuildURL } from "./lib/config";
import { connectRPC, dropRPC } from "./lib/discordRPC";
import { autoLaunch } from "./lib/autoLaunch";
import { autoUpdate } from "./lib/updater";
const trayIcon = nativeImage.createFromPath(
path.resolve(
App.getAppPath(),
"assets",
// MacOS has special size and naming requirements for tray icons
// https://stackoverflow.com/questions/41664208/electron-tray-icon-change-depending-on-dark-theme/41998326#41998326
process.platform === "darwin" ? "trayIconTemplate.png" : "trayIcon.png",
),
);
const WindowIcon = nativeImage.createFromPath(
path.resolve(App.getAppPath(), "assets", "icon.png"),
);
trayIcon.setTemplateImage(true);
WindowIcon.setTemplateImage(true);
onStart();
autoUpdate();
type AppInterface = typeof App & {
shouldRelaunch: boolean;
shouldQuit: boolean;
};
let mainWindow: BrowserWindow;
let app = App as AppInterface;
/**
* Create the main window.
*/
function createWindow() {
const initialConfig = getConfig();
const mainWindowState = windowStateKeeper({
defaultWidth: 1280,
defaultHeight: 720,
});
mainWindow = new BrowserWindow({
autoHideMenuBar: true,
title: "Revolt",
icon: WindowIcon,
frame: initialConfig.frame,
webPreferences: {
preload: path.resolve(App.getAppPath(), "bundle", "app.js"),
contextIsolation: true,
nodeIntegration: false,
},
x: mainWindowState.x,
y: mainWindowState.y,
width: mainWindowState.width,
height: mainWindowState.height,
backgroundColor: "#191919",
minWidth: 300,
minHeight: 300,
});
if (process.platform === "win32") {
App.setAppUserModelId(mainWindow.title);
}
mainWindowState.manage(mainWindow);
mainWindow.loadURL(getBuildURL());
/**
* Window events
*/
mainWindow.on("show", () => buildMenu());
mainWindow.on("hide", () => buildMenu());
mainWindow.on("close", (event) => {
if (
!app.shouldQuit &&
!app.shouldRelaunch &&
getConfig().minimiseToTray
) {
event.preventDefault();
mainWindow.hide();
}
});
mainWindow.webContents.on("before-input-event", (event, input) => {
if (input.control && input.key === "=") {
event.preventDefault();
mainWindow.webContents.setZoomLevel(
mainWindow.webContents.getZoomLevel() + 1,
);
}
});
mainWindow.webContents.on("did-finish-load", () =>
mainWindow.webContents.send("config", getConfig()),
);
mainWindow.webContents.on("context-menu", (_, params) => {
const menu = new Menu();
// Add each spelling suggestion
for (const suggestion of params.dictionarySuggestions) {
menu.append(
new MenuItem({
label: suggestion,
click: () =>
mainWindow.webContents.replaceMisspelling(suggestion),
}),
);
}
// Allow users to add the misspelled word to the dictionary
if (params.misspelledWord) {
menu.append(
new MenuItem({
label: "Add to dictionary",
click: () =>
mainWindow.webContents.session.addWordToSpellCheckerDictionary(
params.misspelledWord,
),
}),
);
}
if (menu.items.length > 0) {
menu.popup();
}
});
/**
* Inter-process communication
*/
ipcMain.on("getAutoStart", () =>
autoLaunch
.isEnabled()
.then((v) => mainWindow.webContents.send("autoStart", v)),
);
ipcMain.on("setAutoStart", async (_, value: boolean) => {
if (value) {
await autoLaunch.enable();
mainWindow.webContents.send("autoStart", true);
} else {
await autoLaunch.disable();
mainWindow.webContents.send("autoStart", false);
}
});
ipcMain.on("set", (_, arg: Partial<ConfigData>) => {
if (typeof arg.discordRPC !== "undefined") {
if (arg.discordRPC) {
connectRPC();
} else {
dropRPC();
}
}
store.set("config", {
...store.get("config"),
...arg,
});
});
ipcMain.on("reload", () => mainWindow.loadURL(getBuildURL()));
ipcMain.on("relaunch", () => {
app.shouldRelaunch = true;
mainWindow.close();
});
ipcMain.on("min", () => mainWindow.minimize());
ipcMain.on("max", () =>
mainWindow.isMaximized()
? mainWindow.unmaximize()
: mainWindow.maximize(),
);
ipcMain.on("close", () => mainWindow.close());
/**
* System tray
*/
const tray = new Tray(trayIcon);
function buildMenu() {
tray.setContextMenu(
Menu.buildFromTemplate([
{ label: "Revolt", type: "normal", enabled: false },
{ label: "---", type: "separator" },
{
label: mainWindow.isVisible()
? "Hide Revolt"
: "Show Revolt",
type: "normal",
click: function () {
if (mainWindow.isVisible()) {
mainWindow.hide();
} else {
mainWindow.show();
}
},
},
{
label: "Restart Revolt",
type: "normal",
click: function () {
app.shouldRelaunch = true;
mainWindow.close();
},
},
{
label: "Quit Revolt",
type: "normal",
click: function () {
app.shouldQuit = true;
app.quit();
},
},
]),
);
}
buildMenu();
tray.setToolTip("Revolt");
tray.setImage(trayIcon);
tray.on("click", function (e) {
if (mainWindow.isVisible()) {
if (mainWindow.isFocused()) {
mainWindow.hide();
} else {
mainWindow.focus();
}
} else {
mainWindow.show();
}
});
}
/**
* Only launch the application once.
*/
const acquiredLock = App.requestSingleInstanceLock();
if (!acquiredLock) {
App.quit();
} else {
App.on("second-instance", () => {
if (mainWindow) {
// Restore from tray if hidden
if (!mainWindow.isVisible()) mainWindow.show();
// Restore from taskbar if minimised
if (mainWindow.isMinimized()) mainWindow.restore();
// Then focus the window
mainWindow.focus();
}
});
App.whenReady().then(async () => {
await firstRun();
createWindow();
App.on("activate", function () {
if (BrowserWindow.getAllWindows().length === 0) createWindow();
});
});
}
/**
* Close out application unless if instructed to relaunch.
*/
App.on("window-all-closed", function () {
if (app.shouldRelaunch) {
const options: RelaunchOptions = {
args: process.argv.slice(1).concat(["--relaunch"]),
};
if (App.isPackaged && process.env.APPIMAGE) {
execFile(process.env.APPIMAGE, options.args);
} else {
App.relaunch(options);
}
App.quit();
return;
}
if (process.platform !== "darwin") App.quit();
});
/**
* Add navigation handlers.
*/
App.on("web-contents-created", (_, contents) => {
contents.on("will-navigate", (event, navigationUrl) => {
const parsedUrl = new URL(navigationUrl);
if (parsedUrl.origin !== getBuildURL()) {
event.preventDefault();
}
});
contents.setWindowOpenHandler(({ url }) => {
if (
url.startsWith("http:") ||
url.startsWith("https:") ||
url.startsWith("mailto:")
) {
setImmediate(() => {
shell.openExternal(url);
});
}
return { action: "deny" };
});
});