|
4 | 4 | using osuTK; |
5 | 5 | using System; |
6 | 6 | using System.Collections.Generic; |
| 7 | +using System.Linq; |
7 | 8 | using osu.Framework.Extensions.EnumExtensions; |
| 9 | +using osu.Framework.Extensions.ObjectExtensions; |
8 | 10 |
|
9 | 11 | namespace osu.Framework.Graphics.Containers |
10 | 12 | { |
@@ -92,5 +94,70 @@ public static TContainer WithChildren<TContainer, TChild>(this TContainer contai |
92 | 94 |
|
93 | 95 | return container; |
94 | 96 | } |
| 97 | + |
| 98 | + /// <summary> |
| 99 | + /// Searches the subtree for <see cref="ITabbableContainer"/>s and moves focus to the <see cref="ITabbableContainer"/> before/after the one currently focused. |
| 100 | + /// </summary> |
| 101 | + /// <param name="target">Container to search for valid focus targets in.</param> |
| 102 | + /// <param name="reverse">Whether to traverse the container's children in reverse when looking for the next target.</param> |
| 103 | + /// <param name="requireFocusedChild"> |
| 104 | + /// Determines the behaviour when the currently focused drawable isn't rooted at this container. |
| 105 | + /// If true, then focus will not be moved. |
| 106 | + /// If false, then focus will be moved to the first valid child. |
| 107 | + /// </param> |
| 108 | + /// <returns>Whether focus was moved to a new <see cref="ITabbableContainer"/>.</returns> |
| 109 | + public static bool MoveFocusToNextTabStop(this CompositeDrawable target, bool reverse = false, bool requireFocusedChild = true) |
| 110 | + { |
| 111 | + var currentlyFocused = target.GetContainingInputManager()?.FocusedDrawable; |
| 112 | + |
| 113 | + if (currentlyFocused == null && requireFocusedChild) |
| 114 | + return false; |
| 115 | + |
| 116 | + var focusManager = target.GetContainingFocusManager().AsNonNull(); |
| 117 | + |
| 118 | + Stack<Drawable> stack = new Stack<Drawable>(); |
| 119 | + stack.Push(target); // Extra push for circular tabbing |
| 120 | + stack.Push(target); |
| 121 | + |
| 122 | + // If we don't have a currently focused child we pretend we've already encountered our target child to move focus to the first valid target. |
| 123 | + bool started = currentlyFocused == null; |
| 124 | + |
| 125 | + while (stack.Count > 0) |
| 126 | + { |
| 127 | + var drawable = stack.Pop(); |
| 128 | + |
| 129 | + if (!started) |
| 130 | + started = ReferenceEquals(drawable, currentlyFocused); |
| 131 | + else if (drawable is ITabbableContainer tabbable && tabbable.CanBeTabbedTo && focusManager.ChangeFocus(drawable)) |
| 132 | + return true; |
| 133 | + |
| 134 | + if (drawable is CompositeDrawable composite) |
| 135 | + { |
| 136 | + var newChildren = composite.InternalChildren.ToList(); |
| 137 | + int bound = reverse ? newChildren.Count : 0; |
| 138 | + |
| 139 | + if (!started) |
| 140 | + { |
| 141 | + // Find currently focused element, to know starting point |
| 142 | + int index = newChildren.IndexOf(currentlyFocused); |
| 143 | + if (index != -1) |
| 144 | + bound = reverse ? index + 1 : index; |
| 145 | + } |
| 146 | + |
| 147 | + if (reverse) |
| 148 | + { |
| 149 | + for (int i = 0; i < bound; i++) |
| 150 | + stack.Push(newChildren[i]); |
| 151 | + } |
| 152 | + else |
| 153 | + { |
| 154 | + for (int i = newChildren.Count - 1; i >= bound; i--) |
| 155 | + stack.Push(newChildren[i]); |
| 156 | + } |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + return false; |
| 161 | + } |
95 | 162 | } |
96 | 163 | } |
0 commit comments