Fix browserWindow detection in menu.popup()

This commit is contained in:
Charles Kerr 2018-02-20 18:35:39 -06:00
parent 21de806c80
commit 5a25b88b50
2 changed files with 12 additions and 17 deletions

View file

@ -53,15 +53,14 @@ Menu.prototype.popup = function (options) {
if (typeof x !== 'number') x = -1 if (typeof x !== 'number') x = -1
if (typeof y !== 'number') y = -1 if (typeof y !== 'number') y = -1
if (typeof positioningItem !== 'number') positioningItem = -1 if (typeof positioningItem !== 'number') positioningItem = -1
if (!window || (window && window.constructor !== BrowserWindow)) {
window = BrowserWindow.getFocusedWindow()
// No window focused? // find which window to use
if (!window) { const wins = BrowserWindow.getAllWindows()
const wins = BrowserWindow.getAllWindows() if (!wins || wins.indexOf(window) === -1) {
if (wins && wins.length > 0) { window = BrowserWindow.getFocusedWindow()
window = wins[0] if (!window && wins && wins.length > 0) {
} else { window = wins[0]
if (!window) {
throw new Error(`Cannot open Menu without a BrowserWindow present`) throw new Error(`Cannot open Menu without a BrowserWindow present`)
} }
} }

View file

@ -344,15 +344,11 @@ describe('Menu module', () => {
}) })
it('returns immediately', () => { it('returns immediately', () => {
const { browserWindow, x, y } = menu.popup({ const input = {window: w, x: 100, y: 101}
window: w, const output = menu.popup(input)
x: 100, assert.equal(output.x, input.x)
y: 101 assert.equal(output.y, input.y)
}) assert.equal(output.browserWindow, input.window)
assert.equal(browserWindow, w)
assert.equal(x, 100)
assert.equal(y, 101)
}) })
it('works without a given BrowserWindow and options', () => { it('works without a given BrowserWindow and options', () => {