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.

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