ComboBox show/hide popup callbacks

Hi guys,
would it be possible to add a callback (a lambda function maybe?) whenever the popup menu of a ComboBox is shown or hidden?
Cheers :slight_smile:

Hi masshacker

I had the same problem,I want to add a callback when I click on the ComboBox, can you tell me how you do it? Thanks!

+1

Hi there,
it’s simple: I didn’t. Unfortunately there is no way to attach something to those events without modifying the JUCE code, so at the time I ended up not using ComboBoxes at all, but rather a combination of buttons and popup menus.
It sucks, I know :smiling_face_with_tear:

Edit: if you just need to detect mouse clicks you could probably attach as a MouseListener through the addMouseListener function. In my case that didn’t help though, because I really wanted callbacks for the popup display events.

adding your own lambda to a derived class from juce::ComboBox is trivial though.

class ComboBoxWithCallback : public juce::ComboBox
{
public:
    ComboBoxWithCallback()
    {
        addMouseListener (this, true);
    }
    
    ~ComboBoxWithCallback(){}
    
    void mouseDown (const juce::MouseEvent& e) override
    {
        if (onPopupActive and isPopupActive())
            onPopupActive();
        
        juce::ComboBox::mouseDown (e);
    }
    
    std::function<void()> onPopupActive;
    
private:
    
    //==============================================================================
    JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ComboBoxWithCallback)

    
};

and call your callback with its lambda

auto comboBox { std::make_unique<ComboBoxWithCallback>() };
comboBox->onPopupActive = [this]
{
    // YOUR CALLBACK HERE
};



Yeah, subclassing can probably be a workaround, but after a while you may start getting tired of having yet another class to maintain…