diff --git a/docs/api/browser-window.md b/docs/api/browser-window.md index 4b4a677af925..2cddafa25e76 100644 --- a/docs/api/browser-window.md +++ b/docs/api/browser-window.md @@ -580,6 +580,12 @@ Returns `BrowserWindow` - The window that is focused in this application, otherw Returns `BrowserWindow` - The window that owns the given `webContents`. +#### `BrowserWindow.fromBrowserView(browserView)` + +* `browserView` [BrowserView](browser-view.md) + +Returns `BrowserWindow | null` - The window that owns the given `browserView`. If the given view is not attached to any window, returns `null`. + #### `BrowserWindow.fromId(id)` * `id` Integer diff --git a/lib/browser/api/browser-window.js b/lib/browser/api/browser-window.js index 96cadbd33de6..b977e8766f9d 100644 --- a/lib/browser/api/browser-window.js +++ b/lib/browser/api/browser-window.js @@ -144,6 +144,14 @@ BrowserWindow.fromWebContents = (webContents) => { } } +BrowserWindow.fromBrowserView = (browserView) => { + for (const window of BrowserWindow.getAllWindows()) { + if (window.getBrowserView() === browserView) return window + } + + return null +} + BrowserWindow.fromDevToolsWebContents = (webContents) => { for (const window of BrowserWindow.getAllWindows()) { const {devToolsWebContents} = window diff --git a/spec/api-browser-window-spec.js b/spec/api-browser-window-spec.js index bb5996e68335..2adc82df05d0 100644 --- a/spec/api-browser-window-spec.js +++ b/spec/api-browser-window-spec.js @@ -9,7 +9,7 @@ const http = require('http') const {closeWindow} = require('./window-helpers') const {ipcRenderer, remote, screen} = require('electron') -const {app, ipcMain, BrowserWindow, protocol, webContents} = remote +const {app, ipcMain, BrowserWindow, BrowserView, protocol, webContents} = remote const isCI = remote.getGlobal('isCi') const nativeModulesEnabled = remote.getGlobal('nativeModulesEnabled') @@ -802,6 +802,29 @@ describe('BrowserWindow module', () => { }) }) + describe('BrowserWindow.fromBrowserView(browserView)', () => { + let bv = null + + beforeEach(() => { + bv = new BrowserView() + w.setBrowserView(bv) + }) + + afterEach(() => { + w.setBrowserView(null) + bv.destroy() + }) + + it('returns the window with the browserView', () => { + assert.equal(BrowserWindow.fromBrowserView(bv).id, w.id) + }) + + it('returns undefined if not attached', () => { + w.setBrowserView(null) + assert.equal(BrowserWindow.fromBrowserView(bv), undefined) + }) + }) + describe('BrowserWindow.setOpacity(opacity)', () => { it('make window with initial opacity', () => { w.destroy()