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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - 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 6 End-User License
  8. Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
  9. End User License Agreement: www.juce.com/juce-6-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. //==============================================================================
  21. /**
  22. A thread that automatically pops up a modal dialog box with a progress bar
  23. and cancel button while it's busy running.
  24. These are handy for performing some sort of task while giving the user feedback
  25. about how long there is to go, etc.
  26. E.g. @code
  27. class MyTask : public ThreadWithProgressWindow
  28. {
  29. public:
  30. MyTask() : ThreadWithProgressWindow ("busy...", true, true)
  31. {
  32. }
  33. void run()
  34. {
  35. for (int i = 0; i < thingsToDo; ++i)
  36. {
  37. // must check this as often as possible, because this is
  38. // how we know if the user's pressed 'cancel'
  39. if (threadShouldExit())
  40. break;
  41. // this will update the progress bar on the dialog box
  42. setProgress (i / (double) thingsToDo);
  43. // ... do the business here...
  44. }
  45. }
  46. };
  47. void doTheTask()
  48. {
  49. MyTask m;
  50. if (m.runThread())
  51. {
  52. // thread finished normally..
  53. }
  54. else
  55. {
  56. // user pressed the cancel button..
  57. }
  58. }
  59. @endcode
  60. @see Thread, AlertWindow
  61. @tags{GUI}
  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() override;
  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.get(); }
  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. std::unique_ptr<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