Browse Source

Merge pull request #11224 from electron/browser-view-window

feature: Add `BrowserWindow.fromBrowserView()`
Charles Kerr 7 years ago
parent
commit
9c217fc6c7
3 changed files with 38 additions and 1 deletions
  1. 6 0
      docs/api/browser-window.md
  2. 8 0
      lib/browser/api/browser-window.js
  3. 24 1
      spec/api-browser-window-spec.js

+ 6 - 0
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

+ 8 - 0
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

+ 24 - 1
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()