SymphonyElectron/js/main.js

122 lines
3.4 KiB
JavaScript
Raw Normal View History

2016-11-07 14:31:39 -06:00
const electron = require('electron');
2017-02-10 20:20:09 -06:00
const packageJSON = require('../package.json');
2016-11-07 14:31:39 -06:00
const menuTemplate = require('./menuTemplate.js');
2017-02-10 20:20:09 -06:00
const path = require('path');
2016-09-26 21:39:43 -05:00
const app = electron.app
2016-11-08 18:32:04 -06:00
const BrowserWindow = electron.BrowserWindow;
let willQuitApp = false;
2016-09-26 21:39:43 -05:00
2016-11-07 14:31:39 -06:00
if (require('electron-squirrel-startup')) return;
2016-11-08 18:32:04 -06:00
if (isDevEnv()) {
// needed for development env because local server doesn't have cert
app.commandLine.appendSwitch('--ignore-certificate-errors');
}
2016-09-26 21:39:43 -05:00
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
2016-11-07 14:31:39 -06:00
let mainWindow;
2017-02-10 20:20:09 -06:00
let childWindows = [];
2016-09-26 21:39:43 -05:00
2016-11-08 18:32:04 -06:00
function isDevEnv() {
var isDev = process.env.ELECTRON_DEV ?
process.env.ELECTRON_DEV.trim().toLowerCase() === "true" : false;
return isDev;
}
2017-02-10 20:20:09 -06:00
function createMainWindow () {
2016-11-07 14:31:39 -06:00
// note: for now, turning off node integration as this is causing failure with
// onelogin, jquery can not get initialized. electron's node integration
// conflicts on the window object.
mainWindow = new BrowserWindow({
title: 'Symphony',
width: 1024, height: 768,
webPreferences: {
sandbox: false,
2017-02-10 20:20:09 -06:00
nodeIntegration: false,
preload: path.join(__dirname, '/main-preload.js')
2016-11-07 14:31:39 -06:00
}
});
mainWindow.loadURL(packageJSON.homepage);
2017-02-10 20:20:09 -06:00
2016-11-08 18:32:04 -06:00
const menu = electron.Menu.buildFromTemplate(menuTemplate(app));
electron.Menu.setApplicationMenu(menu);
mainWindow.on('close', function(e) {
if (willQuitApp) {
mainWindow = null;
return;
}
// mac should hide window when hitting x close
if (process.platform === 'darwin') {
mainWindow.hide();
e.preventDefault();
}
});
2016-09-26 21:39:43 -05:00
mainWindow.on('closed', function () {
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
2016-11-07 14:31:39 -06:00
mainWindow = null;
2016-11-08 18:32:04 -06:00
});
2016-11-08 19:12:36 -06:00
2017-02-10 20:20:09 -06:00
// open external links in default browser - window.open
2016-11-08 19:12:36 -06:00
mainWindow.webContents.on('new-window', function(event, url) {
event.preventDefault();
electron.shell.openExternal(url);
});
2016-09-26 21:39:43 -05:00
}
2017-02-10 20:20:09 -06:00
electron.ipcMain.on('symphony-msg', (event, arg) => {
if (arg && arg.cmd === 'open' && arg.url) {
var width = arg.width || 1024;
var height = arg.height || 768;
var title = arg.title || 'Symphony';
let childWindow = new BrowserWindow({
title: title,
width: width,
height: height,
webPreferences: {
sandbox: false,
nodeIntegration: false,
preload: path.join(__dirname, '/child-preload.js')
}
});
childWindows.push(childWindow);
childWindow.loadURL(arg.url);
return;
}
});
2016-09-26 21:39:43 -05: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.
2016-11-08 18:32:04 -06:00
app.on('ready', function() {
2017-02-10 20:20:09 -06:00
createMainWindow();
2016-11-08 18:32:04 -06:00
});
app.on('before-quit', function() {
willQuitApp = true;
});
2016-09-26 21:39:43 -05:00
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 (process.platform !== 'darwin') {
2016-11-08 18:32:04 -06:00
app.quit();
2016-09-26 21:39:43 -05:00
}
})
app.on('activate', function () {
if (mainWindow === null) {
2017-02-10 20:20:09 -06:00
createMainWindow();
2016-11-08 18:32:04 -06:00
} else {
mainWindow.show();
2016-09-26 21:39:43 -05:00
}
2016-11-07 14:31:39 -06:00
});