Search code examples
apache-flexflasheventscomponents

Flex: Determine if a component is showing


What is the best way to determine if a component in Flex/Flash is showing on the user's screen? I'm looking for an analog to Java's Component.isShowing() method.

The show and hide events fire for visibility, and this seems to work for the first descendant of a ViewStack component, but not further down the display tree.


Solution

  • UIComponent.visible is not necessarily valid for children of an object where visible=false. From the docs:

    "In either case the children of the object will not emit a show or hide event unless the object has specifically written an implementation to do so."

    I wrote a sample application that confirms this to be true. What you can do is walk up the display list checking for visible to be false on a parent. Basically "visible" gives false positives but shouldn't give false negatives. Here is a quick utility I put together:

    package
    {
        import flash.display.DisplayObject;
    
        import mx.core.Application;
    
        public class VisibilityUtils
        {
            public static function isDisplayObjectVisible(obj : DisplayObject) : Boolean {
                if (!obj.visible) return false;
                return checkDisplayObjectVisible(obj);
            }
    
            private static function checkDisplayObjectVisible(obj : DisplayObject) : Boolean {
                if (!obj.parent.visible) return false;
                if (obj.parent != null && !(obj.parent is Application))
                    return checkDisplayObjectVisible(obj.parent);
                else
                    return true;
            }
        }
    }
    

    I haven't done anything more than trivial tests on this but it should get you started.