-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathViewTableBuilder.cs
192 lines (137 loc) · 7.42 KB
/
ViewTableBuilder.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
// Developed by Bulat Bagaviev (@sunnyyssh).
// This file is licensed to you under the MIT license.
using System.Collections.Immutable;
namespace Sunnyyssh.ConsoleUI;
public sealed class ViewTableBuilder : IUIElementBuilder<ViewTable>
{
private static readonly ImmutableList<ConsoleKey> DefaultMoveUpKeys
= new[] { ConsoleKey.UpArrow }.ToImmutableList();
private static readonly ImmutableList<ConsoleKey> DefaultMoveDownKeys
= new[] { ConsoleKey.DownArrow }.ToImmutableList();
private static readonly ImmutableList<ConsoleKey> DefaultMoveRightKeys
= new[] { ConsoleKey.RightArrow }.ToImmutableList();
private static readonly ImmutableList<ConsoleKey> DefaultMoveLeftKeys
= new[] { ConsoleKey.LeftArrow }.ToImmutableList();
private readonly ImmutableList<ImmutableList<string?>> _initData;
private readonly GridDefinition _gridDefinition;
private readonly Color? _cellFocusedBackground = Color.DarkGray;
private readonly Color? _cellFocusedForeground;
private readonly Color? _borderColorFocused;
public ImmutableList<string> Headers { get; }
public Size Size { get; }
#region init properties.
public bool EnterOnCellChange { get; init; }
public bool LoseFocusOnEnter { get; init; } = false;
public LineKind BorderLineKind { get; init; } = LineKind.Single;
public LineCharSet? BorderCharSet { get; init; } = null;
public OverlappingPriority OverlappingPriority { get; init; } = OverlappingPriority.Medium;
public Color CellNotFocusedBackground { get; init; } = Color.Default;
public Color HeaderBackground { get; init; } = Color.Default;
public Color CellNotFocusedForeground { get; init; } = Color.Default;
public Color HeaderForeground { get; init; } = Color.Default;
public Color CellFocusedBackground
{
get => _cellFocusedBackground ?? CellNotFocusedBackground;
init => _cellFocusedBackground = value;
}
public Color CellFocusedForeground
{
get => _cellFocusedForeground ?? CellNotFocusedForeground;
init => _cellFocusedForeground = value;
}
public Color BorderColorNotFocused { get; init; } = Color.Default;
public Color BorderColorFocused
{
get => _borderColorFocused ?? BorderColorNotFocused;
init => _borderColorFocused = value;
}
public ImmutableList<ConsoleKey>? MoveRightKeys { get; init; } = DefaultMoveRightKeys;
public ImmutableList<ConsoleKey>? MoveLeftKeys { get; init; } = DefaultMoveLeftKeys;
public ImmutableList<ConsoleKey>? MoveUpKeys { get; init; } = DefaultMoveUpKeys;
public ImmutableList<ConsoleKey>? MoveDownKeys { get; init; } = DefaultMoveDownKeys;
public bool CellsWordWrap { get; init; } = true;
public bool UserEditable { get; init; } = true;
#endregion
public ViewTable Build(UIElementBuildArgs args)
{
ArgumentNullException.ThrowIfNull(args, nameof(args));
int width = args.Width;
int height = args.Height;
var borderCharSet = BorderCharSet ?? LineCharSets.Of(BorderLineKind);
var absoluteGridDefinition = GridBuilder.ResolveGrid(width, height, true, _gridDefinition);
var resultViewTable = new ViewTable(width, height, Headers, borderCharSet,
_initData, absoluteGridDefinition, OverlappingPriority)
{
CellNotFocusedBackground = CellNotFocusedBackground,
HeaderBackground = HeaderBackground,
CellNotFocusedForeground = CellNotFocusedForeground,
HeaderForeground = HeaderForeground,
CellFocusedBackground = CellFocusedBackground,
CellFocusedForeground = CellFocusedForeground,
BorderColorNotFocused = BorderColorNotFocused,
BorderColorFocused = BorderColorFocused,
MoveRightKeys = MoveRightKeys,
MoveLeftKeys = MoveLeftKeys,
MoveUpKeys = MoveUpKeys,
MoveDownKeys = MoveDownKeys,
CellsWordWrap = CellsWordWrap,
UserEditable = UserEditable,
LoseFocusOnEnter = LoseFocusOnEnter,
EnterOnCellChange = EnterOnCellChange,
};
return resultViewTable;
}
UIElement IUIElementBuilder.Build(UIElementBuildArgs args) => Build(args);
private static ImmutableList<ImmutableList<string?>> CreateEmptyInitValues(int columnsCount, int dataRowsCount)
{
return Enumerable.Repeat(
Enumerable.Repeat((string?)null, columnsCount)
.ToImmutableList(),
dataRowsCount)
.ToImmutableList();
}
#region Constructors
public ViewTableBuilder(int width, int height, ImmutableList<string> headers, GridDefinition gridDefinition, int dataRowsCount)
: this(new Size(width, height), headers, gridDefinition, dataRowsCount)
{ }
public ViewTableBuilder(int width, double heightRelational, ImmutableList<string> headers, GridDefinition gridDefinition, int dataRowsCount)
: this(new Size(width, heightRelational), headers, gridDefinition, dataRowsCount)
{ }
public ViewTableBuilder(double widthRelational, int height, ImmutableList<string> headers, GridDefinition gridDefinition, int dataRowsCount)
: this(new Size(widthRelational, height), headers, gridDefinition, dataRowsCount)
{ }
public ViewTableBuilder(double widthRelational, double heightRelational, ImmutableList<string> headers, GridDefinition gridDefinition, int dataRowsCount)
: this(new Size(widthRelational, heightRelational), headers, gridDefinition, dataRowsCount)
{ }
public ViewTableBuilder(Size size, ImmutableList<string> headers, GridDefinition gridDefinition, int dataRowsCount)
: this(size, headers, gridDefinition, CreateEmptyInitValues(headers.Count, dataRowsCount))
{ }
public ViewTableBuilder(int width, int height, ImmutableList<string> headers, GridDefinition? gridDefinition,
ImmutableList<ImmutableList<string?>> initData)
: this(new Size(width, height), headers, gridDefinition, initData)
{ }
public ViewTableBuilder(int width, double heightRelational, ImmutableList<string> headers, GridDefinition? gridDefinition,
ImmutableList<ImmutableList<string?>> initData)
: this(new Size(width, heightRelational), headers, gridDefinition, initData)
{ }
public ViewTableBuilder(double widthRelational, int height, ImmutableList<string> headers, GridDefinition? gridDefinition,
ImmutableList<ImmutableList<string?>> initData)
: this(new Size(widthRelational, height), headers, gridDefinition, initData)
{ }
public ViewTableBuilder(double widthRelational, double heightRelational, ImmutableList<string> headers, GridDefinition? gridDefinition,
ImmutableList<ImmutableList<string?>> initData)
: this(new Size(widthRelational, heightRelational), headers, gridDefinition, initData)
{ }
public ViewTableBuilder(Size size, ImmutableList<string> headers, GridDefinition? gridDefinition,
ImmutableList<ImmutableList<string?>> initData)
{
ArgumentNullException.ThrowIfNull(headers, nameof(headers));
ArgumentNullException.ThrowIfNull(gridDefinition, nameof(gridDefinition));
ArgumentNullException.ThrowIfNull(initData, nameof(initData));
_initData = initData;
_gridDefinition = gridDefinition;
Size = size;
Headers = headers;
}
#endregion
}