-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
199 lines (173 loc) · 5.27 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// Modules to control application life and create native browser window
const { app, BrowserWindow, ipcMain, Tray, nativeImage, screen } = require('electron')
const { start } = require('repl')
const path = require('path')
const covid = require('./covid')
const exec = require('./exec')
const util = require('./util')
const platform = require('./platform')
const fetch = require('node-fetch')
let mainWindow, tray, overlay = undefined;
global.sharedObj = {}
app.whenReady().then(() => {
createWindow()
createTray()
app.on('activate', function () {
if (BrowserWindow.getAllWindows().length === 0) createWindow()
})
})
app.on('window-all-closed', function () {
exec.kill().then(e => console.log(e))
if (process.platform !== 'darwin') app.quit()
})
function startApp(loadEvent) {
const location = 'ny'
covid.getRate(location).then(covid => {
global.sharedObj.covid = covid
global.sharedObj.location = 'United States'
loadEvent.sender.send('app-ready', true);
})
.catch(err => {
console.log("bad", err)
loadEvent.sender.send('app-ready', err);
})
}
/* WINDOW, TRAY */
function createWindow() {
// Create the browser window.
mainWindow = new BrowserWindow({
width: 325,
// width: 650,
height: 730,
x:500,
y:0,
frame: false,
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
nodeIntegration: true
}
})
// and load the index.html of the app.
mainWindow.loadFile('src/index.html')
// Open the DevTools.
// mainWindow.webContents.openDevTools()
const { width, height } = screen.getPrimaryDisplay().workAreaSize
overlay = new BrowserWindow({
width: width,
height: height,
x: 500, y: 500,
opacity: 0,
alwaysOnTop: true,
frame: false,
resizable: false,
backgroundColor: "#000000",
})
overlay.setIgnoreMouseEvents(true);
overlay.show()
}
function createTray() {
const rootPath = path.resolve(__dirname)
const iconName = (process.platform == 'darwin') ? 'menu-icon.png' : 'menu-icon.ico'
const imagePath = path.join(rootPath, 'resources', platform.get(), iconName)
const icon = nativeImage.createFromPath(imagePath)
tray = new Tray(icon)
tray.setIgnoreDoubleClickEvents(true)
positionWindow()
tray.on('click', function (event) {
toggleWindow()
})
}
function toggleWindow() {
if (mainWindow.isVisible()) {
mainWindow.hide()
} else {
showWindow()
}
}
function showWindow() {
positionWindow()
mainWindow.show()
mainWindow.focus()
}
function positionWindow() {
const trayPos = tray.getBounds()
const windowPos = mainWindow.getBounds()
let x, y = 0
if (process.platform == 'darwin') {
x = Math.round(trayPos.x + (trayPos.width / 2) - (windowPos.width / 2)) - 325
y = Math.round(trayPos.y + trayPos.height)
} else {
x = Math.round(trayPos.x + (trayPos.width / 2) - (windowPos.width / 2))
y = Math.round(trayPos.y + trayPos.height) - 765
}
mainWindow.setPosition(x, y, false)
}
// Update window to show version update alert
function updateWindowPositionForAlert() {
const windowSize = mainWindow.getBounds()
const windowMod = 70
mainWindow.setSize(windowSize.width, windowSize.height + windowMod);
if (process.platform != 'darwin') {
const trayPos = tray.getBounds()
const x = Math.round(trayPos.x + (trayPos.width / 2) - (windowSize.width / 2))
const y = Math.round(trayPos.y + trayPos.height) - 765 - windowMod
mainWindow.setPosition(x, y, false)
}
}
/* LISTENERS */
ipcMain.on('app-loaded', (event) => {
console.log("loaded")
startApp(event);
})
// Get CPU perecntage
ipcMain.on('usage-request', (event) => {
exec.usage().then(stats => {
global.sharedObj.stats = stats
event.sender.send('usage-completion', stats);
})
})
// Check for a new version
ipcMain.on('version-check', (event) => {
fetch("https://storage.googleapis.com/pandemic-pulse/code/version.json")
.then(response => response.json())
.then(data => {
const newVersion = (app.getVersion() != data.version) ? true : false
if (newVersion) updateWindowPositionForAlert()
console.log("new verison", newVersion)
event.sender.send('version-checked', newVersion);
})
.catch(err => {
event.sender.send('version-checked', false);
})
})
// Attach listener in the main process with the given ID
ipcMain.on('execute-request', (event, run) => {
if (run) {
console.log("starting process")
exec.stress(global.sharedObj.covid).then(e => console.log("process running"))
const brightness = util.normalize(global.sharedObj.covid.deathsScaled, 0, 100)
overlay.setOpacity(1 - brightness)
console.log(`[BRIGHTNESS]: ${global.sharedObj.covid.deathsScaled}%`)
}
else {
console.log("killing process")
exec.kill().then(e => console.log("process killed"))
overlay.setOpacity(0)
}
event.sender.send('execute-completion', "Hello World!");
});
// Location change listener
ipcMain.on('location-request', (event, location) => {
console.log("location-request", location)
covid.getRate(location).then(covid => {
global.sharedObj.covid = covid
global.sharedObj.location = location
event.sender.send('location-completion', global.sharedObj);
})
.catch(err => {
console.log("bad", err);
})
});
ipcMain.on('exit-app', (event, location) => {
app.exit(0)
});