Point tpos = position.getLocation(pop.trigger, pop.pointer);
Point mpos = origin.resolve(0, 0, width, height, new Point());
// figure out the best place to put the menu, in screen coordinates; starting with
// the requested popup position
Rectangle bounds = new Rectangle(tpos.x - mpos.x, tpos.y - mpos.y, width, height);
// make sure the menu lies inside the requested bounds if the menu doesn't do
// that itself
if (pop.menu.automaticallyConfine()) {
confine(pop.bounds, bounds);
// fudge is the number of pixels around the menu that we don't need to avoid
// TODO: can we get the menu's Background's insets?
float fudge = 2;
// TODO: do we need any of this finger avoidance stuff if the popup is not
// relative to the pointer? E.g. a combo box with its menu off to the right
// keep the bounds from overlapping the position
if (bounds.width > fudge * 2 && bounds.height > fudge * 2) {
Rectangle ibounds = new Rectangle(bounds);
ibounds.grow(-fudge, -fudge);
// set up the fingerprint
float fingerRadius = touch().hasTouch() ? 10 : 3;
IPoint fingerPos = pop.pointer == null ? tpos : pop.pointer;
Rectangle fingerBox = new Rectangle(
fingerPos.x() - fingerRadius, fingerPos.y() - fingerRadius,
fingerRadius * 2, fingerRadius * 2);
// try and place the menu so it isn't under the finger
if (!avoidPoint(pop.bounds, ibounds, fingerBox)) {
log.warning("Oh god, menu doesn't fit", "menu", pop.menu);
}
bounds.setLocation(ibounds.x() - fudge, ibounds.y() - fudge);
}
}
// save a copy of bounds in screen coordinates
Rectangle screenBounds = new Rectangle(bounds);
// relocate to layer coordinates
bounds.setLocation(Layer.Util.screenToLayer(elems.layer, bounds.x, bounds.y));
// set the menu bounds