From f7d1e2f53ddf9501f7db099f830bfa4e66852df6 Mon Sep 17 00:00:00 2001 From: Vince Lwt Date: Wed, 26 Oct 2016 22:22:27 +0200 Subject: [PATCH] Option to enable tray (disabled by default) --- app/js/main.js | 1 + app/js/settings.js | 2 + app/settings.html | 3 + main.js | 186 +++++++++++++++++++++++---------------------- 4 files changed, 101 insertions(+), 91 deletions(-) diff --git a/app/js/main.js b/app/js/main.js index c7d14b7..bd741e4 100644 --- a/app/js/main.js +++ b/app/js/main.js @@ -98,6 +98,7 @@ function getData() { notifOff: false, enableCoverflow: false, coverflow: false, + tray: false, repeat: true, shuffle: false, lastfm: { diff --git a/app/js/settings.js b/app/js/settings.js index 96c0769..58842c6 100644 --- a/app/js/settings.js +++ b/app/js/settings.js @@ -67,6 +67,7 @@ function resetAll() { settings = { volume: 1, notifOff: false, + tray: false, coverflow: false, enableCoverflow: false, repeat: true, @@ -121,6 +122,7 @@ function updateBtns() { getById("coverflow").checked = (settings.enableCoverflow ? true : false); getById("notifOff").checked = (settings.notifOff ? true : false); getById("dark").checked = (settings.dark ? true : false); + getById("tray").checked = (settings.tray ? true : false); } diff --git a/app/settings.html b/app/settings.html index 5cd5fc1..b865dd7 100644 --- a/app/settings.html +++ b/app/settings.html @@ -24,6 +24,9 @@

Your music



Enable dark mode (Ctrl/Cmd+D) +

+ Enable tray icon (restart needed) +

Disable notifications diff --git a/main.js b/main.js index 2f14a87..dba27c0 100644 --- a/main.js +++ b/main.js @@ -1,10 +1,11 @@ 'use strict'; const electron = require('electron'); -const {app, Menu, Tray} = require('electron') +const { app, Menu, Tray } = require('electron') const BrowserWindow = electron.BrowserWindow; const windowStateKeeper = require('electron-window-state'); - +const Configstore = require('configstore'); +var conf = new Configstore("harmony"); let willQuitApp = false; // Keep a global reference of the window object, if you don't, the window will @@ -13,86 +14,89 @@ let mainWindow; let tray = null; -function createWindow () { - let mainWindowState = windowStateKeeper({ - defaultWidth: 701, - defaultHeight: 450 - }); - - mainWindow = new BrowserWindow({ - height: mainWindowState.height, - resizable: true, - width: mainWindowState.width, - minWidth: 120, - minHeight: 38, - acceptFirstMouse: true, - icon: 'icon.png', - titleBarStyle: 'hidden' - }); - - - mainWindow.setMenu(null); - mainWindow.loadURL('file://' + __dirname + '/app/index.html'); - //mainWindow.webContents.openDevTools(); - - mainWindow.on('close', function(e) { - if (willQuitApp || process.platform !== 'darwin') { - /* the user tried to quit the app */ - mainWindow = null; - } else { - /* the user only tried to close the window */ - e.preventDefault(); - mainWindow.hide(); - } - }); - - mainWindowState.manage(mainWindow); - - - // Create the Application's main menu - if (process.platform == 'darwin') { // To enable shortcuts on OSX - - var template = [{ - label: "Harmony", - submenu: [ - { label: "Undo", accelerator: "CmdOrCtrl+Z", selector: "undo:" }, - { label: "Redo", accelerator: "Shift+CmdOrCtrl+Z", selector: "redo:" }, - { type: "separator" }, - { label: "Cut", accelerator: "CmdOrCtrl+X", selector: "cut:" }, - { label: "Copy", accelerator: "CmdOrCtrl+C", selector: "copy:" }, - { label: "Paste", accelerator: "CmdOrCtrl+V", selector: "paste:" }, - { label: "Select All", accelerator: "CmdOrCtrl+A", selector: "selectAll:" }, - { type: "separator" }, - { label: "Quit", accelerator: "Command+Q", click: function() { app.quit(); }} - ]} - ]; - - Menu.setApplicationMenu(Menu.buildFromTemplate(template)); - } - - - tray = new Tray(__dirname + '/icon.png') - var contextMenu = Menu.buildFromTemplate([ - { label: 'Favorite', click: function() { mainWindow.webContents.executeJavaScript("FavPlaying(true)") } }, - { label: 'Play/Pause', click: function() { mainWindow.webContents.executeJavaScript("playPause()") } }, - { label: 'Next', click: function() { mainWindow.webContents.executeJavaScript("nextTrack()") } }, - { label: 'Previous', click: function() { mainWindow.webContents.executeJavaScript("prevTrack()") } }, - { type: "separator" }, - { label: 'Show player', click: function() { mainWindow.show() } }, - { label: 'Hide player', click: function() { mainWindow.hide() } }, - { label: 'Quit', click: function() { app.quit() } } - ]); - - tray.on('click', function() { - if (process.platform == 'darwin' || process.platform == 'win32'){ - tray.popUpContextMenu(contextMenu); - } else { - mainWindow.isVisible() ? mainWindow.hide() : mainWindow.show(); - } - }); - - tray.setToolTip('Harmony Player') - tray.setContextMenu(contextMenu); +function createWindow() { + let mainWindowState = windowStateKeeper({ + defaultWidth: 701, + defaultHeight: 450 + }); + + mainWindow = new BrowserWindow({ + height: mainWindowState.height, + resizable: true, + width: mainWindowState.width, + minWidth: 120, + minHeight: 38, + acceptFirstMouse: true, + icon: 'icon.png', + titleBarStyle: 'hidden' + }); + + + mainWindow.setMenu(null); + mainWindow.loadURL('file://' + __dirname + '/app/index.html'); + //mainWindow.webContents.openDevTools(); + + mainWindow.on('close', function(e) { + if (willQuitApp || process.platform !== 'darwin') { + /* the user tried to quit the app */ + mainWindow = null; + } else { + /* the user only tried to close the window */ + e.preventDefault(); + mainWindow.hide(); + } + }); + + mainWindowState.manage(mainWindow); + + + // Create the Application's main menu + if (process.platform == 'darwin') { // To enable shortcuts on OSX + + var template = [{ + label: "Harmony", + submenu: [ + { label: "Undo", accelerator: "CmdOrCtrl+Z", selector: "undo:" }, + { label: "Redo", accelerator: "Shift+CmdOrCtrl+Z", selector: "redo:" }, + { type: "separator" }, + { label: "Cut", accelerator: "CmdOrCtrl+X", selector: "cut:" }, + { label: "Copy", accelerator: "CmdOrCtrl+C", selector: "copy:" }, + { label: "Paste", accelerator: "CmdOrCtrl+V", selector: "paste:" }, + { label: "Select All", accelerator: "CmdOrCtrl+A", selector: "selectAll:" }, + { type: "separator" }, + { label: "Quit", accelerator: "Command+Q", click: function() { app.quit(); } } + ] + }]; + + Menu.setApplicationMenu(Menu.buildFromTemplate(template)); + } + + + if (!conf.get("settings").tray) return; + + tray = new Tray(__dirname + '/icon.png'); + + var contextMenu = Menu.buildFromTemplate([ + { label: 'Favorite', click: function() { mainWindow.webContents.executeJavaScript("FavPlaying(true)") } }, + { label: 'Play/Pause', click: function() { mainWindow.webContents.executeJavaScript("playPause()") } }, + { label: 'Next', click: function() { mainWindow.webContents.executeJavaScript("nextTrack()") } }, + { label: 'Previous', click: function() { mainWindow.webContents.executeJavaScript("prevTrack()") } }, + { type: "separator" }, + { label: 'Show player', click: function() { mainWindow.show() } }, + { label: 'Hide player', click: function() { mainWindow.hide() } }, + { label: 'Quit', click: function() { app.quit() } } + ]); + + tray.on('click', function() { + if (process.platform == 'darwin' || process.platform == 'win32') { + tray.popUpContextMenu(contextMenu); + } else { + mainWindow.isVisible() ? mainWindow.hide() : mainWindow.show(); + } + }); + + tray.setToolTip('Harmony Player') + tray.setContextMenu(contextMenu); } @@ -103,18 +107,18 @@ app.setName('Harmony'); app.on('ready', createWindow); // 'activate' is emitted when the user clicks the Dock icon (OS X) -app.on('activate', function () { - mainWindow.show(); +app.on('activate', function() { + mainWindow.show(); }); // 'before-quit' is emitted when Electron receives // the signal to exit and wants to start closing windows -app.on('before-quit', function () { - willQuitApp = true; +app.on('before-quit', function() { + willQuitApp = true; }); -app.on('window-all-closed', function () { - if (process.platform !== 'darwin') { - app.quit(); - } -}); \ No newline at end of file +app.on('window-all-closed', function() { + if (process.platform !== 'darwin') { + app.quit(); + } +});