Wednesday, 14 September 2011

How to keep dragged TitleWindow within Flex Application Boundary

package
{
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Rectangle;

import mx.containers.Canvas;
import mx.containers.TitleWindow;
import mx.events.CloseEvent;
import mx.managers.PopUpManager;

public class AdvancedTitleWindow extends TitleWindow
{
private var titleBarOverlay:TitleWindow;

public function AdvancedTitleWindow()
{
super();
}

override protected function createChildren():void
{
super.createChildren();

if(!this.titleBarOverlay)
{
this.titleBarOverlay = new TitleWindow();
this.titleBarOverlay.width = this.width;
this.titleBarOverlay.height = this.titleBar.height;

this.titleBarOverlay.showCloseButton = true;
this.titleBarOverlay.addEventListener(CloseEvent.CLOSE, onClose);

rawChildren.addChild(this.titleBarOverlay);
}

addDragEventListeners();
}

override protected function updateDisplayList(unscaledWidthArg:Number, unscaledHeightArg:Number):void
{
super.updateDisplayList(unscaledWidthArg, unscaledHeightArg);

this.titleBarOverlay.width = this.width;
this.titleBarOverlay.height = this.titleBar.height;
}

private function addDragEventListeners():void
{
this.titleBarOverlay.addEventListener(MouseEvent.MOUSE_DOWN, onTitleBarPress, false, 0, true);
this.titleBarOverlay.addEventListener(MouseEvent.MOUSE_UP, onTitleBarRelease, false, 0, true);
}

private function onTitleBarPress(mouseEventArg:MouseEvent):void
{
// Here you can set the boundary using owner, parent, parentApplication, etc.
this.startDrag(false, new Rectangle(0, 0, parent.width - this.width, parent.height - this.height));
}

private function onTitleBarRelease(mouseEventArg:MouseEvent):void
{
this.stopDrag();
}

private function onClose(closeEventArg:CloseEvent):void
{
PopUpManager.removePopUp(this);
}
}
}

No comments:

Post a Comment