Lazy ComboBox

I try to have a callback to populate a ComboBox menu, but overriding showPopup() is tricky, because the default implementation uses a lot of private information.

It would be great to have a callback:

std::function<void (juce::PopupMenu*)> onShowPopupCallback;

My use case is that I have toggles in the menu which need to get the correct state on open.

combo.onShowPopupCallback = [this](juce::Popup* popup)
{
    popup->addItem (juce::PopupMenu::Item ("toggle")
        .setAction ([this]{ state = !state; })
        .setChecked (state));
};

I can supply a PR if needed.

Cheers!

It is actually a “single” line fix and one member:

void ComboBox::showPopup()
{
    if (! menuActive)
        menuActive = true;

    auto menu = currentMenu;

    if (onShowPopupCallback)
        onShowPopupCallback (menu);
[...]

And the member ofc.

    /** This callback will be called when the popup menu is about to be shown. */
    std::function<void(PopupMenu&)> onShowPopupCallback;

The user can now

  • add stuff to the menu
  • manipulate the existing menu

And the best, it is non-destructive, because menu is a copy.