diff --git a/lib/browser/api/menu.js b/lib/browser/api/menu.js index e599cfb3c06..83198bdf601 100644 --- a/lib/browser/api/menu.js +++ b/lib/browser/api/menu.js @@ -43,7 +43,7 @@ Menu.prototype._init = function () { } } -Menu.prototype.popup = function (window, x, y, positioningItem) { +Menu.prototype.popupOld = function (window, x, y, positioningItem) { let [newX, newY, newPosition, newWindow] = [x, y, positioningItem, window] let opts let callback @@ -102,6 +102,52 @@ Menu.prototype.popup = function (window, x, y, positioningItem) { return { browserWindow: newWindow, x: newX, y: newY, position: newPosition } } +Menu.prototype.popup = function (window, options, callback) { + let x, y, pos + let [win, opts, cb] = [window, options, callback] + + if (!opts && !callback) { + // win.popup({opts}) + if (typeof window !== 'function') { + opts = window + // win.popup(callback) + } else { + callback = window + } + } + + if (typeof opts === 'object') { + [x, y, pos] = [opts.x, opts.y, opts.newPosition] + } + + // win.popup(win, callback) + if (typeof opts === 'function') cb = opts + + // no callback passed + if (!cb || typeof cb !== 'function') cb = () => {} + + // set defaults + if (typeof x !== 'number') x = -1 + if (typeof y !== 'number') y = -1 + if (typeof pos !== 'number') pos = -1 + if (!win || (win && win.constructor !== BrowserWindow)) { + win = BrowserWindow.getFocusedWindow() + + // No window focused? + if (!win) { + const wins = BrowserWindow.getAllWindows() + if (wins && wins.length > 0) { + win = wins[0] + } else { + throw new Error(`Cannot open Menu without a BrowserWindow present`) + } + } + } + + this.popupAt(win, x, y, pos, cb) + return { browserWindow: win, x, y, position: pos } +} + Menu.prototype.closePopup = function (window) { if (window && window.constructor !== BrowserWindow) { this.closePopupAt(window.id)