Flash menu name when command is invoked with custom item


#1

I have a coloured menu item with a keyboard shortcut attached to it.
I implemented a custom component and add it to the PopupMenu via the addCustomItem.

class ColouredCommandItem : public PopupMenu::CustomComponent
{
public:
	ColouredCommandItem(int commandID_, ApplicationCommandManager* commandManager_,
			const Colour& itemTextColour)
	: CustomComponent(false), commandID(commandID_), commandManager(commandManager_),
	  textColour(itemTextColour), defLookAndFeel(LookAndFeel::getDefaultLookAndFeel())
	{
		jassert (commandManager != nullptr && commandID != 0);

		const ApplicationCommandInfo* const registeredInfo = commandManager->getCommandForID (commandID);

		if (registeredInfo != nullptr)
		{
			ApplicationCommandInfo info (*registeredInfo);
			ApplicationCommandTarget* const target = commandManager->getTargetForCommand (commandID, info);

			displayName = info.shortName;
			DBG(info.flags);

			Array <KeyPress> keyPresses (commandManager_->getKeyMappings()
					->getKeyPressesAssignedToCommand (commandID));

			for (int i = 0; i < keyPresses.size(); ++i)
			{
				const String key (keyPresses.getReference(i).getTextDescriptionWithIcons());

				if (shortcutKey.isNotEmpty())
					shortcutKey << ", ";

				if (key.length() == 1 && key[0] < 128)
					shortcutKey << "shortcut: '" << key << '\'';
				else
					shortcutKey << key;
			}

			shortcutKey = shortcutKey.trim();

		}
	}
	~ColouredCommandItem(){}

	void getIdealSize(int& idealWidth, int& idealHeight)
	{
		// tells the menu how big we'd like to be..
		idealWidth = 200;
		idealHeight = 22;
	}

	void paint (Graphics& g)
	{
		if(isItemHighlighted())
			g.fillAll (findColour (PopupMenu::highlightedBackgroundColourId));
		else
			g.fillAll(findColour(PopupMenu::backgroundColourId));

		g.setFont (defLookAndFeel.getPopupMenuFont());
		if(isItemHighlighted())
			g.setColour (findColour(PopupMenu::highlightedTextColourId));
		else
			g.setColour (textColour);

		g.drawFittedText (displayName,
				25, 0, getWidth() - 8, getHeight(),
				Justification::left, 3);

		g.setFont (13.0f);
		g.drawFittedText (shortcutKey,
				0, 0, getWidth() - 5, getHeight(),
				Justification::right, 3);
	}

	void mouseUp(const MouseEvent& e)
	{
		commandManager->invokeDirectly(commandID, true);
	}
private:
	int commandID;
	Colour textColour;
	ApplicationCommandManager* commandManager;
	LookAndFeel& defLookAndFeel;
	String displayName;
	String shortcutKey;
};

How can I have the menubar name flashing when the command is invoked?

Or is there a more simple solution to have a coloured cammand item?

Thanks,
Peter


#2

Did you see PopupMenu::addColouredItem?

And the best way to flash it would be to use the command manager to run it - that should take care of flashing the menu bar when it’s invoked.


#3

I saw the PopupMenu::addColouredItem but how can I then connect the command manager to the item?

The PopupMenu::addCommandItem is also quite handy because it displays the Keyboard shortcut automatically in the menu.

Would it be possible that the addCommandItem has an additional Colour parameter? :slight_smile:
Or another function like addColouredCommandItem?

Thanks ,
Peter


#4

Hmm, good point. I’ve been meaning to give that class a bit of an overhaul, and make the per-item options more useable.


#5

That would be really nice. Looking forward.