forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFocusController.cs
More file actions
365 lines (307 loc) · 11.5 KB
/
FocusController.cs
File metadata and controls
365 lines (307 loc) · 11.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
// Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
using System;
using System.Collections.Generic;
namespace UnityEngine.UIElements
{
public abstract class Focusable : CallbackEventHandler
{
protected Focusable()
{
focusable = true;
tabIndex = 0;
}
public abstract FocusController focusController { get; }
public bool focusable { get; set; }
// See http://w3c.github.io/html/editing.html#the-tabindex-attribute
public int tabIndex { get; set; }
bool m_DelegatesFocus;
public bool delegatesFocus
{
get { return m_DelegatesFocus; }
set
{
if (!((VisualElement)this).isCompositeRoot)
{
throw new InvalidOperationException("delegatesFocus should only be set on composite roots.");
}
m_DelegatesFocus = value;
}
}
// Used when we want then children of a composite to appear at
// composite root tabIndex position in the focus ring, but
// we do not want the root itself to be part of the ring.
bool m_ExcludeFromFocusRing;
internal bool excludeFromFocusRing
{
get { return m_ExcludeFromFocusRing; }
set
{
if (!((VisualElement)this).isCompositeRoot)
{
throw new InvalidOperationException("excludeFromFocusRing should only be set on composite roots.");
}
m_ExcludeFromFocusRing = value;
}
}
public virtual bool canGrabFocus => focusable;
public virtual void Focus()
{
if (focusController != null)
{
if (canGrabFocus)
{
var elementGettingFocused = GetFocusDelegate();
focusController.SwitchFocus(elementGettingFocused);
}
else
{
focusController.SwitchFocus(null);
}
}
}
public virtual void Blur()
{
if (focusController != null)
{
if (focusController.IsFocused(this))
{
focusController.SwitchFocus(null);
}
}
}
// Use the tree to find the first focusable child.
// FIXME: we should use the focus ring; however, it may happens that the
// children are focusable but not part of the ring.
Focusable GetFocusDelegate()
{
var f = this;
while (f != null && f.delegatesFocus)
{
f = GetFirstFocusableChild(f as VisualElement);
}
return f;
}
static Focusable GetFirstFocusableChild(VisualElement ve)
{
foreach (var child in ve.hierarchy.Children())
{
if (child.canGrabFocus)
{
return child;
}
bool isSlot = child.hierarchy.parent != null && child == child.hierarchy.parent.contentContainer;
if (!child.isCompositeRoot && !isSlot)
{
var f = GetFirstFocusableChild(child);
if (f != null)
{
return f;
}
}
}
return null;
}
protected override void ExecuteDefaultAction(EventBase evt)
{
base.ExecuteDefaultAction(evt);
if (evt != null && evt.target == evt.leafTarget)
{
if (evt.eventTypeId == MouseDownEvent.TypeId())
{
Focus();
}
focusController?.SwitchFocusOnEvent(evt);
}
}
}
public class FocusChangeDirection
{
public static FocusChangeDirection unspecified { get; } = new FocusChangeDirection(-1);
public static FocusChangeDirection none { get; } = new FocusChangeDirection(0);
protected static FocusChangeDirection lastValue { get; } = none;
readonly int m_Value;
protected FocusChangeDirection(int value)
{
m_Value = value;
}
public static implicit operator int(FocusChangeDirection fcd)
{
return fcd?.m_Value ?? 0;
}
}
public interface IFocusRing
{
FocusChangeDirection GetFocusChangeDirection(Focusable currentFocusable, EventBase e);
Focusable GetNextFocusable(Focusable currentFocusable, FocusChangeDirection direction);
}
public class FocusController
{
// https://w3c.github.io/uievents/#interface-focusevent
public FocusController(IFocusRing focusRing)
{
this.focusRing = focusRing;
imguiKeyboardControl = 0;
}
IFocusRing focusRing { get; }
struct FocusedElement
{
public VisualElement m_SubTreeRoot;
public Focusable m_FocusedElement;
}
List<FocusedElement> m_FocusedElements = new List<FocusedElement>();
public Focusable focusedElement => GetRetargetedFocusedElement(null);
internal bool IsFocused(Focusable f)
{
foreach (var fe in m_FocusedElements)
{
if (fe.m_FocusedElement == f)
{
return true;
}
}
return false;
}
internal Focusable GetRetargetedFocusedElement(VisualElement retargetAgainst)
{
var retargetRoot = retargetAgainst?.hierarchy.parent;
if (retargetRoot == null)
{
if (m_FocusedElements.Count > 0)
{
return m_FocusedElements[m_FocusedElements.Count - 1].m_FocusedElement;
}
}
else
{
while (!retargetRoot.isCompositeRoot && retargetRoot.hierarchy.parent != null)
{
retargetRoot = retargetRoot.hierarchy.parent;
}
foreach (var fe in m_FocusedElements)
{
if (fe.m_SubTreeRoot == retargetRoot)
{
return fe.m_FocusedElement;
}
}
}
return null;
}
internal Focusable GetLeafFocusedElement()
{
if (m_FocusedElements.Count > 0)
{
return m_FocusedElements[0].m_FocusedElement;
}
return null;
}
internal void DoFocusChange(Focusable f)
{
m_FocusedElements.Clear();
VisualElement ve = f as VisualElement;
while (ve != null)
{
if (ve.hierarchy.parent == null || ve.isCompositeRoot)
{
m_FocusedElements.Add(new FocusedElement { m_SubTreeRoot = ve, m_FocusedElement = f });
f = ve;
}
ve = ve.hierarchy.parent;
}
}
void AboutToReleaseFocus(Focusable focusable, Focusable willGiveFocusTo, FocusChangeDirection direction)
{
using (FocusOutEvent e = FocusOutEvent.GetPooled(focusable, willGiveFocusTo, direction, this))
{
focusable.SendEvent(e);
}
}
void ReleaseFocus(Focusable focusable, Focusable willGiveFocusTo, FocusChangeDirection direction)
{
using (BlurEvent e = BlurEvent.GetPooled(focusable, willGiveFocusTo, direction, this))
{
focusable.SendEvent(e);
}
}
void AboutToGrabFocus(Focusable focusable, Focusable willTakeFocusFrom, FocusChangeDirection direction)
{
using (FocusInEvent e = FocusInEvent.GetPooled(focusable, willTakeFocusFrom, direction, this))
{
focusable.SendEvent(e);
}
}
void GrabFocus(Focusable focusable, Focusable willTakeFocusFrom, FocusChangeDirection direction)
{
using (FocusEvent e = FocusEvent.GetPooled(focusable, willTakeFocusFrom, direction, this))
{
focusable.SendEvent(e);
}
}
internal void SwitchFocus(Focusable newFocusedElement)
{
SwitchFocus(newFocusedElement, FocusChangeDirection.unspecified);
}
void SwitchFocus(Focusable newFocusedElement, FocusChangeDirection direction)
{
if (GetLeafFocusedElement() == newFocusedElement)
{
return;
}
var oldFocusedElement = GetLeafFocusedElement();
if (newFocusedElement == null || !newFocusedElement.canGrabFocus)
{
if (oldFocusedElement != null)
{
AboutToReleaseFocus(oldFocusedElement, null, direction);
ReleaseFocus(oldFocusedElement, null, direction);
}
}
else if (newFocusedElement != oldFocusedElement)
{
// Retarget event.relatedTarget so it is in the same tree as event.target.
var retargetedNewFocusedElement = (newFocusedElement as VisualElement)?.RetargetElement(oldFocusedElement as VisualElement);
var retargetedOldFocusedElement = (oldFocusedElement as VisualElement)?.RetargetElement(newFocusedElement as VisualElement);
if (oldFocusedElement != null)
{
AboutToReleaseFocus(oldFocusedElement, retargetedNewFocusedElement, direction);
}
AboutToGrabFocus(newFocusedElement, retargetedOldFocusedElement, direction);
if (oldFocusedElement != null)
{
ReleaseFocus(oldFocusedElement, retargetedNewFocusedElement, direction);
}
GrabFocus(newFocusedElement, retargetedOldFocusedElement, direction);
}
}
internal Focusable SwitchFocusOnEvent(EventBase e)
{
FocusChangeDirection direction = focusRing.GetFocusChangeDirection(GetLeafFocusedElement(), e);
if (direction != FocusChangeDirection.none)
{
Focusable f = focusRing.GetNextFocusable(GetLeafFocusedElement(), direction);
SwitchFocus(f, direction);
// f does not have the focus yet. It will when the series of focus events will have been handled.
return f;
}
return GetLeafFocusedElement();
}
/// <summary>
/// This property contains the actual keyboard id of the element being focused in the case of an IMGUIContainer
/// </summary>
internal int imguiKeyboardControl { get; set; }
internal void SyncIMGUIFocus(int imguiKeyboardControlID, Focusable imguiContainerHavingKeyboardControl, bool forceSwitch)
{
imguiKeyboardControl = imguiKeyboardControlID;
if (forceSwitch || imguiKeyboardControl != 0)
{
SwitchFocus(imguiContainerHavingKeyboardControl, FocusChangeDirection.unspecified);
}
else
{
SwitchFocus(null, FocusChangeDirection.unspecified);
}
}
}
}