diff --git a/modules/juce_gui_basics/menus/juce_PopupMenu.cpp b/modules/juce_gui_basics/menus/juce_PopupMenu.cpp index ec1534a960..d630e8a7da 100644 --- a/modules/juce_gui_basics/menus/juce_PopupMenu.cpp +++ b/modules/juce_gui_basics/menus/juce_PopupMenu.cpp @@ -82,7 +82,7 @@ struct ItemComponent : public Component parent.addAndMakeVisible (this); - shortcutKeyDescription = getShortcutKeyDescription(); + updateShortcutKeyDescription(); int itemW = 80; int itemH = 16; @@ -118,7 +118,7 @@ struct ItemComponent : public Component item.isTicked, hasSubMenu (item), item.text, - shortcutKeyDescription, + item.shortcutKeyDescription, item.image, getColour (item)); } @@ -145,14 +145,13 @@ struct ItemComponent : public Component } PopupMenu::Item item; - String shortcutKeyDescription; private: // NB: we use a copy of the one from the item info in case we're using our own section comp ReferenceCountedObjectPtr customComp; bool isHighlighted; - String getShortcutKeyDescription() const + void updateShortcutKeyDescription() { if (item.commandManager != nullptr && item.itemID != 0) { @@ -173,16 +172,14 @@ private: shortcutKey << key; } - return shortcutKey.trim(); + item.shortcutKeyDescription = shortcutKey.trim(); } - - return String(); } String getTextForMeasurement() const { - return shortcutKeyDescription.isNotEmpty() ? item.text + " " + shortcutKeyDescription - : item.text; + return item.shortcutKeyDescription.isNotEmpty() ? item.text + " " + item.shortcutKeyDescription + : item.text; } JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ItemComponent) @@ -1245,6 +1242,7 @@ PopupMenu::Item::Item (const Item& other) image (other.image != nullptr ? other.image->createCopy() : nullptr), customComponent (other.customComponent), commandManager (other.commandManager), + shortcutKeyDescription (other.shortcutKeyDescription), colour (other.colour), isEnabled (other.isEnabled), isTicked (other.isTicked), @@ -1261,6 +1259,7 @@ PopupMenu::Item& PopupMenu::Item::operator= (const Item& other) image = (other.image != nullptr ? other.image->createCopy() : nullptr); customComponent = other.customComponent; commandManager = other.commandManager; + shortcutKeyDescription = other.shortcutKeyDescription; colour = other.colour; isEnabled = other.isEnabled; isTicked = other.isTicked; diff --git a/modules/juce_gui_basics/menus/juce_PopupMenu.h b/modules/juce_gui_basics/menus/juce_PopupMenu.h index 492aac2c73..5e89ffd1e0 100644 --- a/modules/juce_gui_basics/menus/juce_PopupMenu.h +++ b/modules/juce_gui_basics/menus/juce_PopupMenu.h @@ -137,6 +137,14 @@ public: /** A command manager to use to automatically invoke the command, or nullptr if none is specified. */ ApplicationCommandManager* commandManager; + /** An optional string describing the shortcut key for this item. + This is only used for displaying at the right-hand edge of a menu item - the + menu won't attempt to actually catch or process the key. If you supply a + commandManager parameter then the menu will attempt to fill-in this field + automatically. + */ + String shortcutKeyDescription; + /** A colour to use to draw the menu text. By default this is transparent black, which means that the LookAndFeel should choose the colour. */