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.

133 lines
5.3KB

  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::detail
  19. {
  20. //==============================================================================
  21. class ScopedMessageBoxImpl
  22. {
  23. public:
  24. virtual ~ScopedMessageBoxImpl() = default;
  25. virtual void close() = 0;
  26. };
  27. //==============================================================================
  28. class ConcreteScopedMessageBoxImpl : public ScopedMessageBoxImpl,
  29. private AsyncUpdater
  30. {
  31. public:
  32. static ScopedMessageBox show (std::unique_ptr<ScopedMessageBoxInterface>&& native,
  33. std::function<void (int)> callback)
  34. {
  35. return ScopedMessageBox (runAsync (std::move (native),
  36. rawToUniquePtr (ModalCallbackFunction::create (std::move (callback)))));
  37. }
  38. static int showUnmanaged (std::unique_ptr<ScopedMessageBoxInterface>&& native,
  39. ModalComponentManager::Callback* cb)
  40. {
  41. #if JUCE_MODAL_LOOPS_PERMITTED
  42. if (cb == nullptr)
  43. return runSync (std::move (native));
  44. #endif
  45. runAsync (std::move (native), rawToUniquePtr (cb));
  46. return 0;
  47. }
  48. ~ConcreteScopedMessageBoxImpl() override
  49. {
  50. cancelPendingUpdate();
  51. }
  52. void close() override
  53. {
  54. cancelPendingUpdate();
  55. nativeImplementation->close();
  56. self.reset();
  57. }
  58. private:
  59. static std::shared_ptr<ConcreteScopedMessageBoxImpl> runAsync (std::unique_ptr<ScopedMessageBoxInterface>&& p,
  60. std::unique_ptr<ModalComponentManager::Callback>&& c)
  61. {
  62. std::shared_ptr<ConcreteScopedMessageBoxImpl> result (new ConcreteScopedMessageBoxImpl (std::move (p), std::move (c)));
  63. result->self = result;
  64. result->triggerAsyncUpdate();
  65. return result;
  66. }
  67. static int runSync (std::unique_ptr<ScopedMessageBoxInterface>&& p)
  68. {
  69. auto local = std::move (p);
  70. return local != nullptr ? local->runSync() : 0;
  71. }
  72. explicit ConcreteScopedMessageBoxImpl (std::unique_ptr<ScopedMessageBoxInterface>&& p)
  73. : ConcreteScopedMessageBoxImpl (std::move (p), nullptr) {}
  74. ConcreteScopedMessageBoxImpl (std::unique_ptr<ScopedMessageBoxInterface>&& p,
  75. std::unique_ptr<ModalComponentManager::Callback>&& c)
  76. : callback (std::move (c)), nativeImplementation (std::move (p)) {}
  77. void handleAsyncUpdate() override
  78. {
  79. nativeImplementation->runAsync ([weakRecipient = std::weak_ptr<ConcreteScopedMessageBoxImpl> (self)] (int result)
  80. {
  81. const auto notifyRecipient = [result, weakRecipient]
  82. {
  83. if (const auto locked = weakRecipient.lock())
  84. {
  85. if (auto* cb = locked->callback.get())
  86. cb->modalStateFinished (result);
  87. locked->self.reset();
  88. }
  89. };
  90. if (MessageManager::getInstance()->isThisTheMessageThread())
  91. notifyRecipient();
  92. else
  93. MessageManager::callAsync (notifyRecipient);
  94. });
  95. }
  96. std::unique_ptr<ModalComponentManager::Callback> callback;
  97. std::unique_ptr<ScopedMessageBoxInterface> nativeImplementation;
  98. /* The 'old' native message box API doesn't have a concept of message box owners.
  99. Instead, message boxes have to clean up after themselves, once they're done displaying.
  100. To allow this mode of usage, the implementation keeps an owning reference to itself,
  101. which is cleared once the message box is closed or asked to quit. To display a native
  102. message box without a scoped lifetime, just create a Pimpl instance without using
  103. the ScopedMessageBox wrapper, and the Pimpl will destroy itself after it is dismissed.
  104. */
  105. std::shared_ptr<ConcreteScopedMessageBoxImpl> self;
  106. };
  107. } // namespace juce::detail