Why can't I get PopupMenu.containsCommandItem() to work?

If I understand correctly, containsCommandItem() is supposed to tell you whether a given PopupMenu contains an item with a given CommandID. But I can’t get it to return true under any circumstances.

Here is a simple test:

    ApplicationCommandManager testManager;
    auto testMenu = PopupMenu{};

    auto testItem = PopupMenu::Item{};  // create a sample menu item and set its commandID to 5.
    testItem.itemID = 5;                // (using ints here instead of enums for simplicity)
    testMenu.addItem(testItem);         // add the item to the menu

    testMenu.containsCommandItem(5);    // Why does this returns false?


    testMenu.addCommandItem(&testManager, 6, "test");  // Trying addCommandItem() instead...

    testMenu.containsCommandItem(6);    // ...but it still returns false.

What am I getting wrong here?

I figured out what was happening with this problem, but now I’m stuck on another.

The problem was that the PopupMenu::Item has to have its commandManager term specified in order for containsCommandItem() to return positive. Hence the following should work:

ApplicationCommandManager testManager;
auto testMenu = PopupMenu{};

auto testItem = PopupMenu::Item{};
testItem.commandManager = &testManager;     // <-- this line was previously missing
testItem.itemID = 5;
testMenu.addItem(testItem);

testMenu.containsCommandItem(5);            // <-- now returns positive.

New problem: when I supply the commandManager as above, it changes the behavior of the ModalComponentManager::Callback that I’m using to perform the command. The originatingComponent term of the InvocationInfo object is now null when it is picked up by the perform() function of the ApplicationCommandTarget, despite the fact that I’m setting it inside the callback. Can anyone think why this might be?

I figured out what was going wrong last night: supplying the commandManager field of the PopupMenu::Item means that the menu command is sent automatically, before the ModalComponentManager::Callback I’m using gets there. This kind of makes sense, but it means that I have to leave this field blank.

Which brings me back to the original problem: I still can’t find a way of querying a PopupMenu to see if it contains a particular CommandID, (unless I first register it with a commandManager, which I have just established that I don’t want to do).

The JUCE code in question is line 1771 of juce_PopupMenu.cpp:

bool PopupMenu::containsCommandItem (const int commandID) const
{
    for (auto* mi : items)
        if ((mi->itemID == commandID && mi->commandManager != nullptr)    // <-- why must a commandManager have been specified in order for me to use this function?
              || (mi->subMenu != nullptr && mi->subMenu->containsCommandItem (commandID)))
            return true;

    return false;
}

What is the reason for the && mi->commandManager != nullptr bit? Is there no way of querying a menu to see if it contains a specific command, when I haven’t specified the commandManager?