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.

99 lines
4.2KB

  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. class ConcreteScopedContentSharerImpl : public ScopedMessageBoxImpl,
  21. private AsyncUpdater
  22. {
  23. public:
  24. static ScopedMessageBox show (std::unique_ptr<ScopedContentSharerInterface>&& native,
  25. ContentSharer::Callback callback)
  26. {
  27. return ScopedMessageBox (runAsync (std::move (native), std::move (callback)));
  28. }
  29. ~ConcreteScopedContentSharerImpl() override
  30. {
  31. cancelPendingUpdate();
  32. }
  33. void close() override
  34. {
  35. cancelPendingUpdate();
  36. nativeImplementation->close();
  37. self.reset();
  38. }
  39. private:
  40. static std::shared_ptr<ConcreteScopedContentSharerImpl> runAsync (std::unique_ptr<ScopedContentSharerInterface>&& p,
  41. ContentSharer::Callback&& c)
  42. {
  43. std::shared_ptr<ConcreteScopedContentSharerImpl> result (new ConcreteScopedContentSharerImpl (std::move (p), std::move (c)));
  44. result->self = result;
  45. result->triggerAsyncUpdate();
  46. return result;
  47. }
  48. ConcreteScopedContentSharerImpl (std::unique_ptr<ScopedContentSharerInterface>&& p,
  49. ContentSharer::Callback&& c)
  50. : callback (std::move (c)), nativeImplementation (std::move (p)) {}
  51. void handleAsyncUpdate() override
  52. {
  53. nativeImplementation->runAsync ([weakRecipient = std::weak_ptr<ConcreteScopedContentSharerImpl> (self)] (bool result, const String& error)
  54. {
  55. const auto notifyRecipient = [result, error, weakRecipient]
  56. {
  57. if (const auto locked = weakRecipient.lock())
  58. {
  59. NullCheckedInvocation::invoke (locked->callback, result, error);
  60. locked->self.reset();
  61. }
  62. };
  63. if (MessageManager::getInstance()->isThisTheMessageThread())
  64. notifyRecipient();
  65. else
  66. MessageManager::callAsync (notifyRecipient);
  67. });
  68. }
  69. ContentSharer::Callback callback;
  70. std::unique_ptr<ScopedContentSharerInterface> nativeImplementation;
  71. /* The 'old' native message box API doesn't have a concept of content sharer owners.
  72. Instead, content sharers have to clean up after themselves, once they're done displaying.
  73. To allow this mode of usage, the implementation keeps an owning reference to itself,
  74. which is cleared once the content sharer is closed or asked to quit. To display a content
  75. sharer box without a scoped lifetime, just create a Pimpl instance without using
  76. the ScopedContentSharer wrapper, and the Pimpl will destroy itself after it is dismissed.
  77. */
  78. std::shared_ptr<ConcreteScopedContentSharerImpl> self;
  79. };
  80. } // namespace juce::detail