I have a few places in my app where I use a PopupMenu with submenus. When I get the result from the show() method there is no way to tell which submenu the item was selected from that I am aware of. What I have been doing up to this point is something like this:
Then I loop through my application objects that are in each section and store the object IDs in a map where the key is the section ID plus an index that is incremented for each item, and the value is my objects ID. Then I have to check the result to see what range it is in so that I can determine the section. KnownPluginList::addToMenu() deals with this using a menuIdBase variable. Am I missing something? Is there a more direct way to do this? It seems like it would be very useful if each menu item could carry a juce::var instance or something similar.
UPDATE:
Adding a juce::var to PopupMenu::Item, and PopupMenu::Item* getItemForId (int itemId) const noexcept; from the ComboBox class did the trick for me. Hopefully someone has a better idea so I don’t have to touch my local JUCE code.
I did look at the code and that is precisely what I was doing. To clarify my submenus contain items from lists that contain integer IDs that are insert IDs from a database. So without generating unique item IDs across all lists using the technique discussed I have no way of determining which item was selected. Thank you for the response. The code below shows the two approaches. If you have an app that has many instances of this pattern, implementing without the juce::var can get a little tedious. But I can see how this wouldn’t be something high on the list of priorities.
With the id of each sub-menu item (which I suppose is unique in its sub-menu) and the index of the sub-menu it should go it, you could encode an id that’s unique across the whole menu this way:
const int uniqueMenuId = uniqueIdInSubMenu * numSubMenus + subMenuIndex;
and, when an item is selected, you could get back those two values this way:
const int uniqueIdInSubMenu = uniqueMenuId / numSubMenus;
const int subMenuIndex = uniqueMenuId % numSubMenus
it is a little counter-intuitive to encode the sub-menu (that’s hierarchically more important) in the least significant part of the id, but aside from that it should just work.