- Defining the Menus in XM
- Invoking the MenuBuilder Framework
- Adding Menu Features
Invoking the MenuBuilder Framework
After you've defined your XML file, you can use it and the MenuBuilder framework in your AIR application. First, copy the AIRMenuBuilder.js file, found in the frameworks directory of the downloaded AIR SDK, to your project's directory. (I normally put this kind of file within a js or similar subfolder.)
To use the framework, you must include it in your application's primary HTML page, just as you would include any other external JavaScript file:
<script type="text/javascript" src="js/AIRMenuBuilder.js"></script>
The next step is to create a NativeMenu object from the XML data:
var windowMenu = air.ui.Menu.createFromXML('assets/menus.xml');
This line calls the createFromXML() method of the Menu class. It's fed one value: the name of the XML file to be used (here, the assumption is that you've saved the file as menus.xml in the project's assets directory).
The final step is to create the menu from the NativeMenu object. The exact method you call depends on the type of menu you're creating. To create an application menu (for Mac OS X) or window menu (for Windows), invoke setAsMenu():
air.ui.Menu.setAsMenu(windowMenu);
This method is fed the NativeMenu object you've already created. On Mac OS X, you can provide a second argument (the value true) to indicate that the default menus should be overridden. If you don't do this, the new menus will be added between the application's natural Edit and Window menus.
Note that the setAsMenu() method should be called within the HEAD of the HTML document, and outside of any function. This design ensures that the menu is created before the operating system attempts to create the menu automatically. Conversely, if you wanted to add a contextual menu to a page by associating it with a DOM element, you would call setAsContextMenu() after that DOM element has been created (for example, just before the closing BODY tag).