Browse Source

Made TooltipWindow::getTipFor() a public virtual method

tags/2021-05-28
jules 8 years ago
parent
commit
9ac2fae469
2 changed files with 30 additions and 32 deletions
  1. +21
    -26
      modules/juce_gui_basics/windows/juce_TooltipWindow.cpp
  2. +9
    -6
      modules/juce_gui_basics/windows/juce_TooltipWindow.h

+ 21
- 26
modules/juce_gui_basics/windows/juce_TooltipWindow.cpp View File

@@ -24,22 +24,18 @@
============================================================================== ==============================================================================
*/ */
TooltipWindow::TooltipWindow (Component* const parentComp, const int delayMs)
TooltipWindow::TooltipWindow (Component* parentComp, int delayMs)
: Component ("tooltip"), : Component ("tooltip"),
lastComponentUnderMouse (nullptr),
millisecondsBeforeTipAppears (delayMs),
mouseClicks (0), mouseWheelMoves (0),
lastCompChangeTime (0), lastHideTime (0),
reentrant (false)
millisecondsBeforeTipAppears (delayMs)
{ {
if (Desktop::getInstance().getMainMouseSource().canHover())
startTimer (123);
setAlwaysOnTop (true); setAlwaysOnTop (true);
setOpaque (true); setOpaque (true);
if (parentComp != nullptr) if (parentComp != nullptr)
parentComp->addChildComponent (this); parentComp->addChildComponent (this);
if (Desktop::getInstance().getMainMouseSource().canHover())
startTimer (123);
} }
TooltipWindow::~TooltipWindow() TooltipWindow::~TooltipWindow()
@@ -102,14 +98,13 @@ void TooltipWindow::displayTip (Point<int> screenPos, const String& tip)
} }
} }
String TooltipWindow::getTipFor (Component* const c)
String TooltipWindow::getTipFor (Component& c)
{ {
if (c != nullptr
&& Process::isForegroundProcess()
if (Process::isForegroundProcess()
&& ! ModifierKeys::getCurrentModifiers().isAnyMouseButtonDown()) && ! ModifierKeys::getCurrentModifiers().isAnyMouseButtonDown())
{ {
if (TooltipClient* const ttc = dynamic_cast<TooltipClient*> (c))
if (! c->isCurrentlyBlockedByAnotherModalComponent())
if (auto* ttc = dynamic_cast<TooltipClient*> (&c))
if (! c.isCurrentlyBlockedByAnotherModalComponent())
return ttc->getTooltip(); return ttc->getTooltip();
} }
@@ -128,24 +123,24 @@ void TooltipWindow::hideTip()
void TooltipWindow::timerCallback() void TooltipWindow::timerCallback()
{ {
Desktop& desktop = Desktop::getInstance();
const MouseInputSource mouseSource (desktop.getMainMouseSource());
const unsigned int now = Time::getApproximateMillisecondCounter();
auto& desktop = Desktop::getInstance();
auto mouseSource = desktop.getMainMouseSource();
auto now = Time::getApproximateMillisecondCounter();
Component* const newComp = ! mouseSource.isTouch() ? mouseSource.getComponentUnderMouse() : nullptr;
const String newTip (getTipFor (newComp));
const bool tipChanged = (newTip != lastTipUnderMouse || newComp != lastComponentUnderMouse);
auto* newComp = mouseSource.isTouch() ? nullptr : mouseSource.getComponentUnderMouse();
auto newTip = newComp != nullptr ? getTipFor (*newComp) : String();
bool tipChanged = (newTip != lastTipUnderMouse || newComp != lastComponentUnderMouse);
lastComponentUnderMouse = newComp; lastComponentUnderMouse = newComp;
lastTipUnderMouse = newTip; lastTipUnderMouse = newTip;
const int clickCount = desktop.getMouseButtonClickCounter();
const int wheelCount = desktop.getMouseWheelMoveCounter();
const bool mouseWasClicked = (clickCount > mouseClicks || wheelCount > mouseWheelMoves);
auto clickCount = desktop.getMouseButtonClickCounter();
auto wheelCount = desktop.getMouseWheelMoveCounter();
bool mouseWasClicked = (clickCount > mouseClicks || wheelCount > mouseWheelMoves);
mouseClicks = clickCount; mouseClicks = clickCount;
mouseWheelMoves = wheelCount; mouseWheelMoves = wheelCount;
const Point<float> mousePos (mouseSource.getScreenPosition());
const bool mouseMovedQuickly = mousePos.getDistanceFrom (lastMousePos) > 12;
auto mousePos = mouseSource.getScreenPosition();
bool mouseMovedQuickly = mousePos.getDistanceFrom (lastMousePos) > 12;
lastMousePos = mousePos; lastMousePos = mousePos;
if (tipChanged || mouseWasClicked || mouseMovedQuickly) if (tipChanged || mouseWasClicked || mouseMovedQuickly)
@@ -174,7 +169,7 @@ void TooltipWindow::timerCallback()
// appear after a timeout.. // appear after a timeout..
if (newTip.isNotEmpty() if (newTip.isNotEmpty()
&& newTip != tipShowing && newTip != tipShowing
&& now > lastCompChangeTime + (unsigned int) millisecondsBeforeTipAppears)
&& now > lastCompChangeTime + (uint32) millisecondsBeforeTipAppears)
{ {
displayTip (mousePos.roundToInt(), newTip); displayTip (mousePos.roundToInt(), newTip);
} }


+ 9
- 6
modules/juce_gui_basics/windows/juce_TooltipWindow.h View File

@@ -85,6 +85,11 @@ public:
/** Can be called to manually hide the tip if it's showing. */ /** Can be called to manually hide the tip if it's showing. */
void hideTip(); void hideTip();
/** Asks a component for its tooltip.
This can be overridden if you need custom lookup behaviour or to modify the strings.
*/
virtual String getTipFor (Component&);
//============================================================================== //==============================================================================
/** A set of colour IDs to use to change the colour of various aspects of the tooltip. /** A set of colour IDs to use to change the colour of various aspects of the tooltip.
@@ -121,19 +126,17 @@ public:
private: private:
//============================================================================== //==============================================================================
Point<float> lastMousePos; Point<float> lastMousePos;
Component* lastComponentUnderMouse;
Component* lastComponentUnderMouse = nullptr;
String tipShowing, lastTipUnderMouse; String tipShowing, lastTipUnderMouse;
int millisecondsBeforeTipAppears; int millisecondsBeforeTipAppears;
int mouseClicks, mouseWheelMoves;
unsigned int lastCompChangeTime, lastHideTime;
bool reentrant;
int mouseClicks = 0, mouseWheelMoves = 0;
unsigned int lastCompChangeTime = 0, lastHideTime = 0;
bool reentrant = false;
void paint (Graphics&) override; void paint (Graphics&) override;
void mouseEnter (const MouseEvent&) override; void mouseEnter (const MouseEvent&) override;
void timerCallback() override; void timerCallback() override;
void updatePosition (const String&, Point<int>, Rectangle<int>); void updatePosition (const String&, Point<int>, Rectangle<int>);
static String getTipFor (Component*);
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (TooltipWindow) JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (TooltipWindow)
}; };

Loading…
Cancel
Save