-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
568 lines (516 loc) · 17.7 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
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
//const fs = require('fs');
const fs = require('fs-extra');
const windowStateKeeper = require('electron-window-state');
const electron = require('electron');
const {Menu, Tray, ipcMain } = require('electron');
const nativeImage = require('electron').nativeImage;
const path = require('path');
const nrc = require('node-run-cmd');
const wallpaper = require('wallpaper');
const app = electron.app;
const BrowserWindow = electron.BrowserWindow;
var mainWindowState = null;
var bgWindowState = null;
var icons = generateIcons();
let debug = false;
let splash = null;
let bgwin = null;
let win = null;
let tray = null;
let contextMenu = null;
const originalWallpaper = saveOriginalWallpaper();
// Set DPI Scaling
//app.commandLine.appendSwitch('high-dpi-support', 'true');
//app.commandLine.appendSwitch('force-device-scale-factor', '1.5');
console.log(app.getAppPath());
console.log(__dirname);
// Change to working directory
try {
process.chdir(process.cwd()+'/config');
console.log('\nNew working directory:\n' + process.cwd());
// Test FS Module
testFsModule();
} catch (err) {
console.error(`chdir: ${err}`);
}
var screenElectron, mainScreen, allScreens, dimensions, posX, posY, last_dimension;
// Player Window
function createWindow () {
// Set initial window state params
loadDimensions();
// Load the previous state with fallback to defaults
mainWindowState = windowStateKeeper({
path: 'winstate',
file: 'window-state.json',
defaultWidth: 500,
defaultHeight: 550,
defaultX: posX,
defaultY: posY
});
// Create the browser window.
win = new BrowserWindow({
'node-integration': true,
x: mainWindowState.x || mainWindowState.defaultX,
y: mainWindowState.y || mainWindowState.defaultY,
width: mainWindowState.width || 365,
height: mainWindowState.height || 350,
alwaysOnTop: false,
overlayScrollbars: true,
show: false,
transparent: true,
frame: false,
resizeable: false,
skipTaskbar: false,
maximizable: false,
minimizable: false,
icon: icons.roon
});
win.setMinimumSize(350, 150);
win.setMaximumSize(dimensions.width, dimensions.height)
mainWindowState.manage(win);
win.loadFile('index.html')
// on first ready
win.once('ready-to-show', () => {
setTimeout(function(){
splash.close();
win.show();
win.focus();
setTaskbarBtn()
//win.webContents.openDevTools() // Open the DevTools.
}, 2000)
})
win.on('show', () => {
tray.setHighlightMode('always')
})
win.on('hide', () => {
tray.setHighlightMode('never')
})
// Emitted when the window is closed.
win.on('closed', () => {
mainWindowState.saveState(win);
app.quit();
win = null;
bgwin = null;
tray = null;
})
};
// Splash screen
function createSplash() {
// Show the splash screen
splash = new BrowserWindow({
'node-integration': true,
position: "center",
width: 150,
height: 150,
show: false,
frame: false,
movable: false,
alwaysOnTop: true,
transparent: true,
resizeable: false,
skipTaskbar: true,
maximizable: false,
minimizable: false,
focusable: false
});
splash.loadFile('splash.html')
splash.show();
};
// Webview Window
function createBGWindow() {
loadDimensions();
// Load the previous state with fallback to defaults
bgWindowState = windowStateKeeper({
path: 'winstate',
file: 'bg-window-state.json',
defaultWidth: 800,
defaultHeight: 500
});
bgwin = new BrowserWindow({
'node-integration': false,
x: bgWindowState.x || null,
y: bgWindowState.y || null,
width: mainWindowState.width || bgWindowState.defaultWidth,
height: mainWindowState.height || bgWindowState.defaultHeight,
alwaysOnTop: false,
overlayScrollbars: true,
show: true,
transparent: false,
frame: true,
resizeable: true,
skipTaskbar: false,
maximizable: true,
minimizable: true
});
bgWindowState.manage(bgwin);
// and load the index.html of the app.
bgwin.loadFile('webview.html');
bgwin.on('ready-to-show', () => {
bgwin.setIcon(chromium_img);
bgwin.setAutoHideMenuBar(true);
bgwin.setMenuBarVisibility(false);
})
bgwin.on('show', () => {
bgwin.focus()
})
bgwin.on('closed', () => {
bgwin = null;
})
};
// Create Taskbar Buttons
function setTaskbarBtn() {
win.setThumbarButtons([]);
win.setThumbarButtons([
{
tooltip: 'Previous Track',
icon: path.resolve(`${__dirname}/assets/img/previous.png`),
click: function() {
win.webContents.send('transport_req', {control: 'prev'})
}
},
{
tooltip: 'Play',
icon: path.resolve(`${__dirname}/assets/img/play.png`),
click: function() {
win.webContents.send('transport_req', {control: 'play'})
}
},
{
tooltip: 'Next Track',
icon: path.resolve(`${__dirname}/assets/img/next.png`),
click: function() {
win.webContents.send('transport_req', {control: 'next'})
}
}
]);
};
// Default dimensions
function loadDimensions() {
let winSize = (win) ? win.getSize() : [350, 350];
screenElectron = electron.screen;
mainScreen = screenElectron.getPrimaryDisplay();
allScreens = screenElectron.getAllDisplays();
dimensions = mainScreen.size;
posX = (dimensions.width - winSize[0]);
posY = (dimensions.height - winSize[1]);
};
// Auto Position Window
function setWindowPosition(code){
loadDimensions();
switch(code) {
case "TL":
win.setPosition(0, 0, true);
break;
case "TR":
win.setPosition(posX, 0, true);
break;
case "BR":
win.setPosition(posX, posY, true);
break;
case "BL":
win.setPosition(0, posY, true);
break;
}
win.show();
mainWindowState.saveState(win);
};
// Enable Dragging Window
function setWindowDraggable(targetWindow, css_selector, enabled) {
targetWindow.webContents.send('set_draggable', {
css_selector: css_selector,
enabled: enabled
});
};
// Generate App Icons
var roon_img, google_img, chromium_img, gmusic_img, lastfm_img, plex_img, sonos_img, tidal_img, youtube_img, youtubemusic_img, spotify_img, soundcloud_img, pandora_img, amazon_img, apple_img;
function generateIcons() {
// Registered Icons
roon_img = nativeImage.createFromPath(__dirname + '/assets/img/roon.png');
google_img = nativeImage.createFromPath(__dirname + '/assets/img/google.png');
chromium_img = nativeImage.createFromPath(__dirname + '/assets/img/chromium.png');
gmusic_img = nativeImage.createFromPath(__dirname + '/assets/img/googlemusic.png');
lastfm_img = nativeImage.createFromPath(__dirname + '/assets/img/lastfm.png');
plex_img = nativeImage.createFromPath(__dirname + '/assets/img/plex.png');
sonos_img = nativeImage.createFromPath(__dirname + '/assets/img/sonos.png');
tidal_img = nativeImage.createFromPath(__dirname + '/assets/img/tidal.png');
youtube_img = nativeImage.createFromPath(__dirname + '/assets/img/youtube.png');
youtubemusic_img = nativeImage.createFromPath(__dirname + '/assets/img/youtubemusic.png');
spotify_img = nativeImage.createFromPath(__dirname + '/assets/img/spotify.png');
soundcloud_img = nativeImage.createFromPath(__dirname + '/assets/img/soundcloud.png');
pandora_img = nativeImage.createFromPath(__dirname + '/assets/img/pandora.png');
amazon_img = nativeImage.createFromPath(__dirname + '/assets/img/amazonprime.png');
apple_img = nativeImage.createFromPath(__dirname + '/assets/img/applemusic.png');
return {
"chromium": chromium_img,
"roon": roon_img,
"googlemusic": gmusic_img,
"lastfm": lastfm_img,
"plex": plex_img,
"sonos": sonos_img,
"tidal": tidal_img,
"youtube": youtube_img,
"youtubemusic": youtubemusic_img,
"spotify": spotify_img,
"soundcloud": soundcloud_img,
"pandora": pandora_img
}
};
// Save Original Wallpaper
function saveOriginalWallpaper() {
wallpaper.get().then(imagePath => {
console.log("\n\nSave Original Wallpaper: " + imagePath);
// Copy Current Wallpaper
fs.copy(imagePath, './art/originalWallpaper.png')
.then(() => console.log('originalWallpaper copied: success!'))
.catch(err => console.error(err))
});
};
// Get current wallpaper
function getWallpaper() {
wallpaper.get().then(imagePath => {
return imagePath;
});
};
// Set current wallpaper
function setWallpaper(imagePath, setImmediate) {
wallpaper.set(imagePath).then(() => {
console.log('\n\nNew wallpaper: ' + imagePath);
});
};
function testFsModule() {
// Debug FS
try {
var t = new Date;
fs.mkdir('tests', function() {
fs.writeFile('tests/fs-write-test.txt', 'Command: fs.writeFile();\nResult: Success!\nTimestamp: ' + t + '\nWorking Directory: ' + process.cwd(), function() {
fs.readFile('tests/fs-write-test.txt', 'utf-8', function(err, data) {
if(err) {
console.log(err)
} else {
console.log(process.cwd(), data);
}
});
});
});
} catch(err) {
console.log(err);
}
};
app.on('ready', () => {
createSplash();
createWindow();
// Build tray icon
tray = new Tray(icons["roon"]);
let template = [
{
label: "Speed Dial",
type: "normal",
click: function() {
if(bgwin != null) {
bgwin.show();
} else {
createBGWindow();
}
}
},
{type: "separator"},
{
label : "Position",
submenu: [
{
label: 'Free Roam',
type: 'normal',
click:function() {
setWindowDraggable(win, "#drag", true);
}
},
{type: "separator"},
{
label: 'Top Left',
type: 'normal',
click: function() {
setWindowPosition("TL")
}
},
{
label: 'Top Right',
type: 'normal',
click: function() {
setWindowPosition("TR")
}
},
{
label: 'Bottom Right',
type: 'normal',
checked: true,
click:function() {
setWindowPosition("BR")
}
},
{
label: 'Bottom Left',
type: 'normal',
click:function() {
setWindowPosition("BL")
}
}
]
},
{type: "separator"},
{
label: 'Always On Top',
type: 'checkbox',
checked: win.isAlwaysOnTop(),
click: function() {
var alwaystop = win.isAlwaysOnTop() ? false : true;
win.setAlwaysOnTop(alwaystop);
this.checked = alwaystop;
}
},
{
label : "More...",
submenu: [
{
label: 'FullScreen',
type: 'checkbox',
click: function(){
(win.isFullScreen()) ? win.setFullScreen(false) : win.setFullScreen(true);
}
},
{
label: 'Maximize',
type: 'checkbox',
click: function(){
var curSize = win.getSize();
var maxSize = win.getMaximumSize();
if(curSize.reduce((a,b)=>a*b) >= maxSize.reduce((a,b)=>a*b)) {
win.setSize(last_dimension.size[0], last_dimension.size[1]);
win.setPosition(last_dimension.pos[0], last_dimension.pos[1], 1000);
mainWindowState.saveState(win);
} else {
last_dimension = {
size: win.getSize(),
pos: win.getPosition()
};
win.setSize(maxSize[0], maxSize[1]);
win.setPosition(0, 1, true);
mainWindowState.saveState(win);
}
}
},
{
label: 'Hide Dashboard',
type: 'checkbox',
checked: false,
click: function(){
win.webContents.send('toggle_dashboard', {});
this.checked = !this.checked;
}
},
{
label: 'Relaunch',
type: 'checkbox',
click: function(){
app.relaunch({args: process.argv.slice(1).concat(['--relaunch'])})
app.exit(0)
}
},
{
label: 'Settings',
type: 'normal',
click: function(){
win.webContents.send('open_settings', {});
}
},
{
label: 'Inspect',
type: 'normal',
click: function(){
win.toggleDevTools();
}
},
]
},
{
label: 'Quit',
type: 'normal',
click: function() {
app.quit();
}
}
];
contextMenu = Menu.buildFromTemplate(template);
tray.setToolTip('Roon Mini-Controller');
tray.setContextMenu(contextMenu);
tray.on('click', (event) => {
win.isMinimized() ? win.restore() && setTaskbarBtn() : win.minimize();
});
// Get current zone details
ipcMain.on('nowPlaying', (event, arg) => {
console.log('\nnowPlaying', arg);
// Update Taskbar overlay icon
var img = nativeImage.createFromPath('./art/album.png');
tray.setToolTip(arg.now_playing.three_line.line1 + ' - ' + arg.now_playing.three_line.line2);
win.setOverlayIcon(img, 'Now Playing');
// Set Wallpaper
setWallpaper('./art/wallpaper.png');
});
// Get Dimensions
ipcMain.on('get_dimensions', (event, arg) => {
win.webContents.send('window_dimensions', dimensions);
});
// Save Window Position
ipcMain.on('save_position', (event, arg) => {
if (win) mainWindowState.saveState(win);
if (bgwin) bgWindowState.saveState(bgwin);
});
// Rebuild roon_bundle.js
ipcMain.on('rebuild_app', (event, arg) => {
nrc.run('npm run buld').then(function(exitCodes) {
console.log(exitCodes)
setTimeout(function(){
app.relaunch({args: process.argv.slice(1).concat(['--relaunch'])})
app.exit(0)
}, 5000)
}, function(err) {
console.log('Command failed to run with error: ', err);
});
});
// Restart
ipcMain.on('restart_app', (event, arg) => {
app.relaunch({args: process.argv.slice(1).concat(['--relaunch'])})
app.exit(0)
});
// Upde context menu
ipcMain.on('update_context_menu', (event, arg) => {
template[5].submenu[2].checked = arg.checked
contextMenu = Menu.buildFromTemplate(template);
tray.setContextMenu(contextMenu);
win.webContents.send('toggle_dashboard', {});
})
});
app.on('activate', () => {
if (win === null) {
createWindow()
}
});
// Before Quit
app.on('before-quit', () => {
console.log('before-quit: fired');
// Restore Original Wallpaper before exiting
setWallpaper('./art/originalWallpaper.png', 'setImmediate');
});
// Quit when all windows are closed.
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit()
}
});
// Wil Quit - All windows closed, about to quit
app.on('will-quit', () => {
console.log('will-quit: fired');
tray = null;
splash = null;
bgwin = null;
win = null;
});