BrowserWindow = require 'browser-window'
+v8Util = process.atomBinding 'v8_util'
nextCommandId = 0
+overrideProperty = (item, name, defaultValue) ->
+ item[name] ?= defaultValue
+
+ return unless process.platform is 'win32'
+ v8Util.setHiddenValue item, name, item[name]
+ Object.defineProperty item, name,
+ enumerable: true
+ get: -> v8Util.getHiddenValue item, name
+ set: (val) ->
+ v8Util.setHiddenValue item, name, val
+ item.menu?._updateStates()
+
+overrideReadOnlyProperty = (item, name, defaultValue) ->
+ item[name] ?= defaultValue
+ Object.defineProperty item, name,
+ enumerable: true
+ writable: false
+ value: item[name]
+
class MenuItem
@types = ['normal', 'separator', 'submenu', 'checkbox', 'radio']
@type = 'submenu' if not @type? and @submenu?
throw new Error('Invalid submenu') if @type is 'submenu' and @submenu?.constructor isnt Menu
- @type = @type ? 'normal'
- @label = @label ? ''
- @sublabel = @sublabel ? ''
- @accelerator = @accelerator ? null
- @enabled = @enabled ? true
- @visible = @visible ? true
- @checked = @checked ? false
- @submenu = @submenu ? null
+ overrideReadOnlyProperty this, 'type', 'normal'
+ overrideReadOnlyProperty this, 'accelerator', null
+ overrideReadOnlyProperty this, 'submenu', null
+ overrideProperty this, 'label', ''
+ overrideProperty this, 'sublabel', ''
+ overrideProperty this, 'enabled', true
+ overrideProperty this, 'visible', true
+ overrideProperty this, 'checked', false
throw new Error("Unknown menu type #{@type}") if MenuItem.types.indexOf(@type) is -1
switch item.type
when 'normal' then @insertItem pos, item.commandId, item.label
- when 'checkbox'
- # Update states when clicked on Windows.
- if process.platform is 'win32'
- v8Util.setHiddenValue item, 'checked', item.checked
- Object.defineProperty item, 'checked',
- enumerable: true
- get: -> v8Util.getHiddenValue item, 'checked'
- set: (val) =>
- v8Util.setHiddenValue item, 'checked', val
- @_updateStates() if process.platform is 'win32'
-
- @insertCheckItem pos, item.commandId, item.label
+ when 'checkbox' then @insertCheckItem pos, item.commandId, item.label
when 'separator' then @insertSeparator pos
when 'submenu' then @insertSubMenu pos, item.commandId, item.label, item.submenu
when 'radio'
@groupsMap[item.groupId].push item
# Setting a radio menu item should flip other items in the group.
- v8Util.setHiddenValue item, 'checked', item.checked
Object.defineProperty item, 'checked',
enumerable: true
get: -> v8Util.getHiddenValue item, 'checked'
@setSublabel pos, item.sublabel if item.sublabel?
+ # Make menu accessable to items.
+ item.menu = this
+
# Remember the items.
@items.splice pos, 0, item
@commandsMap[item.commandId] = item