PopUpMenu on Button Hover


#1

I am working with a PopUpMenu and have done various ways of doing this but can't seem to get it just right. All in all, I would like:

  • PopUpMenu
    • Appears on Mouseover of a specific button
    • Disappears on mouseleave of parent component of button
    • The popupmenu is outside parent component of button

I got the furthest using a timer every 500ms to ping the button to see isMouseOver() and to see if the popupmenu is a nullptr. If the popumenu is a nullptr, then I create it (the popupmenu is a scoped pointer) and show it (have tried all four methods). This at least satisfies the appearing on mouseover of a specific button.

The problem is that no matter which method I use like showMenuAsync, it doesn't clear and set the menu to nullptr. I have to actually press a button on the menu to get it to close. One thing I tried to do was:


void classClassy::timerCallback()
{
    if (addImgButton->isMouseOver() && m_AddImgButtonList == nullptr)
    {
        m_AddImgButtonList = new PopupMenu();
        if (m_AddImgButtonList != nullptr)
        {
            juce::PopupMenu::Options m_options;
            m_options.withTargetScreenArea(juce::Rectangle<int>(100,100,600,600));
            m_AddImgButtonList->addItem(1, "cats", true, false);
            m_AddImgButtonList->showMenuAsync(m_options, ModalCallbackFunction::forComponent(popupMenuFinishedCallback, this));
        }
    }
    else if (m_AddImgButtonList != nullptr && isMouseOverOrDragging())
    {
        m_AddImgButtonList->clear();
        m_AddImgButtonList = nullptr;
    }
}
void classClassy::popupMenuFinishedCallback(int result, BottomMenuComponent* thiscomponent)
{
    thiscomponent->closePopUpMenu();
    thiscomponent->repaint();
}
void classClassy::closePopUpMenu()
{
    if (m_AddImgButtonList != nullptr)
    {
        m_AddImgButtonList->clear();
        m_AddImgButtonList = nullptr;
    }
}

How would I go about doing this properly?


#2

Have you tried subclassing Button then in your button class using mouseEnter(), mouseExit() and in the latter using dismissAllActiveMenus() ?

Rail


#3

I haven't attempted to do it yet, mainly because my fear is that the scope of the PopUpMenu would then be only within the space designated for the button - resulting in the popupmenu being cut-off and unable to be placed elsewhere. I had something similar with the ToolTipWindow.. I could put the commands into the parent and reference the parent..

The problem, it seems, is that even running asynchronously, that JUCE doesn't recognize mouse enter and mouse leave on a component - for instance, my parent component..


#4

You can always have the button make a call to a parent method if you give the button a pointer to the parent.

Rail