⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 windowmanagersimple.as

📁 flex 实现的一个showcase 喜欢flex的朋友可以
💻 AS
字号:
// ActionScript file
package com.teknision.window
{
    import flash.display.NativeWindow;
    import flash.display.NativeWindowInitOptions;
    import flash.display.StageAlign;
    import flash.display.StageScaleMode;
    import flash.events.Event;
    import flash.events.NativeWindowBoundsEvent;
    import flash.utils.Dictionary;
    import mx.core.WindowedApplication;
    import mx.containers.Canvas;
    import mx.core.Application;
    import mx.core.UIComponent;
    import mx.events.FlexEvent;
    import mx.managers.FocusManager;
    import mx.managers.IFocusManagerContainer;
    
    /**
     * A simplistic window manager for Apollo native windows hosting Flex controls.
     * Used by a more fully featured window manager such as
     * com.partlyhuman.apollo.WindowManager, or code that doesn't require advanced window
     * management.
     * 
     * Simplifies creation and destruction of windows. Encapsulates Daniel Dura's
     * Flex-components-in-native-windows fix posted at 
     * http://www.danieldura.com/archive/apollo-multi-window-support-using-flex
     * 
     * Handles creation and destruction of native windows.
     * 
     * @author Roger Braunstein
     */
    public class WindowManagerSimple
    {
        protected const DEFAULT_WINDOW_WIDTH:int = 500;
        protected const DEFAULT_WINDOW_HEIGHT:int = 300;
        
        protected var viewLookup:Dictionary;
        protected var windowLookup:Dictionary;
        protected var mainWindow:NativeWindow;
        
        protected var mainWindowCanvas:Canvas;
        protected var app:WindowedApplication;
        
        protected var _isReady:Boolean = false;
        public function get isReady():Boolean {return _isReady;}
        
        protected static var instance:WindowManagerSimple;
        
        public function WindowManagerSimple(key:SingletonEnforcer):void
        {
            if (key == null) throw new ArgumentError();
            
            app = WindowedApplication(Application.application);
            
         
            
            mainWindow = app.nativeWindow;// app.window;
            
            
            
            mainWindowCanvas = new Canvas();
            mainWindowCanvas.includeInLayout = false;
            mainWindowCanvas.visible = false;
            app.addChild(mainWindowCanvas);
            mainWindowCanvas.addEventListener(FlexEvent.CREATION_COMPLETE, onReady);
            
            viewLookup = new Dictionary(true);
            windowLookup = new Dictionary(true);
        }
        
        /**
         * It takes at least one frame advance for the window manager to be ready, so 
         * I recommend you call WindowManagerSimple.getInstance() in the initializer of your
         * application.
         */
        public static function getInstance():WindowManagerSimple
        {
            if (!instance) instance = new WindowManagerSimple(new SingletonEnforcer());
            return instance;
        }
        
        /**
         * The window creation method. Windows are created asynchronously, and will appear
         * when they are ready, but their handle is returned synchronously.
         * 
         * This method will throw an error if it is called before it has time to create
         * the temporary canvas on the root stage (necessary for the Apollo Alpha).
         * 
         * Allows an anonymous object to specify window options that override the defaults.
         * This allows you to specify options much more concisely.
         * @see flash.display.NativeWindowInitOptions
         * 
         * uses:
         *   newWindow(AboutView);
         *   newWindow(new Panel(), "empty");
         *   newWindow(AboutView, "About", {resizable: false, minimizable: true}, 300, 400);
         *   newWindow(myComponent, "empty", myWindowInitOptions, 800, 600);
         * 
         * @param viewOrViewClass
         *   The view object or view class which will fill up the window. Must be a Flex component
         *   that can accept focus, or the class of one.
         * @param title
         *   Title of the window
         * @param options
         *   Either a NativeWindowInitOptions object or an Object containing keys that are 
         *   valid properties of NativeWindowInitOptions.
         * @param width
         *   Width of the window
         * @param height
         *   Height of the window
         */
        public function newWindow(viewOrViewClass:Object, title:String = "", options:Object = null, width:int = 0, height:int = 0):NativeWindow
        {
            if (!_isReady) throw new Error("WindowManager isn't ready to add windows yet.");
            
            //prepare window options
            var windowOptions:NativeWindowInitOptions = new NativeWindowInitOptions();
            if (options is NativeWindowInitOptions)
            {
                windowOptions = options as NativeWindowInitOptions;
            } else if (options is Object) {
                for (var key:String in options)
                try
                {
                    windowOptions[key] = options[key];
                } catch (e:Error) {}
            }
            
            //prepare window
            var win:NativeWindow = new NativeWindow(windowOptions);
            win.width = width || DEFAULT_WINDOW_WIDTH;
            win.height = height || DEFAULT_WINDOW_HEIGHT;
            win.title = title;

            win.stage.align = StageAlign.TOP_LEFT;
            win.stage.scaleMode = StageScaleMode.NO_SCALE;
            
            //prepare view
            var view:UIComponent;
            if (viewOrViewClass is Class)
            {
                view = UIComponent(new viewOrViewClass());
            } else {
                view = UIComponent(viewOrViewClass);
            }
            
            view.width = win.stage.stageWidth;
            view.height = win.stage.stageHeight;
            mainWindowCanvas.addChild(view);
            
            view.addEventListener(FlexEvent.CREATION_COMPLETE, newWindowStep2);
            
            //add cross-references
            viewLookup[win] = view;
            windowLookup[view] = win;
            
            return win;
        }
        
        protected function newWindowStep2(event:Event):void
        {
            var view:UIComponent = UIComponent(event.target);
            var win:NativeWindow = NativeWindow(windowLookup[view]);

            view.removeEventListener(FlexEvent.CREATION_COMPLETE, newWindowStep2);
        
            mainWindowCanvas.removeChild(view);
            win.stage.addChild(view);
            
            // view.focusManager = new FocusManager(IFocusManagerContainer(view), true);
            // view.focusManager.activate();
            // view.focusManager.showFocus();

            win.addEventListener(NativeWindowBoundsEvent.RESIZE, onWindowResize);
            win.addEventListener(Event.CLOSE, onWindowClose);

            win.visible = true;
        }
        
        protected function onWindowResize(event:NativeWindowBoundsEvent):void
        {
            var window:NativeWindow = NativeWindow(event.target);
            var view:UIComponent = UIComponent(viewLookup[window]);
            if (!view) throw new Error("View not registered for a resized window");
            view.width = window.stage.stageWidth;
            view.height = window.stage.stageHeight;
        }
        
        protected function onWindowClose(event:Event):void
        {
            var win:NativeWindow = NativeWindow(event.target);
            var view:UIComponent = UIComponent(viewLookup[win]);
            
            win.removeEventListener(NativeWindowBoundsEvent.RESIZE, onWindowResize);
            win.removeEventListener(Event.CLOSE, onWindowClose);
            
            delete windowLookup[view];
            delete viewLookup[win];
            
            win.stage.removeChild(view);
            view = null;
            win = null;
        }
        
        protected function onReady(event:FlexEvent):void
        {
            _isReady = true;
        }
        
    }
}
class SingletonEnforcer{}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -