2016-02-25 06:56:32 +00:00
|
|
|
import fs from 'fs';
|
2016-01-29 14:04:41 +00:00
|
|
|
import path from 'path';
|
2017-04-29 14:52:12 +00:00
|
|
|
import { BrowserWindow, shell, ipcMain, dialog } from 'electron';
|
2016-01-29 14:04:41 +00:00
|
|
|
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';
|
|
|
|
|
2017-11-14 13:05:01 +00:00
|
|
|
const {
|
2017-11-26 04:27:19 +00:00
|
|
|
isOSX, linkIsInternal, getCssToInject, shouldInjectCss, getAppIcon,
|
2017-11-14 13:05:01 +00:00
|
|
|
} = helpers;
|
2016-01-29 14:04:41 +00:00
|
|
|
|
2016-01-23 02:09:47 +00:00
|
|
|
const ZOOM_INTERVAL = 0.1;
|
|
|
|
|
2017-10-05 23:32:48 +00:00
|
|
|
function maybeHideWindow(window, event, fastQuit, tray) {
|
2017-04-29 14:52:12 +00:00
|
|
|
if (isOSX() && !fastQuit) {
|
|
|
|
// this is called when exiting from clicking the cross button on the window
|
|
|
|
event.preventDefault();
|
|
|
|
window.hide();
|
2017-10-05 23:32:48 +00:00
|
|
|
} else if (!fastQuit && tray) {
|
|
|
|
event.preventDefault();
|
|
|
|
window.hide();
|
2017-04-29 14:52:12 +00:00
|
|
|
}
|
|
|
|
// will close the window on other platforms
|
|
|
|
}
|
|
|
|
|
|
|
|
function maybeInjectCss(browserWindow) {
|
|
|
|
if (!shouldInjectCss()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const cssToInject = getCssToInject();
|
|
|
|
|
|
|
|
const injectCss = () => {
|
|
|
|
browserWindow.webContents.insertCSS(cssToInject);
|
|
|
|
};
|
|
|
|
|
|
|
|
browserWindow.webContents.on('did-finish-load', () => {
|
|
|
|
// remove the injection of css the moment the page is loaded
|
|
|
|
browserWindow.webContents.removeListener('did-get-response-details', injectCss);
|
|
|
|
});
|
|
|
|
|
|
|
|
// on every page navigation inject the css
|
|
|
|
browserWindow.webContents.on('did-navigate', () => {
|
|
|
|
// we have to inject the css in did-get-response-details to prevent the fouc
|
|
|
|
// will run multiple times
|
|
|
|
browserWindow.webContents.on('did-get-response-details', injectCss);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-01-23 02:09:47 +00:00
|
|
|
/**
|
|
|
|
*
|
2017-04-29 14:52:12 +00:00
|
|
|
* @param {{}} inpOptions 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}
|
|
|
|
*/
|
2017-04-29 14:52:12 +00:00
|
|
|
function createMainWindow(inpOptions, onAppQuit, setDockBadge) {
|
|
|
|
const options = Object.assign({}, inpOptions);
|
|
|
|
const mainWindowState = windowStateKeeper({
|
|
|
|
defaultWidth: options.width || 1280,
|
|
|
|
defaultHeight: options.height || 800,
|
|
|
|
});
|
|
|
|
|
|
|
|
const mainWindow = new BrowserWindow({
|
|
|
|
frame: !options.hideWindowFrame,
|
|
|
|
width: mainWindowState.width,
|
|
|
|
height: mainWindowState.height,
|
|
|
|
minWidth: options.minWidth,
|
|
|
|
minHeight: options.minHeight,
|
|
|
|
maxWidth: options.maxWidth,
|
|
|
|
maxHeight: options.maxHeight,
|
2017-12-26 18:00:39 +00:00
|
|
|
x: options.x,
|
|
|
|
y: options.y,
|
2017-04-29 14:52:12 +00:00
|
|
|
autoHideMenuBar: !options.showMenuBar,
|
|
|
|
// Convert dashes to spaces because on linux the app name is joined with dashes
|
|
|
|
title: options.name,
|
|
|
|
webPreferences: {
|
|
|
|
javascript: true,
|
|
|
|
plugins: true,
|
|
|
|
// node globals causes problems with sites like messenger.com
|
|
|
|
nodeIntegration: false,
|
|
|
|
webSecurity: !options.insecure,
|
|
|
|
preload: path.join(__dirname, 'static', 'preload.js'),
|
|
|
|
zoomFactor: options.zoom,
|
|
|
|
},
|
|
|
|
// after webpack path here should reference `resources/app/`
|
2017-11-26 04:27:19 +00:00
|
|
|
icon: getAppIcon(),
|
2017-04-29 14:52:12 +00:00
|
|
|
// set to undefined and not false because explicitly setting to false will disable full screen
|
|
|
|
fullscreen: options.fullScreen || undefined,
|
2018-03-16 22:15:44 +00:00
|
|
|
// Whether the window should always stay on top of other windows. Default is false.
|
|
|
|
alwaysOnTop: options.alwaysOnTop,
|
2017-04-29 14:52:12 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
mainWindowState.manage(mainWindow);
|
|
|
|
|
|
|
|
// 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));
|
|
|
|
}
|
|
|
|
|
2018-04-22 19:54:29 +00:00
|
|
|
const adjustWindowZoom = (window, adjustment) => {
|
|
|
|
window.webContents.getZoomFactor((zoomFactor) => {
|
|
|
|
window.webContents.setZoomFactor(zoomFactor + adjustment);
|
|
|
|
});
|
2017-04-29 14:52:12 +00:00
|
|
|
};
|
|
|
|
|
2018-04-22 19:54:29 +00:00
|
|
|
const onZoomIn = () => adjustWindowZoom(mainWindow, ZOOM_INTERVAL);
|
|
|
|
|
|
|
|
const onZoomOut = () => adjustWindowZoom(mainWindow, -ZOOM_INTERVAL);
|
2017-04-29 14:52:12 +00:00
|
|
|
|
|
|
|
const onZoomReset = () => {
|
2018-04-22 19:54:29 +00:00
|
|
|
mainWindow.webContents.setZoomFactor(options.zoom);
|
2017-04-29 14:52:12 +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) {
|
|
|
|
return;
|
|
|
|
}
|
2017-11-14 13:05:01 +00:00
|
|
|
const { session } = mainWindow.webContents;
|
2017-04-29 14:52:12 +00:00
|
|
|
session.clearStorageData(() => {
|
|
|
|
session.clearCache(() => {
|
|
|
|
mainWindow.loadURL(options.targetUrl);
|
|
|
|
});
|
|
|
|
});
|
2016-01-23 06:52:13 +00:00
|
|
|
});
|
2017-04-29 14:52:12 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const onGoBack = () => {
|
|
|
|
mainWindow.webContents.goBack();
|
|
|
|
};
|
|
|
|
|
|
|
|
const onGoForward = () => {
|
|
|
|
mainWindow.webContents.goForward();
|
|
|
|
};
|
|
|
|
|
|
|
|
const getCurrentUrl = () => mainWindow.webContents.getURL();
|
|
|
|
|
|
|
|
const menuOptions = {
|
|
|
|
nativefierVersion: options.nativefierVersion,
|
|
|
|
appQuit: onAppQuit,
|
|
|
|
zoomIn: onZoomIn,
|
|
|
|
zoomOut: onZoomOut,
|
|
|
|
zoomReset: onZoomReset,
|
|
|
|
zoomBuildTimeValue: options.zoom,
|
|
|
|
goBack: onGoBack,
|
|
|
|
goForward: onGoForward,
|
|
|
|
getCurrentUrl,
|
|
|
|
clearAppData,
|
|
|
|
disableDevTools: options.disableDevTools,
|
|
|
|
};
|
|
|
|
|
|
|
|
createMenu(menuOptions);
|
|
|
|
if (!options.disableContextMenu) {
|
2018-04-22 23:48:56 +00:00
|
|
|
initContextMenu();
|
2017-04-29 14:52:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (options.userAgent) {
|
|
|
|
mainWindow.webContents.setUserAgent(options.userAgent);
|
|
|
|
}
|
|
|
|
|
|
|
|
maybeInjectCss(mainWindow);
|
|
|
|
mainWindow.webContents.on('did-finish-load', () => {
|
|
|
|
mainWindow.webContents.send('params', JSON.stringify(options));
|
|
|
|
});
|
|
|
|
|
|
|
|
if (options.counter) {
|
|
|
|
mainWindow.on('page-title-updated', (e, title) => {
|
2017-08-14 01:22:15 +00:00
|
|
|
const itemCountRegex = /[([{](\d*?)\+?[}\])]/;
|
2017-04-29 14:52:12 +00:00
|
|
|
const match = itemCountRegex.exec(title);
|
|
|
|
if (match) {
|
2018-04-14 21:17:25 +00:00
|
|
|
setDockBadge(match[1], options.bounce);
|
2017-04-29 14:52:12 +00:00
|
|
|
} else {
|
|
|
|
setDockBadge('');
|
|
|
|
}
|
2016-01-29 14:04:41 +00:00
|
|
|
});
|
2017-04-29 14:52:12 +00:00
|
|
|
} else {
|
|
|
|
ipcMain.on('notification', () => {
|
|
|
|
if (!isOSX() || mainWindow.isFocused()) {
|
|
|
|
return;
|
|
|
|
}
|
2018-04-14 21:17:25 +00:00
|
|
|
setDockBadge('•', options.bounce);
|
2017-04-29 14:52:12 +00:00
|
|
|
});
|
|
|
|
mainWindow.on('focus', () => {
|
|
|
|
setDockBadge('');
|
|
|
|
});
|
|
|
|
}
|
2016-01-29 14:04:41 +00:00
|
|
|
|
2017-04-29 14:52:12 +00:00
|
|
|
mainWindow.webContents.on('new-window', (event, urlToGo) => {
|
|
|
|
if (mainWindow.useDefaultWindowBehaviour) {
|
|
|
|
mainWindow.useDefaultWindowBehaviour = false;
|
|
|
|
return;
|
2016-04-16 14:06:25 +00:00
|
|
|
}
|
2016-01-23 02:09:47 +00:00
|
|
|
|
2017-04-29 14:52:12 +00:00
|
|
|
if (linkIsInternal(options.targetUrl, urlToGo, options.internalUrls)) {
|
|
|
|
return;
|
2016-01-23 02:09:47 +00:00
|
|
|
}
|
2017-04-29 14:52:12 +00:00
|
|
|
event.preventDefault();
|
|
|
|
shell.openExternal(urlToGo);
|
|
|
|
});
|
2016-01-23 02:09:47 +00:00
|
|
|
|
2017-04-29 14:52:12 +00:00
|
|
|
mainWindow.loadURL(options.targetUrl);
|
2016-05-26 10:01:33 +00:00
|
|
|
|
2017-04-29 14:52:12 +00:00
|
|
|
mainWindow.on('close', (event) => {
|
|
|
|
if (mainWindow.isFullScreen()) {
|
|
|
|
mainWindow.setFullScreen(false);
|
|
|
|
mainWindow.once('leave-full-screen', maybeHideWindow.bind(this, mainWindow, event, options.fastQuit));
|
2016-01-23 02:09:47 +00:00
|
|
|
}
|
2017-10-05 23:32:48 +00:00
|
|
|
maybeHideWindow(mainWindow, event, options.fastQuit, options.tray);
|
2017-04-29 14:52:12 +00:00
|
|
|
});
|
2016-01-23 02:09:47 +00:00
|
|
|
|
2017-04-29 14:52:12 +00:00
|
|
|
return mainWindow;
|
2016-01-23 02:09:47 +00:00
|
|
|
}
|
|
|
|
|
2016-01-25 08:49:11 +00:00
|
|
|
ipcMain.on('cancelNewWindowOverride', () => {
|
2017-04-29 14:52:12 +00:00
|
|
|
const allWindows = BrowserWindow.getAllWindows();
|
|
|
|
allWindows.forEach((window) => {
|
|
|
|
// eslint-disable-next-line no-param-reassign
|
|
|
|
window.useDefaultWindowBehaviour = false;
|
|
|
|
});
|
2016-01-25 08:49:11 +00:00
|
|
|
});
|
|
|
|
|
2016-01-29 14:04:41 +00:00
|
|
|
export default createMainWindow;
|