From 6e1cee5a27b0f867cd34b560e5261a178d6c8933 Mon Sep 17 00:00:00 2001 From: Shelley Vohr Date: Wed, 13 Jul 2022 15:59:30 +0200 Subject: [PATCH] fix: crash on BrowserWindow.setEnabled() --- shell/browser/native_window_mac.mm | 19 +++++++++++++++---- spec-main/api-browser-window-spec.ts | 8 ++++++++ 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/shell/browser/native_window_mac.mm b/shell/browser/native_window_mac.mm index 843e370f36929..ea567d2d6ed41 100644 --- a/shell/browser/native_window_mac.mm +++ b/shell/browser/native_window_mac.mm @@ -483,7 +483,8 @@ void ViewDidMoveToSuperview(NSView* self, SEL _cmd) { // [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); + if ([window_ attachedSheet]) + [window_ endSheet:[window_ attachedSheet]]; [window_ performClose:nil]; @@ -553,7 +554,8 @@ void ViewDidMoveToSuperview(NSView* self, SEL _cmd) { // 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]. - SetEnabled(true); + if ([window_ attachedSheet]) + [window_ endSheet:[window_ attachedSheet]]; if (is_modal() && parent()) { [window_ orderOut:nil]; @@ -597,9 +599,18 @@ void ViewDidMoveToSuperview(NSView* self, SEL _cmd) { void NativeWindowMac::SetEnabled(bool enable) { if (!enable) { - [window_ beginSheet:window_ + NSRect frame = [window_ frame]; + NSWindow* window = + [[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, frame.size.width, + frame.size.height) + styleMask:NSWindowStyleMaskTitled + backing:NSBackingStoreBuffered + defer:NO]; + [window setAlphaValue:0.5]; + + [window_ beginSheet:window completionHandler:^(NSModalResponse returnCode) { - NSLog(@"modal enabled"); + NSLog(@"main window disabled"); return; }]; } else if ([window_ attachedSheet]) { diff --git a/spec-main/api-browser-window-spec.ts b/spec-main/api-browser-window-spec.ts index 1576c86630621..c8b1e1e9f13bf 100644 --- a/spec-main/api-browser-window-spec.ts +++ b/spec-main/api-browser-window-spec.ts @@ -4234,6 +4234,14 @@ describe('BrowserWindow module', () => { await createTwo(); }); + ifit(process.platform !== 'darwin')('can disable and enable a window', () => { + const w = new BrowserWindow({ show: false }); + w.setEnabled(false); + expect(w.isEnabled()).to.be.false('w.isEnabled()'); + w.setEnabled(true); + expect(w.isEnabled()).to.be.true('!w.isEnabled()'); + }); + ifit(process.platform !== 'darwin')('disables parent window', () => { const w = new BrowserWindow({ show: false }); const c = new BrowserWindow({ show: false, parent: w, modal: true });