本机菜单示例:窗口和应用程序菜单 (AIR)

Adobe AIR 1.0 和更高版本

以下示例创建在 本机菜单结构 (AIR) 中显示的菜单。

此菜单在设计上可同时用于 Windows(仅支持窗口菜单)和 Mac OS X(仅支持应用程序菜单)。为进行区分,MenuExample 类构造函数将检查 NativeWindow 和 NativeApplication 类的静态 supportsMenu 属性。如果 NativeWindow.supportsMenu true ,则该构造函数将为窗口创建 NativeMenu 对象,然后创建和添加“File”(文件)和“Edit”(编辑)子菜单。如果 NativeApplication.supportsMenu true ,则该构造函数将创建“File”(文件)和“Edit”(编辑)菜单,并将它们添加到 Mac OS X 操作系统所提供的现有菜单中。

本示例还将说明菜单事件的处理过程。 select 事件在项目级别以及菜单级别进行处理。从包含所选项的菜单到根菜单的菜单链中的每个菜单都将响应 select 事件。 displaying 事件与“Open Recent”(最近打开的项目)菜单一起使用。在打开菜单之前,将以最新的 Documents 数组(在此示例中实际上不发生更改)刷新菜单中的项目。尽管此示例中不显示,但还可以在个别项目上侦听 displaying 事件。

<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>