2
2
mirror of https://github.com/Llewellynvdm/nativefier.git synced 2024-11-16 01:37:07 +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'; import contextMenu from 'electron-context-menu';
function initContextMenu() { function initContextMenu(createNewWindow) {
contextMenu({ contextMenu({
prepend: (params) => { prepend: (params) => {
const items = []; const items = [];
@ -15,7 +15,7 @@ function initContextMenu() {
items.push({ items.push({
label: 'Open Link in New Window', label: 'Open Link in New Window',
click: () => { click: () => {
new BrowserWindow().loadURL(params.linkURL); createNewWindow(params.linkURL);
}, },
}); });
} }

View File

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