Browse Source

Tooltip: Track mouse clicks using global Desktop mouse listener callbacks to fix bug with Timer callbacks missing events

v6.1.6
ed 4 years ago
parent
commit
057b555f08
2 changed files with 20 additions and 13 deletions
  1. +17
    -11
      modules/juce_gui_basics/windows/juce_TooltipWindow.cpp
  2. +3
    -2
      modules/juce_gui_basics/windows/juce_TooltipWindow.h

+ 17
- 11
modules/juce_gui_basics/windows/juce_TooltipWindow.cpp View File

@@ -37,13 +37,19 @@ TooltipWindow::TooltipWindow (Component* parentComp, int delayMs)
if (parentComp != nullptr)
parentComp->addChildComponent (this);
if (Desktop::getInstance().getMainMouseSource().canHover())
auto& desktop = Desktop::getInstance();
if (desktop.getMainMouseSource().canHover())
{
desktop.addGlobalMouseListener (this);
startTimer (123);
}
}
TooltipWindow::~TooltipWindow()
{
hideTip();
Desktop::getInstance().removeGlobalMouseListener (this);
}
void TooltipWindow::setMillisecondsBeforeTipAppears (const int newTimeMs) noexcept
@@ -56,11 +62,15 @@ void TooltipWindow::paint (Graphics& g)
getLookAndFeel().drawTooltip (g, tipShowing, getWidth(), getHeight());
}
void TooltipWindow::mouseEnter (const MouseEvent&)
void TooltipWindow::mouseEnter (const MouseEvent& e)
{
hideTip();
if (e.eventComponent == this)
hideTip();
}
void TooltipWindow::mouseDown (const MouseEvent&) { dismissalMouseEventOccured = true; }
void TooltipWindow::mouseWheelMove (const MouseEvent&, const MouseWheelDetails&) { dismissalMouseEventOccured = true; }
void TooltipWindow::updatePosition (const String& tip, Point<int> pos, Rectangle<int> parentArea)
{
setBounds (getLookAndFeel().getTooltipBounds (tip, pos, parentArea));
@@ -126,6 +136,7 @@ void TooltipWindow::displayTipInternal (Point<int> screenPos, const String& tip,
toFront (false);
manuallyShownTip = shownManually == ShownManually::yes ? tip : String();
dismissalMouseEventOccured = false;
}
}
@@ -148,6 +159,7 @@ void TooltipWindow::hideTip()
{
tipShowing = {};
manuallyShownTip = {};
dismissalMouseEventOccured = false;
removeFromDesktop();
setVisible (false);
@@ -196,17 +208,11 @@ void TooltipWindow::timerCallback()
lastComponentUnderMouse = newComp;
lastTipUnderMouse = newTip;
const auto clickCount = desktop.getMouseButtonClickCounter();
const auto wheelCount = desktop.getMouseWheelMoveCounter();
const auto mouseWasClicked = (clickCount > mouseClicks || wheelCount > mouseWheelMoves);
mouseClicks = clickCount;
mouseWheelMoves = wheelCount;
const auto mousePos = mouseSource.getScreenPosition();
const auto mouseMovedQuickly = (mousePos.getDistanceFrom (lastMousePos) > 12);
lastMousePos = mousePos;
if (tipChanged || mouseWasClicked || mouseMovedQuickly)
if (tipChanged || dismissalMouseEventOccured || mouseMovedQuickly)
lastCompChangeTime = now;
auto showTip = [this, &mouseSource, &mousePos, &newTip]
@@ -225,7 +231,7 @@ void TooltipWindow::timerCallback()
{
// if a tip is currently visible (or has just disappeared), update to a new one
// immediately if needed..
if (newComp == nullptr || mouseWasClicked || newTip.isEmpty())
if (newComp == nullptr || dismissalMouseEventOccured || newTip.isEmpty())
{
if (isVisible())
{


+ 3
- 2
modules/juce_gui_basics/windows/juce_TooltipWindow.h View File

@@ -133,9 +133,8 @@ private:
Component* lastComponentUnderMouse = nullptr;
String tipShowing, lastTipUnderMouse, manuallyShownTip;
int millisecondsBeforeTipAppears;
int mouseClicks = 0, mouseWheelMoves = 0;
unsigned int lastCompChangeTime = 0, lastHideTime = 0;
bool reentrant = false;
bool reentrant = false, dismissalMouseEventOccured = false;
enum ShownManually { yes, no };
void displayTipInternal (Point<int>, const String&, ShownManually);
@@ -143,6 +142,8 @@ private:
std::unique_ptr<AccessibilityHandler> createAccessibilityHandler() override;
void paint (Graphics&) override;
void mouseEnter (const MouseEvent&) override;
void mouseDown (const MouseEvent&) override;
void mouseWheelMove (const MouseEvent&, const MouseWheelDetails&) override;
void timerCallback() override;
void updatePosition (const String&, Point<int>, Rectangle<int>);


Loading…
Cancel
Save