Skip to content

Commit

Permalink
Fixed confirmExit for Electron apps.
Browse files Browse the repository at this point in the history
The previous implementation did not work because the window was unloaded
before the dialog could be opened.

This implementation gets around some… unusual behaviour of Electron's
beforeunload event by using the will-prevent-unload WebContents event.

Signed-off-by: Matthew Gordon <matthew.gordon@arm.com>
  • Loading branch information
mcgordonite authored and kittaakos committed Oct 8, 2019
1 parent fcccdf4 commit aff11be
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 131 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ process.env.LC_NUMERIC = 'C';
const electron = require('electron');
const { join, resolve } = require('path');
const { fork } = require('child_process');
const { app, shell, BrowserWindow, ipcMain, Menu } = electron;
const { app, dialog, shell, BrowserWindow, ipcMain, Menu } = electron;
const applicationName = \`${this.pck.props.frontend.config.applicationName}\`;
const isSingleInstance = ${this.pck.props.backend.config.singleInstance === true ? 'true' : 'false'};
Expand All @@ -140,36 +140,6 @@ const nativeKeymap = require('native-keymap');
const Storage = require('electron-store');
const electronStore = new Storage();
let canPreventStop = true;
const windows = [];
app.on('before-quit', async event => {
if (canPreventStop) {
// Pause the stop.
event.preventDefault();
let preventStop = false;
// Ask all opened windows whether they want to prevent the \`close\` event or not.
for (const window of windows) {
if (!preventStop) {
window.webContents.send('prevent-stop-request');
const preventStopPerWindow = await new Promise((resolve) => {
ipcMain.once('prevent-stop-response', (_, arg) => {
if (!!arg && 'preventStop' in arg && typeof arg.preventStop === 'boolean') {
resolve(arg.preventStop);
}
})
});
if (preventStopPerWindow) {
preventStop = true;
}
}
}
if (!preventStop) {
canPreventStop = false;
app.quit();
}
}
});
app.on('ready', () => {
const { screen } = electron;
Expand Down Expand Up @@ -250,13 +220,20 @@ app.on('ready', () => {
newWindow.on('close', saveWindowState);
newWindow.on('resize', saveWindowStateDelayed);
newWindow.on('move', saveWindowStateDelayed);
newWindow.on('closed', () => {
const index = windows.indexOf(newWindow);
if (index !== -1) {
windows.splice(index, 1);
}
if (windows.length === 0) {
app.quit();
// Fired when a beforeunload handler tries to prevent the page unloading
newWindow.webContents.on('will-prevent-unload', event => {
const preventStop = 0 !== dialog.showMessageBox(newWindow, {
type: 'question',
buttons: ['Yes', 'No'],
title: 'Confirm',
message: 'Are you sure you want to quit?',
detail: 'Any unsaved changes will not be saved.'
});
if (!preventStop) {
// This ignores the beforeunload callback, allowing the page to unload
event.preventDefault();
}
});
Expand All @@ -274,7 +251,6 @@ app.on('ready', () => {
if (!!theUrl) {
newWindow.loadURL(theUrl);
}
windows.push(newWindow);
return newWindow;
}
Expand Down
3 changes: 0 additions & 3 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,6 @@
"frontend": "lib/browser/keyboard/browser-keyboard-module",
"frontendElectron": "lib/electron-browser/keyboard/electron-keyboard-module",
"backendElectron": "lib/electron-node/keyboard/electron-backend-keyboard-module"
},
{
"frontendElectron": "lib/electron-browser/shutdown-hook/electron-shutdown-hook-module"
}
],
"keywords": [
Expand Down
14 changes: 11 additions & 3 deletions packages/core/src/browser/window/default-window-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ export class DefaultWindowService implements WindowService, FrontendApplicationC
this.frontendApplication = app;
window.addEventListener('beforeunload', event => {
if (!this.canUnload()) {
event.returnValue = '';
event.preventDefault();
return '';
return this.preventUnload(event);
}
});
}
Expand Down Expand Up @@ -66,4 +64,14 @@ export class DefaultWindowService implements WindowService, FrontendApplicationC
return confirmExit !== 'always';
}

/**
* Ask the user to confirm if they want to unload the window. Prevent it if they do not.
* @param event The beforeunload event
*/
protected preventUnload(event: BeforeUnloadEvent): string | void {
event.returnValue = '';
event.preventDefault();
return '';
}

}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,11 @@
import { injectable } from 'inversify';
import { ipcRenderer } from 'electron';
import { NewWindowOptions } from '../../browser/window/window-service';
import { FrontendApplication } from '../../browser/frontend-application';
import { DefaultWindowService } from '../../browser/window/default-window-service';

@injectable()
export class ElectronWindowService extends DefaultWindowService {

onStart(app: FrontendApplication): void {
this.frontendApplication = app;
// We do not want to add a `beforeunload` listener to the `window`.
// Why? Because by the time we get into the unload handler, it is already too late. Our application has quit.
// _Emitted when the `window` is going to be closed. It's emitted before the `beforeunload` and `unload` event of the DOM._
// https://github.com/electron/electron/blob/master/docs/api/browser-window.md#event-close
}

openNewWindow(url: string, { external }: NewWindowOptions = {}): undefined {
if (external) {
ipcRenderer.send('open-external', url);
Expand All @@ -40,4 +31,9 @@ export class ElectronWindowService extends DefaultWindowService {
return undefined;
}

protected preventUnload(event: BeforeUnloadEvent): string | void {
// The user will be shown a confirmation dialog by the will-prevent-unload handler in the Electron main script
event.returnValue = false;
}

}

0 comments on commit aff11be

Please sign in to comment.