diff --git a/lib/browser/api/menu.js b/lib/browser/api/menu.js index a340f74abcf0..78f1a4466a84 100644 --- a/lib/browser/api/menu.js +++ b/lib/browser/api/menu.js @@ -45,30 +45,49 @@ Menu.prototype._init = function () { Menu.prototype.popup = function (window, x, y, positioningItem) { let [newX, newY, newPosition, newWindow] = [x, y, positioningItem, window] + let opts // menu.popup(x, y, positioningItem) - if (!window) { - // shift over values - if (typeof window !== 'object' || window.constructor !== BrowserWindow) { - [newPosition, newY, newX, newWindow] = [y, x, window, null] - } + if (window != null && !(window instanceof BrowserWindow)) { + [newPosition, newY, newX, newWindow] = [y, x, window, null] } + // menu.popup({}) + if (window != null && window.constructor === Object) { + opts = window // menu.popup(window, {}) - if (x && typeof x === 'object') { - const opts = x + } else if (x && typeof x === 'object') { + opts = x + } + + if (opts) { newX = opts.x newY = opts.y newPosition = opts.positioningItem } // set defaults - if (typeof x !== 'number') newX = -1 - if (typeof y !== 'number') newY = -1 - if (typeof positioningItem !== 'number') newPosition = -1 - 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) + + return { browserWindow: newWindow, x: newX, y: newY, position: newPosition } } Menu.prototype.closePopup = function (window) { diff --git a/spec/api-menu-spec.js b/spec/api-menu-spec.js index 538c640e7121..0e1050066855 100644 --- a/spec/api-menu-spec.js +++ b/spec/api-menu-spec.js @@ -279,12 +279,49 @@ describe('Menu module', () => { }) afterEach(() => { + menu.closePopup() + menu.closePopup(w) return closeWindow(w).then(() => { w = null }) }) it('returns immediately', () => { - menu.popup(w, {x: 100, y: 100, async: true}) - menu.closePopup(w) + const { browserWindow, x, y } = menu.popup(w, {x: 100, y: 101}) + + assert.equal(browserWindow, w) + assert.equal(x, 100) + assert.equal(y, 101) + }) + + it('works without a given BrowserWindow and options', () => { + const { browserWindow, x, y } = menu.popup({x: 100, y: 101}) + + assert.equal(browserWindow.constructor.name, 'BrowserWindow') + assert.equal(x, 100) + assert.equal(y, 101) + }) + + it('works without a given BrowserWindow', () => { + const { browserWindow, x, y } = menu.popup(100, 101) + + assert.equal(browserWindow.constructor.name, 'BrowserWindow') + assert.equal(x, 100) + assert.equal(y, 101) + }) + + it('works without a given BrowserWindow and 0 options', () => { + const { browserWindow, x, y } = menu.popup(0, 1) + + assert.equal(browserWindow.constructor.name, 'BrowserWindow') + assert.equal(x, 0) + assert.equal(y, 1) + }) + + it('works with a given BrowserWindow and no options', () => { + const { browserWindow, x, y } = menu.popup(w, 100, 101) + + assert.equal(browserWindow, w) + assert.equal(x, 100) + assert.equal(y, 101) }) })