diff --git a/lib/browser/api/browser-window.ts b/lib/browser/api/browser-window.ts index 79b71e5545..0eaaf56845 100644 --- a/lib/browser/api/browser-window.ts +++ b/lib/browser/api/browser-window.ts @@ -232,7 +232,12 @@ BrowserWindow.prototype.getBrowserViews = function () { BrowserWindow.prototype.setTopBrowserView = function (browserView: BrowserView) { if (browserView.ownerWindow !== this) { throw new Error('Given BrowserView is not attached to the window'); } - this.addBrowserView(browserView); + const idx = this._browserViews.indexOf(browserView); + if (idx >= 0) { + this.contentView.addChildView(browserView.webContentsView); + this._browserViews.splice(idx, 1); + this._browserViews.push(browserView); + } }; module.exports = BrowserWindow; diff --git a/spec/api-browser-view-spec.ts b/spec/api-browser-view-spec.ts index 45ed4cfa7b..e8cbbd1dee 100644 --- a/spec/api-browser-view-spec.ts +++ b/spec/api-browser-view-spec.ts @@ -520,6 +520,19 @@ describe('BrowserView module', () => { win2.close(); win2.destroy(); }); + + it('should reorder the BrowserView to the top if it is already in the window', () => { + view = new BrowserView(); + const view2 = new BrowserView(); + defer(() => view2.webContents.destroy()); + w.addBrowserView(view); + w.addBrowserView(view2); + defer(() => w.removeBrowserView(view2)); + + w.setTopBrowserView(view); + const views = w.getBrowserViews(); + expect(views.indexOf(view)).to.equal(views.length - 1); + }); }); describe('BrowserView.webContents.getOwnerBrowserWindow()', () => {