Skip to content

Commit b9605cc

Browse files
trop[bot]codebytere
authored andcommitted
fix: Correct modal focus behavior on macOS (electron#19061)
* 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
1 parent b98e06e commit b9605cc

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

atom/browser/ui/cocoa/atom_ns_window_delegate.mm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,11 @@ - (NSRect)windowWillUseStandardFrame:(NSWindow*)window
8383
return frame;
8484
}
8585

86-
- (void)windowDidBecomeMain:(NSNotification*)notification {
86+
- (void)windowDidBecomeKey:(NSNotification*)notification {
8787
shell_->NotifyWindowFocus();
8888
}
8989

90-
- (void)windowDidResignMain:(NSNotification*)notification {
90+
- (void)windowDidResignKey:(NSNotification*)notification {
9191
shell_->NotifyWindowBlur();
9292
}
9393

spec/api-browser-window-spec.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2330,6 +2330,33 @@ describe('BrowserWindow module', () => {
23302330
})
23312331
})
23322332

2333+
describe('focus event', () => {
2334+
it('should not emit if focusing on a main window with a modal open', (done) => {
2335+
const child = new BrowserWindow({
2336+
parent: w,
2337+
modal: true,
2338+
show: false
2339+
})
2340+
2341+
child.once('ready-to-show', () => {
2342+
child.show()
2343+
})
2344+
2345+
child.on('show', () => {
2346+
w.once('focus', () => {
2347+
expect(child.isDestroyed()).to.equal(true)
2348+
done()
2349+
})
2350+
w.focus() // this should not trigger the above listener
2351+
child.close()
2352+
})
2353+
2354+
// act
2355+
child.loadURL(server.url)
2356+
w.show()
2357+
})
2358+
})
2359+
23332360
describe('sheet-begin event', () => {
23342361
let sheet = null
23352362

0 commit comments

Comments
 (0)