Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed confirmExit behaviour for Electron apps #6285

Merged
merged 1 commit into from
Oct 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -125,44 +125,14 @@ 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 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 @@ -243,13 +213,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.'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the default Chrome message is: https://github.com/eclipse-theia/theia/pull/6285/files#diff-b23416bfa30b320828382edc2ba8ea30L35
Do we want to align electron with Chome?

});

if (!preventStop) {
// This ignores the beforeunload callback, allowing the page to unload
event.preventDefault();
}
});

Expand All @@ -267,7 +244,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;
}

}