const {app, BrowserWindow, ipcMain, Tray, Menu, session, dialog, shell} = require('electron'); const {createServer} = require('./src/server'); const path = require('path'); let win; let tray; let settings; let shouldExit = false; let playing = false; //Get path to asset function assetPath(a) { return path.join(__dirname, 'assets', a); } async function startServer() { settings = await createServer(true, () => { //Server error shouldExit = true; if (win) win.close(); dialog.showMessageBoxSync({ type: 'error', title: 'Server error', message: 'Server error occured, Freezer is probably already running!', buttons: ['Close'] }); }); } async function createWindow() { //Create window win = new BrowserWindow({ width: settings.width, darkTheme: true, height: settings.height, minWidth: 620, minHeight: 600, resizable: true, autoHideMenuBar: true, icon: assetPath("icon.png"), title: 'Freezer', webPreferences: { enableRemoteModule: true, nodeIntegration: true, devTools: true } }); win.loadURL(`http://localhost:${settings.port}`); //Minimize to tray win.on('minimize', (event) => { if (settings.minimizeToTray) { event.preventDefault(); win.hide(); } }); //On close win.on('close', async (event) => { if (shouldExit) { win = null; tray = null; app.quit(); return true; } //Normal exit if (!settings || !settings.arl || settings.arl == '' || settings.closeOnExit) { win.webContents.send('onExit'); shouldExit = true; } event.preventDefault(); win.hide(); return false; }); //Thumbnail Toolbars setThumbarButtons(); } //Create window app.on('ready', async () => { await startServer(); createWindow(); //Create Tray tray = new Tray(assetPath("icon-taskbar.png")); tray.on('double-click', () => restoreWindow()); tray.on('click', () => restoreWindow()); setTray(); }); //Restore or create new window function restoreWindow() { if (win) { win.show(); setThumbarButtons(); return; } createWindow(); } //Update tray context menu function setTray() { const contextMenu = Menu.buildFromTemplate([ { label: 'Restore', type: 'normal', click: () => restoreWindow() }, playing ? { label: 'Pause', type: 'normal', click: () => win.webContents.send('togglePlayback') } : { label: 'Play', type: 'normal', click: () => win.webContents.send('togglePlayback') }, { label: 'Next', type: 'normal', click: () => win.webContents.send('skipNext') }, { label: 'Previous', type: 'normal', click: () => win.webContents.send('skipPrev') }, { label: 'Exit', type: 'normal', click: () => { shouldExit = true; if (!win) return app.quit(); win.close(); } } ]); tray.setContextMenu(contextMenu); } //Update Thumbnail Toolbars (Windows) function setThumbarButtons() { win.setThumbarButtons([ { tooltip: 'Skip Previous', icon: assetPath('skip-previous.png'), click: () => win.webContents.send('skipPrev') }, //Play/Pause playing ? { tooltip: 'Pause', icon: assetPath('pause.png'), click: () => win.webContents.send('togglePlayback') } : { tooltip: 'Play', icon: assetPath('play.png'), click: () => win.webContents.send('togglePlayback') }, //Skip next { tooltip: 'Skip Next', icon: assetPath('skip-next.png'), click: () => win.webContents.send('skipNext') }, ]); } ipcMain.on('openUrl', (event, args) => { shell.openExternal(args); }); //Playing state change from UI ipcMain.on('playing', (event, args) => { playing = args; setThumbarButtons(); setTray(); }); //Update settings from ui ipcMain.on('updateSettings', (event, args) => { Object.assign(settings, args); }); //onExit callback ipcMain.on('onExit', () => { shouldExit = true; win.close(); }); //Open downloads directory ipcMain.on('openDownloadsDir', async () => { if ((await shell.openPath(settings.downloadsPath)) == "") return; shell.showItemInFolder(settings.downloadsPath); }); //Download path picker ipcMain.on('selectDownloadPath', async (event) => { let res = await dialog.showOpenDialog({ title: 'Downloads folder', properties: ['openDirectory', 'promptToCreate'], }); if (!res.canceled && res.filePaths.length > 0) { event.reply('selectDownloadPath', res.filePaths[0]); } }); //Login using browser ipcMain.on('browserLogin', async (event) => { //Initial clean session.defaultSession.clearStorageData(); let lwin = new BrowserWindow({ width: 800, height: 600, icon: assetPath('icon.png'), title: "Deezer Login", resizable: true, autoHideMenuBar: true, webPreferences: { nodeIntegration: false } }); lwin.loadURL('https://deezer.com/login'); let arl = await new Promise((res) => { lwin.webContents.on('did-navigate', async () => { let arlCookie = await session.defaultSession.cookies.get({ name: "arl" }); if (arlCookie.length > 0) { res(arlCookie[0].value); } }); }); lwin.close(); lwin = null; //Delete deezer junk session.defaultSession.clearStorageData(); event.reply('browserLogin', arl); });