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_BubbleMessageComponent.cpp 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - Raw Material Software Limited
  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 6 End-User License
  8. Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
  9. End User License Agreement: www.juce.com/juce-6-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce
  19. {
  20. BubbleMessageComponent::BubbleMessageComponent (int fadeOutLengthMs)
  21. : fadeOutLength (fadeOutLengthMs), mouseClickCounter (0),
  22. expiryTime (0), deleteAfterUse (false)
  23. {
  24. }
  25. BubbleMessageComponent::~BubbleMessageComponent()
  26. {
  27. }
  28. void BubbleMessageComponent::showAt (const Rectangle<int>& pos,
  29. const AttributedString& text,
  30. const int numMillisecondsBeforeRemoving,
  31. const bool removeWhenMouseClicked,
  32. const bool deleteSelfAfterUse)
  33. {
  34. createLayout (text);
  35. setPosition (pos);
  36. init (numMillisecondsBeforeRemoving, removeWhenMouseClicked, deleteSelfAfterUse);
  37. }
  38. void BubbleMessageComponent::showAt (Component* const component,
  39. const AttributedString& text,
  40. const int numMillisecondsBeforeRemoving,
  41. const bool removeWhenMouseClicked,
  42. const bool deleteSelfAfterUse)
  43. {
  44. createLayout (text);
  45. setPosition (component);
  46. init (numMillisecondsBeforeRemoving, removeWhenMouseClicked, deleteSelfAfterUse);
  47. }
  48. void BubbleMessageComponent::createLayout (const AttributedString& text)
  49. {
  50. textLayout.createLayoutWithBalancedLineLengths (text, 256);
  51. }
  52. void BubbleMessageComponent::init (const int numMillisecondsBeforeRemoving,
  53. const bool removeWhenMouseClicked,
  54. const bool deleteSelfAfterUse)
  55. {
  56. setAlpha (1.0f);
  57. setVisible (true);
  58. deleteAfterUse = deleteSelfAfterUse;
  59. expiryTime = numMillisecondsBeforeRemoving > 0
  60. ? (Time::getMillisecondCounter() + (uint32) numMillisecondsBeforeRemoving) : 0;
  61. mouseClickCounter = Desktop::getInstance().getMouseButtonClickCounter();
  62. if (! (removeWhenMouseClicked && isShowing()))
  63. mouseClickCounter += 0xfffff;
  64. startTimer (77);
  65. repaint();
  66. }
  67. const float bubblePaddingX = 20.0f;
  68. const float bubblePaddingY = 14.0f;
  69. void BubbleMessageComponent::getContentSize (int& w, int& h)
  70. {
  71. w = (int) (bubblePaddingX + textLayout.getWidth());
  72. h = (int) (bubblePaddingY + textLayout.getHeight());
  73. }
  74. void BubbleMessageComponent::paintContent (Graphics& g, int w, int h)
  75. {
  76. g.setColour (findColour (TooltipWindow::textColourId));
  77. textLayout.draw (g, Rectangle<float> (bubblePaddingX / 2.0f, bubblePaddingY / 2.0f,
  78. (float) w - bubblePaddingX, (float) h - bubblePaddingY));
  79. }
  80. void BubbleMessageComponent::timerCallback()
  81. {
  82. if (Desktop::getInstance().getMouseButtonClickCounter() > mouseClickCounter)
  83. hide (false);
  84. else if (expiryTime != 0 && Time::getMillisecondCounter() > expiryTime)
  85. hide (true);
  86. }
  87. void BubbleMessageComponent::hide (const bool fadeOut)
  88. {
  89. stopTimer();
  90. if (fadeOut)
  91. Desktop::getInstance().getAnimator().fadeOut (this, fadeOutLength);
  92. else
  93. setVisible (false);
  94. if (deleteAfterUse)
  95. delete this;
  96. }
  97. } // namespace juce