1 | /* |
---|
2 | * Ext JS Library 0.30 |
---|
3 | * Copyright(c) 2006-2009, Ext JS, LLC. |
---|
4 | * licensing@extjs.com |
---|
5 | * |
---|
6 | * http://extjs.com/license |
---|
7 | */ |
---|
8 | |
---|
9 | /** |
---|
10 | * @class Ext.air.NativeWindow |
---|
11 | * @extends Ext.air.NativeObservable |
---|
12 | * |
---|
13 | * Wraps the AIR NativeWindow class to give an Ext friendly API. <br/><br/>This class also adds |
---|
14 | * automatic state management (position and size) for the window (by id) and it can be used |
---|
15 | * for easily creating "minimize to system tray" for the main window in your application.<br/><br/> |
---|
16 | * |
---|
17 | * Note: Many of the config options for this class can only be applied to NEW windows. Passing |
---|
18 | * in an existing instance of a window along with those config options will have no effect. |
---|
19 | * |
---|
20 | * @constructor |
---|
21 | * @param {Object} config |
---|
22 | */ |
---|
23 | Ext.air.NativeWindow = function(config){ |
---|
24 | Ext.apply(this, config); |
---|
25 | |
---|
26 | /** |
---|
27 | * @type String |
---|
28 | */ |
---|
29 | this.id = this.id || Ext.uniqueId(); |
---|
30 | |
---|
31 | this.addEvents( |
---|
32 | /** |
---|
33 | * @event close |
---|
34 | * @param {Object} e The air event object |
---|
35 | */ |
---|
36 | 'close', |
---|
37 | /** |
---|
38 | * @event closing |
---|
39 | * @param {Object} e The air event object |
---|
40 | */ |
---|
41 | 'closing', |
---|
42 | /** |
---|
43 | * @event move |
---|
44 | * @param {Object} e The air event object |
---|
45 | */ |
---|
46 | 'move', |
---|
47 | /** |
---|
48 | * @event moving |
---|
49 | * @param {Object} e The air event object |
---|
50 | */ |
---|
51 | 'moving', |
---|
52 | /** |
---|
53 | * @event resize |
---|
54 | * @param {Object} e The air event object |
---|
55 | */ |
---|
56 | 'resize', |
---|
57 | /** |
---|
58 | * @event resizing |
---|
59 | * @param {Object} e The air event object |
---|
60 | */ |
---|
61 | 'resizing', |
---|
62 | /** |
---|
63 | * @event displayStateChange |
---|
64 | * @param {Object} e The air event object |
---|
65 | */ |
---|
66 | 'displayStateChange', |
---|
67 | /** |
---|
68 | * @event displayStateChanging |
---|
69 | * @param {Object} e The air event object |
---|
70 | */ |
---|
71 | 'displayStateChanging' |
---|
72 | ); |
---|
73 | |
---|
74 | Ext.air.NativeWindow.superclass.constructor.call(this); |
---|
75 | |
---|
76 | if(!this.instance){ |
---|
77 | var options = new air.NativeWindowInitOptions(); |
---|
78 | options.systemChrome = this.chrome; |
---|
79 | options.type = this.type; |
---|
80 | options.resizable = this.resizable; |
---|
81 | options.minimizable = this.minimizable; |
---|
82 | options.maximizable = this.maximizable; |
---|
83 | options.transparent = this.transparent; |
---|
84 | |
---|
85 | this.loader = window.runtime.flash.html.HTMLLoader.createRootWindow(false, options, false); |
---|
86 | if (this.file) { |
---|
87 | this.loader.load(new air.URLRequest(this.file)); |
---|
88 | } else { |
---|
89 | this.loader.loadString(this.html || ''); |
---|
90 | } |
---|
91 | |
---|
92 | |
---|
93 | this.instance = this.loader.window.nativeWindow; |
---|
94 | }else{ |
---|
95 | this.loader = this.instance.stage.getChildAt(0); |
---|
96 | } |
---|
97 | |
---|
98 | var provider = Ext.state.Manager; |
---|
99 | var b = air.Screen.mainScreen.visibleBounds; |
---|
100 | |
---|
101 | var state = provider.get(this.id) || {}; |
---|
102 | provider.set(this.id, state); |
---|
103 | |
---|
104 | var win = this.instance; |
---|
105 | |
---|
106 | var width = Math.max(state.width || this.width, 100); |
---|
107 | var height = Math.max(state.height || this.height, 100); |
---|
108 | |
---|
109 | var centerX = b.x + ((b.width/2)-(width/2)); |
---|
110 | var centerY = b.y + ((b.height/2)-(height/2)); |
---|
111 | |
---|
112 | var x = !Ext.isEmpty(state.x, false) ? state.x : (!Ext.isEmpty(this.x, false) ? this.x : centerX); |
---|
113 | var y = !Ext.isEmpty(state.y, false) ? state.y : (!Ext.isEmpty(this.y, false) ? this.y : centerY); |
---|
114 | |
---|
115 | win.width = width; |
---|
116 | win.height = height; |
---|
117 | win.x = x; |
---|
118 | win.y = y; |
---|
119 | |
---|
120 | win.addEventListener('move', function(){ |
---|
121 | if(win.displayState != air.NativeWindowDisplayState.MINIMIZED && win.width > 100 && win.height > 100) { |
---|
122 | state.x = win.x; |
---|
123 | state.y = win.y; |
---|
124 | } |
---|
125 | }); |
---|
126 | win.addEventListener('resize', function(){ |
---|
127 | if (win.displayState != air.NativeWindowDisplayState.MINIMIZED && win.width > 100 && win.height > 100) { |
---|
128 | state.width = win.width; |
---|
129 | state.height = win.height; |
---|
130 | } |
---|
131 | }); |
---|
132 | |
---|
133 | Ext.air.NativeWindowManager.register(this); |
---|
134 | this.on('close', this.unregister, this); |
---|
135 | |
---|
136 | /** |
---|
137 | * @cfg {Boolean} minimizeToTray |
---|
138 | * True to enable minimizing to the system tray. Note: this should only be applied |
---|
139 | * to the primary window in your application. A trayIcon is required. |
---|
140 | */ |
---|
141 | if(this.minimizeToTray){ |
---|
142 | this.initMinimizeToTray(this.trayIcon, this.trayMenu); |
---|
143 | } |
---|
144 | |
---|
145 | }; |
---|
146 | |
---|
147 | Ext.extend(Ext.air.NativeWindow, Ext.air.NativeObservable, { |
---|
148 | |
---|
149 | /** |
---|
150 | * @cfg {air.NativeWindow} instance |
---|
151 | * The native window instance to wrap. If undefined, a new window will be created. |
---|
152 | */ |
---|
153 | |
---|
154 | /** |
---|
155 | * @cfg {String} trayIcon |
---|
156 | * The icon to display when minimized in the system tray |
---|
157 | */ |
---|
158 | /** |
---|
159 | * @cfg {NativeMenu} trayMenu |
---|
160 | * Menu to display when the tray icon is right clicked |
---|
161 | */ |
---|
162 | /** |
---|
163 | * @cfg {String} trayTip |
---|
164 | * Tooltip for the tray icon |
---|
165 | */ |
---|
166 | |
---|
167 | /** |
---|
168 | * @cfg {String} chrome |
---|
169 | * The native window chrome (defaults to 'standard', can also be 'none'). |
---|
170 | */ |
---|
171 | chrome: 'standard', // can also be none |
---|
172 | /** |
---|
173 | * @cfg {String} type |
---|
174 | * The native window type - normal, utility or lightweight. (defaults to normal) |
---|
175 | */ |
---|
176 | type: 'normal', // can be normal, utility or lightweight |
---|
177 | /** |
---|
178 | * @cfg {Number} width |
---|
179 | */ |
---|
180 | width:600, |
---|
181 | /** |
---|
182 | * @cfg {Number} height |
---|
183 | */ |
---|
184 | height:400, |
---|
185 | /** |
---|
186 | * @cfg {Boolean} resizable |
---|
187 | */ |
---|
188 | resizable: true, |
---|
189 | /** |
---|
190 | * @cfg {Boolean} minimizable |
---|
191 | */ |
---|
192 | minimizable: true, |
---|
193 | /** |
---|
194 | * @cfg {Boolean} maximizable |
---|
195 | */ |
---|
196 | maximizable: true, |
---|
197 | /** |
---|
198 | * @cfg {Boolean} transparent |
---|
199 | */ |
---|
200 | transparent: false, |
---|
201 | |
---|
202 | /** |
---|
203 | * Returns the air.NativeWindow instance |
---|
204 | * @return air.NativeWindow |
---|
205 | */ |
---|
206 | getNative : function(){ |
---|
207 | return this.instance; |
---|
208 | }, |
---|
209 | |
---|
210 | /** |
---|
211 | * Returns the x/y coordinates for centering the windw on the screen |
---|
212 | * @return {x: Number, y: Number} |
---|
213 | */ |
---|
214 | getCenterXY : function(){ |
---|
215 | var b = air.Screen.mainScreen.visibleBounds; |
---|
216 | return { |
---|
217 | x: b.x + ((b.width/2)-(this.width/2)), |
---|
218 | y: b.y + ((b.height/2)-(this.height/2)) |
---|
219 | }; |
---|
220 | }, |
---|
221 | |
---|
222 | /** |
---|
223 | * Shows the window |
---|
224 | */ |
---|
225 | show :function(){ |
---|
226 | if(this.trayed){ |
---|
227 | Ext.air.SystemTray.hideIcon(); |
---|
228 | this.trayed = false; |
---|
229 | } |
---|
230 | this.instance.visible = true; |
---|
231 | }, |
---|
232 | |
---|
233 | /** |
---|
234 | * Shows and activates the window |
---|
235 | */ |
---|
236 | activate : function(){ |
---|
237 | this.show(); |
---|
238 | this.instance.activate(); |
---|
239 | }, |
---|
240 | |
---|
241 | /** |
---|
242 | * Hides the window |
---|
243 | */ |
---|
244 | hide :function(){ |
---|
245 | this.instance.visible = false; |
---|
246 | }, |
---|
247 | |
---|
248 | /** |
---|
249 | * Closes the window |
---|
250 | */ |
---|
251 | close : function(){ |
---|
252 | this.instance.close(); |
---|
253 | }, |
---|
254 | |
---|
255 | /** |
---|
256 | * Returns true if this window is minimized |
---|
257 | * @return Boolean |
---|
258 | */ |
---|
259 | isMinimized :function(){ |
---|
260 | return this.instance.displayState == air.NativeWindowDisplayState.MINIMIZED; |
---|
261 | }, |
---|
262 | |
---|
263 | /** |
---|
264 | * Returns true if this window is maximized |
---|
265 | * @return Boolean |
---|
266 | */ |
---|
267 | isMaximized :function(){ |
---|
268 | return this.instance.displayState == air.NativeWindowDisplayState.MAXIMIZED; |
---|
269 | }, |
---|
270 | |
---|
271 | /** |
---|
272 | * Moves the window to the passed xy and y coordinates |
---|
273 | * @param {Number} x |
---|
274 | * @param {Number} y |
---|
275 | */ |
---|
276 | moveTo : function(x, y){ |
---|
277 | this.x = this.instance.x = x; |
---|
278 | this.y = this.instance.y = y; |
---|
279 | }, |
---|
280 | /** |
---|
281 | * Enter full-screen mode for the window. |
---|
282 | * @param {Boolean} nonInteractive (optional) Boolean flag to allow the full screen window to be interactive or not. By default this is false. |
---|
283 | * Example Code: |
---|
284 | * var win = new Ext.air.NativeWindow({instance: Ext.air.NativeWindow.getRootWindow()}); |
---|
285 | * win.fullscreen(); |
---|
286 | */ |
---|
287 | fullscreen: function(nonInteractive) { |
---|
288 | var SDS = runtime.flash.display.StageDisplayState; |
---|
289 | this.instance.stage.displayState = nonInteractive ? SDS.FULL_SCREEN : SDS.FULL_SCREEN_INTERACTIVE; |
---|
290 | }, |
---|
291 | |
---|
292 | bringToFront: function() { |
---|
293 | this.instance.orderToFront(); |
---|
294 | }, |
---|
295 | |
---|
296 | bringInFrontOf: function(win) { |
---|
297 | this.instance.orderInFrontOf(win.instance ? win.instance : win); |
---|
298 | }, |
---|
299 | |
---|
300 | sendToBack: function() { |
---|
301 | this.instance.orderToBack(); |
---|
302 | }, |
---|
303 | |
---|
304 | sendBehind: function(win) { |
---|
305 | this.instance.orderInBackOf(win.instance ? win.instance : win); |
---|
306 | }, |
---|
307 | |
---|
308 | |
---|
309 | /** |
---|
310 | * @param {Number} width |
---|
311 | * @param {Number} height |
---|
312 | */ |
---|
313 | resize : function(width, height){ |
---|
314 | this.width = this.instance.width = width; |
---|
315 | this.height = this.instance.height = height; |
---|
316 | }, |
---|
317 | |
---|
318 | unregister : function(){ |
---|
319 | Ext.air.NativeWindowManager.unregister(this); |
---|
320 | }, |
---|
321 | |
---|
322 | initMinimizeToTray : function(icon, menu){ |
---|
323 | var tray = Ext.air.SystemTray; |
---|
324 | |
---|
325 | tray.setIcon(icon, this.trayTip); |
---|
326 | this.on('displayStateChanging', function(e){ |
---|
327 | if(e.afterDisplayState == 'minimized'){ |
---|
328 | e.preventDefault(); |
---|
329 | this.hide(); |
---|
330 | tray.showIcon(); |
---|
331 | this.trayed = true; |
---|
332 | } |
---|
333 | }, this); |
---|
334 | |
---|
335 | tray.on('click', function(){ |
---|
336 | this.activate(); |
---|
337 | }, this); |
---|
338 | |
---|
339 | if(menu){ |
---|
340 | tray.setMenu(menu); |
---|
341 | } |
---|
342 | } |
---|
343 | }); |
---|
344 | |
---|
345 | /** |
---|
346 | * Returns the first opened window in your application |
---|
347 | * @return air.NativeWindow |
---|
348 | * @static |
---|
349 | */ |
---|
350 | Ext.air.NativeWindow.getRootWindow = function(){ |
---|
351 | return air.NativeApplication.nativeApplication.openedWindows[0]; |
---|
352 | }; |
---|
353 | |
---|
354 | /** |
---|
355 | * Returns the javascript "window" object of the first opened window in your application |
---|
356 | * @return Window |
---|
357 | * @static |
---|
358 | */ |
---|
359 | Ext.air.NativeWindow.getRootHtmlWindow = function(){ |
---|
360 | return Ext.air.NativeWindow.getRootWindow().stage.getChildAt(0).window; |
---|
361 | }; |
---|
362 | |
---|
363 | /** |
---|
364 | * @class Ext.air.NativeWindowGroup |
---|
365 | * |
---|
366 | * A collection of NativeWindows. |
---|
367 | */ |
---|
368 | Ext.air.NativeWindowGroup = function(){ |
---|
369 | var list = {}; |
---|
370 | |
---|
371 | return { |
---|
372 | /** |
---|
373 | * @param {Object} win |
---|
374 | */ |
---|
375 | register : function(win){ |
---|
376 | list[win.id] = win; |
---|
377 | }, |
---|
378 | |
---|
379 | /** |
---|
380 | * @param {Object} win |
---|
381 | */ |
---|
382 | unregister : function(win){ |
---|
383 | delete list[win.id]; |
---|
384 | }, |
---|
385 | |
---|
386 | /** |
---|
387 | * @param {String} id |
---|
388 | */ |
---|
389 | get : function(id){ |
---|
390 | return list[id]; |
---|
391 | }, |
---|
392 | |
---|
393 | /** |
---|
394 | * Closes all windows |
---|
395 | */ |
---|
396 | closeAll : function(){ |
---|
397 | for(var id in list){ |
---|
398 | if(list.hasOwnProperty(id)){ |
---|
399 | list[id].close(); |
---|
400 | } |
---|
401 | } |
---|
402 | }, |
---|
403 | |
---|
404 | /** |
---|
405 | * Executes the specified function once for every window in the group, passing each |
---|
406 | * window as the only parameter. Returning false from the function will stop the iteration. |
---|
407 | * @param {Function} fn The function to execute for each item |
---|
408 | * @param {Object} scope (optional) The scope in which to execute the function |
---|
409 | */ |
---|
410 | each : function(fn, scope){ |
---|
411 | for(var id in list){ |
---|
412 | if(list.hasOwnProperty(id)){ |
---|
413 | if(fn.call(scope || list[id], list[id]) === false){ |
---|
414 | return; |
---|
415 | } |
---|
416 | } |
---|
417 | } |
---|
418 | } |
---|
419 | }; |
---|
420 | }; |
---|
421 | |
---|
422 | /** |
---|
423 | * @class Ext.air.NativeWindowManager |
---|
424 | * @extends Ext.air.NativeWindowGroup |
---|
425 | * |
---|
426 | * Collection of all NativeWindows created. |
---|
427 | * |
---|
428 | * @singleton |
---|
429 | */ |
---|
430 | Ext.air.NativeWindowManager = new Ext.air.NativeWindowGroup(); |
---|