-
Notifications
You must be signed in to change notification settings - Fork 4
/
electron-main.js
488 lines (427 loc) · 12.6 KB
/
electron-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
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
const { app, ipcMain, BrowserWindow, dialog } = require('electron');
app.commandLine.appendSwitch('ignore-gpu-blacklist');
app.allowRendererProcessReuse = false;
const path = require('path');
const url = require('url');
const fs = require('fs');
const os = require('os');
const Store = require('electron-store');
const Sentry = require('@sentry/electron');
const BackendManager = require('./src/node/backend-manager.js');
const fontHelper = require('./src/node/font-helper');
const MonitorManager = require('./src/node/monitor-manager.js');
const MenuManager = require('./src/node/menu-manager.js');
const UpdateManager = require('./src/node/update-manager.js');
const UglyNotify = require('./src/node/ugly-notify.js');
const events = require('./src/node/ipc-events');
const { getDeeplinkUrl, handleDeepLinkUrl } = require('./src/node/deep-link-helper');
Sentry.init({ dsn: 'https://bbd96134db9147658677dcf024ae5a83@o28957.ingest.sentry.io/5617300' });
Sentry.captureMessage('User Census', {
level: 'info',
tags: {
census: 'v1',
from: 'backend',
},
});
let mainWindow;
let menuManager;
global.backend = { alive: false };
global.devices = {};
let logger = null;
function createLogFile() {
var storageDir = app.getPath('userData');
function chkDir(target) {
if (fs.existsSync(target)) {
return;
} else {
chkDir(path.dirname(target));
fs.mkdirSync(target);
}
}
chkDir(storageDir);
let filename = path.join(app.getPath('userData'), 'backend.log');
global.backend.logfile = filename;
let writeStream = fs.createWriteStream(filename, { flags: 'w' });
logger = writeStream;
console._stdout = writeStream;
console._stderr = writeStream;
const setCloseEventHandler = () => {
writeStream.on('close', () => {
writeStream = fs.createWriteStream(filename, { flags: 'a' });
logger = writeStream;
console._stdout = writeStream;
console._stderr = writeStream;
setCloseEventHandler();
});
}
setCloseEventHandler();
return writeStream;
}
var DEBUG = false;
logger = process.stderr.isTTY ? process.stderr : createLogFile();
if (process.argv.indexOf('--debug') > 0) {
DEBUG = true;
console.log('DEBUG Mode');
// require('electron-reload')(__dirname);
}
// Solve transparent window issue
if (process.platform === 'linux') {
app.disableHardwareAcceleration();
}
function onGhostUp(data) {
global.backend.alive = true;
global.backend.port = data.port;
if (mainWindow) {
mainWindow.webContents.send(events.BACKEND_UP, global.backend);
}
}
function onGhostDown() {
global.backend.alive = false;
global.backend.port = undefined;
if (mainWindow) {
//mainWindow.webContents.send(events.BACKEND_DOWN);
}
}
function onDeviceUpdated(deviceInfo) {
let deviceID = `${deviceInfo.source}:${deviceInfo.uuid}`;
let origDeviceInfo = global.devices[deviceID];
if (origDeviceInfo && origDeviceInfo.st_id !== null && origDeviceInfo.st_id !== deviceInfo.st_id) {
switch (deviceInfo.st_id) {
case 48:
UglyNotify.send(deviceInfo.name, 'Is paused');
break;
case 64:
UglyNotify.send(deviceInfo.name, 'Is completed!');
break;
case 128:
UglyNotify.send(deviceInfo.name, 'Is aborted');
break;
}
}
if (mainWindow) {
mainWindow.webContents.send('device-status', deviceInfo);
}
if (deviceInfo.alive) {
if (menuManager) {
menuManager.updateDevice(deviceInfo.uuid, deviceInfo);
}
} else {
if (global.devices[deviceID]) {
if (menuManager) {
menuManager.removeDevice(deviceInfo.uuid, global.devices[deviceID]);
}
delete global.devices[deviceID];
}
}
global.devices[deviceID] = deviceInfo;
}
require('./src/node/bootstrap.js');
const updateManager = new UpdateManager();
const backendManager = new BackendManager({
location: process.env.BACKEND,
trace_pid: process.pid,
server: process.argv.indexOf('--server') > 0,
on_ready: onGhostUp,
on_device_updated: onDeviceUpdated,
on_stderr: (data) => logger.write(`${data}`),
on_stopped: onGhostDown,
debug: DEBUG,
c: console,
});
backendManager.start();
//Run monitorexe api
monitorManager = null;
if (process.argv.includes('--monitor')) {
console.log('Starting Monitor');
monitorManager = new MonitorManager({
location: process.env.BACKEND
});
//kill process first, in case last time shut down
monitorManager.killProcSync();
monitorManager.startProc();
}
let shadowWindow;
let shouldCloseShadowWindow = false;
const createShadowWindow = () => {
if (!shadowWindow) {
shadowWindow = new BrowserWindow({
show: false,
webPreferences: {
nodeIntegration: true
},
});
// shadowWindow.webContents.openDevTools();
loadShadowWindow();
shadowWindow.on('close', function (e) {
if (!shouldCloseShadowWindow) {
e.preventDefault();
} else {
console.log('Shadow window closed');
}
});
}
};
const loadShadowWindow = () => {
if (shadowWindow) {
shadowWindow.loadURL(url.format({
pathname: path.join(__dirname, 'public/shadow-index.html'),
protocol: 'file:'
}));
}
}
function createWindow() {
// Create the browser window.
mainWindow = new BrowserWindow({
width: 1300,
height: 650,
minWidth: 800,
minHeight: 400,
titleBarStyle: process.platform === 'darwin' ? 'hidden' : 'default',
frame: process.platform !== 'win32',
title: `Beam Studio - ${app.getVersion()}`,
webPreferences: {
enableRemoteModule: true,
preload: path.join(__dirname, 'src/node', 'main-window-entry.js'),
nodeIntegration: true
},
trafficLightPosition: { x: 12, y: 25 },
vibrancy: 'light'
});
const store = new Store();
if (!store.get('poke-ip-addr')) {
store.set('poke-ip-addr', '192.168.1.1');
}
if (!store.get('customizedLaserConfigs')) {
mainWindow.webContents
.executeJavaScript('({...localStorage});', true)
.then(localStorage => {
const keysNeedParse = ['auto_check_update', 'auto_connect', 'guessing_poke', 'loop_compensation', 'notification', 'printer-is-ready'];
for (let key in localStorage) {
if (keysNeedParse.includes(key)) {
try {
localStorage[key] = JSON.parse(localStorage[key]);
console.log(key, localStorage[key]);
} catch (e) {
console.log(key, e);
// Error when parsing
}
}
}
store.set(localStorage);
});
}
// mainWindow.maximize();
mainWindow.loadURL(url.format({
pathname: path.join(__dirname, 'public/index.html'),
protocol: 'file:',
slashes: true,
}));
let isCloseConfirmed = false;
let isFrontEndReady = false;
ipcMain.on(events.FRONTEND_READY, () => {
isFrontEndReady = true;
});
mainWindow.on('close', function (e) {
if (isFrontEndReady && !isCloseConfirmed) {
e.preventDefault();
mainWindow.webContents.send('WINDOW_CLOSE');
// if save dialog does not pop in 10 seconds, something may goes wrong in frontend, close the app
let isSaveDialogPopped = false;
ipcMain.once('SAVE_DIALOG_POPPED', () => {
isSaveDialogPopped = true;
});
const closeBeamStudio = () => {
isCloseConfirmed = true;
if (monitorManager) {
monitorManager.killProc();
}
backendManager.stop();
mainWindow.close();
shouldCloseShadowWindow = true;
try {
shadowWindow.close();
} catch (e) {
console.log(e);
}
}
setTimeout(() => {
if (!isSaveDialogPopped) {
closeBeamStudio();
}
}, 10000);
ipcMain.once('CLOSE_REPLY', (event, reply) => {
if (reply) {
closeBeamStudio();
}
});
} else {
if (monitorManager) {
monitorManager.killProc();
}
backendManager.stop();
shouldCloseShadowWindow = true;
try {
shadowWindow.close();
} catch (e) {
console.log(e);
}
}
});
mainWindow.on('closed', function () {
mainWindow = null;
if (process.platform === 'darwin' && DEBUG) {
console.log('Main window closed.');
} else {
app.quit();
}
});
mainWindow.on('page-title-updated', function (event) {
event.preventDefault();
});
menuManager.on('DEBUG-RELOAD', () => {
mainWindow.loadURL(url.format({
pathname: path.join(__dirname, 'public/index.html'),
protocol: 'file:',
slashes: true,
}));
loadShadowWindow();
});
menuManager.on('DEBUG-INSPECT', () => {
mainWindow.webContents.openDevTools();
});
ipcMain.on('DEBUG-INSPECT', () => {
mainWindow.webContents.openDevTools();
});
if (!process.argv.includes('--test') && (process.defaultApp || DEBUG)) {
mainWindow.webContents.openDevTools();
}
updateManager.setMainWindow(mainWindow);
}
ipcMain.on(events.CHECK_BACKEND_STATUS, () => {
if (mainWindow) {
mainWindow.send(events.NOTIFY_BACKEND_STATUS, {
backend: global.backend,
devices: global.devices
});
} else {
console.error('Recv async-status request but main window not exist');
}
});
ipcMain.on(events.SVG_URL_TO_IMG_URL, (e, data) => {
const { svgUrl: url, imgWidth: width, imgHeight: height, bb, imageRatio, id, strokeWidth } = data;
if (shadowWindow) {
shadowWindow.send(events.SVG_URL_TO_IMG_URL, { url, width, height, bb, imageRatio, id, strokeWidth });
}
});
ipcMain.on(events.SVG_URL_TO_IMG_URL_DONE, (e, data) => {
const { imageUrl, id } = data;
mainWindow.send(`${events.SVG_URL_TO_IMG_URL_DONE}_${id}`, imageUrl);
});
fontHelper.registerEvents();
ipcMain.on('save-dialog', function (event, title, allFiles, extensionName, extensions, filename, file) {
const isMac = process.platform === 'darwin';
const isLinux = process.platform === 'linux';
const options = {
defaultPath: isLinux ? `${filename}.${extensions[0]}` : filename,
title,
filters: [
{ name: isMac ? `${extensionName} (*.${extensions[0]})` : extensionName, extensions },
{ name: allFiles, extensions: ['*'] }
]
}
const filePath = dialog.showSaveDialogSync(options)
if (!filePath) {
event.returnValue = false;
return;
}
switch (typeof (file)) {
case 'string':
fs.writeFile(filePath, file, function (err) {
if (err) {
dialog.showErrorBox('Error', err);
event.returnValue = false;
return;
}
});
break;
case 'object':
fs.writeFileSync(filePath, file, function (err) {
if (err) {
dialog.showErrorBox('Error', err);
event.returnValue = false;
return;
}
});
break;
default:
event.returnValue = false;
dialog.showErrorBox('Error: something wrong, please contact FLUX Support');
break;
}
event.returnValue = filePath;
})
console.log('Running Beam Studio on ', os.arch());
app.setAsDefaultProtocolClient('beam-studio');
const hasLock = app.requestSingleInstanceLock();
console.log('hasLock', hasLock);
if (process.platform === 'win32' && !hasLock && getDeeplinkUrl(process.argv)) {
// if primary instance exists and open from deep link, return
app.quit();
return;
}
// win32 deep link handler
app.on('second-instance', (e, argv) => {
e.preventDefault();
console.log(argv);
if (mainWindow) {
if (mainWindow.isMinimized()) {
mainWindow.restore();
}
mainWindow.focus();
}
const url = getDeeplinkUrl(argv);
handleDeepLinkUrl(mainWindow, url);
});
// macOS deep link handler
app.on('will-finish-launching', () => {
app.on('open-url', (event, url) => {
handleDeepLinkUrl(mainWindow, url);
});
});
if (os.arch() == 'ia32' || os.arch() == 'x32') {
app.commandLine.appendSwitch('js-flags', '--max-old-space-size=2048');
} else {
app.commandLine.appendSwitch('js-flags', '--max-old-space-size=4096');
}
app.on('ready', () => {
menuManager = new MenuManager();
menuManager.on(events.MENU_CLICK, (data) => {
data = {
id: data.id,
serial: data.serial,
}
if (mainWindow) {
mainWindow.webContents.send(events.MENU_CLICK, data);
} else {
console.log('Menu event triggered but window does not exist.');
}
});
if (!mainWindow) {
createShadowWindow();
createWindow();
} else {
console.log("MainWindow instance", mainWindow);
mainWindow.focus();
}
});
// app.on('window-all-closed', function () {
// });
app.on('activate', function () {
// On OS X it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (mainWindow === null) {
createWindow();
}
});
app.on('before-quit', function () {
});