float-pigment-forest 0.8.2

A node tree implementation for float-pigment-layout.
Documentation
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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
// Tests for layout cache invalidation
// These tests ensure the layout engine correctly invalidates and recalculates
// positions when style properties change. Key scenarios:
// - order property changes in flex container
// - flex-direction changes
// - display property changes
// - Style mutations requiring position recalculation

use crate::*;
use float_pigment_css::typing::*;

unsafe fn as_ref<'a>(node: *mut Node) -> &'a Node {
    &*node
}

// Case: Position cache invalidation when order changes
// Spec points:
// - Changing order property reorders flex items visually
// - Positions must be recalculated after order change
// In this test:
// - Initial: items 1,2,3,4 with widths 1,2,3,4 and order 1,2,3,4
// - After: order becomes 4,3,2,1 (reversed)
// - Items should be repositioned in reverse order
#[test]
pub fn position_cache_if_order_changed() {
    unsafe {
        let root = as_ref(Node::new_ptr());
        let container = as_ref(Node::new_ptr());
        container.set_display(Display::Flex);
        container.set_width(DefLength::Points(Len::from_f32(300.)));
        container.set_height(DefLength::Points(Len::from_f32(100.)));
        root.append_child(convert_node_ref_to_ptr(container));
        for i in 0..4 {
            let item = as_ref(Node::new_ptr());
            item.set_width(DefLength::Points(Len::from_f32(1. * (i + 1) as f32)));
            item.set_height(DefLength::Points(Len::from_f32(1. * (i + 1) as f32)));
            container.append_child(convert_node_ref_to_ptr(item));
        }
        root.layout(
            OptionSize::new(
                OptionNum::some(Len::from_f32(375.)),
                OptionNum::some(Len::from_f32(750.)),
            ),
            Size::new(Len::from_f32(0.), Len::from_f32(0.)),
        );

        assert_eq!(
            container.get_child_at(0).unwrap().layout_position().left,
            0.
        );
        assert_eq!(
            container.get_child_at(1).unwrap().layout_position().left,
            1.
        );
        assert_eq!(
            container.get_child_at(2).unwrap().layout_position().left,
            3.
        );
        assert_eq!(
            container.get_child_at(3).unwrap().layout_position().left,
            6.
        );

        for i in 0..4 {
            if let Some(item) = container.get_child_at(i) {
                item.set_order((4 - i) as i32);
            }
        }
        root.layout(
            OptionSize::new(
                OptionNum::some(Len::from_f32(375.)),
                OptionNum::some(Len::from_f32(750.)),
            ),
            Size::new(Len::from_f32(0.), Len::from_f32(0.)),
        );
        assert_eq!(
            container.get_child_at(0).unwrap().layout_position().left,
            9.
        );
        assert_eq!(
            container.get_child_at(1).unwrap().layout_position().left,
            7.
        );
        assert_eq!(
            container.get_child_at(2).unwrap().layout_position().left,
            4.
        );
        assert_eq!(
            container.get_child_at(3).unwrap().layout_position().left,
            0.
        );
    }
}

// Case: Position cache with equal-width items and order change
// Spec points:
// - Order change with equal-width items still requires recalculation
// In this test:
// - Four items of width=1px each
// - Initial positions: 0, 1, 2, 3
// - After order reversal: 3, 2, 1, 0
#[test]
pub fn position_cache_if_order_changed_2() {
    unsafe {
        let root = as_ref(Node::new_ptr());
        let container = as_ref(Node::new_ptr());
        container.set_display(Display::Flex);
        container.set_width(DefLength::Points(Len::from_f32(300.)));
        container.set_height(DefLength::Points(Len::from_f32(100.)));
        root.append_child(convert_node_ref_to_ptr(container));
        for _ in 0..4 {
            let item = as_ref(Node::new_ptr());
            item.set_width(DefLength::Points(Len::from_f32(1.)));
            item.set_height(DefLength::Points(Len::from_f32(1.)));
            container.append_child(convert_node_ref_to_ptr(item));
        }
        root.layout(
            OptionSize::new(
                OptionNum::some(Len::from_f32(375.)),
                OptionNum::some(Len::from_f32(750.)),
            ),
            Size::new(Len::from_f32(0.), Len::from_f32(0.)),
        );

        assert_eq!(
            container.get_child_at(0).unwrap().layout_position().left,
            0.
        );
        assert_eq!(
            container.get_child_at(1).unwrap().layout_position().left,
            1.
        );
        assert_eq!(
            container.get_child_at(2).unwrap().layout_position().left,
            2.
        );
        assert_eq!(
            container.get_child_at(3).unwrap().layout_position().left,
            3.
        );

        for i in 0..4 {
            if let Some(item) = container.get_child_at(i) {
                item.set_order((4 - i) as i32);
            }
        }
        root.layout(
            OptionSize::new(
                OptionNum::some(Len::from_f32(375.)),
                OptionNum::some(Len::from_f32(750.)),
            ),
            Size::new(Len::from_f32(0.), Len::from_f32(0.)),
        );
        assert_eq!(
            container.get_child_at(0).unwrap().layout_position().left,
            3.
        );
        assert_eq!(
            container.get_child_at(1).unwrap().layout_position().left,
            2.
        );
        assert_eq!(
            container.get_child_at(2).unwrap().layout_position().left,
            1.
        );
        assert_eq!(
            container.get_child_at(3).unwrap().layout_position().left,
            0.
        );
    }
}

// Case: Position cache invalidation when flex-direction changes
// Spec points:
// - Changing flex-direction from row to row-reverse
// - Items repositioned from left-to-right to right-to-left
// In this test:
// - Initial: row direction, items at 0, 1, 3, 6
// - After: row-reverse, items positioned from right edge
#[test]
pub fn position_cache_if_flex_direction_changed() {
    unsafe {
        let root = as_ref(Node::new_ptr());
        let container = as_ref(Node::new_ptr());
        container.set_display(Display::Flex);
        container.set_width(DefLength::Points(Len::from_f32(300.)));
        container.set_height(DefLength::Points(Len::from_f32(100.)));
        root.append_child(convert_node_ref_to_ptr(container));
        for i in 0..4 {
            let item = as_ref(Node::new_ptr());
            item.set_width(DefLength::Points(Len::from_f32(1. * (i + 1) as f32)));
            item.set_height(DefLength::Points(Len::from_f32(1. * (i + 1) as f32)));
            container.append_child(convert_node_ref_to_ptr(item));
        }
        root.layout(
            OptionSize::new(
                OptionNum::some(Len::from_f32(375.)),
                OptionNum::some(Len::from_f32(750.)),
            ),
            Size::new(Len::from_f32(0.), Len::from_f32(0.)),
        );
        assert_eq!(
            container.get_child_at(0).unwrap().layout_position().left,
            0.
        );
        assert_eq!(
            container.get_child_at(1).unwrap().layout_position().left,
            1.
        );
        assert_eq!(
            container.get_child_at(2).unwrap().layout_position().left,
            3.
        );
        assert_eq!(
            container.get_child_at(3).unwrap().layout_position().left,
            6.
        );
        container.set_flex_direction(FlexDirection::RowReverse);
        root.layout(
            OptionSize::new(
                OptionNum::some(Len::from_f32(375.)),
                OptionNum::some(Len::from_f32(750.)),
            ),
            Size::new(Len::from_f32(0.), Len::from_f32(0.)),
        );
        assert_eq!(
            container.get_child_at(0).unwrap().layout_position().left,
            299.
        );
        assert_eq!(
            container.get_child_at(1).unwrap().layout_position().left,
            297.
        );
        assert_eq!(
            container.get_child_at(2).unwrap().layout_position().left,
            294.
        );
        assert_eq!(
            container.get_child_at(3).unwrap().layout_position().left,
            290.
        );
    }
}

// Case: Position cache with equal-width items and flex-direction change
// Spec points:
// - Direction change with equal-width items
// In this test:
// - Four items of width=1px
// - Initial: 0, 1, 2, 3
// - After row-reverse: 299, 298, 297, 296
#[test]
pub fn position_cache_if_flex_direction_changed_2() {
    unsafe {
        let root = as_ref(Node::new_ptr());
        let container = as_ref(Node::new_ptr());
        container.set_display(Display::Flex);
        container.set_width(DefLength::Points(Len::from_f32(300.)));
        container.set_height(DefLength::Points(Len::from_f32(100.)));
        root.append_child(convert_node_ref_to_ptr(container));
        for _ in 0..4 {
            let item = as_ref(Node::new_ptr());
            item.set_width(DefLength::Points(Len::from_f32(1.)));
            item.set_height(DefLength::Points(Len::from_f32(1.)));
            container.append_child(convert_node_ref_to_ptr(item));
        }
        root.layout(
            OptionSize::new(
                OptionNum::some(Len::from_f32(375.)),
                OptionNum::some(Len::from_f32(750.)),
            ),
            Size::new(Len::from_f32(0.), Len::from_f32(0.)),
        );
        assert_eq!(
            container.get_child_at(0).unwrap().layout_position().left,
            0.
        );
        assert_eq!(
            container.get_child_at(1).unwrap().layout_position().left,
            1.
        );
        assert_eq!(
            container.get_child_at(2).unwrap().layout_position().left,
            2.
        );
        assert_eq!(
            container.get_child_at(3).unwrap().layout_position().left,
            3.
        );
        container.set_flex_direction(FlexDirection::RowReverse);
        root.layout(
            OptionSize::new(
                OptionNum::some(Len::from_f32(375.)),
                OptionNum::some(Len::from_f32(750.)),
            ),
            Size::new(Len::from_f32(0.), Len::from_f32(0.)),
        );
        assert_eq!(
            container.get_child_at(0).unwrap().layout_position().left,
            299.
        );
        assert_eq!(
            container.get_child_at(1).unwrap().layout_position().left,
            298.
        );
        assert_eq!(
            container.get_child_at(2).unwrap().layout_position().left,
            297.
        );
        assert_eq!(
            container.get_child_at(3).unwrap().layout_position().left,
            296.
        );
    }
}

// Case: Position cache with absolute child and mark_dirty_propagate
// Spec points:
// - Absolute positioned children need recalculation on dirty
// - Sibling positions should remain stable
// In this test:
// - Nested absolute container with two children
// - After mark_dirty_propagate, positions should remain correct
#[test]
pub fn layout() {
    unsafe {
        let root = as_ref(Node::new_ptr());

        let container = as_ref(Node::new_ptr());
        container.set_height(DefLength::Points(Len::from_f32(700.)));

        let parent = as_ref(Node::new_ptr());
        parent.set_height(DefLength::Points(Len::from_f32(300.)));
        parent.set_width(DefLength::Points(Len::from_f32(300.)));
        parent.set_position(Position::Absolute);
        parent.set_top(DefLength::Points(Len::from_f32(10.)));
        parent.set_left(DefLength::Points(Len::from_f32(10.)));

        let child = as_ref(Node::new_ptr());
        child.set_width(DefLength::Points(Len::from_f32(50.)));
        child.set_position(Position::Absolute);
        child.set_top(DefLength::Points(Len::from_f32(10.)));
        child.set_left(DefLength::Points(Len::from_f32(10.)));

        let item = as_ref(Node::new_ptr());
        item.set_height(DefLength::Points(Len::from_f32(50.)));

        let item_b = as_ref(Node::new_ptr());
        item_b.set_height(DefLength::Points(Len::from_f32(50.)));

        root.append_child(convert_node_ref_to_ptr(container));
        container.append_child(convert_node_ref_to_ptr(parent));
        parent.append_child(convert_node_ref_to_ptr(child));
        child.append_child(convert_node_ref_to_ptr(item));
        child.append_child(convert_node_ref_to_ptr(item_b));

        root.layout(
            OptionSize::new(OptionNum::some(Len::from_f32(375.)), OptionNum::none()),
            Size::new(Len::from_f32(0.), Len::from_f32(0.)),
        );
        assert_eq!(item_b.layout_position().left, 0.);
        assert_eq!(item_b.layout_position().top, 50.);

        root.layout(
            OptionSize::new(OptionNum::some(Len::from_f32(375.)), OptionNum::none()),
            Size::new(Len::from_f32(0.), Len::from_f32(0.)),
        );
        item.mark_dirty_propagate();
        assert_eq!(item_b.layout_position().left, 0.);
        assert_eq!(item_b.layout_position().top, 50.);
    }
}

// Case: Clear position cache when parent display changes
// Spec points:
// - display: none removes element from layout (zero size)
// - Restoring display: flex restores original layout
// In this test:
// - Initial: flex container with absolute child at left=10
// - After display=none: child position becomes 0x0x0
// - After display=flex restored: child position restored
#[test]
pub fn clear_position_cache_if_parent_display_changed() {
    unsafe {
        let root = as_ref(Node::new_ptr());
        let container = as_ref(Node::new_ptr());
        container.set_display(Display::Flex);
        container.set_width(DefLength::Points(Len::from_f32(300.)));
        container.set_height(DefLength::Points(Len::from_f32(100.)));
        root.append_child(convert_node_ref_to_ptr(container));
        let item = as_ref(Node::new_ptr());
        item.set_width(DefLength::Points(Len::from_f32(10.)));
        item.set_height(DefLength::Points(Len::from_f32(10.)));
        item.set_position(Position::Absolute);
        item.set_left(DefLength::Points(Len::from_f32(10.)));
        container.append_child(convert_node_ref_to_ptr(item));

        root.layout(
            OptionSize::new(
                OptionNum::some(Len::from_f32(375.)),
                OptionNum::some(Len::from_f32(750.)),
            ),
            Size::new(Len::from_f32(0.), Len::from_f32(0.)),
        );

        assert_eq!(item.layout_position().left, 10.);
        assert_eq!(item.layout_position().width, 10.);
        assert_eq!(item.layout_position().height, 10.);
        container.set_display(Display::None);
        root.layout(
            OptionSize::new(
                OptionNum::some(Len::from_f32(375.)),
                OptionNum::some(Len::from_f32(750.)),
            ),
            Size::new(Len::from_f32(0.), Len::from_f32(0.)),
        );
        assert_eq!(item.layout_position().left, 0.);
        assert_eq!(item.layout_position().width, 0.);
        assert_eq!(item.layout_position().height, 0.);

        container.set_display(Display::Flex);
        root.layout(
            OptionSize::new(
                OptionNum::some(Len::from_f32(375.)),
                OptionNum::some(Len::from_f32(750.)),
            ),
            Size::new(Len::from_f32(0.), Len::from_f32(0.)),
        );
        assert_eq!(item.layout_position().left, 10.);
        assert_eq!(item.layout_position().width, 10.);
        assert_eq!(item.layout_position().height, 10.);
    }
}

// Case: Complex nested layout with inline elements
// Spec points:
// - Inline elements within flex items require careful cache management
// - Changing child height should trigger proper relayout
// In this test:
// - Nested structure with flex, block, and inline elements
// - After changing first child height, inline position should update correctly
#[test]
pub fn test() {
    unsafe {
        let root = as_ref(Node::new_ptr());
        let container = as_ref(Node::new_ptr());
        container.set_width(DefLength::Percent(1.0));
        container.set_height(DefLength::Percent(1.0));
        root.append_child(convert_node_ref_to_ptr(container));

        let flex_box = as_ref(Node::new_ptr());
        flex_box.set_display(Display::Flex);
        flex_box.set_flex_direction(FlexDirection::Column);
        container.append_child(convert_node_ref_to_ptr(flex_box));

        let block_box = as_ref(Node::new_ptr());
        flex_box.append_child(convert_node_ref_to_ptr(block_box));

        let first = as_ref(Node::new_ptr());
        first.set_height(DefLength::Points(Len::from_f32(10.)));
        block_box.append_child(convert_node_ref_to_ptr(first));

        let second = as_ref(Node::new_ptr());
        block_box.append_child(convert_node_ref_to_ptr(second));

        let item = as_ref(Node::new_ptr());
        item.set_height(DefLength::Points(Len::from_f32(10.)));
        second.append_child(convert_node_ref_to_ptr(item));

        let item = as_ref(Node::new_ptr());
        item.set_height(DefLength::Points(Len::from_f32(10.)));
        second.append_child(convert_node_ref_to_ptr(item));

        let inline = as_ref(Node::new_ptr());
        inline.set_display(Display::Inline);
        second.append_child(convert_node_ref_to_ptr(inline));

        let inline_child = as_ref(Node::new_ptr());
        inline_child.set_height(DefLength::Points(Len::from_f32(24.)));
        inline_child.set_width(DefLength::Points(Len::from_f32(34.)));
        inline.append_child(convert_node_ref_to_ptr(inline_child));

        root.layout(
            OptionSize::new(
                OptionNum::some(Len::from_f32(375.)),
                OptionNum::some(Len::from_f32(750.)),
            ),
            Size::new(Len::from_f32(0.), Len::from_f32(0.)),
        );
        println!(
            "{}",
            root.dump_to_html(
                DumpOptions {
                    recursive: true,
                    style: float_pigment_forest::DumpStyleMode::Mutation,
                    layout: true,
                },
                2
            )
        );

        assert_eq!(inline.layout_position().top, 20.);
        first.set_height(DefLength::Points(Len::from_f32(20.)));

        root.layout(
            OptionSize::new(
                OptionNum::some(Len::from_f32(375.)),
                OptionNum::some(Len::from_f32(750.)),
            ),
            Size::new(Len::from_f32(0.), Len::from_f32(0.)),
        );
        println!(
            "{}",
            root.dump_to_html(
                DumpOptions {
                    recursive: true,
                    style: float_pigment_forest::DumpStyleMode::Mutation,
                    layout: true,
                },
                2
            )
        );
        assert_eq!(inline.layout_position().top, 20.);
    }
}