Ivan Zamylin
Ivan Zamylin

Reputation: 1748

Flex 4 component on top of TitleWindow

I have Flex s:Group with some ui elements and I want it to be always (!) on top of any component inside my app, including s:TitleWindow (dialog). How can I achieve it with Flex 4?

Thanks in advance!

Upvotes: 1

Views: 1428

Answers (3)

Ilya Zaytsev
Ilya Zaytsev

Reputation: 1055

Add you top component in systemManager.rawChildren after application creationComplete event. See down:

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" 
               creationComplete="application_creationCompleteHandler(event)"
               xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">
    <fx:Script>
        <![CDATA[
            import mx.core.UIComponent;
            import mx.events.CloseEvent;
            import mx.events.FlexEvent;
            import mx.managers.PopUpManager;

            protected function application_creationCompleteHandler(event:FlexEvent):void
            {
                panel.width = width;
                panel.height = 300;
                systemManager.rawChildren.addChild(panel);
            }

            protected function addPopup():void
            {
                PopUpManager.addPopUp(titleWin, this, true);
                PopUpManager.centerPopUp(titleWin);
            }

            protected function button_clickHandler(event:MouseEvent):void
            {
                addPopup();
            }

            protected function titleWin_closeHandler(evt:CloseEvent):void 
            {
                PopUpManager.removePopUp(evt.currentTarget as UIComponent);
            }
        ]]>
    </fx:Script>
    <fx:Declarations>
        <s:Panel id="panel" backgroundColor="0x00ff00" alpha="0.9" title="Panel">
            <s:layout>
                <s:VerticalLayout paddingLeft="50" horizontalAlign="left" verticalAlign="middle" />
            </s:layout>
            <s:Button id="btn" width="200" height="100" label="create popup" 
                      click="button_clickHandler(event)" /> 
        </s:Panel>

        <s:TitleWindow id="titleWin"
                       title="Spark TitleWindow"
                       close="titleWin_closeHandler(event);"
                       width="300">
            <s:layout>
                <s:VerticalLayout paddingLeft="10" paddingRight="10"
                                  paddingTop="10" paddingBottom="10" />
            </s:layout>
            <s:Label text="Popup window"
                     fontSize="24"
                     width="100%"/>
        </s:TitleWindow>
    </fx:Declarations>
</s:Application>

Upvotes: 2

eddipedia
eddipedia

Reputation: 411

You should use the mx.managers.PopUpManager class. It provides a simple interface to handle displaying content above any other.

You can write your own interface that encapsulates the PopUpManager interface to give you a chance to bring all your "always-on-top" components on top again, even when you add another popup that should appear behind.

Simple example:

public final class AlwaysOnTopPopUpManager {

    private static var alwaysOnTopDisplayObjects:Array;

    public static function addAlwaysOnTopObject(displayObject:IFlexDisplayObject):void
    {
        alwaysOnTopDisplayObjects = alwaysOnTopDisplayObjects || [];

        if (alwaysOnTopDisplayObjects.indexOf(displayObject) == -1)
        {
            alwaysOnTopDisplayObjects.push(displayObject);
            arrange();
        }
    }


    public static function removeAlwaysOnTopObject(displayObject:IFlexDisplayObject):void
    {
        if (alwaysOnTopDisplayObjects)
        {
            var index:int = alwaysOnTopDisplayObjects.indexOf(displayObject);

            if (index != -1)
            {
                alwaysOnTopDisplayObjects.splice(index, 1);
            }
        }
    }


    // uses the same interface as mx.managers.PopUpManager
    public static function addPopUp(window:IFlexDisplayObject, parent:DisplayObject, modal:Boolean = false, childList:String = null, moduleFactory:IFlexModuleFactory = null):void
    {
        mx.managers.PopUpManager.addPopUp(window, parent, modal, childList, moduleFactory);
        arrange();
    }


    private static function arrange():void
    {
        for each (var popup:IFlexDisplayObject in alwaysOnTopDisplayObjects)
        {
            mx.managers.PopUpManager.bringToFront(popup);
        }
    }

The class holds a list of objects to appear above any other content. Each time an object is added to that list or to the popups the arrange() method is called and bring all your registered objects to front again.

Upvotes: 3

duTr
duTr

Reputation: 714

If all your elements are in the same MXML file, you simply have to put your s:Group element at the bottom of the file as the last element of it.

If your views are wired more dynamically, then you can force its index with something like this:

var topIndex:int = myParentView.numChildren - 1;
myParentView.setChildIndex(myGroupElement, topIndex);

Where myGroupElement if the reference to your s:Group or its ID.

myGroupElement must already be a child of myParentView when this code is executed.

If your s:Group element is only displayed occasionally, you might be interested in Popups.

Upvotes: 0

Related Questions