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.

161 lines
4.8KB

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