Always Make First Element in popupMenu visible?

Using the following code, I create a popupmenu, add it to the DOM, and
populate it. Upon first appearance, the first element is visible.
If I scroll the menu items down, then close the popup, upon
the next appearance, it’s positioned where it was closed,
even though it’s been re-built.
How do I always set the menu item list so that the first
element is visible?

code:
var popup = document.getElementById(“edgewise-popMenu”) ; // already exist?..

if (popup) { // yes…
while (popup.firstChild) { // clear old items…
popup.removeChild(popup.firstChild) ;
}
}
else { // new…
popup = document.createElement(“menupopup”) ;
popup.setAttribute(“id”, “edgewise-popMenu”) ;

  var popupSet = document.getElementById("mainPopupSet") ;  // add to DOM...
  popupSet.appendChild(popup) ;

}

// add all EW Actions to menu…

for (var i = 0; i < EdgeWiseOV.actions.length; i++) { // for each EW Action…
var item = document.createElement(‘menuitem’) ;
item.setAttribute(‘label’, EdgeWiseOV.actions[i].visual) ;
item.setAttribute(‘value’, EdgeWiseOV.actions[i].code) ;
item.setAttribute(‘tooltiptext’, EdgeWiseOV.actions[i].tip) ;
item.addEventListener(“command”, function (itemEvt) { // make it clickable…
EdgeWiseOV.doClickRun(itemEvt.originalTarget.getAttribute(“value”)) ;
},
false ) ;
popup.appendChild(item) ;

}

popup.openPopupAtScreen( 1, 29, false) ; // show to user

code end

Thanks to anyone…
Paul

A menupopup contains an anonymous arrowscrollbox. In fact that is all it contains. The arrowscrollbox then contains all the menuitems. You can manually adjust the position of the arrowscrollbox in the popupshowing event handler. You would probably want to use the scrollTo() method of the ScrollBoxObject property of the arrowscrollbox. There is an example here:
http://mxr.mozilla.org/mozilla-central/source/toolkit/content/tests/chrome/window_largemenu.xul#106

1 Like

Thanks for the quick reply.
Works beautifully.

Paul