Saturday, April 9, 2011

Working with menus in AIR: Part 2

In the last tutorial we've discussed how to create menus for AIR applications in Windows. Today we will learn how to make it also work on Mac OS.

The trick is, that unlike Windows, Mac OS doesn't have window menus, but it has application menus instead. This means that if we apply our NativeMenu object to, it will only work on Windows, but if we set it to, it will only work on Mac.

// For Windows: = myMenu;

// For Mac OS: = myMenu;

How to fix this? We can check, which of the two menu types does the user's OS support, and then apply our menu object to the nativeWindow on Windows or NativeApplication on Mac.

if (NativeWindow.supportsMenu)
{ = myMenu;
if (NativeApplication.supportsMenu)
{ = myMenu;

The rest of the code is the same on all platforms. Now that we have set our myMenu to what we needed to, we can work with the myMenu object and do whatever we want to it. So, if we combine the code from this tutorial and the code from the previous tutorial, we will get this:

import flash.display.NativeMenu;
import flash.display.NativeMenuItem;

var myMenu:NativeMenu = new NativeMenu();

if (NativeWindow.supportsMenu)
{ = myMenu;
if (NativeApplication.supportsMenu)
{ = myMenu;

var item:NativeMenuItem = new NativeMenuItem("Hello!");

var mySubMenu:NativeMenu = new NativeMenu();
var subitem1:NativeMenuItem = new NativeMenuItem("Red"); = "Whoop";
var subitem2:NativeMenuItem = new NativeMenuItem("Blue"); = 123;
var subitem3:NativeMenuItem = new NativeMenuItem("Green"); = ["array","values"];

item.submenu = mySubMenu;

subitem1.addEventListener(Event.SELECT, selectItem);
subitem2.addEventListener(Event.SELECT, selectItem);
subitem3.addEventListener(Event.SELECT, selectItem);

function selectItem(evt:Event):void
trace("You've pressed the " + + " menu item, which has a data value of " +;

Thanks for reading!


Working with menus in AIR: Part 1
Working with menus in AIR: Part 3
Working with menus in AIR: Part 4
Working with menus in AIR: Part 5
Working with menus in AIR: Part 6

No comments:

Post a Comment