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.

255 lines
7.9KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - Raw Material Software Limited
  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 6 End-User License
  8. Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
  9. End User License Agreement: www.juce.com/juce-6-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce
  19. {
  20. TooltipWindow::TooltipWindow (Component* parentComp, int delayMs)
  21. : Component ("tooltip"),
  22. millisecondsBeforeTipAppears (delayMs)
  23. {
  24. setAlwaysOnTop (true);
  25. setOpaque (true);
  26. setAccessible (false);
  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. displayTipInternal (screenPos, tip, ShownManually::yes);
  60. }
  61. void TooltipWindow::displayTipInternal (Point<int> screenPos, const String& tip, ShownManually shownManually)
  62. {
  63. if (! reentrant)
  64. {
  65. ScopedValueSetter<bool> setter (reentrant, true, false);
  66. if (tipShowing != tip)
  67. {
  68. tipShowing = tip;
  69. repaint();
  70. }
  71. if (auto* parent = getParentComponent())
  72. {
  73. updatePosition (tip, parent->getLocalPoint (nullptr, screenPos),
  74. parent->getLocalBounds());
  75. }
  76. else
  77. {
  78. const auto physicalPos = ScalingHelpers::scaledScreenPosToUnscaled (screenPos);
  79. const auto scaledPos = ScalingHelpers::unscaledScreenPosToScaled (*this, physicalPos);
  80. updatePosition (tip, scaledPos, Desktop::getInstance().getDisplays().getDisplayForPoint (screenPos)->userArea);
  81. addToDesktop (ComponentPeer::windowHasDropShadow
  82. | ComponentPeer::windowIsTemporary
  83. | ComponentPeer::windowIgnoresKeyPresses
  84. | ComponentPeer::windowIgnoresMouseClicks);
  85. }
  86. #if JUCE_DEBUG
  87. activeTooltipWindows.addIfNotAlreadyThere (this);
  88. auto* parent = getParentComponent();
  89. for (auto* w : activeTooltipWindows)
  90. {
  91. if (w != nullptr && w != this && w->tipShowing == tipShowing && w->getParentComponent() == parent)
  92. {
  93. // Looks like you have more than one TooltipWindow showing the same tip..
  94. // Be careful not to create more than one instance of this class with the
  95. // same parent component!
  96. jassertfalse;
  97. }
  98. }
  99. #endif
  100. toFront (false);
  101. manuallyShownTip = shownManually == ShownManually::yes ? tip : String();
  102. }
  103. }
  104. String TooltipWindow::getTipFor (Component& c)
  105. {
  106. if (isForegroundOrEmbeddedProcess (&c)
  107. && ! ModifierKeys::currentModifiers.isAnyMouseButtonDown())
  108. {
  109. if (auto* ttc = dynamic_cast<TooltipClient*> (&c))
  110. if (! c.isCurrentlyBlockedByAnotherModalComponent())
  111. return ttc->getTooltip();
  112. }
  113. return {};
  114. }
  115. void TooltipWindow::hideTip()
  116. {
  117. if (! reentrant)
  118. {
  119. tipShowing = {};
  120. manuallyShownTip = {};
  121. removeFromDesktop();
  122. setVisible (false);
  123. #if JUCE_DEBUG
  124. activeTooltipWindows.removeAllInstancesOf (this);
  125. #endif
  126. }
  127. }
  128. float TooltipWindow::getDesktopScaleFactor() const
  129. {
  130. if (lastComponentUnderMouse != nullptr)
  131. return Component::getApproximateScaleFactorForComponent (lastComponentUnderMouse);
  132. return Component::getDesktopScaleFactor();
  133. }
  134. std::unique_ptr<AccessibilityHandler> TooltipWindow::createAccessibilityHandler()
  135. {
  136. return createIgnoredAccessibilityHandler (*this);
  137. }
  138. void TooltipWindow::timerCallback()
  139. {
  140. auto& desktop = Desktop::getInstance();
  141. auto mouseSource = desktop.getMainMouseSource();
  142. auto now = Time::getApproximateMillisecondCounter();
  143. auto* newComp = mouseSource.isTouch() ? nullptr : mouseSource.getComponentUnderMouse();
  144. if (newComp == nullptr || getParentComponent() == nullptr || newComp->getPeer() == getPeer())
  145. {
  146. const auto componentChanged = (newComp != lastComponentUnderMouse);
  147. const auto newTip = [this, &newComp, componentChanged]
  148. {
  149. if (dynamic_cast<TooltipClient*> (newComp) != nullptr)
  150. return getTipFor (*newComp);
  151. return componentChanged ? String() : manuallyShownTip;
  152. }();
  153. const auto tipChanged = (newTip != lastTipUnderMouse || componentChanged);
  154. lastComponentUnderMouse = newComp;
  155. lastTipUnderMouse = newTip;
  156. const auto clickCount = desktop.getMouseButtonClickCounter();
  157. const auto wheelCount = desktop.getMouseWheelMoveCounter();
  158. const auto mouseWasClicked = (clickCount > mouseClicks || wheelCount > mouseWheelMoves);
  159. mouseClicks = clickCount;
  160. mouseWheelMoves = wheelCount;
  161. const auto mousePos = mouseSource.getScreenPosition();
  162. const auto mouseMovedQuickly = (mousePos.getDistanceFrom (lastMousePos) > 12);
  163. lastMousePos = mousePos;
  164. if (tipChanged || mouseWasClicked || mouseMovedQuickly)
  165. lastCompChangeTime = now;
  166. auto showTip = [this, &mouseSource, &mousePos, &newTip]
  167. {
  168. if (mouseSource.getLastMouseDownPosition() == lastMousePos)
  169. return;
  170. const auto isShowingManualTip = (manuallyShownTip.isNotEmpty() && manuallyShownTip == newTip);
  171. displayTipInternal (mousePos.roundToInt(),
  172. newTip,
  173. isShowingManualTip ? ShownManually::yes : ShownManually::no);
  174. };
  175. if (isVisible() || now < lastHideTime + 500)
  176. {
  177. // if a tip is currently visible (or has just disappeared), update to a new one
  178. // immediately if needed..
  179. if (newComp == nullptr || mouseWasClicked || newTip.isEmpty())
  180. {
  181. if (isVisible())
  182. {
  183. lastHideTime = now;
  184. hideTip();
  185. }
  186. }
  187. else if (tipChanged)
  188. {
  189. showTip();
  190. }
  191. }
  192. else
  193. {
  194. // if there isn't currently a tip, but one is needed, only let it appear after a timeout
  195. if (newTip.isNotEmpty()
  196. && newTip != tipShowing
  197. && now > lastCompChangeTime + (uint32) millisecondsBeforeTipAppears)
  198. {
  199. showTip();
  200. }
  201. }
  202. }
  203. }
  204. } // namespace juce