From 9e660b1d327d378df7f454f6ea12dd5295af9bfe Mon Sep 17 00:00:00 2001 From: Felix Rieseberg Date: Fri, 8 Dec 2017 14:36:52 -0800 Subject: [PATCH] :wrench: Always find a window (or error) --- lib/browser/api/menu.js | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/browser/api/menu.js b/lib/browser/api/menu.js index 1cf1a1013403..bec3cce7644e 100644 --- a/lib/browser/api/menu.js +++ b/lib/browser/api/menu.js @@ -63,10 +63,23 @@ Menu.prototype.popup = function (window, x, y, positioningItem) { } // set defaults - if (!window) newWindow = BrowserWindow.getFocusedWindow() if (typeof newX !== 'number') newX = -1 if (typeof newY !== 'number') newY = -1 if (typeof newPosition !== 'number') newPosition = -1 + if (!newWindow || (newWindow && newWindow.constructor !== BrowserWindow)) { + newWindow = BrowserWindow.getFocusedWindow() + + // No window focused? + if (!newWindow) { + const browserWindows = BrowserWindow.getAllWindows() + + if (browserWindows && browserWindows.length > 0) { + newWindow = browserWindows[0] + } else { + throw new Error(`Cannot open Menu without a BrowserWindow present`) + } + } + } this.popupAt(newWindow, newX, newY, newPosition) }