Home » AIR, Adobe

FLASH CS4/CS5 and Adobe AIR: Application and Windows Menu

23 August 2010 No Comment

When you write AIR desktop applications you can create menu bars very similar to the OS native menu.

In this sample we set a custom Window menu (on Microsoft os) or an application menu (using MAC os) in according with the installed OS.

To demostrate how it works I have included an Exit menu item and some basic actions on a movieclip positioned on the Stage.

Here the result on my Windows 7:
air-applicationmenu

air-applicationmenu2

air-applicationmenu3

Following the simple document class :

package com.fabiobiondi.training.airlevel1.core.menu 
{
	import flash.desktop.NativeApplication;
	import flash.display.MovieClip;
	import flash.display.NativeMenu;
	import flash.display.NativeMenuItem;
	import flash.display.NativeWindow;
	import flash.events.Event;
	import flash.events.MouseEvent;
 
	/**
	 * ...
	 * @author Fabio Biondi
	 */
	public class ApplicationAndWindowsMenuBase extends MovieClip
	{
 
		private var menuRoot:NativeMenu;
 
		public function ApplicationAndWindowsMenuBase() 
		{
			initMenu()			
		}
 
		/**
		 * Create Application (MAC) or Window (Microsoft) menu 
		 */
		private function initMenu():void
		{
			menuRoot = new NativeMenu();
			menuRoot.addItem(fileMenu());
			menuRoot.addItem(actionMenu());
			menuRoot.addItem(editMenu());
 
			// If we are on Mac OS X
			if (NativeApplication.supportsMenu) 
				NativeApplication.nativeApplication.menu = menuRoot
 
			// If we are on Windows
			if (NativeWindow.supportsMenu)
				stage.nativeWindow.menu = menuRoot;
 
		}
 
 
		/**
		 * Create the subMenu 'actionMenu' with its subItems
		 * @return a NaviteMenuItem
		 */
		private function actionMenu():NativeMenuItem
		{
			var actionMenu	: NativeMenuItem = new NativeMenuItem ("Actions");
			actionMenu.submenu = new NativeMenu();
 
			var rotateItem:NativeMenuItem = new NativeMenuItem ("Rotate 45°");
			rotateItem.addEventListener(Event.SELECT, onSelectItem)
			actionMenu.submenu.addItem(rotateItem)
 
			var separator:NativeMenuItem = new NativeMenuItem ("", true);
			actionMenu.submenu.addItem(separator)
 
			var scaleItem:NativeMenuItem = new NativeMenuItem ("Scale * 2");
			scaleItem.addEventListener(Event.SELECT, onSelectItem)
			actionMenu.submenu.addItem(scaleItem)
 
			return actionMenu;
		}
 
 
 
		/**
		 * Create the subMenu 'editMenu' with its subItems
		 * @return a NaviteMenuItem
		 */
		private function editMenu():NativeMenuItem
		{
			var editMenu	: NativeMenuItem = new NativeMenuItem ("Edit");
			editMenu.submenu = new NativeMenu();
 
			var removeItem:NativeMenuItem = new NativeMenuItem ("Remove");
			removeItem.addEventListener(Event.SELECT, onSelectItem)
			editMenu.submenu.addItem(removeItem)
 
			return editMenu;
		}
 
 
 
		/**
		 * Create the subMenu 'File' with the exit button
		 * @return a NaviteMenuItem
		 */
		private function fileMenu():NativeMenuItem
		{
			var fileMenu	: NativeMenuItem = new NativeMenuItem ("File");
			fileMenu.submenu = new NativeMenu();
 
			var exitItem:NativeMenuItem = new NativeMenuItem ("Exit");
			exitItem.addEventListener(Event.SELECT, onSelectItem)
			fileMenu.submenu.addItem(exitItem)
 
			return fileMenu;
		}
 
 
 
		/**
		 * Select Handler
		 * @param	e	Event
		 */
		private function onSelectItem(e:Event):void 
		{
			var item:NativeMenuItem = e.target as NativeMenuItem
 
			switch(item.label) {
 
				case "Rotate 45°":
					star_mc.rotation += 45;
					break;
 
				case "Scale * 2":
					star_mc.scaleX = star_mc.scaleY += 2;
					break;
 
				case "Remove":
					this.removeChild(star_mc)
					break;
 
				case "Exit":
					NativeApplication.nativeApplication.exit()
					break;
			}			
		}		
 
	}
}

To put in place this sample you need to create a new Adobe AIR 2 Flash file, setting its document class to
com.fabiobiondi.training.airlevel1.core.menu.ApplicationAndWindowsMenuBase
(but naturally you are free to change the package ; )

airproperties

airsettings

Furthermore, i have simply create a movieclip on stage with instance name: star_mc

workspace

Download source code (flash CS4)


This sample is part of the training course “Adobe AIR in Flash CS5″ available only in italian language on my website: http://www.fabiobiondi.com

Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.