[flexcoders] How to measure a Menu before calling show()?

2007-02-05 Thread karlgold
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



Re: [flexcoders] How to measure a Menu before calling show()?

2007-02-05 Thread Doug McCune
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