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.

275 lines
9.9KB

  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. //==============================================================================
  21. namespace KeyboardFocusTraverserHelpers
  22. {
  23. static bool isKeyboardFocusable (const Component* comp, const Component* container)
  24. {
  25. return comp->getWantsKeyboardFocus() && container->isParentOf (comp);
  26. }
  27. static Component* traverse (Component* current, Component* container,
  28. FocusHelpers::NavigationDirection direction)
  29. {
  30. if (auto* comp = FocusHelpers::navigateFocus (current, container, direction,
  31. &Component::isKeyboardFocusContainer))
  32. {
  33. if (isKeyboardFocusable (comp, container))
  34. return comp;
  35. return traverse (comp, container, direction);
  36. }
  37. return nullptr;
  38. }
  39. }
  40. Component* KeyboardFocusTraverser::getNextComponent (Component* current)
  41. {
  42. return KeyboardFocusTraverserHelpers::traverse (current, current->findKeyboardFocusContainer(),
  43. FocusHelpers::NavigationDirection::forwards);
  44. }
  45. Component* KeyboardFocusTraverser::getPreviousComponent (Component* current)
  46. {
  47. return KeyboardFocusTraverserHelpers::traverse (current, current->findKeyboardFocusContainer(),
  48. FocusHelpers::NavigationDirection::backwards);
  49. }
  50. Component* KeyboardFocusTraverser::getDefaultComponent (Component* parentComponent)
  51. {
  52. for (auto* comp : getAllComponents (parentComponent))
  53. if (KeyboardFocusTraverserHelpers::isKeyboardFocusable (comp, parentComponent))
  54. return comp;
  55. return nullptr;
  56. }
  57. std::vector<Component*> KeyboardFocusTraverser::getAllComponents (Component* parentComponent)
  58. {
  59. std::vector<Component*> components;
  60. FocusHelpers::findAllComponents (parentComponent,
  61. components,
  62. &Component::isKeyboardFocusContainer);
  63. auto removePredicate = [parentComponent] (const Component* comp)
  64. {
  65. return ! KeyboardFocusTraverserHelpers::isKeyboardFocusable (comp, parentComponent);
  66. };
  67. components.erase (std::remove_if (std::begin (components), std::end (components), std::move (removePredicate)),
  68. std::end (components));
  69. return components;
  70. }
  71. //==============================================================================
  72. //==============================================================================
  73. #if JUCE_UNIT_TESTS
  74. struct KeyboardFocusTraverserTests : public UnitTest
  75. {
  76. KeyboardFocusTraverserTests()
  77. : UnitTest ("KeyboardFocusTraverser", UnitTestCategories::gui)
  78. {}
  79. void runTest() override
  80. {
  81. ScopedJuceInitialiser_GUI libraryInitialiser;
  82. const MessageManagerLock mml;
  83. beginTest ("No child wants keyboard focus");
  84. {
  85. TestComponent parent;
  86. expect (traverser.getDefaultComponent (&parent) == nullptr);
  87. expect (traverser.getAllComponents (&parent).empty());
  88. }
  89. beginTest ("Single child wants keyboard focus");
  90. {
  91. TestComponent parent;
  92. parent.children[5].setWantsKeyboardFocus (true);
  93. auto* defaultComponent = traverser.getDefaultComponent (&parent);
  94. expect (defaultComponent == &parent.children[5]);
  95. expect (defaultComponent->getWantsKeyboardFocus());
  96. expect (traverser.getNextComponent (defaultComponent) == nullptr);
  97. expect (traverser.getPreviousComponent (defaultComponent) == nullptr);
  98. expect (traverser.getAllComponents (&parent).size() == 1);
  99. }
  100. beginTest ("Multiple children want keyboard focus");
  101. {
  102. TestComponent parent;
  103. Component* focusChildren[]
  104. {
  105. &parent.children[1],
  106. &parent.children[9],
  107. &parent.children[3],
  108. &parent.children[5],
  109. &parent.children[8],
  110. &parent.children[0]
  111. };
  112. for (auto* focusChild : focusChildren)
  113. focusChild->setWantsKeyboardFocus (true);
  114. auto allComponents = traverser.getAllComponents (&parent);
  115. for (auto* focusChild : focusChildren)
  116. expect (std::find (allComponents.cbegin(), allComponents.cend(), focusChild) != allComponents.cend());
  117. auto* componentToTest = traverser.getDefaultComponent (&parent);
  118. for (;;)
  119. {
  120. expect (componentToTest->getWantsKeyboardFocus());
  121. expect (std::find (std::begin (focusChildren), std::end (focusChildren), componentToTest) != std::end (focusChildren));
  122. componentToTest = traverser.getNextComponent (componentToTest);
  123. if (componentToTest == nullptr)
  124. break;
  125. }
  126. int focusOrder = 1;
  127. for (auto* focusChild : focusChildren)
  128. focusChild->setExplicitFocusOrder (focusOrder++);
  129. componentToTest = traverser.getDefaultComponent (&parent);
  130. for (auto* focusChild : focusChildren)
  131. {
  132. expect (componentToTest == focusChild);
  133. expect (componentToTest->getWantsKeyboardFocus());
  134. componentToTest = traverser.getNextComponent (componentToTest);
  135. }
  136. }
  137. beginTest ("Single nested child wants keyboard focus");
  138. {
  139. TestComponent parent;
  140. Component grandparent;
  141. grandparent.addAndMakeVisible (parent);
  142. auto& focusChild = parent.children[5];
  143. focusChild.setWantsKeyboardFocus (true);
  144. expect (traverser.getDefaultComponent (&grandparent) == &focusChild);
  145. expect (traverser.getDefaultComponent (&parent) == &focusChild);
  146. expect (traverser.getNextComponent (&focusChild) == nullptr);
  147. expect (traverser.getPreviousComponent (&focusChild) == nullptr);
  148. expect (traverser.getAllComponents (&parent).size() == 1);
  149. }
  150. beginTest ("Multiple nested children want keyboard focus");
  151. {
  152. TestComponent parent;
  153. Component grandparent;
  154. grandparent.addAndMakeVisible (parent);
  155. Component* focusChildren[]
  156. {
  157. &parent.children[1],
  158. &parent.children[4],
  159. &parent.children[5]
  160. };
  161. for (auto* focusChild : focusChildren)
  162. focusChild->setWantsKeyboardFocus (true);
  163. auto allComponents = traverser.getAllComponents (&parent);
  164. expect (std::equal (allComponents.cbegin(), allComponents.cend(), focusChildren,
  165. [] (const Component* c1, const Component* c2) { return c1 == c2; }));
  166. const auto front = *focusChildren;
  167. const auto back = *std::prev (std::end (focusChildren));
  168. expect (traverser.getDefaultComponent (&grandparent) == front);
  169. expect (traverser.getDefaultComponent (&parent) == front);
  170. expect (traverser.getNextComponent (front) == *std::next (std::begin (focusChildren)));
  171. expect (traverser.getPreviousComponent (back) == *std::prev (std::end (focusChildren), 2));
  172. std::array<Component, 3> otherParents;
  173. for (auto& p : otherParents)
  174. {
  175. grandparent.addAndMakeVisible (p);
  176. p.setWantsKeyboardFocus (true);
  177. }
  178. expect (traverser.getDefaultComponent (&grandparent) == front);
  179. expect (traverser.getDefaultComponent (&parent) == front);
  180. expect (traverser.getNextComponent (back) == &otherParents.front());
  181. expect (traverser.getNextComponent (&otherParents.back()) == nullptr);
  182. expect (traverser.getAllComponents (&grandparent).size() == numElementsInArray (focusChildren) + otherParents.size());
  183. expect (traverser.getAllComponents (&parent).size() == (size_t) numElementsInArray (focusChildren));
  184. for (auto* focusChild : focusChildren)
  185. focusChild->setWantsKeyboardFocus (false);
  186. expect (traverser.getDefaultComponent (&grandparent) == &otherParents.front());
  187. expect (traverser.getDefaultComponent (&parent) == nullptr);
  188. expect (traverser.getAllComponents (&grandparent).size() == otherParents.size());
  189. expect (traverser.getAllComponents (&parent).empty());
  190. }
  191. }
  192. private:
  193. struct TestComponent : public Component
  194. {
  195. TestComponent()
  196. {
  197. for (auto& child : children)
  198. addAndMakeVisible (child);
  199. }
  200. std::array<Component, 10> children;
  201. };
  202. KeyboardFocusTraverser traverser;
  203. };
  204. static KeyboardFocusTraverserTests keyboardFocusTraverserTests;
  205. #endif
  206. } // namespace juce