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.

286 lines
7.1KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 7 technical preview.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For the technical preview this file cannot be licensed commercially.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. namespace juce
  14. {
  15. struct ModalComponentManager::ModalItem : public ComponentMovementWatcher
  16. {
  17. ModalItem (Component* comp, bool shouldAutoDelete)
  18. : ComponentMovementWatcher (comp),
  19. component (comp), autoDelete (shouldAutoDelete)
  20. {
  21. jassert (comp != nullptr);
  22. }
  23. ~ModalItem() override
  24. {
  25. if (autoDelete)
  26. std::unique_ptr<Component> componentDeleter (component);
  27. }
  28. void componentMovedOrResized (bool, bool) override {}
  29. using ComponentMovementWatcher::componentMovedOrResized;
  30. void componentPeerChanged() override
  31. {
  32. componentVisibilityChanged();
  33. }
  34. void componentVisibilityChanged() override
  35. {
  36. if (! component->isShowing())
  37. cancel();
  38. }
  39. using ComponentMovementWatcher::componentVisibilityChanged;
  40. void componentBeingDeleted (Component& comp) override
  41. {
  42. ComponentMovementWatcher::componentBeingDeleted (comp);
  43. if (component == &comp || comp.isParentOf (component))
  44. {
  45. autoDelete = false;
  46. cancel();
  47. }
  48. }
  49. void cancel()
  50. {
  51. if (isActive)
  52. {
  53. isActive = false;
  54. if (auto* mcm = ModalComponentManager::getInstanceWithoutCreating())
  55. mcm->triggerAsyncUpdate();
  56. }
  57. }
  58. Component* component;
  59. OwnedArray<Callback> callbacks;
  60. int returnValue = 0;
  61. bool isActive = true, autoDelete;
  62. JUCE_DECLARE_NON_COPYABLE (ModalItem)
  63. };
  64. //==============================================================================
  65. ModalComponentManager::ModalComponentManager()
  66. {
  67. }
  68. ModalComponentManager::~ModalComponentManager()
  69. {
  70. stack.clear();
  71. clearSingletonInstance();
  72. }
  73. JUCE_IMPLEMENT_SINGLETON (ModalComponentManager)
  74. //==============================================================================
  75. void ModalComponentManager::startModal (Component* component, bool autoDelete)
  76. {
  77. if (component != nullptr)
  78. stack.add (new ModalItem (component, autoDelete));
  79. }
  80. void ModalComponentManager::attachCallback (Component* component, Callback* callback)
  81. {
  82. if (callback != nullptr)
  83. {
  84. std::unique_ptr<Callback> callbackDeleter (callback);
  85. for (int i = stack.size(); --i >= 0;)
  86. {
  87. auto* item = stack.getUnchecked (i);
  88. if (item->component == component)
  89. {
  90. item->callbacks.add (callback);
  91. callbackDeleter.release();
  92. break;
  93. }
  94. }
  95. }
  96. }
  97. void ModalComponentManager::endModal (Component* component)
  98. {
  99. for (int i = stack.size(); --i >= 0;)
  100. {
  101. auto* item = stack.getUnchecked (i);
  102. if (item->component == component)
  103. item->cancel();
  104. }
  105. }
  106. void ModalComponentManager::endModal (Component* component, int returnValue)
  107. {
  108. for (int i = stack.size(); --i >= 0;)
  109. {
  110. auto* item = stack.getUnchecked (i);
  111. if (item->component == component)
  112. {
  113. item->returnValue = returnValue;
  114. item->cancel();
  115. }
  116. }
  117. }
  118. int ModalComponentManager::getNumModalComponents() const
  119. {
  120. int n = 0;
  121. for (auto* item : stack)
  122. if (item->isActive)
  123. ++n;
  124. return n;
  125. }
  126. Component* ModalComponentManager::getModalComponent (int index) const
  127. {
  128. int n = 0;
  129. for (int i = stack.size(); --i >= 0;)
  130. {
  131. auto* item = stack.getUnchecked (i);
  132. if (item->isActive)
  133. if (n++ == index)
  134. return item->component;
  135. }
  136. return nullptr;
  137. }
  138. bool ModalComponentManager::isModal (const Component* comp) const
  139. {
  140. for (auto* item : stack)
  141. if (item->isActive && item->component == comp)
  142. return true;
  143. return false;
  144. }
  145. bool ModalComponentManager::isFrontModalComponent (const Component* comp) const
  146. {
  147. return comp == getModalComponent (0);
  148. }
  149. void ModalComponentManager::handleAsyncUpdate()
  150. {
  151. for (int i = stack.size(); --i >= 0;)
  152. {
  153. auto* item = stack.getUnchecked (i);
  154. if (! item->isActive)
  155. {
  156. std::unique_ptr<ModalItem> deleter (stack.removeAndReturn (i));
  157. Component::SafePointer<Component> compToDelete (item->autoDelete ? item->component : nullptr);
  158. for (int j = item->callbacks.size(); --j >= 0;)
  159. item->callbacks.getUnchecked (j)->modalStateFinished (item->returnValue);
  160. compToDelete.deleteAndZero();
  161. }
  162. }
  163. }
  164. void ModalComponentManager::bringModalComponentsToFront (bool topOneShouldGrabFocus)
  165. {
  166. ComponentPeer* lastOne = nullptr;
  167. for (int i = 0; i < getNumModalComponents(); ++i)
  168. {
  169. auto* c = getModalComponent (i);
  170. if (c == nullptr)
  171. break;
  172. if (auto* peer = c->getPeer())
  173. {
  174. if (peer != lastOne)
  175. {
  176. if (lastOne == nullptr)
  177. {
  178. peer->toFront (topOneShouldGrabFocus);
  179. if (topOneShouldGrabFocus)
  180. peer->grabFocus();
  181. }
  182. else
  183. {
  184. peer->toBehind (lastOne);
  185. }
  186. lastOne = peer;
  187. }
  188. }
  189. }
  190. }
  191. bool ModalComponentManager::cancelAllModalComponents()
  192. {
  193. auto numModal = getNumModalComponents();
  194. for (int i = numModal; --i >= 0;)
  195. if (auto* c = getModalComponent (i))
  196. c->exitModalState (0);
  197. return numModal > 0;
  198. }
  199. //==============================================================================
  200. #if JUCE_MODAL_LOOPS_PERMITTED
  201. int ModalComponentManager::runEventLoopForCurrentComponent()
  202. {
  203. // This can only be run from the message thread!
  204. JUCE_ASSERT_MESSAGE_THREAD
  205. int returnValue = 0;
  206. if (auto* currentlyModal = getModalComponent (0))
  207. {
  208. FocusRestorer focusRestorer;
  209. bool finished = false;
  210. attachCallback (currentlyModal, ModalCallbackFunction::create ([&] (int r) { returnValue = r; finished = true; }));
  211. JUCE_TRY
  212. {
  213. while (! finished)
  214. {
  215. if (! MessageManager::getInstance()->runDispatchLoopUntil (20))
  216. break;
  217. }
  218. }
  219. JUCE_CATCH_EXCEPTION
  220. }
  221. return returnValue;
  222. }
  223. #endif
  224. } // namespace juce