From 3d6343ed515c97841b7c5f2610037ad1fb2cb9f1 Mon Sep 17 00:00:00 2001 From: Shelley Vohr Date: Tue, 4 May 2021 12:11:16 +0200 Subject: [PATCH] fix: close attached sheet on window close (#28967) --- shell/browser/native_window_mac.mm | 15 ++++++++++----- spec-main/api-browser-window-spec.ts | 9 ++++++++- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/shell/browser/native_window_mac.mm b/shell/browser/native_window_mac.mm index 881e997329a..a4677f1823c 100644 --- a/shell/browser/native_window_mac.mm +++ b/shell/browser/native_window_mac.mm @@ -473,6 +473,12 @@ void NativeWindowMac::Close() { return; } + // If a sheet is attached to the window when we call + // [window_ performClose:nil], the window won't close properly + // even after the user has ended the sheet. + // Ensure it's closed before calling [window_ performClose:nil]. + SetEnabled(true); + [window_ performClose:nil]; // Closing a sheet doesn't trigger windowShouldClose, @@ -540,8 +546,7 @@ void NativeWindowMac::Hide() { // If a sheet is attached to the window when we call [window_ orderOut:nil], // the sheet won't be able to show again on the same window. // Ensure it's closed before calling [window_ orderOut:nil]. - if ([window_ attachedSheet]) - [window_ endSheet:[window_ attachedSheet]]; + SetEnabled(true); if (is_modal() && parent()) { [window_ orderOut:nil]; @@ -575,14 +580,14 @@ bool NativeWindowMac::IsEnabled() { } void NativeWindowMac::SetEnabled(bool enable) { - if (enable) { - [window_ endSheet:[window_ attachedSheet]]; - } else { + if (!enable) { [window_ beginSheet:window_ completionHandler:^(NSModalResponse returnCode) { NSLog(@"modal enabled"); return; }]; + } else if ([window_ attachedSheet]) { + [window_ endSheet:[window_ attachedSheet]]; } } diff --git a/spec-main/api-browser-window-spec.ts b/spec-main/api-browser-window-spec.ts index 3ec02d62832..a96c4891801 100644 --- a/spec-main/api-browser-window-spec.ts +++ b/spec-main/api-browser-window-spec.ts @@ -6,7 +6,7 @@ import * as os from 'os'; import * as qs from 'querystring'; import * as http from 'http'; import { AddressInfo } from 'net'; -import { app, BrowserWindow, BrowserView, ipcMain, OnBeforeSendHeadersListenerDetails, protocol, screen, webContents, session, WebContents, BrowserWindowConstructorOptions } from 'electron/main'; +import { app, BrowserWindow, BrowserView, dialog, ipcMain, OnBeforeSendHeadersListenerDetails, protocol, screen, webContents, session, WebContents, BrowserWindowConstructorOptions } from 'electron/main'; import { emittedOnce, emittedUntil, emittedNTimes } from './events-helpers'; import { ifit, ifdescribe, defer, delay } from './spec-helpers'; @@ -102,6 +102,13 @@ describe('BrowserWindow module', () => { w = null as unknown as BrowserWindow; }); + it('should work if called when a messageBox is showing', async () => { + const closed = emittedOnce(w, 'closed'); + dialog.showMessageBox(w, { message: 'Hello Error' }); + w.close(); + await closed; + }); + it('should emit unload handler', async () => { await w.loadFile(path.join(fixtures, 'api', 'unload.html')); const closed = emittedOnce(w, 'closed');