Skip to content

Commit

Permalink
fix: Correct modal focus behavior on macOS (#18995)
Browse files Browse the repository at this point in the history
Fixes #18502

This PR changes the focus and blur events that we emit in Electron to listen to changes in key window rather than main window. It swaps out windowDidBecomeMain and windowDidResignMain for windowDidBecomeKey and windowDidResignKey, respectively.
  • Loading branch information
erickzhao authored and codebytere committed Jul 1, 2019
1 parent 3173b66 commit c7da54e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
4 changes: 2 additions & 2 deletions shell/browser/ui/cocoa/atom_ns_window_delegate.mm
Expand Up @@ -88,11 +88,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();
}

Expand Down
28 changes: 28 additions & 0 deletions spec/api-browser-window-spec.js
Expand Up @@ -1637,6 +1637,34 @@ describe('BrowserWindow module', () => {
})
})

describe('focus event', () => {
it('should not emit if focusing on a main window with a modal open', (done) => {
const childWindowClosed = false
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

Expand Down

0 comments on commit c7da54e

Please sign in to comment.