-
-
Notifications
You must be signed in to change notification settings - Fork 101
/
main.js
275 lines (261 loc) · 9.2 KB
/
main.js
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
// This is the electron startup script
const { app, BrowserWindow, Tray, Menu, dialog, ipcMain, powerMonitor } = require('electron');
const { autoUpdater } = require("electron-updater");
const { nativeImage } = require('electron/common');
const path = require("path");
let server;
let mainWindow;
let trayIcon;
const WindowProperties = {
width: 1260,
height: 850,
minWidth: 450,
minHeight: 450
}
const gotTheLock = app.requestSingleInstanceLock();
function processError(err) {
if (server !== undefined) {
server.generateAndSendErrorReport(err);
} else {
dialog.showErrorBox("Unexpected error", "There was an unexpected error, and there was an other error generating the error report. Please open a bug report on the project's Github page or contact one of the developers. Stack Trace: " + err.toString());
}
}
process.on('uncaughtException', processError);
function createWindow() {
mainWindow = new BrowserWindow({
width: WindowProperties.width,
height: WindowProperties.height,
minWidth: WindowProperties.minWidth,
minHeight: WindowProperties.minHeight,
webPreferences: {},
show: false,
});
mainWindow.setMenu(null);
mainWindow.loadURL('http://127.0.0.1:4455');
mainWindow.webContents.on('did-finish-load', function() {
mainWindow.show();
});
mainWindow.on('close', function(event) {
if (!app.isQuiting) {
event.preventDefault();
mainWindow.hide();
}
return false;
});
// start the server
server = require("./dist/index");
}
function createTrayIcon() {
const icon = path.join(process.resourcesPath, "build/icon.png");
const nativeIcon = nativeImage.createFromPath(icon);
trayIcon = new Tray(app.isPackaged ? nativeIcon.resize({ width: 32 }) : nativeIcon.resize({ width: 32 }));
trayIcon.setContextMenu(
Menu.buildFromTemplate([{
label: 'Show',
click: () => {
mainWindow.show();
},
},
{
type: "separator",
},
{
label: 'Quit',
click: () => {
dialog.showMessageBox(mainWindow, {
title: "Are you sure?",
message: "Are you sure you want to quit TallyArbiter?",
buttons: ["Yes", "No"],
}).then((v) => {
if (v.response == 0) {
app.isQuiting = true;
app.quit();
}
});
},
},
]));
trayIcon.setToolTip("Tally Arbiter");
trayIcon.on("click", () => {
if (mainWindow.isVisible()) {
mainWindow.hide();
} else {
mainWindow.show();
}
});
}
function checkForUpdates() {
autoUpdater.autoDownload = false;
autoUpdater.autoInstallOnAppQuit = false;
autoUpdater.checkForUpdates();
autoUpdater.on("update-available", (info) => {
ipcMain.on("updateButtonPressed", (event, arg) => {
if(info.releaseNotes.includes("WARNING")){
dialog.showMessageBox(releaseNotesWindow, {
title: "Warning",
message: "Please read release notes carefully, since this update may require updating listener clients or some other manual intervention. If you read release notes carefully, you can continue.",
buttons: ["Update", "Cancel"],
}).then((v) => {
if (v.response == 0) {
dialog.showMessageBox(mainWindow, {
title: "Downloading update",
message: "The update is being downloaded in the background. Once finished, you will be prompted to save your work and restart TallyArbiter."
});
autoUpdater.downloadUpdate();
}
});
} else {
dialog.showMessageBox(mainWindow, {
title: "Downloading update",
message: "The update is being downloaded in the background. Once finished, you will be prompted to save your work and restart TallyArbiter."
});
autoUpdater.downloadUpdate()
}
});
let releaseDate = new Date(Date.parse(info.releaseDate)).toLocaleString();
let releaseNotesPage = `
<html>
<head>
<title>Release notes for version ${info.releaseName}</title>
<style>
.btn {
display: inline-block;
font-weight: 400;
text-align: center;
white-space: nowrap;
vertical-align: middle;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
border: 1px solid transparent;
padding: .375rem .75rem;
font-size: 1rem;
line-height: 1.5;
border-radius: .25rem;
transition: color .15s ease-in-out,background-color .15s ease-in-out,border-color .15s ease-in-out,box-shadow .15s ease-in-out;
}
.btn-success {
color: #fff;
background-color: #28a745;
border-color: #28a745;
}
.btn-success:hover {
color: #fff;
background-color: #218838;
border-color: #1e7e34;
}
.btn-danger {
color: #fff;
background-color: #dc3545;
border-color: #dc3545;
}
.btn-danger:hover {
color: #fff;
background-color: #c82333;
border-color: #bd2130;
}
</style>
</head>
<body>
<script>const { ipcRenderer } = require('electron');</script>
<h1><b>There's an update available for TallyArbiter.</b> Do you want to download and install it?</h1>
<button class="btn btn-success" onclick="ipcRenderer.send('updateButtonPressed')">Update</button> <button class="btn btn-danger" onclick="window.close();">Cancel</button>
<h1>Release notes for version <b>${info.releaseName}</b>:</h1>
${info.releaseNotes}
<br><br>
<p style="font-size: x-large">Release name: <b>${info.releaseName}</b></p>
<p style="font-size: x-large">Release date: <b>${releaseDate}</b></p>
<p style="font-size: x-large">sha512: <b><code>${info.sha512}</code></b></p>
</body>
</html>
`;
releaseNotesWindow = new BrowserWindow({
width: 850,
height: 1260,
minHeight: 450,
minWidth: 450,
webPreferences: {
nodeIntegration: true,
contextIsolation: false
},
show: false
});
releaseNotesWindow.setMenu(null);
releaseNotesWindow.loadURL(`data:text/html;charset=utf-8,${encodeURIComponent(releaseNotesPage)}`);
releaseNotesWindow.webContents.on('did-finish-load', function() {
releaseNotesWindow.show();
//releaseNotesWindow.webContents.openDevTools();
});
});
autoUpdater.on("update-downloaded", () => {
dialog.showMessageBox(null, {
title: "Update downloaded",
message: "The update has been downloaded. Save your work and then press the Update button.",
buttons: ["Update"],
}).then((r) => {
if (r.response == 0) {
autoUpdater.quitAndInstall();
}
});
});
}
powerMonitor.on('shutdown', () => {
app.isQuiting = true;
app.quit();
});
if (!gotTheLock) {
app.quit();
} else {
app.whenReady().then(() => {
createWindow();
createTrayIcon();
checkForUpdates();
app.on('activate', function() {
if (BrowserWindow.getAllWindows().length === 0) createWindow();
});
}).catch((err) => {
processError(err);
});
app.on('second-instance', () => {
// Someone tried to run a second instance, we should focus our window.
if (mainWindow) {
if (!mainWindow.isVisible()) {
mainWindow.show();
}
if (mainWindow.isMinimized()) {
mainWindow.restore();
}
mainWindow.focus();
}
});
app.on('window-all-closed', function() {
if (process.platform !== 'darwin') {
app.preventDefault() // Prevents the window from closing
dialog.showMessageBox(mainWindow, {
title: "Are you sure?",
message: "Are you sure you want to quit TallyArbiter?",
buttons: ["Yes", "No"],
}).then((v) => {
if (v.response == 0) {
app.quit();
}
});
}
});
// Listen for web contents being created
app.on('web-contents-created', (e, contents) => {
if (contents.getType() == 'window') {
// Listen for any new window events
contents.on('new-window', (e, url) => {
e.preventDefault();
const win = new BrowserWindow({ show: false });
win.loadURL(url);
win.setMenu(null);
win.webContents.on('did-finish-load', function() {
win.show();
});
});
}
})
}