Auto Launch (#75)

* Auto Launch

* Applying comments

* fix app.quit
This commit is contained in:
rbarros-daitan 2017-05-09 18:44:14 -03:00 committed by Lynn
parent 1df319ee5b
commit c0908e64ef
5 changed files with 142 additions and 13 deletions

View File

@ -1,3 +1,5 @@
{
"url": "https://foundation-dev.symphony.com"
"url": "https://foundation-dev.symphony.com",
"minimizeOnClose" : true,
"launchOnStartup" : true
}

View File

@ -340,6 +340,7 @@
<ROW Action="SET_APPDIR" Type="307" Source="APPDIR" Target="[ProgramFilesFolder][Manufacturer]\[ProductName]" MultiBuildTarget="DefaultBuild:[AI_UserProgramFiles][Manufacturer]\[ProductName]"/>
<ROW Action="SET_SHORTCUTDIR" Type="307" Source="SHORTCUTDIR" Target="[ProgramMenuFolder][ProductName]"/>
<ROW Action="SET_TARGETDIR_TO_APPDIR" Type="51" Source="TARGETDIR" Target="[APPDIR]"/>
<ROW Action="Symphony.exe" Type="1042" Source="Symphony.exe" Target="--install"/>
</COMPONENT>
<COMPONENT cid="caphyon.advinst.msicomp.MsiIconsComponent">
<ROW Name="icon.exe" SourcePath="..\build\icon.ico" Index="0"/>
@ -356,6 +357,7 @@
<ROW Action="AI_DATA_SETTER" Sequence="54"/>
<ROW Action="AI_SETMIXINSTLOCATION" Sequence="749"/>
<ROW Action="AI_TxtUpdaterInstall" Sequence="5101"/>
<ROW Action="Symphony.exe" Condition="( NOT Installed )" Sequence="5935"/>
</COMPONENT>
<COMPONENT cid="caphyon.advinst.msicomp.MsiInstallUISequenceComponent">
<ROW Action="AI_ResolveKnownFolders" Sequence="52"/>

View File

@ -4,7 +4,7 @@ const electron = require('electron');
const app = electron.app;
const nodeURL = require('url');
const squirrelStartup = require('electron-squirrel-startup');
const AutoLaunch = require('auto-launch');
const { getConfigField } = require('./config.js');
const { isMac, isDevEnv } = require('./utils/misc.js');
@ -37,6 +37,11 @@ if (shouldQuit) {
app.quit();
}
var symphonyAutoLauncher = new AutoLaunch({
name: 'Symphony',
path: process.execPath,
});
/**
* This method will be called when Electron has finished
* initialization and is ready to create browser windows.
@ -45,7 +50,52 @@ if (shouldQuit) {
app.on('ready', getUrlAndOpenMainWindow);
function getUrlAndOpenMainWindow() {
// for dev env allow passing url argument
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) => {
@ -60,13 +110,13 @@ function getUrlAndOpenMainWindow() {
}
getConfigField('url')
.then(createWin).catch(function (err){
let title = 'Error loading configuration';
electron.dialog.showErrorBox(title, title + ': ' + err);
});
.then(createWin).catch(function (err) {
let title = 'Error loading configuration';
electron.dialog.showErrorBox(title, title + ': ' + err);
});
}
function createWin(urlFromConfig){
function createWin(urlFromConfig) {
let protocol = '';
// add https protocol if none found.
let parsedUrl = nodeURL.parse(urlFromConfig);
@ -81,7 +131,7 @@ function createWin(urlFromConfig){
windowMgr.createMainWindow(url);
}
app.on('window-all-closed', function() {
app.on('window-all-closed', function () {
// 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) {
@ -89,7 +139,7 @@ app.on('window-all-closed', function() {
}
});
app.on('activate', function() {
app.on('activate', function () {
if (windowMgr.isMainWindow(null)) {
getUrlAndOpenMainWindow();
} else {

View File

@ -1,6 +1,19 @@
'use strict';
const electron = require('electron');
const { getConfigField, updateConfigField } = require('../config.js');
const AutoLaunch = require('auto-launch');
const isMac = require('../utils/misc.js').isMac;
var minimizeOnClose = false;
var launchOnStartup = false;
setCheckboxValues();
var symphonyAutoLauncher = new AutoLaunch({
name: 'Symphony',
path: process.execPath,
});
const template = [
{
@ -31,7 +44,7 @@ const template = [
},
{
label: 'Toggle Developer Tools',
accelerator: process.platform === 'darwin' ? 'Alt+Command+I' : 'Ctrl+Shift+I',
accelerator: isMac ? 'Alt+Command+I' : 'Ctrl+Shift+I',
click (item, focusedWindow) {
if (focusedWindow) {
focusedWindow.webContents.toggleDevTools();
@ -81,7 +94,7 @@ const template = [
];
function getTemplate(app) {
if (process.platform === 'darwin' && template[0].label !== app.getName()) {
if (isMac && template[0].label !== app.getName()) {
template.unshift({
label: app.getName(),
submenu: [
@ -158,7 +171,68 @@ function getTemplate(app) {
]
}
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){
symphonyAutoLauncher.enable()
.catch(function (err) {
let title = 'Error setting AutoLaunch configuration';
electron.dialog.showErrorBox(title, title + ': ' + err);
});
} else {
symphonyAutoLauncher.disable()
.catch(function (err) {
let title = 'Error setting AutoLaunch configuration';
electron.dialog.showErrorBox(title, title + ': ' + err);
});
}
launchOnStartup = item.checked;
updateConfigField('launchOnStartup', launchOnStartup);
}
}
)
// 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);
}
}
)
return template;
}
module.exports = getTemplate;
function setCheckboxValues(){
getConfigField('minimizeOnClose').then(function(mClose) {
minimizeOnClose = mClose;
}).catch(function (err){
let title = 'Error loading configuration';
electron.dialog.showErrorBox(title, title + ': ' + err);
});
getConfigField('launchOnStartup').then(function(lStartup) {
launchOnStartup = lStartup;
}).catch(function (err){
let title = 'Error loading configuration';
electron.dialog.showErrorBox(title, title + ': ' + err);
});
}
module.exports = getTemplate;

View File

@ -85,6 +85,7 @@
"dependencies": {
"@paulcbetts/system-idle-time": "^1.0.4",
"async": "^2.1.5",
"auto-launch": "^5.0.1",
"electron-context-menu": "^0.8.0",
"electron-rebuild": "^1.5.7",
"electron-squirrel-startup": "^1.0.0",