- Windows in Adobe AIR
- Creating Windows Using NativeWindow
- Creating Windows Using mx.core.Window
- Getting a Window Reference
- Window Operations
- Understanding Window Events
- Creating Custom Window Chrome
- Summary
Creating Windows Using NativeWindow
NativeWindow can be used to host an array of content such as HTML, Adobe® Flash® SWF files, or images. It is not, however, intended for use with Adobe Flex components directly. Instead, please refer to “Creating Windows Using mx.core.Window” later in this chapter.
A special type of NativeWindow, HTMLLoader.createRootContent(), exists specifically for hosting HTML content. It includes the necessary machinery for loading HTML as well as support for scrolling content.
For now let’s start with the basics. Here’s how to go about creating and configuring a NativeWindow:
- Create and configure NativeWindowInitOptions.
- Create an instance of NativeWindow, passing in NativeWindowInitOptions.
- Open the Window onscreen.
Listing 5.1 outlines these steps in ActionScript code. If you have downloaded the source code for this book, then you will find the correlating project in your FlexBuilder called “Chapter05-01”.
Listing 5.1. Creating a NativeWindow
<?xml version="1.0" encoding="utf-8"?> <mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" verticalAlign="middle" horizontalAlign="center"> <mx:Script> <![CDATA[ private function openWindow():void { var windowOptions:NativeWindowInitOptions = new NativeWindowInitOptions(); windowOptions.systemChrome = NativeWindowSystemChrome.STANDARD; windowOptions.type = NativeWindowType.NORMAL; var newWindow:NativeWindow = new NativeWindow( windowOptions ); newWindow.activate(); } ]]> </mx:Script> <mx:Button label="Create Window" click="openWindow()" /> </mx:WindowedApplication>
Setting NativeWindowInitOptions
NativeWindow initialization options, NativeWindowInitOptions, describe the look and behavior of your window. Once set, these parameters are passed into the constructor when instantiating the NativeWindow instance. These options are not mandatory because they all have default values. For instance, not passing in NativeWindowInitOptions gives you a standard-looking window for your operating system with standard window controls. As we progress through this chapter, we explore how we can change this default behavior—but keep in mind that after the window is created, these options cannot be changed! Table 5.1 outlines the configurable options.
Table 5.1. Properties of NativeWindowInitOptions
Property |
Description |
systemChrome |
Specifies the type of system chrome used by the window |
type |
Specifies the type of the window to be created |
maximizable |
Specifies whether the window can be maximized |
minimizable |
Specifies whether the window can be minimized |
resizable |
Specifies whether the window can be resized |
transparent |
Specifies whether the window supports transparency and alpha blending against the desktop |
Let’s explore what each of these NativeWindowInitOptions are and how they affect the characteristics of a new native window. First up is the systemChrome. The chrome is what frames the content of a native window.
NativeWindowInitOptions.systemChrome
The frame that encompasses a window is referred to as the chrome. The chrome typically offers controls to manipulate the window, such as minimize, drag, resize, and close.
There are three options for systemChrome, as shown in the following sections.
NativeWindowSystemChrome.STANDARD
This option creates a standard-looking native window as per the operating system the Adobe AIR application is running on (see Figure 5.1). Also, the transparent property of the window must be set to false (which is the default value). The following snippet demonstrates how to set the systemChrome to standard, which is also the default value if none is specified.
var windowOptions:NativeWindowInitOptions = new NativeWindowInitOptions(); windowOptions.systemChrome = NativeWindowSystemChrome.STANDARD;
Figure 5.1 Standard system chrome on Mac OS X and Windows XP.
NativeWindowSystemChrome.NONE
This option specifies that the window should not display any system chrome whatsoever. Creating a NativeWindow with no chrome generates a rectangle onscreen with no controls. This is the starting point for implementing custom chrome discussed later in this chapter. The following demonstrates how to specify no system chrome:
var windowOptions:NativeWindowInitOptions = new NativeWindowInitOptions(); windowOptions.systemChrome = NativeWindowSystemChrome.NONE;
NativeWindowInitOptions.type
Each window offers unique traits suited for different roles in an application. There are three NativeWindowTypes to choose from:
- NORMAL
- UTILITY
- LIGHTWEIGHT
NativeWindowType.NORMAL
This is the default window type. If nothing is specified for this parameter in your NativeWindowInitOptions, Figure 5.2 shows what is displayed.
Figure 5.2 Default window type on Mac OS X and Windows XP.
NORMAL windows have typical controls such as minimize, maximize, and close. Their physical characteristics match that of any standard window on each respective operating system.
NativeWindowType.UTILITY
In Figure 5.3, you see the same system chrome but differences in both physical and behavioral aspects of NativeWindow.
Figure 5.3 UTILITY windows have a slimmer title bar, and they don’t show up in the Windows taskbar or the Mac OS X Dock (note the lack of a Minimize icon).
Often used as containers for supporting content or tool palettes, these windows do not serve as the primary focus of an application. Their content may change as events happen in the main application window, such as displaying properties of an object that has received focus.
NativeWindowType.LIGHTWEIGHT
LIGHTWEIGHT NativeWindows have no chrome whatsoever. In fact, you’ll get a runtime error unless you specifically set the systemChrome property to NONE. Creating a window in this fashion gives you a white box that can’t be moved or even closed directly. Figure 5.4 demonstrates a native window with no chrome and uses a bitmap image as the window’s background.
Figure 5.4 An Adobe AIR application implemented with custom window chrome.
Uses for LIGHTWEIGHT NativeWindows range from custom system chrome implementations to toast messages (dialogs that temporarily slide up onscreen like toast out of a toaster) to drawer dialogs common on Mac OS X.
NativeWindowInitOptions.transparent
This property refers to the transparency of the window background window. A transparent window has no default background. Any area not occupied by a display object is invisible; for example, whatever lies beneath your application window shows through.
You can also change the alpha property of your display objects to allow underlying desktop content to show through.
NativeWindowInitOptions.maximizable
When this property is set to false, the window cannot be maximized. For a window with system chrome, this affects the appearance of the window Maximize button, such as making it appear disabled.
NativeWindowInitOptions.minimizable
When this property is set to false, the window cannot be minimized. As with a window with system chrome, this affects the appearance of the window Minimize button.
NativeWindowInitOptions.resizable
When this property is set to false, the window cannot be resized.
Creating an Instance of the Window
Now we need to create a new NativeWindow instance. Remember that the properties defined in NativeWindowInitOptions cannot be changed after we instantiate the window. The default window size is determined by the operating system, but you can change it by setting the window bounds. (We’ll look at this later in the chapter.)
var newWindow:NativeWindow = new NativeWindow( windowOptions );
The variable windowOptions refers to the NativeWindowInitOptions we constructed in the previous section.
Putting the Window Onscreen
If we were to stop at the previous step, the user would not see anything appear onscreen. After instantiating our NativeWindow, we need to specifically put it on the screen. There are two ways this can be accomplished:
NativeWindow.activate()
or
NativeWindow.visible = true
Using NativeWindow.activate()
Invoking the activate() method on the NativeWindow instance does the following:
- Makes the window visible
- Brings the window to the front
- Gives the window keyboard and mouse focus
The following snippet instantiates a new NativeWindow, passing in window options, followed by the activate() method.
var newWindow:NativeWindow = new NativeWindow( windowOptions ); newWindow.activate();
Using NativeWindow.visible
This property specifies whether the window is visible on the desktop. It affects only visibility and does not give the window focus or bring it to the front.
For example, you might want to open a supporting UTILITY type window for an application where focus must remain on the primary window. Rather than activating your window, simply set its visible property to true, and it appears onscreen without the primary window flashing in and out of focus.
By default, visible is set to false. To make the window visible, do the following:
var newWindow:NativeWindow = new NativeWindow( windowOptions ); newWindow.visible = true