原生選單範例:視窗與應用程式選單 (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」選單使用。就在選單開啟之前,選單中的項目都會根據 recentDocuments 陣列重新整理 (在此範例中,這個陣列實際上並不會變更)。您也可以對個別項目偵聽 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; 
        } 
    } 
}