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.

118 lines
3.8KB

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