You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I agree to follow the Code of Conduct that this project adheres to.
I have searched the issue tracker for a feature request that matches the one I want to file, without success.
Problem Description
While it is possible to determine the BrowserWindow from a WebContents instance using BrowserWindow.fromWebContents(), there is not a similar method for BaseWindow.
Proposed Solution
New static method: BaseWindow.fromWebContents()
Alternatives Considered
exportfunctionbaseWindowFromWebContents(wc: Electron.WebContents): Electron.BaseWindow|null{constallWindows=BaseWindow.getAllWindows()for(constwinofallWindows){if(win.contentView.children.some(// This should probably handle nested views(child)=>childinstanceofWebContentsView&&child.webContents===wc)){returnwin}}returnnull}
Additional Information
Assuming that BrowserWindow is a subclass of BaseWindow, this method should also work for BrowserWindow.webContents instances.
This API doesn't exist because there isn't a clean relationship between a WebContents and a BaseWindow, it could exist anywhere in the view heirachy.
Traversing the entire view tree of a BaseWindow just looking for a WebContents isn't very efficient and individual apps using these APIs will be able to make much more performant relationship determinations (because their know their view structure).
I'd be a -1 against adding such an API in Electron core.
I'm also not sure this function should be in Electron.
The implementation seems error prone. For example, someone could add container views to a BaseWindow that contain more child WebContentsViews. Maybe there will be more view types in the future.
Adding support for traversing this structure seems like a relatively high maintenance burden for something that an app can implement rather easily.
Looking into this a bit further, it looks like owner windows are currently assigned to webContents when they are initialized or set as the main webContents of a BrowserWindow.
If each WebContentsView kept track of its BaseWindow owner window and updated it when it got assigned to a new window, this could be implemented with good performance.
Drawback: Situations like the following would still be difficult:
A View is added to a BaseWindow
A WebContentsView is added to the View
The View is removed from the BaseWindow and assigned to a different BaseWindow
The owner would have to be updated for the View and for the nested WebContentsView
Possible to do but probably prone to errors and regressions, especially as new features are added.
After this investigation, I wouldn't -1 outright but I'm not convinced yet that it's worth the effort.
Activity
Nezha1234-pz commentedon Dec 1, 2024
export function baseWindowFromWebContents(
wc: Electron.WebContents
): Electron.BaseWindow | null {
const allWindows = BaseWindow.getAllWindows()
for (const win of allWindows) {
if (
win.contentView.children.some(
// This should probably handle nested views
(child) => child instanceof WebContentsView && child.webContents === wc
)
) {
return win
}
}
return null
}
MarshallOfSound commentedon Dec 1, 2024
This API doesn't exist because there isn't a clean relationship between a WebContents and a BaseWindow, it could exist anywhere in the view heirachy.
Traversing the entire view tree of a BaseWindow just looking for a WebContents isn't very efficient and individual apps using these APIs will be able to make much more performant relationship determinations (because their know their view structure).
I'd be a -1 against adding such an API in Electron core.
jjeff commentedon Dec 2, 2024
I don't expect that most implementations will have a very deep view tree.
Also am I wrong to assume that 99% of
webContentsinstances will have a parent BaseWindow?nikwen commentedon Dec 2, 2024
I'm also not sure this function should be in Electron.
The implementation seems error prone. For example, someone could add container views to a
BaseWindowthat contain more childWebContentsViews. Maybe there will be more view types in the future.Adding support for traversing this structure seems like a relatively high maintenance burden for something that an app can implement rather easily.
nikwen commentedon Dec 3, 2024
Looking into this a bit further, it looks like owner windows are currently assigned to
webContentswhen they are initialized or set as the mainwebContentsof aBrowserWindow.If each
WebContentsViewkept track of itsBaseWindowowner window and updated it when it got assigned to a new window, this could be implemented with good performance.Drawback: Situations like the following would still be difficult:
Viewis added to aBaseWindowWebContentsViewis added to theViewBaseWindowand assigned to a differentBaseWindowViewand for the nestedWebContentsViewPossible to do but probably prone to errors and regressions, especially as new features are added.
After this investigation, I wouldn't -1 outright but I'm not convinced yet that it's worth the effort.
Starting point to dig through the code.
codebytere commentedon Aug 23, 2026
closing per the discussion: a WebContents doesn't have a well-defined owning BaseWindow, and apps that need the mapping can keep it themselves.