Build node-webkit style Menu API arounding the delegate-style Menu API.

This commit is contained in:
Cheng Zhao 2013-05-16 19:34:23 +08:00
parent faf7280d1f
commit 84e721188b
4 changed files with 38 additions and 12 deletions

View file

@ -15,6 +15,7 @@
'browser/api/lib/dialog.coffee', 'browser/api/lib/dialog.coffee',
'browser/api/lib/ipc.coffee', 'browser/api/lib/ipc.coffee',
'browser/api/lib/menu.coffee', 'browser/api/lib/menu.coffee',
'browser/api/lib/menu_item.coffee',
'browser/atom/atom.coffee', 'browser/atom/atom.coffee',
'browser/atom/objects_registry.coffee', 'browser/atom/objects_registry.coffee',
'browser/atom/rpc_server.coffee', 'browser/atom/rpc_server.coffee',

View file

@ -208,7 +208,7 @@ v8::Handle<v8::Value> Menu::InsertRadioItem(const v8::Arguments &args) {
v8::Handle<v8::Value> Menu::InsertSeparator(const v8::Arguments &args) { v8::Handle<v8::Value> Menu::InsertSeparator(const v8::Arguments &args) {
UNWRAP_MEMNU_AND_CHECK; UNWRAP_MEMNU_AND_CHECK;
if (!args[0]->IsNumber() || !args[1]->IsNumber() || !args[2]->IsString()) if (!args[0]->IsNumber())
return node::ThrowTypeError("Bad argument"); return node::ThrowTypeError("Bad argument");
int index = args[0]->IntegerValue(); int index = args[0]->IntegerValue();

View file

@ -1,7 +1,9 @@
EventEmitter = require('events').EventEmitter EventEmitter = require('events').EventEmitter
BrowserWindow = require 'browser_window' BrowserWindow = require 'browser_window'
MenuItem = require 'menu_item'
bindings = process.atomBinding 'menu' bindings = process.atomBinding 'menu'
Menu = bindings.Menu Menu = bindings.Menu
Menu::__proto__ = EventEmitter.prototype Menu::__proto__ = EventEmitter.prototype
@ -11,23 +13,28 @@ Menu::popup = (window) ->
popup.call this, window popup.call this, window
insertSubMenu = Menu::insertSubMenu Menu::insert = (pos, item) ->
Menu::insertSubMenu = (index, command_id, label, submenu) -> throw new TypeError('Invalid item') unless item?.constructor is MenuItem
throw new TypeError('Invalid menu') unless submenu?.constructor is Menu
@menus = [] unless Array.isArray @menus switch item.type
@menus.push submenu # prevent submenu from getting destroyed when 'normal' then @insertItem pos, item.commandId, item.label
insertSubMenu.apply this, arguments 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
Menu::appendItem = (args...) -> @insertItem -1, args... @setSublabel pos, item.sublabel if item.sublabel?
Menu::appendCheckItem = (args...) -> @insertCheckItem -1, args...
Menu::appendRadioItem = (args...) -> @insertRadioItem -1, args... @items = {} unless @items?
Menu::appendSeparator = (args...) -> @insertSeparator -1, args... @items[item.commandId] = item
Menu::appendSubMenu = (args...) -> @insertSubMenu -1, args...
Menu::append = (item) ->
@insert @getItemCount(), item
Menu.setApplicationMenu = (menu) -> Menu.setApplicationMenu = (menu) ->
throw new TypeError('Invalid menu') unless menu?.constructor is Menu throw new TypeError('Invalid menu') unless menu?.constructor is Menu
bindings.setApplicationMenu menu bindings.setApplicationMenu menu
Menu.sendActionToFirstResponder = bindings.sendActionToFirstResponder Menu.sendActionToFirstResponder = bindings.sendActionToFirstResponder
module.exports = Menu module.exports = Menu

View file

@ -0,0 +1,18 @@
nextCommandId = 0
class MenuItem
@types = ['normal', 'separator', 'submenu', 'checkbox', 'radio']
constructor: (options) ->
{@type, @label, @sublabel, @click, @checked, @groupId, @submenu} = options
@type = @type ? 'normal'
@label = @label ? ''
@sublabel = @sublabel ? ''
throw new Error('Unknown menu type') if MenuItem.types.indexOf(@type) is -1
throw new Error('Invalid menu') if @type is 'submenu' and @submenu?.constructor.name isnt 'Menu'
@commandId = ++nextCommandId
module.exports = MenuItem