-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.js
81 lines (69 loc) · 2.07 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
const electron = require("electron");
const platform = require("os").platform();
const path = require("path");
const api = require("./api");
// Import parts of electron to use
const { app, Menu, Tray } = electron;
process.on("unhandledRejection", (reason, p) => {
console.log("Unhandled Rejection at: Promise", p, "reason:", reason);
});
const assetsDirectory = path.join(__dirname, "src", "assets", "img");
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let tray;
const targetDir = path.join(app.getPath("home"), ".bolero");
api.create({ targetDir });
function createTray() {
let trayImage = path.join(assetsDirectory, "icon-128x128.png");
if (platform === "darwin") {
trayImage = path.join(assetsDirectory, "icon.png");
} else if (platform === "win32") {
trayImage = path.join(assetsDirectory, "icon.ico");
}
tray = new Tray(trayImage);
if (platform === "darwin") {
tray.setPressedImage(path.join(assetsDirectory, "iconHighlight.png"));
}
const contextMenu = Menu.buildFromTemplate([
{
label: "Open in Browser",
accelerator: "Alt+Command+O",
click: showBrowserWindow
},
{
label: "Power Off",
accelerator: "Alt+Command+X",
click: function() {
if (tray) {
tray.destroy();
tray = null;
}
terminate();
}
}
]);
tray.setContextMenu(contextMenu);
tray.setToolTip("Deviota Bolero");
tray.on("click", showBrowserWindow);
tray.on("right-click", () => tray.popUpContextMenu(contextMenu));
}
function showBrowserWindow() {
electron.shell.openExternal(`http://localhost:${api.PORT}`);
}
function terminate() {
console.log("STOPPING BOLERO..");
api.stop().then(() => {
console.log("BOLERO STOPPED!");
app.quit();
process.exit(0);
});
}
process.on("SIGINT", terminate);
process.on("SIGTERM", terminate);
// Don't show the app in the doc
app.dock && app.dock.hide();
app.on("ready", () => {
api.start();
createTray();
showBrowserWindow();
});