Skip to content

Instantly share code, notes, and snippets.

@troygilbert
Created April 15, 2010 04:25
Show Gist options
  • Save troygilbert/366677 to your computer and use it in GitHub Desktop.
Save troygilbert/366677 to your computer and use it in GitHub Desktop.
AutoSizedWindow
package
{
import flash.display.NativeWindow;
import flash.geom.Point;
import mx.core.ScrollPolicy;
import mx.core.Window;
public class AutoSizedWindow extends Window
{
protected var _autoWidth:Boolean = true;
protected var _autoHeight:Boolean = true;
/** Auto-size width. **/
public function get autoWidth():Boolean { return _autoWidth; }
public function set autoWidth(value:Boolean):void
{
_autoWidth = value;
invalidateSize();
}
/** Auto-size height. **/
public function get autoHeight():Boolean { return _autoHeight; }
public function set autoHeight(value:Boolean):void
{
_autoHeight = value;
invalidateSize();
}
/** Measure. **/
override protected function measure():void
{
super.measure();
if (nativeWindow.closed) return;
var chromeWidth:Number = nativeWindow.width - systemManager.stage.stageWidth;
var chromeHeight:Number = nativeWindow.height - systemManager.stage.stageHeight;
var idealWidth:Number = measuredWidth + chromeWidth;
var idealHeight:Number = measuredHeight + chromeHeight;
horizontalScrollPolicy = autoWidth ? ScrollPolicy.OFF : ScrollPolicy.AUTO;
verticalScrollPolicy = autoHeight ? ScrollPolicy.OFF : ScrollPolicy.AUTO;
if (autoWidth && autoHeight)
{
nativeWindow.minSize = new Point(idealWidth, idealHeight);
nativeWindow.maxSize = new Point(idealWidth, idealHeight);
width = idealWidth;
height = idealHeight;
}
else if (autoWidth && !autoHeight)
{
nativeWindow.minSize = new Point(idealWidth, NativeWindow.systemMinSize.y);
nativeWindow.maxSize = new Point(idealWidth, Math.min(idealHeight, NativeWindow.systemMaxSize.y));
idealWidth += verticalScrollBar ? verticalScrollBar.width : 15; // this comes from mx/core/Window.as
width = idealWidth;
}
else if (!autoWidth && autoHeight)
{
nativeWindow.minSize = new Point(NativeWindow.systemMinSize.x, idealHeight);
nativeWindow.maxSize = new Point(Math.min(idealWidth, NativeWindow.systemMaxSize.x), idealHeight);
idealHeight += horizontalScrollBar ? horizontalScrollBar.height : 15; // this comes from mx/core/Window.as
height = idealHeight;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment