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.

juce_ThreadWithProgressWindow.h 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. //==============================================================================
  16. /**
  17. A thread that automatically pops up a modal dialog box with a progress bar
  18. and cancel button while it's busy running.
  19. These are handy for performing some sort of task while giving the user feedback
  20. about how long there is to go, etc.
  21. E.g. @code
  22. class MyTask : public ThreadWithProgressWindow
  23. {
  24. public:
  25. MyTask() : ThreadWithProgressWindow ("busy...", true, true)
  26. {
  27. }
  28. void run()
  29. {
  30. for (int i = 0; i < thingsToDo; ++i)
  31. {
  32. // must check this as often as possible, because this is
  33. // how we know if the user's pressed 'cancel'
  34. if (threadShouldExit())
  35. break;
  36. // this will update the progress bar on the dialog box
  37. setProgress (i / (double) thingsToDo);
  38. // ... do the business here...
  39. }
  40. }
  41. };
  42. void doTheTask()
  43. {
  44. MyTask m;
  45. if (m.runThread())
  46. {
  47. // thread finished normally..
  48. }
  49. else
  50. {
  51. // user pressed the cancel button..
  52. }
  53. }
  54. @endcode
  55. @see Thread, AlertWindow
  56. @tags{GUI}
  57. */
  58. class JUCE_API ThreadWithProgressWindow : public Thread,
  59. private Timer
  60. {
  61. public:
  62. //==============================================================================
  63. /** Creates the thread.
  64. Initially, the dialog box won't be visible, it'll only appear when the
  65. runThread() method is called.
  66. @param windowTitle the title to go at the top of the dialog box
  67. @param hasProgressBar whether the dialog box should have a progress bar (see
  68. setProgress() )
  69. @param hasCancelButton whether the dialog box should have a cancel button
  70. @param timeOutMsWhenCancelling when 'cancel' is pressed, this is how long to wait for
  71. the thread to stop before killing it forcibly (see
  72. Thread::stopThread() )
  73. @param cancelButtonText the text that should be shown in the cancel button
  74. (if it has one). Leave this empty for the default "Cancel"
  75. @param componentToCentreAround if this is non-null, the window will be positioned
  76. so that it's centred around this component.
  77. */
  78. ThreadWithProgressWindow (const String& windowTitle,
  79. bool hasProgressBar,
  80. bool hasCancelButton,
  81. int timeOutMsWhenCancelling = 10000,
  82. const String& cancelButtonText = String(),
  83. Component* componentToCentreAround = nullptr);
  84. /** Destructor. */
  85. ~ThreadWithProgressWindow() override;
  86. //==============================================================================
  87. #if JUCE_MODAL_LOOPS_PERMITTED
  88. /** Starts the thread and waits for it to finish.
  89. This will start the thread, make the dialog box appear, and wait until either
  90. the thread finishes normally, or until the cancel button is pressed.
  91. Before returning, the dialog box will be hidden.
  92. @param priority the priority to use when starting the thread - see
  93. Thread::startThread() for values
  94. @returns true if the thread finished normally; false if the user pressed cancel
  95. */
  96. bool runThread (int priority = 5);
  97. #endif
  98. /** Starts the thread and returns.
  99. This will start the thread and make the dialog box appear in a modal state. When
  100. the thread finishes normally, or the cancel button is pressed, the window will be
  101. hidden and the threadComplete() method will be called.
  102. @param priority the priority to use when starting the thread - see
  103. Thread::startThread() for values
  104. */
  105. void launchThread (int priority = 5);
  106. /** The thread should call this periodically to update the position of the progress bar.
  107. @param newProgress the progress, from 0.0 to 1.0
  108. @see setStatusMessage
  109. */
  110. void setProgress (double newProgress);
  111. /** The thread can call this to change the message that's displayed in the dialog box. */
  112. void setStatusMessage (const String& newStatusMessage);
  113. /** Returns the AlertWindow that is being used. */
  114. AlertWindow* getAlertWindow() const noexcept { return alertWindow.get(); }
  115. //==============================================================================
  116. /** This method is called (on the message thread) when the operation has finished.
  117. You may choose to use this callback to delete the ThreadWithProgressWindow object.
  118. */
  119. virtual void threadComplete (bool userPressedCancel);
  120. private:
  121. //==============================================================================
  122. void timerCallback() override;
  123. double progress;
  124. std::unique_ptr<AlertWindow> alertWindow;
  125. String message;
  126. CriticalSection messageLock;
  127. const int timeOutMsWhenCancelling;
  128. bool wasCancelledByUser;
  129. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ThreadWithProgressWindow)
  130. };
  131. } // namespace juce