fix: Correct modal focus behavior on macOS (#18995)

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.
This commit is contained in:
Erick Zhao 2019-07-01 10:07:27 -07:00 committed by Shelley Vohr
parent 3173b66d00
commit c7da54e82a
2 changed files with 30 additions and 2 deletions

View file

@ -88,11 +88,11 @@ using TitleBarStyle = electron::NativeWindowMac::TitleBarStyle;
return frame;
}
- (void)windowDidBecomeMain:(NSNotification*)notification {
- (void)windowDidBecomeKey:(NSNotification*)notification {
shell_->NotifyWindowFocus();
}
- (void)windowDidResignMain:(NSNotification*)notification {
- (void)windowDidResignKey:(NSNotification*)notification {
shell_->NotifyWindowBlur();
}

View file

@ -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