Skip to content

BaseWindow.fromWebContents() #44899

Closed as not planned
Closed as not planned

Description

@jjeff

Preflight Checklist

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

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
}

Additional Information

Assuming that BrowserWindow is a subclass of BaseWindow, this method should also work for BrowserWindow.webContents instances.

Activity

  1. Nezha1234-pz commented on Dec 1, 2024

    @Nezha1234-pz

    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
    }

  2. MarshallOfSound commented on Dec 1, 2024

    @MarshallOfSound
    Member

    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.

  3. jjeff commented on Dec 2, 2024

    @jjeff
    Author

    I don't expect that most implementations will have a very deep view tree.

    Also am I wrong to assume that 99% of webContents instances will have a parent BaseWindow?

  4. nikwen commented on Dec 2, 2024

    @nikwen
    Member

    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.

  5. nikwen commented on Dec 3, 2024

    @nikwen
    Member

    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.

    Starting point to dig through the code.

  6. codebytere commented on Aug 23, 2026

    @codebytere
    Member

    closing per the discussion: a WebContents doesn't have a well-defined owning BaseWindow, and apps that need the mapping can keep it themselves.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions