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.

juce_FocusTraverser.cpp 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. namespace FocusHelpers
  21. {
  22. static int getOrder (const Component* c)
  23. {
  24. auto order = c->getExplicitFocusOrder();
  25. return order > 0 ? order : std::numeric_limits<int>::max();
  26. }
  27. template <typename FocusContainerFn>
  28. static void findAllComponents (Component* parent,
  29. std::vector<Component*>& components,
  30. FocusContainerFn isFocusContainer)
  31. {
  32. if (parent == nullptr || parent->getNumChildComponents() == 0)
  33. return;
  34. std::vector<Component*> localComponents;
  35. for (auto* c : parent->getChildren())
  36. if (c->isVisible() && c->isEnabled())
  37. localComponents.push_back (c);
  38. const auto compareComponents = [&] (const Component* a, const Component* b)
  39. {
  40. const auto getComponentOrderAttributes = [] (const Component* c)
  41. {
  42. return std::make_tuple (getOrder (c),
  43. c->isAlwaysOnTop() ? 0 : 1,
  44. c->getY(),
  45. c->getX());
  46. };
  47. return getComponentOrderAttributes (a) < getComponentOrderAttributes (b);
  48. };
  49. // This will sort so that they are ordered in terms of explicit focus,
  50. // always on top, left-to-right, and then top-to-bottom.
  51. std::stable_sort (localComponents.begin(), localComponents.end(), compareComponents);
  52. for (auto* c : localComponents)
  53. {
  54. components.push_back (c);
  55. if (! (c->*isFocusContainer)())
  56. findAllComponents (c, components, isFocusContainer);
  57. }
  58. }
  59. enum class NavigationDirection { forwards, backwards };
  60. template <typename FocusContainerFn>
  61. static Component* navigateFocus (Component* current,
  62. Component* focusContainer,
  63. NavigationDirection direction,
  64. FocusContainerFn isFocusContainer)
  65. {
  66. if (focusContainer != nullptr)
  67. {
  68. std::vector<Component*> components;
  69. findAllComponents (focusContainer, components, isFocusContainer);
  70. const auto iter = std::find (components.cbegin(), components.cend(), current);
  71. if (iter == components.cend())
  72. return nullptr;
  73. switch (direction)
  74. {
  75. case NavigationDirection::forwards:
  76. if (iter != std::prev (components.cend()))
  77. return *std::next (iter);
  78. break;
  79. case NavigationDirection::backwards:
  80. if (iter != components.cbegin())
  81. return *std::prev (iter);
  82. break;
  83. }
  84. }
  85. return nullptr;
  86. }
  87. }
  88. //==============================================================================
  89. Component* FocusTraverser::getNextComponent (Component* current)
  90. {
  91. jassert (current != nullptr);
  92. return FocusHelpers::navigateFocus (current,
  93. current->findFocusContainer(),
  94. FocusHelpers::NavigationDirection::forwards,
  95. &Component::isFocusContainer);
  96. }
  97. Component* FocusTraverser::getPreviousComponent (Component* current)
  98. {
  99. jassert (current != nullptr);
  100. return FocusHelpers::navigateFocus (current,
  101. current->findFocusContainer(),
  102. FocusHelpers::NavigationDirection::backwards,
  103. &Component::isFocusContainer);
  104. }
  105. Component* FocusTraverser::getDefaultComponent (Component* parentComponent)
  106. {
  107. if (parentComponent != nullptr)
  108. {
  109. std::vector<Component*> components;
  110. FocusHelpers::findAllComponents (parentComponent,
  111. components,
  112. &Component::isFocusContainer);
  113. if (! components.empty())
  114. return components.front();
  115. }
  116. return nullptr;
  117. }
  118. std::vector<Component*> FocusTraverser::getAllComponents (Component* parentComponent)
  119. {
  120. std::vector<Component*> components;
  121. FocusHelpers::findAllComponents (parentComponent,
  122. components,
  123. &Component::isFocusContainer);
  124. return components;
  125. }
  126. //==============================================================================
  127. //==============================================================================
  128. #if JUCE_UNIT_TESTS
  129. struct FocusTraverserTests : public UnitTest
  130. {
  131. FocusTraverserTests()
  132. : UnitTest ("FocusTraverser", UnitTestCategories::gui)
  133. {}
  134. void runTest() override
  135. {
  136. ScopedJuceInitialiser_GUI libraryInitialiser;
  137. const MessageManagerLock mml;
  138. beginTest ("Basic traversal");
  139. {
  140. TestComponent parent;
  141. expect (traverser.getDefaultComponent (&parent) == &parent.children.front());
  142. for (auto iter = parent.children.begin(); iter != parent.children.end(); ++iter)
  143. expect (traverser.getNextComponent (&(*iter)) == (iter == std::prev (parent.children.cend()) ? nullptr
  144. : &(*std::next (iter))));
  145. for (auto iter = parent.children.rbegin(); iter != parent.children.rend(); ++iter)
  146. expect (traverser.getPreviousComponent (&(*iter)) == (iter == std::prev (parent.children.rend()) ? nullptr
  147. : &(*std::next (iter))));
  148. auto allComponents = traverser.getAllComponents (&parent);
  149. expect (std::equal (allComponents.cbegin(), allComponents.cend(), parent.children.cbegin(),
  150. [] (const Component* c1, const Component& c2) { return c1 == &c2; }));
  151. }
  152. beginTest ("Disabled components are ignored");
  153. {
  154. checkIgnored ([] (Component& c) { c.setEnabled (false); });
  155. }
  156. beginTest ("Invisible components are ignored");
  157. {
  158. checkIgnored ([] (Component& c) { c.setVisible (false); });
  159. }
  160. beginTest ("Explicit focus order comes before unspecified");
  161. {
  162. TestComponent parent;
  163. auto& explicitFocusComponent = parent.children[2];
  164. explicitFocusComponent.setExplicitFocusOrder (1);
  165. expect (traverser.getDefaultComponent (&parent) == &explicitFocusComponent);
  166. expect (traverser.getAllComponents (&parent).front() == &explicitFocusComponent);
  167. }
  168. beginTest ("Explicit focus order comparison");
  169. {
  170. checkComponentProperties ([this] (Component& child) { child.setExplicitFocusOrder (getRandom().nextInt ({ 1, 100 })); },
  171. [] (const Component& c1, const Component& c2) { return c1.getExplicitFocusOrder()
  172. <= c2.getExplicitFocusOrder(); });
  173. }
  174. beginTest ("Left to right");
  175. {
  176. checkComponentProperties ([this] (Component& child) { child.setTopLeftPosition (getRandom().nextInt ({ 0, 100 }), 0); },
  177. [] (const Component& c1, const Component& c2) { return c1.getX() <= c2.getX(); });
  178. }
  179. beginTest ("Top to bottom");
  180. {
  181. checkComponentProperties ([this] (Component& child) { child.setTopLeftPosition (0, getRandom().nextInt ({ 0, 100 })); },
  182. [] (const Component& c1, const Component& c2) { return c1.getY() <= c2.getY(); });
  183. }
  184. beginTest ("Focus containers have their own focus");
  185. {
  186. Component root;
  187. TestComponent container;
  188. container.setFocusContainerType (Component::FocusContainerType::focusContainer);
  189. root.addAndMakeVisible (container);
  190. expect (traverser.getDefaultComponent (&root) == &container);
  191. expect (traverser.getNextComponent (&container) == nullptr);
  192. expect (traverser.getPreviousComponent (&container) == nullptr);
  193. expect (traverser.getDefaultComponent (&container) == &container.children.front());
  194. for (auto iter = container.children.begin(); iter != container.children.end(); ++iter)
  195. expect (traverser.getNextComponent (&(*iter)) == (iter == std::prev (container.children.cend()) ? nullptr
  196. : &(*std::next (iter))));
  197. for (auto iter = container.children.rbegin(); iter != container.children.rend(); ++iter)
  198. expect (traverser.getPreviousComponent (&(*iter)) == (iter == std::prev (container.children.rend()) ? nullptr
  199. : &(*std::next (iter))));
  200. expect (traverser.getAllComponents (&root).size() == 1);
  201. auto allContainerComponents = traverser.getAllComponents (&container);
  202. expect (std::equal (allContainerComponents.cbegin(), allContainerComponents.cend(), container.children.cbegin(),
  203. [] (const Component* c1, const Component& c2) { return c1 == &c2; }));
  204. }
  205. beginTest ("Non-focus containers pass-through focus");
  206. {
  207. Component root;
  208. TestComponent container;
  209. container.setFocusContainerType (Component::FocusContainerType::none);
  210. root.addAndMakeVisible (container);
  211. expect (traverser.getDefaultComponent (&root) == &container);
  212. expect (traverser.getNextComponent (&container) == &container.children.front());
  213. expect (traverser.getPreviousComponent (&container) == nullptr);
  214. expect (traverser.getDefaultComponent (&container) == &container.children.front());
  215. for (auto iter = container.children.begin(); iter != container.children.end(); ++iter)
  216. expect (traverser.getNextComponent (&(*iter)) == (iter == std::prev (container.children.cend()) ? nullptr
  217. : &(*std::next (iter))));
  218. for (auto iter = container.children.rbegin(); iter != container.children.rend(); ++iter)
  219. expect (traverser.getPreviousComponent (&(*iter)) == (iter == std::prev (container.children.rend()) ? &container
  220. : &(*std::next (iter))));
  221. expect (traverser.getAllComponents (&root).size() == container.children.size() + 1);
  222. }
  223. }
  224. private:
  225. struct TestComponent : public Component
  226. {
  227. TestComponent()
  228. {
  229. for (auto& child : children)
  230. addAndMakeVisible (child);
  231. }
  232. std::array<Component, 10> children;
  233. };
  234. void checkComponentProperties (std::function<void (Component&)>&& childFn,
  235. std::function<bool (const Component&, const Component&)>&& testProperty)
  236. {
  237. TestComponent parent;
  238. for (auto& child : parent.children)
  239. childFn (child);
  240. auto* comp = traverser.getDefaultComponent (&parent);
  241. for (const auto& child : parent.children)
  242. if (&child != comp)
  243. expect (testProperty (*comp, child));
  244. for (;;)
  245. {
  246. auto* next = traverser.getNextComponent (comp);
  247. if (next == nullptr)
  248. break;
  249. expect (testProperty (*comp, *next));
  250. comp = next;
  251. }
  252. }
  253. void checkIgnored (const std::function<void(Component&)>& makeIgnored)
  254. {
  255. TestComponent parent;
  256. auto iter = parent.children.begin();
  257. makeIgnored (*iter);
  258. expect (traverser.getDefaultComponent (&parent) == std::addressof (*std::next (iter)));
  259. iter += 5;
  260. makeIgnored (*iter);
  261. expect (traverser.getNextComponent (std::addressof (*std::prev (iter))) == std::addressof (*std::next (iter)));
  262. expect (traverser.getPreviousComponent (std::addressof (*std::next (iter))) == std::addressof (*std::prev (iter)));
  263. auto allComponents = traverser.getAllComponents (&parent);
  264. expect (std::find (allComponents.cbegin(), allComponents.cend(), &parent.children.front()) == allComponents.cend());
  265. expect (std::find (allComponents.cbegin(), allComponents.cend(), std::addressof (*iter)) == allComponents.cend());
  266. }
  267. FocusTraverser traverser;
  268. };
  269. static FocusTraverserTests focusTraverserTests;
  270. #endif
  271. } // namespace juce