Popup Menu Hit Testing


#1

Hey All,

I’m currently experimenting with Popup menus and am wondering what your opinions are on how to best implement a “hit test” feature for the Popup menu? In essence, I have a Popupmenu that is tied to a DrawableButton using .showat() function call. However, once the popupmenu is created, the user has to actually click somewhere outside of the popupmenu to close it or click on an option within the popup menu.

What I’m trying to implement is for the popup menu to disappear when the mouse is not over the DrawableButton that the popup menu is attached to or when the mouse is not over the actual Popup menu. Effectively, I only want the popup menu to appear when the mouse is nearby where it appears and disappears when it isn’t.

I’m not entirely sure how to best implement this? Is there a viable function in the LookAndFeel methods that I could override for Popup menu that can easily accomplish this? I haven’t been able to find a solid location where it would make the most sense to implement this type of feature.


#2

You’d probably want to build some kind of hack with a global mouse-listener (see the Desktop class). But you’re going to have to deal with a lot of messy edge-cases, so be very sure that this really is the behaviour that you need.


#3

Thanks Jules, I figured as much. I was thinking of a dirty solution using a timer possibly and seeing if the popup menu is open, if the mouse is around it or not? I don’t need it to be “instantly” closed - I just need it to close eventually. I have a flag already when the popup menu is open so I can base my timer off that but I just need a hit test function to see if the mouse is either on the drawable button or the popup menu. Thoughts on this?


#4

Sure, you can do it with a timer, but like I said, this is the kind of thing where you’ll hit all kinds of annoying edge-cases. Personally I’d avoid it unless it really is crucial to your UX.