AIR | Creating Native Windows

June 4th, 2008 posted by Marcio Rosa

Adobe® AIR™ lets developers use their existing web development skills in HTML, AJAX, Flash and Flex to build and deploy rich Internet applications to the desktop.

Windowing API

//create the init options
var options:NativeWindowInitOptions = new NativeWindowInitOptions();
options.transparent = false;
options.systemChrome = NativeWindowSystemChrome.STANDARD;
options.type = NativeWindowType.NORMAL;
//create the window
var newWindow:NativeWindow = new NativeWindow(options);
newWindow.title = "My Window";
newWindow.width = 600;
newWindow.height = 400;
 
//activate and show the new window
newWindow.activate();

You create native windows using the NativeWindow class. The settings for the
window are contained in an instance of the NativeWindowInitOptions
class as shown below:

Manipulating Native Windows

NativeWindow.activate();
NativeWindow.alwaysInFront = true;
NativeWindow.close();
NativeWindow.maximize();
NativeWindow.minimize();
NativeWindow.orderInBackOf(otherWin);
NativeWindow.orderInFrontOf(otherWin);
NativeWindow.orderToBack();
NativeWindow.orderToFront();

Also has properties like x, y, width, and height that can be animated
just like a DisplayObject.

Native window methods - Adding Content to Windows

//newWindow is a NativeWindow instance
var htmlView:HTMLLoader = new HTMLLoader();
htmlView.width = 300;
htmlView.height = 500;
//set the stage
newWindow.stage.align = "TL";
newWindow.stage.scaleMode = "noScale";
newWindow.stage.addChild(htmlView);
 
//urlString is the URL of the HTML page to load
htmlView.load(new URLRequest(“http://yahoo.com”));

You can add content to native windows by simply using the addChild method
of the native window’s stage object:

Native Window Events
Events in the flash.events.Event class:

Event.ACTIVATE
Event.DEACTIVATE
Event.CLOSING
Event.CLOSE

Events in the NativeWindowBoundsEvent class:

NativeWindowBoundsEvent.MOVING
NativeWindowBoundsEvent.MOVE
NativeWindowBoundsEvent.RESIZING
NativeWindowBoundsEvent.RESIZE

Events in the NativeWindowDisplayStateEvent class:

NativeWindowDisplayStateEvent.DISPLAY_STATE_CHANGING
NativeWindowDisplayStateEvent.DISPLAY_STATE_CHANGE

Fullscreen Window Mode

stage.displayState = StageDisplayState.FULL_SCREEN;

You enter full screen mode the same way that you do for a regular SWF by
setting the stage’s displayState property:

Unlike browser SWF files, you can accept keyboard input when in full screen
mode. Use responsibly.

Staying On Top

NativeWindow.alwaysInFront = true;

You can make your applications always stay in front of other applications
by setting the window’s alwaysInFront property:

Handling Multiple Monitors
You can enumerate the screens of the virtual desktop with the following screen
methods and properties:

Screen.screens

Provides an array of Screen objects describing the available screens. Note that the
order of the array is not signicant.

Screen.mainScreen

Provides a Screen object for the main screen. On Mac OS X, this will be the screen
displaying the menu bar. On Windows, this will be the system-designated primary
screen.

Screen.getScreensForRectangle()

Provides an array of Screen objects describing the screens intersected by the given
rectangle. The rectangle passed to this method is in pixel coordinates on the virtual
desktop.