-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.js
104 lines (84 loc) Β· 2.12 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
const { app, ipcMain } = require("electron");
const path = require("path");
const AppTray = require("./app/AppTray");
const InfoWindow = require("./app/InfoWindow");
const MainWindow = require("./app/MainWindow");
const DataIO = require("./DataIO");
const status = {
DEVELOPMENT: "DEVELOPMENT",
PRODUCTION: "PRODUCTION",
};
process.env.NODE_ENV = status.PRODUCTION;
const isMac = process.platform === "darwin";
const isWin = process.platform === "win32";
const isDev = process.env.NODE_ENV === status.DEVELOPMENT;
if (isWin) {
app.setAppUserModelId(app.name);
}
let mainWindow = null;
let infoWindow = null;
let tray = null;
const gotTheLock = app.requestSingleInstanceLock();
function createMainWindow() {
mainWindow = new MainWindow(
path.join(__dirname, "assets", "img", "logo-white.png"),
isDev,
path.join(__dirname, "front-end", "build", "index.html")
);
ipcMain.on("app:ready", () =>
mainWindow.webContents.send("time:set", DataIO.readData())
);
ipcMain.on("time:save", (_, time) => DataIO.saveData(time));
}
function createTray() {
tray = new AppTray(
path.join(__dirname, "assets", "img", "tray-icon.png"),
isDev,
isWin,
mainWindow
);
}
function createInfoWindow() {
infoWindow = new InfoWindow(
path.join(__dirname, "assets", "img", "logo-white.png"),
path.join(__dirname, "info-window", "index.html")
);
ipcMain.on("info:close", () => {
infoWindow.close();
infoWindow = null;
});
}
const initialize = () => {
if (!gotTheLock) {
app.quit();
return;
}
createMainWindow();
createTray();
!isDev && createInfoWindow();
ipcMain.on("timer:update", (_, time) =>
isWin
? tray.setToolTip(time.length > 0 ? `UnHook\nTime Left: ${time}` : "")
: tray.setTitle(time)
);
};
app.on("ready", initialize);
app.on("window-all-closed", () => {
if (isMac) {
app.quit();
}
});
app.on("activate", () => {
if (mainWindow === null) {
initialize();
}
});
app.on("second-instance", () => {
if (mainWindow) {
setTimeout(() => {
mainWindow.center();
mainWindow.show();
mainWindow.focus();
}, 1000);
}
});