SymphonyElectron/js/menus/menuTemplate.js
Kiran Niranjan b72e95fd37 Electron-56 (Always on top) (#127)
* ELECTRON-56 - Implemented always on top feature

* ELECTRON-56 - Made some code refactoring

* ELECTRON-56 - Also fetching the alwaysOnTop prop from the event emitter

* ELECTRON-56 - Refactored code

* ELECTRON-56 - Exported only the required methods in event emitter
2017-06-15 10:11:29 -07:00

313 lines
9.6 KiB
JavaScript

'use strict';
const electron = require('electron');
const { getConfigField, updateConfigField } = require('../config.js');
const AutoLaunch = require('auto-launch');
const isMac = require('../utils/misc.js').isMac;
const childProcess = require('child_process');
const log = require('../log.js');
const logLevels = require('../enums/logLevels.js');
const eventEmitter = require('../eventEmitter');
var minimizeOnClose = false;
var launchOnStartup = false;
var isAlwaysOnTop = false;
setCheckboxValues();
var symphonyAutoLauncher = new AutoLaunch({
name: 'Symphony',
path: process.execPath,
});
let launchAgentPath = '~/Library/LaunchAgents/com.symphony.symphony-desktop.agent.plist';
const template = [
{
label: 'Edit',
submenu: [
{ role: 'undo' },
{ role: 'redo' },
{ type: 'separator' },
{ role: 'cut' },
{ role: 'copy' },
{ role: 'paste' },
{ role: 'pasteandmatchstyle' },
{ role: 'delete' },
{ role: 'selectall' }
]
},
{
label: 'View',
submenu: [
{
label: 'Reload',
accelerator: 'CmdOrCtrl+R',
click (item, focusedWindow) {
if (focusedWindow) {
focusedWindow.reload();
}
}
},
{
label: 'Toggle Developer Tools',
accelerator: isMac ? 'Alt+Command+I' : 'Ctrl+Shift+I',
click (item, focusedWindow) {
if (focusedWindow) {
focusedWindow.webContents.toggleDevTools();
}
}
},
{
type: 'separator'
},
{
role: 'resetzoom'
},
{
role: 'zoomin'
},
{
role: 'zoomout'
},
{
type: 'separator'
},
{
role: 'togglefullscreen'
}
]
},
{
role: 'window',
submenu: [
{
role: 'minimize'
},
{
role: 'close'
}
]
},
{
role: 'help',
submenu: [
{
label: 'Learn More',
click () { electron.shell.openExternal('https://www.symphony.com') }
}
]
}
];
function getTemplate(app) {
if (isMac && template[0].label !== app.getName()) {
template.unshift({
label: app.getName(),
submenu: [
{
role: 'about'
},
{
type: 'separator'
},
{
role: 'services',
submenu: []
},
{
type: 'separator'
},
{
role: 'hide'
},
{
role: 'hideothers'
},
{
role: 'unhide'
},
{
type: 'separator'
},
{
role: 'quit'
}
]
});
// Edit menu.
template[1].submenu.push(
{
type: 'separator'
},
{
label: 'Speech',
submenu: [
{
role: 'startspeaking'
},
{
role: 'stopspeaking'
}
]
}
)
// Window menu.
template[3].submenu = [
{
label: 'Close',
accelerator: 'CmdOrCtrl+W',
role: 'close'
},
{
label: 'Minimize',
accelerator: 'CmdOrCtrl+M',
role: 'minimize'
},
{
label: 'Zoom',
role: 'zoom'
},
{
type: 'separator'
},
{
label: 'Bring All to Front',
role: 'front'
}
]
}
var index = 2;
if (isMac && template[0].label !== app.getName()){
index = 3;
}
// Window menu -> launchOnStartup.
template[index].submenu.push(
{
label: 'Auto Launch On Startup',
type: 'checkbox',
checked: launchOnStartup,
click: function (item) {
if (item.checked){
if (isMac){
// TODO: Need to change this implementation to AutoLaunch once they fix this issue ->
// https://github.com/Teamwork/node-auto-launch/issues/28
childProcess.exec(`launchctl load ${launchAgentPath}`, (err) => {
if (err){
let title = 'Error setting AutoLaunch configuration';
log.send(logLevels.ERROR, 'MenuTemplate: ' + title + ': process error ' + err);
electron.dialog.showErrorBox(title, 'Please try reinstalling the application');
}
});
} else {
symphonyAutoLauncher.enable()
.catch(function (err) {
let title = 'Error setting AutoLaunch configuration';
log.send(logLevels.ERROR, 'MenuTemplate: ' + title + ': auto launch error ' + err);
electron.dialog.showErrorBox(title, title + ': ' + err);
});
}
} else {
if (isMac){
// TODO: Need to change this implementation to AutoLaunch once they fix this issue ->
// https://github.com/Teamwork/node-auto-launch/issues/28
childProcess.exec(`launchctl unload ${launchAgentPath}`, (err) => {
if (err){
let title = 'Error disabling AutoLaunch configuration';
log.send(logLevels.ERROR, 'MenuTemplate: ' + title + ': process error ' + err);
electron.dialog.showErrorBox(title, 'Please try reinstalling the application');
}
});
} else {
symphonyAutoLauncher.disable()
.catch(function (err) {
let title = 'Error setting AutoLaunch configuration';
log.send(logLevels.ERROR, 'MenuTemplate: ' + title + ': auto launch error ' + err);
electron.dialog.showErrorBox(title, title + ': ' + err);
});
}
}
launchOnStartup = item.checked;
updateConfigField('launchOnStartup', launchOnStartup);
}
}
)
// Window menu -> alwaysOnTop.
template[index].submenu.push(
{
label: 'Always on top',
type: 'checkbox',
checked: isAlwaysOnTop,
click: (item) => {
isAlwaysOnTop = item.checked;
eventEmitter.emit('isAlwaysOnTop', isAlwaysOnTop);
updateConfigField('alwaysOnTop', isAlwaysOnTop);
}
}
)
// Window menu -> minimizeOnClose.
// ToDo: Add behavior on Close.
template[index].submenu.push(
{
label: 'Minimize on Close',
type: 'checkbox',
checked: minimizeOnClose,
click: function (item) {
minimizeOnClose = item.checked;
updateConfigField('minimizeOnClose', minimizeOnClose);
}
}
)
if (!isMac){
template[index].submenu.push(
{
label: 'Quit Symphony',
click: function () {
app.quit();
}
}
)
}
return template;
}
function setCheckboxValues(){
getConfigField('minimizeOnClose').then(function(mClose) {
minimizeOnClose = mClose;
}).catch(function (err){
let title = 'Error loading configuration';
log.send(logLevels.ERROR, 'MenuTemplate: error getting config field minimizeOnClose, error: ' + err);
electron.dialog.showErrorBox(title, title + ': ' + err);
});
getConfigField('launchOnStartup').then(function(lStartup) {
launchOnStartup = lStartup;
}).catch(function (err){
let title = 'Error loading configuration';
log.send(logLevels.ERROR, 'MenuTemplate: error getting config field launchOnStartup, error: ' + err);
electron.dialog.showErrorBox(title, title + ': ' + err);
});
getConfigField('alwaysOnTop').then(function(mAlwaysOnTop) {
isAlwaysOnTop = mAlwaysOnTop;
eventEmitter.emit('isAlwaysOnTop', isAlwaysOnTop);
}).catch(function (err){
let title = 'Error loading configuration';
log.send(logLevels.ERROR, 'MenuTemplate: error getting config field alwaysOnTop, error: ' + err);
electron.dialog.showErrorBox(title, title + ': ' + err);
});
}
function getMinimizeOnClose(){
return minimizeOnClose;
}
module.exports = {
getTemplate : getTemplate,
getMinimizeOnClose : getMinimizeOnClose
};