Thanks Doug.
Interestingly enough, the public method UIComponent.validateNow()
does exactly that (frameworks/source/mx/core/UIComponent.as):
public function validateNow():void
{
UIComponentGlobals.layoutManager.validateClient(this);
}
But calling that still doesn't seem to set the width and height of
the menu.
I poked around the framework code a bit more, but before I got
anywhere it occurred to me to just do this:
var itemMenu:Menu = Menu.createMenu(null, menuData, true);
itemMenu.iconField = "icon";
itemMenu.show(x, y);
itemMenu.hide();
var x:Number = e.stageX;
var y:Number = e.stageY;
var width:Number = itemMenu.getExplicitOrMeasuredWidth();
var height:Number = itemMenu.getExplicitOrMeasuredHeight();
if (x + width > this.stage.width) {
// show menu to the left of the cursor
x -= width;
}
if (y + height > this.stage.height) {
// show menu above the cursor
y -= height;
}
itemMenu.show(x, y);
That seems to work fine without any noticeable flicker (at least on
my 3-year-old laptop).
And yes, your scrollable menu looks great and I plan to use it!
Regards,
Karl
--- In [email protected], Doug McCune <[EMAIL PROTECTED]> wrote:
>
> If you're comfortable looking at the source code for the framework
> classes, then check out the source of MenuBar.as at line 1574, see
the
> showMenu() method. That has logic that repositions the menu so that
it
> remains on the stage. You could use that code and re-purpose it a
little
> to get it working for your situation. It looks like the key line in
> there is this:
>
> UIComponentGlobals.layoutManager.validateClient(menu, true);
>
> which will validate the size of the Menu before you then use the
width
> and height to check the x and y position.
>
> Also, see if my Scrollable Menu component might fit your needs:
> http://dougmccune.com/blog/2007/01/26/even-better-scrollable-menus-
for-flex/
>
> -Doug
>
> karlgold wrote:
> >
> > I want to display a popup menu when the user clicks in a certain
spot
> > in application:
> >
> > private function showContextMenu(e:MouseEvent):void {
> >
> > var menuData:Array = new Array();
> > // populate menuData
> >
> > var itemMenu:Menu = Menu.createMenu(null, menuData, true);
> > itemMenu.show(e.stageX, e.stageY);
> > }
> >
> > This works fine. However, if the user clicks close to the bottom
or
> > right margin of the application, the menu is cut off.
> >
> > I'd like to do something like this:
> >
> > private function showContextMenu(e:MouseEvent):void {
> >
> > var menuData:Array = new Array();
> > // populate menuData
> >
> > var itemMenu:Menu = Menu.createMenu(null, menuData, true);
> > // how to tell a Menu to measure its width and height before
> > // displaying it?
> >
> > var x:Number = e.stageX;
> > var y:Number = e.stageY;
> >
> > if (x + itemMenu.width > this.stage.width) {
> > // show menu to the left of the cursor
> > x -= itemMenu.width;
> > }
> >
> > if (y + itemMenu.height > this.stage.height) {
> > // show menu above the cursor
> > y -= itemMenu.height;
> > }
> >
> > itemMenu.show(x, y);
> > }
> >
> > It appears that the width and height are 0 prior to calling show
().
> > Is there any way to measure the menu before drawing it?
> >
> > Thanks,
> >
> > Karl
> >
> >
>