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.

120 lines
3.8KB

  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. ThreadWithProgressWindow::ThreadWithProgressWindow (const String& title,
  21. const bool hasProgressBar,
  22. const bool hasCancelButton,
  23. const int cancellingTimeOutMs,
  24. const String& cancelButtonText,
  25. Component* componentToCentreAround)
  26. : Thread ("ThreadWithProgressWindow"),
  27. progress (0.0),
  28. timeOutMsWhenCancelling (cancellingTimeOutMs),
  29. wasCancelledByUser (false)
  30. {
  31. alertWindow.reset (LookAndFeel::getDefaultLookAndFeel()
  32. .createAlertWindow (title, {},
  33. cancelButtonText.isEmpty() ? TRANS("Cancel")
  34. : cancelButtonText,
  35. {}, {}, MessageBoxIconType::NoIcon, hasCancelButton ? 1 : 0,
  36. componentToCentreAround));
  37. // if there are no buttons, we won't allow the user to interrupt the thread.
  38. alertWindow->setEscapeKeyCancels (false);
  39. if (hasProgressBar)
  40. alertWindow->addProgressBarComponent (progress);
  41. }
  42. ThreadWithProgressWindow::~ThreadWithProgressWindow()
  43. {
  44. stopThread (timeOutMsWhenCancelling);
  45. }
  46. void ThreadWithProgressWindow::launchThread (int priority)
  47. {
  48. JUCE_ASSERT_MESSAGE_THREAD
  49. startThread (priority);
  50. startTimer (100);
  51. {
  52. const ScopedLock sl (messageLock);
  53. alertWindow->setMessage (message);
  54. }
  55. alertWindow->enterModalState();
  56. }
  57. void ThreadWithProgressWindow::setProgress (const double newProgress)
  58. {
  59. progress = newProgress;
  60. }
  61. void ThreadWithProgressWindow::setStatusMessage (const String& newStatusMessage)
  62. {
  63. const ScopedLock sl (messageLock);
  64. message = newStatusMessage;
  65. }
  66. void ThreadWithProgressWindow::timerCallback()
  67. {
  68. bool threadStillRunning = isThreadRunning();
  69. if (! (threadStillRunning && alertWindow->isCurrentlyModal (false)))
  70. {
  71. stopTimer();
  72. stopThread (timeOutMsWhenCancelling);
  73. alertWindow->exitModalState (1);
  74. alertWindow->setVisible (false);
  75. wasCancelledByUser = threadStillRunning;
  76. threadComplete (threadStillRunning);
  77. return; // (this may be deleted now)
  78. }
  79. const ScopedLock sl (messageLock);
  80. alertWindow->setMessage (message);
  81. }
  82. void ThreadWithProgressWindow::threadComplete (bool) {}
  83. #if JUCE_MODAL_LOOPS_PERMITTED
  84. bool ThreadWithProgressWindow::runThread (const int priority)
  85. {
  86. launchThread (priority);
  87. while (isTimerRunning())
  88. MessageManager::getInstance()->runDispatchLoopUntil (5);
  89. return ! wasCancelledByUser;
  90. }
  91. #endif
  92. } // namespace juce