-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRowTextChooserBuilder.cs
213 lines (156 loc) · 7.38 KB
/
RowTextChooserBuilder.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
// Developed by Bulat Bagaviev (@sunnyyssh).
// This file is licensed to you under the MIT license.
using System.Collections.Immutable;
using System.Diagnostics.Contracts;
namespace Sunnyyssh.ConsoleUI;
public sealed class RowTextChooserBuilder : IUIElementBuilder<RowTextChooser>
{
private static readonly OptionChooserKeySet HorizontalDefaultKeySet =
new OptionChooserKeySet(
new[] { ConsoleKey.RightArrow, ConsoleKey.D },
new[] { ConsoleKey.LeftArrow, ConsoleKey.A },
new[] { ConsoleKey.Enter },
new[] { ConsoleKey.Enter });
private static readonly OptionChooserKeySet VerticalDefaultKeySet =
new OptionChooserKeySet(
new[] { ConsoleKey.DownArrow, ConsoleKey.S },
new[] { ConsoleKey.UpArrow, ConsoleKey.W },
new[] { ConsoleKey.Enter },
new[] { ConsoleKey.Enter });
private readonly List<string> _options;
public OptionChooserKeySet? KeySet { get; init; }
public bool CanChooseOnlyOne { get; init; } = true;
public Size Size { get; }
public BorderKind BorderKind { get; init; } = BorderKind.SingleLine;
public LineCharSet? BorderLineCharSet { get; init; }
public Color BorderColor { get; init; } = Color.Default;
public OverlappingPriority OverlappingPriority { get; init; } = OverlappingPriority.Medium;
public string[] Options => _options.ToArray();
public Orientation Orientation { get; }
public TextOptionColorSet ColorSet { get; init; } =
new TextOptionColorSet(Color.Default, Color.Default);
public RowTextChooserBuilder Add(string option)
{
ArgumentNullException.ThrowIfNull(option, nameof(option));
_options.Add(option);
return this;
}
public RowTextChooser Build(UIElementBuildArgs args)
{
ArgumentNullException.ThrowIfNull(args, nameof(args));
int width = args.Width;
int height = args.Height;
var options = _options.ToArray();
Grid initGrid;
if (Orientation == Orientation.Vertical)
{
var initOptions = InitializeOptionsVertical(options);
initGrid = InitializeVerticalGrid(width, height, initOptions);
}
else
{
var initOptions = InitializeOptionsHorizontal(options);
initGrid = InitializeHorizontalGrid(width, height, initOptions);
}
var resultElements = initGrid.Children
.Where(ch => ch.Child is TextOptionElement)
// Only TextOptionElement children can be here.
.Select(ch => (TextOptionElement)ch.Child)
.ToImmutableList();
var keySet = KeySet ?? (Orientation == Orientation.Vertical ? VerticalDefaultKeySet : HorizontalDefaultKeySet);
var resultChooser = new RowTextChooser(width, height,
initGrid, resultElements, keySet,
CanChooseOnlyOne, OverlappingPriority);
return resultChooser;
}
private Grid InitializeVerticalGrid(int width, int height, IUIElementBuilder<TextOptionElement>[] initOptions)
{
var rows = initOptions
.Select(builder => builder.Size.IsHeightRelational
? GridRow.FromRowRelation(builder.Size.HeightRelation.Value)
: GridRow.FromHeight(builder.Size.Height.Value));
var columns = Enumerable.Repeat(GridColumn.FromColumnRelation(1), 1);
var gridDefinition = new GridDefinition(
GridRowDefinition.From(rows),
GridColumnDefinition.From(columns));
var gridBuilder = new GridBuilder(width, height, gridDefinition)
{
BorderLineCharSet = BorderLineCharSet,
BorderKind = BorderKind,
BorderColor = BorderColor,
};
for (int row = 0; row < initOptions.Length; row++)
{
gridBuilder.Add(initOptions[row].UnsafeWithSize(Size.FullSize), row, 0);
}
return gridBuilder.Build(new UIElementBuildArgs(width, height));
}
private Grid InitializeHorizontalGrid(int width, int height, IUIElementBuilder<TextOptionElement>[] initOptions)
{
var rows = Enumerable.Repeat(GridRow.FromRowRelation(1), 1);
var columns = initOptions
.Select(builder => builder.Size.IsWidthRelational
? GridColumn.FromColumnRelation(builder.Size.WidthRelation.Value)
: GridColumn.FromWidth(builder.Size.Width.Value));
var gridDefinition = new GridDefinition(
GridRowDefinition.From(rows),
GridColumnDefinition.From(columns));
var gridBuilder = new GridBuilder(width, height, gridDefinition)
{
BorderLineCharSet = BorderLineCharSet,
BorderKind = BorderKind,
BorderColor = BorderColor,
};
for (int column = 0; column < initOptions.Length; column++)
{
gridBuilder.Add(initOptions[column].UnsafeWithSize(Size.FullSize), 0, column);
}
return gridBuilder.Build(new UIElementBuildArgs(width, height));
}
UIElement IUIElementBuilder.Build(UIElementBuildArgs args) => Build(args);
private IUIElementBuilder<TextOptionElement>[] InitializeOptionsVertical(string[] options)
{
int count = options.Length;
double heightRelational = 1.0 / count;
const double wideWidth = 1.0;
var result = new IUIElementBuilder<TextOptionElement>[count];
for (int i = 0; i < count; i++)
{
var textOptionBuilder = new TextOptionElementBuilder(new Size(wideWidth, heightRelational), options[i], ColorSet);
result[i] = textOptionBuilder;
}
return result;
}
private IUIElementBuilder<TextOptionElement>[] InitializeOptionsHorizontal(string[] options)
{
int count = options.Length;
double widthRelational = 1.0 / count;
const double wideHeight = 1.0;
var result = new IUIElementBuilder<TextOptionElement>[count];
for (int i = 0; i < count; i++)
{
var textOptionBuilder = new TextOptionElementBuilder(new Size(widthRelational, wideHeight), options[i], ColorSet);
result[i] = textOptionBuilder;
}
return result;
}
public RowTextChooserBuilder(int width, int height, Orientation orientation, [Pure] IEnumerable<string>? initOptions = null)
: this(new Size(width, height), orientation, initOptions)
{ }
public RowTextChooserBuilder(int width, double heightRelation, Orientation orientation, [Pure] IEnumerable<string>? initOptions = null)
: this(new Size(width, heightRelation), orientation, initOptions)
{ }
public RowTextChooserBuilder(double widthRelation, int height, Orientation orientation, [Pure] IEnumerable<string>? initOptions = null)
: this(new Size(widthRelation, height), orientation, initOptions)
{ }
public RowTextChooserBuilder(double widthRelation, double heightRelation, Orientation orientation, [Pure] IEnumerable<string>? initOptions = null)
: this(new Size(widthRelation, heightRelation), orientation, initOptions)
{ }
public RowTextChooserBuilder(Size size, Orientation orientation, [Pure] IEnumerable<string>? initOptions = null)
{
ArgumentNullException.ThrowIfNull(size, nameof(size));
Size = size;
Orientation = orientation;
_options = initOptions?.ToList() ?? new List<string>();
}
}