Nativefier/app/src/components/mainWindow/mainWindow.js

214 lines
6.7 KiB
JavaScript
Raw Normal View History

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';
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
const ZOOM_INTERVAL = 0.1;
/**
*
* @param {{}} options AppArgs from nativefier.json
* @param {function} onAppQuit
* @param {function} setDockBadge
* @returns {electron.BrowserWindow}
*/
function createMainWindow(options, onAppQuit, setDockBadge) {
2016-01-29 15:04:41 +01:00
const mainWindowState = windowStateKeeper({
defaultWidth: options.width || 1280,
defaultHeight: options.height || 800
});
2016-01-29 15:04:41 +01:00
const mainWindow = new BrowserWindow({
2016-04-17 01:32:52 +02:00
frame: !options.hideWindowFrame,
2016-01-29 15:04:41 +01:00
width: mainWindowState.width,
height: mainWindowState.height,
minWidth: options.minWidth,
minHeight: options.minHeight,
maxWidth: options.maxWidth,
maxHeight: options.maxHeight,
2016-01-29 15:04:41 +01:00
x: mainWindowState.x,
y: mainWindowState.y,
autoHideMenuBar: !options.showMenuBar,
2016-01-29 15:04:41 +01:00
// 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,
webPreferences: {
2016-01-29 15:04:41 +01:00
javascript: true,
plugins: true,
// node globals causes problems with sites like messenger.com
nodeIntegration: false,
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/`
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
});
mainWindowState.manage(mainWindow);
// 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));
}
// after first run, no longer force maximize to be true
if (options.maximize) {
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-29 15:04:41 +01:00
const onZoomIn = () => {
currentZoom += ZOOM_INTERVAL;
mainWindow.webContents.send('change-zoom', currentZoom);
};
2016-01-29 15:04:41 +01:00
const onZoomOut = () => {
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);
if (!options.disableContextMenu) {
initContextMenu(mainWindow);
}
if (options.userAgent) {
mainWindow.webContents.setUserAgent(options.userAgent);
}
mainWindow.webContents.on('did-finish-load', () => {
mainWindow.webContents.send('params', JSON.stringify(options));
2016-05-26 11:41:25 +02:00
// remove the injection of css the moment the page is loaded
mainWindow.webContents.removeListener('did-get-response-details', injectCss);
});
2016-05-26 11:41:25 +02:00
const injectCss = () => {
mainWindow.webContents.insertCSS(getCssToInject());
}
// on every page navigation inject the css
mainWindow.webContents.on('did-start-loading', () => {
// we have to inject the css in did-get-response-details to prevent the fouc
// will run multiple times
mainWindow.webContents.on('did-get-response-details', injectCss);
});
if (options.counter) {
2016-01-29 15:04:41 +01:00
mainWindow.on('page-title-updated', () => {
if (mainWindow.isFocused()) {
return;
}
if (options.counter) {
const itemCountRegex = /[\(\[{](\d*?)[}\]\)]/;
2016-01-29 15:04:41 +01:00
const match = itemCountRegex.exec(mainWindow.getTitle());
if (match) {
setDockBadge(match[1]);
}
return;
}
setDockBadge('●');
});
}
2016-01-29 15:04:41 +01:00
mainWindow.webContents.on('new-window', (event, urlToGo) => {
if (mainWindow.useDefaultWindowBehaviour) {
mainWindow.useDefaultWindowBehaviour = false;
return;
}
if (linkIsInternal(options.targetUrl, urlToGo)) {
return;
}
event.preventDefault();
shell.openExternal(urlToGo);
});
mainWindow.loadURL(options.targetUrl);
2016-01-29 15:04:41 +01:00
mainWindow.on('focus', () => {
setDockBadge('');
});
mainWindow.on('close', event => {
if (mainWindow.isFullScreen()) {
mainWindow.setFullScreen(false);
2016-04-26 00:09:01 +02:00
mainWindow.once('leave-full-screen', maybeHideWindow.bind(this, mainWindow, event, options.fastQuit));
}
2016-04-26 00:09:01 +02:00
maybeHideWindow(mainWindow, event, options.fastQuit);
});
return mainWindow;
}
ipcMain.on('cancelNewWindowOverride', () => {
const allWindows = BrowserWindow.getAllWindows();
allWindows.forEach(window => {
window.useDefaultWindowBehaviour = false;
});
});
2016-04-26 00:09:01 +02:00
function maybeHideWindow(window, event, fastQuit) {
if (isOSX() && !fastQuit) {
// 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;