mirror of
https://github.com/Llewellynvdm/nativefier.git
synced 2024-12-23 02:28:55 +00:00
This commit is contained in:
parent
27d3d5a537
commit
0848143096
@ -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);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
@ -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) => {
|
||||
|
Loading…
Reference in New Issue
Block a user