A.8. Add a Native Menu

The NativeMenu class gives us the ability to integrate to the menu system that is native to the operating system. This creates an interface that is more in line with what users expect from a desktop application running in their computers' environment. We also have the ability to add keyboard shortcuts to access these menu items. To add a NativeMenu, you will first need to build up the NativeMenu and NativeMenuItems before adding them to the shell using the addItem() method of the NativeApplication.nativeApplication.menu object. Add the following code to the init() method of the WeatherAIR.mxml file. The code in Listing A-4 will create a menu named Navigation with two submenus, Preferences and Full Forecast. It also adds keyboard shortcuts for selecting the menu items. Finally, it adds an event listener listening for the SELECT event and calling the handleMenuClick() function. Copy the code from Listing A-4 to the init() method within the WeatherAIR.mxml file.

Example A-4. Code to build up NativeMenu
var nativeMenu:NativeMenu = new NativeMenu(); var menuItem:NativeMenuItem = new NativeMenuItem("Navigation"); var prefMenu:NativeMenuItem = new NativeMenuItem("Preferences"); var forecastMenu:NativeMenuItem = new NativeMenuItem("Full Forecast"); prefMenu.addEventListener(Event.SELECT,handleMenuClick); prefMenu.keyEquivalent = "P"; forecastMenu.addEventListener(Event.SELECT,handleMenuClick); forecastMenu.keyEquivalent = "F"; nativeMenu.addItem(prefMenu); nativeMenu.addItem(forecastMenu); ...

Get Beginning Adobe® AIR™: Building Applications for the Adobe Integrated Runtime now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.