Native menu example: Window and application menu (AIR)

Adobe AIR 1.0 and later

The following example creates the menu shown in Native menu structure (AIR) .

The menu is designed to work both on Windows, for which only window menus are supported, and on Mac OS X, for which only application menus are supported. To make the distinction, the MenuExample class constructor checks the static supportsMenu properties of the NativeWindow and NativeApplication classes. If NativeWindow.supportsMenu is true , then the constructor creates a NativeMenu object for the window and then creates and adds the File and Edit submenus. If NativeApplication.supportsMenu is true , then the constructor creates and adds the File and Edit menus to the existing menu provided by the Mac OS X operating system.

The example also illustrates menu event handling. The select event is handled at the item level and also at the menu level. Each menu in the chain from the menu containing the selected item to the root menu responds to the select event. The displaying event is used with the “Open Recent” menu. Just before the menu is opened, the items in the menu are refreshed from the recent Documents array (which doesn’t actually change in this example). Although not shown in this example, you can also listen for displaying events on individual items.

<html> 
<head> 
<script src="AIRAliases.js" type="text/javascript"></script> 
<script type="text/javascript"> 
var application = air.NativeApplication.nativeApplication; 
var recentDocuments =  
    new Array(new air.File("app-storage:/GreatGatsby.pdf"),  
             new air.File("app-storage:/WarAndPeace.pdf"),  
             new air.File("app-storage:/Iliad.pdf")); 
 
function MenuExample(){ 
    var fileMenu; 
    var editMenu; 
     
    if (air.NativeWindow.supportsMenu &&  
         nativeWindow.systemChrome != air.NativeWindowSystemChrome.NONE) { 
        nativeWindow.menu = new air.NativeMenu(); 
        nativeWindow.menu.addEventListener(air.Event.SELECT, selectCommandMenu); 
        fileMenu = nativeWindow.menu.addItem(new air.NativeMenuItem("File")); 
        fileMenu.submenu = createFileMenu(); 
     
        editMenu = nativeWindow.menu.addItem(new air.NativeMenuItem("Edit")); 
        editMenu.submenu = createEditMenu(); 
    } 
     
    if (air.NativeApplication.supportsMenu) { 
        application.menu.addEventListener(air.Event.SELECT, selectCommandMenu); 
        fileMenu = application.menu.addItem(new air.NativeMenuItem("File")); 
        fileMenu.submenu = createFileMenu(); 
        editMenu = application.menu.addItem(new air.NativeMenuItem("Edit")); 
        editMenu.submenu = createEditMenu(); 
    } 
} 
         
function createFileMenu() { 
    var fileMenu = new air.NativeMenu(); 
    fileMenu.addEventListener(air.Event.SELECT,selectCommandMenu); 
     
    var newCommand = fileMenu.addItem(new air.NativeMenuItem("New")); 
    newCommand.addEventListener(air.Event.SELECT, selectCommand); 
    var saveCommand = fileMenu.addItem(new air.NativeMenuItem("Save")); 
    saveCommand.addEventListener(air.Event.SELECT, selectCommand); 
    var openFile = fileMenu.addItem(new air.NativeMenuItem("Open Recent"));  
    openFile.submenu = new air.NativeMenu(); 
    openFile.submenu.addEventListener(air.Event.DISPLAYING, updateRecentDocumentMenu); 
    openFile.submenu.addEventListener(air.Event.SELECT, selectCommandMenu); 
     
    return fileMenu; 
} 
 
function createEditMenu() { 
    var editMenu = new air.NativeMenu(); 
    editMenu.addEventListener(air.Event.SELECT,selectCommandMenu); 
     
    var copyCommand = editMenu.addItem(new air.NativeMenuItem("Copy")); 
    copyCommand.addEventListener(air.Event.SELECT,selectCommand); 
    copyCommand.keyEquivalent = "c"; 
    var pasteCommand = editMenu.addItem(new air.NativeMenuItem("Paste")); 
    pasteCommand.addEventListener(air.Event.SELECT, selectCommand); 
    pasteCommand.keyEquivalent = "v"; 
    editMenu.addItem(new air.NativeMenuItem("", true)); 
    var preferencesCommand = editMenu.addItem(new air.NativeMenuItem("Preferences")); 
    preferencesCommand.addEventListener(air.Event.SELECT,selectCommand); 
     
    return editMenu; 
} 
 
function updateRecentDocumentMenu(event) { 
    air.trace("Updating recent document menu."); 
    var docMenu = air.NativeMenu(event.target); 
     
    for (var i = docMenu.numItems - 1; i >= 0; i--) { 
        docMenu.removeItemAt(i); 
    } 
     
    for (var file in recentDocuments) { 
        var menuItem =  
            docMenu.addItem(new air.NativeMenuItem(recentDocuments[file].name)); 
        menuItem.data = recentDocuments[file]; 
        menuItem.addEventListener(air.Event.SELECT, selectRecentDocument); 
    } 
} 
 
function selectRecentDocument(event) { 
    air.trace("Selected recent document: " + event.target.data.name); 
} 
 
function selectCommand(event) { 
    air.trace("Selected command: " + event.target.label); 
} 
 
function selectCommandMenu(event) { 
    if (event.currentTarget.parent != null) { 
        var menuItem = findItemForMenu(event.currentTarget); 
        if(menuItem != null){ 
            air.trace("Select event for \"" + event.target.label +  
            "\" command handled by menu: " + menuItem.label); 
        } 
    } else { 
        air.trace("Select event for \"" + event.target.label +  
                "\" command handled by root menu."); 
    } 
} 
 
function findItemForMenu(menu){ 
    for (var item in menu.parent.items) { 
        if (item != null) { 
            if (item.submenu == menu) { 
                return item; 
            } 
        } 
    } 
    return null; 
} 
</script> 
<title>AIR menus</title> 
</head> 
<body onload="MenuExample()"></body> 
</html> 

// Ethnio survey code removed