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.

235 lines
7.3KB

  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. if (parentComp != nullptr)
  27. parentComp->addChildComponent (this);
  28. if (Desktop::getInstance().getMainMouseSource().canHover())
  29. startTimer (123);
  30. }
  31. TooltipWindow::~TooltipWindow()
  32. {
  33. hideTip();
  34. }
  35. void TooltipWindow::setMillisecondsBeforeTipAppears (const int newTimeMs) noexcept
  36. {
  37. millisecondsBeforeTipAppears = newTimeMs;
  38. }
  39. void TooltipWindow::paint (Graphics& g)
  40. {
  41. getLookAndFeel().drawTooltip (g, tipShowing, getWidth(), getHeight());
  42. }
  43. void TooltipWindow::mouseEnter (const MouseEvent&)
  44. {
  45. hideTip();
  46. }
  47. void TooltipWindow::updatePosition (const String& tip, Point<int> pos, Rectangle<int> parentArea)
  48. {
  49. setBounds (getLookAndFeel().getTooltipBounds (tip, pos, parentArea));
  50. setVisible (true);
  51. }
  52. #if JUCE_DEBUG
  53. static Array<TooltipWindow*> activeTooltipWindows;
  54. #endif
  55. void TooltipWindow::displayTip (Point<int> screenPos, const String& tip)
  56. {
  57. jassert (tip.isNotEmpty());
  58. if (! reentrant)
  59. {
  60. ScopedValueSetter<bool> setter (reentrant, true, false);
  61. if (tipShowing != tip)
  62. {
  63. tipShowing = tip;
  64. repaint();
  65. }
  66. if (auto* parent = getParentComponent())
  67. {
  68. updatePosition (tip, parent->getLocalPoint (nullptr, screenPos),
  69. parent->getLocalBounds());
  70. }
  71. else
  72. {
  73. const auto physicalPos = ScalingHelpers::scaledScreenPosToUnscaled (screenPos);
  74. const auto scaledPos = ScalingHelpers::unscaledScreenPosToScaled (*this, physicalPos);
  75. updatePosition (tip, scaledPos, Desktop::getInstance().getDisplays().getDisplayForPoint (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. auto* parent = getParentComponent();
  84. for (auto* w : activeTooltipWindows)
  85. {
  86. if (w != nullptr && w != this && w->tipShowing == tipShowing && w->getParentComponent() == parent)
  87. {
  88. // Looks like you have more than one TooltipWindow showing the same tip..
  89. // Be careful not to create more than one instance of this class with the
  90. // same parent component!
  91. jassertfalse;
  92. }
  93. }
  94. #endif
  95. toFront (false);
  96. if (auto* handler = getAccessibilityHandler())
  97. {
  98. setDescription (tip);
  99. handler->grabFocus();
  100. }
  101. }
  102. }
  103. String TooltipWindow::getTipFor (Component& c)
  104. {
  105. if (Process::isForegroundProcess()
  106. && ! ModifierKeys::currentModifiers.isAnyMouseButtonDown())
  107. {
  108. if (auto* ttc = dynamic_cast<TooltipClient*> (&c))
  109. if (! c.isCurrentlyBlockedByAnotherModalComponent())
  110. return ttc->getTooltip();
  111. }
  112. return {};
  113. }
  114. void TooltipWindow::hideTip()
  115. {
  116. if (! reentrant)
  117. {
  118. if (auto* handler = getAccessibilityHandler())
  119. handler->giveAwayFocus();
  120. tipShowing.clear();
  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. void TooltipWindow::timerCallback()
  135. {
  136. auto& desktop = Desktop::getInstance();
  137. auto mouseSource = desktop.getMainMouseSource();
  138. auto now = Time::getApproximateMillisecondCounter();
  139. auto* newComp = mouseSource.isTouch() ? nullptr : mouseSource.getComponentUnderMouse();
  140. if (newComp == nullptr || getParentComponent() == nullptr || newComp->getPeer() == getPeer())
  141. {
  142. auto newTip = newComp != nullptr ? getTipFor (*newComp) : String();
  143. bool tipChanged = (newTip != lastTipUnderMouse || newComp != lastComponentUnderMouse);
  144. lastComponentUnderMouse = newComp;
  145. lastTipUnderMouse = newTip;
  146. auto clickCount = desktop.getMouseButtonClickCounter();
  147. auto wheelCount = desktop.getMouseWheelMoveCounter();
  148. bool mouseWasClicked = (clickCount > mouseClicks || wheelCount > mouseWheelMoves);
  149. mouseClicks = clickCount;
  150. mouseWheelMoves = wheelCount;
  151. auto mousePos = mouseSource.getScreenPosition();
  152. bool mouseMovedQuickly = mousePos.getDistanceFrom (lastMousePos) > 12;
  153. lastMousePos = mousePos;
  154. if (tipChanged || mouseWasClicked || mouseMovedQuickly)
  155. lastCompChangeTime = now;
  156. if (isVisible() || now < lastHideTime + 500)
  157. {
  158. // if a tip is currently visible (or has just disappeared), update to a new one
  159. // immediately if needed..
  160. if (newComp == nullptr || mouseWasClicked || newTip.isEmpty())
  161. {
  162. if (isVisible())
  163. {
  164. lastHideTime = now;
  165. hideTip();
  166. }
  167. }
  168. else if (tipChanged)
  169. {
  170. displayTip (mousePos.roundToInt(), newTip);
  171. }
  172. }
  173. else
  174. {
  175. // if there isn't currently a tip, but one is needed, only let it
  176. // appear after a timeout..
  177. if (newTip.isNotEmpty()
  178. && newTip != tipShowing
  179. && now > lastCompChangeTime + (uint32) millisecondsBeforeTipAppears)
  180. {
  181. displayTip (mousePos.roundToInt(), newTip);
  182. }
  183. }
  184. }
  185. }
  186. //==============================================================================
  187. std::unique_ptr<AccessibilityHandler> TooltipWindow::createAccessibilityHandler()
  188. {
  189. return std::make_unique<AccessibilityHandler> (*this, AccessibilityRole::tooltip);
  190. }
  191. } // namespace juce