The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

210 lines
6.3KB

  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. #if JUCE_DEBUG
  54. static Array<TooltipWindow*> activeTooltipWindows;
  55. #endif
  56. void TooltipWindow::displayTip (Point<int> screenPos, const String& tip)
  57. {
  58. jassert (tip.isNotEmpty());
  59. if (! reentrant)
  60. {
  61. ScopedValueSetter<bool> setter (reentrant, true, false);
  62. if (tipShowing != tip)
  63. {
  64. tipShowing = tip;
  65. repaint();
  66. }
  67. if (auto* parent = getParentComponent())
  68. {
  69. updatePosition (tip, parent->getLocalPoint (nullptr, screenPos),
  70. parent->getLocalBounds());
  71. }
  72. else
  73. {
  74. updatePosition (tip, screenPos, Desktop::getInstance().getDisplays().findDisplayForPoint (screenPos).userArea);
  75. addToDesktop (ComponentPeer::windowHasDropShadow
  76. | ComponentPeer::windowIsTemporary
  77. | ComponentPeer::windowIgnoresKeyPresses
  78. | ComponentPeer::windowIgnoresMouseClicks);
  79. }
  80. #if JUCE_DEBUG
  81. activeTooltipWindows.addIfNotAlreadyThere (this);
  82. for (auto* w : activeTooltipWindows)
  83. {
  84. if (w != this && w->tipShowing == tipShowing)
  85. {
  86. // Looks like you have more than one TooltipWindow showing the same tip..
  87. // Be careful not to create more than one instance of this class with the
  88. // same parent component!
  89. jassertfalse;
  90. }
  91. }
  92. #endif
  93. toFront (false);
  94. }
  95. }
  96. String TooltipWindow::getTipFor (Component& c)
  97. {
  98. if (Process::isForegroundProcess()
  99. && ! ModifierKeys::currentModifiers.isAnyMouseButtonDown())
  100. {
  101. if (auto* ttc = dynamic_cast<TooltipClient*> (&c))
  102. if (! c.isCurrentlyBlockedByAnotherModalComponent())
  103. return ttc->getTooltip();
  104. }
  105. return {};
  106. }
  107. void TooltipWindow::hideTip()
  108. {
  109. if (! reentrant)
  110. {
  111. tipShowing.clear();
  112. removeFromDesktop();
  113. setVisible (false);
  114. #if JUCE_DEBUG
  115. activeTooltipWindows.removeAllInstancesOf (this);
  116. #endif
  117. }
  118. }
  119. void TooltipWindow::timerCallback()
  120. {
  121. auto& desktop = Desktop::getInstance();
  122. auto mouseSource = desktop.getMainMouseSource();
  123. auto now = Time::getApproximateMillisecondCounter();
  124. auto* newComp = mouseSource.isTouch() ? nullptr : mouseSource.getComponentUnderMouse();
  125. auto* parent = getParentComponent();
  126. if (newComp == nullptr || parent == nullptr || parent == newComp || parent->isParentOf (newComp))
  127. {
  128. auto newTip = newComp != nullptr ? getTipFor (*newComp) : String();
  129. bool tipChanged = (newTip != lastTipUnderMouse || newComp != lastComponentUnderMouse);
  130. lastComponentUnderMouse = newComp;
  131. lastTipUnderMouse = newTip;
  132. auto clickCount = desktop.getMouseButtonClickCounter();
  133. auto wheelCount = desktop.getMouseWheelMoveCounter();
  134. bool mouseWasClicked = (clickCount > mouseClicks || wheelCount > mouseWheelMoves);
  135. mouseClicks = clickCount;
  136. mouseWheelMoves = wheelCount;
  137. auto mousePos = mouseSource.getScreenPosition();
  138. bool mouseMovedQuickly = mousePos.getDistanceFrom (lastMousePos) > 12;
  139. lastMousePos = mousePos;
  140. if (tipChanged || mouseWasClicked || mouseMovedQuickly)
  141. lastCompChangeTime = now;
  142. if (isVisible() || now < lastHideTime + 500)
  143. {
  144. // if a tip is currently visible (or has just disappeared), update to a new one
  145. // immediately if needed..
  146. if (newComp == nullptr || mouseWasClicked || newTip.isEmpty())
  147. {
  148. if (isVisible())
  149. {
  150. lastHideTime = now;
  151. hideTip();
  152. }
  153. }
  154. else if (tipChanged)
  155. {
  156. displayTip (mousePos.roundToInt(), newTip);
  157. }
  158. }
  159. else
  160. {
  161. // if there isn't currently a tip, but one is needed, only let it
  162. // appear after a timeout..
  163. if (newTip.isNotEmpty()
  164. && newTip != tipShowing
  165. && now > lastCompChangeTime + (uint32) millisecondsBeforeTipAppears)
  166. {
  167. displayTip (mousePos.roundToInt(), newTip);
  168. }
  169. }
  170. }
  171. }
  172. } // namespace juce