-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
134 lines (116 loc) · 4.09 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
const electron = require('electron');
const {app, Menu, BrowserWindow, Tray, globalShortcut, ipcMain, shell} = require('electron');
const apikeys = require('./app/keys');
const messages = require('./app/messages');
const balloonNotifications = require('./app/balloonNotifications');
const settings = require('./app/settings');
const fsHandle = require('./app/fsHandle');
let tray = null;
let window = null;
let lastQual = 0;
let airQuality = 0;
const windowSettings = {
show: false,
resizable: false,
x: 0,
y: 0,
frame: false,
width: 350,
height: 550,
maxWidth: 350,
maxHeight: 550,
allwaysOnTop: false,
minimizable: true
}
process.env.GOOGLE_API_KEY = apikeys.googleAuto;
process.env.NODE_ENV = settings.environment;
app.on('ready', () => {
tray = new Tray(`${__dirname}/assets/icons/icon.ico`);
window = new BrowserWindow(windowSettings);
window.loadURL(`file://${__dirname}/main.html`);
fsHandle.readConf();
ipcMain.on('response:quality', (e, item) => {
airQuality = item;
setIcons();
displayBalloonNotification();
lastQual = airQuality;
});
ipcMain.on('updatenLink:clicked', (e, link) => {
shell.openExternal(link);
});
setIcons = () => {
if (airQuality == 0) {
tray.setImage(`${__dirname}/assets/icons/icon.ico`, messages.noData);
window.setOverlayIcon(null, messages.noData);
tray.setToolTip(messages.noData);
} else if (airQuality <= 2) {
tray.setImage(`${__dirname}/assets/icons/green.ico`);
window.setOverlayIcon(`${__dirname}/assets/icons/green.ico`, messages.goodQual);
tray.setToolTip(messages.goodQual);
} else if(airQuality <= 4) {
tray.setImage(`${__dirname}/assets/icons/yellow.ico`);
window.setOverlayIcon(`${__dirname}/assets/icons/yellow.ico`, messages.mediumQual);
tray.setToolTip(messages.mediumQual);
} else {
tray.setImage(`${__dirname}/assets/icons/red.ico`);
window.setOverlayIcon(`${__dirname}/assets/icons/red.ico`, messages.lowQual);
tray.setToolTip(messages.lowQual);
};
};
displayBalloonNotification = () => {
if(process.winair.config.notifications == "on") {
if(lastQual == 0) {
setBalloonContent(balloonNotifications.title, balloonNotifications.dataReceived);
} else if(airQuality == 0) {
} else if(lastQual != airQuality) {
setBalloonContent(balloonNotifications.title, balloonNotifications.airQualityChanged);
}
}
}
setBalloonContent = (title, content) => {
tray.displayBalloon({title: title, content: content});
}
setWindowPosition = () => {
let cursorPosition = electron.screen.getCursorScreenPoint()
let display = electron.screen.getDisplayNearestPoint(cursorPosition)
const {width, height} = display.workAreaSize;
const boundsX = display.bounds.x;
const boundsY = display.bounds.y;
const windowPosX = boundsX + width - windowSettings.width - 3;
const windowPosY = boundsY + height - windowSettings.height;
window.setPosition(windowPosX, windowPosY);
}
const contextMenu = Menu.buildFromTemplate([
{
label: 'Quit',
click() {
app.quit()
}
}
]);
tray.setToolTip(messages.noData);
tray.setContextMenu(contextMenu);
tray.on('click', () => {
setWindowPosition();
process.winair.config.notifications = "off";
window.isVisible() ? null : window.show();
});
window.on('blur', () => {
process.winair.config.notifications = "on";
if(process.env.NODE_ENV === 'production') {
window.hide();
}
});
if(process.env.NODE_ENV === 'production') {
app.setLoginItemSettings({
openAtLogin: true
})
} else {
app.setLoginItemSettings({
openAtLogin: false
})
}
window.on('show', () => {
setIcons();
});
});