2016-01-29 14:04:41 +00:00
|
|
|
import path from 'path';
|
|
|
|
import electron from 'electron';
|
|
|
|
import windowStateKeeper from 'electron-window-state';
|
|
|
|
import helpers from './../../helpers/helpers';
|
|
|
|
import createMenu from './../menu/menu';
|
2016-01-25 07:56:33 +00:00
|
|
|
import initContextMenu from './../contextMenu/contextMenu';
|
|
|
|
|
2016-02-25 02:37:06 +00:00
|
|
|
const {BrowserWindow, shell, ipcMain, dialog} = electron;
|
2016-01-29 14:04:41 +00:00
|
|
|
const {isOSX, linkIsInternal} = helpers;
|
|
|
|
|
2016-01-23 02:09:47 +00:00
|
|
|
const ZOOM_INTERVAL = 0.1;
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {{}} options AppArgs from nativefier.json
|
2016-01-23 07:12:53 +00:00
|
|
|
* @param {function} onAppQuit
|
|
|
|
* @param {function} setDockBadge
|
2016-01-23 02:09:47 +00:00
|
|
|
* @returns {electron.BrowserWindow}
|
|
|
|
*/
|
|
|
|
function createMainWindow(options, onAppQuit, setDockBadge) {
|
2016-01-29 14:04:41 +00:00
|
|
|
const mainWindowState = windowStateKeeper({
|
2016-01-23 06:52:13 +00:00
|
|
|
defaultWidth: options.width || 1280,
|
|
|
|
defaultHeight: options.height || 800
|
|
|
|
});
|
2016-01-23 02:09:47 +00:00
|
|
|
|
2016-01-29 14:04:41 +00:00
|
|
|
const mainWindow = new BrowserWindow({
|
|
|
|
width: mainWindowState.width,
|
|
|
|
height: mainWindowState.height,
|
|
|
|
x: mainWindowState.x,
|
|
|
|
y: mainWindowState.y,
|
|
|
|
'auto-hide-menu-bar': !options.showMenuBar,
|
|
|
|
// Convert dashes to spaces because on linux the app name is joined with dashes
|
2016-01-29 16:30:25 +00:00
|
|
|
title: options.name,
|
2016-01-29 14:04:41 +00:00
|
|
|
'web-preferences': {
|
|
|
|
javascript: true,
|
|
|
|
plugins: true,
|
|
|
|
// node globals causes problems with sites like messenger.com
|
|
|
|
nodeIntegration: false,
|
2016-02-23 13:31:47 +00:00
|
|
|
webSecurity: !options.insecure,
|
2016-01-29 14:04:41 +00:00
|
|
|
preload: path.join(__dirname, 'static', 'preload.js')
|
|
|
|
},
|
|
|
|
// after webpack path here should reference `resources/app/`
|
2016-02-02 15:15:07 +00:00
|
|
|
icon: path.join(__dirname, '../', '/icon.png')
|
2016-01-29 14:04:41 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
let currentZoom = 1;
|
2016-01-23 02:09:47 +00:00
|
|
|
|
2016-01-29 14:04:41 +00:00
|
|
|
const onZoomIn = () => {
|
2016-01-23 02:09:47 +00:00
|
|
|
currentZoom += ZOOM_INTERVAL;
|
|
|
|
mainWindow.webContents.send('change-zoom', currentZoom);
|
|
|
|
};
|
|
|
|
|
2016-01-29 14:04:41 +00:00
|
|
|
const onZoomOut = () => {
|
2016-01-23 02:09:47 +00:00
|
|
|
currentZoom -= ZOOM_INTERVAL;
|
|
|
|
mainWindow.webContents.send('change-zoom', currentZoom);
|
|
|
|
};
|
|
|
|
|
2016-02-25 02:37:06 +00:00
|
|
|
const clearAppData = () => {
|
|
|
|
dialog.showMessageBox(mainWindow, {
|
|
|
|
type: 'warning',
|
|
|
|
buttons: ['Yes', 'Cancel'],
|
|
|
|
defaultId: 1,
|
|
|
|
title: 'Clear cache confirmation',
|
|
|
|
message: 'This will clear all data (cookies, local storage etc) from this app. Are you sure you wish to proceed?'
|
|
|
|
}, response => {
|
|
|
|
if (response === 0) {
|
|
|
|
const session = mainWindow.webContents.session;
|
|
|
|
session.clearStorageData(() => {
|
|
|
|
session.clearCache(() => {
|
|
|
|
mainWindow.loadURL(options.targetUrl);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const onGoBack = () => {
|
|
|
|
mainWindow.webContents.goBack();
|
|
|
|
};
|
|
|
|
|
|
|
|
const onGoForward = () => {
|
|
|
|
mainWindow.webContents.goForward();
|
|
|
|
};
|
|
|
|
|
|
|
|
const getCurrentUrl = () => {
|
|
|
|
return mainWindow.webContents.getURL();
|
|
|
|
};
|
|
|
|
|
|
|
|
const menuOptions = {
|
|
|
|
nativefierVersion: options.nativefierVersion,
|
|
|
|
appQuit: onAppQuit,
|
|
|
|
zoomIn: onZoomIn,
|
|
|
|
zoomOut: onZoomOut,
|
|
|
|
goBack: onGoBack,
|
|
|
|
goForward: onGoForward,
|
|
|
|
getCurrentUrl: getCurrentUrl,
|
|
|
|
clearAppData: clearAppData
|
|
|
|
};
|
|
|
|
|
|
|
|
createMenu(menuOptions);
|
2016-01-25 07:56:33 +00:00
|
|
|
initContextMenu(mainWindow);
|
2016-01-23 02:09:47 +00:00
|
|
|
|
|
|
|
if (options.userAgent) {
|
|
|
|
mainWindow.webContents.setUserAgent(options.userAgent);
|
|
|
|
}
|
|
|
|
|
2016-01-29 14:04:41 +00:00
|
|
|
mainWindow.webContents.on('did-finish-load', () => {
|
2016-01-23 02:09:47 +00:00
|
|
|
mainWindow.webContents.send('params', JSON.stringify(options));
|
|
|
|
});
|
|
|
|
|
2016-01-23 05:43:33 +00:00
|
|
|
if (options.counter) {
|
2016-01-29 14:04:41 +00:00
|
|
|
mainWindow.on('page-title-updated', () => {
|
2016-01-23 07:12:53 +00:00
|
|
|
if (mainWindow.isFocused()) {
|
2016-01-23 02:09:47 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.counter) {
|
2016-01-30 15:42:08 +00:00
|
|
|
const itemCountRegex = /[\(\[{](\d*?)[}\]\)]/;
|
2016-01-29 14:04:41 +00:00
|
|
|
const match = itemCountRegex.exec(mainWindow.getTitle());
|
2016-01-23 02:09:47 +00:00
|
|
|
if (match) {
|
|
|
|
setDockBadge(match[1]);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
setDockBadge('●');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-01-29 14:04:41 +00:00
|
|
|
mainWindow.webContents.on('new-window', (event, urlToGo) => {
|
2016-01-25 08:49:11 +00:00
|
|
|
if (mainWindow.useDefaultWindowBehaviour) {
|
|
|
|
mainWindow.useDefaultWindowBehaviour = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-01-23 02:09:47 +00:00
|
|
|
if (linkIsInternal(options.targetUrl, urlToGo)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
event.preventDefault();
|
|
|
|
shell.openExternal(urlToGo);
|
|
|
|
});
|
|
|
|
|
|
|
|
mainWindow.loadURL(options.targetUrl);
|
2016-01-23 05:43:33 +00:00
|
|
|
|
2016-01-29 14:04:41 +00:00
|
|
|
mainWindow.on('focus', () => {
|
2016-01-23 05:43:33 +00:00
|
|
|
setDockBadge('');
|
2016-01-23 02:09:47 +00:00
|
|
|
});
|
|
|
|
|
2016-01-23 06:47:32 +00:00
|
|
|
mainWindow.on('close', event => {
|
|
|
|
if (mainWindow.isFullScreen()) {
|
|
|
|
mainWindow.setFullScreen(false);
|
|
|
|
mainWindow.once('leave-full-screen', maybeHideWindow.bind(this, mainWindow, event));
|
2016-01-23 02:09:47 +00:00
|
|
|
}
|
2016-01-23 18:02:23 +00:00
|
|
|
maybeHideWindow(mainWindow, event);
|
2016-01-23 02:09:47 +00:00
|
|
|
});
|
|
|
|
|
2016-01-23 06:52:13 +00:00
|
|
|
mainWindowState.manage(mainWindow);
|
2016-01-23 02:09:47 +00:00
|
|
|
return mainWindow;
|
|
|
|
}
|
|
|
|
|
2016-01-25 08:49:11 +00:00
|
|
|
ipcMain.on('cancelNewWindowOverride', () => {
|
|
|
|
const allWindows = BrowserWindow.getAllWindows();
|
|
|
|
allWindows.forEach(window => {
|
|
|
|
window.useDefaultWindowBehaviour = false;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-01-23 06:47:32 +00:00
|
|
|
function maybeHideWindow(window, event) {
|
|
|
|
if (isOSX()) {
|
|
|
|
// this is called when exiting from clicking the cross button on the window
|
|
|
|
event.preventDefault();
|
|
|
|
window.hide();
|
|
|
|
}
|
|
|
|
// will close the window on other platforms
|
|
|
|
}
|
|
|
|
|
2016-01-29 14:04:41 +00:00
|
|
|
export default createMainWindow;
|