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.

180 lines
5.6KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. TooltipWindow::TooltipWindow (Component* const parentComp, const int delayMs)
  18. : Component ("tooltip"),
  19. lastComponentUnderMouse (nullptr),
  20. millisecondsBeforeTipAppears (delayMs),
  21. mouseClicks (0), mouseWheelMoves (0),
  22. lastCompChangeTime (0), lastHideTime (0),
  23. reentrant (false)
  24. {
  25. if (Desktop::getInstance().getMainMouseSource().canHover())
  26. startTimer (123);
  27. setAlwaysOnTop (true);
  28. setOpaque (true);
  29. if (parentComp != nullptr)
  30. parentComp->addChildComponent (this);
  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. void TooltipWindow::displayTip (Point<int> screenPos, const String& tip)
  54. {
  55. jassert (tip.isNotEmpty());
  56. if (! reentrant)
  57. {
  58. ScopedValueSetter<bool> setter (reentrant, true, false);
  59. if (tipShowing != tip)
  60. {
  61. tipShowing = tip;
  62. repaint();
  63. }
  64. if (Component* const parent = getParentComponent())
  65. {
  66. updatePosition (tip, parent->getLocalPoint (nullptr, screenPos),
  67. parent->getLocalBounds());
  68. }
  69. else
  70. {
  71. updatePosition (tip, screenPos, Desktop::getInstance().getDisplays()
  72. .getDisplayContaining (screenPos).userArea);
  73. addToDesktop (ComponentPeer::windowHasDropShadow
  74. | ComponentPeer::windowIsTemporary
  75. | ComponentPeer::windowIgnoresKeyPresses);
  76. }
  77. toFront (false);
  78. }
  79. }
  80. String TooltipWindow::getTipFor (Component* const c)
  81. {
  82. if (c != nullptr
  83. && Process::isForegroundProcess()
  84. && ! ModifierKeys::getCurrentModifiers().isAnyMouseButtonDown())
  85. {
  86. if (TooltipClient* const ttc = dynamic_cast<TooltipClient*> (c))
  87. if (! c->isCurrentlyBlockedByAnotherModalComponent())
  88. return ttc->getTooltip();
  89. }
  90. return String();
  91. }
  92. void TooltipWindow::hideTip()
  93. {
  94. if (! reentrant)
  95. {
  96. tipShowing.clear();
  97. removeFromDesktop();
  98. setVisible (false);
  99. }
  100. }
  101. void TooltipWindow::timerCallback()
  102. {
  103. Desktop& desktop = Desktop::getInstance();
  104. const MouseInputSource mouseSource (desktop.getMainMouseSource());
  105. const unsigned int now = Time::getApproximateMillisecondCounter();
  106. Component* const newComp = mouseSource.isMouse() ? mouseSource.getComponentUnderMouse() : nullptr;
  107. const String newTip (getTipFor (newComp));
  108. const bool tipChanged = (newTip != lastTipUnderMouse || newComp != lastComponentUnderMouse);
  109. lastComponentUnderMouse = newComp;
  110. lastTipUnderMouse = newTip;
  111. const int clickCount = desktop.getMouseButtonClickCounter();
  112. const int wheelCount = desktop.getMouseWheelMoveCounter();
  113. const bool mouseWasClicked = (clickCount > mouseClicks || wheelCount > mouseWheelMoves);
  114. mouseClicks = clickCount;
  115. mouseWheelMoves = wheelCount;
  116. const Point<float> mousePos (mouseSource.getScreenPosition());
  117. const bool mouseMovedQuickly = mousePos.getDistanceFrom (lastMousePos) > 12;
  118. lastMousePos = mousePos;
  119. if (tipChanged || mouseWasClicked || mouseMovedQuickly)
  120. lastCompChangeTime = now;
  121. if (isVisible() || now < lastHideTime + 500)
  122. {
  123. // if a tip is currently visible (or has just disappeared), update to a new one
  124. // immediately if needed..
  125. if (newComp == nullptr || mouseWasClicked || newTip.isEmpty())
  126. {
  127. if (isVisible())
  128. {
  129. lastHideTime = now;
  130. hideTip();
  131. }
  132. }
  133. else if (tipChanged)
  134. {
  135. displayTip (mousePos.roundToInt(), newTip);
  136. }
  137. }
  138. else
  139. {
  140. // if there isn't currently a tip, but one is needed, only let it
  141. // appear after a timeout..
  142. if (newTip.isNotEmpty()
  143. && newTip != tipShowing
  144. && now > lastCompChangeTime + (unsigned int) millisecondsBeforeTipAppears)
  145. {
  146. displayTip (mousePos.roundToInt(), newTip);
  147. }
  148. }
  149. }