Fast workaround for mouseOver issue with custom item in PopupMenu on OS X


Hello everyone !

I’ve seen that some of you have (or had) trouble with the mouse over detection when using custom items in a PopupMenu on OS X. Jules proposed to create our own workaround based on Timers, just as he did for “classic” items of the PopupMenu, to detect the mouse position and transmit it to the appropriate custom item.

This is maybe the best way to go, but for those who, like I did, only need to know which item is highlighted, I have found a little workaround that should be faster to implement that the one with Timers. Let’s say that we are in this case :

class MyItem : juce::Component;

where MyItem is the item we will add to our PopupMenu. Once you have instanced it and added it to the PopupMenu, you can see that its parent component is a NormalComponentWrapper, which is defined like this in juce_PopupMenu.cpp :

class NormalComponentWrapper : public PopupMenu::CustomComponent

and if you take a look at the CustomComponent class, you will see that it contains the information of whether the component is highlighted (so has the mouse over it) or not.

So when you want to know if the mouse is over your instance of MyItem, you only have to do this check :

    juce::PopupMenu::CustomComponent* parent = dynamic_cast<juce::PopupMenu::CustomComponent*>(getParentComponent());
    bool isMouseOver = parent->isItemHighlighted();
    //Do whatever you want then

For example, it could be useful in the paint() method of MyItem, where you should draw the highlight effect by yourself.

Well, this is maybe not the sexiest way to do this but I hope it could help !

Don’t hesitate to correct this post if you find something cleaner :wink:

Have a nice day !

PS : I do not know why on OS X the highlight info is not transmitted (when it is done perfectly on Windows), event though it is well computed. If you have any hint, maybe Jules would be interested !