-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCanvasBuilder.cs
195 lines (141 loc) · 7.47 KB
/
CanvasBuilder.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
// 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 CanvasBuilder : IUIElementBuilder<Canvas>
{
private readonly List<QueuedPositionChild> _orderedQueuedChildren = new();
public Size Size { get; }
public bool FocusFlowLoop { get; init; } = false;
public bool OverridesFocusFlow { get; init; } = true;
public bool EnableOverlapping { get; init; } = true;
public ImmutableList<ConsoleKey> FocusChangeKeys { get; init; } = new[] { ConsoleKey.Tab }.ToImmutableList();
public CanvasBuilder Add(IUIElementBuilder elementBuilder, int left, int top)
=> Add(elementBuilder, new Position(left, top));
public CanvasBuilder Add(IUIElementBuilder elementBuilder, int left, double topRelation)
=> Add(elementBuilder, new Position(left, topRelation));
public CanvasBuilder Add(IUIElementBuilder elementBuilder, double leftRelation, int top)
=> Add(elementBuilder, new Position(leftRelation, top));
public CanvasBuilder Add(IUIElementBuilder elementBuilder, double leftRelation, double topRelation)
=> Add(elementBuilder, new Position(leftRelation, topRelation));
public CanvasBuilder Add(IUIElementBuilder elementBuilder, Position position)
{
ArgumentNullException.ThrowIfNull(elementBuilder, nameof(elementBuilder));
ArgumentNullException.ThrowIfNull(position, nameof(position));
_orderedQueuedChildren.Add(new QueuedPositionChild(elementBuilder, null, position));
return this;
}
public CanvasBuilder Add(IUIElementBuilder elementBuilder, int left, int top, out BuiltUIElement builtUIElement)
=> Add(elementBuilder, new Position(left, top), out builtUIElement);
public CanvasBuilder Add(IUIElementBuilder elementBuilder, int left, double topRelation, out BuiltUIElement builtUIElement)
=> Add(elementBuilder, new Position(left, topRelation), out builtUIElement);
public CanvasBuilder Add(IUIElementBuilder elementBuilder, double leftRelation, int top, out BuiltUIElement builtUIElement)
=> Add(elementBuilder, new Position(leftRelation, top), out builtUIElement);
public CanvasBuilder Add(IUIElementBuilder elementBuilder, double leftRelation, double topRelation, out BuiltUIElement builtUIElement)
=> Add(elementBuilder, new Position(leftRelation, topRelation), out builtUIElement);
public CanvasBuilder Add(IUIElementBuilder elementBuilder, Position position, out BuiltUIElement builtUIElement)
{
ArgumentNullException.ThrowIfNull(elementBuilder, nameof(elementBuilder));
ArgumentNullException.ThrowIfNull(position, nameof(position));
var initializer = new UIElementInitializer<UIElement>();
builtUIElement = new BuiltUIElement(initializer);
_orderedQueuedChildren.Add(new QueuedPositionChild(elementBuilder, initializer, position));
return this;
}
public CanvasBuilder Add<TUIElement>(IUIElementBuilder<TUIElement> elementBuilder, int left, int top,
out BuiltUIElement<TUIElement> builtUIElement)
where TUIElement : UIElement
=> Add(elementBuilder, new Position(left, top), out builtUIElement);
public CanvasBuilder Add<TUIElement>(IUIElementBuilder<TUIElement> elementBuilder, int left, double topRelation,
out BuiltUIElement<TUIElement> builtUIElement)
where TUIElement : UIElement
=> Add(elementBuilder, new Position(left, topRelation), out builtUIElement);
public CanvasBuilder Add<TUIElement>(IUIElementBuilder<TUIElement> elementBuilder, double leftRelation, int top,
out BuiltUIElement<TUIElement> builtUIElement)
where TUIElement : UIElement
=> Add(elementBuilder, new Position(leftRelation, top), out builtUIElement);
public CanvasBuilder Add<TUIElement>(IUIElementBuilder<TUIElement> elementBuilder,
double leftRelation, double topRelation,
out BuiltUIElement<TUIElement> builtUIElement)
where TUIElement : UIElement
=> Add(elementBuilder, new Position(leftRelation, topRelation), out builtUIElement);
public CanvasBuilder Add<TUIElement>(IUIElementBuilder<TUIElement> elementBuilder, Position position,
out BuiltUIElement<TUIElement> builtUIElement)
where TUIElement : UIElement
{
ArgumentNullException.ThrowIfNull(elementBuilder, nameof(elementBuilder));
ArgumentNullException.ThrowIfNull(position, nameof(position));
var initializer = new UIElementInitializer<TUIElement>();
builtUIElement = new BuiltUIElement<TUIElement>(initializer);
_orderedQueuedChildren.Add(new QueuedPositionChild(elementBuilder, initializer, position));
return this;
}
public Canvas Build(UIElementBuildArgs args)
{
ArgumentNullException.ThrowIfNull(args, nameof(args));
var placementBuilder = new ElementsFieldBuilder(args.Width, args.Height, EnableOverlapping);
foreach (var queuedChild in _orderedQueuedChildren)
{
placementBuilder.Place(queuedChild.Builder, queuedChild.Position, out var childInfo);
if (queuedChild.Initializer is not null)
{
queuedChild.Initializer.Initialize(childInfo.Child);
}
}
var orderedChildren = placementBuilder.Build();
var focusFlowSpecification = InitializeFocusSpecification(orderedChildren);
var resultCanvas = new Canvas(args.Width, args.Height, focusFlowSpecification, orderedChildren);
return resultCanvas;
}
private FocusFlowSpecification InitializeFocusSpecification(IReadOnlyList<ChildInfo> orderedChildren)
{
var specBuilder = new FocusFlowSpecificationBuilder(OverridesFocusFlow);
var focusables = orderedChildren
.Where(child => child.IsFocusable)
.Select(child => (IFocusable)child.Child)
.ToArray();
if (focusables.Length <= 1)
{
if (focusables.Length == 1)
{
specBuilder.Add(focusables[0]);
}
return specBuilder.Build();
}
foreach (var focusable in focusables)
{
specBuilder.Add(focusable);
}
for (int i = 0; i < focusables.Length - 1; i++)
{
specBuilder.AddFlow(focusables[i], focusables[i + 1], FocusChangeKeys);
}
if (FocusFlowLoop)
{
specBuilder.AddFlow(focusables[^1], focusables[0], FocusChangeKeys);
}
else
{
specBuilder.AddLoseFocus(focusables[^1], FocusChangeKeys);
}
return specBuilder.Build();
}
UIElement IUIElementBuilder.Build(UIElementBuildArgs args) => Build(args);
public CanvasBuilder(int width, int height)
: this(new Size(width, height))
{ }
public CanvasBuilder(int width, double heightRelation)
: this(new Size(width, heightRelation))
{ }
public CanvasBuilder(double widthRelation, int height)
: this(new Size(widthRelation, height))
{ }
public CanvasBuilder(double widthRelation, double heightRelation)
: this(new Size(widthRelation, heightRelation))
{ }
public CanvasBuilder(Size size)
{
ArgumentNullException.ThrowIfNull(size, nameof(size));
Size = size;
}
}