Merge pull request #11968 from electron/refactor-menu-popup
Refactor menu.popup
This commit is contained in:
commit
2a97e48465
6 changed files with 47 additions and 95 deletions
|
@ -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) {
|
||||
|
|
|
@ -299,7 +299,12 @@ WebContents.prototype._init = function () {
|
|||
this.on('pepper-context-menu', function (event, params, callback) {
|
||||
// Access Menu via electron.Menu to prevent circular require.
|
||||
const menu = electron.Menu.buildFromTemplate(params.menu)
|
||||
menu.popup(event.sender.getOwnerBrowserWindow(), params.x, params.y, callback)
|
||||
menu.popup({
|
||||
window: event.sender.getOwnerBrowserWindow(),
|
||||
x: params.x,
|
||||
y: params.y,
|
||||
callback
|
||||
})
|
||||
})
|
||||
|
||||
// The devtools requests the webContents to reload.
|
||||
|
|
|
@ -63,7 +63,7 @@ const createMenu = function (x, y, items) {
|
|||
|
||||
// The menu is expected to show asynchronously.
|
||||
setTimeout(function () {
|
||||
menu.popup(remote.getCurrentWindow())
|
||||
menu.popup({window: remote.getCurrentWindow()})
|
||||
})
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue