Many operating systems provide a taskbar, such as the Mac OS X dock, that can contain an icon to represent an application. Adobe® AIR® provides an interface for interacting with the application task bar icon through the NativeApplication.nativeApplication.icon property.
AIR creates the NativeApplication.nativeApplication.icon object automatically. The object type is either DockIcon or SystemTrayIcon, depending on the operating system. You can determine which of these InteractiveIcon subclasses that AIR supports on the current operating system using the NativeApplication.supportsDockIcon and NativeApplication.supportsSystemTrayIcon properties. The InteractiveIcon base class provides the properties width, height, and bitmaps, which you can use to change the image used for the icon. However, accessing properties specific to DockIcon or SystemTrayIcon on the wrong operating system generates a runtime error.
To set or change the image used for an icon, create an array containing one or more images and assign it to the NativeApplication.nativeApplication.icon.bitmaps property. The size of taskbar icons can be different on different operating systems. To avoid image degradation due to scaling, you can add multiple sizes of images to the bitmaps array. If you provide more than one image, AIR selects the size closest to the current display size of the taskbar icon, scaling it only if necessary. The following example sets the image for a taskbar icon using two images:
air.NativeApplication.nativeApplication.icon.bitmaps =
[bmp16x16.bitmapData, bmp128x128.bitmapData];
To change the icon image, assign an array containing the new image or images to the bitmaps property. You can animate the icon by changing the image in response to an enterFrame or timer event.
To remove the icon from the notification area on Windows, or restore the default icon appearance on Mac OS X, set bitmaps to an empty array:
air.NativeApplication.nativeApplication.icon.bitmaps = [];
AIR supports dock icons when NativeApplication.supportsDockIcon is true. The NativeApplication.nativeApplication.icon property represents the application icon on the dock (not a window dock icon).
You can add commands to the standard dock menu by creating a NativeMenu object containing the commands and assigning it to the NativeApplication.nativeApplication.icon.menu property. The items in the menu are displayed above the standard dock icon menu items.
You can bounce the dock icon by calling the NativeApplication.nativeApplication.icon.bounce() method. If you set the bounce() priority parameter to informational, then the icon bounces once. If you set it to critical, then the icon bounces until the user activates the application. Constants for the priority parameter are defined in the NotificationType class.
When the dock icon is clicked, the NativeApplication object dispatches an invoke event. If the application is not running, the system launches it. Otherwise, the invoke event is delivered to the running application instance.
AIR supports system tray icons when NativeApplication.supportsSystemTrayIcon is true, which is currently the case only on Windows. On Windows, system tray icons are displayed in the notification area of the taskbar. No icon is displayed by default. To show an icon, assign an array containing BitmapData objects to the icon bitmaps property. To change the icon image, assign an array containing the new images to bitmaps. To remove the icon, set bitmaps to null.
You can add a menu to the system tray icon by creating a NativeMenu object and assigning it to the NativeApplication.nativeApplication.icon.menu property (no default menu is provided by the operating system). Access the system tray icon menu by right-clicking the icon.
Add a tooltip to an icon by setting the tooltip property:
air.NativeApplication.nativeApplication.icon.tooltip = "Application name";
The SystemTrayIcon object referenced by the NativeApplication.nativeApplication.icon property dispatches a ScreenMouseEvent for click, mouseDown, mouseUp, rightClick, rightMouseDown, and rightMouseUp events. You can use these events, along with an icon menu, to allow users to interact with your application when it has no visible windows.
The following example creates an AIR application which has a system tray icon, but no visible windows. The system tray icon has a menu with a single command for exiting the application.
<html>
<head>
<script src="AIRAliases.js" language="JavaScript" type="text/javascript"></script>
<script language="JavaScript" type="text/javascript">
var iconLoadComplete = function(event)
{
air.NativeApplication.nativeApplication.icon.bitmaps = new runtime.Array(event.target.content.bitmapData);
}
air.NativeApplication.nativeApplication.autoExit = false;
var iconLoad = new air.Loader();
var iconMenu = new air.NativeMenu();
var exitCommand = iconMenu.addItem(new air.NativeMenuItem("Exit"));
exitCommand.addEventListener(air.Event.SELECT,function(event){
air.NativeApplication.nativeApplication.icon.bitmaps = [];
air.NativeApplication.nativeApplication.exit();
});
if (air.NativeApplication.supportsSystemTrayIcon) {
air.NativeApplication.nativeApplication.autoExit = false;
iconLoad.contentLoaderInfo.addEventListener(air.Event.COMPLETE,iconLoadComplete);
iconLoad.load(new air.URLRequest("icons/AIRApp_16.png"));
air.NativeApplication.nativeApplication.icon.tooltip = "AIR application";
air.NativeApplication.nativeApplication.icon.menu = iconMenu;
}
if (air.NativeApplication.supportsDockIcon) {
iconLoad.contentLoaderInfo.addEventListener(air.Event.COMPLETE,iconLoadComplete);
iconLoad.load(new air.URLRequest("icons/AIRApp_128.png"));
air.NativeApplication.nativeApplication.icon.menu = iconMenu;
}
window.nativeWindow.close();
</script>
</head>
<body>
</body>
</html>
Iconified representations of windows are typically displayed in the window area of a taskbar or dock to allow users to easily access background or minimized windows. The Mac OS X dock displays an icon for your application as well as an icon for each minimized window. The Microsoft Windows taskbar displays a button containing the progam icon and title for each normal-type window in your application.
When a window is in the background, you can notify the user that an event of interest related to the window has occurred. On Mac OS X, you can notify the user by bouncing the application dock icon (as described in Bouncing the dock). On Windows, you can highlight the window taskbar button by calling the notifyUser() method of the NativeWindow instance. The type parameter passed to the method determines the urgency of the notification:
The following statement highlights the taskbar button of a window:
window.nativeWindow.notifyUser(air.NotificationType.INFORMATIONAL);
Calling the NativeWindow.notifyUser() method on an operating system that does not support window-level notification has no effect. Use the NativeWindow.supportsNotification property to determine if window notification is supported.
On the Windows operating system, windows created with the types utility or lightweight do not appear on the taskbar. Invisible windows do not appear on the taskbar, either.
Because the initial window is necessarily of type, normal, in order to create an application without any windows appearing in the taskbar, you must either close the initial window or leave it invisible. To close all windows in your application without terminating the application, set the autoExit property of the NativeApplication object to false before closing the last window. To simply prevent the initial window from ever becoming visible, add <visible>false</visible> to the <initalWindow> element of the application descriptor file (and do not set the visible property to true or call the activate() method of the window).
In new windows opened by the application, set the type property of the NativeWindowInitOption object passed to the window constructor to NativeWindowType.UTILITY or NativeWindowType.LIGHTWEIGHT.
On Mac OS X, windows that are minimized are displayed on the dock taskbar. You can prevent the minimized icon from being displayed by hiding the window instead of minimizing it. The following example listens for a nativeWindowDisplayState change event and cancels it if the window is being minimized. Instead the handler sets the window visible property to false:
function preventMinimize(event){
if(event.afterDisplayState == air.NativeWindowDisplayState.MINIMIZED){
event.preventDefault();
event.target.visible = false;
}
}
If a window is minimized on the Mac OS X dock when you set the visible property to false, the dock icon is not removed. A user can still click the icon to make the window reappear.
Send me an e-mail when comments are added to this page | Comment Report