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.

177 lines
5.6KB

  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. millisecondsBeforeTipAppears (delayMs),
  20. mouseClicks (0),
  21. mouseWheelMoves (0),
  22. lastHideTime (0),
  23. lastComponentUnderMouse (nullptr)
  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 (tipShowing != tip)
  62. repaint();
  63. tipShowing = tip;
  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. String TooltipWindow::getTipFor (Component* const c)
  80. {
  81. if (c != nullptr
  82. && Process::isForegroundProcess()
  83. && ! ModifierKeys::getCurrentModifiers().isAnyMouseButtonDown())
  84. {
  85. if (TooltipClient* const ttc = dynamic_cast <TooltipClient*> (c))
  86. if (! c->isCurrentlyBlockedByAnotherModalComponent())
  87. return ttc->getTooltip();
  88. }
  89. return String::empty;
  90. }
  91. void TooltipWindow::hideTip()
  92. {
  93. tipShowing.clear();
  94. removeFromDesktop();
  95. setVisible (false);
  96. }
  97. void TooltipWindow::timerCallback()
  98. {
  99. Desktop& desktop = Desktop::getInstance();
  100. const MouseInputSource mouseSource (desktop.getMainMouseSource());
  101. const unsigned int now = Time::getApproximateMillisecondCounter();
  102. Component* const newComp = mouseSource.isMouse() ? mouseSource.getComponentUnderMouse() : nullptr;
  103. const String newTip (getTipFor (newComp));
  104. const bool tipChanged = (newTip != lastTipUnderMouse || newComp != lastComponentUnderMouse);
  105. lastComponentUnderMouse = newComp;
  106. lastTipUnderMouse = newTip;
  107. const int clickCount = desktop.getMouseButtonClickCounter();
  108. const int wheelCount = desktop.getMouseWheelMoveCounter();
  109. const bool mouseWasClicked = (clickCount > mouseClicks || wheelCount > mouseWheelMoves);
  110. mouseClicks = clickCount;
  111. mouseWheelMoves = wheelCount;
  112. const Point<float> mousePos (mouseSource.getScreenPosition());
  113. const bool mouseMovedQuickly = mousePos.getDistanceFrom (lastMousePos) > 12;
  114. lastMousePos = mousePos;
  115. if (tipChanged || mouseWasClicked || mouseMovedQuickly)
  116. lastCompChangeTime = now;
  117. if (isVisible() || now < lastHideTime + 500)
  118. {
  119. // if a tip is currently visible (or has just disappeared), update to a new one
  120. // immediately if needed..
  121. if (newComp == nullptr || mouseWasClicked || newTip.isEmpty())
  122. {
  123. if (isVisible())
  124. {
  125. lastHideTime = now;
  126. hideTip();
  127. }
  128. }
  129. else if (tipChanged)
  130. {
  131. displayTip (mousePos.roundToInt(), newTip);
  132. }
  133. }
  134. else
  135. {
  136. // if there isn't currently a tip, but one is needed, only let it
  137. // appear after a timeout..
  138. if (newTip.isNotEmpty()
  139. && newTip != tipShowing
  140. && now > lastCompChangeTime + (unsigned int) millisecondsBeforeTipAppears)
  141. {
  142. displayTip (mousePos.roundToInt(), newTip);
  143. }
  144. }
  145. }