-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
94 lines (76 loc) · 2.37 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
require('update-electron-app')();
const { app, BrowserWindow, Tray } = require("electron");
const { sendSystemInfo, getCpuLoad } = require('./api/systemInfo');
const getContextMenu = require('./api/getContextMenu');
const windowStateKeeper = require("electron-window-state");
const handleApp = require('./api/handleAppLifecycle');
const path = require("path");
// create window and load local html file into it
function createWindow() {
// remenber window state
const mainWindowState = windowStateKeeper({
defaultWidth: 940,
defaultHeight: 630
});
// window
const win = new BrowserWindow({
x: mainWindowState.x,y: mainWindowState.y,
width: mainWindowState.width,height: mainWindowState.height,
minWidth: 940,minHeight: 630,
frame: false,
title: "System Info",
backgroundColor: "#121212",
icon: "app.ico",
show: false,
webPreferences: {
preload: path.join(__dirname, "/src/js/preload.js")
}
});
// load file
win.loadFile("src/index.html");
win.once("ready-to-show", () => {
// manange window state
mainWindowState.manage(win);
// handle lifecycle of app
handleApp();
// show window
win.show();
});
return win;
}
// In Electron, browser windows can only be created
// after the app module's ready event is fired.
app.whenReady().then(() => {
// send cpu usage
sendSystemInfo();
// create window
const window = createWindow();
const tray = new Tray("app.ico");
tray.setContextMenu(getContextMenu(window));
getCpuLoad().then(data =>{
data ? tray.setToolTip("CPU " + data.toFixed(0) + "%") : "";
})
tray.on("mouse-move",()=>{
getCpuLoad().then(data =>{
data ? tray.setToolTip("CPU " + data.toFixed(0) + "%") : "";
})
});
tray.on("click",()=>{
if(window.isVisible()){
window.hide();
}else{
window.show();
}
});
// for macOs
// creating new window in mac if all windows are closed
// because mac doesn't close appliction if all widnwods are closed
app.on("activate", () => {
if (BrowserWindow.getAllWindows() === 0) {
createWindow();
}
})
});
app.on("window-all-closed", () => {
if (process.platform != "darwin") app.quit();
});