What is the proper way of getting notified when the user selects the same item in a ComboBox that is currently selected?
What I tried so far with no success:
Overriding comboBoxChanged or valueChanged or handleAsyncUpdate in a subclassed ComboBox. All of those methods get only called if the value actually changed.
What I tried with partial success:
Passing a PopupMenu::CustomCallback that handles menuItemTriggered() to every PopupMenu::Item in the ComboBox. Problem here is that the callback in fact is called every time an item is selected but it has no knowledge of the actual item that was selected. Now asking the ComboBox which item is currently selected will still return the old value because it will only get updated after this callback.
From the description of menuItemTriggered() I would have assumed that always returning true from it will always force ComboBox::comboBoxChanged() to be called but that turned out to be a misassumption (Probably because another check for change is run after exitModalState):
true if the itemID should be sent to the exitModalState method, or false if it should send 0, indicating no further action should be taken
Now I guess I could make the PopupMenu::CustomCallback::menuItemTriggered() smart enough to know which item was selected if the callback is called but that would IMO imply having a PopupMenu::CustomCallback for every item in the ComboBox which seems overly complicated to me and I fear I’m missing the easy way.
I came up against this same problem today, when making a menu for plug-in presets.
Selecting a preset from the menu the first time would load the new parameter values just fine, but then selecting the same preset again would do nothing. This is a problem when a user modifies a parameter value in between! You want them to be able to revert to the stored preset values by selecting it again – but in its default behavior, ComboBox doesn’t do anything if a selection is the same as the previous one.
I looked into making the ComboBox an AudioProcessorValueTreeState::Listener, so it would be updated of changed parameter values, and thereby clear/reset its preset selection when that happened. However, for several reasons that I won’t delve into for now, that seemed like a bad idea.
In the end I figured out a stupid hack, which I’ll share here below. But I would appreciate any feedback from folks wiser in these matters…
In the Editor constructor, after addAndMakeVisible(presetMenu) and whatever else you might do there, add this:
Now whenever you make a selection from the ComboBox, it immediately resets itself to the “nothingSelected” state (i.e. index 0), but it changes the content of the “nothingSelected” text so that it looks like your selection is still made.
OK, when I use this, after making my selection, the code is indeed processed, but the item text is grayed out and the item is not checked in the popup menu.
I assume the greyed out text is just a LookAndFeel issue which draws the textWhenNothingSelected in a different colour, but when I open the popup menu, nothing is checked as being selected. Maybe that’s a trade-off you decided was OK?
For example, I simply process my function first, then do what you say:
Correct, in the case I was using this for, I didn’t care about that trade-off.
Yes, the greyed out text can be avoided by changing the LookAndFeel method, I overrode the drawComboBoxTextWhenNothingSelected to avoid that.
Drawing the checkmark is also handled in a LookAndFeel method (drawPopupMenuItem), but when you call setSelectedId(0), that means the ComboBox no longer “knows” which item was selected… so it has no way to pass that on to the LookAndFeel.
I like this idea, and just tried implementing it, but it’s not working right off the bat. It does allow for the same item to be reselected, so it’s good on that front.
However, the ComboBox now seems unable to properly keep track of the items in the PopupMenu. At first, no checkmark is shown in the list, and then as soon as I select an item, a checkmark appears next to every item in the list. Also, the ComboBox’s text display (its Label) only shows the name of the first item, no matter which item is selected.
Note that the ComboBox::addItem method includes a newItemId argument, and that gets passed to the PopupMenu via PopupMenu::addItem [4/6]. But here, where you’re using PopupMenu::addItem [2/6] instead, it doesn’t take an Id argument, and I’m not clear yet how to work around that. (Unfortunately there’s not a version of PopupMenu::addItem that takes both an Id argument and a std::function argument.)