1
votes

How do I get the available browser width and height in Flex (not Flash)?

There is Capabilities.screenResolutionY and screenResolutionX that give the total monitor resolution and there is stage.stageWidth and stage.stageHeight but in the documentation it says "Calling the stageWidth property of a Stage object throws an exception for any caller that is not in the same security sandbox as the Stage owner (the main SWF file)". AND at times the stage property is null (how do you even get it on non-display objects like if you were in some random class?).

So how would I do this the Flex way and not throw errors (my application may be loaded by another at some point)?

~~ UPDATE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
So I've been doing some testing and it looks like,

Rectangle(systemManager.getVisibleApplicationRect()).width
Rectangle(systemManager.getVisibleApplicationRect()).height 

give me the information I need. These are even available at Application initialize.

Also,

systemManager.stage.stageWidth
systemManager.stage.stageHeight

give me the same information.

The systemManager is a property on the Application and every UIComponent so unless I'm in one of those classes (and I think in UIComponent's case it's a child of the Application) I still need to find a way to get to a reference to the systemManager in other types of classes.

~~ UPDATE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
So freakazoid_em commented that I can use FlexGlobals.topLevelApplication to get the reference to the systemManager. With that I can get a reference to the visible browser width and height from anywhere in my Flex application (at anytime).

1
What height are you looking for" The height and width of the browser window? Or the "viewable" browser area (no toolbars or tabs scrollbars or chrome)? stageHeight and stageWidth should give you the size of the "Flash Player Rectangle" but that may not have any relation to the browser. You should have no trouble accessing the stage in Flex after the applicationComplete event fires. Before that, nothing is on the stage, and the stage is not accessible. - JeffryHouser
how about using the this.explicitHeight and this.explicitWidth on your main application (Main.mxml) ? It should give you the width and height of your application when embedded in another html page - Majid Laissi
@Flextras - The viewable area of the browser ie the browser not including the chrome. The viewport. - 1.21 gigawatts
To get the ISystemManager instance from the top-level application you can use FlexGlobals.topLevelApplication. This property is set by the topmost Application object. - eddipedia

1 Answers

0
votes
var visibleWidth:Number = FlexGlobals.topLevelApplication.systemManager.getVisibleApplicationRect().width;
var visibleHeight:Number = FlexGlobals.topLevelApplication.systemManager.getVisibleApplicationRect().height;

// this seems to return the same values
var visibleWidth:Number = FlexGlobals.topLevelApplication.systemManager.stage.stageWidth;
var visibleHeight:Number = FlexGlobals.topLevelApplication.systemManager.stage.stageHeight;