30namespace KeyboardFocusTraverserHelpers
32 static bool isKeyboardFocusable (
const Component* comp,
const Component* container)
34 return comp->getWantsKeyboardFocus() && container->isParentOf (comp);
37 static Component* traverse (Component* current, Component* container,
38 detail::FocusHelpers::NavigationDirection direction)
40 if (
auto* comp = detail::FocusHelpers::navigateFocus (current, container, direction,
43 if (isKeyboardFocusable (comp, container))
46 return traverse (comp, container, direction);
56 detail::FocusHelpers::NavigationDirection::forwards);
62 detail::FocusHelpers::NavigationDirection::backwards);
68 if (KeyboardFocusTraverserHelpers::isKeyboardFocusable (comp, parentComponent))
77 detail::FocusHelpers::findAllComponents (parentComponent,
83 return ! KeyboardFocusTraverserHelpers::isKeyboardFocusable (comp, parentComponent);
103 void runTest()
override
106 const MessageManagerLock
mml;
108 beginTest (
"No child wants keyboard focus");
112 expect (
traverser.getDefaultComponent (&parent) ==
nullptr);
113 expect (
traverser.getAllComponents (&parent).empty());
116 beginTest (
"Single child wants keyboard focus");
120 parent.children[5].setWantsKeyboardFocus (
true);
129 expect (
traverser.getAllComponents (&parent).size() == 1);
132 beginTest (
"Multiple children want keyboard focus");
182 beginTest (
"Single nested child wants keyboard focus");
197 expect (
traverser.getAllComponents (&parent).size() == 1);
200 beginTest (
"Multiple nested children want keyboard focus");
220 [] (
const Component*
c1,
const Component*
c2) { return c1 == c2; }));
226 expect (
traverser.getDefaultComponent (&parent) == front);
235 p.setWantsKeyboardFocus (
true);
239 expect (
traverser.getDefaultComponent (&parent) == front);
249 expect (
traverser.getDefaultComponent (&parent) ==
nullptr);
251 expect (
traverser.getAllComponents (&parent).empty());
260 for (
auto& child : children)
261 addAndMakeVisible (child);
The base class for all JUCE user-interface objects.
bool isKeyboardFocusContainer() const noexcept
Returns true if this component has been marked as a keyboard focus container.
Component * findKeyboardFocusContainer() const
Returns the keyboard focus container for this component.
Component * getPreviousComponent(Component *current) override
Returns the component that should be given keyboard focus after the specified one when moving "backwa...
std::vector< Component * > getAllComponents(Component *parentComponent) override
Returns all of the components that can receive keyboard focus within the given parent component in tr...
Component * getNextComponent(Component *current) override
Returns the component that should be given keyboard focus after the specified one when moving "forwar...
Component * getDefaultComponent(Component *parentComponent) override
Returns the component that should receive keyboard focus by default within the given parent component...
This is a base class for classes that perform a unit test.
Type unalignedPointerCast(void *ptr) noexcept
Casts a pointer to another type via void*, which suppresses the cast-align warning which sometimes ar...
constexpr int numElementsInArray(Type(&)[N]) noexcept
Handy function for getting the number of elements in a simple const C array.