| Adobe AIR 1.1 |
|
|
Managing windowsYou use the properties and methods of the NativeWindow class to manage the appearance, behavior, and life cycle of desktop windows. Getting a NativeWindow instanceTo manipulate a window, you must first get the window instance. You can get a window instance from one of the following places:
Activating, showing, and hiding windowsTo activate a window, call the NativeWindow activate() method. Activating a window brings the window to the front, gives it keyboard and mouse focus, and, if necessary, makes it visible by restoring the window or setting the visible property to true. Activating a window does not change the ordering of other windows in the application. Calling the activate() method causes the window to dispatch an activate event. To show a hidden window without activating it, set the visible property to true. This brings the window to the front, but will not assign the focus to the window. To hide a window from view, set its visible property to false. Hiding a window suppresses the display of both the window, any related task bar icons, and, on Mac OS X, the entry in the Windows menu. Note: On Mac OS X, it is not possible to completely
hide a minimized window that has a dock icon. If the visible property
is set to false on a minimized window, the dock
icon for the window is still displayed. If the user clicks the icon,
the window is restored to a visible state and displayed.
Changing the window display orderAIR provides several methods for directly changing the display order of windows. You can move a window to the front of the display order or to the back; you can move a window above another window or behind it. At the same time, the user can reorder windows by activating them. You can keep a window in front of other windows by setting its alwaysInFront property to true. If more than one window has this setting, then the display order of these windows is sorted among each other, but they are always sorted above windows which have alwaysInFront set to false. Windows in the top-most group are also displayed above windows in other applications, even when the AIR application is not active. Because this behavior can be disruptive to a user, setting alwaysInFront to true should only be done when necessary and appropriate. Examples of justified uses include:
Note: AIR does not enforce proper use of the alwaysInFront property.
However, if your application disrupts a user’s workflow, it is likely
to be consigned to that same user’s trash can.
The NativeWindow class provides the following properties and methods for setting the display order of a window relative to other windows:
Note: If a window is hidden (visible is false)
or minimized, then calling the display order methods has no effect.
Closing a windowTo close a window, use the NativeWindow.close() method. Closing a window unloads the contents of the window, but if other objects have references to this content, the content objects will not be destroyed. The NativeWindow.close() method executes asynchronously, the application that is contained in the window continues to run during the closing process. The close method dispatches a close event when the close operation is complete. The NativeWindow object is still technically valid, but accessing most properties and methods on a closed window generates an IllegalOperationError. You cannot reopen a closed window. Check the closed property of a window to test whether a window has been closed. To simply hide a window from view, set the NativeWindow.visible property to false. If the Nativeapplication.autoExit property is true, which is the default, then the application exits when its last window closes. Allowing cancellation of window operationsWhen a window uses system chrome, user interaction with the window can be canceled by listening for, and canceling the default behavior of the appropriate events. For example, when a user clicks the system chrome close button, the closing event is dispatched. If any registered listener calls the preventDefault() method of the event, then the window does not close. When a window does not use system chrome, notification events for intended changes are not automatically dispatched before the change is made. Hence, if you call the methods for closing a window, changing the window state, or set any of the window bounds properties, the change cannot be canceled. To notify components in your application before a window change is made, your application logic can dispatch the relevant notification event using the dispatchEvent() method of the window. function onCloseCommand(event){
var closingEvent = new air.Event(air.Event.CLOSING,true,true);
dispatchEvent(closingEvent);
if(!closingEvent.isDefaultPrevented()){
win.close();
}
}
The dispatchEvent() method returns false if the event preventDefault() method is called by a listener. However, it can also return false for other reasons, so it is better to explicitly use the isDefaultPrevented() method to test whether the change should be canceled. Maximizing, minimizing, and restoring a windowTo maximize the window, use the NativeWindow maximize() method. window.nativeWindow.maximize(); To minimize the window, use the NativeWindow minimize() method. window.nativeWindow.minimize(); To restore the window (that is, return it to the size that it was before it was either minimized or maximized), use the NativeWindow restore() method. window.nativeWindow.restore(); Note: The behavior that results from maximizing an
AIR window is different from the Mac OS X standard behavior. Rather
than toggling between an application-defined “standard” size and
the last size set by the user, AIR windows toggle between the size last
set by the application or user and the full usable area of the screen.
Example: Minimizing, maximizing, restoring and closing a windowThe following short HTML page demonstrates the NativeWindow maximize(), minimize(), restore(), and close() methods: <html>
<head>
<title>Change Window Display State</title>
<script src="AIRAliases.js"/>
<script type="text/javascript">
function onMaximize(){
window.nativeWindow.maximize();
}
function onMinimize(){
window.nativeWindow.minimize();
}
function onRestore(){
window.nativeWindow.restore();
}
function onClose(){
window.nativeWindow.close();
}
</script>
</head>
<body>
<h1>AIR window display state commands</h1>
<button onClick="onMaximize()">Maximize</button>
<button onClick="onMinimize()">Minimize</button>
<button onClick="onRestore()">Restore</button>
<button onClick="onClose()">Close</button>
</body>
</html>
Resizing and moving a windowWhen a window uses system chrome, the chrome provides drag controls for resizing the window and moving around the desktop. If a window does not use system chrome you must add your own controls to allow the user to resize and move the window. Note: To resize or move a window, you must first obtain
a reference to the NativeWindow instance. For information about
how to obtain a window reference, see Getting a NativeWindow instance.
Resizing a windowTo resize a window, use the NativeWindow startResize() method. When this method is called from a mouseDown event, the resizing operation is driven by the mouse and completes when the operating system receives a mouseUp event. When calling startResize(), you pass in an argument that specifies the edge or corner from which to resize the window. Moving a windowTo move a window without resizing it, use the NativeWindow startMove() method. Like the startResize() method, when the startMove() method is called from a mouseDown event, the move process is mouse-driven and completes when the operating system receives a mouseUp event. For more information about the startResize() and startMove() methods, see the Adobe AIR Language Reference for HTML Developers (http://www.adobe.com/go/learn_air_html_jslr). Example: Resizing and moving windowsThe following example shows how to initiate resizing and moving operations on a window: <html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script src="AIRAliases.js"/>
<script type="text/javascript">
function onResize(type){
nativeWindow.startResize(type);
}
function onNativeMove(){
nativeWindow.startMove();
}
</script>
<style type="text/css" media="screen">
.drag {
width:200px;
height:200px;
margin:0px auto;
padding:15px;
border:1px dashed #333;
background-color:#eee;
}
.resize {
background-color:#FF0000;
padding:10px;
}
.left {
float:left;
}
.right {
float:right;
}
</style>
<title>Move and Resize the Window</title>
</head>
<body>
<div class="resize left" onmousedown="onResize(air.NativeWindowResize.TOP_LEFT)">Drag to resize</div>
<div class="resize right" onmousedown="onResize(air.NativeWindowResize.TOP_RIGHT)">Drag to resize</div>
<div class="drag" onmousedown="onNativeMove()">Drag to move</div>
<div class="resize left" onmousedown="onResize(air.NativeWindowResize.BOTTOM_LEFT)">Drag to resize</div>
<div class="resize right" onmousedown="onResize(air.NativeWindowResize.BOTTOM_RIGHT)">Drag to resize</div>
</body>
</html>
|