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.

203 lines
6.0KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For this technical preview, this file is not subject to commercial licensing.
  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. if (parentComp != nullptr)
  22. parentComp->addChildComponent (this);
  23. if (Desktop::getInstance().getMainMouseSource().canHover())
  24. startTimer (123);
  25. }
  26. TooltipWindow::~TooltipWindow()
  27. {
  28. hideTip();
  29. }
  30. void TooltipWindow::setMillisecondsBeforeTipAppears (const int newTimeMs) noexcept
  31. {
  32. millisecondsBeforeTipAppears = newTimeMs;
  33. }
  34. void TooltipWindow::paint (Graphics& g)
  35. {
  36. getLookAndFeel().drawTooltip (g, tipShowing, getWidth(), getHeight());
  37. }
  38. void TooltipWindow::mouseEnter (const MouseEvent&)
  39. {
  40. hideTip();
  41. }
  42. void TooltipWindow::updatePosition (const String& tip, Point<int> pos, Rectangle<int> parentArea)
  43. {
  44. setBounds (getLookAndFeel().getTooltipBounds (tip, pos, parentArea));
  45. setVisible (true);
  46. }
  47. #if JUCE_DEBUG
  48. static Array<TooltipWindow*> activeTooltipWindows;
  49. #endif
  50. void TooltipWindow::displayTip (Point<int> screenPos, const String& tip)
  51. {
  52. jassert (tip.isNotEmpty());
  53. if (! reentrant)
  54. {
  55. ScopedValueSetter<bool> setter (reentrant, true, false);
  56. if (tipShowing != tip)
  57. {
  58. tipShowing = tip;
  59. repaint();
  60. }
  61. if (auto* parent = getParentComponent())
  62. {
  63. updatePosition (tip, parent->getLocalPoint (nullptr, screenPos),
  64. parent->getLocalBounds());
  65. }
  66. else
  67. {
  68. updatePosition (tip, screenPos, Desktop::getInstance().getDisplays().findDisplayForPoint (screenPos).userArea);
  69. addToDesktop (ComponentPeer::windowHasDropShadow
  70. | ComponentPeer::windowIsTemporary
  71. | ComponentPeer::windowIgnoresKeyPresses
  72. | ComponentPeer::windowIgnoresMouseClicks);
  73. }
  74. #if JUCE_DEBUG
  75. activeTooltipWindows.addIfNotAlreadyThere (this);
  76. auto* parent = getParentComponent();
  77. for (auto* w : activeTooltipWindows)
  78. {
  79. if (w != this && w->tipShowing == tipShowing && w->getParentComponent() == parent)
  80. {
  81. // Looks like you have more than one TooltipWindow showing the same tip..
  82. // Be careful not to create more than one instance of this class with the
  83. // same parent component!
  84. jassertfalse;
  85. }
  86. }
  87. #endif
  88. toFront (false);
  89. }
  90. }
  91. String TooltipWindow::getTipFor (Component& c)
  92. {
  93. if (Process::isForegroundProcess()
  94. && ! ModifierKeys::currentModifiers.isAnyMouseButtonDown())
  95. {
  96. if (auto* ttc = dynamic_cast<TooltipClient*> (&c))
  97. if (! c.isCurrentlyBlockedByAnotherModalComponent())
  98. return ttc->getTooltip();
  99. }
  100. return {};
  101. }
  102. void TooltipWindow::hideTip()
  103. {
  104. if (! reentrant)
  105. {
  106. tipShowing.clear();
  107. removeFromDesktop();
  108. setVisible (false);
  109. #if JUCE_DEBUG
  110. activeTooltipWindows.removeAllInstancesOf (this);
  111. #endif
  112. }
  113. }
  114. void TooltipWindow::timerCallback()
  115. {
  116. auto& desktop = Desktop::getInstance();
  117. auto mouseSource = desktop.getMainMouseSource();
  118. auto now = Time::getApproximateMillisecondCounter();
  119. auto* newComp = mouseSource.isTouch() ? nullptr : mouseSource.getComponentUnderMouse();
  120. if (newComp == nullptr || getParentComponent() == nullptr || newComp->getPeer() == getPeer())
  121. {
  122. auto newTip = newComp != nullptr ? getTipFor (*newComp) : String();
  123. bool tipChanged = (newTip != lastTipUnderMouse || newComp != lastComponentUnderMouse);
  124. lastComponentUnderMouse = newComp;
  125. lastTipUnderMouse = newTip;
  126. auto clickCount = desktop.getMouseButtonClickCounter();
  127. auto wheelCount = desktop.getMouseWheelMoveCounter();
  128. bool mouseWasClicked = (clickCount > mouseClicks || wheelCount > mouseWheelMoves);
  129. mouseClicks = clickCount;
  130. mouseWheelMoves = wheelCount;
  131. auto mousePos = mouseSource.getScreenPosition();
  132. bool mouseMovedQuickly = mousePos.getDistanceFrom (lastMousePos) > 12;
  133. lastMousePos = mousePos;
  134. if (tipChanged || mouseWasClicked || mouseMovedQuickly)
  135. lastCompChangeTime = now;
  136. if (isVisible() || now < lastHideTime + 500)
  137. {
  138. // if a tip is currently visible (or has just disappeared), update to a new one
  139. // immediately if needed..
  140. if (newComp == nullptr || mouseWasClicked || newTip.isEmpty())
  141. {
  142. if (isVisible())
  143. {
  144. lastHideTime = now;
  145. hideTip();
  146. }
  147. }
  148. else if (tipChanged)
  149. {
  150. displayTip (mousePos.roundToInt(), newTip);
  151. }
  152. }
  153. else
  154. {
  155. // if there isn't currently a tip, but one is needed, only let it
  156. // appear after a timeout..
  157. if (newTip.isNotEmpty()
  158. && newTip != tipShowing
  159. && now > lastCompChangeTime + (uint32) millisecondsBeforeTipAppears)
  160. {
  161. displayTip (mousePos.roundToInt(), newTip);
  162. }
  163. }
  164. }
  165. }
  166. } // namespace juce