win: Setting enabled/visiable should also update menu.

This commit is contained in:
Cheng Zhao 2014-05-26 12:00:20 +08:00
parent dfa1ae1c20
commit d38ffea4a3
2 changed files with 32 additions and 21 deletions

View file

@ -1,7 +1,27 @@
BrowserWindow = require 'browser-window' BrowserWindow = require 'browser-window'
v8Util = process.atomBinding 'v8_util'
nextCommandId = 0 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 class MenuItem
@types = ['normal', 'separator', 'submenu', 'checkbox', 'radio'] @types = ['normal', 'separator', 'submenu', 'checkbox', 'radio']
@ -13,14 +33,14 @@ class MenuItem
@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
@type = @type ? 'normal' overrideReadOnlyProperty this, 'type', 'normal'
@label = @label ? '' overrideReadOnlyProperty this, 'accelerator', null
@sublabel = @sublabel ? '' overrideReadOnlyProperty this, 'submenu', null
@accelerator = @accelerator ? null overrideProperty this, 'label', ''
@enabled = @enabled ? true overrideProperty this, 'sublabel', ''
@visible = @visible ? true overrideProperty this, 'enabled', true
@checked = @checked ? false overrideProperty this, 'visible', true
@submenu = @submenu ? null overrideProperty this, 'checked', false
throw new Error("Unknown menu type #{@type}") if MenuItem.types.indexOf(@type) is -1 throw new Error("Unknown menu type #{@type}") if MenuItem.types.indexOf(@type) is -1

View file

@ -64,18 +64,7 @@ Menu::insert = (pos, item) ->
switch item.type switch item.type
when 'normal' then @insertItem pos, item.commandId, item.label when 'normal' then @insertItem pos, item.commandId, item.label
when 'checkbox' when 'checkbox' then @insertCheckItem pos, item.commandId, item.label
# 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 'separator' then @insertSeparator pos when 'separator' then @insertSeparator pos
when 'submenu' then @insertSubMenu pos, item.commandId, item.label, item.submenu when 'submenu' then @insertSubMenu pos, item.commandId, item.label, item.submenu
when 'radio' when 'radio'
@ -85,7 +74,6 @@ Menu::insert = (pos, item) ->
@groupsMap[item.groupId].push item @groupsMap[item.groupId].push item
# Setting a radio menu item should flip other items in the group. # Setting a radio menu item should flip other items in the group.
v8Util.setHiddenValue item, 'checked', item.checked
Object.defineProperty item, 'checked', Object.defineProperty item, 'checked',
enumerable: true enumerable: true
get: -> v8Util.getHiddenValue item, 'checked' get: -> v8Util.getHiddenValue item, 'checked'
@ -101,6 +89,9 @@ Menu::insert = (pos, item) ->
@setSublabel pos, item.sublabel if item.sublabel? @setSublabel pos, item.sublabel if item.sublabel?
# Make menu accessable to items.
item.menu = this
# Remember the items. # Remember the items.
@items.splice pos, 0, item @items.splice pos, 0, item
@commandsMap[item.commandId] = item @commandsMap[item.commandId] = item