setup dynamic tray menu depending on server state

This commit is contained in:
Stephen Birarda 2015-12-28 12:39:14 -08:00
parent 2bbfda5da7
commit cd3762a889

View file

@ -12,6 +12,7 @@ var path = require('path');
var hfprocess = require('./modules/hf-process.js');
var Process = hfprocess.Process;
var ProcessGroup = hfprocess.ProcessGroup;
var ProcessGroupStates = hfprocess.ProcessGroupStates;
const ipcMain = electron.ipcMain;
@ -20,6 +21,9 @@ var tray = null;
var path = require('path');
var TRAY_ICON = path.join(__dirname, '../resources/console-tray.png');
// print out uncaught exceptions in the console
process.on('uncaughtException', console.log.bind(console));
var shouldQuit = app.makeSingleInstance(function(commandLine, workingDirectory) {
// Someone tried to run a second instance, focus the window (if there is one)
return true;
@ -46,6 +50,9 @@ if (argv.localDebugBuilds || argv.localReleaseBuilds) {
acPath = pathFinder.discoveredPath("assignment-client", argv.localReleaseBuilds);
}
// if at this point any of the paths are null, we're missing something we wanted to find
// TODO: show an error for the binaries that couldn't be found
function openFileBrowser(path) {
var type = os.type();
if (type == "Windows_NT") {
@ -57,9 +64,6 @@ function openFileBrowser(path) {
}
}
// if at this point any of the paths are null, we're missing something we wanted to find
// TODO: show an error for the binaries that couldn't be found
function startInterface(url) {
var argArray = [];
@ -73,20 +77,44 @@ function startInterface(url) {
pInterface.start();
}
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
app.on('ready', function() {
// hide the dock icon
app.dock.hide()
var homeServer = null;
// Create tray icon
tray = new Tray(TRAY_ICON);
tray.setToolTip('High Fidelity');
const GO_HOME_INDEX = 0;
const SERVER_LABEL_INDEX = 2;
const RESTART_INDEX = 3;
const SETTINGS_INDEX = 5;
var contextMenu = Menu.buildFromTemplate([
function buildMenuArray(serverState) {
var menuArray = [
{
label: 'Go Home',
click: function() { startInterface('hifi://localhost'); }
click: function() { startInterface('hifi://localhost'); },
enabled: false
},
{
type: 'separator'
},
{
label: "Server - Stopped",
enabled: false
},
{
label: "Start",
click: function() { homeServer.restart(); }
},
{
label: "Stop",
visible: false,
click: function() { homeServer.stop(); }
},
{
label: "Settings",
click: function() { shell.openExternal('http://localhost:40100/settings'); },
enabled: false
},
{
label: "View Logs",
click: function() { openFileBrowser(logPath); }
},
{
type: 'separator'
@ -96,14 +124,65 @@ app.on('ready', function() {
accelerator: 'Command+Q',
click: function() { app.quit(); }
}
]);
];
tray.setContextMenu(contextMenu);
updateMenuArray(menuArray, serverState);
return menuArray;
}
function updateMenuArray(menuArray, serverState) {
// update the tray menu state
var running = serverState == ProcessGroupStates.STARTED;
var serverLabelItem = menuArray[SERVER_LABEL_INDEX];
var restartItem = menuArray[RESTART_INDEX];
// Go Home is only enabled if running
menuArray[GO_HOME_INDEX].enabled = running;
// Stop is only visible if running
menuArray[RESTART_INDEX + 1].visible = running;
// Settings is only visible if running
menuArray[SETTINGS_INDEX].enabled = running;
if (serverState == ProcessGroupStates.STARTED) {
serverLabelItem.label = "Server - Started";
restartItem.label = "Restart";
} else if (serverState == ProcessGroupStates.STOPPED) {
serverLabelItem.label = "Server - Stopped";
restartItem.label = "Start";
} else if (serverState == ProcessGroupStates.STOPPING) {
serverLabelItem.label = "Server - Stopping";
restartItem.label = "Restart";
}
}
function updateTrayMenu(serverState) {
if (tray) {
var menuArray = buildMenuArray(serverState);
console.log(menuArray);
tray.setContextMenu(Menu.buildFromTemplate(menuArray));
}
}
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
app.on('ready', function() {
// hide the dock icon
app.dock.hide()
var logPath = path.join(app.getAppPath(), 'logs');
// Create tray icon
tray = new Tray(TRAY_ICON);
tray.setToolTip('High Fidelity');
updateTrayMenu(ProcessGroupStates.STOPPED);
if (interfacePath && dsPath && acPath) {
var homeServer = new ProcessGroup('home', [
homeServer = new ProcessGroup('home', [
new Process('domain-server', dsPath),
new Process('ac-monitor', acPath, ['-n6', '--log-directory', logPath])
]);
@ -119,35 +198,9 @@ app.on('ready', function() {
// handle process updates
// homeServer.on('process-update', sendProcessUpdate);
// homeServer.on('state-update', sendProcessGroupUpdate);
homeServer.on('state-update', function(processGroup) { updateTrayMenu(processGroup.state); });
// start the home server
homeServer.start();
ipcMain.on('start-interface', function(event, arg) {
});
ipcMain.on('restart-server', function(event, arg) {
homeServer.restart();
});
ipcMain.on('stop-server', function(event, arg) {
homeServer.stop();
});
ipcMain.on('open-logs', function(event, arg) {
openFileBrowser(logPath);
});
ipcMain.on('update-all-processes', function(event, arg) {
// // enumerate our processes and call sendProcessUpdate to update
// // the window with their status
// for (let process of homeServer.processes) {
// sendProcessUpdate(process);
// }
//
// sendProcessGroupUpdate(homeServer);
});
}
});