Make Menu API accept TopLevelWindow

This commit is contained in:
Cheng Zhao 2018-04-17 17:14:49 +09:00
parent 1340b17424
commit 7473b612c5
10 changed files with 41 additions and 40 deletions

View file

@ -1,6 +1,6 @@
'use strict'
const {BrowserWindow, MenuItem, webContents} = require('electron')
const {TopLevelWindow, MenuItem, webContents} = require('electron')
const EventEmitter = require('events').EventEmitter
const v8Util = process.atomBinding('v8_util')
const bindings = process.atomBinding('menu')
@ -26,7 +26,7 @@ const delegate = {
executeCommand: (menu, event, id) => {
const command = menu.commandsMap[id]
if (!command) return
command.click(event, BrowserWindow.getFocusedWindow(), webContents.getFocusedWebContents())
command.click(event, TopLevelWindow.getFocusedWindow(), webContents.getFocusedWebContents())
},
menuWillShow: (menu) => {
// Ensure radio groups have at least one menu item seleted
@ -61,14 +61,14 @@ Menu.prototype.popup = function (options) {
if (typeof positioningItem !== 'number') positioningItem = -1
// find which window to use
const wins = BrowserWindow.getAllWindows()
const wins = TopLevelWindow.getAllWindows()
if (!wins || wins.indexOf(window) === -1) {
window = BrowserWindow.getFocusedWindow()
window = TopLevelWindow.getFocusedWindow()
if (!window && wins && wins.length > 0) {
window = wins[0]
}
if (!window) {
throw new Error(`Cannot open Menu without a BrowserWindow present`)
throw new Error(`Cannot open Menu without a TopLevelWindow present`)
}
}
@ -77,7 +77,7 @@ Menu.prototype.popup = function (options) {
}
Menu.prototype.closePopup = function (window) {
if (window && window.constructor !== BrowserWindow) {
if (window instanceof TopLevelWindow) {
this.closePopupAt(window.id)
} else {
// Passing -1 (invalid) would make closePopupAt close the all menu runners
@ -148,7 +148,7 @@ Menu.setApplicationMenu = function (menu) {
menu._callMenuWillShow()
bindings.setApplicationMenu(menu)
} else {
const windows = BrowserWindow.getAllWindows()
const windows = TopLevelWindow.getAllWindows()
return windows.map(w => w.setMenu(menu))
}
}