This topic describes how to create the various types of native menu supported by AIR.
To create a new NativeMenu object to serve as the root of the menu, use the NativeMenu constructor:
var root:NativeMenu = new NativeMenu();
For application and window menus, the root menu represents the menu bar and should only contain items that open submenus. Context menu and pop-up menus do not have a menu bar, so the root menu can contain commands and separator lines as well as submenus.
After the menu is created, you can add menu items. Items appear in the menu in the order in which they are added, unless you add the items at a specific index using the addItemAt() method of a menu object.
To assign the menu as an application, window, icon, or context menu, or display it as a pop-up menu as shown in the following sections:
NativeApplication.nativeApplication.menu = root;
nativeWindowObject.menu = root;
Setting a context menu on an interactive object
interactiveObject.contextMenu = root;
DockIcon(NativeApplication.nativeApplication.icon).menu = root;
Setting a system tray icon menu
SystemTrayIcon(NativeApplication.nativeApplication.icon).menu = root;
root.display(stage, x, y);
To create a submenu, you add a NativeMenuItem object to the parent menu and then assign the NativeMenu object defining the submenu to the item's submenu property. AIR provides two ways to create submenu items and their associated menu object:
You can create a new menu item and its related menu object in one step with the addSubmenu() method:
var editMenuItem:NativeMenuItem = root.addSubmenu(new NativeMenu(), "Edit");
You can also create the menu item and assign the menu object to its submenu property separately:
var editMenuItem:NativeMenuItem = root.addItem("Edit", false);
editMenuItem.submenu = new NativeMenu();
To create a menu command, add a NativeMenuItem object to a menu and add an event listener referencing the function implementing the menu command:
var copy:NativeMenuItem = new NativeMenuItem("Copy", false);
copy.addEventListener(Event.SELECT, onCopyCommand);
editMenu.addItem(copy);
You can listen for the select event on the command item itself (as shown in the example), or you can listen for the select event on a parent menu object.
To create a separator line, create a NativeMenuItem, setting the isSeparator parameter to true in the constructor. Then add the separator item to the menu in the correct location:
var separatorA:NativeMenuItem = new NativeMenuItem("A", true);
editMenu.addItem(separatorA);
Note that the label specified for the separator, if any, will not be displayed.