AIR System Tray and Dock Example
In a recent time tracking application I needed to show a custom icon in the system tray (or dock for Mac). Here were the requirements:[*]Show custom Application icon in the dock or system tray
[*]Change the status of the project when the Application icon was clicked
[*]Change the Application icon image when the status changed
[*]Show all the projects in the context menu and switch between them
[*]Handle common window methods (min, max, close, restore)
<?xml version="1.0" encoding="utf-8"?><s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" backgroundColor="#ffffff" name="System Tray Example" color="#333333" creationComplete="appInit()"><fx:Declarations><!-- 将非可视元素(例如服务、值对象)放在此处 --></fx:Declarations><fx:Script><!public var TrackingIcon1:Class;private var SystemTrayIcon1:Class;// these should be 128 x 128 to look cleaner// I resampled these from 16x16 using Preview which is why they look blurrypublic var TrackingIcon1128:Class;private var SystemTrayIcon128:Class;// these store our different set of iconsprivate var systemTrayIconArray:Array = new Array();private var systemTrayTrackingIcon1Array:Array = new Array();// project namesprivate var projectNames:Array = ['Music Player Project', 'Video Player Project'];// indicates if we are tracking the project or notprivate var tracking:Boolean = false;// menu item for starting or stopping the project the context menuprivate var startStopProject:NativeMenuItem;// reference to hold the selected project nameprivate var selectedProject:String = "";// set the icons on the application and create the context menusprivate function appInit():void {var systemTrayIcon:Bitmap = new SystemTrayIcon1();var trackingIcon:Bitmap = new TrackingIcon1();var iconStarted:Bitmap = new TrackingIcon1128();var iconStopped:Bitmap = new SystemTrayIcon128();// assign the icons to the arrayssystemTrayTrackingIcon1Array = ;systemTrayIconArray = ;// assign the icons to the application icon arrayNativeApplication.nativeApplication.icon.bitmaps = systemTrayTrackingIcon1Array;// add tool tips and event listeners to the icon CLICK eventif (NativeApplication.supportsSystemTrayIcon) {SystemTrayIcon(NativeApplication.nativeApplication.icon).tooltip = selectedProject;SystemTrayIcon(NativeApplication.nativeApplication.icon).addEventListener(MouseEvent.CLICK, systemTrayHandler);}else if (NativeApplication.supportsDockIcon) {var dockIcon:DockIcon = NativeApplication.nativeApplication.icon as DockIcon;NativeApplication.nativeApplication.addEventListener(InvokeEvent.INVOKE, systemTrayHandler);}// create context menu and assign it to our applicationcreateMenu();// set startup propertiesselectedProject = projectNames;projectName1.text = selectedProject;projectStatus1.text = selectedProject + " started";projectStatus2.source = new TrackingIcon1();}// create system tray and dock context menuprivate function createMenu():void {// create the tray and dock item menuvar trayMenu:NativeMenu = new NativeMenu();// we are adding common window menu options to the context menuvar minimizeMenu:NativeMenuItem = new NativeMenuItem("Minimize");var maximizeMenu:NativeMenuItem = new NativeMenuItem("Maximize");var restoreMenu:NativeMenuItem = new NativeMenuItem("Restore");var closeMenu:NativeMenuItem = new NativeMenuItem("Close");// separators in the context menuvar separator:NativeMenuItem = new NativeMenuItem("",true);var separator2:NativeMenuItem = new NativeMenuItem("",true);// current projects - these would be dynamically created...var projectMenu1:NativeMenuItem = new NativeMenuItem(projectNames);var projectMenu2:NativeMenuItem = new NativeMenuItem(projectNames);startStopProject = new NativeMenuItem("Start Project");// handles when a context menu item is selectedminimizeMenu.addEventListener(Event.SELECT, contextMenuHandler);maximizeMenu.addEventListener(Event.SELECT, contextMenuHandler);restoreMenu.addEventListener(Event.SELECT, contextMenuHandler);closeMenu.addEventListener(Event.SELECT, contextMenuHandler);projectMenu1.addEventListener(Event.SELECT, contextMenuHandler);projectMenu2.addEventListener(Event.SELECT, contextMenuHandler);startStopProject.addEventListener(Event.SELECT, contextMenuHandler);trayMenu.addItem(minimizeMenu);trayMenu.addItem(maximizeMenu);trayMenu.addItem(restoreMenu);trayMenu.addItem(closeMenu);trayMenu.addItem(separator);trayMenu.addItem(projectMenu1);trayMenu.addItem(projectMenu2);trayMenu.addItem(separator2);trayMenu.addItem(startStopProject);// add the menu to the menu propertyif (NativeApplication.supportsSystemTrayIcon) {SystemTrayIcon(NativeApplication.nativeApplication.icon).menu = trayMenu;}else if (NativeApplication.supportsDockIcon) {DockIcon(NativeApplication.nativeApplication.icon).menu = trayMenu;}}// swap the system tray icon when the icon is clicked (LEFT MOUSE CLICK)private function systemTrayHandler(e:Event):void {tracking = !tracking;if (tracking) {startTracking()}else {stopTracking()}}// update the system tray / dock icon to the tracking iconprivate function startTracking():void {NativeApplication.nativeApplication.icon.bitmaps = systemTrayTrackingIcon1Array;// update the ui to reflect the new tracking statusstartStopProject.label = "Stop Project";projectStatus1.text = selectedProject + " started";projectStatus2.source = new TrackingIcon1();tracking = true;}// update the system tray / dock icon to the non-tracking iconprivate function stopTracking():void {NativeApplication.nativeApplication.icon.bitmaps = systemTrayIconArray;// update the ui to reflect the new tracking statusstartStopProject.label = "Start Project";projectStatus1.text = selectedProject + " stopped";projectStatus2.source = new SystemTrayIcon1();tracking = false;}// handle the right click context menu in the system tray or dockprivate function contextMenuHandler(e:Event):void {// get selected menu itemvar menuItem:NativeMenuItem = e.target as NativeMenuItem;// handle the selected context menuif (menuItem.label == "Minimize") { this.minimize(); }if (menuItem.label == "Maximize") { this.maximize(); }if (menuItem.label == "Restore") { this.restore(); }if (menuItem.label == "Close") { this.close(); }// handle switching to another projectif (menuItem.label == projectNames) { updateCurrentProject(menuItem.label) }if (menuItem.label == projectNames) { updateCurrentProject(menuItem.label) }// switch the project statusif (menuItem.label == "Start Project") {startTracking();}else if (menuItem.label == "Stop Project") {stopTracking();}}// update the project nameprivate function updateCurrentProject(name:String):void {stopTracking();selectedProject = name;// automatically start tracking the new project - sure why notstartTracking();}]]></fx:Script><!-- view --><s:Label text="System Tray / Dock AIR Example" left="10" bottom="10"/><s:Label text="Project Started" id="projectStatus1"verticalCenter="0" horizontalCenter="0" fontSize="11"/><s:Label id="projectName1" text="{selectedProject}"fontSize="18" left="10" top="10" fontWeight="bold"/><s:Line y="41" right="10" left="10"/><mx:Image id="projectStatus2" width="16" height="16" right="10" bottom="10"/></s:WindowedApplication>
页:
[1]