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.

221 lines
6.8KB

  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. void TooltipWindow::timerCallback()
  129. {
  130. auto& desktop = Desktop::getInstance();
  131. auto mouseSource = desktop.getMainMouseSource();
  132. auto now = Time::getApproximateMillisecondCounter();
  133. auto* newComp = mouseSource.isTouch() ? nullptr : mouseSource.getComponentUnderMouse();
  134. if (newComp == nullptr || getParentComponent() == nullptr || newComp->getPeer() == getPeer())
  135. {
  136. auto newTip = newComp != nullptr ? getTipFor (*newComp) : String();
  137. bool tipChanged = (newTip != lastTipUnderMouse || newComp != lastComponentUnderMouse);
  138. lastComponentUnderMouse = newComp;
  139. lastTipUnderMouse = newTip;
  140. auto clickCount = desktop.getMouseButtonClickCounter();
  141. auto wheelCount = desktop.getMouseWheelMoveCounter();
  142. bool mouseWasClicked = (clickCount > mouseClicks || wheelCount > mouseWheelMoves);
  143. mouseClicks = clickCount;
  144. mouseWheelMoves = wheelCount;
  145. auto mousePos = mouseSource.getScreenPosition();
  146. bool mouseMovedQuickly = mousePos.getDistanceFrom (lastMousePos) > 12;
  147. lastMousePos = mousePos;
  148. if (tipChanged || mouseWasClicked || mouseMovedQuickly)
  149. lastCompChangeTime = now;
  150. if (isVisible() || now < lastHideTime + 500)
  151. {
  152. // if a tip is currently visible (or has just disappeared), update to a new one
  153. // immediately if needed..
  154. if (newComp == nullptr || mouseWasClicked || newTip.isEmpty())
  155. {
  156. if (isVisible())
  157. {
  158. lastHideTime = now;
  159. hideTip();
  160. }
  161. }
  162. else if (tipChanged)
  163. {
  164. displayTip (mousePos.roundToInt(), newTip);
  165. }
  166. }
  167. else
  168. {
  169. // if there isn't currently a tip, but one is needed, only let it
  170. // appear after a timeout..
  171. if (newTip.isNotEmpty()
  172. && newTip != tipShowing
  173. && now > lastCompChangeTime + (uint32) millisecondsBeforeTipAppears)
  174. {
  175. displayTip (mousePos.roundToInt(), newTip);
  176. }
  177. }
  178. }
  179. }
  180. } // namespace juce