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.4KB

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