juce_ModalComponentManager.cpp 7.8 KB

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