Build node-webkit style Menu API arounding the delegate-style Menu API.
This commit is contained in:
parent
faf7280d1f
commit
84e721188b
4 changed files with 38 additions and 12 deletions
1
atom.gyp
1
atom.gyp
|
@ -15,6 +15,7 @@
|
|||
'browser/api/lib/dialog.coffee',
|
||||
'browser/api/lib/ipc.coffee',
|
||||
'browser/api/lib/menu.coffee',
|
||||
'browser/api/lib/menu_item.coffee',
|
||||
'browser/atom/atom.coffee',
|
||||
'browser/atom/objects_registry.coffee',
|
||||
'browser/atom/rpc_server.coffee',
|
||||
|
|
|
@ -208,7 +208,7 @@ v8::Handle<v8::Value> Menu::InsertRadioItem(const v8::Arguments &args) {
|
|||
v8::Handle<v8::Value> Menu::InsertSeparator(const v8::Arguments &args) {
|
||||
UNWRAP_MEMNU_AND_CHECK;
|
||||
|
||||
if (!args[0]->IsNumber() || !args[1]->IsNumber() || !args[2]->IsString())
|
||||
if (!args[0]->IsNumber())
|
||||
return node::ThrowTypeError("Bad argument");
|
||||
|
||||
int index = args[0]->IntegerValue();
|
||||
|
|
|
@ -1,7 +1,9 @@
|
|||
EventEmitter = require('events').EventEmitter
|
||||
BrowserWindow = require 'browser_window'
|
||||
MenuItem = require 'menu_item'
|
||||
|
||||
bindings = process.atomBinding 'menu'
|
||||
|
||||
Menu = bindings.Menu
|
||||
Menu::__proto__ = EventEmitter.prototype
|
||||
|
||||
|
@ -11,23 +13,28 @@ Menu::popup = (window) ->
|
|||
|
||||
popup.call this, window
|
||||
|
||||
insertSubMenu = Menu::insertSubMenu
|
||||
Menu::insertSubMenu = (index, command_id, label, submenu) ->
|
||||
throw new TypeError('Invalid menu') unless submenu?.constructor is Menu
|
||||
Menu::insert = (pos, item) ->
|
||||
throw new TypeError('Invalid item') unless item?.constructor is MenuItem
|
||||
|
||||
@menus = [] unless Array.isArray @menus
|
||||
@menus.push submenu # prevent submenu from getting destroyed
|
||||
insertSubMenu.apply this, arguments
|
||||
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
|
||||
|
||||
Menu::appendItem = (args...) -> @insertItem -1, args...
|
||||
Menu::appendCheckItem = (args...) -> @insertCheckItem -1, args...
|
||||
Menu::appendRadioItem = (args...) -> @insertRadioItem -1, args...
|
||||
Menu::appendSeparator = (args...) -> @insertSeparator -1, args...
|
||||
Menu::appendSubMenu = (args...) -> @insertSubMenu -1, args...
|
||||
@setSublabel pos, item.sublabel if item.sublabel?
|
||||
|
||||
@items = {} unless @items?
|
||||
@items[item.commandId] = item
|
||||
|
||||
Menu::append = (item) ->
|
||||
@insert @getItemCount(), item
|
||||
|
||||
Menu.setApplicationMenu = (menu) ->
|
||||
throw new TypeError('Invalid menu') unless menu?.constructor is Menu
|
||||
bindings.setApplicationMenu menu
|
||||
|
||||
Menu.sendActionToFirstResponder = bindings.sendActionToFirstResponder
|
||||
|
||||
module.exports = Menu
|
||||
|
|
18
browser/api/lib/menu_item.coffee
Normal file
18
browser/api/lib/menu_item.coffee
Normal 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
|
Loading…
Reference in a new issue