2
2
mirror of https://github.com/Llewellynvdm/nativefier.git synced 2024-12-23 02:28:55 +00:00

Fix #590, Fix #439 - Ensure children windows have the same behavior as the mainWindow (PR #591)

This commit is contained in:
David Kramer 2018-04-30 18:36:45 -07:00 committed by Ronan Jouchet
parent 27d3d5a537
commit 0848143096
2 changed files with 56 additions and 32 deletions

View File

@ -1,7 +1,7 @@
import { shell, BrowserWindow } from 'electron';
import { shell } from 'electron';
import contextMenu from 'electron-context-menu';
function initContextMenu() {
function initContextMenu(createNewWindow) {
contextMenu({
prepend: (params) => {
const items = [];
@ -15,7 +15,7 @@ function initContextMenu() {
items.push({
label: 'Open Link in New Window',
click: () => {
new BrowserWindow().loadURL(params.linkURL);
createNewWindow(params.linkURL);
},
});
}

View File

@ -63,17 +63,7 @@ function createMainWindow(inpOptions, onAppQuit, setDockBadge) {
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,
x: options.x,
y: options.y,
autoHideMenuBar: !options.showMenuBar,
const DEFAULT_WINDOW_OPTIONS = {
// Convert dashes to spaces because on linux the app name is joined with dashes
title: options.name,
webPreferences: {
@ -85,13 +75,26 @@ function createMainWindow(inpOptions, onAppQuit, setDockBadge) {
preload: path.join(__dirname, 'static', 'preload.js'),
zoomFactor: options.zoom,
},
};
const mainWindow = new BrowserWindow(Object.assign({
frame: !options.hideWindowFrame,
width: mainWindowState.width,
height: mainWindowState.height,
minWidth: options.minWidth,
minHeight: options.minHeight,
maxWidth: options.maxWidth,
maxHeight: options.maxHeight,
x: options.x,
y: options.y,
autoHideMenuBar: !options.showMenuBar,
// after webpack path here should reference `resources/app/`
icon: getAppIcon(),
// set to undefined and not false because explicitly setting to false will disable full screen
fullscreen: options.fullScreen || undefined,
// Whether the window should always stay on top of other windows. Default is false.
alwaysOnTop: options.alwaysOnTop,
});
}, DEFAULT_WINDOW_OPTIONS));
mainWindowState.manage(mainWindow);
@ -146,6 +149,41 @@ function createMainWindow(inpOptions, onAppQuit, setDockBadge) {
const getCurrentUrl = () => mainWindow.webContents.getURL();
let createNewWindow;
const onNewWindow = (event, urlToGo) => {
if (mainWindow.useDefaultWindowBehaviour) {
mainWindow.useDefaultWindowBehaviour = false;
return;
}
event.preventDefault();
if (!linkIsInternal(options.targetUrl, urlToGo, options.internalUrls)) {
shell.openExternal(urlToGo);
return;
}
// eslint-disable-next-line no-param-reassign
event.guest = createNewWindow(urlToGo);
};
const sendParamsOnDidFinishLoad = (window) => {
window.webContents.on('did-finish-load', () => {
window.webContents.send('params', JSON.stringify(options));
});
};
createNewWindow = (url) => {
const window = new BrowserWindow(DEFAULT_WINDOW_OPTIONS);
if (options.userAgent) {
window.webContents.setUserAgent(options.userAgent);
}
maybeInjectCss(window);
sendParamsOnDidFinishLoad(window);
window.webContents.on('new-window', onNewWindow);
window.loadURL(url);
return window;
};
const menuOptions = {
nativefierVersion: options.nativefierVersion,
appQuit: onAppQuit,
@ -162,7 +200,7 @@ function createMainWindow(inpOptions, onAppQuit, setDockBadge) {
createMenu(menuOptions);
if (!options.disableContextMenu) {
initContextMenu();
initContextMenu(createNewWindow);
}
if (options.userAgent) {
@ -170,9 +208,7 @@ function createMainWindow(inpOptions, onAppQuit, setDockBadge) {
}
maybeInjectCss(mainWindow);
mainWindow.webContents.on('did-finish-load', () => {
mainWindow.webContents.send('params', JSON.stringify(options));
});
sendParamsOnDidFinishLoad(mainWindow);
if (options.counter) {
mainWindow.on('page-title-updated', (e, title) => {
@ -196,19 +232,7 @@ function createMainWindow(inpOptions, onAppQuit, setDockBadge) {
});
}
mainWindow.webContents.on('new-window', (event, urlToGo) => {
if (mainWindow.useDefaultWindowBehaviour) {
mainWindow.useDefaultWindowBehaviour = false;
return;
}
if (linkIsInternal(options.targetUrl, urlToGo, options.internalUrls)) {
return;
}
event.preventDefault();
shell.openExternal(urlToGo);
});
mainWindow.webContents.on('new-window', onNewWindow);
mainWindow.loadURL(options.targetUrl);
mainWindow.on('close', (event) => {