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.

juce_TooltipWindow.cpp 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  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 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. namespace juce
  20. {
  21. TooltipWindow::TooltipWindow (Component* parentComp, int delayMs)
  22. : Component ("tooltip"),
  23. millisecondsBeforeTipAppears (delayMs)
  24. {
  25. setAlwaysOnTop (true);
  26. setOpaque (true);
  27. if (parentComp != nullptr)
  28. parentComp->addChildComponent (this);
  29. if (Desktop::getInstance().getMainMouseSource().canHover())
  30. startTimer (123);
  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 (auto* 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. | ComponentPeer::windowIgnoresMouseClicks);
  77. }
  78. toFront (false);
  79. }
  80. }
  81. String TooltipWindow::getTipFor (Component& c)
  82. {
  83. if (Process::isForegroundProcess()
  84. && ! ModifierKeys::getCurrentModifiers().isAnyMouseButtonDown())
  85. {
  86. if (auto* ttc = dynamic_cast<TooltipClient*> (&c))
  87. if (! c.isCurrentlyBlockedByAnotherModalComponent())
  88. return ttc->getTooltip();
  89. }
  90. return {};
  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. auto& desktop = Desktop::getInstance();
  104. auto mouseSource = desktop.getMainMouseSource();
  105. auto now = Time::getApproximateMillisecondCounter();
  106. auto* newComp = mouseSource.isTouch() ? nullptr : mouseSource.getComponentUnderMouse();
  107. auto newTip = newComp != nullptr ? getTipFor (*newComp) : String();
  108. bool tipChanged = (newTip != lastTipUnderMouse || newComp != lastComponentUnderMouse);
  109. lastComponentUnderMouse = newComp;
  110. lastTipUnderMouse = newTip;
  111. auto clickCount = desktop.getMouseButtonClickCounter();
  112. auto wheelCount = desktop.getMouseWheelMoveCounter();
  113. bool mouseWasClicked = (clickCount > mouseClicks || wheelCount > mouseWheelMoves);
  114. mouseClicks = clickCount;
  115. mouseWheelMoves = wheelCount;
  116. auto mousePos = mouseSource.getScreenPosition();
  117. 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 + (uint32) millisecondsBeforeTipAppears)
  145. {
  146. displayTip (mousePos.roundToInt(), newTip);
  147. }
  148. }
  149. }
  150. } // namespace juce