23 November 2009
Restoring a Minimized AIR App From the Mac OS Dock
Those familiar with the Mac OS dock will know that clicking the dock icon of a minimized application will restore that application to it’s pre-minimized state. This is a nice little feature as the minimize/tray area of the dock can quickly become a disorganized mess if you keep a lot of apps open. Those familiar with developing AIR apps will also know that AIR apps don’t do this out of the box. This kinda sucks.
Fortunately there is a fairly easy way to implement this feature for yourself. All you need to do is setup two event listeners and two properties.
The two properties you need are “isMinimized” and “isMaximized”. Both are booleans, the first indicating the application is … drumroll … minimized and the section maximized. While you could get away with just one of these having both offers more flexibility in the future should you need it. For example: it is possible for an app to be neither maximized nor minimized.
The two events you need to listen for are NativeWindowDisplayStateEvent.DISPLAY_STATE_CHANGING and InvokeEvent.INVOKE. The first event is dispatched when the application’s display state changes (i.e., when maximized or minimized). The second is dispatched when the dock icon is clicked.
With those few things all you need to do now is a few start up things:
/**
* Event handler for the application's creationComplete event
*/
private function init():void
{
// Check to see if we have a dock available for clicking
if (NativeApplication.supportsDockIcon)
{
// Wire up a listener for the dock icon's invoke event
NativeApplication.nativeApplication.addEventListener(InvokeEvent.INVOKE, onDockIconClick);
}
// Effectively, listen for minimize and maximize event.
this.addEventListener(NativeWindowDisplayStateEvent.DISPLAY_STATE_CHANGING, onChanging);
}
/**
* Event handler for display state change events
*/
private function onChanging(event:NativeWindowDisplayStateEvent):void
{
switch (event.afterDisplayState) // <-- our new state
{
case "minimized":
isMaximized = false;
isMinimized = true;
break;
case "maximized":
isMaximized = true;
isMinimized = false;
break;
}
}
/**
* Called when the dock icon is clicked.
*/
private function onDockIconClick(event:InvokeEvent):void
{
if (isMinimized)
{
// If the app is minimized call this
stage.nativeWindow.restore();
isMaximized = true;
isMinimized = false;
}
}
In the above code it’s assumed that init will be called by the app’s creationComplete event. init then checks to see if the platform on which the app is running supports dock icons (that is, is Mac OS). If it is we add an event listener for the InvokeEvent which is dispatched when the app’s dock icon is clicked. After that init wires up a second event listener, this time listening for the NativeWindowDisplayStateEvent.DISPLAY_STATE_CHANGING event. This event is dispatched when the app is minimized or maximized.
onChanging is our event handler for the display state change event. We use this method to keep track of whether our app is maximized or minimized. onDockIconClick is our event handler for the app’s InvokeEvent that will be dispatched when the dock icon is clicked. This method checks to see if the app is currently minimized, and if it is it calls the window’s restore method.







