feat: add focusOnNavigation flag to WebPreferences (#49511)

* feat: add focusOnNavigation webPreference

Co-authored-by: Kyle Cutler <kycutler@microsoft.com>

* WebContentsView tests

Co-authored-by: Kyle Cutler <kycutler@microsoft.com>

* fix

Co-authored-by: Kyle Cutler <kycutler@microsoft.com>

* fix

Co-authored-by: Kyle Cutler <kycutler@microsoft.com>

---------

Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: Kyle Cutler <kycutler@microsoft.com>
This commit is contained in:
trop[bot]
2026-02-04 15:44:03 -05:00
committed by GitHub
parent 0abdb91b78
commit 2dbdf223b7
7 changed files with 77 additions and 0 deletions

View File

@@ -1610,6 +1610,35 @@ describe('webContents module', () => {
await expect(blurPromise).to.eventually.be.fulfilled();
});
});
describe('focusOnNavigation webPreference', () => {
afterEach(closeAllWindows);
it('focuses the webContents on navigation by default', async () => {
const w = new BrowserWindow({ show: true });
await once(w, 'focus');
await w.loadURL('about:blank');
await moveFocusToDevTools(w);
expect(w.webContents.isFocused()).to.be.false();
await w.loadURL('data:text/html,<body>test</body>');
expect(w.webContents.isFocused()).to.be.true();
});
it('does not focus the webContents on navigation when focusOnNavigation is false', async () => {
const w = new BrowserWindow({
show: true,
webPreferences: {
focusOnNavigation: false
}
});
await once(w, 'focus');
await w.loadURL('about:blank');
await moveFocusToDevTools(w);
expect(w.webContents.isFocused()).to.be.false();
await w.loadURL('data:text/html,<body>test</body>');
expect(w.webContents.isFocused()).to.be.false();
});
});
});
describe('getOSProcessId()', () => {

View File

@@ -398,4 +398,38 @@ describe('WebContentsView', () => {
v.setBorderRadius(100);
});
});
describe('focusOnNavigation webPreference', () => {
it('focuses the webContents on navigation by default', async () => {
const w = new BrowserWindow();
await once(w, 'focus');
const v = new WebContentsView();
w.setContentView(v);
await v.webContents.loadURL('about:blank');
const devToolsFocused = once(v.webContents, 'devtools-focused');
v.webContents.openDevTools({ mode: 'right' });
await devToolsFocused;
expect(v.webContents.isFocused()).to.be.false();
await v.webContents.loadURL('data:text/html,<body>test</body>');
expect(v.webContents.isFocused()).to.be.true();
});
it('does not focus the webContents on navigation when focusOnNavigation is false', async () => {
const w = new BrowserWindow();
await once(w, 'focus');
const v = new WebContentsView({
webPreferences: {
focusOnNavigation: false
}
});
w.setContentView(v);
await v.webContents.loadURL('about:blank');
const devToolsFocused = once(v.webContents, 'devtools-focused');
v.webContents.openDevTools({ mode: 'right' });
await devToolsFocused;
expect(v.webContents.isFocused()).to.be.false();
await v.webContents.loadURL('data:text/html,<body>test</body>');
expect(v.webContents.isFocused()).to.be.false();
});
});
});