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.

211 lines
6.3KB

  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 with the
  89. // same parent component!
  90. jassertfalse;
  91. }
  92. }
  93. #endif
  94. toFront (false);
  95. }
  96. }
  97. String TooltipWindow::getTipFor (Component& c)
  98. {
  99. if (Process::isForegroundProcess()
  100. && ! ModifierKeys::currentModifiers.isAnyMouseButtonDown())
  101. {
  102. if (auto* ttc = dynamic_cast<TooltipClient*> (&c))
  103. if (! c.isCurrentlyBlockedByAnotherModalComponent())
  104. return ttc->getTooltip();
  105. }
  106. return {};
  107. }
  108. void TooltipWindow::hideTip()
  109. {
  110. if (! reentrant)
  111. {
  112. tipShowing.clear();
  113. removeFromDesktop();
  114. setVisible (false);
  115. #if JUCE_DEBUG
  116. activeTooltipWindows.removeAllInstancesOf (this);
  117. #endif
  118. }
  119. }
  120. void TooltipWindow::timerCallback()
  121. {
  122. auto& desktop = Desktop::getInstance();
  123. auto mouseSource = desktop.getMainMouseSource();
  124. auto now = Time::getApproximateMillisecondCounter();
  125. auto* newComp = mouseSource.isTouch() ? nullptr : mouseSource.getComponentUnderMouse();
  126. auto* parent = getParentComponent();
  127. if (newComp == nullptr || parent == nullptr || parent == newComp || parent->isParentOf (newComp))
  128. {
  129. auto newTip = newComp != nullptr ? getTipFor (*newComp) : String();
  130. bool tipChanged = (newTip != lastTipUnderMouse || newComp != lastComponentUnderMouse);
  131. lastComponentUnderMouse = newComp;
  132. lastTipUnderMouse = newTip;
  133. auto clickCount = desktop.getMouseButtonClickCounter();
  134. auto wheelCount = desktop.getMouseWheelMoveCounter();
  135. bool mouseWasClicked = (clickCount > mouseClicks || wheelCount > mouseWheelMoves);
  136. mouseClicks = clickCount;
  137. mouseWheelMoves = wheelCount;
  138. auto mousePos = mouseSource.getScreenPosition();
  139. bool mouseMovedQuickly = mousePos.getDistanceFrom (lastMousePos) > 12;
  140. lastMousePos = mousePos;
  141. if (tipChanged || mouseWasClicked || mouseMovedQuickly)
  142. lastCompChangeTime = now;
  143. if (isVisible() || now < lastHideTime + 500)
  144. {
  145. // if a tip is currently visible (or has just disappeared), update to a new one
  146. // immediately if needed..
  147. if (newComp == nullptr || mouseWasClicked || newTip.isEmpty())
  148. {
  149. if (isVisible())
  150. {
  151. lastHideTime = now;
  152. hideTip();
  153. }
  154. }
  155. else if (tipChanged)
  156. {
  157. displayTip (mousePos.roundToInt(), newTip);
  158. }
  159. }
  160. else
  161. {
  162. // if there isn't currently a tip, but one is needed, only let it
  163. // appear after a timeout..
  164. if (newTip.isNotEmpty()
  165. && newTip != tipShowing
  166. && now > lastCompChangeTime + (uint32) millisecondsBeforeTipAppears)
  167. {
  168. displayTip (mousePos.roundToInt(), newTip);
  169. }
  170. }
  171. }
  172. }
  173. } // namespace juce