2016-01-24 14:20:29 +01:00
|
|
|
import 'source-map-support/register';
|
2016-01-29 15:04:41 +01:00
|
|
|
import fs from 'fs';
|
|
|
|
import path from 'path';
|
|
|
|
import electron from 'electron';
|
|
|
|
import createLoginWindow from './components/login/loginWindow';
|
|
|
|
import createMainWindow from './components/mainWindow/mainWindow';
|
|
|
|
import helpers from './helpers/helpers';
|
2016-01-24 14:20:29 +01:00
|
|
|
|
2016-01-29 15:04:41 +01:00
|
|
|
const {app, ipcMain} = electron;
|
|
|
|
const {isOSX} = helpers;
|
2016-01-19 12:53:10 +01:00
|
|
|
|
2016-01-22 12:57:39 +01:00
|
|
|
const APP_ARGS_FILE_PATH = path.join(__dirname, '..', 'nativefier.json');
|
2016-01-29 15:04:41 +01:00
|
|
|
const appArgs = JSON.parse(fs.readFileSync(APP_ARGS_FILE_PATH, 'utf8'));
|
2015-07-06 04:31:09 +02:00
|
|
|
|
2016-01-27 16:30:54 +01:00
|
|
|
const DEFAULT_ICON_PATH = path.join(__dirname, '/icon.png');
|
|
|
|
const Tray = electron.Tray;
|
2016-01-27 17:21:40 +01:00
|
|
|
const Menu = electron.Menu;
|
2016-01-27 16:30:54 +01:00
|
|
|
|
2016-01-29 15:04:41 +01:00
|
|
|
let mainWindow;
|
2016-01-23 03:09:47 +01:00
|
|
|
|
2016-01-25 16:42:28 +01:00
|
|
|
if (appArgs.insecure) {
|
|
|
|
app.commandLine.appendSwitch('ignore-certificate-errors');
|
|
|
|
}
|
|
|
|
|
2016-01-27 16:30:54 +01:00
|
|
|
if(!appArgs.icon){
|
|
|
|
appArgs.icon = DEFAULT_ICON_PATH;
|
|
|
|
}
|
|
|
|
|
2016-01-23 08:12:53 +01:00
|
|
|
// do nothing for setDockBadge if not OSX
|
|
|
|
let setDockBadge = () => {};
|
2016-01-23 19:02:23 +01:00
|
|
|
|
2016-01-23 08:12:53 +01:00
|
|
|
if (isOSX()) {
|
|
|
|
setDockBadge = app.dock.setBadge;
|
|
|
|
}
|
|
|
|
|
2016-01-29 15:04:41 +01:00
|
|
|
app.on('window-all-closed', () => {
|
2016-01-27 17:21:40 +01:00
|
|
|
// Need a better place to store user options, unless you intend to dump everything into cli
|
|
|
|
// determined opts
|
|
|
|
if(appArgs.minimizeToTray){
|
|
|
|
mainWindow.hide();
|
|
|
|
}
|
2016-01-21 05:25:33 +01:00
|
|
|
if (!isOSX()) {
|
2015-07-05 08:08:13 +02:00
|
|
|
app.quit();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-01-29 15:04:41 +01:00
|
|
|
app.on('activate', (event, hasVisibleWindows) => {
|
2016-01-21 05:25:33 +01:00
|
|
|
if (isOSX()) {
|
|
|
|
// this is called when the dock is clicked
|
|
|
|
if (!hasVisibleWindows) {
|
|
|
|
mainWindow.show();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-01-29 15:04:41 +01:00
|
|
|
app.on('before-quit', () => {
|
2016-01-21 05:25:33 +01: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-27 16:30:54 +01:00
|
|
|
let appIcon = null;
|
2016-01-29 15:04:41 +01:00
|
|
|
app.on('ready', () => {
|
2016-01-23 08:12:53 +01:00
|
|
|
mainWindow = createMainWindow(appArgs, app.quit, setDockBadge);
|
2016-01-27 16:30:54 +01:00
|
|
|
|
|
|
|
appIcon = new Tray(appArgs.icon);
|
2016-01-27 17:21:40 +01:00
|
|
|
let menu = Menu.buildFromTemplate([
|
|
|
|
{
|
|
|
|
label: 'Minimize to Tray',
|
|
|
|
type: 'checkbox',
|
|
|
|
checked: appArgs.minimizeToTray || true,
|
|
|
|
click: function (menuItem) {
|
|
|
|
appArgs.minimizeToTray = menuItem.checked = !menuItem.checked;
|
|
|
|
fs.writeFileSync(APP_ARGS_FILE_PATH, JSON.stringify(appArgs));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]);
|
|
|
|
appIcon.setContextMenu(menu);
|
|
|
|
|
2015-07-06 03:38:04 +02:00
|
|
|
});
|
2015-07-05 08:08:13 +02:00
|
|
|
|
2016-01-29 15:04:41 +01:00
|
|
|
app.on('login', (event, webContents, request, authInfo, callback) => {
|
2016-01-23 03:09:47 +01:00
|
|
|
// for http authentication
|
2016-01-21 19:39:52 +01:00
|
|
|
event.preventDefault();
|
2016-01-23 03:09:47 +01:00
|
|
|
createLoginWindow(callback);
|
2016-01-21 19:39:52 +01:00
|
|
|
});
|
|
|
|
|
2016-01-29 15:04:41 +01:00
|
|
|
ipcMain.on('notification', (event, title, opts) => {
|
2016-01-23 06:43:33 +01:00
|
|
|
if (!isOSX() || mainWindow.isFocused()) {
|
|
|
|
return;
|
|
|
|
}
|
2016-01-23 08:12:53 +01:00
|
|
|
setDockBadge('●');
|
2016-01-23 06:43:33 +01:00
|
|
|
});
|