From 1eb398b328d304f227deef0e89ca0be7b9e20755 Mon Sep 17 00:00:00 2001 From: Shelley Vohr Date: Wed, 16 Aug 2023 13:28:29 +0200 Subject: [PATCH] fix: crash when calling `BrowserWindow.moveTop()` on modal children (#39499) fix: crash when calling moveTop() on modal children --- shell/browser/native_window_mac.mm | 7 ++++--- spec/api-browser-window-spec.ts | 11 +++++++++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/shell/browser/native_window_mac.mm b/shell/browser/native_window_mac.mm index 7c44cfbed18a..b6ce683db3d8 100644 --- a/shell/browser/native_window_mac.mm +++ b/shell/browser/native_window_mac.mm @@ -812,7 +812,7 @@ bool NativeWindowMac::MoveAbove(const std::string& sourceId) { if (!webrtc::GetWindowOwnerPid(window_id)) return false; - if (!parent()) { + if (!parent() || is_modal()) { [window_ orderWindow:NSWindowAbove relativeTo:window_id]; } else { NSWindow* other_window = [NSApp windowWithWindowNumber:window_id]; @@ -823,10 +823,11 @@ bool NativeWindowMac::MoveAbove(const std::string& sourceId) { } void NativeWindowMac::MoveTop() { - if (!parent()) + if (!parent() || is_modal()) { [window_ orderWindow:NSWindowAbove relativeTo:0]; - else + } else { ReorderChildWindowAbove(window_, nullptr); + } } void NativeWindowMac::SetResizable(bool resizable) { diff --git a/spec/api-browser-window-spec.ts b/spec/api-browser-window-spec.ts index bf7ddada8508..c2057bce7b32 100644 --- a/spec/api-browser-window-spec.ts +++ b/spec/api-browser-window-spec.ts @@ -1283,6 +1283,8 @@ describe('BrowserWindow module', () => { }); describe('BrowserWindow.moveTop()', () => { + afterEach(closeAllWindows); + it('should not steal focus', async () => { const posDelta = 50; const wShownInactive = once(w, 'show'); @@ -1324,6 +1326,15 @@ describe('BrowserWindow module', () => { await closeWindow(otherWindow, { assertNotWindows: false }); expect(BrowserWindow.getAllWindows()).to.have.lengthOf(1); }); + + it('should not crash when called on a modal child window', async () => { + const shown = once(w, 'show'); + w.show(); + await shown; + + const child = new BrowserWindow({ modal: true, parent: w }); + expect(() => { child.moveTop(); }).to.not.throw(); + }); }); describe('BrowserWindow.moveAbove(mediaSourceId)', () => {