-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
121 lines (106 loc) · 2.78 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
const path = require("path");
const { app, Menu, ipcMain } = require("electron");
const log = require("electron-log");
const Store = require("./Store");
const MainWindow = require("./MainWindow");
const AppTray = require("./AppTray");
// Set env
process.env.NODE_ENV = "production";
const isDev = process.env.NODE_ENV === "development" ? true : false;
const isMac = process.platform === "darwin" ? true : false;
let mainWindow = null;
let tray = null;
// init store
const store = new Store({
configName: "user-settings",
defaults: {
settings: {
cpuThreshold: 65.0,
alertFrequency: 2.5,
},
},
});
function createMainWindow() {
mainWindow = new MainWindow("./app/index.html", isDev);
}
function instantiateMainWindow() {
createMainWindow();
mainWindow.webContents.on("dom-ready", () => {
// parse the settings file when the dom is ready; send the contents to
// the renderer process
mainWindow.webContents.send("settings:get", store.get("settings"));
log.info(
`dom-ready, sent settings:get with settings ${JSON.stringify(
store.get("settings")
)}`
);
});
}
app.on("ready", () => {
instantiateMainWindow();
const mainMenu = Menu.buildFromTemplate(menu);
Menu.setApplicationMenu(mainMenu);
mainWindow.on("close", (e) => {
// ideally prevent the situtation the
// exception handles below
if (!app.isQuitting) {
e.preventDefault();
// window is hidden unless quit explicitly
mainWindow.hide();
}
return true;
});
// instantiate the tray
const icon = path.join(__dirname, "assets", "icons", "tray_icon.png");
tray = new AppTray(icon, mainWindow);
});
const menu = [
...(isMac ? [{ role: "appMenu" }] : []),
{
role: "fileMenu",
},
{
label: "View",
submenu: [
{
label: "Toggle Navigation",
click: () => mainWindow.webContents.send("nav:toggle"),
},
],
},
...(isDev
? [
{
label: "Developer",
submenu: [
{ role: "reload" },
{ role: "forcereload" },
{ type: "separator" },
{ role: "toggledevtools" },
],
},
]
: []),
];
// set the settings when the renderer triggers the event
ipcMain.on("settings:set", (e, settingsObj) => {
// set and get the contents; send to renderer
store.set(settingsObj);
mainWindow.webContents.send("settings:get", store.get("settings"));
log.info(
`recieved settings:set with settings ${JSON.stringify(
settingsObj
)} store now: ${JSON.stringify(store.get("settings"))}`
);
});
app.on("window-all-closed", () => {
if (!isMac) {
app.quit();
}
});
app.on("activate", () => {
if (mainWindow.getAllWindows().length === 0) {
createMainWindow();
}
});
app.allowRendererProcessReuse = true;