Packageflash.display
Classpublic final class Screen
InheritanceScreen Inheritance EventDispatcher Inheritance Object

Language version: ActionScript 3.0
Runtime version: AIR 1.0

The Screen class provides information about the display screens available to this application.

Screens are independent desktop areas within a possibly larger "virtual desktop." The origin of the virtual desktop is the top-left corner of the operating-system-designated main screen. Thus, the coordinates for the bounds of an individual display screen may be negative. There may also be areas of the virtual desktop that are not within any of the display screens.

The Screen class includes static class members for accessing the available screen objects and instance members for accessing the properties of an individual screen. Screen information should not be cached since it can be changed by a user at any time.

Note that there is not necessarily a one-to-one correspondance between screens and the physical monitors attached to a computer. For example, two monitors may display the same screen.

You cannot instantiate the Screen class directly. Calls to the new Screen() constructor throw an ArgumentError exception.

View the examples.



Public Properties
 PropertyDefined by
  bounds : Rectangle
[read-only] The bounds of this screen.
Screen
  colorDepth : int
[read-only] The color depth of this screen (expressed in number of bits).
Screen
 Inheritedconstructor : Object
A reference to the class object or constructor function for a given object instance.
Object
  contentsScaleFactor : Number
[read-only] The scale factor of the screen
Screen
  mainScreen : Screen
[static][read-only] The primary display.
Screen
  mode : ScreenMode
The current screen mode of the Screen object.
Screen
  modes : Array
[read-only] The array of ScreenMode objects of the Screen object.
Screen
 Inheritedprototype : Object
[static] A reference to the prototype object of a class or function object.
Object
  safeArea : Rectangle
[read-only] The bounds of the 'safe' area on this screen in which content will always be visible.
Screen
  screens : Array
[static][read-only] The array of the currently available screens.
Screen
  visibleBounds : Rectangle
[read-only] The bounds of the area on this screen in which windows can be visible.
Screen
Public Methods
 MethodDefined by
 Inherited
addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void
Registers an event listener object with an EventDispatcher object so that the listener receives notification of an event.
EventDispatcher
 Inherited
Dispatches an event into the event flow.
EventDispatcher
  
[static] Returns the (possibly empty) set of screens that intersect the provided rectangle.
Screen
 Inherited
Checks whether the EventDispatcher object has any listeners registered for a specific type of event.
EventDispatcher
 Inherited
Indicates whether an object has a specified property defined.
Object
 Inherited
Indicates whether an instance of the Object class is in the prototype chain of the object specified as the parameter.
Object
 Inherited
Indicates whether the specified property exists and is enumerable.
Object
 Inherited
removeEventListener(type:String, listener:Function, useCapture:Boolean = false):void
Removes a listener from the EventDispatcher object.
EventDispatcher
 Inherited
Sets the availability of a dynamic property for loop operations.
Object
 Inherited
Returns the string representation of this object, formatted according to locale-specific conventions.
Object
 Inherited
Returns the string representation of the specified object.
Object
 Inherited
Returns the primitive value of the specified object.
Object
 Inherited
Checks whether an event listener is registered with this EventDispatcher object or any of its ancestors for the specified event type.
EventDispatcher
Events
 EventSummaryDefined by
 Inherited [broadcast event] Dispatched when the Flash Player or AIR application gains operating system focus and becomes active.EventDispatcher
 Inherited [broadcast event] Dispatched when the Flash Player or AIR application operating loses system focus and is becoming inactive.EventDispatcher
Property detail
boundsproperty
bounds:Rectangle  [read-only]

Language version: ActionScript 3.0
Runtime version: AIR 1.0

The bounds of this screen.

The screen location is relative to the virtual desktop.

On Linux systems that use certain window managers, this property returns the desktop bounds, not the screen's visible bounds.

Implementation
    public function get bounds():Rectangle

Example
The following example shows how to get the bounds of a screen (in this case, the primary display screen):
import flash.display.Screen;
import flash.geom.Rectangle;

var mainScreen:Screen = Screen.mainScreen;
var screenBounds:Rectangle = mainScreen.bounds;

colorDepthproperty 
colorDepth:int  [read-only]

Language version: ActionScript 3.0
Runtime version: AIR 1.0

The color depth of this screen (expressed in number of bits).

Implementation
    public function get colorDepth():int

Example
The following example shows how to get the color depth of a screen (in this case, the primary display screen):

var mainScreen:Screen = Screen.mainScreen;
var colors:uint = mainScreen.colorDepth;

contentsScaleFactorproperty 
contentsScaleFactor:Number  [read-only]

Language version: ActionScript 3.0
Runtime version: AIR 50.0

The scale factor of the screen

Implementation
    public function get contentsScaleFactor():Number
mainScreenproperty 
mainScreen:Screen  [read-only]

Language version: ActionScript 3.0
Runtime version: AIR 1.0

The primary display.

Implementation
    public static function get mainScreen():Screen

Example
The following example shows how to get the Screen object representing the "main" screen of this computer:

var primaryScreen:Screen = Screen.mainScreen;

modeproperty 
mode:ScreenMode  [read-write]

Language version: ActionScript 3.0
Runtime version: AIR 31.0

The current screen mode of the Screen object. The mode property indicates the current screen mode of the specific Screen device and can be set to change the current screen mode.

You can only set screen mode in full-screen mode. Setting it on applications not in full-screen mode throws an exception.

On mobile platforms, this property is not supported and returns null (and trying to set the property has no effect).

Implementation
    public function get mode():ScreenMode
    public function set mode(value:ScreenMode):void

Example
The following example shows how to set the mode of a screen (in this case, the primary display screen):
import flash.display.Screen;
import flash.display.ScreenMode;
import flash.display.StageDisplayState;

function FindBestModeIndex(modes:Array): int // this method is an example of an user defined function.
{ 
    return modes.length - 1; // algorithm for selection of the best mode is to be defined by the AS developer.
}

var primaryDisplay:Screen = Screen.mainScreen;
var oldScreenMode:ScreenMode = primaryDisplay.mode;
	
var modes:Array = primaryDisplay.modes;
var best_mode_index:uint = FindBestModeIndex(modes);

try {
	stage.displayState = StageDisplayState.FULL_SCREEN;
	// Setting of screen mode not in full-screen mode throws the kInvalidPropertyOutsideFullScreen exception.  
	primaryDisplay.mode = modes[best_mode_index]);
}
catch(error:Error){
	stage.displayState = StageDisplayState.NORMAL;
}

modesproperty 
modes:Array  [read-only]

Language version: ActionScript 3.0
Runtime version: AIR 31.0

The array of ScreenMode objects of the Screen object. The modes property indicates supported screen modes of the specific Screen device.

Note that on mobile devices where the mode property is not supported, this property will be null.

Implementation
    public function get modes():Array
safeAreaproperty 
safeArea:Rectangle  [read-only]

Language version: ActionScript 3.0
Runtime version: AIR 50.0

The bounds of the 'safe' area on this screen in which content will always be visible.

The safeArea of a screen excludes any cut-out features such as cameras/speakers at the top of mobile devices, or the borders of screens that may be impacted by a curved edge of the screen. On devices with rectangular corners and no cut-outs, the safeArea property returns the same value as the visibleBounds property.

Implementation
    public function get safeArea():Rectangle
screensproperty 
screens:Array  [read-only]

Language version: ActionScript 3.
Runtime version: AIR 1.0

The array of the currently available screens.

Modifying the returned array has no effect on the available screens.

Implementation
    public static function get screens():Array

Example
The following example shows how to get the array containing the available screens:

var screenArray:Array = Screen.screens;

visibleBoundsproperty 
visibleBounds:Rectangle  [read-only]

Language version: ActionScript 3.0
Runtime version: AIR 1.0

The bounds of the area on this screen in which windows can be visible.

The visibleBounds of a screen excludes the task bar (and other docked desk bars) on Windows, and excludes the menu bar and, depending on system settings, the dock on Mac OS X. On some Linux configurations, it is not possible to determine the visible bounds. In these cases, the visibleBounds property returns the same value as the screenBounds property.

Implementation
    public function get visibleBounds():Rectangle

Example
The following example shows how to get the usable bounds of a screen (in this case, the primary display screen):
import flash.display.Screen;
import flash.geom.Rectangle;

var mainScreen:Screen = Screen.mainScreen;
var screenBounds:Rectangle = mainScreen.visibleBounds;

Method detail
getScreensForRectangle()method
public static function getScreensForRectangle(rect:Rectangle):Array

Language version: ActionScript 3.0
Runtime version: AIR 1.0

Returns the (possibly empty) set of screens that intersect the provided rectangle.

Parameters
rect:Rectangle — A rectangle with coordinates relative to the origin of the virtual desktop, which is the top-left corner of the primary screen.

Returns
Array — An array of Screen objects containing the screens that contain any part of the area defined by the rect parameter.

Example
The following example shows how to get the array of screens containing at least part of a given rectangle:
import flash.display.Screen;
import flash.geom.Rectangle;

var rect:Rectangle = new Rectangle(-200, 100, 1000, 600);
var intersectedScreens:Array = Screen.getScreensForRectangle(rect);

Examples
examples\ScreenExample
The following example defines a DockingWindow class to create a window that docks to the sides of the screen. This task is accomplished by performing the following steps:
  1. Responding to keyboard events to determine the side of the screen on which to dock.
  2. Accessing the static Screen class method getScreensForRectangle() to get the Screen object for the screen upon which the window is currently displayed.
  3. Resetting the window bounds based on the screen dimensions.
  4. Redrawing the window content based on the new window dimensions.

Note, this class is intended to be used as the root class of an AIR application with the settings SystemChrome="none" and transparent="true". To use this class in a window with system chrome, you must take the chrome thickness and the minimum width of the window into account when calculating window location and size.


package
{
	import flash.display.Screen;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.KeyboardEvent;
	import flash.geom.Rectangle;
	import flash.ui.Keyboard;

	public class DockingWindow extends Sprite
	{
		private const dockedWidth:uint = 80;
		private const dockedHeight:uint = 80;
		
		public function DockingWindow():void{
			stage.align = StageAlign.TOP_LEFT;
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.addEventListener(KeyboardEvent.KEY_DOWN,onKey);
			dockLeft();
		}
		
		private function onKey(event:KeyboardEvent):void{
			switch(event.keyCode){
				case Keyboard.LEFT :
					dockLeft();
					break;
				case Keyboard.RIGHT :
					dockRight();
					break;
				case Keyboard.UP :
					dockTop();
					break;
				case Keyboard.DOWN :
					dockBottom();
					break;
				case Keyboard.SPACE :
					stage.nativeWindow.close();
			}	
		}
		
		public function dockLeft():void{
			var screen:Screen = getCurrentScreen();
			stage.nativeWindow.x = screen.visibleBounds.left;
			stage.nativeWindow.y = screen.visibleBounds.top;
			stage.nativeWindow.height = screen.visibleBounds.height;
			stage.stageWidth = dockedWidth;
			drawContent();
		}
		
		public function dockRight():void{
			var screen:Screen = getCurrentScreen();
			stage.nativeWindow.x = screen.visibleBounds.width - dockedWidth;			
			stage.nativeWindow.y = screen.visibleBounds.top;
			stage.stageWidth = dockedWidth;
			stage.nativeWindow.height = screen.visibleBounds.height;
			drawContent();
		}
		
		public function dockTop():void{
			var screen:Screen = getCurrentScreen();
			stage.nativeWindow.x = screen.visibleBounds.left;
			stage.nativeWindow.y = screen.visibleBounds.top;
			stage.nativeWindow.width = screen.visibleBounds.width;
			stage.stageHeight = dockedHeight;
			drawContent();
		}
		
		public function dockBottom():void{
			var screen:Screen = getCurrentScreen();
			stage.nativeWindow.x = screen.visibleBounds.left;
			stage.nativeWindow.y = screen.visibleBounds.height - dockedHeight;
			stage.nativeWindow.width = screen.visibleBounds.width;
			stage.stageHeight = dockedHeight;	
			drawContent();		
		}
		
		private function getCurrentScreen():Screen{
			return Screen.getScreensForRectangle(stage.nativeWindow.bounds)[0];
		}
		
		private function drawContent():void{
			const size:int = 60;
			const pad:int = 10;
			var numHSquares:int = Math.floor(stage.stageWidth/(size + pad));
			var numVSquares:int = Math.floor(stage.stageHeight/(size + pad));
			with (graphics){
				clear();
				lineStyle(1);
				beginFill(0x3462d5,.7);
				for(var i:int = 0; i < numHSquares; i++){
					for(var j:int = 0; j < numVSquares; j++){				
							drawRect((i * (size + pad)) + pad, (j * (size + pad)) + pad, size, size);
					}
				}
				endFill();
			}
		}
	}
}