From fd4dfefb6aab60762d1f921ca74430cdf18ba47c Mon Sep 17 00:00:00 2001 From: Benjamin Dobell Date: Mon, 26 Oct 2020 01:36:24 +1100 Subject: [PATCH] Fixed atom.confirm and internal use of dialog.showMessageBox --- src/application-delegate.js | 27 +++++----- src/main-process/atom-application.js | 8 ++- src/main-process/atom-window.js | 60 ++++++++++------------- src/main-process/auto-update-manager.js | 38 ++++++-------- src/main-process/file-recovery-service.js | 13 +++-- 5 files changed, 67 insertions(+), 79 deletions(-) diff --git a/src/application-delegate.js b/src/application-delegate.js index cb6dccd32..faca76358 100644 --- a/src/application-delegate.js +++ b/src/application-delegate.js @@ -228,11 +228,11 @@ module.exports = class ApplicationDelegate { { type: 'info', normalizeAccessKeys: true }, options ); - remote.dialog.showMessageBox( - remote.getCurrentWindow(), - options, - callback - ); + remote.dialog + .showMessageBox(remote.getCurrentWindow(), options) + .then(result => { + callback(result.response); + }); } else { // Legacy sync version: options can only have `message`, // `detailedMessage` (optional), and buttons array or object (optional) @@ -246,13 +246,16 @@ module.exports = class ApplicationDelegate { buttonLabels = Object.keys(buttons); } - const chosen = remote.dialog.showMessageBox(remote.getCurrentWindow(), { - type: 'info', - message, - detail: detailedMessage, - buttons: buttonLabels, - normalizeAccessKeys: true - }); + const chosen = remote.dialog.showMessageBoxSync( + remote.getCurrentWindow(), + { + type: 'info', + message, + detail: detailedMessage, + buttons: buttonLabels, + normalizeAccessKeys: true + } + ); if (Array.isArray(buttons)) { return chosen; diff --git a/src/main-process/atom-application.js b/src/main-process/atom-application.js index bcf398973..5c9fbb253 100644 --- a/src/main-process/atom-application.js +++ b/src/main-process/atom-application.js @@ -2025,8 +2025,8 @@ module.exports = class AtomApplication extends EventEmitter { dialog.showOpenDialog(parentWindow, openOptions, callback); } - promptForRestart() { - dialog.showMessageBox( + async promptForRestart() { + const result = await dialog.showMessageBox( BrowserWindow.getFocusedWindow(), { type: 'warning', @@ -2034,11 +2034,9 @@ module.exports = class AtomApplication extends EventEmitter { message: 'You will need to restart Atom for this change to take effect.', buttons: ['Restart Atom', 'Cancel'] - }, - response => { - if (response === 0) this.restart(); } ); + if (result.response === 0) this.restart(); } restart() { diff --git a/src/main-process/atom-window.js b/src/main-process/atom-window.js index 2ff32af92..20c05112d 100644 --- a/src/main-process/atom-window.js +++ b/src/main-process/atom-window.js @@ -211,22 +211,17 @@ module.exports = class AtomWindow extends EventEmitter { this.resolveClosedPromise(); }); - this.browserWindow.on('unresponsive', () => { + this.browserWindow.on('unresponsive', async () => { if (this.isSpec) return; - dialog.showMessageBox( - this.browserWindow, - { - type: 'warning', - buttons: ['Force Close', 'Keep Waiting'], - cancelId: 1, // Canceling should be the least destructive action - message: 'Editor is not responding', - detail: - 'The editor is not responding. Would you like to force close it or just keep waiting?' - }, - response => { - if (response === 0) this.browserWindow.destroy(); - } - ); + const result = await dialog.showMessageBox(this.browserWindow, { + type: 'warning', + buttons: ['Force Close', 'Keep Waiting'], + cancelId: 1, // Canceling should be the least destructive action + message: 'Editor is not responding', + detail: + 'The editor is not responding. Would you like to force close it or just keep waiting?' + }); + if (result.response === 0) this.browserWindow.destroy(); }); this.browserWindow.webContents.on('crashed', async () => { @@ -237,24 +232,23 @@ module.exports = class AtomWindow extends EventEmitter { } await this.fileRecoveryService.didCrashWindow(this); - dialog.showMessageBox( - this.browserWindow, - { - type: 'warning', - buttons: ['Close Window', 'Reload', 'Keep It Open'], - cancelId: 2, // Canceling should be the least destructive action - message: 'The editor has crashed', - detail: 'Please report this issue to https://github.com/atom/atom' - }, - response => { - switch (response) { - case 0: - return this.browserWindow.destroy(); - case 1: - return this.browserWindow.reload(); - } - } - ); + + const result = await dialog.showMessageBox(this.browserWindow, { + type: 'warning', + buttons: ['Close Window', 'Reload', 'Keep It Open'], + cancelId: 2, // Canceling should be the least destructive action + message: 'The editor has crashed', + detail: 'Please report this issue to https://github.com/atom/atom' + }); + + switch (result.response) { + case 0: + this.browserWindow.destroy(); + break; + case 1: + this.browserWindow.reload(); + break; + } }); this.browserWindow.webContents.on('will-navigate', (event, url) => { diff --git a/src/main-process/auto-update-manager.js b/src/main-process/auto-update-manager.js index a55df8bd9..f33d718d2 100644 --- a/src/main-process/auto-update-manager.js +++ b/src/main-process/auto-update-manager.js @@ -168,17 +168,14 @@ module.exports = class AutoUpdateManager extends EventEmitter { onUpdateNotAvailable() { autoUpdater.removeListener('error', this.onUpdateError); const { dialog } = require('electron'); - dialog.showMessageBox( - { - type: 'info', - buttons: ['OK'], - icon: this.iconPath, - message: 'No update available.', - title: 'No Update Available', - detail: `Version ${this.version} is the latest version.` - }, - () => {} - ); // noop callback to get async behavior + dialog.showMessageBox({ + type: 'info', + buttons: ['OK'], + icon: this.iconPath, + message: 'No update available.', + title: 'No Update Available', + detail: `Version ${this.version} is the latest version.` + }); } onUpdateError(event, message) { @@ -187,17 +184,14 @@ module.exports = class AutoUpdateManager extends EventEmitter { this.onUpdateNotAvailable ); const { dialog } = require('electron'); - dialog.showMessageBox( - { - type: 'warning', - buttons: ['OK'], - icon: this.iconPath, - message: 'There was an error checking for updates.', - title: 'Update Error', - detail: message - }, - () => {} - ); // noop callback to get async behavior + dialog.showMessageBox({ + type: 'warning', + buttons: ['OK'], + icon: this.iconPath, + message: 'There was an error checking for updates.', + title: 'Update Error', + detail: message + }); } getWindows() { diff --git a/src/main-process/file-recovery-service.js b/src/main-process/file-recovery-service.js index 96834dd78..c0a36dfbe 100644 --- a/src/main-process/file-recovery-service.js +++ b/src/main-process/file-recovery-service.js @@ -83,13 +83,12 @@ module.exports = class FileRecoveryService { recoveryFile.recoveryPath }".`; console.log(detail); - dialog.showMessageBox( - window, - { type: 'info', buttons: ['OK'], message, detail }, - () => { - /* noop callback to get async behavior */ - } - ); + dialog.showMessageBox(window, { + type: 'info', + buttons: ['OK'], + message, + detail + }); }) .then(() => { for (let window of this.windowsByRecoveryFile.get(recoveryFile)) {