Flex [original] Air desktop applications minimize pallets

Source: Internet
Author: User

Example: how to minimize the pallets of an air desktop application

 

1. Core class: logic for minimizing pallets

package {    import flash.desktop.NativeApplication;    import flash.desktop.SystemTrayIcon;    import flash.display.BitmapData;    import flash.display.Loader;    import flash.display.NativeMenu;    import flash.display.NativeMenuItem;    import flash.display.NativeWindowDisplayState;    import flash.events.Event;    import flash.events.IOErrorEvent;    import flash.events.MouseEvent;    import flash.events.NativeWindowDisplayStateEvent;    import flash.net.URLRequest;        import mx.controls.Alert;    import mx.core.FlexGlobals;    import mx.core.UIComponent;    import mx.events.CloseEvent;        import spark.components.Application;        /**      * <br>Utils for  Let System to Tray </br>     * This class describes how to dock an AIR application to the system tray      * and then undock it again.      * The minimize and close actions of the WindowedApplication are caught, so      * that we can introduce our own actions.      *      * A simple systray menu is presented, to show the usage of that.      *      * @Author: S.Radovanovic      * @Url: http://www.cnblogs.com/loveFlex/     * @date: 2012-06-18      * @author binyy     * Updated for AIR Beta 3      */     public class Systray extends UIComponent    {        private static var stu:Systray;                private var _dockImage:BitmapData;         private var _app:Application = FlexGlobals.topLevelApplication as Application;        private const _icon:String = "assets/m_16.png";        [Bindable]        [Embed(source="assets/m_24.png")]        private var _iconClass:Class;                /**closing Application call back function*/        public var closingApp:Function = null;                /**toolTips*/        [Bindable]        public var toolTips:String = "SupweMedia";                public function Systray()        {            addEventListener(Event.ADDED_TO_STAGE,onAdded);        }                private function onAdded(event:Event):void        {            createSystemTray();        }                /**         * create system to tray util         * */        private function createSystemTray():void        {            //Use the loader object to load an image, which will be used for the systray //After the image has been loaded into the object, we can prepare the application //for docking to the system tray             var loader:Loader = new Loader();             loader.contentLoaderInfo.addEventListener(Event.COMPLETE, prepareForSystray);             loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,onIoError);            loader.load(new URLRequest(_icon));                         //Catch the closing event so that the user can decide if it wants to dock or really //close the application             _app.addEventListener(Event.CLOSING, closingApplication);         }                private function onIoError(event:IOErrorEvent):void        {            trace(event.text);        }                /**          * Check if the user wants to close the application or dock it          */         private function closingApplication(evt:Event):void {             //Don't close, so prevent the event from happening             evt.preventDefault();                         //Check what the user really want's to do //Alert.buttonWidth = 110; //            Alert.buttonWidth = 90;            Alert.buttonHeight = 25;            Alert.yesLabel = "Close";             Alert.noLabel = "Mini";             Alert.cancelLabel = "Cancel";            Alert.show("Close or minimize?", "Attention", 11, _app, alertCloseHandler,_iconClass);         }                 // Event handler function for displaying the selected Alert button.         private function alertCloseHandler(event:CloseEvent):void {             if (event.detail == Alert.YES) {                 closeApp(event);             } else if(event.detail == Alert.NO) {                 dock();             } else {}        }                         /**          * Check to see if the application may be docked and set basic properties          */         public function prepareForSystray(event:Event):void {                         //Retrieve the image being used as the systray icon             _dockImage = event.target.content.bitmapData;                         //For windows systems we can set the systray props //(there's also an implementation for mac's, it's similar and you can find it on the net   )             if (NativeApplication.supportsSystemTrayIcon){                 setSystemTrayProperties();                                 //Set some systray menu options, so that the user can right-click and access functionality //without needing to open the application                 SystemTrayIcon(NativeApplication.nativeApplication .icon).menu = createSystrayRootMenu();             }         }                 /**          * Create a menu that can be accessed from the systray          */         private function createSystrayRootMenu():NativeMenu{             //Add the menuitems with the corresponding actions             var menu:NativeMenu = new NativeMenu();             var openNativeMenuItem:NativeMenuItem = new NativeMenuItem("Open");             var exitNativeMenuItem:NativeMenuItem = new NativeMenuItem("Exit");                         //What should happen when the user clicks on something                         openNativeMenuItem.addEventListener(Event.SELECT, undock);                         exitNativeMenuItem.addEventListener(Event.SELECT, closeApp);                         //Add the menuitems to the menu             menu.addItem(openNativeMenuItem);             menu.addItem(new NativeMenuItem("",true));             //separator             menu.addItem(exitNativeMenuItem);                         return menu;         }                 /**          * To be able to dock and undock we need to set some eventlisteners          */         private function setSystemTrayProperties():void{             //Text to show when hovering of the docked application icon             SystemTrayIcon(NativeApplication.nativeApplication.icon).tooltip = toolTips;                         //We want to be able to open the application after it has been docked             SystemTrayIcon(NativeApplication.nativeApplication .icon).addEventListener(MouseEvent.CLICK, undock);                         //Listen to the display state changing of the window, so that we can catch the minimize             stage.nativeWindow.addEventListener(NativeWindowDisplayStateEvent.DISPLAY_STATE_CHANGING, nwMinimized); //Catch the minimize event         }                 /**          * Do the appropriate actions after the windows display state has changed.          * E.g. dock when the user clicks on minize          */         private function nwMinimized(displayStateEvent:NativeWindowDisplayStateEvent):void {                         //Do we have an minimize action? //The afterDisplayState hasn't happened yet, but only describes the state the window will go to, //so we can prevent it!             if(displayStateEvent.afterDisplayState == NativeWindowDisplayState.MINIMIZED) {                 //Prevent the windowedapplication minimize action from happening and implement our own minimize //The reason the windowedapplication minimize action is caught, is that if active we're not able to //undock the application back neatly. The application doesn't become visible directly, but only after clicking //on the taskbars application link. (Not sure yet what happens exactly with standard minimize)                 displayStateEvent.preventDefault();                                 //Dock (our own minimize)                 dock();             }         }                 /**          * Do our own 'minimize' by docking the application to the systray (showing the application icon in the systray)          */         public function dock():void {             //Hide the applcation             stage.nativeWindow.visible = false;                         //Setting the bitmaps array will show the application icon in the systray             NativeApplication.nativeApplication .icon.bitmaps = [_dockImage];                         //set toolTips when minisize             SystemTrayIcon(NativeApplication.nativeApplication.icon).tooltip = toolTips;        }                 /**          * Show the application again and remove the application icon from the systray          */         public function undock(evt:Event):void {             //After setting the window to visible, make sure that the application is ordered to the front, //else we'll still need to click on the application on the taskbar to make it visible             stage.nativeWindow.visible = true;             stage.nativeWindow.orderToFront();                         //Clearing the bitmaps array also clears the applcation icon from the systray             NativeApplication.nativeApplication .icon.bitmaps = [];         }                 /**          * Close the application          */         private function closeApp(evt:Event):void {             if(closingApp != null){                closingApp.call(this);            }            stage.nativeWindow.close();         }             }}

 

2: Call Method

Add the tray component to the main application: closinghandler is the function executed when the application is closed, tooltips is the prompt text that the cursor moves to the tray

<ascomponent:Systray closingApp="closingHandler" toolTips="I am Binyy">

 

This function can be expanded. We hope you can share with us any better version, 3q ~~!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.