1
0
mirror of https://github.com/jiahaog/Nativefier synced 2024-06-30 08:10:49 +02:00
Nativefier/app/src/main.js

100 lines
2.7 KiB
JavaScript
Raw Normal View History

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-29 15:04:41 +01:00
const {app, ipcMain} = electron;
const {isOSX} = helpers;
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'));
const DEFAULT_ICON_PATH = path.join(__dirname, '/icon.png');
const Tray = electron.Tray;
const Menu = electron.Menu;
2016-01-29 15:04:41 +01:00
let mainWindow;
if (appArgs.insecure) {
app.commandLine.appendSwitch('ignore-certificate-errors');
}
if(!appArgs.icon){
appArgs.icon = DEFAULT_ICON_PATH;
}
// do nothing for setDockBadge if not OSX
let setDockBadge = () => {};
2016-01-23 19:02:23 +01:00
if (isOSX()) {
setDockBadge = app.dock.setBadge;
}
2016-01-29 15:04:41 +01:00
app.on('window-all-closed', () => {
// Need a better place to store user options, unless you intend to dump everything into cli
// determined opts
if(appArgs.minimizeToTray){
mainWindow.hide();
}
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) => {
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', () => {
// 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);
}
});
let appIcon = null;
2016-01-29 15:04:41 +01:00
app.on('ready', () => {
mainWindow = createMainWindow(appArgs, app.quit, setDockBadge);
appIcon = new Tray(appArgs.icon);
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-05 08:08:13 +02:00
2016-01-29 15:04:41 +01:00
app.on('login', (event, webContents, request, authInfo, callback) => {
// for http authentication
2016-01-21 19:39:52 +01:00
event.preventDefault();
createLoginWindow(callback);
2016-01-21 19:39:52 +01:00
});
2016-01-29 15:04:41 +01:00
ipcMain.on('notification', (event, title, opts) => {
if (!isOSX() || mainWindow.isFocused()) {
return;
}
setDockBadge('●');
});