maccman/macgap — pull request #116 by rawcreative (2014-05-17 00:02:15+00:00)
Title: Menu API Enhancements

Adds additional functionality and flexibility to the menu API as well as fixing #110

## Changes
### addItem()
This method now allows for adding root level menu items as well as specifying the menu position, with the additional 'index' argument.

//Syntax: addItem(Label, Shorcut Keys, Callback, Index);
//Add a root level menu
macgap.menu.addItem('Foo', 'cmd+g', function() { alert('yay'); }, 1);

//Add an item to existing menu
macgap.menu.getItem('File').submenu().addItem('Foo', 'cmd+g', function() { alert('yay'); }, 1);

The index parameter is optional for both root level and submenus, excluding it will add the menu item to the end of the menu.

## Additions
### removeMenu(ID)
Added removeMenu shortcut which now allows:

macgap.menu.removeMenu('File');

instead of the more verbose

macgap.menu.getItem('File').remove().

MenuItemProxy now uses this method to remove items as well.

### enable() & disable()
Added enable() and disable() methods to MenuItemProxy which allows for enabling and disabling menu items via js

// Disable the Save menu item
macgap.menu.getItem('File').submenu().getItem('Save').disable();

// Enable the Save menu item
macgap.menu.getItem('File').submenu().getItem('Save').enable();

I debated removing the need to use .submenu() and instead return the submenu automatically when calling getItem() but decided to keep it in for now as it would remove the ability to change the title/accelerator keys and callbacks for the root level menu items unless additional methods were implemented to deal with those.

Ideally I'd like to make creating/editing menus much easier by passing a simply JS object with the desired menu configuration but doing so would require writing a ton of C code to parse the object and it just doesn't seem to be worthwhile to do for the time being. It can easily be done with the 10.9 JavascriptCore api so it may be something to look into for the MacGapNode project.