The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

205 lines
6.0KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. namespace juce
  20. {
  21. TooltipWindow::TooltipWindow (Component* parentComp, int delayMs)
  22. : Component ("tooltip"),
  23. millisecondsBeforeTipAppears (delayMs)
  24. {
  25. setAlwaysOnTop (true);
  26. setOpaque (true);
  27. if (parentComp != nullptr)
  28. parentComp->addChildComponent (this);
  29. if (Desktop::getInstance().getMainMouseSource().canHover())
  30. startTimer (123);
  31. }
  32. TooltipWindow::~TooltipWindow()
  33. {
  34. hideTip();
  35. }
  36. void TooltipWindow::setMillisecondsBeforeTipAppears (const int newTimeMs) noexcept
  37. {
  38. millisecondsBeforeTipAppears = newTimeMs;
  39. }
  40. void TooltipWindow::paint (Graphics& g)
  41. {
  42. getLookAndFeel().drawTooltip (g, tipShowing, getWidth(), getHeight());
  43. }
  44. void TooltipWindow::mouseEnter (const MouseEvent&)
  45. {
  46. hideTip();
  47. }
  48. void TooltipWindow::updatePosition (const String& tip, Point<int> pos, Rectangle<int> parentArea)
  49. {
  50. setBounds (getLookAndFeel().getTooltipBounds (tip, pos, parentArea));
  51. setVisible (true);
  52. }
  53. #if JUCE_DEBUG
  54. static Array<TooltipWindow*> activeTooltipWindows;
  55. #endif
  56. void TooltipWindow::displayTip (Point<int> screenPos, const String& tip)
  57. {
  58. jassert (tip.isNotEmpty());
  59. if (! reentrant)
  60. {
  61. ScopedValueSetter<bool> setter (reentrant, true, false);
  62. if (tipShowing != tip)
  63. {
  64. tipShowing = tip;
  65. repaint();
  66. }
  67. if (auto* parent = getParentComponent())
  68. {
  69. updatePosition (tip, parent->getLocalPoint (nullptr, screenPos),
  70. parent->getLocalBounds());
  71. }
  72. else
  73. {
  74. updatePosition (tip, screenPos, Desktop::getInstance().getDisplays()
  75. .getDisplayContaining (screenPos).userArea);
  76. addToDesktop (ComponentPeer::windowHasDropShadow
  77. | ComponentPeer::windowIsTemporary
  78. | ComponentPeer::windowIgnoresKeyPresses
  79. | ComponentPeer::windowIgnoresMouseClicks);
  80. }
  81. #if JUCE_DEBUG
  82. activeTooltipWindows.addIfNotAlreadyThere (this);
  83. for (auto* w : activeTooltipWindows)
  84. {
  85. if (w != this && w->tipShowing == tipShowing)
  86. {
  87. // Looks like you have more than one TooltipWindow showing the same tip..
  88. // Be careful not to create more than one instance of this class!
  89. jassertfalse;
  90. }
  91. }
  92. #endif
  93. toFront (false);
  94. }
  95. }
  96. String TooltipWindow::getTipFor (Component& c)
  97. {
  98. if (Process::isForegroundProcess()
  99. && ! ModifierKeys::getCurrentModifiers().isAnyMouseButtonDown())
  100. {
  101. if (auto* ttc = dynamic_cast<TooltipClient*> (&c))
  102. if (! c.isCurrentlyBlockedByAnotherModalComponent())
  103. return ttc->getTooltip();
  104. }
  105. return {};
  106. }
  107. void TooltipWindow::hideTip()
  108. {
  109. if (! reentrant)
  110. {
  111. tipShowing.clear();
  112. removeFromDesktop();
  113. setVisible (false);
  114. #if JUCE_DEBUG
  115. activeTooltipWindows.removeAllInstancesOf (this);
  116. #endif
  117. }
  118. }
  119. void TooltipWindow::timerCallback()
  120. {
  121. auto& desktop = Desktop::getInstance();
  122. auto mouseSource = desktop.getMainMouseSource();
  123. auto now = Time::getApproximateMillisecondCounter();
  124. auto* newComp = mouseSource.isTouch() ? nullptr : mouseSource.getComponentUnderMouse();
  125. auto newTip = newComp != nullptr ? getTipFor (*newComp) : String();
  126. bool tipChanged = (newTip != lastTipUnderMouse || newComp != lastComponentUnderMouse);
  127. lastComponentUnderMouse = newComp;
  128. lastTipUnderMouse = newTip;
  129. auto clickCount = desktop.getMouseButtonClickCounter();
  130. auto wheelCount = desktop.getMouseWheelMoveCounter();
  131. bool mouseWasClicked = (clickCount > mouseClicks || wheelCount > mouseWheelMoves);
  132. mouseClicks = clickCount;
  133. mouseWheelMoves = wheelCount;
  134. auto mousePos = mouseSource.getScreenPosition();
  135. bool mouseMovedQuickly = mousePos.getDistanceFrom (lastMousePos) > 12;
  136. lastMousePos = mousePos;
  137. if (tipChanged || mouseWasClicked || mouseMovedQuickly)
  138. lastCompChangeTime = now;
  139. if (isVisible() || now < lastHideTime + 500)
  140. {
  141. // if a tip is currently visible (or has just disappeared), update to a new one
  142. // immediately if needed..
  143. if (newComp == nullptr || mouseWasClicked || newTip.isEmpty())
  144. {
  145. if (isVisible())
  146. {
  147. lastHideTime = now;
  148. hideTip();
  149. }
  150. }
  151. else if (tipChanged)
  152. {
  153. displayTip (mousePos.roundToInt(), newTip);
  154. }
  155. }
  156. else
  157. {
  158. // if there isn't currently a tip, but one is needed, only let it
  159. // appear after a timeout..
  160. if (newTip.isNotEmpty()
  161. && newTip != tipShowing
  162. && now > lastCompChangeTime + (uint32) millisecondsBeforeTipAppears)
  163. {
  164. displayTip (mousePos.roundToInt(), newTip);
  165. }
  166. }
  167. }
  168. } // namespace juce