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.

187 lines
5.8KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software 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, const Rectangle<int>& parentArea)
  49. {
  50. int w, h;
  51. getLookAndFeel().getTooltipSize (tip, w, h);
  52. setBounds (Rectangle<int> (pos.x > parentArea.getCentreX() ? pos.x - (w + 12) : pos.x + 24,
  53. pos.y > parentArea.getCentreY() ? pos.y - (h + 6) : pos.y + 6,
  54. w, h)
  55. .constrainedWithin (parentArea));
  56. setVisible (true);
  57. }
  58. void TooltipWindow::displayTip (Point<int> screenPos, const String& tip)
  59. {
  60. jassert (tip.isNotEmpty());
  61. if (! reentrant)
  62. {
  63. ScopedValueSetter<bool> setter (reentrant, true, false);
  64. if (tipShowing != tip)
  65. {
  66. tipShowing = tip;
  67. repaint();
  68. }
  69. if (Component* const parent = getParentComponent())
  70. {
  71. updatePosition (tip, parent->getLocalPoint (nullptr, screenPos),
  72. parent->getLocalBounds());
  73. }
  74. else
  75. {
  76. updatePosition (tip, screenPos, Desktop::getInstance().getDisplays()
  77. .getDisplayContaining (screenPos).userArea);
  78. addToDesktop (ComponentPeer::windowHasDropShadow
  79. | ComponentPeer::windowIsTemporary
  80. | ComponentPeer::windowIgnoresKeyPresses);
  81. }
  82. toFront (false);
  83. }
  84. }
  85. String TooltipWindow::getTipFor (Component* const c)
  86. {
  87. if (c != nullptr
  88. && Process::isForegroundProcess()
  89. && ! ModifierKeys::getCurrentModifiers().isAnyMouseButtonDown())
  90. {
  91. if (TooltipClient* const ttc = dynamic_cast <TooltipClient*> (c))
  92. if (! c->isCurrentlyBlockedByAnotherModalComponent())
  93. return ttc->getTooltip();
  94. }
  95. return String();
  96. }
  97. void TooltipWindow::hideTip()
  98. {
  99. if (! reentrant)
  100. {
  101. tipShowing.clear();
  102. removeFromDesktop();
  103. setVisible (false);
  104. }
  105. }
  106. void TooltipWindow::timerCallback()
  107. {
  108. Desktop& desktop = Desktop::getInstance();
  109. const MouseInputSource mouseSource (desktop.getMainMouseSource());
  110. const unsigned int now = Time::getApproximateMillisecondCounter();
  111. Component* const newComp = mouseSource.isMouse() ? mouseSource.getComponentUnderMouse() : nullptr;
  112. const String newTip (getTipFor (newComp));
  113. const bool tipChanged = (newTip != lastTipUnderMouse || newComp != lastComponentUnderMouse);
  114. lastComponentUnderMouse = newComp;
  115. lastTipUnderMouse = newTip;
  116. const int clickCount = desktop.getMouseButtonClickCounter();
  117. const int wheelCount = desktop.getMouseWheelMoveCounter();
  118. const bool mouseWasClicked = (clickCount > mouseClicks || wheelCount > mouseWheelMoves);
  119. mouseClicks = clickCount;
  120. mouseWheelMoves = wheelCount;
  121. const Point<float> mousePos (mouseSource.getScreenPosition());
  122. const bool mouseMovedQuickly = mousePos.getDistanceFrom (lastMousePos) > 12;
  123. lastMousePos = mousePos;
  124. if (tipChanged || mouseWasClicked || mouseMovedQuickly)
  125. lastCompChangeTime = now;
  126. if (isVisible() || now < lastHideTime + 500)
  127. {
  128. // if a tip is currently visible (or has just disappeared), update to a new one
  129. // immediately if needed..
  130. if (newComp == nullptr || mouseWasClicked || newTip.isEmpty())
  131. {
  132. if (isVisible())
  133. {
  134. lastHideTime = now;
  135. hideTip();
  136. }
  137. }
  138. else if (tipChanged)
  139. {
  140. displayTip (mousePos.roundToInt(), newTip);
  141. }
  142. }
  143. else
  144. {
  145. // if there isn't currently a tip, but one is needed, only let it
  146. // appear after a timeout..
  147. if (newTip.isNotEmpty()
  148. && newTip != tipShowing
  149. && now > lastCompChangeTime + (unsigned int) millisecondsBeforeTipAppears)
  150. {
  151. displayTip (mousePos.roundToInt(), newTip);
  152. }
  153. }
  154. }