Skip to content

Instantly share code, notes, and snippets.

@adamski
Last active August 13, 2019 14:51
Show Gist options
  • Save adamski/a97fd427e3551e61e79cd3fb7454f70d to your computer and use it in GitHub Desktop.
Save adamski/a97fd427e3551e61e79cd3fb7454f70d to your computer and use it in GitHub Desktop.
/*
==============================================================================
ButtonTooltipBubble.h
Created: 13 Aug 2019 2:51:47pm
Author: Adam Wilson
==============================================================================
This class is designed to add a tooltip to a Button or Button subclass.
Usage: Add and instantiate as a member in your class. E.g. for a button subclass:
```
ButtonTooltipBubble tooltip { *this };
```
or for a Button member:
```
ButtonTooltipBubble tooltip { myButton };
```
*/
#pragma once
#include "JuceHeader.h"
class ButtonTooltipBubble
: private MouseListener,
private Button::Listener,
private Timer
{
public:
ButtonTooltipBubble (Button& buttonToWatch) : watchedButton (buttonToWatch)
{
watchedButton.addMouseListener (this, true);
watchedButton.addListener (this);
if (! tooltipBubble->isOnDesktop())
{
tooltipBubble->addToDesktop(0);
tooltipBubble->setVisible (true);
tooltipBubble->setAllowedPlacement (BubbleComponent::BubblePlacement::below);
}
}
~ButtonTooltipBubble()
{
watchedButton.removeMouseListener (this);
}
void mouseEnter (const MouseEvent&) override
{
startTimer(1000);
}
void mouseExit (const MouseEvent&) override
{
tooltipBubble->setVisible (false);
stopTimer();
}
void buttonClicked (Button *button) override
{
tooltipBubble->setVisible (false);
stopTimer();
}
void timerCallback() override
{
auto attrString = AttributedString (watchedButton.getTooltip());
attrString.setColour (Colours::white);
tooltipBubble->showAt (&watchedButton, attrString, 2000);
stopTimer();
}
SharedResourcePointer<BubbleMessageComponent> tooltipBubble;
private:
Button& watchedButton;
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ButtonTooltipBubble);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment