-
Notifications
You must be signed in to change notification settings - Fork 0
/
RenderFrameBuilder.cs
160 lines (133 loc) · 4.65 KB
/
RenderFrameBuilder.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
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Rendering;
using Microsoft.AspNetCore.Components.RenderTree;
using System.Runtime.InteropServices;
namespace BlazorTransitionGroup.Internal;
#pragma warning disable BL0006
enum RenderFrame {
OpenElement,
CloseElement,
OpenComponent,
CloseComponent,
Attribute,
Text,
ElementReferenceCapture,
ComponentReferenceCapture,
}
[StructLayout(LayoutKind.Explicit, Pack = 4)]
struct Frame {
[FieldOffset(0)] RenderFrame _renderFrame;
// ref
[FieldOffset(8)] Action<ElementReference> _lementReferenceCaptureAction;
[FieldOffset(8)] Action<object> _componentReferenceCaptureAction;
// component and element
[FieldOffset(8)] object? _key;
// component
[FieldOffset(16)] Type _type;
[FieldOffset(24)] bool _isAppendKey;
// element
[FieldOffset(16)] string _name;
// attribute
[FieldOffset(24)] RenderTreeFrame _renderTreeFrame;
// text content
[FieldOffset(8)] string _text;
public RenderFrame RenderFrame {
get => _renderFrame;
set => _renderFrame = value;
}
public object? Key {
get => _key;
set => _key = value;
}
public Type Type {
get => _type;
set => _type = value;
}
public bool IsAppendKey {
get => _isAppendKey;
set => _isAppendKey = value;
}
public string Name {
get => _name;
set => _name = value;
}
public string Text {
get => _text;
set => _text = value;
}
public RenderTreeFrame RenderTreeFrame {
get => _renderTreeFrame;
set => _renderTreeFrame = value;
}
public Action<ElementReference> ElementReferenceCaptureAction {
get => _lementReferenceCaptureAction;
set => _lementReferenceCaptureAction = value;
}
public Action<object> ComponentReferenceCaptureAction {
get => _componentReferenceCaptureAction;
set => _componentReferenceCaptureAction = value;
}
};
class RenderFrameBuilder {
readonly List<Frame> _frames = new();
public object Key { get; }
public bool IsAnimationElement { get; set; }
public RenderFrameBuilder(object key) {
Key = key;
}
public void Add(Frame frame) {
_frames.Add(frame);
}
public void RenderChild(ref int sequence, RenderTreeBuilder builder) {
foreach (var frame in _frames) {
switch (frame.RenderFrame) {
case RenderFrame.OpenElement: {
builder.OpenElement(sequence++, frame.Name);
if (frame.Key != null) {
builder.SetKey(frame.Name);
}
break;
}
case RenderFrame.OpenComponent: {
builder.OpenComponent(sequence++, frame.Type);
if (frame.Key != null) {
builder.SetKey(frame.Key);
}
if (frame.IsAppendKey) {
builder.AddAttribute(sequence++, "Key", frame.Key);
}
break;
}
case RenderFrame.Attribute: {
builder.AddAttribute(sequence++, frame.RenderTreeFrame);
break;
}
case RenderFrame.Text: {
builder.AddContent(sequence++, frame.Text);
break;
}
case RenderFrame.ElementReferenceCapture: {
builder.AddElementReferenceCapture(sequence++, frame.ElementReferenceCaptureAction);
break;
}
case RenderFrame.ComponentReferenceCapture: {
builder.AddComponentReferenceCapture(sequence++, frame.ComponentReferenceCaptureAction);
break;
}
case RenderFrame.CloseComponent: {
builder.CloseComponent();
break;
}
case RenderFrame.CloseElement: {
builder.CloseElement();
break;
}
}
}
}
}
public static class WithIndexExtension {
public static IEnumerable<(T Value, int Index)> WithIndex<T>(this IEnumerable<T> sequence) {
return sequence.Select((x, i) => (x, i));
}
}