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.

261 lines
8.2KB

  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. auto& desktop = Desktop::getInstance();
  30. if (desktop.getMainMouseSource().canHover())
  31. {
  32. desktop.addGlobalMouseListener (this);
  33. startTimer (123);
  34. }
  35. }
  36. TooltipWindow::~TooltipWindow()
  37. {
  38. hideTip();
  39. Desktop::getInstance().removeGlobalMouseListener (this);
  40. }
  41. void TooltipWindow::setMillisecondsBeforeTipAppears (const int newTimeMs) noexcept
  42. {
  43. millisecondsBeforeTipAppears = newTimeMs;
  44. }
  45. void TooltipWindow::paint (Graphics& g)
  46. {
  47. getLookAndFeel().drawTooltip (g, tipShowing, getWidth(), getHeight());
  48. }
  49. void TooltipWindow::mouseEnter (const MouseEvent& e)
  50. {
  51. if (e.eventComponent == this)
  52. hideTip();
  53. }
  54. void TooltipWindow::mouseDown (const MouseEvent&) { dismissalMouseEventOccured = true; }
  55. void TooltipWindow::mouseWheelMove (const MouseEvent&, const MouseWheelDetails&) { dismissalMouseEventOccured = true; }
  56. void TooltipWindow::updatePosition (const String& tip, Point<int> pos, Rectangle<int> parentArea)
  57. {
  58. setBounds (getLookAndFeel().getTooltipBounds (tip, pos, parentArea));
  59. setVisible (true);
  60. }
  61. #if JUCE_DEBUG
  62. static Array<TooltipWindow*> activeTooltipWindows;
  63. #endif
  64. void TooltipWindow::displayTip (Point<int> screenPos, const String& tip)
  65. {
  66. jassert (tip.isNotEmpty());
  67. displayTipInternal (screenPos, tip, ShownManually::yes);
  68. }
  69. void TooltipWindow::displayTipInternal (Point<int> screenPos, const String& tip, ShownManually shownManually)
  70. {
  71. if (! reentrant)
  72. {
  73. ScopedValueSetter<bool> setter (reentrant, true, false);
  74. if (tipShowing != tip)
  75. {
  76. tipShowing = tip;
  77. repaint();
  78. }
  79. if (auto* parent = getParentComponent())
  80. {
  81. updatePosition (tip, parent->getLocalPoint (nullptr, screenPos),
  82. parent->getLocalBounds());
  83. }
  84. else
  85. {
  86. const auto physicalPos = ScalingHelpers::scaledScreenPosToUnscaled (screenPos);
  87. const auto scaledPos = ScalingHelpers::unscaledScreenPosToScaled (*this, physicalPos);
  88. updatePosition (tip, scaledPos, Desktop::getInstance().getDisplays().getDisplayForPoint (screenPos)->userArea);
  89. addToDesktop (ComponentPeer::windowHasDropShadow
  90. | ComponentPeer::windowIsTemporary
  91. | ComponentPeer::windowIgnoresKeyPresses
  92. | ComponentPeer::windowIgnoresMouseClicks);
  93. }
  94. #if JUCE_DEBUG
  95. activeTooltipWindows.addIfNotAlreadyThere (this);
  96. auto* parent = getParentComponent();
  97. for (auto* w : activeTooltipWindows)
  98. {
  99. if (w != nullptr && w != this && w->tipShowing == tipShowing && w->getParentComponent() == parent)
  100. {
  101. // Looks like you have more than one TooltipWindow showing the same tip..
  102. // Be careful not to create more than one instance of this class with the
  103. // same parent component!
  104. jassertfalse;
  105. }
  106. }
  107. #endif
  108. toFront (false);
  109. manuallyShownTip = shownManually == ShownManually::yes ? tip : String();
  110. dismissalMouseEventOccured = false;
  111. }
  112. }
  113. String TooltipWindow::getTipFor (Component& c)
  114. {
  115. if (isForegroundOrEmbeddedProcess (&c)
  116. && ! ModifierKeys::currentModifiers.isAnyMouseButtonDown())
  117. {
  118. if (auto* ttc = dynamic_cast<TooltipClient*> (&c))
  119. if (! c.isCurrentlyBlockedByAnotherModalComponent())
  120. return ttc->getTooltip();
  121. }
  122. return {};
  123. }
  124. void TooltipWindow::hideTip()
  125. {
  126. if (! reentrant)
  127. {
  128. tipShowing = {};
  129. manuallyShownTip = {};
  130. dismissalMouseEventOccured = false;
  131. removeFromDesktop();
  132. setVisible (false);
  133. #if JUCE_DEBUG
  134. activeTooltipWindows.removeAllInstancesOf (this);
  135. #endif
  136. }
  137. }
  138. float TooltipWindow::getDesktopScaleFactor() const
  139. {
  140. if (lastComponentUnderMouse != nullptr)
  141. return Component::getApproximateScaleFactorForComponent (lastComponentUnderMouse);
  142. return Component::getDesktopScaleFactor();
  143. }
  144. std::unique_ptr<AccessibilityHandler> TooltipWindow::createAccessibilityHandler()
  145. {
  146. return createIgnoredAccessibilityHandler (*this);
  147. }
  148. void TooltipWindow::timerCallback()
  149. {
  150. auto& desktop = Desktop::getInstance();
  151. auto mouseSource = desktop.getMainMouseSource();
  152. auto now = Time::getApproximateMillisecondCounter();
  153. auto* newComp = mouseSource.isTouch() ? nullptr : mouseSource.getComponentUnderMouse();
  154. if (newComp == nullptr || getParentComponent() == nullptr || newComp->getPeer() == getPeer())
  155. {
  156. const auto componentChanged = (newComp != lastComponentUnderMouse);
  157. const auto newTip = [this, &newComp, componentChanged]
  158. {
  159. if (dynamic_cast<TooltipClient*> (newComp) != nullptr)
  160. return getTipFor (*newComp);
  161. return componentChanged ? String() : manuallyShownTip;
  162. }();
  163. const auto tipChanged = (newTip != lastTipUnderMouse || componentChanged);
  164. lastComponentUnderMouse = newComp;
  165. lastTipUnderMouse = newTip;
  166. const auto mousePos = mouseSource.getScreenPosition();
  167. const auto mouseMovedQuickly = (mousePos.getDistanceFrom (lastMousePos) > 12);
  168. lastMousePos = mousePos;
  169. if (tipChanged || dismissalMouseEventOccured || mouseMovedQuickly)
  170. lastCompChangeTime = now;
  171. auto showTip = [this, &mouseSource, &mousePos, &newTip]
  172. {
  173. if (mouseSource.getLastMouseDownPosition() == lastMousePos)
  174. return;
  175. const auto isShowingManualTip = (manuallyShownTip.isNotEmpty() && manuallyShownTip == newTip);
  176. displayTipInternal (mousePos.roundToInt(),
  177. newTip,
  178. isShowingManualTip ? ShownManually::yes : ShownManually::no);
  179. };
  180. if (isVisible() || now < lastHideTime + 500)
  181. {
  182. // if a tip is currently visible (or has just disappeared), update to a new one
  183. // immediately if needed..
  184. if (newComp == nullptr || dismissalMouseEventOccured || newTip.isEmpty())
  185. {
  186. if (isVisible())
  187. {
  188. lastHideTime = now;
  189. hideTip();
  190. }
  191. }
  192. else if (tipChanged)
  193. {
  194. showTip();
  195. }
  196. }
  197. else
  198. {
  199. // if there isn't currently a tip, but one is needed, only let it appear after a timeout
  200. if (newTip.isNotEmpty()
  201. && newTip != tipShowing
  202. && now > lastCompChangeTime + (uint32) millisecondsBeforeTipAppears)
  203. {
  204. showTip();
  205. }
  206. }
  207. }
  208. }
  209. } // namespace juce