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.

113 lines
3.5KB

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