Adobe AIR Programming: Working with Windows - Advanced | WebReference

Adobe AIR Programming: Working with Windows - Advanced


Getting a Window Reference

[This tutorial is the advanced portion of the session started here.]

Before you can work with a particular window, you first need to get a reference of that Window instance. The following sections describe the various ways to obtain a Window reference.

Window Constructor

You can use the window constructor for a new NativeWindow to get a reference, like this:

Current Window Stage

You can get a reference directly from the current window stage, as follows:

Display Object on the Stage

Any display object on the stage can also give you a reference, as follows:

As an example, suppose you have an mx.containers.Panel in some window. To get the reference to the parent NativeWindow instance, you can do this:

Referencing the Active Window

A desktop window that currently holds user focus is referred to as the "active" window. You can reference this window via NativeApplication, as follows:

If the active window on the desktop is not associated with your application, activeWindow returns a null value.

Referencing All Opened Windows

All open windows can be referenced via the nativeApplication object. These can be cycled through like any Array. Each element will be a NativeWindow instance.

Window Operations

In this section we look into controlling a Window's dimensions, positioning and behaviors.

Resizing a Window

You can invoke a resize action on a window by calling the following method:

The resize functionality only exists in NativeWindow. In your Window instance of type mx.core.Window or mx.core.WindowedApplication, you need to call the startResize() method on the NativeWindow property of your window. (Window and WindowedApplication are essentially just an Adobe Flex wrapper on NativeWindow.)

The next code example (as shown in Listing 5.4) demonstrates an mx.core.Window being created with a button that initiates the resize of that same window from the lower-right corner. (Click and hold the Start Resize button and drag your mouse to resize the window.)

LISTING 5.4 Initiating Window Resize

Listing 5.4 is an oversimplified example for sake of clarity. A more realistic use case would involve having graphic elements within a custom window chrome initiate this resize behavior. (See "Creating Custom Window Chrome" later in this chapter.)

Moving a Window

To move a window, call the startMove() method on the NativeWindow instance. If you're using mx.core.Window, reference the underlying NativeWindow via the NativeWindow property:

Maximizing, Minimizing, and Restoring a Window

Maximizing causes a window to expand to the bounds of the current screen. To maximize a window, use NativeWindow.maximize();

To minimize a window, use NativeWindow.minimize();

To restore a window, use NativeWindow.restore();

Restoring a window simply means that the window will return to the size that it was before it was either minimized or maximized.

Closing a Window

To close a window, use NativeWindow.close()

Closing a window empties the contents of the window, but if any other objects have references to that content, the content objects are not destroyed. You can check the closed property of a window to test whether a window has been closed. If the window being closed is the last one, and the NativeApplication.autoExit property is set to true (the default setting), the application quits.