-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
256 lines (226 loc) · 5.63 KB
/
app.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
const {
app,
BrowserWindow,
Menu,
globalShortcut,
Tray,
ipcMain,
Notification,
} = require("electron");
const AutoLaunch = require("auto-launch");
process.env.NODE_ENV = "production"; //development
const isDev = process.env.NODE_ENV !== "production" ? true : false,
isMac = process.env.NODE_ENV === "darwin" ? true : false,
isWin = process.env.NODE_ENV === "win32" ? true : false,
isLin = process.env.NODE_ENV === "linux" ? true : false;
console.log(process.platform);
//////////////////////
// Application Windows
//////////////////////
// Assignments
let mainWindow, aboutWindow, notificationWindow, settingsWindow;
////////////////////////
/// Application Page ///
////////////////////////
const icon = __dirname + "\\assets\\battery-2.ico";
// Main Application
function runApplication() {
mainWindow = new BrowserWindow({
icon: icon,
title: "Battery Informer",
height: 400,
width: 400,
resizable: false,
frame: false,
webPreferences: {
nodeIntegration: false,
contextIsolation: true,
},
});
mainWindow.center();
// isDev ? mainWindow.webContents.openDevTools() : null;
mainWindow.loadFile("./app/start.html");
// Hiding the Main Window
mainWindow.on("close", (event) => {
if (app.quitting) {
mainWindow = null;
} else {
event.preventDefault();
mainWindow.hide();
}
});
}
//////////////////
/// About Page ///
//////////////////
function aboutPage() {
aboutWindow = new BrowserWindow({
title: "About Battery Informer",
icon: icon,
width: 350,
height: 400,
resizable: false,
});
aboutWindow.loadFile("./app/about.html");
}
/////////////////////////
/// Notification Page ///
/////////////////////////
function notificationPage() {
notificationWindow = new BrowserWindow({
title: "Notification Page",
icon: icon,
height: 189,
width: 428,
resizable: false,
frame: false,
});
notificationWindow.center();
notificationWindow.loadFile("./app/notification.html");
}
/////////////////////
/// Settings Page ///
/////////////////////
function settingsPage() {
settingsWindow = new BrowserWindow({
title: "Settings",
icon: icon,
height: 230,
width: 400,
resizable: false,
frame: false,
webPreferences: {
nodeIntegration: false,
contextIsolation: true,
// preload: "./preload.js",
},
});
settingsWindow.center();
settingsWindow.loadFile("./app/settings.html");
}
// Notification Alert On StartUp
app.setAppUserModelId("Battery Notifier");
const NOTIFICATION_TITLE = "Battery Notifier Active";
const NOTIFICATION_BODY = "Application is now active in the background.";
function showNotification() {
new Notification({
title: NOTIFICATION_TITLE,
body: NOTIFICATION_BODY,
}).show();
}
///////////////////////
// Starting Application
///////////////////////
app.on("ready", () => {
runApplication();
showNotification();
// The Menu to be Made
const mainmenu = Menu.buildFromTemplate(menu);
// The Menu to Set
Menu.setApplicationMenu(mainmenu);
// Quit Menu
// globalShortcut.register("CmdOrCtrl+W", () => app.quit());
mainWindow.on("ready", () => (mainWindow = null));
// Auto-Launch
let autoLaunch = new AutoLaunch({
name: "Battery Informer",
path: app.getPath("exe"),
});
autoLaunch.isEnabled().then((isEnabled) => {
if (!isEnabled) autoLaunch.enable();
});
});
///////////////////
// Application Menu
///////////////////
const menu = [
// About Page
// Macintosh
...(isMac
? [
{ role: "appMenu" },
{
label: "File",
submenu: [
{ label: "About " + app.name, click: () => aboutPage() },
{ type: "separator" },
{ label: "Settings", click: () => settingsPage() },
],
},
]
: [
// Windows
{
label: "File",
submenu: [
{
label: "Quit Appication",
accelerator: "CmdOrCtrl+W",
click: () => {
app.quit();
},
},
],
// role:"fileMenu"
},
{
label: "Info",
submenu: [
{ label: "About Battery Informer", click: () => aboutPage() },
{ type: "separator" },
{ label: "Settings", click: () => settingsPage() },
],
},
]),
...(isDev
? [
{
label: "Developer Tools",
submenu: [
{ label: "Notification Page", click: () => notificationPage() },
{ role: "reload" },
{ role: "forcereload" },
{ type: "separator" }, // Making a line in the menu
{ role: "toggledevtools" },
],
},
]
: []),
];
// Application Tray Icon
let tray = null;
app.whenReady().then(() => {
tray = new Tray(icon);
const contextMenu = Menu.buildFromTemplate([
{ label: "About", click: () => aboutPage() },
{ label: "Settings", click: () => settingsPage() },
{ type: "separator" },
{
label: "Exit",
click: () => {
app.exit();
},
},
]);
tray.setToolTip("Battery Informer");
tray.setContextMenu(contextMenu);
});
///////////////////////
// Battery Main Process
///////////////////////
const batteryLevel = require("battery-level");
batteryLevel().then((level) => {
var totalBattery = level * 100;
console.log(totalBattery);
setInterval(function () {
//this code runs every second
if (totalBattery > 90) {
notificationPage();
}
}, 420000); // 7mins
// 5000 5secs
// 1200000 20mins
});
ipcMain.on("form:value", (e, options) => {
console.log(options.sliderValue);
});