The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

182 lines
6.0KB

  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. /*
  21. Instances of this type can show and dismiss a message box.
  22. This is an interface rather than a concrete type so that platforms can pick an implementation at
  23. runtime if necessary.
  24. */
  25. struct ScopedMessageBoxInterface
  26. {
  27. virtual ~ScopedMessageBoxInterface() = default;
  28. /* Shows the message box.
  29. When the message box exits normally, it should send the result to the passed-in function.
  30. The passed-in function is safe to call from any thread at any time.
  31. */
  32. virtual void runAsync (std::function<void (int)>) = 0;
  33. /* Shows the message box and blocks. */
  34. virtual int runSync() = 0;
  35. /* Forcefully closes the message box.
  36. This will be called when the message box handle has fallen out of scope.
  37. If the message box has already been closed by the user, this shouldn't do anything.
  38. */
  39. virtual void close() = 0;
  40. /* Implemented differently for each platform. */
  41. static std::unique_ptr<ScopedMessageBoxInterface> create (const MessageBoxOptions& options);
  42. };
  43. //==============================================================================
  44. class ScopedMessageBox::Pimpl : private AsyncUpdater
  45. {
  46. public:
  47. static ScopedMessageBox show (std::unique_ptr<ScopedMessageBoxInterface>&& native, std::function<void (int)> callback)
  48. {
  49. return ScopedMessageBox (runAsync (std::move (native), rawToUniquePtr (ModalCallbackFunction::create (std::move (callback)))));
  50. }
  51. static int showUnmanaged (std::unique_ptr<ScopedMessageBoxInterface>&& native,
  52. ModalComponentManager::Callback* cb)
  53. {
  54. #if JUCE_MODAL_LOOPS_PERMITTED
  55. if (cb == nullptr)
  56. return runSync (std::move (native));
  57. #endif
  58. runAsync (std::move (native), rawToUniquePtr (cb));
  59. return 0;
  60. }
  61. ~Pimpl() override
  62. {
  63. cancelPendingUpdate();
  64. }
  65. void close()
  66. {
  67. cancelPendingUpdate();
  68. nativeImplementation->close();
  69. self.reset();
  70. }
  71. private:
  72. static std::shared_ptr<Pimpl> runAsync (std::unique_ptr<ScopedMessageBoxInterface>&& p,
  73. std::unique_ptr<ModalComponentManager::Callback>&& c)
  74. {
  75. std::shared_ptr<Pimpl> result (new Pimpl (std::move (p), std::move (c)));
  76. result->self = result;
  77. result->triggerAsyncUpdate();
  78. return result;
  79. }
  80. static int runSync (std::unique_ptr<ScopedMessageBoxInterface>&& p)
  81. {
  82. auto local = std::move (p);
  83. return local != nullptr ? local->runSync() : 0;
  84. }
  85. explicit Pimpl (std::unique_ptr<ScopedMessageBoxInterface>&& p)
  86. : Pimpl (std::move (p), nullptr) {}
  87. Pimpl (std::unique_ptr<ScopedMessageBoxInterface>&& p,
  88. std::unique_ptr<ModalComponentManager::Callback>&& c)
  89. : callback (std::move (c)), nativeImplementation (std::move (p)) {}
  90. void handleAsyncUpdate() override
  91. {
  92. nativeImplementation->runAsync ([weakRecipient = std::weak_ptr<Pimpl> (self)] (int result)
  93. {
  94. const auto notifyRecipient = [result, weakRecipient]
  95. {
  96. if (const auto locked = weakRecipient.lock())
  97. {
  98. if (auto* cb = locked->callback.get())
  99. cb->modalStateFinished (result);
  100. locked->self.reset();
  101. }
  102. };
  103. if (MessageManager::getInstance()->isThisTheMessageThread())
  104. notifyRecipient();
  105. else
  106. MessageManager::callAsync (notifyRecipient);
  107. });
  108. }
  109. std::unique_ptr<ModalComponentManager::Callback> callback;
  110. std::unique_ptr<ScopedMessageBoxInterface> nativeImplementation;
  111. /* The 'old' native message box API doesn't have a concept of message box owners.
  112. Instead, message boxes have to clean up after themselves, once they're done displaying.
  113. To allow this mode of usage, the implementation keeps an owning reference to itself,
  114. which is cleared once the message box is closed or asked to quit. To display a native
  115. message box without a scoped lifetime, just create a Pimpl instance without using
  116. the ScopedMessageBox wrapper, and the Pimpl will destroy itself after it is dismissed.
  117. */
  118. std::shared_ptr<Pimpl> self;
  119. };
  120. //==============================================================================
  121. ScopedMessageBox::ScopedMessageBox() = default;
  122. ScopedMessageBox::ScopedMessageBox (std::shared_ptr<Pimpl> p)
  123. : pimpl (std::move (p)) {}
  124. ScopedMessageBox::~ScopedMessageBox() noexcept
  125. {
  126. close();
  127. }
  128. ScopedMessageBox::ScopedMessageBox (ScopedMessageBox&& other) noexcept
  129. : pimpl (std::exchange (other.pimpl, nullptr)) {}
  130. ScopedMessageBox& ScopedMessageBox::operator= (ScopedMessageBox&& other) noexcept
  131. {
  132. ScopedMessageBox temp (std::move (other));
  133. std::swap (temp.pimpl, pimpl);
  134. return *this;
  135. }
  136. void ScopedMessageBox::close()
  137. {
  138. if (pimpl != nullptr)
  139. pimpl->close();
  140. pimpl.reset();
  141. }
  142. } // namespace juce