Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: getting focused window with destroyed webContents #33539

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/browser/api/browser-window.ts
Expand Up @@ -72,7 +72,10 @@ BrowserWindow.getAllWindows = () => {

BrowserWindow.getFocusedWindow = () => {
for (const window of BrowserWindow.getAllWindows()) {
if (window.isFocused() || window.isDevToolsFocused()) return window;
const hasWC = window.webContents && !window.webContents.isDestroyed();
if (!window.isDestroyed() && hasWC) {
if (window.isFocused() || window.isDevToolsFocused()) return window;
}
}
return null;
};
Expand Down
19 changes: 19 additions & 0 deletions spec-main/api-browser-window-spec.ts
Expand Up @@ -3718,6 +3718,25 @@ describe('BrowserWindow module', () => {
expect(w.getChildWindows().length).to.equal(0);
});

it('closes a grandchild window when a middle child window is destroyed', (done) => {
const w = new BrowserWindow();

w.loadFile(path.join(fixtures, 'pages', 'base-page.html'));
w.webContents.executeJavaScript('window.open("")');

w.webContents.on('did-create-window', async (window) => {
const childWindow = new BrowserWindow({ parent: window });

await delay();
window.close();

childWindow.on('closed', () => {
expect(() => { BrowserWindow.getFocusedWindow(); }).to.not.throw();
done();
});
});
});

it('should not affect the show option', () => {
const w = new BrowserWindow({ show: false });
const c = new BrowserWindow({ show: false, parent: w });
Expand Down