Friday, April 1, 2011

Working with windows in AIR: Part 6

In this tutorial we will learn how to close all the opened sub-windows of our application once we close the main window.

If you tried testing the codes from the previous tutorials and closed the main window without closing the sub windows, you may have noticed that the sub windows remain there until you close them manually.

We are going to make them close themselves when we close the main window. Here's the code we have for now:

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;

myWindow.activate();

We can refer to the main window using this.stage.nativeWindow and add a listener for an Event.CLOSING event, which is thrown after the user clicks the close button but the window hasn't closed yet.

We can create an array of the windows that are opened by our application and then close each of them like this:

function closeAll(evt:Event):void
{
var allWindows:Array = NativeApplication.nativeApplication.openedWindows;
for (var i:int = 0; i<allWindows.length; i++)
{
allWindows[i].close();
}
}

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.events.Event;
import flash.desktop.NativeApplication;

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;

myWindow.activate();

this.stage.nativeWindow.addEventListener(Event.CLOSING, closeAll);

function closeAll(evt:Event):void
{
var allWindows:Array = NativeApplication.nativeApplication.openedWindows;
for (var i:int = 0; i<allWindows.length; i++)
{
allWindows[i].close();
}
}

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 5
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

1 comment:

Anonymous said...

This is really helpful to me.. Thank you!

Post a Comment