-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOptionChooser.cs
244 lines (186 loc) · 6.86 KB
/
OptionChooser.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
// Developed by Bulat Bagaviev (@sunnyyssh).
// This file is licensed to you under the MIT license.
namespace Sunnyyssh.ConsoleUI;
public sealed class OptionChosenOnEventArgs
{
public int OptionIndex { get; }
public OptionElement OptionElement { get; }
public OptionChosenOnEventArgs(int optionIndex, OptionElement optionElement)
{
OptionIndex = optionIndex;
OptionElement = optionElement;
}
}
public delegate void OptionChosenOnEventHandler(OptionChooser sender, OptionChosenOnEventArgs args);
public sealed class OptionChosenOffEventArgs
{
public int OptionIndex { get; }
public OptionElement OptionElement { get; }
public OptionChosenOffEventArgs(int optionIndex, OptionElement optionElement)
{
OptionIndex = optionIndex;
OptionElement = optionElement;
}
}
public delegate void OptionChosenOffEventHandler(OptionChooser sender, OptionChosenOffEventArgs args);
public sealed record OptionChooserKeySet(ConsoleKey[] MoveNextKeys, ConsoleKey[] MovePreviousKeys, ConsoleKey[] ChosenOnKeys, ConsoleKey[] ChosenOffKeys)
{
public bool IsMoveNext(ConsoleKey key) => MoveNextKeys.Contains(key);
public bool IsMovePrevious(ConsoleKey key) => MovePreviousKeys.Contains(key);
public bool IsChosenOn(ConsoleKey key) => ChosenOnKeys.Contains(key);
public bool IsChosenOff(ConsoleKey key) => ChosenOffKeys.Contains(key);
}
public abstract class OptionChooser : UIElement, IFocusable
{
private readonly Handler<OptionChooser, OptionChosenOnEventArgs> _chosenOnEventHandler = new(5);
private readonly Handler<OptionChooser, OptionChosenOffEventArgs> _chosenOffEventHandler = new(5);
private readonly OptionChooserKeySet _keySet;
private int _currentIndex = 0;
protected IReadOnlyList<OptionElement> OrderedOptions { get; }
public bool CanChooseOnlyOne { get; }
// ReSharper disable once NotAccessedField.Local
private ForceTakeFocusHandler? _forceTakeFocusHandler;
// ReSharper disable once NotAccessedField.Local
private ForceLoseFocusHandler? _forceLoseFocusHandler;
public bool IsWaitingFocus { get; set; } = true;
bool IFocusable.IsWaitingFocus => IsWaitingFocus;
public bool IsFocused { get; private set; }
protected OptionElement Current => OrderedOptions[_currentIndex];
protected int CurrentIndex => _currentIndex;
bool IFocusable.HandlePressedKey(ConsoleKeyInfo keyInfo)
{
var key = keyInfo.Key;
if (_keySet.IsChosenOn(key) && !Current.IsChosen)
{
ChosenOnCurrent(out bool loseFocus);
return !loseFocus;
}
if (_keySet.IsChosenOff(key) && Current.IsChosen)
{
ChosenOffCurrent(out bool loseFocus);
return !loseFocus;
}
if (_keySet.IsMoveNext(key))
{
MoveNext();
return true;
}
if (_keySet.IsMovePrevious(key))
{
MovePrevious();
return true;
}
return true;
}
private void ChosenOffCurrent(out bool loseFocus)
{
if (!Current.IsChosen)
{
loseFocus = false;
return;
}
_chosenOffEventHandler.Invoke(this, new OptionChosenOffEventArgs(CurrentIndex, Current));
Current.ChosenOff();
loseFocus = false;
}
private void ChosenOnCurrent(out bool loseFocus)
{
if (Current.IsChosen)
{
loseFocus = false;
return;
}
if (CanChooseOnlyOne)
{
foreach (var option in OrderedOptions)
{
if (!option.IsChosen)
continue;
if (option == Current)
continue;
option.ChosenOff();
}
}
_chosenOnEventHandler.Invoke(this, new OptionChosenOnEventArgs(CurrentIndex, Current));
Current.ChosenOn();
loseFocus = CanChooseOnlyOne;
}
private bool MovePrevious() => MoveOn(-1);
private bool MoveNext() => MoveOn(1);
private bool MoveOn(int offset)
{
if (_currentIndex + offset < 0 || _currentIndex + offset >= OrderedOptions.Count)
return false;
var pastOption = OrderedOptions[_currentIndex];
_currentIndex = (_currentIndex + offset) % OrderedOptions.Count;
var newOption = OrderedOptions[_currentIndex];
if (IsFocused)
{
pastOption.FocusOff();
newOption.FocusOn();
}
return true;
}
void IFocusable.TakeFocus()
{
IsFocused = true;
Current.FocusOn();
}
void IFocusable.LoseFocus()
{
Current.FocusOff();
IsFocused = false;
}
public event OptionChosenOnEventHandler ChosenOn
{
add
{
ArgumentNullException.ThrowIfNull(value, nameof(value));
_chosenOnEventHandler.Add(
new Action<OptionChooser, OptionChosenOnEventArgs>(value),
true);
}
remove
{
ArgumentNullException.ThrowIfNull(value, nameof(value));
_chosenOnEventHandler.Remove(
new Action<OptionChooser, OptionChosenOnEventArgs>(value));
}
}
public event OptionChosenOffEventHandler ChosenOff
{
add
{
ArgumentNullException.ThrowIfNull(value, nameof(value));
_chosenOffEventHandler.Add(
new Action<OptionChooser, OptionChosenOffEventArgs>(value),
true);
}
remove
{
ArgumentNullException.ThrowIfNull(value, nameof(value));
_chosenOffEventHandler.Remove(
new Action<OptionChooser, OptionChosenOffEventArgs>(value));
}
}
event ForceTakeFocusHandler? IFocusable.ForceTakeFocus
{
add => _forceTakeFocusHandler += value ?? throw new ArgumentNullException(nameof(value));
remove => _forceTakeFocusHandler -= value ?? throw new ArgumentNullException(nameof(value));
}
event ForceLoseFocusHandler? IFocusable.ForceLoseFocus
{
add => _forceLoseFocusHandler += value ?? throw new ArgumentNullException(nameof(value));
remove => _forceLoseFocusHandler -= value ?? throw new ArgumentNullException(nameof(value));
}
protected OptionChooser(int width, int height, IReadOnlyList<OptionElement> options,
OptionChooserKeySet keySet, bool canChooseOnlyOne, OverlappingPriority priority)
: base(width, height, priority)
{
ArgumentNullException.ThrowIfNull(options, nameof(options));
ArgumentNullException.ThrowIfNull(keySet, nameof(keySet));
_keySet = keySet;
OrderedOptions = options;
CanChooseOnlyOne = canChooseOnlyOne;
}
}