Wednesday, May 25, 2011

HTML in AIR: Part 14

Today we will handle window.close(), window.moveBy(), window.moveTo(), window.resizeBy() and window.resizeTo Javascript methods in AS3.

First lets edit the HTML code so that it includes all the said JS methods:

<html>
<body>
<input type="button" value="Change title!" onclick="window.document.title='hello'">
<input type="button" value="Open window!" onclick="window.open('http://www.kirill-poletaev.blogspot.com/', 'mywindow', 
'width=1200,height=800,scrollbars=1,resizable=0')">
<input type="button" value="Close!" onclick="window.close()">
<input type="button" value="Move to (0,0)!" onclick="window.moveTo(0,0)">
<input type="button" value="Move left by 20!" onclick="window.moveBy(20,0)">
<input type="button" value="Resize to 1200x800!" onclick="window.resizeTo(1200,800)">
<input type="button" value="Resize by -50 width and height!" onclick="window.resizeBy(-50, -50)">
</body>
</html>

Now, the custom HTMLHost class. When window.close() is called in JS, HTMLHost responds with calling windowClose(), which should close the main native window:

override public function windowClose():void
{
NativeApplication.nativeApplication.openedWindows[0].close();
}

Simple as that!

Next, the window.moveBy(), window.moveTo(), window.resizeBy() and window.resizeTo JS functions. HTMLHost responds to all of them with just one method - windowRect(), which is a setter, and which has one parameter - a Rectangle object.

override public function set windowRect(mybounds:Rectangle):void
{
NativeApplication.nativeApplication.openedWindows[0].bounds = mybounds;
}

So, the whole myHTMLHost class looks like this so far:

package 
{
import flash.html.HTMLHost;
import flash.desktop.NativeApplication;
import flash.html.HTMLWindowCreateOptions;
import flash.geom.Rectangle;
import flash.html.HTMLLoader;
import flash.display.NativeWindowInitOptions;

public class myHTMLHost extends HTMLHost
{
public function myHTMLHost(defaultBehavior:Boolean=true)
{
super(defaultBehavior);
}

override public function updateTitle(titlestr:String):void
{
NativeApplication.nativeApplication.openedWindows[0].title = titlestr;
}

override public function createWindow(options:HTMLWindowCreateOptions):HTMLLoader
{
var boundRect:Rectangle = new Rectangle(options.x,options.y,options.width,options.height);
var myoptions:NativeWindowInitOptions = new NativeWindowInitOptions();
myoptions.resizable = options.resizable;
var newloader:HTMLLoader = HTMLLoader.createRootWindow(true,myoptions,options.scrollBarsVisible,boundRect);
return newloader;
}

override public function windowClose():void
{
NativeApplication.nativeApplication.openedWindows[0].close();
}

override public function set windowRect(mybounds:Rectangle):void
{
NativeApplication.nativeApplication.openedWindows[0].bounds = mybounds;
}
}
}

Thanks for reading!

Related:

HTML in AIR: Part 1
HTML in AIR: Part 2
HTML in AIR: Part 3
HTML in AIR: Part 4
HTML in AIR: Part 5
HTML in AIR: Part 6
HTML in AIR: Part 7
HTML in AIR: Part 8
HTML in AIR: Part 9
HTML in AIR: Part 10
HTML in AIR: Part 11
HTML in AIR: Part 12
HTML in AIR: Part 13
HTML in AIR: Part 15
HTML in AIR: Part 16
HTML in AIR: Part 17

No comments:

Post a Comment