The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

181 lines
5.3KB

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