-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
287 lines (249 loc) · 10.9 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
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
const { app, BrowserWindow, Menu, Tray, ipcMain, webContents } = require('electron');
const Vibrant = require('node-vibrant');
const express = require('express');
const server = require('./src/js/battery-listener');
const jsonHandler = require('./jsonHandler');
const os = require('os')
const fs = require('fs')
const path = require('path');
const icon = __dirname + '/favicon.ico'
const { spawn } = require('child_process');
var movable = false;
// checks if the appExe is named electron so it doesn't autostart electron.exe while developing
if (!app.getPath('exe').includes('electron')) {
// starts the app at login
app.setLoginItemSettings({
openAtLogin: true,
path: app.getPath('exe')
});
}
// starts the background Serivce which provides information for the music and device care widget
const backgroundServicePath = path.join(path.dirname(app.getPath('exe')), 'backgroundService', 'backgroundService.exe')
// Check if the file exists
if (fs.existsSync(backgroundServicePath)) {
const backgroundServiceChild = spawn(backgroundServicePath);
}
// declares the variables so the app doesnt outputs many error
let musicWidget = null;
let batteryWidget = null;
let deviceCareWidget = null;
let weatherWidget = null;
let topStoriesWidget = null;
let flightWidget = null;
let calendarWidget = null;
let quickNotesWidget = null;
let digitalClockWidget = null;
let upcomingMoviesWidget = null;
const folderPath = path.join(os.homedir(), 'AppData', 'Local', 'Galaxy-Widgets');
if (!fs.existsSync(folderPath)) {
fs.mkdirSync(folderPath);
}
// info for widget windows
const widgetsData = {
widgets: [
// { name: "deviceCareWidget", width: 390, height: 125, html: "./src/widgets/deviceCare/deviceCare.html" },
{
name: "batteryWidget",
displayName: "Battery",
clickthrough: true,
sizes: {
small: {
width: 175, height: 175, html: "./src/widgets/deviceCare/battery/small/battery.html"
},
medium: {
width: 390, height: 175, html: "./src/widgets/deviceCare/battery/medium/battery.html"
}
},
},
{
name: "weatherWidget",
displayName: "Weather",
clickthrough: true,
sizes: {
small: {
width: 175, height: 175, html: "./src/widgets/weather/weather/small/weather.html"
},
medium: {
width: 390, height: 175, html: "./src/widgets/weather/weather/medium/weather.html"
}
},
},
{
name: "topStoriesWidget",
displayName: "Top Stories",
clickthrough: true,
sizes: {
small: {
width: 175, height: 175, html: "./src/widgets/news/topStories/small/topStories.html"
},
medium: {
width: 390, height: 175, html: "./src/widgets/news/topStories/medium/topStories.html"
},
large: {
width: 390, height: 250, html: "./src/widgets/news/topStories/large/topStories.html"
}
}
},
{
name: "musicWidget",
displayName: "Music Player",
clickthrough: true,
sizes: {
small: {
width: 175, height: 175, html: "./src/widgets/music/music/small/music.html"
},
medium: {
width: 390, height: 175, html: "./src/widgets/music/music/medium/music.html"
}
}
},
{
name: "calendarWidget",
displayName: "Calendar",
clickthrough: true,
sizes: {
small: {
width: 175, height: 175, html: "./src/widgets/calendar/calendar/small/calendar.html"
},
large: {
width: 390, height: 225, html: "./src/widgets/calendar/calendar/large/calendar.html"
}
}
}
// { name: "flightWidget", width: 390, height: 175, html: "./src/widgets/wallet/flight.html" },
// { name: "quickNotesWidget", width: 390, height: 175, html: "./src/widgets/notes/quickNotes.html", },
// { name: "digitalClockWidget", width: 390, height: 100, html: "./src/widgets/clock/digitalClock.html" },
// { name: "upcomingMoviesWidget", width: 390, height: 200, html: "./src/widgets/videoPlayer/upcomingMovies.html" },
],
};
app.on('ready', () => {
// creates tray for quitting the app
tray = new Tray(icon)
const sizeMenuItems = [];
// Create a sub-menu for widget sizes
for (const widget of widgetsData.widgets) {
const widgetSubMenu = [];
for (const sizeKey in widget.sizes) {
widgetSubMenu.push({
label: sizeKey[0].toUpperCase() + sizeKey.slice(1),
click: () => {
console.log(`Widget: ${widget.name}, Size: ${sizeKey}`);
const widgetStates = JSON.parse(fs.readFileSync(folderPath + "\\widgetStates.json"))
widgetStates[widget.name].size = sizeKey
fs.writeFileSync(folderPath + "\\widgetStates.json", JSON.stringify(widgetStates, null, 4))
}
});
}
sizeMenuItems.push({
label: widget.displayName,
submenu: widgetSubMenu
});
}
// Build the context menu
const contextMenu = Menu.buildFromTemplate([
{
label: 'Toggle movable', click: () => {
movable = !movable;
}
},
{ type: 'separator' },
...sizeMenuItems, // Add the widget size sub-menu
{ type: 'separator' },
{ label: 'Quit', click: () => { app.quit(); } }
]);
tray.setToolTip('Galaxy Widgets');
tray.setContextMenu(contextMenu);
function setStates() {
const widgetStates = JSON.parse(fs.readFileSync(folderPath + "\\widgetStates.json"))
const widgetPositions = JSON.parse(fs.readFileSync(folderPath + "\\widgetPositions.json"))
// goes through each widget
widgetsData.widgets.forEach(widget => {
// checks if the widget is enabled and if it doesnt exist (== null)
if (widgetStates[widget.name].show == "true" && eval(widget.name) == null) {
const widgetWindow = new BrowserWindow({
width: widget.sizes[widgetStates[widget.name].size].width,
height: widget.sizes[widgetStates[widget.name].size].height,
transparent: true,
frame: false,
resizable: false,
transparency: true,
skipTaskbar: true,
webPreferences: {
contextIsolation: false,
nodeIntegration: true,
}
});
// sets clickthrough based on widgetsData
widgetWindow.setIgnoreMouseEvents(widget.clickthrough)
// sets the variable to something else than null so it wont get created again
eval(`${widget.name} = widgetWindow`)
// sets the position | uses setBounds because setPosition makes the widget bigger in anything else than 100% scaling
if (eval(widget.name) != null) {
widgetWindow.setBounds({
width: widget.sizes[widgetStates[widget.name].size].width,
height: widget.sizes[widgetStates[widget.name].size].height,
x: parseInt(widgetPositions[widget.name].x),
y: parseInt(widgetPositions[widget.name].y)
});
}
widgetWindow.loadFile(path.join(__dirname, widget.sizes[widgetStates[widget.name].size].html));
// sets the variable again to null when its closed
widgetWindow.on('closed', () => {
eval(`${widget.name} = null`);
});
// fixes widget appearing in taskbar
widgetWindow.on('focus', () => {
widgetWindow.setSkipTaskbar(true)
});
widgetWindow.on('moved', () => {
let roundedX, roundedY;
const currentPosition = widgetWindow.getPosition()
const currentX = currentPosition[0];
const currentY = currentPosition[1];
roundedX = Math.round(currentX / 25) * 25;
roundedY = Math.round(currentY / 25) * 25;
widgetPositions[widget.name].x = roundedX.toString();
widgetPositions[widget.name].y = roundedY.toString();
fs.writeFileSync(path.join(folderPath, 'widgetPositions.json'), JSON.stringify(widgetPositions, null, 4));
const widgetStatesUpdated = JSON.parse(fs.readFileSync(folderPath + "\\widgetStates.json"))
widgetWindow.setBounds({
width: widget.sizes[widgetStatesUpdated[widget.name].size].width,
height: widget.sizes[widgetStatesUpdated[widget.name].size].height,
x: roundedX,
y: roundedY
});
});
}
// destroys window if it gets deactivated
else if (widgetStates[widget.name].show != "true" && eval(widget.name) != null) {
eval(`${widget.name}.destroy()`);
eval(`${widget.name} = null`);
}
// fixes widget appearing in taskbar
else if (widgetStates[widget.name].show == "true" && eval(widget.name) != null) {
// check if current loaded html is the wanted size of widget
if (!eval(`${widget.name}.webContents.getURL()`).includes(widgetStates[widget.name].size)) {
// change size
eval(`${widget.name}.setBounds({
width: ${widget.sizes[widgetStates[widget.name].size].width},
height: ${widget.sizes[widgetStates[widget.name].size].height},
x: parseInt(widgetPositions[widget.name].x),
y: parseInt(widgetPositions[widget.name].y),
})`)
// change html
eval(`${widget.name}.loadFile(path.join(__dirname, widget.sizes[widgetStates[widget.name].size].html))`)
}
eval(`${widget.name}.setIgnoreMouseEvents(${!movable})`)
eval(`${widget.name}.setMovable(${movable})`)
}
});
}
setStates()
setInterval(function () {
setStates();
}, 400);
});
// hot reloader for easier development
try {
require('electron-reloader')(module)
} catch (_) { }