Fade to Gray Modal Window Flex Effect

I’ve always liked the effect Windows XP gives you when you select Shut Down… and it then gives you the Shut Down, Restart, Log Off etc options in a modal window. If you leave that window in place for a second or so, your desktop slowly fades to gray while your modal window stays full color. I’ve been wondering for a while whether that effect was possible in Flex.

So, the simple answer is yes. Wait for this example to load, hit the Launch Popup button and watch the background image (you’ll need Flash Player 9).

So, how does it work? Flex 2’s PopUpManager class has an undocumented internal property called modalWindowClass. By setting that property to a class definition of your choosing, an instance of your class is created when you create a modal popup window. This instance sits “behind” your modal popup, affecting the appearance of the application. These windows are often called popup blockers.

I’ve created a popup blocker class (SaturationFadePopUpBlocker) that uses a new SaturationFade effect I’ve also written. The SaturationFade effect changes the color saturation level of component that is the target of the effect using an instance of the Flash Player’s ColorMatrixFilter.

To add the popup blocker to a Flex 2 application is very simple. The following is the single line of code that is required add the above popup blocker effect (alongside the need to import the necessary classes).

PopUpManager.mx_internal::modalWindowClass = SaturationFadePopUpBlocker;

The SaturationFadePopUpBlocker and SaturationFade effect classes can be downloaded as a Compiled SWC (inside this zip) or as a Flex Builder Library Project. The Library Project includes the compiled SWC and all the source code.

Although this example fades the background to gray, the SatuationFade effect I have written lets you change the color saturation of a component to any level, including making it brighter. The start and end saturation levels, along with the duration of the saturation fade, are all customisable in the Saturation effect and SaturationFadePopUpBlocker classes.

The SaturationFade effect class can also be used as a standalone effect, in place of any of the existing Flex framework effect classes. In this example, the SaturationFade effect plays when you hold your mouse down over the image, with the color saturation level fading from 1 to 0. The saturation level animates back to 1 when you let the mouse go:

The code for the above example is:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
xmlns:eff="com.adobe.effects.*" >
<mx:Script>
<![CDATA[
import com.adobe.effects.SaturationFade;
]]>
</mx:Script>
<eff:SaturationFade id="fadeToGray" duration="500"
saturationFrom="1" saturationTo="0"  />
<eff:SaturationFade id="fadeBack" duration="500"
saturationFrom="0" saturationTo="1" />
<mx:Image mouseDownEffect="fadeToGray" mouseUpEffect="fadeBack"
source="sunset.jpg"  />
</mx:Application>

I won’t go into detail about the implementation of the classes for now. The SaturationFadePopUpBlocker class is very small and should be fairly self-explanatory to most people. If there’s a demand, I’ll do a future post about creating effects using the Flex effects framework and, in particular, how the SaturationFade effect works.

59 Responses to Fade to Gray Modal Window Flex Effect

  1. Bjorn Schultheiss says:

    A true queenslander comment

  2. Sarge says:

    Hope you don’t mind but I copied your example in Flash 8 on my blog – gave you props for the idea. Nice work. Check it out:
    http://pixelfumes.blogspot.com/2006/09/flash-8-version-of-modal-pop-up.html
    -Sarge

  3. Andrey says:

    Can you give an example, where to put line
    PopUpManager.mx_internal::modalWindowClass = SaturationFadePopUpBlocker;
    Thanks in advance!

  4. Alistair McLeod says:

    Hi Andrey,
    Putting that line in the creationComplete handler of your main application should suffice.
    Ali

  5. Andrey says:

    Thanks! I was creating a non-modal window and that’s was my fault:)

  6. Sho says:

    Great technique. Thanks for creating this.

  7. Alex says:

    One thing i observed is that the window is not modal anymore, i can push the buttons behind it although they are black and white, using the swc. Is there any setting i can set to prevent this and make the popup act like a true modal window?
    Thanks for this great example

  8. chris says:

    Guillermo, I’ve found a way:
    In SaturationFadePopUpBlocker.as change overriden method createChildren.
    there’s a code:
    protected override function createChildren():void
    {
    super.createChildren();
    modalWindow = new FlexSprite();
    modalWindow.name = “modalWindow”;
    var sm : ISystemManager = (parent is SystemManager)
    ? (parent as SystemManager)
    : null;
    addChild((modalWindow as DisplayObject));
    var g : Graphics = modalWindow.graphics;
    var s : Rectangle = sm ? sm.screen : new Rectangle(0,0,100,100);
    g.beginFill(0xFFFFFF, 0.0);
    g.drawRect(0,0,1,1);
    g.endFill();
    modalWindow.width = s.width;
    modalWindow.height = s.height;
    modalWindow.x = s.x;
    modalWindow.y = s.y;
    originalFilters = parentApplication.filters;
    effect = new SaturationFade( parentApplication );
    effect.duration = FADE_DURATION;
    effect.saturationFrom = SATURATION_FROM;
    effect.saturationTo = SATURATION_TO;
    effect.play();
    }
    and that’s all.
    But there’s a problem I don’t know how to fix: when I try to launch a popup it’s ok, but then I try to run another popup from this current popup, after close first popup, effect doesn’t work correctly 🙁

  9. Mark Luffel says:

    The “PopUpManager.mx_internal::modalWindowClass” property doesn’t seem to be available with the new 2.0.1 version of Flex Builder. Does this have anything to do with the mx_internal namespace?

  10. clshrock says:

    For Flex 2.0.1 in the SaturationFade.as file, I had to change all references of “EffectInstance” to “IEffectInstance”–including the import statement and the initInstance method signature.

  11. Greg K. says:

    For Flex 2.0.1 PopupManager is not a class within itself. It’s a wrapper onto a Singleton of type IPopUpManager
    You get it like this before your init function:
    private static var ipum:IPopUpManager = Singleton.getInstance(“mx.managers::IPopUpManager”) as IPopUpManager;
    and then in creationComplete you use:
    ipum.mx_internal::modalWindowClass = SaturationFadePopUpBlocker;

  12. Greg K. says:

    also in the function above, modalFunction needs to be declared as FlexSprite before use.

  13. cpozen says:

    Alistair,
    Any word on the flex calendar release ? I’m in desperate need for a project.

  14. Community says:

    Hi Andrey,
    Putting that line in the creationComplete handler of your main application should suffice.

  15. yesil perde says:

    One thing i observed is that the window is not modal anymore, i can push the buttons behind it although they are black and white, using the swc. Is there any setting i can set to prevent this and make the popup act like a true modal window?
    Thanks for this great example

  16. canli yayin says:

    The “PopUpManager.mx_internal::modalWindowClass” property doesn’t seem to be available with the new 2.0.1 version of Flex Builder. Does this have anything to do with the mx_internal namespace?

  17. Jochen says:

    Is it possible to post a new version of this nice effect? With all the fixes mentioned here I still have runtime problems.

  18. mirc says:

    is Fade to Gray Modal Window Flex Effect an adobe photoshop plug-in ?

  19. hosting says:

    modalFunction needs to be declared as FlexSprite before use.

  20. aşk says:

    “So, the simple answer is yes. Wait for this example to load, hit the Launch Popup button and watch the background image (you’ll need Flash Player 9).” but how 🙁

  21. sohbet odaları says:

    Any word on the flex calendar release ? I’m in desperate need for a project. thanks for all!!

  22. Cyril says:

    Hi,
    very nice, but i didn’t succeed in make it run with Flex3/Apollo. Anyone has a full working example ?
    Thanks,
    Cyril

  23. çeviri says:

    For Flex 2.0.1 in the SaturationFade.as file, I had to change all references of “EffectInstance” to “IEffectInstance”–including the import statement and the initInstance method signature.

  24. One thing i observed is that the window is not modal anymore, i can push the buttons behind it although they are black and white, using the swc.

  25. Sohbet odalarý says:

    I am also interested in the calendar feature and thank you a lot for this effect.

  26. LarryA says:

    Nice work! What code do you use to close the smaller form when you hit the “x” on the form?
    Thanks in advance

  27. Abel says:

    is there a zip file of the whole modal app? As a new Flex student, I still get pretty lost. Or if someone can put the entire code for the App that makes the pop up window appear and the background go gray, that would be awesome, please.
    Cordially,
    Abel K.
    Miami Beach, FL

  28. chat says:

    What code do you use to close the smaller can put the entire code for the App that makes the pop

  29. seks says:

    Nice work..
    saturationTo=”1″ or saturationTo=”2″ good working..

  30. nate says:

    you should enable “view source” code on example projects…

  31. For Flex 2.0.1 PopupManager is not a class within itself. It’s a wrapper onto a Singleton of type IPopUpManager
    You get it like this before your init function:
    private static var ipum:IPopUpManager = Singleton.getInstance(“mx.managers::IPopUpManager”) as

  32. islami sohbet says:

    you should enable “view source” code on example projects

  33. ev says:

    You get it like this before your init function:
    private static var ipum:IPopUpManager = Singleton.getInstance(“mx.managers::IPopUpManager”) as

  34. blah says:

    Alistair,
    Any word on the flex calendar release ? I’m in desperate need for a project.

  35. seksi kizlar says:

    The “PopUpManager.mx_internal::modalWindowClass” property doesn’t seem to be available with the new 2.0.1 version of Flex Builder. Does this have anything to do with the mx_internal namespace?

  36. abhishek says:

    could u send modal page(form) sample which is working correctlly

  37. chat says:

    The “PopUpManager.mx_internal::modalWindowClass” property doesn’t seem to be available with the new 2.0.1 version of Flex Builder. Does this have anything to do with the mx_internal namespace?

  38. zhsu says:

    flex 2.0.1 solution:
    uncheck the project’s compiler ‘enable strict type checking’
    then
    var ipm:IPopUpManager =Singleton.getInstance(“mx.managers::IPopUpManager”) as IPopUpManager;
    ipm.mx_internal::modalWindowClass=SaturationFadePopUpBlocker;

  39. dimpled says:

    I found a small issue if you load a popup right after closing a popup – seems the new popup takes the de-saturated filter as the “originalfilter”, so when you close the new popup – it reverts to, you guessed it: the desaturated filter.
    The crappy fix I made is to change “parentApplication.filters = originalfilters” to “parentApplication.filters = new Array()” which just erases the filters.
    The better fix would be to check for the instance of a SaturationFade filter and remove it from the array of originalfilters..
    anyway, nice little class – for those of you having trouble getting it to work in flex builder 3, be sure to follow clshrock’s post about using the Singleton wrapper with the IPopUpManger object.

  40. komik says:

    Putting that line in the creationComplete handler of your main application should suffice.
    +1

  41. Hayvanlar says:

    Hi,
    Putting that line in the creationComplete handler of your main application should suffice.
    TgrL

  42. uzamax says:

    What code do you use to close the smaller form when you hit the “x” on the form

  43. One thing i observed is that the window is not modal anymore, i can push the buttons behind it although they are black and white, using the swc.

  44. oyun says:

    you should enable “view source” code on example projects

  45. you should enable “view source” code on example projects…

  46. kadin says:

    you should enable view source code on example projects

  47. thanks
    Thanks! I was creating a non-modal window and that’s was my fault:)

  48. oyun says:

    What code do you use to close the smaller can put the entire code for the App that makes the pop

  49. Oyunlar says:

    Any word on the flex calendar release ?

  50. sohbet says:

    The “PopUpManager.mx_internal::modalWindowClass” property doesn’t seem to be available with the new 2.0.1 version of Flex Builder. Does this have anything to do with the mx_internal namespace?

  51. çet says:

    Any word on the flex calendar release ? I’m in desperate need for a project.

  52. Stas says:

    For Flex Builder 3 you can write also
    import mx.core.Singleton;
    import mx.managers.*
    …..
    var ipm:IPopUpManager = Singleton.getInstance(“mx.managers::IPopUpManager”) as IPopUpManager;
    var o:Object = ipm;
    var p:QName = new QName(mx_internal, “modalWindowClass”);
    o[p] = SaturationFadePopUpBlocker;

  53. edward says:

    Any word on the flex calendar release ?

  54. aşk says:

    Putting that line in the creationComplete handler of your main application should suffice.

  55. Web Design says:

    Thanks! I was creating a non-modal window and that’s was my fault:)

  56. Aşk says:

    Can you give an example, where to put line
    PopUpManager.mx_internal::modalWindowClass = SaturationFadePopUpBlocker;
    Thanks in advance!

  57. sesli sohbet says:

    Thanks! I was creating a non-modal window and that’s was my fault:)