这两天为了Fluorida的closePopUp功能,读了点Flex框架的源码,对Alert,TitleWindow以及Flex的PopUp功能做了简单的分析。
【Alert和PopUp】
Alert内部其实是调用了PopUpManager.在parent参数为null或者为Application的时候,弹出的窗口将跟当前Application在一个容器下。Alert在最顶层,Application在最底层,中间那层是一个称之为modalWindows的控件,其实就是Alert后面那个磨砂的层。为了点到Alert上的按钮,写了一个小程序分析Alert的结构,不是很好读,但是可以运行一下,看看分析出的Alert的内部结构:(大略说一下,Alert的Child有一个AlertForm,而AlertForm的Child除了第一个是TextField以外,都是按钮)
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="horizontal">
<mx:Script>
<![CDATA[
import mx.core.IChildList;
import mx.core.UIComponent;
import mx.core.IFlexDisplayObject;
import mx.managers.ISystemManager;
import mx.managers.PopUpManagerChildList;
import mx.managers.PopUpManager;
import mx.controls.Alert;
import mx.events.CloseEvent;
import mx.core.Singleton;
import mx.managers.IPopUpManager;
private function showSimpleAlert():void{
Alert.okLabel="oKey";
Alert.show("Hello, World!","",15,null,alertCloseHandle);
var myTimer:Timer = new Timer(1000, 1);
myTimer.addEventListener("timer", timerHandler);
myTimer.start();
}
private function alertCloseHandle(event:CloseEvent):void{
simpleAlertShower.label = event.detail.toString();
}
private function timerHandler(event:TimerEvent):void{
var text:String = "elements:";
var sm:ISystemManager = (Application.application.root as ISystemManager);
text+=sm.numChildren.toString();
text+=";\n modalWindows:";
text+=sm.numModalWindows.toString();
for(var index:int = 0; index < sm.numChildren; index++)
{
text += "\n" + index + " : ";
text += sm.getChildAt(index).toString();
}
var alert:Alert = sm.getChildAt(sm.numChildren - 1) as Alert;
text += "\n buttonFlags : "+alert.buttonFlags;
text += "\n alertChildren:" + alert.numChildren;
for(var index:int = 0; index < alert.numChildren; index++)
{
text +="\n" + alert.getChildAt(index).toString();
}
var alertForm:UIComponent = alert.getChildAt(0) as UIComponent;
text += "\n alertFormChildren:" + alertForm.numChildren;
for(var index:int = 0; index < alertForm.numChildren; index++)
{
text +="\n"+index+":"+ alertForm.getChildAt(index).toString();
}
popupChildText.text = text;
alertForm.getChildAt(1).dispatchEvent(new MouseEvent(MouseEvent.CLICK));
// var popupContainer:IChildList = (application.root as IChildList);
// PopUpManager.removePopUp(popupContainer.getChildAt( popupContainer.numChildren - 1 ) as IFlexDisplayObject);
}
]]>
</mx:Script>
<mx:Button id="simpleAlertShower" click="showSimpleAlert();" label="Click Me"/>
<mx:Text id="popupChildText"/>
</mx:Application>
【关于TitleWindow】
TitleWindow作为弹出窗口的时候,跟Alert处的位置没什么区别,我想说的是TitleWindow的closeButton在哪里。下面这个同样不好读的程序可以帮助你分析TitleWindow或者说Panel里面都有用什么,以及closeButton在哪,其实就是在rawChildren的最后一个。
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="horizontal">
<mx:Script>
<![CDATA[
import mx.containers.TitleWindow;
import mx.core.IChildList;
import mx.core.UIComponent;
import mx.core.IFlexDisplayObject;
import mx.managers.ISystemManager;
import mx.managers.PopUpManagerChildList;
import mx.managers.PopUpManager;
import mx.controls.Alert;
import mx.events.CloseEvent;
import mx.core.Singleton;
import mx.managers.IPopUpManager;
private function showSimpleAlert():void{
var popUpWindow:TitleWindow = TitleWindow(PopUpManager.createPopUp(this,TitleWindow,true));
popUpWindow.width = 400;
popUpWindow.height = 300;
popUpWindow.visible = true;
popUpWindow.showCloseButton = true;
var myTimer:Timer = new Timer(2000, 1);
myTimer.addEventListener("timer", timerHandler);
myTimer.start();
}
private function alertCloseHandle(event:CloseEvent):void{
simpleAlertShower.label = event.detail.toString();
}
private function timerHandler(event:TimerEvent):void{
var text:String = "elements:";
var sm:ISystemManager = (Application.application.root as ISystemManager);
text+=sm.numChildren.toString();
text+=";\n modalWindows:";
text+=sm.numModalWindows.toString();
text+="\n top children: ";
for(var index:int = 0; index < sm.numChildren; index++)
{
text += "\n" + index + " : ";
text += sm.getChildAt(index).toString();
}
var titleWindow:TitleWindow = sm.getChildAt(sm.numChildren - 1) as TitleWindow;
text += "\n popUpWindowrawChildren:" + titleWindow.rawChildren.numChildren;
for(var index:int = 0; index < titleWindow.rawChildren.numChildren; index++)
{
text +="\n" + titleWindow.rawChildren.getChildAt(index).toString();
}
var titleBar:UIComponent = (titleWindow.rawChildren.getChildAt(2) as UIComponent);
text += " has " + titleBar.numChildren;
text += "\n" + titleBar.getChildAt(3).toString();
popupChildText.text = text;
// var popupContainer:IChildList = (application.root as IChildList);
// PopUpManager.removePopUp(popupContainer.getChildAt( popupContainer.numChildren - 1 ) as IFlexDisplayObject);
PopUpManager.removePopUp(titleWindow);
}
]]>
</mx:Script>
<mx:Button id="simpleAlertShower" click="showSimpleAlert();" label="Click Me"/>
<mx:Text id="popupChildText"/>
</mx:Application>