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.

226 lines
6.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. 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. const auto physicalPos = ScalingHelpers::scaledScreenPosToUnscaled (screenPos);
  75. const auto scaledPos = ScalingHelpers::unscaledScreenPosToScaled (*this, physicalPos);
  76. updatePosition (tip, scaledPos, Desktop::getInstance().getDisplays().getDisplayForPoint (screenPos)->userArea);
  77. addToDesktop (ComponentPeer::windowHasDropShadow
  78. | ComponentPeer::windowIsTemporary
  79. | ComponentPeer::windowIgnoresKeyPresses
  80. | ComponentPeer::windowIgnoresMouseClicks);
  81. }
  82. #if JUCE_DEBUG
  83. activeTooltipWindows.addIfNotAlreadyThere (this);
  84. auto* parent = getParentComponent();
  85. for (auto* w : activeTooltipWindows)
  86. {
  87. if (w != nullptr && w != this && w->tipShowing == tipShowing && w->getParentComponent() == parent)
  88. {
  89. // Looks like you have more than one TooltipWindow showing the same tip..
  90. // Be careful not to create more than one instance of this class with the
  91. // same parent component!
  92. jassertfalse;
  93. }
  94. }
  95. #endif
  96. toFront (false);
  97. }
  98. }
  99. String TooltipWindow::getTipFor (Component& c)
  100. {
  101. if (isForegroundOrEmbeddedProcess (&c)
  102. && ! ModifierKeys::currentModifiers.isAnyMouseButtonDown())
  103. {
  104. if (auto* ttc = dynamic_cast<TooltipClient*> (&c))
  105. if (! c.isCurrentlyBlockedByAnotherModalComponent())
  106. return ttc->getTooltip();
  107. }
  108. return {};
  109. }
  110. void TooltipWindow::hideTip()
  111. {
  112. if (! reentrant)
  113. {
  114. tipShowing.clear();
  115. removeFromDesktop();
  116. setVisible (false);
  117. #if JUCE_DEBUG
  118. activeTooltipWindows.removeAllInstancesOf (this);
  119. #endif
  120. }
  121. }
  122. float TooltipWindow::getDesktopScaleFactor() const
  123. {
  124. if (lastComponentUnderMouse != nullptr)
  125. return Component::getApproximateScaleFactorForComponent (lastComponentUnderMouse);
  126. return Component::getDesktopScaleFactor();
  127. }
  128. std::unique_ptr<AccessibilityHandler> TooltipWindow::createAccessibilityHandler()
  129. {
  130. return createIgnoredAccessibilityHandler (*this);
  131. }
  132. void TooltipWindow::timerCallback()
  133. {
  134. auto& desktop = Desktop::getInstance();
  135. auto mouseSource = desktop.getMainMouseSource();
  136. auto now = Time::getApproximateMillisecondCounter();
  137. auto* newComp = mouseSource.isTouch() ? nullptr : mouseSource.getComponentUnderMouse();
  138. if (newComp == nullptr || getParentComponent() == nullptr || newComp->getPeer() == getPeer())
  139. {
  140. auto newTip = newComp != nullptr ? getTipFor (*newComp) : String();
  141. bool tipChanged = (newTip != lastTipUnderMouse || newComp != lastComponentUnderMouse);
  142. lastComponentUnderMouse = newComp;
  143. lastTipUnderMouse = newTip;
  144. auto clickCount = desktop.getMouseButtonClickCounter();
  145. auto wheelCount = desktop.getMouseWheelMoveCounter();
  146. bool mouseWasClicked = (clickCount > mouseClicks || wheelCount > mouseWheelMoves);
  147. mouseClicks = clickCount;
  148. mouseWheelMoves = wheelCount;
  149. auto mousePos = mouseSource.getScreenPosition();
  150. bool mouseMovedQuickly = mousePos.getDistanceFrom (lastMousePos) > 12;
  151. lastMousePos = mousePos;
  152. if (tipChanged || mouseWasClicked || mouseMovedQuickly)
  153. lastCompChangeTime = now;
  154. if (isVisible() || now < lastHideTime + 500)
  155. {
  156. // if a tip is currently visible (or has just disappeared), update to a new one
  157. // immediately if needed..
  158. if (newComp == nullptr || mouseWasClicked || newTip.isEmpty())
  159. {
  160. if (isVisible())
  161. {
  162. lastHideTime = now;
  163. hideTip();
  164. }
  165. }
  166. else if (tipChanged)
  167. {
  168. displayTip (mousePos.roundToInt(), newTip);
  169. }
  170. }
  171. else
  172. {
  173. // if there isn't currently a tip, but one is needed, only let it
  174. // appear after a timeout..
  175. if (newTip.isNotEmpty()
  176. && newTip != tipShowing
  177. && now > lastCompChangeTime + (uint32) millisecondsBeforeTipAppears)
  178. {
  179. displayTip (mousePos.roundToInt(), newTip);
  180. }
  181. }
  182. }
  183. }
  184. } // namespace juce