Merge pull request #3715 from atom/do-not-edit-menu-template
Do not modify the specified menu template
This commit is contained in:
commit
8aee0f52e9
4 changed files with 15 additions and 4 deletions
|
@ -26,6 +26,8 @@ class MenuItem
|
||||||
|
|
||||||
{click, @selector, @type, @role, @label, @sublabel, @accelerator, @icon, @enabled, @visible, @checked, @submenu} = options
|
{click, @selector, @type, @role, @label, @sublabel, @accelerator, @icon, @enabled, @visible, @checked, @submenu} = options
|
||||||
|
|
||||||
|
if @submenu? and @submenu.constructor isnt Menu
|
||||||
|
@submenu = Menu.buildFromTemplate @submenu
|
||||||
@type = 'submenu' if not @type? and @submenu?
|
@type = 'submenu' if not @type? and @submenu?
|
||||||
throw new Error('Invalid submenu') if @type is 'submenu' and @submenu?.constructor isnt Menu
|
throw new Error('Invalid submenu') if @type is 'submenu' and @submenu?.constructor isnt Menu
|
||||||
|
|
||||||
|
|
|
@ -169,9 +169,8 @@ Menu.buildFromTemplate = (template) ->
|
||||||
for item in positionedTemplate
|
for item in positionedTemplate
|
||||||
throw new TypeError('Invalid template for MenuItem') unless typeof item is 'object'
|
throw new TypeError('Invalid template for MenuItem') unless typeof item is 'object'
|
||||||
|
|
||||||
item.submenu = Menu.buildFromTemplate item.submenu if item.submenu?
|
|
||||||
menuItem = new MenuItem(item)
|
menuItem = new MenuItem(item)
|
||||||
menuItem[key] = value for key, value of item when not menuItem[key]?
|
menuItem[key] ?= value for key, value of item
|
||||||
menu.append menuItem
|
menu.append menuItem
|
||||||
|
|
||||||
menu
|
menu
|
||||||
|
|
|
@ -26,7 +26,9 @@ Create a new `MenuItem` with the following method:
|
||||||
* `visible` Boolean
|
* `visible` Boolean
|
||||||
* `checked` Boolean
|
* `checked` Boolean
|
||||||
* `submenu` Menu - Should be specified for `submenu` type menu item, when
|
* `submenu` Menu - Should be specified for `submenu` type menu item, when
|
||||||
it's specified the `type: 'submenu'` can be omitted for the menu item
|
it's specified the `type: 'submenu'` can be omitted for the menu item.
|
||||||
|
If the value is not a `Menu` then it will be automatically converted to one
|
||||||
|
using `Menu.buildFromTemplate`.
|
||||||
* `id` String - Unique within a single menu. If defined then it can be used
|
* `id` String - Unique within a single menu. If defined then it can be used
|
||||||
as a reference to this item by the position attribute.
|
as a reference to this item by the position attribute.
|
||||||
* `position` String - This field allows fine-grained definition of the
|
* `position` String - This field allows fine-grained definition of the
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
assert = require 'assert'
|
assert = require 'assert'
|
||||||
|
|
||||||
{remote} = require 'electron'
|
{remote, ipcRenderer} = require 'electron'
|
||||||
{Menu, MenuItem} = remote.require 'electron'
|
{Menu, MenuItem} = remote.require 'electron'
|
||||||
|
|
||||||
describe 'menu module', ->
|
describe 'menu module', ->
|
||||||
|
@ -9,6 +9,14 @@ describe 'menu module', ->
|
||||||
menu = Menu.buildFromTemplate [label: 'text', extra: 'field']
|
menu = Menu.buildFromTemplate [label: 'text', extra: 'field']
|
||||||
assert.equal menu.items[0].extra, 'field'
|
assert.equal menu.items[0].extra, 'field'
|
||||||
|
|
||||||
|
it 'does not modify the specified template', ->
|
||||||
|
template = ipcRenderer.sendSync 'eval', """
|
||||||
|
var template = [{label: 'text', submenu: [{label: 'sub'}]}];
|
||||||
|
require('electron').Menu.buildFromTemplate(template);
|
||||||
|
template;
|
||||||
|
"""
|
||||||
|
assert.deepStrictEqual template, [label: 'text', submenu: [label: 'sub']]
|
||||||
|
|
||||||
describe 'Menu.buildFromTemplate should reorder based on item position specifiers', ->
|
describe 'Menu.buildFromTemplate should reorder based on item position specifiers', ->
|
||||||
it 'should position before existing item', ->
|
it 'should position before existing item', ->
|
||||||
menu = Menu.buildFromTemplate [
|
menu = Menu.buildFromTemplate [
|
||||||
|
|
Loading…
Reference in a new issue