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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
|
package layout
import "testing"
type testNode struct {
style Style
children []*testNode
layout Layout
dirty bool
parent *testNode
intrinsicW int
intrinsicH int
}
func newTestNode(style Style) *testNode {
return &testNode{
style: style,
dirty: true,
}
}
func (n *testNode) LayoutStyle() Style { return n.style }
func (n *testNode) LayoutChildren() []Layoutable {
result := make([]Layoutable, len(n.children))
for i, child := range n.children {
result[i] = child
}
return result
}
func (n *testNode) SetLayout(l Layout) { n.layout = l }
func (n *testNode) GetLayout() Layout { return n.layout }
func (n *testNode) IsDirty() bool { return n.dirty }
func (n *testNode) SetDirty(d bool) { n.dirty = d }
func (n *testNode) Tag() string { return "" }
func (n *testNode) HeightForWidth(width int) int {
_, h := n.IntrinsicSize()
return h
}
func (n *testNode) IntrinsicSize() (width, height int) {
if n.intrinsicW > 0 || n.intrinsicH > 0 {
return n.intrinsicW, n.intrinsicH
}
if !n.style.Width.IsAuto() {
width = int(n.style.Width.Amount)
}
if !n.style.Height.IsAuto() {
height = int(n.style.Height.Amount)
}
if width > 0 || height > 0 {
return width, height
}
if len(n.children) == 0 {
return 0, 0
}
isRow := n.style.Direction == Row
if n.style.Display == DisplayBlock {
isRow = false
}
var intrinsicW, intrinsicH int
for i, child := range n.children {
childW, childH := child.IntrinsicSize()
childStyle := child.LayoutStyle()
marginH := childStyle.Margin.Horizontal()
marginV := childStyle.Margin.Vertical()
if isRow {
intrinsicW += childW + marginH
if childH+marginV > intrinsicH {
intrinsicH = childH + marginV
}
} else {
if childW+marginH > intrinsicW {
intrinsicW = childW + marginH
}
intrinsicH += childH + marginV
}
if i > 0 {
if isRow {
intrinsicW += n.style.Gap
} else {
intrinsicH += n.style.Gap
}
}
}
intrinsicW += n.style.Padding.Horizontal()
intrinsicH += n.style.Padding.Vertical()
return intrinsicW, intrinsicH
}
func (n *testNode) SetIntrinsicSize(width, height int) {
n.intrinsicW = width
n.intrinsicH = height
}
func (n *testNode) AddChild(children ...*testNode) {
for _, child := range children {
child.parent = n
n.children = append(n.children, child)
}
n.markDirty()
}
func (n *testNode) RemoveChild(child *testNode) bool {
for i, c := range n.children {
if c == child {
n.children[i] = n.children[len(n.children)-1]
n.children = n.children[:len(n.children)-1]
child.parent = nil
n.markDirty()
return true
}
}
return false
}
func (n *testNode) SetStyle(style Style) {
n.style = style
n.markDirty()
}
func (n *testNode) markDirty() {
for node := n; node != nil && !node.dirty; node = node.parent {
node.dirty = true
}
}
func TestTestNode_ImplementsLayoutable(t *testing.T) {
var _ Layoutable = (*testNode)(nil)
}
func TestTestNode_NewNode(t *testing.T) {
style := DefaultStyle()
style.Width = Fixed(100)
style.Height = Fixed(50)
node := newTestNode(style)
if node.style.Width != Fixed(100) {
t.Errorf("newTestNode style.Width = %+v, want Fixed(100)", node.style.Width)
}
if node.style.Height != Fixed(50) {
t.Errorf("newTestNode style.Height = %+v, want Fixed(50)", node.style.Height)
}
if !node.IsDirty() {
t.Error("newTestNode should be dirty")
}
if len(node.children) != 0 {
t.Errorf("newTestNode should have no children, got %d", len(node.children))
}
}
func TestTestNode_AddChild(t *testing.T) {
parent := newTestNode(DefaultStyle())
child1 := newTestNode(DefaultStyle())
child2 := newTestNode(DefaultStyle())
parent.dirty = false
parent.AddChild(child1, child2)
if len(parent.children) != 2 {
t.Errorf("AddChild: len(children) = %d, want 2", len(parent.children))
}
if parent.children[0] != child1 {
t.Error("AddChild: first child mismatch")
}
if parent.children[1] != child2 {
t.Error("AddChild: second child mismatch")
}
if child1.parent != parent {
t.Error("AddChild: child1.parent not set")
}
if child2.parent != parent {
t.Error("AddChild: child2.parent not set")
}
if !parent.IsDirty() {
t.Error("AddChild should mark parent dirty")
}
}
func TestTestNode_RemoveChild(t *testing.T) {
type tc struct {
setup func() (*testNode, *testNode, *testNode)
removeChild func(*testNode, *testNode, *testNode) *testNode
expectFound bool
expectLen int
}
tests := map[string]tc{
"remove existing child": {
setup: func() (*testNode, *testNode, *testNode) {
parent := newTestNode(DefaultStyle())
child1 := newTestNode(DefaultStyle())
child2 := newTestNode(DefaultStyle())
parent.AddChild(child1, child2)
parent.dirty = false
return parent, child1, child2
},
removeChild: func(parent, child1, child2 *testNode) *testNode { return child1 },
expectFound: true,
expectLen: 1,
},
"remove non-existent child": {
setup: func() (*testNode, *testNode, *testNode) {
parent := newTestNode(DefaultStyle())
child1 := newTestNode(DefaultStyle())
parent.AddChild(child1)
parent.dirty = false
otherChild := newTestNode(DefaultStyle())
return parent, child1, otherChild
},
removeChild: func(parent, child1, otherChild *testNode) *testNode { return otherChild },
expectFound: false,
expectLen: 1,
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
parent, child1, child2 := tt.setup()
toRemove := tt.removeChild(parent, child1, child2)
wasDirty := parent.IsDirty()
found := parent.RemoveChild(toRemove)
if found != tt.expectFound {
t.Errorf("RemoveChild returned %v, want %v", found, tt.expectFound)
}
if len(parent.children) != tt.expectLen {
t.Errorf("len(children) = %d, want %d", len(parent.children), tt.expectLen)
}
if tt.expectFound {
if toRemove.parent != nil {
t.Error("removed child's parent should be nil")
}
if !parent.IsDirty() {
t.Error("RemoveChild should mark parent dirty")
}
} else {
if parent.IsDirty() != wasDirty {
t.Error("RemoveChild should not change dirty state when child not found")
}
}
})
}
}
func TestTestNode_SetStyle(t *testing.T) {
node := newTestNode(DefaultStyle())
node.dirty = false
newStyle := DefaultStyle()
newStyle.Width = Fixed(200)
node.SetStyle(newStyle)
if node.style.Width != Fixed(200) {
t.Errorf("SetStyle did not update style, Width = %+v", node.style.Width)
}
if !node.IsDirty() {
t.Error("SetStyle should mark node dirty")
}
}
func TestTestNode_MarkDirty_PropagatesUp(t *testing.T) {
root := newTestNode(DefaultStyle())
middle := newTestNode(DefaultStyle())
leaf := newTestNode(DefaultStyle())
root.AddChild(middle)
middle.AddChild(leaf)
root.dirty = false
middle.dirty = false
leaf.dirty = false
leaf.markDirty()
if !leaf.IsDirty() {
t.Error("leaf should be dirty")
}
if !middle.IsDirty() {
t.Error("middle should be dirty (propagated from leaf)")
}
if !root.IsDirty() {
t.Error("root should be dirty (propagated from leaf)")
}
}
func TestTestNode_MarkDirty_StopsAtAlreadyDirty(t *testing.T) {
root := newTestNode(DefaultStyle())
middle := newTestNode(DefaultStyle())
leaf := newTestNode(DefaultStyle())
root.AddChild(middle)
middle.AddChild(leaf)
root.dirty = false
middle.dirty = false
leaf.dirty = false
middle.dirty = true
leaf.markDirty()
if !leaf.IsDirty() {
t.Error("leaf should be dirty")
}
if !middle.IsDirty() {
t.Error("middle should still be dirty")
}
if root.IsDirty() {
t.Error("root should still be clean (propagation stopped at middle)")
}
}
func TestTestNode_DirtyPropagationAfterAddChild(t *testing.T) {
root := newTestNode(DefaultStyle())
parent := newTestNode(DefaultStyle())
root.AddChild(parent)
root.dirty = false
parent.dirty = false
child := newTestNode(DefaultStyle())
parent.AddChild(child)
if !parent.IsDirty() {
t.Error("parent should be dirty after AddChild")
}
if !root.IsDirty() {
t.Error("root should be dirty (propagated from parent)")
}
}
func TestDefaultStyle(t *testing.T) {
style := DefaultStyle()
if !style.Width.IsAuto() {
t.Error("DefaultStyle Width should be Auto")
}
if !style.Height.IsAuto() {
t.Error("DefaultStyle Height should be Auto")
}
if !style.MinWidth.IsAuto() {
t.Errorf("DefaultStyle MinWidth should be Auto, got %+v", style.MinWidth)
}
if !style.MinHeight.IsAuto() {
t.Errorf("DefaultStyle MinHeight should be Auto, got %+v", style.MinHeight)
}
if !style.MaxWidth.IsAuto() {
t.Error("DefaultStyle MaxWidth should be Auto")
}
if !style.MaxHeight.IsAuto() {
t.Error("DefaultStyle MaxHeight should be Auto")
}
if style.Display != DisplayBlock {
t.Errorf("DefaultStyle Display = %v, want DisplayBlock", style.Display)
}
if style.Direction != Row {
t.Errorf("DefaultStyle Direction = %v, want Row", style.Direction)
}
if style.AlignItems != AlignStretch {
t.Errorf("DefaultStyle AlignItems = %v, want AlignStretch", style.AlignItems)
}
if style.FlexShrink != 1.0 {
t.Errorf("DefaultStyle FlexShrink = %v, want 1.0", style.FlexShrink)
}
if style.FlexGrow != 0 {
t.Errorf("DefaultStyle FlexGrow = %v, want 0", style.FlexGrow)
}
if style.Gap != 0 {
t.Errorf("DefaultStyle Gap = %v, want 0", style.Gap)
}
if style.AlignSelf != nil {
t.Errorf("DefaultStyle AlignSelf should be nil, got %v", style.AlignSelf)
}
}
|