2
2
mirror of https://github.com/Llewellynvdm/nativefier.git synced 2024-11-04 20:38:03 +00:00

Prompt to confirm when page is attempting to prevent unload (#1163)

Should alleviate part of the issue in #1151
This commit is contained in:
Adam Weeden 2021-04-28 21:37:50 -04:00 committed by GitHub
parent 83b284e727
commit bc6be8445d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 1 deletions

View File

@ -1,7 +1,14 @@
import * as fs from 'fs';
import * as path from 'path';
import { BrowserWindow, shell, ipcMain, dialog, Event } from 'electron';
import {
BrowserWindow,
shell,
ipcMain,
dialog,
Event,
WebContents,
} from 'electron';
import windowStateKeeper from 'electron-window-state';
import log from 'loglevel';
@ -253,6 +260,28 @@ export function createMainWindow(
}
};
const onWillPreventUnload = (event: Event): void => {
log.debug('onWillPreventUnload', event);
const eventAny = event as any;
if (eventAny.sender === undefined) {
return;
}
const webContents: WebContents = eventAny.sender;
const browserWindow = BrowserWindow.fromWebContents(webContents);
const choice = dialog.showMessageBoxSync(browserWindow, {
type: 'question',
buttons: ['Proceed', 'Stay'],
message:
'You may have unsaved changes, are you sure you want to proceed?',
title: 'Changes you made may not be saved.',
defaultId: 0,
cancelId: 1,
});
if (choice === 0) {
event.preventDefault();
}
};
const createNewWindow: (url: string) => BrowserWindow = (url: string) => {
const window = new BrowserWindow(DEFAULT_WINDOW_OPTIONS);
if (options.userAgent) {
@ -267,6 +296,7 @@ export function createMainWindow(
sendParamsOnDidFinishLoad(window);
window.webContents.on('new-window', onNewWindow);
window.webContents.on('will-navigate', onWillNavigate);
window.webContents.on('will-prevent-unload', onWillPreventUnload);
window.loadURL(url); // eslint-disable-line @typescript-eslint/no-floating-promises
return window;
};
@ -464,6 +494,7 @@ export function createMainWindow(
mainWindow.webContents.on('new-window', onNewWindow);
mainWindow.webContents.on('will-navigate', onWillNavigate);
mainWindow.webContents.on('will-prevent-unload', onWillPreventUnload);
mainWindow.webContents.on('did-finish-load', () => {
// Restore pinch-to-zoom, disabled by default in recent Electron.
// See https://github.com/nativefier/nativefier/issues/379#issuecomment-598309817

View File

@ -11,6 +11,7 @@ import {
BrowserWindow,
} from 'electron';
import electronDownload from 'electron-dl';
import * as log from 'loglevel';
import { createLoginWindow } from './components/loginWindow';
import {
@ -123,6 +124,7 @@ const setDockBadge = isRunningMacos
: () => undefined;
app.on('window-all-closed', () => {
log.debug('windows-all-closed');
if (!isOSX() || appArgs.fastQuit) {
app.quit();
}
@ -138,6 +140,7 @@ app.on('activate', (event, hasVisibleWindows) => {
});
app.on('before-quit', () => {
log.debug('before-quit');
// not fired when the close button on the window is clicked
if (isOSX()) {
// need to force a quit as a workaround here to simulate the osx app hiding behaviour
@ -149,6 +152,14 @@ app.on('before-quit', () => {
}
});
app.on('will-quit', (event) => {
log.debug('will-quit', event);
});
app.on('quit', (event, exitCode) => {
log.debug('quit', event, exitCode);
});
if (appArgs.crashReporter) {
app.on('will-finish-launching', () => {
crashReporter.start({