Skip to content
This repository has been archived by the owner on Sep 12, 2022. It is now read-only.

Commit

Permalink
Option to enable tray (disabled by default)
Browse files Browse the repository at this point in the history
  • Loading branch information
vincelwt committed Oct 26, 2016
1 parent 0137785 commit f7d1e2f
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 91 deletions.
1 change: 1 addition & 0 deletions app/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ function getData() {
notifOff: false,
enableCoverflow: false,
coverflow: false,
tray: false,
repeat: true,
shuffle: false,
lastfm: {
Expand Down
2 changes: 2 additions & 0 deletions app/js/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ function resetAll() {
settings = {
volume: 1,
notifOff: false,
tray: false,
coverflow: false,
enableCoverflow: false,
repeat: true,
Expand Down Expand Up @@ -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);

}

Expand Down
3 changes: 3 additions & 0 deletions app/settings.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ <h1>Your music</h1>
<br><br>
<input type="checkbox" id="dark" onchange="checkbox('dark', this.checked)"> Enable dark mode (Ctrl/Cmd+D)

<br><br>
<input type="checkbox" id="tray" onchange="checkbox('tray', this.checked)"> Enable tray icon (restart needed)

<br><br>
<input type="checkbox" id="notifOff" onchange="checkbox('notifOff', this.checked)"> Disable notifications

Expand Down
186 changes: 95 additions & 91 deletions main.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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);

}

Expand All @@ -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();
}
});
app.on('window-all-closed', function() {
if (process.platform !== 'darwin') {
app.quit();
}
});

0 comments on commit f7d1e2f

Please sign in to comment.