2016-02-25 07:56:32 +01:00
|
|
|
import fs from 'fs';
|
2016-01-29 15:04:41 +01: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 08:56:33 +01:00
|
|
|
import initContextMenu from './../contextMenu/contextMenu';
|
|
|
|
|
2016-02-25 03:37:06 +01:00
|
|
|
const {BrowserWindow, shell, ipcMain, dialog} = electron;
|
2016-02-25 07:56:32 +01:00
|
|
|
const {isOSX, linkIsInternal, getCssToInject} = helpers;
|
2016-01-29 15:04:41 +01:00
|
|
|
|
2016-01-23 03:09:47 +01:00
|
|
|
const ZOOM_INTERVAL = 0.1;
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {{}} options AppArgs from nativefier.json
|
2016-01-23 08:12:53 +01:00
|
|
|
* @param {function} onAppQuit
|
|
|
|
* @param {function} setDockBadge
|
2016-01-23 03:09:47 +01:00
|
|
|
* @returns {electron.BrowserWindow}
|
|
|
|
*/
|
|
|
|
function createMainWindow(options, onAppQuit, setDockBadge) {
|
2016-01-29 15:04:41 +01:00
|
|
|
const mainWindowState = windowStateKeeper({
|
2016-01-23 07:52:13 +01:00
|
|
|
defaultWidth: options.width || 1280,
|
|
|
|
defaultHeight: options.height || 800
|
|
|
|
});
|
2016-01-23 03:09:47 +01:00
|
|
|
|
2016-01-29 15:04:41 +01: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 17:30:25 +01:00
|
|
|
title: options.name,
|
2016-01-29 15:04:41 +01:00
|
|
|
'web-preferences': {
|
|
|
|
javascript: true,
|
|
|
|
plugins: true,
|
|
|
|
// node globals causes problems with sites like messenger.com
|
|
|
|
nodeIntegration: false,
|
2016-02-23 14:31:47 +01:00
|
|
|
webSecurity: !options.insecure,
|
2016-01-29 15:04:41 +01:00
|
|
|
preload: path.join(__dirname, 'static', 'preload.js')
|
|
|
|
},
|
|
|
|
// after webpack path here should reference `resources/app/`
|
2016-02-25 11:26:28 +01:00
|
|
|
icon: path.join(__dirname, '../', '/icon.png'),
|
|
|
|
// set to undefined and not false because explicitly setting to false will disable full screen
|
|
|
|
fullscreen: options.fullScreen || undefined
|
2016-01-29 15:04:41 +01:00
|
|
|
});
|
|
|
|
|
2016-03-25 14:06:59 +01:00
|
|
|
mainWindowState.manage(mainWindow);
|
|
|
|
|
2016-02-25 11:26:28 +01:00
|
|
|
// after first run, no longer force full screen to be true
|
|
|
|
if (options.fullScreen) {
|
|
|
|
options.fullScreen = undefined;
|
|
|
|
fs.writeFileSync(path.join(__dirname, '..', 'nativefier.json'), JSON.stringify(options));
|
|
|
|
}
|
|
|
|
|
2016-03-22 18:09:21 +01:00
|
|
|
// after first run, no longer force maximize to be true
|
2016-03-22 18:16:34 +01:00
|
|
|
if (options.maximize) {
|
2016-03-22 18:09:21 +01:00
|
|
|
mainWindow.maximize();
|
|
|
|
options.maximize = undefined;
|
|
|
|
fs.writeFileSync(path.join(__dirname, '..', 'nativefier.json'), JSON.stringify(options));
|
|
|
|
}
|
|
|
|
|
2016-01-29 15:04:41 +01:00
|
|
|
let currentZoom = 1;
|
2016-01-23 03:09:47 +01:00
|
|
|
|
2016-01-29 15:04:41 +01:00
|
|
|
const onZoomIn = () => {
|
2016-01-23 03:09:47 +01:00
|
|
|
currentZoom += ZOOM_INTERVAL;
|
|
|
|
mainWindow.webContents.send('change-zoom', currentZoom);
|
|
|
|
};
|
|
|
|
|
2016-01-29 15:04:41 +01:00
|
|
|
const onZoomOut = () => {
|
2016-01-23 03:09:47 +01:00
|
|
|
currentZoom -= ZOOM_INTERVAL;
|
|
|
|
mainWindow.webContents.send('change-zoom', currentZoom);
|
|
|
|
};
|
|
|
|
|
2016-02-25 03:37:06 +01: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 08:56:33 +01:00
|
|
|
initContextMenu(mainWindow);
|
2016-01-23 03:09:47 +01:00
|
|
|
|
|
|
|
if (options.userAgent) {
|
|
|
|
mainWindow.webContents.setUserAgent(options.userAgent);
|
|
|
|
}
|
|
|
|
|
2016-03-14 07:39:41 +01:00
|
|
|
mainWindow.webContents.on('did-get-response-details', () => {
|
2016-01-23 03:09:47 +01:00
|
|
|
mainWindow.webContents.send('params', JSON.stringify(options));
|
2016-02-25 07:56:32 +01:00
|
|
|
mainWindow.webContents.insertCSS(getCssToInject());
|
2016-01-23 03:09:47 +01:00
|
|
|
});
|
|
|
|
|
2016-01-23 06:43:33 +01:00
|
|
|
if (options.counter) {
|
2016-01-29 15:04:41 +01:00
|
|
|
mainWindow.on('page-title-updated', () => {
|
2016-01-23 08:12:53 +01:00
|
|
|
if (mainWindow.isFocused()) {
|
2016-01-23 03:09:47 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.counter) {
|
2016-01-30 16:42:08 +01:00
|
|
|
const itemCountRegex = /[\(\[{](\d*?)[}\]\)]/;
|
2016-01-29 15:04:41 +01:00
|
|
|
const match = itemCountRegex.exec(mainWindow.getTitle());
|
2016-01-23 03:09:47 +01:00
|
|
|
if (match) {
|
|
|
|
setDockBadge(match[1]);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
setDockBadge('●');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-01-29 15:04:41 +01:00
|
|
|
mainWindow.webContents.on('new-window', (event, urlToGo) => {
|
2016-01-25 09:49:11 +01:00
|
|
|
if (mainWindow.useDefaultWindowBehaviour) {
|
|
|
|
mainWindow.useDefaultWindowBehaviour = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-01-23 03:09:47 +01:00
|
|
|
if (linkIsInternal(options.targetUrl, urlToGo)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
event.preventDefault();
|
|
|
|
shell.openExternal(urlToGo);
|
|
|
|
});
|
|
|
|
|
|
|
|
mainWindow.loadURL(options.targetUrl);
|
2016-01-23 06:43:33 +01:00
|
|
|
|
2016-01-29 15:04:41 +01:00
|
|
|
mainWindow.on('focus', () => {
|
2016-01-23 06:43:33 +01:00
|
|
|
setDockBadge('');
|
2016-01-23 03:09:47 +01:00
|
|
|
});
|
|
|
|
|
2016-01-23 07:47:32 +01:00
|
|
|
mainWindow.on('close', event => {
|
|
|
|
if (mainWindow.isFullScreen()) {
|
|
|
|
mainWindow.setFullScreen(false);
|
|
|
|
mainWindow.once('leave-full-screen', maybeHideWindow.bind(this, mainWindow, event));
|
2016-01-23 03:09:47 +01:00
|
|
|
}
|
2016-01-23 19:02:23 +01:00
|
|
|
maybeHideWindow(mainWindow, event);
|
2016-01-23 03:09:47 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
return mainWindow;
|
|
|
|
}
|
|
|
|
|
2016-01-25 09:49:11 +01:00
|
|
|
ipcMain.on('cancelNewWindowOverride', () => {
|
|
|
|
const allWindows = BrowserWindow.getAllWindows();
|
|
|
|
allWindows.forEach(window => {
|
|
|
|
window.useDefaultWindowBehaviour = false;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-01-23 07:47:32 +01: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 15:04:41 +01:00
|
|
|
export default createMainWindow;
|