| Adobe AIR 1.1 |
|
|
AIR menu basicsThe native menu classes allow you to access the native menu features of the operating system on which your application is running. NativeMenu objects can be used for application menus (available on Mac OS X), window menus (available on Windows), context menus, and pop-up menus. AIR menu classesThe Adobe® AIR™ Menu classes include:
Menu varietiesAIR supports the following types of menus:
Default menusThe following default menus are provided by the operating system or a built-in AIR class:
Menu structureMenus are hierarchical in nature. NativeMenu objects contain child NativeMenuItem objects. NativeMenuItem objects that represent submenus, in turn, can contain NativeMenu objects. The top- or root-level menu object in the structure represents the menu bar for application and window menus. (Context, icon, and pop-up menus don’t have a menu bar). The following diagram illustrates the structure of a typical menu. The root menu represents the menu bar and contains two menu items referencing a File submenu and an Edit submenu. The File submenu in this structure contains two command items and an item that references an Open Recent Menu submenu, which, itself, contains three items. The Edit submenu contains three commands and a separator. ![]() Defining a submenu requires both a NativeMenu and a NativeMenuItem object. The NativeMenuItem object defines the label displayed in the parent menu and allows the user to open the submenu. The NativeMenu object serves as a container for items in the submenu. The NativeMenuItem object references the NativeMenu object through the NativeMenuItem submenu property. To view a code example that creates this menu see Example: Window and application menu. Menu eventsNativeMenu and NativeMenuItem objects both dispatch displaying and select events:
Key equivalents for menu commandsYou can assign a key equivalent (sometimes called an accelerator) to a menu command. The menu item dispatches a select event to any registered listeners when the key, or key combination is pressed. The menu containing the item must be part of the menu of the application or the active window for the command to be invoked. Key equivalents have two parts, a string representing the primary key and an array of modifier keys that must also be pressed. To assign the primary key, set the menu item keyEquivalent property to the single character string for that key. If you use an uppercase letter, the shift key is added to the modifier array automatically. On Mac OS X, the default modifier is the command key (Keyboard.COMMAND). On Windows, it is the control key (Keyboard.CONTROL). These default keys are automatically added to the modifier array. To assign different modifier keys, assign a new array containing the desired key codes to the keyEquivalentModifiers property. The default array is overwritten. Whether you use the default modifiers or assign your own modifier array, the shift key is added if the string you assign to the keyEquivalent property is an uppercase letter. Constants for the key codes to use for the modifier keys are defined in the Keyboard class. The assigned key equivalent string is automatically displayed beside the menu item name. The format depends on the user’s operating system and system preferences. Note: If you assign the Keyboard.COMMAND value
to a key modifier array on the Windows operating system, no key
equivalent is displayed in the menu. However, the control key must
be used to activate the menu command.
The following example assigns Ctrl+Shift+G as the key equivalent for a menu item: var item = new air.NativeMenuItem("Ungroup");
item.keyEquivalent = "G";
This example assigns Ctrl+Shift+G as the key equivalent by setting the modifier array directly: var item = new air.NativeMenuItem("Ungroup");
item.keyEquivalent = "G";
item.keyEquivalentModifiers = [air.Keyboard.CONTROL];
Note: Key equivalents are only triggered for application
and window menus. If you add a key equivalent to a context or pop-up
menu, the key equivalent is displayed in the menu label, but the
associated menu command is never invoked.
MnemonicsMnemonics are part of the operating system keyboard interface to menus. Both Mac OS X and Windows allow users to open menus and select commands with the keyboard, but there are subtle differences. On Mac OS X, the user types the first letter or two of the menu or command and then types return. On Windows, only a single letter is significant. By default, the significant letter is the first character in the label, but if you assign a mnemonic to the menu item, then the significant character becomes the designated letter. If two items in a menu have the same significant character (whether or not a mnemonic has been assigned), then the user’s keyboard interaction with the menu changes slightly. Instead of pressing a single letter to select the menu or command, the user must press the letter as many times as necessary to highlight the desired item and then press the enter key to complete the selection. To maintain a consistent behavior, it is advisable to assign a unique mnemonic to each item in a menu for window menus. Specify the mnemonic character as an index into the label string. The index of the first character in a label is 0. Thus, to use “r” as the mnemonic for a menu item labeled, “Format,” you would set the mnemonicIndex property equal to 2. var item = new air.NativeMenuItem("Format");
item.mnemonicIndex = 2;
Menu item stateMenu items have the two state properties, checked and enabled:
Attaching an object to a menu itemThe data property of the NativeMenuItem class allows you to reference an arbitrary object in each item. For example, in an “Open Recent” menu, you could assign the File object for each document to each menu item. var file = air.File.applicationStorageDirectory.resolvePath("GreatGatsby.pdf")
var menuItem = docMenu.addItem(new air.NativeMenuItem(file.name));
menuItem.data = file;
|