fix: call focus on native window on call to webContents.focus on mac (#22323)

* fix: call focus on native window on call to webContents.focus on mac

On mac call to web_contents()->Focus() is not enough so it's
necessary to call it on native window.

* fixup! fix: call focus on native window on call to webContents.focus on mac

* fixup! fix: call focus on native window on call to webContents.focus on mac

* test: close all windows after test

* fix: also match the behavior on Linux

Co-authored-by: Cheng Zhao <zcbenz@github.com>
This commit is contained in:
CezaryKulakowski 2020-03-02 08:51:02 +01:00 committed by GitHub
parent cad7054e4f
commit 0bc906853e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 0 deletions

View file

@ -2071,6 +2071,12 @@ void WebContents::CopyImageAt(int x, int y) {
}
void WebContents::Focus() {
// Focusing on WebContents does not automatically focus the window on macOS
// and Linux, do it manually to match the behavior on Windows.
#if defined(OS_MACOSX) || defined(OS_LINUX)
if (owner_window())
owner_window()->Focus(true);
#endif
web_contents()->Focus();
}

View file

@ -3835,6 +3835,22 @@ describe('BrowserWindow module', () => {
})
})
describe('window.webContents.focus()', () => {
afterEach(closeAllWindows)
it('focuses window', (done) => {
const w1 = new BrowserWindow({ x: 100, y: 300, width: 300, height: 200 })
w1.loadURL('about:blank')
const w2 = new BrowserWindow({ x: 300, y: 300, width: 300, height: 200 })
w2.loadURL('about:blank')
w1.webContents.focus()
// Give focus some time to switch to w1
setTimeout(() => {
expect(w1.webContents.isFocused()).to.be.true('focuses window')
done()
})
})
})
const features = process.electronBinding('features')
ifdescribe(features.isOffscreenRenderingEnabled())('offscreen rendering', () => {
let w: BrowserWindow