New more modern PopupMenu style

you should be able to create your PopupMenu and simply copy it to the ComboBox’ one like that :

PopupMenu menu;
menu.addItem (...);
*comboBox.getRootMenu() = menu;
1 Like

yup, that’s what I ended up doing!

also this:

    int menuID {1};
    auto* popupMenu = classChooser.getRootMenu();
    popupMenu->addItem(PopupMenu::Item("Custom date")
                       .setTicked(false)
                       .setAction([this]()
    {
    //....
    })
                       .setID(menuID++));
    
    classChooser.setSelectedId(1);

that makes it auto-select the first item in your menu.

1 Like

Would someone mind putting the example from the original post into the docs? It’s a better place than the ephemeral forum… :wink:

2 Likes

This might be a breaking change, but can we get isTickable as well as isTicked. That way the look and feel could draw an empty box when not ticked.

4 Likes

I wanted the “isTickable” option as well. I’m dropping some hackish code here that acheive precisely that to help people whom are struggling with this as well:

class UntickableItemCustomCallback final : public juce::PopupMenu::CustomCallback
{
public:
	UntickableItemCustomCallback() = delete;
	explicit UntickableItemCustomCallback(const std::function<void()>& inCallback);

	bool menuItemTriggered() override;

private:
    std::function<void()> _callback;
};

class CustomPopupMenu : public juce::PopupMenu
{
public:
	void addUntickableItem(juce::String inText, std::function<void()> inCallback);
	void addUntickableItem(juce::String inText, bool inIsEnabled, std::function<void()> inCallback);
};
UntickableItemCustomCallback::UntickableItemCustomCallback(const std::function<void()>& inCallback) : _callback(inCallback)
{
}

bool UntickableItemCustomCallback::menuItemTriggered()
{
	_callback();
    return false;
}

void CustomPopupMenu::addUntickableItem(const juce::String inText, const std::function<void()> inCallback)
{
    addUntickableItem(inText, true, inCallback);
}

void CustomPopupMenu::addUntickableItem(juce::String inText, const bool inIsEnabled, std::function<void()> inCallback)
{
    juce::PopupMenu::Item i;
	i.itemID = -2; //Because -1 is juce's default. As ticking one item with a given ID ticks every items with the same one, ticking default juce's items was ticking all of our "untickable" items.
	i.text = std::move(inText);
    i.isEnabled = inIsEnabled;
	i.customCallback = juce::ReferenceCountedObjectPtr(new UntickableItemCustomCallback(inCallback));
    juce::PopupMenu::addItem(std::move(i));
}
1 Like