electron/browser/api/lib/menu.coffee

60 lines
2 KiB
CoffeeScript
Raw Normal View History

2013-05-14 11:24:52 +00:00
EventEmitter = require('events').EventEmitter
BrowserWindow = require 'browser-window'
MenuItem = require 'menu-item'
2013-05-14 11:24:52 +00:00
bindings = process.atomBinding 'menu'
Menu = bindings.Menu
2013-05-14 11:24:52 +00:00
Menu::__proto__ = EventEmitter.prototype
popup = Menu::popup
Menu::popup = (window) ->
throw new TypeError('Invalid window') unless window?.constructor is BrowserWindow
2013-05-14 11:24:52 +00:00
popup.call this, window
Menu::append = (item) ->
@insert @getItemCount(), item
Menu::insert = (pos, item) ->
throw new TypeError('Invalid item') unless item?.constructor is MenuItem
switch item.type
when 'normal' then @insertItem pos, item.commandId, item.label
when 'checkbox' then @insertCheckItem pos, item.commandId, item.label
when 'radio' then @insertRadioItem pos, item.commandId, item.label, item.groupId
when 'separator' then @insertSeparator pos
when 'submenu' then @insertSubMenu pos, item.commandId, item.label, item.submenu
@setSublabel pos, item.sublabel if item.sublabel?
unless @items?
@items = {}
@delegate =
isCommandIdChecked: (commandId) => @items[commandId]?.checked
isCommandIdEnabled: (commandId) => @items[commandId]?.enabled
isCommandIdVisible: (commandId) => @items[commandId]?.visible
getAcceleratorForCommandId: (commandId) => @items[commandId]?.accelerator
executeCommand: (commandId) => @items[commandId]?.click()
@items[item.commandId] = item
Menu.setApplicationMenu = (menu) ->
throw new TypeError('Invalid menu') unless menu?.constructor is Menu
bindings.setApplicationMenu menu
Menu.sendActionToFirstResponder = bindings.sendActionToFirstResponder
Menu.buildFromTemplate = (template) ->
throw new TypeError('Invalid template for Menu') unless Array.isArray template
menu = new Menu
for item in template
throw new TypeError('Invalid template for MenuItem') unless typeof item is 'object'
item.submenu = Menu.buildFromTemplate item.submenu if item.submenu?
menu.append new MenuItem(item)
menu
2013-05-14 11:24:52 +00:00
module.exports = Menu