Merge pull request #11968 from electron/refactor-menu-popup

Refactor menu.popup
This commit is contained in:
John Kleinschmidt 2018-02-21 14:29:52 -05:00 committed by GitHub
commit 2a97e48465
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 47 additions and 95 deletions

View file

@ -46,63 +46,31 @@ Menu.prototype._init = function () {
this.delegate = delegate
}
Menu.prototype.popup = function (window, x, y, positioningItem) {
let [newX, newY, newPosition, newWindow] = [x, y, positioningItem, window]
let opts
let callback
Menu.prototype.popup = function (options) {
let {window, x, y, positioningItem, callback} = options
// menu.popup(x, y, positioningItem)
if (window != null && !(window instanceof BrowserWindow)) {
[newPosition, newY, newX, newWindow] = [y, x, window, null]
}
// menu.popup([w], x, y, callback)
if (typeof newPosition === 'function') {
callback = newPosition
}
// menu.popup({}) || menu.popup(window, callback)
if ((window != null && window.constructor === Object) ||
(x && typeof x === 'function')) {
opts = window
callback = arguments[1]
// menu.popup(window, {})
} else if (x && typeof x === 'object') {
opts = x
callback = arguments[2]
}
if (opts) {
newX = opts.x
newY = opts.y
newPosition = opts.positioningItem
}
if (typeof callback !== 'function') {
callback = () => {}
}
// no callback passed
if (!callback || typeof callback !== 'function') callback = () => {}
// set defaults
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()
if (typeof x !== 'number') x = -1
if (typeof y !== 'number') y = -1
if (typeof positioningItem !== 'number') positioningItem = -1
// 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`)
}
// find which window to use
const wins = BrowserWindow.getAllWindows()
if (!wins || wins.indexOf(window) === -1) {
window = BrowserWindow.getFocusedWindow()
if (!window && wins && wins.length > 0) {
window = wins[0]
}
if (!window) {
throw new Error(`Cannot open Menu without a BrowserWindow present`)
}
}
this.popupAt(newWindow, newX, newY, newPosition, callback)
return { browserWindow: newWindow, x: newX, y: newY, position: newPosition }
this.popupAt(window, x, y, positioningItem, callback)
return { browserWindow: window, x, y, position: positioningItem }
}
Menu.prototype.closePopup = function (window) {