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.

168 lines
5.1KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - 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 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-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. ProgressBar::ProgressBar (double& progress_)
  21. : progress (progress_),
  22. displayPercentage (true),
  23. lastCallbackTime (0)
  24. {
  25. currentValue = jlimit (0.0, 1.0, progress);
  26. }
  27. ProgressBar::~ProgressBar()
  28. {
  29. }
  30. //==============================================================================
  31. void ProgressBar::setPercentageDisplay (const bool shouldDisplayPercentage)
  32. {
  33. displayPercentage = shouldDisplayPercentage;
  34. repaint();
  35. }
  36. void ProgressBar::setTextToDisplay (const String& text)
  37. {
  38. displayPercentage = false;
  39. displayedMessage = text;
  40. }
  41. void ProgressBar::lookAndFeelChanged()
  42. {
  43. setOpaque (getLookAndFeel().isProgressBarOpaque (*this));
  44. }
  45. void ProgressBar::colourChanged()
  46. {
  47. lookAndFeelChanged();
  48. }
  49. void ProgressBar::paint (Graphics& g)
  50. {
  51. String text;
  52. if (displayPercentage)
  53. {
  54. if (currentValue >= 0 && currentValue <= 1.0)
  55. text << roundToInt (currentValue * 100.0) << '%';
  56. }
  57. else
  58. {
  59. text = displayedMessage;
  60. }
  61. getLookAndFeel().drawProgressBar (g, *this,
  62. getWidth(), getHeight(),
  63. currentValue, text);
  64. }
  65. void ProgressBar::visibilityChanged()
  66. {
  67. if (isVisible())
  68. startTimer (30);
  69. else
  70. stopTimer();
  71. }
  72. void ProgressBar::timerCallback()
  73. {
  74. double newProgress = progress;
  75. const uint32 now = Time::getMillisecondCounter();
  76. const int timeSinceLastCallback = (int) (now - lastCallbackTime);
  77. lastCallbackTime = now;
  78. if (currentValue != newProgress
  79. || newProgress < 0 || newProgress >= 1.0
  80. || currentMessage != displayedMessage)
  81. {
  82. if (currentValue < newProgress
  83. && newProgress >= 0 && newProgress < 1.0
  84. && currentValue >= 0 && currentValue < 1.0)
  85. {
  86. newProgress = jmin (currentValue + 0.0008 * timeSinceLastCallback,
  87. newProgress);
  88. }
  89. currentValue = newProgress;
  90. currentMessage = displayedMessage;
  91. repaint();
  92. if (auto* handler = getAccessibilityHandler())
  93. handler->notifyAccessibilityEvent (AccessibilityEvent::valueChanged);
  94. }
  95. }
  96. //==============================================================================
  97. std::unique_ptr<AccessibilityHandler> ProgressBar::createAccessibilityHandler()
  98. {
  99. class ProgressBarAccessibilityHandler : public AccessibilityHandler
  100. {
  101. public:
  102. explicit ProgressBarAccessibilityHandler (ProgressBar& progressBarToWrap)
  103. : AccessibilityHandler (progressBarToWrap,
  104. AccessibilityRole::progressBar,
  105. AccessibilityActions{},
  106. AccessibilityHandler::Interfaces { std::make_unique<ValueInterface> (progressBarToWrap) }),
  107. progressBar (progressBarToWrap)
  108. {
  109. }
  110. String getHelp() const override { return progressBar.getTooltip(); }
  111. private:
  112. class ValueInterface : public AccessibilityRangedNumericValueInterface
  113. {
  114. public:
  115. explicit ValueInterface (ProgressBar& progressBarToWrap)
  116. : progressBar (progressBarToWrap)
  117. {
  118. }
  119. bool isReadOnly() const override { return true; }
  120. void setValue (double) override { jassertfalse; }
  121. double getCurrentValue() const override { return progressBar.progress; }
  122. AccessibleValueRange getRange() const override { return { { 0.0, 1.0 }, 0.001 }; }
  123. private:
  124. ProgressBar& progressBar;
  125. //==============================================================================
  126. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ValueInterface)
  127. };
  128. ProgressBar& progressBar;
  129. //==============================================================================
  130. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ProgressBarAccessibilityHandler)
  131. };
  132. return std::make_unique<ProgressBarAccessibilityHandler> (*this);
  133. }
  134. } // namespace juce