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.

360 lines
14KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - 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 6 End-User License
  8. Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
  9. End User License Agreement: www.juce.com/juce-6-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. beginTest ("Basic traversal");
  138. {
  139. TestComponent parent;
  140. expect (traverser.getDefaultComponent (&parent) == &parent.children.front());
  141. for (auto iter = parent.children.begin(); iter != parent.children.end(); ++iter)
  142. expect (traverser.getNextComponent (&(*iter)) == (iter == std::prev (parent.children.cend()) ? nullptr
  143. : &(*std::next (iter))));
  144. for (auto iter = parent.children.rbegin(); iter != parent.children.rend(); ++iter)
  145. expect (traverser.getPreviousComponent (&(*iter)) == (iter == std::prev (parent.children.rend()) ? nullptr
  146. : &(*std::next (iter))));
  147. auto allComponents = traverser.getAllComponents (&parent);
  148. expect (std::equal (allComponents.cbegin(), allComponents.cend(), parent.children.cbegin(),
  149. [] (const Component* c1, const Component& c2) { return c1 == &c2; }));
  150. }
  151. beginTest ("Disabled components are ignored");
  152. {
  153. checkIgnored ([] (Component& c) { c.setEnabled (false); });
  154. }
  155. beginTest ("Invisible components are ignored");
  156. {
  157. checkIgnored ([] (Component& c) { c.setVisible (false); });
  158. }
  159. beginTest ("Explicit focus order comes before unspecified");
  160. {
  161. TestComponent parent;
  162. auto& explicitFocusComponent = parent.children[2];
  163. explicitFocusComponent.setExplicitFocusOrder (1);
  164. expect (traverser.getDefaultComponent (&parent) == &explicitFocusComponent);
  165. expect (traverser.getAllComponents (&parent).front() == &explicitFocusComponent);
  166. }
  167. beginTest ("Explicit focus order comparison");
  168. {
  169. checkComponentProperties ([this] (Component& child) { child.setExplicitFocusOrder (getRandom().nextInt ({ 1, 100 })); },
  170. [] (const Component& c1, const Component& c2) { return c1.getExplicitFocusOrder()
  171. <= c2.getExplicitFocusOrder(); });
  172. }
  173. beginTest ("Left to right");
  174. {
  175. checkComponentProperties ([this] (Component& child) { child.setTopLeftPosition (getRandom().nextInt ({ 0, 100 }), 0); },
  176. [] (const Component& c1, const Component& c2) { return c1.getX() <= c2.getX(); });
  177. }
  178. beginTest ("Top to bottom");
  179. {
  180. checkComponentProperties ([this] (Component& child) { child.setTopLeftPosition (0, getRandom().nextInt ({ 0, 100 })); },
  181. [] (const Component& c1, const Component& c2) { return c1.getY() <= c2.getY(); });
  182. }
  183. beginTest ("Focus containers have their own focus");
  184. {
  185. Component root;
  186. TestComponent container;
  187. container.setFocusContainerType (Component::FocusContainerType::focusContainer);
  188. root.addAndMakeVisible (container);
  189. expect (traverser.getDefaultComponent (&root) == &container);
  190. expect (traverser.getNextComponent (&container) == nullptr);
  191. expect (traverser.getPreviousComponent (&container) == nullptr);
  192. expect (traverser.getDefaultComponent (&container) == &container.children.front());
  193. for (auto iter = container.children.begin(); iter != container.children.end(); ++iter)
  194. expect (traverser.getNextComponent (&(*iter)) == (iter == std::prev (container.children.cend()) ? nullptr
  195. : &(*std::next (iter))));
  196. for (auto iter = container.children.rbegin(); iter != container.children.rend(); ++iter)
  197. expect (traverser.getPreviousComponent (&(*iter)) == (iter == std::prev (container.children.rend()) ? nullptr
  198. : &(*std::next (iter))));
  199. expect (traverser.getAllComponents (&root).size() == 1);
  200. auto allContainerComponents = traverser.getAllComponents (&container);
  201. expect (std::equal (allContainerComponents.cbegin(), allContainerComponents.cend(), container.children.cbegin(),
  202. [] (const Component* c1, const Component& c2) { return c1 == &c2; }));
  203. }
  204. beginTest ("Non-focus containers pass-through focus");
  205. {
  206. Component root;
  207. TestComponent container;
  208. container.setFocusContainerType (Component::FocusContainerType::none);
  209. root.addAndMakeVisible (container);
  210. expect (traverser.getDefaultComponent (&root) == &container);
  211. expect (traverser.getNextComponent (&container) == &container.children.front());
  212. expect (traverser.getPreviousComponent (&container) == nullptr);
  213. expect (traverser.getDefaultComponent (&container) == &container.children.front());
  214. for (auto iter = container.children.begin(); iter != container.children.end(); ++iter)
  215. expect (traverser.getNextComponent (&(*iter)) == (iter == std::prev (container.children.cend()) ? nullptr
  216. : &(*std::next (iter))));
  217. for (auto iter = container.children.rbegin(); iter != container.children.rend(); ++iter)
  218. expect (traverser.getPreviousComponent (&(*iter)) == (iter == std::prev (container.children.rend()) ? &container
  219. : &(*std::next (iter))));
  220. expect (traverser.getAllComponents (&root).size() == container.children.size() + 1);
  221. }
  222. }
  223. private:
  224. struct TestComponent : public Component
  225. {
  226. TestComponent()
  227. {
  228. for (auto& child : children)
  229. addAndMakeVisible (child);
  230. }
  231. std::array<Component, 10> children;
  232. };
  233. void checkComponentProperties (std::function<void (Component&)>&& childFn,
  234. std::function<bool (const Component&, const Component&)>&& testProperty)
  235. {
  236. TestComponent parent;
  237. for (auto& child : parent.children)
  238. childFn (child);
  239. auto* comp = traverser.getDefaultComponent (&parent);
  240. for (const auto& child : parent.children)
  241. if (&child != comp)
  242. expect (testProperty (*comp, child));
  243. for (;;)
  244. {
  245. auto* next = traverser.getNextComponent (comp);
  246. if (next == nullptr)
  247. break;
  248. expect (testProperty (*comp, *next));
  249. comp = next;
  250. }
  251. }
  252. void checkIgnored (const std::function<void(Component&)>& makeIgnored)
  253. {
  254. TestComponent parent;
  255. auto iter = parent.children.begin();
  256. makeIgnored (*iter);
  257. expect (traverser.getDefaultComponent (&parent) == std::addressof (*std::next (iter)));
  258. iter += 5;
  259. makeIgnored (*iter);
  260. expect (traverser.getNextComponent (std::addressof (*std::prev (iter))) == std::addressof (*std::next (iter)));
  261. expect (traverser.getPreviousComponent (std::addressof (*std::next (iter))) == std::addressof (*std::prev (iter)));
  262. auto allComponents = traverser.getAllComponents (&parent);
  263. expect (std::find (allComponents.cbegin(), allComponents.cend(), &parent.children.front()) == allComponents.cend());
  264. expect (std::find (allComponents.cbegin(), allComponents.cend(), std::addressof (*iter)) == allComponents.cend());
  265. }
  266. FocusTraverser traverser;
  267. };
  268. static FocusTraverserTests focusTraverserTests;
  269. #endif
  270. } // namespace juce