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.

126 lines
4.0KB

  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. BubbleMessageComponent::BubbleMessageComponent (int fadeOutLengthMs)
  22. : fadeOutLength (fadeOutLengthMs), mouseClickCounter (0),
  23. expiryTime (0), deleteAfterUse (false)
  24. {
  25. }
  26. BubbleMessageComponent::~BubbleMessageComponent()
  27. {
  28. }
  29. void BubbleMessageComponent::showAt (const Rectangle<int>& pos,
  30. const AttributedString& text,
  31. const int numMillisecondsBeforeRemoving,
  32. const bool removeWhenMouseClicked,
  33. const bool deleteSelfAfterUse)
  34. {
  35. createLayout (text);
  36. setPosition (pos);
  37. init (numMillisecondsBeforeRemoving, removeWhenMouseClicked, deleteSelfAfterUse);
  38. }
  39. void BubbleMessageComponent::showAt (Component* const component,
  40. const AttributedString& text,
  41. const int numMillisecondsBeforeRemoving,
  42. const bool removeWhenMouseClicked,
  43. const bool deleteSelfAfterUse)
  44. {
  45. createLayout (text);
  46. setPosition (component);
  47. init (numMillisecondsBeforeRemoving, removeWhenMouseClicked, deleteSelfAfterUse);
  48. }
  49. void BubbleMessageComponent::createLayout (const AttributedString& text)
  50. {
  51. textLayout.createLayoutWithBalancedLineLengths (text, 256);
  52. }
  53. void BubbleMessageComponent::init (const int numMillisecondsBeforeRemoving,
  54. const bool removeWhenMouseClicked,
  55. const bool deleteSelfAfterUse)
  56. {
  57. setAlpha (1.0f);
  58. setVisible (true);
  59. deleteAfterUse = deleteSelfAfterUse;
  60. expiryTime = numMillisecondsBeforeRemoving > 0
  61. ? (Time::getMillisecondCounter() + (uint32) numMillisecondsBeforeRemoving) : 0;
  62. mouseClickCounter = Desktop::getInstance().getMouseButtonClickCounter();
  63. if (! (removeWhenMouseClicked && isShowing()))
  64. mouseClickCounter += 0xfffff;
  65. startTimer (77);
  66. repaint();
  67. }
  68. const float bubblePaddingX = 20.0f;
  69. const float bubblePaddingY = 14.0f;
  70. void BubbleMessageComponent::getContentSize (int& w, int& h)
  71. {
  72. w = (int) (bubblePaddingX + textLayout.getWidth());
  73. h = (int) (bubblePaddingY + textLayout.getHeight());
  74. }
  75. void BubbleMessageComponent::paintContent (Graphics& g, int w, int h)
  76. {
  77. g.setColour (findColour (TooltipWindow::textColourId));
  78. textLayout.draw (g, Rectangle<float> (bubblePaddingX / 2.0f, bubblePaddingY / 2.0f,
  79. w - bubblePaddingX, h - bubblePaddingY));
  80. }
  81. void BubbleMessageComponent::timerCallback()
  82. {
  83. if (Desktop::getInstance().getMouseButtonClickCounter() > mouseClickCounter)
  84. hide (false);
  85. else if (expiryTime != 0 && Time::getMillisecondCounter() > expiryTime)
  86. hide (true);
  87. }
  88. void BubbleMessageComponent::hide (const bool fadeOut)
  89. {
  90. stopTimer();
  91. if (fadeOut)
  92. Desktop::getInstance().getAnimator().fadeOut (this, fadeOutLength);
  93. else
  94. setVisible (false);
  95. if (deleteAfterUse)
  96. delete this;
  97. }
  98. } // namespace juce