-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFocusFlowManager.cs
404 lines (329 loc) · 11.6 KB
/
FocusFlowManager.cs
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
// Developed by Bulat Bagaviev (@sunnyyssh).
// This file is licensed to you under the MIT license.
// ReSharper disable InconsistentlySynchronizedField
using System.Diagnostics.CodeAnalysis;
namespace Sunnyyssh.ConsoleUI;
internal record FocusManagerOptions(
FocusFlowSpecification Specification,
bool FocusFlowLoop,
bool ChangeFocusWhenSingle,
bool ThrowOnNotFocusedHandling,
ConsoleKey? SpecialKey = null);
internal record FocusFlowEndedArgs;
internal delegate void FocusFlowEndedHandler(FocusFlowManager lostManager, FocusFlowEndedArgs args);
internal delegate void ForceManagerTakeFocusHandler(FocusFlowManager manager);
/// <summary>
/// Manager of the whole focus flow. It handles keys and delegates it to the focused <see cref="IFocusable"/>.
/// </summary>
internal sealed class FocusFlowManager
{
/// <summary>
/// Focus flow and handling keys is delegated to this manager.
/// If current <see cref="IFocusable"/> is <see cref="IFocusManagerHolder"/> (<see cref="Wrapper"/> for example)
/// then its <see cref="FocusFlowManager"/> is the successor of focus.
/// </summary>
private FocusFlowManager? _successor;
private readonly FocusManagerOptions _options;
private readonly FocusableChain _focusableChain;
private readonly object _keyHandlingLockObject = new();
/// <summary>
/// Focused child.
/// </summary>
public IFocusable? FocusedItem => _focusableChain.FocusedItem;
/// <summary>
/// Indicates if this instance should handle all keys on its own.
/// </summary>
public bool OverridesFlow { get; }
/// <summary>
/// Indicastes if any child is waiting for focus at this moment.
/// </summary>
public bool HasWaitingFocusable => _focusableChain.HasWaiting;
public int ChildrenCount => _focusableChain.Count();
public IFocusable[] Children => _focusableChain.ToArray();
/// <summary>
/// Indicates if this instance is handling focus now.
/// </summary>
public bool IsFocused { get; private set; }
public bool TryGiveFocusTo(IFocusable focusable)
{
if (!_focusableChain.TrySetCurrentTo(focusable))
return false;
if (IsFocused)
{
if (_focusableChain.FocusedItem is {} from)
{
RemoveFocusFrom(from);
}
_focusableChain.SetFocusToCurrent();
GiveFocusTo(focusable);
}
return true;
}
/// <summary>
/// Handles pressed key.
/// </summary>
/// <param name="args">Args of pressed key.</param>
/// <exception cref="FocusFlowException">It's not focused.</exception>
public void HandlePressedKey(KeyPressedArgs args)
{
lock (_keyHandlingLockObject)
{
if (!IsFocused)
{
if (_options.ThrowOnNotFocusedHandling)
throw new FocusFlowException("It's not focused.");
return;
}
if (_options.SpecialKey == args.KeyInfo.Key)
{
SpecialKeyPressed?.Invoke(args);
return;
}
// If it overrides flow then it should handle it on its own.
// Then it's neccessary to call it before own handling.
if (_successor is not null && _successor.OverridesFlow)
{
_successor.HandlePressedKey(args);
return;
}
if (IsKeyLose(args.KeyInfo.Key))
{
MoveNext();
return;
}
// Check if key moves focus.
if (TryGetNext(args.KeyInfo.Key, out var next))
{
if (_focusableChain.FocusedItem is {} from)
{
RemoveFocusFrom(from);
}
_focusableChain.TrySetCurrentTo(next);
_focusableChain.SetFocusToCurrent();
GiveFocusTo(next);
return;
}
// Delegating focus if it doesn't override flow.
if (_successor is {} successor)
{
successor.HandlePressedKey(args);
return;
}
// If there is one focused it chould handle this key.
if (_focusableChain.FocusedItem is {} focused)
{
bool toKeepFocus = focused.HandlePressedKey(args.KeyInfo);
if (!toKeepFocus)
{
MoveNext();
}
}
}
}
// Checks in specification if key forces lose focus.
private bool IsKeyLose(ConsoleKey key)
{
if (_focusableChain.Current is not {} current)
{
return false;
}
var spec = _options.Specification.Children[current];
return spec.FocusLose.Contains(key);
}
// Tries to get next <see cref="IFocusable"/> if key should move focus to next according to the specification.
private bool TryGetNext(ConsoleKey pressedKey, [NotNullWhen(true)] out IFocusable? next)
{
next = null;
if (_focusableChain.FocusedItem is null)
{
return false;
}
if (!_options.Specification.Children.TryGetValue(_focusableChain.FocusedItem, out var spec))
{
return false;
}
return RecursiveGetNext(pressedKey, spec, out next);
}
// If child is not waiting for focus. Its specification is checked for next one by this key.
// If it waits it is next.
private bool RecursiveGetNext(ConsoleKey pressedKey, ChildSpecification from, [NotNullWhen(true)] out IFocusable? next)
{
next = null;
if (!from.Flows.TryGetValue(pressedKey, out var possibleNext))
{
return false;
}
if (possibleNext.IsWaitingFocus)
{
next = possibleNext;
return true;
}
var nextSpecification = _options.Specification.Children[possibleNext];
return RecursiveGetNext(pressedKey, nextSpecification, out next);
}
// It goes to the next IFocusable which IsWaitingFocus == true
// And it handles their focus change.
private void MoveNext()
{
if (_focusableChain.IsEmpty)
return;
if (!IsFocused)
{
_ = _focusableChain.MoveNext();
return;
}
if (_focusableChain.FocusedItem == _focusableChain.Current)
{
if (!_focusableChain.MoveNextWaitingFocus())
{
FocusFlowEnded?.Invoke(this, new FocusFlowEndedArgs());
return;
}
if (_focusableChain.Current == _focusableChain.FocusedItem
&& !_options.ChangeFocusWhenSingle
// If current IFocusable is IFocusManagerHolder then it should handle focus change.
// It's bad decision but it's not expensive.
&& _focusableChain.Current is not IFocusManagerHolder)
return;
}
if (_focusableChain.FocusedItem is not null)
RemoveFocusFrom(_focusableChain.FocusedItem);
if (_focusableChain.Current is not null)
{
GiveFocusTo(_focusableChain.Current);
_focusableChain.SetFocusToCurrent();
}
}
public event FocusFlowEndedHandler? FocusFlowEnded;
public event ForceManagerTakeFocusHandler? ForceTakeFocus;
// Delegates focus flow handling to the enother manager.
private void GiveManagementTo(FocusFlowManager successor)
{
if (successor == this)
{
throw new FocusFlowException($"Focus manager can't give management to itself. {nameof(successor)} was same as this instance.");
}
var pastSuccessor = _successor;
_successor = successor;
if (pastSuccessor is not null)
{
pastSuccessor.LoseFocus();
}
_successor.TakeFocus();
}
// Restores handling keys on its own.
private void RestoreOwnManagement()
{
var pastSuccessor = _successor;
_successor = null;
if (pastSuccessor is not null)
{
pastSuccessor.LoseFocus();
}
}
/// <summary>
/// Takes focus. Makes current <see cref="IFocusable"/> take focus.
/// </summary>
/// <exception cref="FocusFlowException">It's already focused.</exception>
public void TakeFocus()
{
if (IsFocused)
throw new FocusFlowException("It's already focused.");
IsFocused = true;
// Trying to give focus to waiting.
if (!_focusableChain.Current?.IsWaitingFocus ?? false)
{
_focusableChain.MoveNextWaitingFocus();
}
_focusableChain.SetFocusToCurrent();
if (_focusableChain.FocusedItem is {} focused)
{
GiveFocusTo(focused);
}
}
/// <summary>
/// Loses focus. Makes focused <see cref="IFocusable"/> lose focus.
/// </summary>
/// <exception cref="FocusFlowException">It's not focused.</exception>
public void LoseFocus()
{
if (!IsFocused)
throw new FocusFlowException("It's not focused.");
IsFocused = false;
if (_focusableChain.FocusedItem is {} focused)
{
_focusableChain.LoseFocus();
RemoveFocusFrom(focused);
}
}
private void GiveFocusTo(IFocusable focusable)
{
focusable.TakeFocus();
if (focusable is IFocusManagerHolder holder)
{
GiveManagementTo(holder.GetFocusManager());
}
}
private void RemoveFocusFrom(IFocusable focusable)
{
if (focusable is IFocusManagerHolder)
{
RestoreOwnManagement();
}
focusable.LoseFocus();
}
private void SubscribeChildEvents(IFocusable child)
{
child.ForceLoseFocus += OnChildForceLoseFocus;
child.ForceTakeFocus += OnChildForceTakeFocus;
}
// ReSharper disable once UnusedMember.Local
private void UnsubscribeChildEvents(IFocusable child)
{
child.ForceLoseFocus -= OnChildForceLoseFocus;
child.ForceTakeFocus -= OnChildForceTakeFocus;
}
private void OnChildForceTakeFocus(IFocusable sender)
{
if (!_focusableChain.TrySetCurrentTo(sender))
return;
if (IsFocused)
{
if (_focusableChain.FocusedItem is { } past)
{
RemoveFocusFrom(past);
}
_focusableChain.SetFocusToCurrent();
GiveFocusTo(sender);
}
else
{
ForceTakeFocus?.Invoke(this);
}
}
private void OnChildForceLoseFocus(IFocusable sender)
{
if (_focusableChain.FocusedItem != sender)
return;
if (IsFocused)
{
MoveNext();
}
}
public event KeyPressedHandler? SpecialKeyPressed;
/// <summary>
/// Creates an instance of <see cref="FocusFlowManager"/>.
/// </summary>
/// <param name="options">Specifies flow.</param>
public FocusFlowManager(FocusManagerOptions options)
{
ArgumentNullException.ThrowIfNull(options, nameof(options));
_options = options;
OverridesFlow = options.Specification.OverridesFlow;
_focusableChain = new FocusableChain(_options.FocusFlowLoop, options.Specification.Children.Keys);
foreach (var focusable in options.Specification.Children.Keys)
{
SubscribeChildEvents(focusable);
}
}
}