diff --git a/modules/juce_gui_basics/windows/juce_TooltipWindow.cpp b/modules/juce_gui_basics/windows/juce_TooltipWindow.cpp index abf71124af..565ad0acca 100644 --- a/modules/juce_gui_basics/windows/juce_TooltipWindow.cpp +++ b/modules/juce_gui_basics/windows/juce_TooltipWindow.cpp @@ -24,22 +24,18 @@ ============================================================================== */ -TooltipWindow::TooltipWindow (Component* const parentComp, const int delayMs) +TooltipWindow::TooltipWindow (Component* parentComp, int delayMs) : 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); setOpaque (true); if (parentComp != nullptr) parentComp->addChildComponent (this); + + if (Desktop::getInstance().getMainMouseSource().canHover()) + startTimer (123); } TooltipWindow::~TooltipWindow() @@ -102,14 +98,13 @@ void TooltipWindow::displayTip (Point 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()) { - if (TooltipClient* const ttc = dynamic_cast (c)) - if (! c->isCurrentlyBlockedByAnotherModalComponent()) + if (auto* ttc = dynamic_cast (&c)) + if (! c.isCurrentlyBlockedByAnotherModalComponent()) return ttc->getTooltip(); } @@ -128,24 +123,24 @@ void TooltipWindow::hideTip() 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; 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; mouseWheelMoves = wheelCount; - const Point mousePos (mouseSource.getScreenPosition()); - const bool mouseMovedQuickly = mousePos.getDistanceFrom (lastMousePos) > 12; + auto mousePos = mouseSource.getScreenPosition(); + bool mouseMovedQuickly = mousePos.getDistanceFrom (lastMousePos) > 12; lastMousePos = mousePos; if (tipChanged || mouseWasClicked || mouseMovedQuickly) @@ -174,7 +169,7 @@ void TooltipWindow::timerCallback() // appear after a timeout.. if (newTip.isNotEmpty() && newTip != tipShowing - && now > lastCompChangeTime + (unsigned int) millisecondsBeforeTipAppears) + && now > lastCompChangeTime + (uint32) millisecondsBeforeTipAppears) { displayTip (mousePos.roundToInt(), newTip); } diff --git a/modules/juce_gui_basics/windows/juce_TooltipWindow.h b/modules/juce_gui_basics/windows/juce_TooltipWindow.h index 42d60a33f0..b4a6656b5f 100644 --- a/modules/juce_gui_basics/windows/juce_TooltipWindow.h +++ b/modules/juce_gui_basics/windows/juce_TooltipWindow.h @@ -85,6 +85,11 @@ public: /** Can be called to manually hide the tip if it's showing. */ 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. @@ -121,19 +126,17 @@ public: private: //============================================================================== Point lastMousePos; - Component* lastComponentUnderMouse; + Component* lastComponentUnderMouse = nullptr; String tipShowing, lastTipUnderMouse; 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 mouseEnter (const MouseEvent&) override; void timerCallback() override; void updatePosition (const String&, Point, Rectangle); - static String getTipFor (Component*); - JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (TooltipWindow) };