From b9605cc602fed360fd9bf742dc4ec8af13769afa Mon Sep 17 00:00:00 2001 From: "trop[bot]" <37223003+trop[bot]@users.noreply.github.com> Date: Tue, 2 Jul 2019 18:26:59 -0700 Subject: [PATCH] fix: Correct modal focus behavior on macOS (#19061) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * wip: wish i could hack the main window focus away * main -> key * remove useless code * test: Add focus event spec * test: more robust spec * test: simplify test * duplicate non-firing macOS event listener 😳 * destroy 🚨 * chore: remove unused variable --- .../ui/cocoa/atom_ns_window_delegate.mm | 4 +-- spec/api-browser-window-spec.js | 27 +++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/atom/browser/ui/cocoa/atom_ns_window_delegate.mm b/atom/browser/ui/cocoa/atom_ns_window_delegate.mm index b96316a3ee753..8b1d83aef8cd5 100644 --- a/atom/browser/ui/cocoa/atom_ns_window_delegate.mm +++ b/atom/browser/ui/cocoa/atom_ns_window_delegate.mm @@ -83,11 +83,11 @@ - (NSRect)windowWillUseStandardFrame:(NSWindow*)window return frame; } -- (void)windowDidBecomeMain:(NSNotification*)notification { +- (void)windowDidBecomeKey:(NSNotification*)notification { shell_->NotifyWindowFocus(); } -- (void)windowDidResignMain:(NSNotification*)notification { +- (void)windowDidResignKey:(NSNotification*)notification { shell_->NotifyWindowBlur(); } diff --git a/spec/api-browser-window-spec.js b/spec/api-browser-window-spec.js index 540a197b4eb6e..d309ad0253753 100644 --- a/spec/api-browser-window-spec.js +++ b/spec/api-browser-window-spec.js @@ -2330,6 +2330,33 @@ describe('BrowserWindow module', () => { }) }) + describe('focus event', () => { + it('should not emit if focusing on a main window with a modal open', (done) => { + const child = new BrowserWindow({ + parent: w, + modal: true, + show: false + }) + + child.once('ready-to-show', () => { + child.show() + }) + + child.on('show', () => { + w.once('focus', () => { + expect(child.isDestroyed()).to.equal(true) + done() + }) + w.focus() // this should not trigger the above listener + child.close() + }) + + // act + child.loadURL(server.url) + w.show() + }) + }) + describe('sheet-begin event', () => { let sheet = null