Merge pull request #10618 from electron/add-menuitembyid
[WIP] add getMenuItemById to Menu API
This commit is contained in:
commit
15b0878a17
3 changed files with 41 additions and 0 deletions
|
@ -89,6 +89,12 @@ Closes the context menu in the `browserWindow`.
|
|||
|
||||
Appends the `menuItem` to the menu.
|
||||
|
||||
#### `menu.getMenuItemById(id)`
|
||||
|
||||
* `id` String
|
||||
|
||||
Returns `MenuItem` the item with the specified `id`
|
||||
|
||||
#### `menu.insert(pos, menuItem)`
|
||||
|
||||
* `pos` Integer
|
||||
|
|
|
@ -189,6 +189,18 @@ Menu.prototype.closePopup = function (window) {
|
|||
}
|
||||
}
|
||||
|
||||
Menu.prototype.getMenuItemById = function (id) {
|
||||
const items = this.items
|
||||
|
||||
let found = items.find(item => item.id === id) || null
|
||||
for (let i = 0, length = items.length; !found && i < length; i++) {
|
||||
if (items[i].submenu) {
|
||||
found = items[i].submenu.getMenuItemById(id)
|
||||
}
|
||||
}
|
||||
return found
|
||||
}
|
||||
|
||||
Menu.prototype.append = function (item) {
|
||||
return this.insert(this.getItemCount(), item)
|
||||
}
|
||||
|
|
|
@ -195,6 +195,29 @@ describe('menu module', function () {
|
|||
})
|
||||
})
|
||||
|
||||
describe('Menu.getMenuItemById', function () {
|
||||
it('should return the item with the given id', function () {
|
||||
var menu = Menu.buildFromTemplate([
|
||||
{
|
||||
label: 'View',
|
||||
submenu: [
|
||||
{
|
||||
label: 'Enter Fullscreen',
|
||||
accelerator: 'Control+Command+F',
|
||||
id: 'fullScreen'
|
||||
},
|
||||
{
|
||||
label: 'Exit Fullscreen',
|
||||
accelerator: 'Control+Command+F'
|
||||
}
|
||||
]
|
||||
}
|
||||
])
|
||||
const fsc = menu.getMenuItemById('fullScreen')
|
||||
assert.equal(menu.items[0].submenu.items[0], fsc)
|
||||
})
|
||||
})
|
||||
|
||||
describe('Menu.insert', function () {
|
||||
it('should store item in @items by its index', function () {
|
||||
var menu = Menu.buildFromTemplate([
|
||||
|
|
Loading…
Reference in a new issue