SymphonyElectron/js/main.js

149 lines
3.5 KiB
JavaScript
Raw Normal View History

2017-02-14 19:44:15 -06:00
'use strict';
2016-11-07 14:31:39 -06:00
const electron = require('electron');
const app = electron.app;
const nodeURL = require('url');
2017-03-03 18:07:48 -06:00
const squirrelStartup = require('electron-squirrel-startup');
const AutoLaunch = require('auto-launch');
const { getConfigField } = require('./config.js');
const { isMac, isDevEnv } = require('./utils/misc.js');
2016-09-26 21:39:43 -05:00
2017-03-01 18:32:21 -06:00
// exit early for squirrel installer
2017-03-03 18:07:48 -06:00
if (squirrelStartup) {
2017-02-12 18:57:56 -06:00
return;
}
2016-11-07 14:31:39 -06:00
2017-03-01 18:32:21 -06:00
require('./mainApiMgr.js');
2017-02-14 19:44:15 -06:00
2017-03-01 18:32:21 -06:00
// monitor memory of main process
require('./memoryMonitor.js');
2017-02-10 20:20:09 -06:00
2017-03-01 18:32:21 -06:00
const windowMgr = require('./windowMgr.js');
2017-02-10 20:20:09 -06:00
// only allow a single instance of app.
const shouldQuit = app.makeSingleInstance(() => {
// Someone tried to run a second instance, we should focus our window.
let mainWin = windowMgr.getMainWindow();
if (mainWin) {
if (mainWin.isMinimized()) {
mainWin.restore();
}
mainWin.focus();
}
});
// quit if another instance is already running
if (shouldQuit) {
app.quit();
}
var symphonyAutoLauncher = new AutoLaunch({
name: 'Symphony',
path: process.execPath,
});
2017-02-13 18:31:42 -06:00
/**
* This method will be called when Electron has finished
* initialization and is ready to create browser windows.
* Some APIs can only be used after this event occurs.
*/
app.on('ready', getUrlAndOpenMainWindow);
function getUrlAndOpenMainWindow() {
let installMode = false;
process.argv.some((val) => {
let flag = '--install';
if (val === flag) {
installMode = true;
getConfigField('launchOnStartup')
.then(setStartup);
}
return false;
});
if (installMode === false){
openMainWindow();
}
}
function setStartup(lStartup){
if (lStartup === true){
symphonyAutoLauncher.isEnabled()
.then(function(isEnabled){
if(isEnabled){
app.quit();
}
symphonyAutoLauncher.enable()
.then(function (){
app.quit();
});
})
} else{
symphonyAutoLauncher.isEnabled()
.then(function(isEnabled){
if(isEnabled){
symphonyAutoLauncher.disable()
.then(function (){
app.quit();
});
} else{
app.quit();
}
})
}
}
function openMainWindow(){
if (isDevEnv) {
let url;
process.argv.forEach((val) => {
if (val.startsWith('--url=')) {
url = val.substr(6);
}
});
if (url) {
windowMgr.createMainWindow(url);
return;
}
}
getConfigField('url')
.then(createWin).catch(function (err) {
let title = 'Error loading configuration';
electron.dialog.showErrorBox(title, title + ': ' + err);
});
}
function createWin(urlFromConfig) {
let protocol = '';
// add https protocol if none found.
let parsedUrl = nodeURL.parse(urlFromConfig);
if (!parsedUrl.protocol) {
protocol = 'https';
}
var url = nodeURL.format({
protocol: protocol,
slahes: true,
pathname: parsedUrl.href
});
windowMgr.createMainWindow(url);
}
2016-11-08 18:32:04 -06:00
app.on('window-all-closed', function () {
2017-02-12 18:57:56 -06:00
// On OS X it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (!isMac) {
2017-02-12 18:57:56 -06:00
app.quit();
}
});
2016-09-26 21:39:43 -05:00
app.on('activate', function () {
2017-03-01 18:32:21 -06:00
if (windowMgr.isMainWindow(null)) {
getUrlAndOpenMainWindow();
2017-02-12 18:57:56 -06:00
} else {
2017-03-01 18:32:21 -06:00
windowMgr.showMainWindow();
2017-02-12 18:57:56 -06:00
}
2016-11-07 14:31:39 -06:00
});