fix: getting focused window with destroyed webContents (#33404)
* fix: getting focused window with destroyed webContents * fix: add extra safeguards
This commit is contained in:
parent
8ea0631b82
commit
d1ea62c3e8
2 changed files with 23 additions and 1 deletions
|
@ -72,8 +72,11 @@ BrowserWindow.getAllWindows = () => {
|
||||||
|
|
||||||
BrowserWindow.getFocusedWindow = () => {
|
BrowserWindow.getFocusedWindow = () => {
|
||||||
for (const window of BrowserWindow.getAllWindows()) {
|
for (const window of BrowserWindow.getAllWindows()) {
|
||||||
|
const hasWC = window.webContents && !window.webContents.isDestroyed();
|
||||||
|
if (!window.isDestroyed() && hasWC) {
|
||||||
if (window.isFocused() || window.isDevToolsFocused()) return window;
|
if (window.isFocused() || window.isDevToolsFocused()) return window;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return null;
|
return null;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -3725,6 +3725,25 @@ describe('BrowserWindow module', () => {
|
||||||
expect(w.getChildWindows().length).to.equal(0);
|
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', () => {
|
it('should not affect the show option', () => {
|
||||||
const w = new BrowserWindow({ show: false });
|
const w = new BrowserWindow({ show: false });
|
||||||
const c = new BrowserWindow({ show: false, parent: w });
|
const c = new BrowserWindow({ show: false, parent: w });
|
||||||
|
|
Loading…
Reference in a new issue