-
Notifications
You must be signed in to change notification settings - Fork 18
/
main.js
94 lines (76 loc) · 2.34 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
const {
app,
BrowserWindow,
shell,
Menu,
Tray,
} = require('electron');
const path = require('path');
const child = require('child_process');
let win;
let tray;
let pyProc = null;
function createWindow(url) {
win = new BrowserWindow({
width: 400,
height: 600,
// frame: false,
});
win.loadURL(url);
// win.webContents.on('did-finish-load', function() {
// win.webContents.insertCSS('.sidebar-nav{ display: none !important; }');
// });
win.on('closed', () => {
win = null;
});
}
function openConfigFolder(file) {
const configFile = path.join(app.getPath('appData'), 'micboard', file);
shell.showItemInFolder(configFile);
}
function openLogFile() {
const file = path.join(app.getPath('appData'), 'micboard', 'micboard.log');
shell.openItem(file);
}
const createPyProc = () => {
const script = path.join(__dirname, 'dist', 'micboard-service', 'micboard-service').replace('app.asar', 'app.asar.unpacked');
pyProc = child.spawn(script, [], {
stdio: ['ignore', 'inherit', 'inherit'],
});
if (pyProc != null) {
console.log('child process success');
}
};
const exitPyProc = () => {
pyProc.kill();
pyProc = null;
};
function restartMicboardServer() {
pyProc.kill();
pyProc = null;
setTimeout(createPyProc, 250);
}
app.on('ready', () => {
const icon = path.join(__dirname, 'build', 'trayTemplate.png').replace('app.asar', 'app.asar.unpacked');
tray = new Tray(icon);
const contextMenu = Menu.buildFromTemplate([
{ label: 'About', click() { createWindow('http://localhost:8058/about'); } },
{ type: 'separator' },
{ label: 'Launch Micboard', click() { shell.openExternal('http://localhost:8058'); } },
{ label: 'Edit Configuration', click() { shell.openExternal('http://localhost:8058/#settings=true'); } },
{ label: 'Open Configuration Directory', click() { openConfigFolder('config.json'); } },
{ type: 'separator' },
{ label: 'Restart Micboard Server', click() { restartMicboardServer(); } },
{ label: 'Open log file', click() { openLogFile(); } },
{ role: 'quit' },
]);
tray.setToolTip('micboard');
tray.setContextMenu(contextMenu);
createPyProc();
setTimeout(() => {
shell.openExternal('http://localhost:8058');
}, 5000);
});
// app.on('ready', createPyProc);
app.on('window-all-closed', e => e.preventDefault());
app.on('will-quit', exitPyProc);