Suggested Fix to have BurgerMenuComponent display shortcutKeys

I was experimenting with the BurgerMenuComponent, and noticed that it does NOT display shortcutKeyDescriptions. I’m not sure why. Is it that shortcutKeys are considered superfluous in a mobile app? But you could use a BurgerMenuComponent in a non-mobile implementation, right? Or to have a mobile and non-mobile version be the same?

In any case, I discovered that PopupMenu::ItemComponent::updateShortcutKeyDescription() is NOT called in this case.

It seemed impossible to call that directly given the way the code is written. (Maybe I’m wrong…) But in any case, simply copying that code into the BurgerMenuComponent solves the issue.

This is the JUCE Menus Demo. It has shortcut keys assigned but they do not show up in the burger menu. Before:

After:

The code the way it is now:

void BurgerMenuComponent::addMenuBarItemsForMenu (PopupMenu& menu, int menuIdx)
{
    for (PopupMenu::MenuItemIterator it (menu); it.next();)
    {
        auto& item = it.getItem();

        if (item.isSeparator)
            continue;
        
         if (hasSubMenu (item))
            addMenuBarItemsForMenu (*item.subMenu, menuIdx);
        else
            rows.add (Row {false, menuIdx, it.getItem()});
    }
}

Suggested fix (or something like it):

void BurgerMenuComponent::addMenuBarItemsForMenu (PopupMenu& menu, int menuIdx)
{
    for (PopupMenu::MenuItemIterator it (menu); it.next();)
    {
        auto& item = it.getItem();

        if (item.isSeparator)
            continue;
        
        // ==========================================
        // add shortcutKeyDescriptions
        // copied from PopupMenu::ItemComponent::updateShortcutKeyDescription()
        if (item.commandManager != nullptr
            && item.itemID != 0
            && item.shortcutKeyDescription.isEmpty())
        {
            String shortcutKey;
            
            for (auto& keypress : item.commandManager->getKeyMappings()
                 ->getKeyPressesAssignedToCommand (item.itemID))
            {
                auto key = keypress.getTextDescriptionWithIcons();
                
                if (shortcutKey.isNotEmpty())
                    shortcutKey << ", ";
                
                if (key.length() == 1 && key[0] < 128)
                    shortcutKey << "shortcut: '" << key << '\'';
                else
                    shortcutKey << key;
            }
            
            item.shortcutKeyDescription = shortcutKey.trim();
        }
        // ==========================================

        if (hasSubMenu (item))
            addMenuBarItemsForMenu (*item.subMenu, menuIdx);
        else
            rows.add (Row {false, menuIdx, it.getItem()});
    }
}

Of course, it would be better to just call the PopupMenu::ItemComponent::updateShortcutKeyDescription() function, if it were accessible.

1 Like