本机菜单示例:窗口和应用程序菜单 (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 事件。

package { 
    import flash.display.NativeMenu; 
    import flash.display.NativeMenuItem; 
    import flash.display.NativeWindow; 
    import flash.display.Sprite; 
    import flash.events.Event; 
    import flash.filesystem.File; 
    import flash.desktop.NativeApplication; 
 
    public class MenuExample extends Sprite 
    { 
        private var recentDocuments:Array =  
            new Array(new File("app-storage:/GreatGatsby.pdf"),  
                     new File("app-storage:/WarAndPeace.pdf"),  
                     new File("app-storage:/Iliad.pdf")); 
                                             
        public function MenuExample() 
        { 
            var fileMenu:NativeMenuItem; 
            var editMenu:NativeMenuItem; 
             
            if (NativeWindow.supportsMenu){ 
                stage.nativeWindow.menu = new NativeMenu(); 
                stage.nativeWindow.menu.addEventListener(Event.SELECT, selectCommandMenu); 
                fileMenu = stage.nativeWindow.menu.addItem(new NativeMenuItem("File")); 
                fileMenu.submenu = createFileMenu(); 
                editMenu = stage.nativeWindow.menu.addItem(new NativeMenuItem("Edit")); 
                editMenu.submenu = createEditMenu(); 
            } 
             
            if (NativeApplication.supportsMenu){ 
                NativeApplication.nativeApplication.menu.addEventListener(Event.SELECT, selectCommandMenu); 
                fileMenu = NativeApplication.nativeApplication.menu.addItem(new NativeMenuItem("File")); 
                fileMenu.submenu = createFileMenu(); 
                editMenu = NativeApplication.nativeApplication.menu.addItem(new NativeMenuItem("Edit")); 
                editMenu.submenu = createEditMenu(); 
            } 
        } 
                 
        public function createFileMenu():NativeMenu { 
            var fileMenu:NativeMenu = new NativeMenu(); 
            fileMenu.addEventListener(Event.SELECT, selectCommandMenu); 
             
            var newCommand:NativeMenuItem = fileMenu.addItem(new NativeMenuItem("New")); 
            newCommand.addEventListener(Event.SELECT, selectCommand); 
            var saveCommand:NativeMenuItem = fileMenu.addItem(new NativeMenuItem("Save")); 
            saveCommand.addEventListener(Event.SELECT, selectCommand); 
            var openRecentMenu:NativeMenuItem =  
                    fileMenu.addItem(new NativeMenuItem("Open Recent"));  
            openRecentMenu.submenu = new NativeMenu(); 
            openRecentMenu.submenu.addEventListener(Event.DISPLAYING, 
                                            updateRecentDocumentMenu); 
            openRecentMenu.submenu.addEventListener(Event.SELECT, selectCommandMenu); 
             
            return fileMenu; 
        } 
         
        public function createEditMenu():NativeMenu { 
            var editMenu:NativeMenu = new NativeMenu(); 
            editMenu.addEventListener(Event.SELECT, selectCommandMenu); 
             
            var copyCommand:NativeMenuItem = editMenu.addItem(new NativeMenuItem("Copy")); 
            copyCommand.addEventListener(Event.SELECT, selectCommand); 
            copyCommand.keyEquivalent = "c"; 
            var pasteCommand:NativeMenuItem =  
                    editMenu.addItem(new NativeMenuItem("Paste")); 
            pasteCommand.addEventListener(Event.SELECT, selectCommand); 
            pasteCommand.keyEquivalent = "v"; 
            editMenu.addItem(new NativeMenuItem("", true)); 
            var preferencesCommand:NativeMenuItem =  
                    editMenu.addItem(new NativeMenuItem("Preferences")); 
            preferencesCommand.addEventListener(Event.SELECT, selectCommand); 
             
            return editMenu; 
        } 
         
        private function updateRecentDocumentMenu(event:Event):void { 
            trace("Updating recent document menu."); 
            var docMenu:NativeMenu = NativeMenu(event.target); 
             
            for each (var item:NativeMenuItem in docMenu.items) { 
                docMenu.removeItem(item); 
            } 
             
            for each (var file:File in recentDocuments) { 
                var menuItem:NativeMenuItem =  
                        docMenu.addItem(new NativeMenuItem(file.name)); 
                menuItem.data = file; 
                menuItem.addEventListener(Event.SELECT, selectRecentDocument); 
            } 
        } 
         
        private function selectRecentDocument(event:Event):void { 
            trace("Selected recent document: " + event.target.data.name); 
        } 
         
        private function selectCommand(event:Event):void { 
            trace("Selected command: " + event.target.label); 
        } 
 
        private function selectCommandMenu(event:Event):void { 
            if (event.currentTarget.parent != null) { 
                var menuItem:NativeMenuItem = 
                        findItemForMenu(NativeMenu(event.currentTarget)); 
                if (menuItem != null) { 
                    trace("Select event for \"" +  
                            event.target.label +  
                            "\" command handled by menu: " +  
                            menuItem.label); 
                } 
            } else { 
                trace("Select event for \"" +  
                        event.target.label +  
                        "\" command handled by root menu."); 
            } 
        } 
         
        private function findItemForMenu(menu:NativeMenu):NativeMenuItem { 
            for each (var item:NativeMenuItem in menu.parent.items) { 
                if (item != null) { 
                    if (item.submenu == menu) { 
                        return item; 
                    } 
                } 
            } 
            return null; 
        } 
    } 
}