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';
|
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);
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -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) => {
|
||||||
|
Loading…
Reference in New Issue
Block a user