electron/atom/browser/api/lib/menu-item.coffee
Cheng Zhao 04fbec5120 Generate menu group id automatically.
In GTK+ radio menu items are managed automatically, so group id won't
have any effect there, in the meanwhile we need to maintain the same
behavior on all platforms, so we have to generate group id instead of
letting users specifying it.
2014-05-25 12:37:00 +08:00

34 lines
1 KiB
CoffeeScript

BrowserWindow = require 'browser-window'
nextCommandId = 0
class MenuItem
@types = ['normal', 'separator', 'submenu', 'checkbox', 'radio']
constructor: (options) ->
Menu = require 'menu'
{click, @selector, @type, @label, @sublabel, @accelerator, @enabled, @visible, @checked, @submenu} = options
@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
throw new Error('Unknown menu type') if MenuItem.types.indexOf(@type) is -1
@commandId = ++nextCommandId
@click = =>
if typeof click is 'function'
click this, BrowserWindow.getFocusedWindow()
else if typeof @selector is 'string'
Menu.sendActionToFirstResponder @selector
module.exports = MenuItem