NativeWindow.js 9.84 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
/*
 * Ext JS Library 0.30
 * Copyright(c) 2006-2009, Ext JS, LLC.
 * licensing@extjs.com
 * 
 * http://extjs.com/license
 */

/**
 * @class Ext.air.NativeWindow
 * @extends Ext.air.NativeObservable
 * 
 * Wraps the AIR NativeWindow class to give an Ext friendly API. <br/><br/>This class also adds 
 * automatic state management (position and size) for the window (by id) and it can be used 
 * for easily creating "minimize to system tray" for the main window in your application.<br/><br/>
 * 
 * Note: Many of the config options for this class can only be applied to NEW windows. Passing 
 * in an existing instance of a window along with those config options will have no effect.
 * 
 * @constructor
 * @param {Object} config
 */
Ext.air.NativeWindow = function(config){
	Ext.apply(this, config);
	
	/**
	 * @type String
	 */
	this.id = this.id || Ext.uniqueId();
	
	this.addEvents(
		/**
		 * @event close
		 * @param {Object} e The air event object
		 */
		'close', 
		/**
		 * @event closing
		 * @param {Object} e The air event object
		 */
		'closing',
		/**
		 * @event move
		 * @param {Object} e The air event object
		 */
		'move',
		/**
		 * @event moving
		 * @param {Object} e The air event object
		 */
		'moving',
		/**
		 * @event resize
		 * @param {Object} e The air event object
		 */
		'resize',
		/**
		 * @event resizing
		 * @param {Object} e The air event object
		 */
		'resizing',
		/**
		 * @event displayStateChange
		 * @param {Object} e The air event object
		 */
		'displayStateChange',
		/**
		 * @event displayStateChanging
		 * @param {Object} e The air event object
		 */
		'displayStateChanging'
	);
	
	Ext.air.NativeWindow.superclass.constructor.call(this);
	
	if(!this.instance){
		var options = new air.NativeWindowInitOptions();
		options.systemChrome = this.chrome;
		options.type = this.type;
		options.resizable = this.resizable;
		options.minimizable = this.minimizable;
		options.maximizable = this.maximizable;
		options.transparent = this.transparent;
		
		this.loader = window.runtime.flash.html.HTMLLoader.createRootWindow(false, options, false);
		if (this.file) {
			this.loader.load(new air.URLRequest(this.file));	
		} else {
			this.loader.loadString(this.html || '');
		}
		
	
		this.instance = this.loader.window.nativeWindow;
	}else{
		this.loader = this.instance.stage.getChildAt(0);
	}
	
	var provider = Ext.state.Manager;
	var b = air.Screen.mainScreen.visibleBounds;
	
	var state = provider.get(this.id) || {};
	provider.set(this.id, state);
		
	var win = this.instance;
	
	var width = Math.max(state.width || this.width, 100);
	var height = Math.max(state.height || this.height, 100);
	
	var centerX = b.x + ((b.width/2)-(width/2));
	var centerY = b.y + ((b.height/2)-(height/2));
	
	var x = !Ext.isEmpty(state.x, false) ? state.x : (!Ext.isEmpty(this.x, false) ? this.x : centerX);
	var y = !Ext.isEmpty(state.y, false) ? state.y : (!Ext.isEmpty(this.y, false) ? this.y : centerY);
	
	win.width = width;
	win.height = height;
	win.x = x;
	win.y = y;
	
	win.addEventListener('move', function(){
		if(win.displayState != air.NativeWindowDisplayState.MINIMIZED && win.width > 100 && win.height > 100) {
			state.x = win.x;
			state.y = win.y;
		}
	});	
	win.addEventListener('resize', function(){
		if (win.displayState != air.NativeWindowDisplayState.MINIMIZED && win.width > 100 && win.height > 100) {
			state.width = win.width;
			state.height = win.height;
		}
	});
	
	Ext.air.NativeWindowManager.register(this);
	this.on('close', this.unregister, this);
	
	/**
	 * @cfg {Boolean} minimizeToTray 
	 * True to enable minimizing to the system tray. Note: this should only be applied
	 * to the primary window in your application. A trayIcon is required.
	 */
	if(this.minimizeToTray){
		this.initMinimizeToTray(this.trayIcon, this.trayMenu);
	}
	
};

Ext.extend(Ext.air.NativeWindow, Ext.air.NativeObservable, {
	
	/**
	 * @cfg {air.NativeWindow} instance 
	 * The native window instance to wrap. If undefined, a new window will be created.
	 */
	
	/**
	 * @cfg {String} trayIcon 
	 * The icon to display when minimized in the system tray
	 */
	/**
	 * @cfg {NativeMenu} trayMenu 
	 * Menu to display when the tray icon is right clicked
	 */
	/**
	 * @cfg {String} trayTip 
	 * Tooltip for the tray icon
	 */	
	
	/**
	 * @cfg {String} chrome 
	 * The native window chrome (defaults to 'standard', can also be 'none').
	 */
	chrome: 'standard', // can also be none
	/**
	 * @cfg {String} type 
	 * The native window type - normal, utility or lightweight. (defaults to normal)
	 */
	type: 'normal',	// can be normal, utility or lightweight
	/**
	 * @cfg {Number} width
	 */
	width:600,
	/**
	 * @cfg {Number} height 
	 */
	height:400,
	/**
	 * @cfg {Boolean} resizable 
	 */
	resizable: true,
	/**
	 * @cfg {Boolean} minimizable 
	 */
	minimizable: true,
	/**
	 * @cfg {Boolean} maximizable 
	 */
	maximizable: true,
	/**
	 * @cfg {Boolean} transparent
	 */
	transparent: false,
	
	/**
	 * Returns the air.NativeWindow instance
	 * @return air.NativeWindow
	 */
	getNative : function(){
		return this.instance;
	},
	
	/**
	 * Returns the x/y coordinates for centering the windw on the screen
	 * @return {x: Number, y: Number}
	 */
	getCenterXY : function(){
		var b = air.Screen.mainScreen.visibleBounds;
		return {
			x: b.x + ((b.width/2)-(this.width/2)),
			y: b.y + ((b.height/2)-(this.height/2))
		};
	},
	
	/**
	 * Shows the window
	 */
	show :function(){
		if(this.trayed){
			Ext.air.SystemTray.hideIcon();
			this.trayed = false;
		}
		this.instance.visible = true;
	},
	
	/**
	 * Shows and activates the window
	 */
	activate : function(){
		this.show();
		this.instance.activate();
	},
	
	/**
	 * Hides the window
	 */
	hide :function(){
		this.instance.visible = false;
	},
	
	/**
	 * Closes the window
	 */
	close : function(){
		this.instance.close();	
	},
	
	/**
	 * Returns true if this window is minimized
	 * @return Boolean
	 */
	isMinimized :function(){
		return this.instance.displayState == air.NativeWindowDisplayState.MINIMIZED;
	},
	
	/**
	 * Returns true if this window is maximized
	 * @return Boolean
	 */
	isMaximized :function(){
		return this.instance.displayState == air.NativeWindowDisplayState.MAXIMIZED;
	},
	
	/**
	 * Moves the window to the passed xy and y coordinates
	 * @param {Number} x
	 * @param {Number} y
	 */
	moveTo : function(x, y){
		this.x = this.instance.x = x;
		this.y = this.instance.y = y;	
	},
	/**
	 * Enter full-screen mode for the window.
	 * @param {Boolean} nonInteractive (optional) Boolean flag to allow the full screen window to be interactive or not. By default this is false.
	 * Example Code:
	 * var win = new Ext.air.NativeWindow({instance: Ext.air.NativeWindow.getRootWindow()});
	 * win.fullscreen();
	 */
	fullscreen: function(nonInteractive) {
		var SDS = runtime.flash.display.StageDisplayState;
		this.instance.stage.displayState = nonInteractive ? SDS.FULL_SCREEN : SDS.FULL_SCREEN_INTERACTIVE; 
	},
	
	bringToFront: function() {
		this.instance.orderToFront();
	},
	
	bringInFrontOf: function(win) {
		this.instance.orderInFrontOf(win.instance ? win.instance : win);
	},
	
	sendToBack: function() {
		this.instance.orderToBack();
	},
	
	sendBehind: function(win) {
		this.instance.orderInBackOf(win.instance ? win.instance : win);
	},
	
	
	/**
	 * @param {Number} width
	 * @param {Number} height
	 */
	resize : function(width, height){
		this.width = this.instance.width = width;
		this.height = this.instance.height = height;	
	},
	
	unregister : function(){
		Ext.air.NativeWindowManager.unregister(this);
	},
	
	initMinimizeToTray : function(icon, menu){
		var tray = Ext.air.SystemTray;
		
		tray.setIcon(icon, this.trayTip);
		this.on('displayStateChanging', function(e){
			if(e.afterDisplayState == 'minimized'){
				e.preventDefault();
				this.hide();
				tray.showIcon();
				this.trayed = true;
			}
		}, this);
		
		tray.on('click', function(){
			this.activate();
		}, this);
		
		if(menu){
			tray.setMenu(menu);
		}
	}
});

/**
 * Returns the first opened window in your application
 * @return air.NativeWindow
 * @static
 */
Ext.air.NativeWindow.getRootWindow = function(){
	return air.NativeApplication.nativeApplication.openedWindows[0];
};

/**
 * Returns the javascript "window" object of the first opened window in your application
 * @return Window
 * @static
 */
Ext.air.NativeWindow.getRootHtmlWindow = function(){
	return Ext.air.NativeWindow.getRootWindow().stage.getChildAt(0).window;
};

/**
 * @class Ext.air.NativeWindowGroup
 * 
 * A collection of NativeWindows.
 */
Ext.air.NativeWindowGroup = function(){
    var list = {};

    return {
		/**
		 * @param {Object} win
		 */
        register : function(win){
            list[win.id] = win;
        },

        /**
		 * @param {Object} win
		 */
        unregister : function(win){
            delete list[win.id];
        },

        /**
		 * @param {String} id
		 */
        get : function(id){
            return list[id];
        },

        /**
		 * Closes all windows
		 */
        closeAll : function(){
            for(var id in list){
                if(list.hasOwnProperty(id)){
                    list[id].close();
                }
            }
        },

        /**
         * Executes the specified function once for every window in the group, passing each
         * window as the only parameter. Returning false from the function will stop the iteration.
         * @param {Function} fn The function to execute for each item
         * @param {Object} scope (optional) The scope in which to execute the function
         */
        each : function(fn, scope){
            for(var id in list){
                if(list.hasOwnProperty(id)){
                    if(fn.call(scope || list[id], list[id]) === false){
                        return;
                    }
                }
            }
        }
    };
};

/**
 * @class Ext.air.NativeWindowManager
 * @extends Ext.air.NativeWindowGroup
 * 
 * Collection of all NativeWindows created.
 * 
 * @singleton
 */
Ext.air.NativeWindowManager = new Ext.air.NativeWindowGroup();