The Ghost in the shell – Transparency

I recently re-read the post Creating a Notification Popup Widget from Emil Crumhorn.

Using the alpha functionality you can get a nice effects, e.g. a ghost shell. For example put the following into your View code into the method createPartControl() in the “RCP with a view” example. See Eclipse RCP Tutorial for details on RCP development.

	Shell shell = PlatformUI.getWorkbench()
		.getActiveWorkbenchWindow().getShell();
		int cur = shell.getAlpha();
		cur -= 150;
		System.out.println(cur);
		shell.setAlpha(cur);

And you get the following result:

Of course this is not very practical but you can use this to have a nice fade out effect of your RCP application. To get this put the following two methods into the class “ApplicationWorkbenchWindowAdvisor “.


	@Override
	public void postWindowClose() {
		Shell shell = PlatformUI.getWorkbench()
				.getActiveWorkbenchWindow().getShell();
		while (shell.getAlpha() > 0) {
			int alphaValue = shell.getAlpha() - 8;
			int newAlpha = alphaValue > 0 ? alphaValue : 0;
			shell.setAlpha(newAlpha);
                        // Consider evil ;-)  non-alpha systems
			if (shell.getAlpha()== 255){
				break;
			}
			wait(50);
			shell.update();

		}
		super.postWindowClose();
	}

	public static void wait(int n) {
		long t0, t1;
		t0 = System.currentTimeMillis();
		do {
			t1 = System.currentTimeMillis();
		} while (t1 - t0 < n);
	}

I tried to demonstrate via a small video this but unfortunately my screencast does not show the fade out effect. Therefore you have to try it yourself; it looks really nice.

 
Filed under: Eclipse

Comments

  1. Michal Tkacz Says:

    Be aware that the code above starts an infinite loop on systems that don’t support alpha functionality as the getAlpha() method continues to return 255 in that case.

  2. Aurelien Pupier Says:

    So cool! :)

    but why are you using UIPlugin.getDefaul() instead of PlatformUI. UIPlugin is internal.

  3. Philipp Kursawe Says:

    Nice one. Although when I close an app I want it to disappear right away :)

  4. Gilles Says:

    @Philipp: Absolutely correct.

    The effect might be cool for new windows which need some initiallization; the alpha value could indicate the level of completeness.

    Thanks for that, Lars. Works nice.

  5. Remy Chi Jian Suen Says:

    It is possible to get into an infinite loop with that code on Linux if your window manager and X server isn’t setup for transparent windows (mine isn’t). In that case, the getAlpha() method will always return 255.

    Looking at the win32 SWT source code, it also seems that if your Windows version isn’t 5.1 or higher (at least XP I guess?), then it’ll simply return 255.

  6. Nicolas Richeton Says:

    Hi Lars,

    You could also use the Nebula animation package and do :

    @Override
    public void postWindowOpen() {
    super.postWindowOpen();
    AlphaEffect.fadeOnClose( getWindowConfigurer().getWindow().getShell(), 500, new ExpoOut(), new AnimationRunner() );
    }

    The animation package takes care of catching the close event and do the animation.

    Note that in your example, you are locking the the UI thread thus locking any other animation or UI update during the fade out.

    You should use Display.timerExec() to schedule updates or another thread with Thread.sleep()

    The animation package is currently included in the Nebula Gallery widget. Javadoc and downloads are on the Nebula site.

  7. Lars Vogel Says:

    @Aurelien Thanks, I fixed this and use now PlatformUI

  8. Lars Vogel Says:

    @Michal @Nicolas @Remy thanks for the advice, I added a small check for non-alpha systems.

    @Philipp @Gilles Now that I have alpha fading I ONLY start the application to see it fade away. ;-)

  9. Tonny Madsen Says:

    Your loop are going to take all system resources, so you might want to use this instead

    /**
    * Sleeps for the specified number of msec.
    *
    * @param msec the length
    */
    public static void sleep(int msec) {
    cont = false;
    display.timerExec(msec, new Runnable() {
    @Override
    public void run() {
    cont = true;
    }
    });
    while (!cont) {
    if (!display.readAndDispatch()) {
    display.sleep();
    }
    }
    }

  10. Lars Vogel Says:

    @Tonny: Thanks.

Leave a Reply