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.

169 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. repaint();
  49. }
  50. void ProgressBar::paint (Graphics& g)
  51. {
  52. String text;
  53. if (displayPercentage)
  54. {
  55. if (currentValue >= 0 && currentValue <= 1.0)
  56. text << roundToInt (currentValue * 100.0) << '%';
  57. }
  58. else
  59. {
  60. text = displayedMessage;
  61. }
  62. getLookAndFeel().drawProgressBar (g, *this,
  63. getWidth(), getHeight(),
  64. currentValue, text);
  65. }
  66. void ProgressBar::visibilityChanged()
  67. {
  68. if (isVisible())
  69. startTimer (30);
  70. else
  71. stopTimer();
  72. }
  73. void ProgressBar::timerCallback()
  74. {
  75. double newProgress = progress;
  76. const uint32 now = Time::getMillisecondCounter();
  77. const int timeSinceLastCallback = (int) (now - lastCallbackTime);
  78. lastCallbackTime = now;
  79. if (! approximatelyEqual (currentValue, newProgress)
  80. || newProgress < 0 || newProgress >= 1.0
  81. || currentMessage != displayedMessage)
  82. {
  83. if (currentValue < newProgress
  84. && newProgress >= 0 && newProgress < 1.0
  85. && currentValue >= 0 && currentValue < 1.0)
  86. {
  87. newProgress = jmin (currentValue + 0.0008 * timeSinceLastCallback,
  88. newProgress);
  89. }
  90. currentValue = newProgress;
  91. currentMessage = displayedMessage;
  92. repaint();
  93. if (auto* handler = getAccessibilityHandler())
  94. handler->notifyAccessibilityEvent (AccessibilityEvent::valueChanged);
  95. }
  96. }
  97. //==============================================================================
  98. std::unique_ptr<AccessibilityHandler> ProgressBar::createAccessibilityHandler()
  99. {
  100. class ProgressBarAccessibilityHandler : public AccessibilityHandler
  101. {
  102. public:
  103. explicit ProgressBarAccessibilityHandler (ProgressBar& progressBarToWrap)
  104. : AccessibilityHandler (progressBarToWrap,
  105. AccessibilityRole::progressBar,
  106. AccessibilityActions{},
  107. AccessibilityHandler::Interfaces { std::make_unique<ValueInterface> (progressBarToWrap) }),
  108. progressBar (progressBarToWrap)
  109. {
  110. }
  111. String getHelp() const override { return progressBar.getTooltip(); }
  112. private:
  113. class ValueInterface : public AccessibilityRangedNumericValueInterface
  114. {
  115. public:
  116. explicit ValueInterface (ProgressBar& progressBarToWrap)
  117. : progressBar (progressBarToWrap)
  118. {
  119. }
  120. bool isReadOnly() const override { return true; }
  121. void setValue (double) override { jassertfalse; }
  122. double getCurrentValue() const override { return progressBar.progress; }
  123. AccessibleValueRange getRange() const override { return { { 0.0, 1.0 }, 0.001 }; }
  124. private:
  125. ProgressBar& progressBar;
  126. //==============================================================================
  127. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ValueInterface)
  128. };
  129. ProgressBar& progressBar;
  130. //==============================================================================
  131. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ProgressBarAccessibilityHandler)
  132. };
  133. return std::make_unique<ProgressBarAccessibilityHandler> (*this);
  134. }
  135. } // namespace juce