SymphonyElectron/js/main.js

172 lines
5.0 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
2017-02-12 18:57:56 -06: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.
let mainWindow;
let windows = {};
2016-11-08 18:32:04 -06:00
let willQuitApp = false;
2016-09-26 21:39:43 -05:00
2017-02-12 18:57:56 -06:00
if (require('electron-squirrel-startup')) {
return;
}
2016-11-07 14:31:39 -06:00
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');
}
function isDevEnv() {
2017-02-12 18:57:56 -06:00
let isDev = process.env.ELECTRON_DEV ?
2016-11-08 18:32:04 -06:00
process.env.ELECTRON_DEV.trim().toLowerCase() === "true" : false;
return isDev;
}
2017-02-10 20:20:09 -06:00
function createMainWindow () {
2017-02-12 18:57:56 -06:00
let key = getWindowKey();
// 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 electron.BrowserWindow({
title: 'Symphony',
width: 1024, height: 768,
webPreferences: {
2017-02-12 19:11:02 -06:00
sandbox: true,
2017-02-12 18:57:56 -06:00
nodeIntegration: false,
preload: path.join(__dirname, '/main-preload.js'),
winKey: key
}
});
storeWindowKey(key, mainWindow)
mainWindow.loadURL(packageJSON.homepage);
2017-02-12 19:11:02 -06:00
2017-02-12 18:57:56 -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();
}
});
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-08 18:32:04 -06:00
mainWindow = null;
2017-02-12 18:57:56 -06:00
});
// open external links in default browser - window.open
mainWindow.webContents.on('new-window', function(event, url) {
event.preventDefault();
electron.shell.openExternal(url);
});
}
function getWindowKey() {
// generate guid:
// http://stackoverflow.com/questions/105034/create-guid-uuid-in-javascript
function s4() {
return Math.floor((1 + Math.random()) * 0x10000).toString(16)
.substring(1);
2016-11-08 18:32:04 -06:00
}
2017-02-12 18:57:56 -06:00
return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
s4() + '-' + s4() + s4() + s4();
}
function storeWindowKey(key, browserWin) {
windows[key] = browserWin;
}
/**
* Ensure events comes from a window that we have created.
* @param {EventEmitter} event node emitter event to be tested
* @return {Boolean} returns true if exists otherwise false
*/
function isValidWindow(event) {
if (event && event.sender) {
// validate that event sender is from window we created
let browserWin = electron.BrowserWindow.fromWebContents(event.sender)
let winKey = event.sender.browserWindowOptions &&
event.sender.browserWindowOptions.webPreferences &&
event.sender.browserWindowOptions.webPreferences.winKey;
if (browserWin instanceof electron.BrowserWindow) {
let win = windows[winKey];
return win && win === browserWin;
}
2016-11-08 18:32:04 -06:00
}
2017-02-12 18:57:56 -06:00
return false;
2016-09-26 21:39:43 -05:00
}
2017-02-10 20:20:09 -06:00
electron.ipcMain.on('symphony-msg', (event, arg) => {
2017-02-12 18:57:56 -06:00
if (!isValidWindow(event)) {
console.log('invalid window try to perform action, ignoring action.');
return;
}
2017-02-10 20:20:09 -06:00
if (arg && arg.cmd === 'open' && arg.url) {
2017-02-12 18:57:56 -06:00
let width = arg.width || 1024;
let height = arg.height || 768;
let title = arg.title || 'Symphony';
let winKey = getWindowKey();
2017-02-10 20:20:09 -06:00
2017-02-12 18:57:56 -06:00
let childWindow = new electron.BrowserWindow({
2017-02-10 20:20:09 -06:00
title: title,
width: width,
height: height,
webPreferences: {
2017-02-12 19:11:02 -06:00
sandbox: true,
2017-02-10 20:20:09 -06:00
nodeIntegration: false,
2017-02-12 18:57:56 -06:00
preload: path.join(__dirname, '/child-preload.js'),
winKey: winKey
2017-02-10 20:20:09 -06:00
}
});
2017-02-12 18:57:56 -06:00
storeWindowKey(winKey, childWindow);
2017-02-10 20:20:09 -06:00
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 () {
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 (process.platform !== 'darwin') {
app.quit();
}
});
2016-09-26 21:39:43 -05:00
app.on('activate', function () {
2017-02-12 18:57:56 -06:00
if (mainWindow === null) {
createMainWindow();
} else {
mainWindow.show();
}
2016-11-07 14:31:39 -06:00
});