Audio plugin host https://kx.studio/carla
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.

249 lines
7.5KB

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