fix: crash on BrowserWindow.setEnabled() (#34904)

fix: crash on BrowserWindow.setEnabled()
This commit is contained in:
Shelley Vohr 2022-07-19 12:31:49 +02:00 committed by GitHub
parent 38848c5bf7
commit eb8c9452cb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 4 deletions

View file

@ -484,7 +484,8 @@ void NativeWindowMac::Close() {
// [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];
@ -554,7 +555,8 @@ 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].
SetEnabled(true);
if ([window_ attachedSheet])
[window_ endSheet:[window_ attachedSheet]];
if (is_modal() && parent()) {
[window_ orderOut:nil];
@ -593,9 +595,18 @@ bool NativeWindowMac::IsEnabled() {
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]) {

View file

@ -4331,6 +4331,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 });