James Williams
LinkedInMastodonGithub

Application Window Fadeout with Griffon and Trident

Tags: Griffon

Ubuntu Karmic Koala and Windows 7 were released recently and though both have wizzier effects on the operating system level, you might want to add it to your applications. With Java 6 or higher and a decent graphics card you can do just that. The original inspiration for this post was my friend Josh's Griffon Tip on Intercepting Window Closing events.

Instead of sticking to his more practical application of the concept, I decided to add a little more bling and use Trident to implement a window slowly fading out on application shutdown instead of an immediate destruction of the window. In our controller we set up a timeline to update the opacity on timeline pulse. It will execute app.shutdown() when the timeline has completed:

import com.sun.awt.AWTUtilities
import org.pushingpixels.trident.Timeline
import org.pushingpixels.trident.callback.TimelineCallback

class TestfadeController {
    // these will be injected by Griffon
    def model
    def view

    void mvcGroupInit(Map args) {
        // this method is called after model and view are injected
        model.timeline = new Timeline(this)
        model.timeline.setDuration(3000)
        def cb = [
            onTimelineStateChanged:{oldState,newState,durationFraction,timelinePosition ->                   
                    if (timelinePosition == 1.0f)
                        app.shutdown()
                },
            onTimelinePulse: {durationFraction,timelinePosition -> 
                    AWTUtilities.setWindowOpacity(app.appFrames[0], (float)(1-durationFraction))
                }
        ] as TimelineCallback
        model.timeline.addCallback(cb)
    }

    /*
    def action = { evt = null ->
    }
    */
}

To handle the machines that might not be high-powered enough to handle translucency, my System76 Starling netbook being one of them, we need to adjust our windowClosing closure to check for translucency support and initiate a regular shutdown if it is not.

import javax.swing.WindowConstants
import org.pushingpixels.trident.Timeline
import com.sun.awt.AWTUtilities

application(title:'testfade',
  size:[320,480],
  defaultCloseOperation:WindowConstants.DO_NOTHING_ON_CLOSE,
    windowClosing: {evt ->
        if (AWTUtilities.isTranslucencySupported(AWTUtilities.Translucency.TRANSLUCENT)) {
            model.timeline.play()
        } else app.shutdown()

    }
) {
    // add content here
    label('Content Goes Here') // deleteme
}

Like Josh said in his post, we have to make sure the autoShutdown property in griffon-app/conf/Application.groovy is set to false. Though I didn't use TridentBuilder specifically, I installed the plugin to get the basic Trident assets.