2015-07-05 06:08:13 +00:00
|
|
|
/**
|
|
|
|
* Created by JiaHao on 4/7/15.
|
|
|
|
*/
|
|
|
|
|
|
|
|
var fs = require('fs');
|
2016-01-22 11:57:39 +00:00
|
|
|
var path = require('path');
|
2016-01-19 03:28:04 +00:00
|
|
|
var electron = require('electron');
|
2016-01-23 02:09:47 +00:00
|
|
|
var createMainWindow = require('./components/mainWindow/mainWindow');
|
|
|
|
var createLoginWindow = require('./components/login/loginWindow');
|
|
|
|
var helpers = require('./helpers/helpers');
|
2016-01-19 03:28:04 +00:00
|
|
|
var app = electron.app;
|
2016-01-21 18:39:52 +00:00
|
|
|
var ipcMain = electron.ipcMain;
|
2016-01-23 02:09:47 +00:00
|
|
|
var isOSX = helpers.isOSX;
|
2016-01-19 11:53:10 +00:00
|
|
|
|
2016-01-22 11:57:39 +00:00
|
|
|
const APP_ARGS_FILE_PATH = path.join(__dirname, '..', 'nativefier.json');
|
2015-07-05 06:08:13 +00:00
|
|
|
|
2015-07-06 02:31:09 +00:00
|
|
|
var appArgs = JSON.parse(fs.readFileSync(APP_ARGS_FILE_PATH, 'utf8'));
|
|
|
|
|
2016-01-23 02:09:47 +00:00
|
|
|
var mainWindow;
|
|
|
|
|
2016-01-23 07:12:53 +00:00
|
|
|
// do nothing for setDockBadge if not OSX
|
|
|
|
let setDockBadge = () => {};
|
2016-01-23 18:02:23 +00:00
|
|
|
|
2016-01-23 07:12:53 +00:00
|
|
|
if (isOSX()) {
|
|
|
|
setDockBadge = app.dock.setBadge;
|
|
|
|
}
|
|
|
|
|
2016-01-23 18:02:23 +00:00
|
|
|
app.on('window-all-closed', function() {
|
2016-01-21 04:25:33 +00:00
|
|
|
if (!isOSX()) {
|
2015-07-05 06:08:13 +00:00
|
|
|
app.quit();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-01-23 18:02:23 +00:00
|
|
|
app.on('activate', function(event, hasVisibleWindows) {
|
2016-01-21 04:25:33 +00:00
|
|
|
if (isOSX()) {
|
|
|
|
// this is called when the dock is clicked
|
|
|
|
if (!hasVisibleWindows) {
|
|
|
|
mainWindow.show();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-01-23 18:02:23 +00:00
|
|
|
app.on('before-quit', function() {
|
2016-01-21 04:25:33 +00:00
|
|
|
// not fired when the close button on the window is clicked
|
|
|
|
if (isOSX()) {
|
|
|
|
// need to force a quit as a workaround here to simulate the osx app hiding behaviour
|
|
|
|
// Somehow sokution at https://github.com/atom/electron/issues/444#issuecomment-76492576 does not work,
|
|
|
|
// e.prevent default appears to persist
|
|
|
|
|
|
|
|
// might cause issues in the future as before-quit and will-quit events are not called
|
|
|
|
app.exit(0);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-01-23 18:02:23 +00:00
|
|
|
app.on('ready', function() {
|
2016-01-23 07:12:53 +00:00
|
|
|
mainWindow = createMainWindow(appArgs, app.quit, setDockBadge);
|
2015-07-06 01:38:04 +00:00
|
|
|
});
|
2015-07-05 06:08:13 +00:00
|
|
|
|
2016-01-21 18:39:52 +00:00
|
|
|
app.on('login', function(event, webContents, request, authInfo, callback) {
|
2016-01-23 02:09:47 +00:00
|
|
|
// for http authentication
|
2016-01-21 18:39:52 +00:00
|
|
|
event.preventDefault();
|
2016-01-23 02:09:47 +00:00
|
|
|
createLoginWindow(callback);
|
2016-01-21 18:39:52 +00:00
|
|
|
});
|
|
|
|
|
2016-01-23 05:43:33 +00:00
|
|
|
ipcMain.on('notification', function(event, title, opts) {
|
|
|
|
if (!isOSX() || mainWindow.isFocused()) {
|
|
|
|
return;
|
|
|
|
}
|
2016-01-23 07:12:53 +00:00
|
|
|
setDockBadge('●');
|
2016-01-23 05:43:33 +00:00
|
|
|
});
|