Thursday, March 31, 2011

Working with windows in AIR: Part 5

Today we will learn how to close and re-open windows.

Windows can be closed by the user if he presses the close button on the window, or through code - using a method called close();

myWindow.close();

However, if a window is closed using either of the methods above, you won't be able to re-open it again. If you try to do that - AIR will throw you an error, saying that this operation cannot be done to an already closed window.

There's a work-around for this, though. Instead of closing the window, we make it invisible. This way the user thinks he closed the window, but its actually there, just not visible to him.

Firstly, here's the code for creating the window:

var myOptions:NativeWindowInitOptions = new NativeWindowInitOptions();
myOptions.type = NativeWindowType.UTILITY;
myOptions.systemChrome = NativeWindowSystemChrome.STANDARD;
myOptions.transparent = false;

var myWindow:NativeWindow = new NativeWindow(myOptions);
myWindow.width = 200;
myWindow.height = 200;
myWindow.x = (stage.fullScreenWidth - myWindow.width)/2;
myWindow.y = (stage.fullScreenHeight - myWindow.height)/2;

var myText:TextField = new TextField();
myText.text = "Hello world!";

myWindow.stage.addChild(myText);
myWindow.stage.scaleMode = StageScaleMode.NO_SCALE;
myWindow.stage.align = StageAlign.TOP_LEFT;

You can see that I have also centered the window by setting its X and Y values manually.

Now, create a button, put it on stage and name it myButton.

We activate() the window when the button is clicked:

myButton.addEventListener(MouseEvent.CLICK, openWindow);

function openWindow(MouseEvent):void
{
myWindow.activate();
}

Now, the closing. We are using an UTILITY type of window, which has a built-in button to close the window. We can catch an event called Event.CLOSING, which is distributed after the user clicks the close button but before the window actually closes.

In that very moment, we need to make the window invisible and then make sure the window doesn't close.

myWindow.addEventListener(Event.CLOSING, closeWindow);

function closeWindow(evt:Event):void
{
myWindow.visible = false;
evt.preventDefault();
}

And now it works!

Here's the full code:

import flash.display.NativeWindowInitOptions;
import flash.display.NativeWindowType;
import flash.display.NativeWindow;
import flash.display.NativeWindowSystemChrome;
import flash.text.TextField;
import flash.display.StageScaleMode;
import flash.display.StageAlign;
import flash.display.Shape;
import flash.events.MouseEvent;
import flash.events.Event;

var myOptions:NativeWindowInitOptions = new NativeWindowInitOptions();
myOptions.type = NativeWindowType.UTILITY;
myOptions.systemChrome = NativeWindowSystemChrome.STANDARD;
myOptions.transparent = false;

var myWindow:NativeWindow = new NativeWindow(myOptions);
myWindow.width = 200;
myWindow.height = 200;
myWindow.x = (stage.fullScreenWidth - myWindow.width)/2;
myWindow.y = (stage.fullScreenHeight - myWindow.height)/2;

var myText:TextField = new TextField();
myText.text = "Hello world!";

myWindow.stage.addChild(myText);
myWindow.stage.scaleMode = StageScaleMode.NO_SCALE;
myWindow.stage.align = StageAlign.TOP_LEFT;

myButton.addEventListener(MouseEvent.CLICK, openWindow);

function openWindow(MouseEvent):void
{
myWindow.activate();
}

myWindow.addEventListener(Event.CLOSING, closeWindow);

function closeWindow(evt:Event):void
{
myWindow.visible = false;
evt.preventDefault();
}

Thanks for reading!

Related:

Working with windows in AIR: Part 1
Working with windows in AIR: Part 2
Working with windows in AIR: Part 3
Working with windows in AIR: Part 4
Working with windows in AIR: Part 6
Working with windows in AIR: Part 7
Working with windows in AIR: Part 8
Working with windows in AIR: Part 9
Working with windows in AIR: Part 10
Working with windows in AIR: Part 11
Working with windows in AIR: Part 12

No comments:

Post a Comment