azul-layout 0.0.13

Layout solver + font and image loader the Azul GUI framework
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
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
//! Native progress bar widget with customizable backgrounds, height, and
//! gradient styling. The main type is [`ProgressBar`], which is rendered
//! into a DOM via [`ProgressBar::dom()`].

use azul_core::dom::{Dom, IdOrClass, IdOrClass::Class, IdOrClassVec};
#[allow(clippy::wildcard_imports)] // widget/render module pulls in the css property/value types it builds with
use azul_css::{
    dynamic_selector::{CssPropertyWithConditions, CssPropertyWithConditionsVec},
    props::{
        basic::*,
        layout::*,
        property::{CssProperty, *},
        style::*,
    },
    *,
};
use azul_css::css::BoxOrStatic;

const STYLE_BACKGROUND_CONTENT_2688422633177340412_ITEMS: &[StyleBackgroundContent] =
    &[StyleBackgroundContent::LinearGradient(LinearGradient {
        direction: Direction::FromTo(DirectionCorners {
            dir_from: DirectionCorner::Top,
            dir_to: DirectionCorner::Bottom,
        }),
        extend_mode: ExtendMode::Clamp,
        stops: NormalizedLinearColorStopVec::from_const_slice(
            LINEAR_COLOR_STOP_12009347504665939_ITEMS,
        ),
    })];
const STYLE_BACKGROUND_CONTENT_14586281004485141058_ITEMS: &[StyleBackgroundContent] =
    &[StyleBackgroundContent::LinearGradient(LinearGradient {
        direction: Direction::FromTo(DirectionCorners {
            dir_from: DirectionCorner::Top,
            dir_to: DirectionCorner::Bottom,
        }),
        extend_mode: ExtendMode::Clamp,
        stops: NormalizedLinearColorStopVec::from_const_slice(
            LINEAR_COLOR_STOP_3104396762583413726_ITEMS,
        ),
    })];
const LINEAR_COLOR_STOP_12009347504665939_ITEMS: &[NormalizedLinearColorStop] = &[
    NormalizedLinearColorStop {
        offset: PercentageValue::const_new(0),
        color: ColorOrSystem::color(ColorU {
            r: 193,
            g: 255,
            b: 187,
            a: 255,
        }),
    },
    NormalizedLinearColorStop {
        offset: PercentageValue::const_new(10),
        color: ColorOrSystem::color(ColorU {
            r: 205,
            g: 255,
            b: 205,
            a: 255,
        }),
    },
    NormalizedLinearColorStop {
        offset: PercentageValue::const_new(15),
        color: ColorOrSystem::color(ColorU {
            r: 156,
            g: 238,
            b: 172,
            a: 255,
        }),
    },
    NormalizedLinearColorStop {
        offset: PercentageValue::const_new(20),
        color: ColorOrSystem::color(ColorU {
            r: 0,
            g: 211,
            b: 40,
            a: 255,
        }),
    },
    NormalizedLinearColorStop {
        offset: PercentageValue::const_new(30),
        color: ColorOrSystem::color(ColorU {
            r: 0,
            g: 211,
            b: 40,
            a: 255,
        }),
    },
    NormalizedLinearColorStop {
        offset: PercentageValue::const_new(70),
        color: ColorOrSystem::color(ColorU {
            r: 32,
            g: 219,
            b: 65,
            a: 255,
        }),
    },
    NormalizedLinearColorStop {
        offset: PercentageValue::const_new(100),
        color: ColorOrSystem::color(ColorU {
            r: 32,
            g: 219,
            b: 65,
            a: 255,
        }),
    },
];
const LINEAR_COLOR_STOP_3104396762583413726_ITEMS: &[NormalizedLinearColorStop] = &[
    NormalizedLinearColorStop {
        offset: PercentageValue::const_new(0),
        color: ColorOrSystem::color(ColorU {
            r: 243,
            g: 243,
            b: 243,
            a: 255,
        }),
    },
    NormalizedLinearColorStop {
        offset: PercentageValue::const_new(10),
        color: ColorOrSystem::color(ColorU {
            r: 252,
            g: 252,
            b: 252,
            a: 255,
        }),
    },
    NormalizedLinearColorStop {
        offset: PercentageValue::const_new(15),
        color: ColorOrSystem::color(ColorU {
            r: 218,
            g: 218,
            b: 218,
            a: 255,
        }),
    },
    NormalizedLinearColorStop {
        offset: PercentageValue::const_new(20),
        color: ColorOrSystem::color(ColorU {
            r: 201,
            g: 201,
            b: 201,
            a: 255,
        }),
    },
    NormalizedLinearColorStop {
        offset: PercentageValue::const_new(30),
        color: ColorOrSystem::color(ColorU {
            r: 218,
            g: 218,
            b: 218,
            a: 255,
        }),
    },
    NormalizedLinearColorStop {
        offset: PercentageValue::const_new(70),
        color: ColorOrSystem::color(ColorU {
            r: 203,
            g: 203,
            b: 203,
            a: 255,
        }),
    },
    NormalizedLinearColorStop {
        offset: PercentageValue::const_new(100),
        color: ColorOrSystem::color(ColorU {
            r: 203,
            g: 203,
            b: 203,
            a: 255,
        }),
    },
];

/// A native progress bar widget with customizable bar/container backgrounds and height.
#[derive(Debug, Clone)]
#[repr(C)]
pub struct ProgressBar {
    pub progressbar_state: ProgressBarState,
    pub height: PixelValue,
    pub bar_background: StyleBackgroundContentVec,
    pub container_background: StyleBackgroundContentVec,
}

/// Internal state for a [`ProgressBar`], tracking completion percentage.
#[derive(Copy, Debug, Clone)]
#[repr(C)]
pub struct ProgressBarState {
    pub percent_done: f32,
    pub display_percentage: bool,
}

impl ProgressBar {
    /// Creates a new progress bar with the given completion percentage (0.0 to 100.0).
    #[inline]
    #[must_use] pub const fn create(percent_done: f32) -> Self {
        Self {
            progressbar_state: ProgressBarState {
                percent_done,
                display_percentage: false,
            },
            height: PixelValue::const_px(15),
            bar_background: StyleBackgroundContentVec::from_const_slice(
                STYLE_BACKGROUND_CONTENT_2688422633177340412_ITEMS,
            ),
            container_background: StyleBackgroundContentVec::from_const_slice(
                STYLE_BACKGROUND_CONTENT_14586281004485141058_ITEMS,
            ),
        }
    }

    /// Replaces `self` with a default (0%) progress bar, returning the previous value.
    #[inline]
    #[must_use]
    pub const fn swap_with_default(&mut self) -> Self {
        let mut s = Self::create(0.0);
        core::mem::swap(&mut s, self);
        s
    }

    pub fn set_container_background(&mut self, background: StyleBackgroundContentVec) {
        self.container_background = background;
    }

    #[must_use] pub fn with_container_background(mut self, background: StyleBackgroundContentVec) -> Self {
        self.set_container_background(background);
        self
    }

    pub fn set_bar_background(&mut self, background: StyleBackgroundContentVec) {
        self.bar_background = background;
    }

    #[must_use] pub fn with_bar_background(mut self, background: StyleBackgroundContentVec) -> Self {
        self.set_bar_background(background);
        self
    }

    pub const fn set_height(&mut self, height: PixelValue) {
        self.height = height;
    }

    #[must_use] pub const fn with_height(mut self, height: PixelValue) -> Self {
        self.set_height(height);
        self
    }

    /// Renders this progress bar into a [`Dom`] tree consisting of a container div
    /// with two children: the filled bar and the remaining empty space.
    #[allow(clippy::too_many_lines)] // large but cohesive: single-purpose layout/render/parse routine (one branch per case)
    #[must_use] pub fn dom(self) -> Dom {
        use azul_core::dom::DomVec;

        // Use percentage widths for the progress bar and remaining space.
        // The container uses flex-direction: row, and we set explicit widths
        // on the children using CSS percentages.
        let percent_done = self.progressbar_state.percent_done.clamp(0.0, 100.0);

        Dom::create_div()
            .with_css_props(CssPropertyWithConditionsVec::from_vec(vec![
                // .__azul-native-progress-bar-container
                CssPropertyWithConditions::simple(CssProperty::Height(LayoutHeightValue::Exact(
                    LayoutHeight::Px(self.height),
                ))),
                CssPropertyWithConditions::simple(CssProperty::FlexDirection(
                    LayoutFlexDirectionValue::Exact(LayoutFlexDirection::Row),
                )),
                CssPropertyWithConditions::simple(CssProperty::BoxShadowBottom(
                    StyleBoxShadowValue::Exact(BoxOrStatic::heap(StyleBoxShadow {
                        offset_x: PixelValueNoPercent {
                            inner: PixelValue::const_px(0),
                        },
                        offset_y: PixelValueNoPercent {
                            inner: PixelValue::const_px(0),
                        },
                        color: ColorU {
                            r: 0,
                            g: 0,
                            b: 0,
                            a: 9,
                        },
                        blur_radius: PixelValueNoPercent {
                            inner: PixelValue::const_px(15),
                        },
                        spread_radius: PixelValueNoPercent {
                            inner: PixelValue::const_px(2),
                        },
                        clip_mode: BoxShadowClipMode::Inset,
                    })),
                )),
                CssPropertyWithConditions::simple(CssProperty::BoxShadowTop(
                    StyleBoxShadowValue::Exact(BoxOrStatic::heap(StyleBoxShadow {
                        offset_x: PixelValueNoPercent {
                            inner: PixelValue::const_px(0),
                        },
                        offset_y: PixelValueNoPercent {
                            inner: PixelValue::const_px(0),
                        },
                        color: ColorU {
                            r: 0,
                            g: 0,
                            b: 0,
                            a: 9,
                        },
                        blur_radius: PixelValueNoPercent {
                            inner: PixelValue::const_px(15),
                        },
                        spread_radius: PixelValueNoPercent {
                            inner: PixelValue::const_px(2),
                        },
                        clip_mode: BoxShadowClipMode::Inset,
                    })),
                )),
                CssPropertyWithConditions::simple(CssProperty::BoxShadowRight(
                    StyleBoxShadowValue::Exact(BoxOrStatic::heap(StyleBoxShadow {
                        offset_x: PixelValueNoPercent {
                            inner: PixelValue::const_px(0),
                        },
                        offset_y: PixelValueNoPercent {
                            inner: PixelValue::const_px(0),
                        },
                        color: ColorU {
                            r: 0,
                            g: 0,
                            b: 0,
                            a: 9,
                        },
                        blur_radius: PixelValueNoPercent {
                            inner: PixelValue::const_px(15),
                        },
                        spread_radius: PixelValueNoPercent {
                            inner: PixelValue::const_px(2),
                        },
                        clip_mode: BoxShadowClipMode::Inset,
                    })),
                )),
                CssPropertyWithConditions::simple(CssProperty::BoxShadowLeft(
                    StyleBoxShadowValue::Exact(BoxOrStatic::heap(StyleBoxShadow {
                        offset_x: PixelValueNoPercent {
                            inner: PixelValue::const_px(0),
                        },
                        offset_y: PixelValueNoPercent {
                            inner: PixelValue::const_px(0),
                        },
                        color: ColorU {
                            r: 0,
                            g: 0,
                            b: 0,
                            a: 9,
                        },
                        blur_radius: PixelValueNoPercent {
                            inner: PixelValue::const_px(15),
                        },
                        spread_radius: PixelValueNoPercent {
                            inner: PixelValue::const_px(2),
                        },
                        clip_mode: BoxShadowClipMode::Inset,
                    })),
                )),
                CssPropertyWithConditions::simple(CssProperty::BorderBottomRightRadius(
                    StyleBorderBottomRightRadiusValue::Exact(StyleBorderBottomRightRadius {
                        inner: PixelValue::const_px(3),
                    }),
                )),
                CssPropertyWithConditions::simple(CssProperty::BorderBottomLeftRadius(
                    StyleBorderBottomLeftRadiusValue::Exact(StyleBorderBottomLeftRadius {
                        inner: PixelValue::const_px(3),
                    }),
                )),
                CssPropertyWithConditions::simple(CssProperty::BorderTopRightRadius(
                    StyleBorderTopRightRadiusValue::Exact(StyleBorderTopRightRadius {
                        inner: PixelValue::const_px(3),
                    }),
                )),
                CssPropertyWithConditions::simple(CssProperty::BorderTopLeftRadius(
                    StyleBorderTopLeftRadiusValue::Exact(StyleBorderTopLeftRadius {
                        inner: PixelValue::const_px(3),
                    }),
                )),
                CssPropertyWithConditions::simple(CssProperty::BorderBottomWidth(
                    LayoutBorderBottomWidthValue::Exact(LayoutBorderBottomWidth {
                        inner: PixelValue::const_px(1),
                    }),
                )),
                CssPropertyWithConditions::simple(CssProperty::BorderLeftWidth(
                    LayoutBorderLeftWidthValue::Exact(LayoutBorderLeftWidth {
                        inner: PixelValue::const_px(1),
                    }),
                )),
                CssPropertyWithConditions::simple(CssProperty::BorderRightWidth(
                    LayoutBorderRightWidthValue::Exact(LayoutBorderRightWidth {
                        inner: PixelValue::const_px(1),
                    }),
                )),
                CssPropertyWithConditions::simple(CssProperty::BorderTopWidth(
                    LayoutBorderTopWidthValue::Exact(LayoutBorderTopWidth {
                        inner: PixelValue::const_px(1),
                    }),
                )),
                CssPropertyWithConditions::simple(CssProperty::BorderBottomStyle(
                    StyleBorderBottomStyleValue::Exact(StyleBorderBottomStyle {
                        inner: BorderStyle::Solid,
                    }),
                )),
                CssPropertyWithConditions::simple(CssProperty::BorderLeftStyle(
                    StyleBorderLeftStyleValue::Exact(StyleBorderLeftStyle {
                        inner: BorderStyle::Solid,
                    }),
                )),
                CssPropertyWithConditions::simple(CssProperty::BorderRightStyle(
                    StyleBorderRightStyleValue::Exact(StyleBorderRightStyle {
                        inner: BorderStyle::Solid,
                    }),
                )),
                CssPropertyWithConditions::simple(CssProperty::BorderTopStyle(
                    StyleBorderTopStyleValue::Exact(StyleBorderTopStyle {
                        inner: BorderStyle::Solid,
                    }),
                )),
                CssPropertyWithConditions::simple(CssProperty::BorderBottomColor(
                    StyleBorderBottomColorValue::Exact(StyleBorderBottomColor {
                        inner: ColorU {
                            r: 178,
                            g: 178,
                            b: 178,
                            a: 255,
                        },
                    }),
                )),
                CssPropertyWithConditions::simple(CssProperty::BorderLeftColor(
                    StyleBorderLeftColorValue::Exact(StyleBorderLeftColor {
                        inner: ColorU {
                            r: 178,
                            g: 178,
                            b: 178,
                            a: 255,
                        },
                    }),
                )),
                CssPropertyWithConditions::simple(CssProperty::BorderRightColor(
                    StyleBorderRightColorValue::Exact(StyleBorderRightColor {
                        inner: ColorU {
                            r: 178,
                            g: 178,
                            b: 178,
                            a: 255,
                        },
                    }),
                )),
                CssPropertyWithConditions::simple(CssProperty::BorderTopColor(
                    StyleBorderTopColorValue::Exact(StyleBorderTopColor {
                        inner: ColorU {
                            r: 178,
                            g: 178,
                            b: 178,
                            a: 255,
                        },
                    }),
                )),
                CssPropertyWithConditions::simple(CssProperty::BackgroundContent(
                    StyleBackgroundContentVecValue::Exact(self.container_background.clone()),
                )),
            ]))
            .with_ids_and_classes({
                const IDS_AND_CLASSES_10874511710181900075: &[IdOrClass] = &[Class(
                    AzString::from_const_str("__azul-native-progress-bar-container"),
                )];
                IdOrClassVec::from_const_slice(IDS_AND_CLASSES_10874511710181900075)
            })
            .with_children(DomVec::from_vec(vec![
                Dom::create_div()
                    .with_css_props(CssPropertyWithConditionsVec::from_vec(vec![
                        // .__azul-native-progress-bar-bar
                        // Use percentage width instead of flex-grow hack
                        CssPropertyWithConditions::simple(CssProperty::Width(
                            LayoutWidthValue::Exact(LayoutWidth::Px(
                                PixelValue::percent(percent_done),
                            )),
                        )),
                        CssPropertyWithConditions::simple(CssProperty::BoxShadowBottom(
                            StyleBoxShadowValue::Exact(BoxOrStatic::heap(StyleBoxShadow {
                                offset_x: PixelValueNoPercent {
                                    inner: PixelValue::const_px(0),
                                },
                                offset_y: PixelValueNoPercent {
                                    inner: PixelValue::const_px(0),
                                },
                                color: ColorU {
                                    r: 0,
                                    g: 51,
                                    b: 0,
                                    a: 51,
                                },
                                blur_radius: PixelValueNoPercent {
                                    inner: PixelValue::const_px(15),
                                },
                                spread_radius: PixelValueNoPercent {
                                    inner: PixelValue::const_px(12),
                                },
                                clip_mode: BoxShadowClipMode::Inset,
                            })),
                        )),
                        CssPropertyWithConditions::simple(CssProperty::BoxShadowTop(
                            StyleBoxShadowValue::Exact(BoxOrStatic::heap(StyleBoxShadow {
                                offset_x: PixelValueNoPercent {
                                    inner: PixelValue::const_px(0),
                                },
                                offset_y: PixelValueNoPercent {
                                    inner: PixelValue::const_px(0),
                                },
                                color: ColorU {
                                    r: 0,
                                    g: 51,
                                    b: 0,
                                    a: 51,
                                },
                                blur_radius: PixelValueNoPercent {
                                    inner: PixelValue::const_px(15),
                                },
                                spread_radius: PixelValueNoPercent {
                                    inner: PixelValue::const_px(12),
                                },
                                clip_mode: BoxShadowClipMode::Inset,
                            })),
                        )),
                        CssPropertyWithConditions::simple(CssProperty::BoxShadowRight(
                            StyleBoxShadowValue::Exact(BoxOrStatic::heap(StyleBoxShadow {
                                offset_x: PixelValueNoPercent {
                                    inner: PixelValue::const_px(0),
                                },
                                offset_y: PixelValueNoPercent {
                                    inner: PixelValue::const_px(0),
                                },
                                color: ColorU {
                                    r: 0,
                                    g: 51,
                                    b: 0,
                                    a: 51,
                                },
                                blur_radius: PixelValueNoPercent {
                                    inner: PixelValue::const_px(15),
                                },
                                spread_radius: PixelValueNoPercent {
                                    inner: PixelValue::const_px(12),
                                },
                                clip_mode: BoxShadowClipMode::Inset,
                            })),
                        )),
                        CssPropertyWithConditions::simple(CssProperty::BoxShadowLeft(
                            StyleBoxShadowValue::Exact(BoxOrStatic::heap(StyleBoxShadow {
                                offset_x: PixelValueNoPercent {
                                    inner: PixelValue::const_px(0),
                                },
                                offset_y: PixelValueNoPercent {
                                    inner: PixelValue::const_px(0),
                                },
                                color: ColorU {
                                    r: 0,
                                    g: 51,
                                    b: 0,
                                    a: 51,
                                },
                                blur_radius: PixelValueNoPercent {
                                    inner: PixelValue::const_px(15),
                                },
                                spread_radius: PixelValueNoPercent {
                                    inner: PixelValue::const_px(12),
                                },
                                clip_mode: BoxShadowClipMode::Inset,
                            })),
                        )),
                        CssPropertyWithConditions::simple(CssProperty::BorderBottomRightRadius(
                            StyleBorderBottomRightRadiusValue::Exact(
                                StyleBorderBottomRightRadius {
                                    inner: PixelValue::const_px(1),
                                },
                            ),
                        )),
                        CssPropertyWithConditions::simple(CssProperty::BorderBottomLeftRadius(
                            StyleBorderBottomLeftRadiusValue::Exact(StyleBorderBottomLeftRadius {
                                inner: PixelValue::const_px(1),
                            }),
                        )),
                        CssPropertyWithConditions::simple(CssProperty::BorderTopRightRadius(
                            StyleBorderTopRightRadiusValue::Exact(StyleBorderTopRightRadius {
                                inner: PixelValue::const_px(1),
                            }),
                        )),
                        CssPropertyWithConditions::simple(CssProperty::BorderTopLeftRadius(
                            StyleBorderTopLeftRadiusValue::Exact(StyleBorderTopLeftRadius {
                                inner: PixelValue::const_px(1),
                            }),
                        )),
                        CssPropertyWithConditions::simple(CssProperty::BackgroundContent(
                            StyleBackgroundContentVecValue::Exact(self.bar_background),
                        )),
                    ]))
                    .with_ids_and_classes({
                        const IDS_AND_CLASSES_16512648314570682783: &[IdOrClass] = &[Class(
                            AzString::from_const_str("__azul-native-progress-bar-bar"),
                        )];
                        IdOrClassVec::from_const_slice(IDS_AND_CLASSES_16512648314570682783)
                    }),
                Dom::create_div()
                    .with_css_props(CssPropertyWithConditionsVec::from_vec(vec![
                        // .__azul-native-progress-bar-remaining
                        // Use percentage width for the remaining space
                        CssPropertyWithConditions::simple(CssProperty::Width(
                            LayoutWidthValue::Exact(LayoutWidth::Px(
                                PixelValue::percent(100.0 - percent_done),
                            )),
                        )),
                    ]))
                    .with_ids_and_classes({
                        const IDS_AND_CLASSES_2492405364126620395: &[IdOrClass] = &[Class(
                            AzString::from_const_str("__azul-native-progress-bar-remaining"),
                        )];
                        IdOrClassVec::from_const_slice(IDS_AND_CLASSES_2492405364126620395)
                    }),
            ]))
    }
}

#[cfg(test)]
#[allow(
    clippy::float_cmp,
    clippy::cast_precision_loss,
    clippy::cast_possible_truncation,
    clippy::unreadable_literal,
    clippy::too_many_lines
)]
mod autotest_generated {
    use std::collections::HashSet;

    use azul_core::dom::NodeType;

    use super::*;

    // ------------------------------------------------------------------
    // Helpers
    // ------------------------------------------------------------------

    /// Every `f32` a caller can realistically hand to `ProgressBar::create`.
    /// The percentage is stored raw and only clamped inside `dom()`, where it is
    /// pushed through `PixelValue::percent` — which multiplies by 1000 and casts
    /// to `isize`. That cast saturates (NaN → 0, out of range → `isize::MIN/MAX`),
    /// so none of these may panic or wrap.
    ///
    /// `NAN` is deliberately absent: it is the one input that is unordered against
    /// the clamp bounds, so it gets its own test.
    const ADVERSARIAL_PERCENTS: [f32; 16] = [
        0.0,
        -0.0,
        1.0,
        50.0,
        100.0,
        -1.0,
        101.0,
        0.001,
        -0.001,
        f32::EPSILON,
        f32::MIN_POSITIVE,
        -f32::MIN_POSITIVE,
        f32::MAX,
        f32::MIN,
        f32::INFINITY,
        f32::NEG_INFINITY,
    ];

    /// Heights that stress the `f32 → isize` fixed-point encoding behind
    /// `PixelValue`: zero, both signed zeroes, the saturating extremes, NaN, and
    /// the relative metrics the widget is not supposed to reject.
    fn adversarial_heights() -> Vec<PixelValue> {
        vec![
            PixelValue::zero(),
            PixelValue::const_px(0),
            PixelValue::px(-0.0),
            PixelValue::px(-1.0),
            PixelValue::px(0.001),
            PixelValue::px(f32::MAX),
            PixelValue::px(f32::MIN),
            PixelValue::px(f32::INFINITY),
            PixelValue::px(f32::NEG_INFINITY),
            PixelValue::px(f32::NAN),
            PixelValue::percent(100.0),
            PixelValue::em(0.001),
            // The largest whole-pixel value `const_px` can scale by 1000 without
            // overflowing `isize` (one more would be a debug-build panic *inside
            // the argument*, not inside `set_height`).
            PixelValue::const_px(isize::MAX / 1000),
        ]
    }

    /// The raw fixed-point encoding of a length: `FloatValue` stores `value * 1000`
    /// as an `isize`, so this is what actually survives — comparing it avoids a
    /// second lossy float round-trip through `get()`.
    fn raw(pv: PixelValue) -> isize {
        pv.number.number()
    }

    /// The addresses `ProgressBar::create` hands out for the two static
    /// gradients — the reference every "is this still borrowed?" assertion below
    /// compares against.
    ///
    /// Deliberately NOT `STYLE_BACKGROUND_CONTENT_*_ITEMS.as_ptr()`. Those are
    /// `const` items, and every *use site* of a `const &[T]` gets its own
    /// promoted read-only allocation; two use sites share an address only if the
    /// optimizer merges them, which it does in an optimized build and does not
    /// in an unoptimized one. Comparing a `create()` pointer against the const
    /// was therefore an accidental green that held only because the suite had
    /// never been run on the dev profile. `create()` contains ONE use site of
    /// each const, so the address it returns is stable across calls — and that
    /// is exactly the property under test: a `create()` that copied the slice
    /// into a heap vec would hand out a fresh address every time.
    fn create_gradient_ptrs() -> (
        *const StyleBackgroundContent,
        *const StyleBackgroundContent,
    ) {
        let pb = ProgressBar::create(0.0);
        (pb.bar_background.as_ptr(), pb.container_background.as_ptr())
    }

    /// A heap-allocated background of `n` distinct solid colours. Heap-backed on
    /// purpose: it is the only case where the vec owns memory that can be
    /// double-freed or leaked.
    fn solid(n: usize) -> StyleBackgroundContentVec {
        StyleBackgroundContentVec::from_vec(
            (0..n)
                .map(|i| {
                    StyleBackgroundContent::Color(ColorU {
                        r: (i % 256) as u8,
                        g: 1,
                        b: 2,
                        a: 255,
                    })
                })
                .collect(),
        )
    }

    fn kids(dom: &Dom) -> &[Dom] {
        dom.children.as_ref()
    }

    /// The filled part (`.__azul-native-progress-bar-bar`).
    fn bar(dom: &Dom) -> &Dom {
        &kids(dom)[0]
    }

    /// The empty part (`.__azul-native-progress-bar-remaining`).
    fn remaining(dom: &Dom) -> &Dom {
        &kids(dom)[1]
    }

    /// The declared properties of a node's inline style, in declaration order.
    fn inline_props(dom: &Dom) -> Vec<CssProperty> {
        dom.root
            .style
            .iter_inline_properties()
            .map(|(p, _)| p.clone())
            .collect()
    }

    /// The CSS classes of a node, in declaration order.
    fn classes(dom: &Dom) -> Vec<String> {
        dom.root
            .get_ids_and_classes()
            .as_ref()
            .iter()
            .filter_map(|c| match c {
                IdOrClass::Class(s) => Some(s.as_str().to_string()),
                IdOrClass::Id(_) => None,
            })
            .collect()
    }

    fn width_of(dom: &Dom) -> Option<PixelValue> {
        dom.root
            .style
            .iter_inline_properties()
            .find_map(|(p, _)| match p {
                CssProperty::Width(v) => match v.get_property() {
                    Some(LayoutWidth::Px(pv)) => Some(*pv),
                    Some(other) => panic!("the progress bar must size in lengths, got {other:?}"),
                    None => None,
                },
                _ => None,
            })
    }

    fn height_of(dom: &Dom) -> Option<PixelValue> {
        dom.root
            .style
            .iter_inline_properties()
            .find_map(|(p, _)| match p {
                CssProperty::Height(v) => match v.get_property() {
                    Some(LayoutHeight::Px(pv)) => Some(*pv),
                    Some(other) => panic!("the progress bar must size in lengths, got {other:?}"),
                    None => None,
                },
                _ => None,
            })
    }

    /// The background layers a node declares, cloned out of the DOM.
    fn background_of(dom: &Dom) -> Option<Vec<StyleBackgroundContent>> {
        dom.root
            .style
            .iter_inline_properties()
            .find_map(|(p, _)| match p {
                CssProperty::BackgroundContent(v) => {
                    v.get_property().map(|b| b.as_ref().to_vec())
                }
                _ => None,
            })
    }

    /// The *address* of a node's background buffer — the only way to tell a move
    /// from a copy, and a copy from a use-after-free.
    fn background_ptr(dom: &Dom) -> Option<*const StyleBackgroundContent> {
        dom.root
            .style
            .iter_inline_properties()
            .find_map(|(p, _)| match p {
                CssProperty::BackgroundContent(v) => {
                    v.get_property().map(StyleBackgroundContentVec::as_ptr)
                }
                _ => None,
            })
    }

    /// Every absolute length a chrome property declares (box shadows excluded —
    /// they carry `PixelValueNoPercent`, which cannot express a relative unit).
    fn lengths_of(p: &CssProperty) -> Vec<PixelValue> {
        let one = |pv: Option<PixelValue>| pv.into_iter().collect::<Vec<_>>();
        match p {
            CssProperty::BorderBottomWidth(v) => one(v.get_property().map(|x| x.inner)),
            CssProperty::BorderLeftWidth(v) => one(v.get_property().map(|x| x.inner)),
            CssProperty::BorderRightWidth(v) => one(v.get_property().map(|x| x.inner)),
            CssProperty::BorderTopWidth(v) => one(v.get_property().map(|x| x.inner)),
            CssProperty::BorderBottomRightRadius(v) => one(v.get_property().map(|x| x.inner)),
            CssProperty::BorderBottomLeftRadius(v) => one(v.get_property().map(|x| x.inner)),
            CssProperty::BorderTopRightRadius(v) => one(v.get_property().map(|x| x.inner)),
            CssProperty::BorderTopLeftRadius(v) => one(v.get_property().map(|x| x.inner)),
            _ => Vec::new(),
        }
    }

    // ------------------------------------------------------------------
    // ProgressBar::create
    // ------------------------------------------------------------------

    #[test]
    fn create_stores_the_percentage_bit_for_bit_and_never_normalises_it() {
        // `create` is documented as taking 0.0..=100.0 but performs no validation:
        // whatever comes in has to come back out untouched, sign of zero included.
        for p in ADVERSARIAL_PERCENTS {
            let pb = ProgressBar::create(p);
            assert_eq!(
                pb.progressbar_state.percent_done.to_bits(),
                p.to_bits(),
                "create() rewrote the percentage {p}",
            );
            assert!(
                !pb.progressbar_state.display_percentage,
                "a fresh progress bar must not opt into the percentage label",
            );
        }

        // NaN cannot be compared, only inspected.
        let nan = ProgressBar::create(f32::NAN);
        assert!(
            nan.progressbar_state.percent_done.is_nan(),
            "create() silently replaced a NaN percentage",
        );
    }

    #[test]
    fn create_defaults_to_a_15px_height() {
        let pb = ProgressBar::create(50.0);
        assert_eq!(pb.height.metric, SizeMetric::Px, "the default height must be absolute");
        assert_eq!(raw(pb.height), 15_000, "the default height is 15px in 1/1000 units");
    }

    #[test]
    fn create_borrows_the_static_gradients_instead_of_allocating_them() {
        let pb = ProgressBar::create(50.0);
        let (bar_ptr, container_ptr) = create_gradient_ptrs();

        // Pointer identity, not just content equality: a `create()` that copied the
        // static slice into a heap vec would allocate on every frame, and one that
        // kept the static pointer but claimed ownership of it would free `&'static`
        // memory on drop.
        assert_eq!(
            pb.bar_background.as_ptr(),
            bar_ptr,
            "the bar gradient stopped being shared with the static slice",
        );
        assert_eq!(
            pb.container_background.as_ptr(),
            container_ptr,
            "the container gradient stopped being shared with the static slice",
        );
        // Content still pinned to the declared constants, so "shared" cannot
        // degrade into "shared with something else".
        assert_eq!(
            pb.bar_background.as_ref(),
            STYLE_BACKGROUND_CONTENT_2688422633177340412_ITEMS,
        );
        assert_eq!(
            pb.container_background.as_ref(),
            STYLE_BACKGROUND_CONTENT_14586281004485141058_ITEMS,
        );
        assert_eq!(pb.bar_background.len(), 1);
        assert_eq!(pb.container_background.len(), 1);
        assert_eq!(
            pb.bar_background.capacity(),
            pb.bar_background.len(),
            "a borrowed buffer must report capacity == len, or the free path over-reads",
        );

        // 10_000 bars built and dropped: if the destructor of a static-backed vec
        // were ever flipped to the owning one, this frees the same `&'static`
        // allocation 10_000 times.
        for i in 0..10_000 {
            let pb = ProgressBar::create(i as f32);
            assert_eq!(pb.bar_background.len(), 1);
            assert_eq!(pb.bar_background.as_ptr(), bar_ptr);
        }
    }

    #[test]
    fn create_backgrounds_are_the_declared_gradients_with_sorted_stops() {
        let pb = ProgressBar::create(0.0);
        assert_eq!(
            pb.bar_background.as_ref(),
            STYLE_BACKGROUND_CONTENT_2688422633177340412_ITEMS,
        );
        assert_eq!(
            pb.container_background.as_ref(),
            STYLE_BACKGROUND_CONTENT_14586281004485141058_ITEMS,
        );

        for bg in [&pb.bar_background, &pb.container_background] {
            match &bg.as_ref()[0] {
                StyleBackgroundContent::LinearGradient(g) => {
                    let stops = g.stops.as_ref();
                    assert_eq!(stops.len(), 7, "a gradient lost or gained a colour stop");

                    // Unsorted or out-of-range stops make the gradient renderer's
                    // interpolation run backwards over a segment.
                    let mut prev = f32::NEG_INFINITY;
                    for s in stops {
                        let offset = s.offset.normalized() * 100.0;
                        assert!(
                            (0.0..=100.0).contains(&offset),
                            "gradient stop outside 0%..100%: {offset}",
                        );
                        assert!(
                            offset >= prev,
                            "gradient stops are not sorted: {offset} follows {prev}",
                        );
                        prev = offset;
                    }
                }
                other => panic!("the progress bar gradients degraded to {other:?}"),
            }
        }
    }

    #[test]
    fn create_is_usable_in_const_context() {
        // `create` is `const fn`; a caller may therefore build a bar as a `const`
        // item. That only const-evaluates while the backgrounds stay
        // `from_const_slice` (a heap allocation would not be const-evaluable).
        const CONST_BAR: ProgressBar = ProgressBar::create(12.5);

        assert_eq!(CONST_BAR.progressbar_state.percent_done, 12.5);
        assert_eq!(raw(CONST_BAR.height), 15_000);
        assert_eq!(CONST_BAR.bar_background.len(), 1);
    }

    // ------------------------------------------------------------------
    // ProgressBar::swap_with_default
    // ------------------------------------------------------------------

    #[test]
    fn swap_with_default_returns_the_previous_bar_and_installs_a_pristine_one() {
        for p in ADVERSARIAL_PERCENTS {
            let mut pb = ProgressBar::create(p).with_height(PixelValue::const_px(99));
            let prev = pb.swap_with_default();

            assert_eq!(
                prev.progressbar_state.percent_done.to_bits(),
                p.to_bits(),
                "the returned bar is not the one that was there ({p})",
            );
            assert_eq!(raw(prev.height), 99_000, "the returned bar lost its height");

            assert_eq!(
                pb.progressbar_state.percent_done.to_bits(),
                0_u32,
                "the replacement must be +0.0 — a -0.0 would encode with the sign bit set",
            );
            assert_eq!(raw(pb.height), 15_000, "the replacement must use the default height");
            assert_eq!(
                pb.bar_background.as_ptr(),
                create_gradient_ptrs().0,
                "the replacement must borrow the static gradient again",
            );
        }
    }

    #[test]
    fn swap_with_default_keeps_a_nan_percentage_and_moves_owned_memory_out() {
        let owned = solid(4);
        let ptr = owned.as_ptr();
        let mut pb = ProgressBar::create(f32::NAN).with_bar_background(owned);

        let prev = pb.swap_with_default();

        assert!(
            prev.progressbar_state.percent_done.is_nan(),
            "a NaN percentage did not survive the swap",
        );
        assert_eq!(
            prev.bar_background.as_ptr(),
            ptr,
            "the heap buffer was copied instead of moved out",
        );
        assert_eq!(prev.bar_background.len(), 4);

        // Dropping the previous value frees that heap buffer. If `swap_with_default`
        // had left `self` pointing at it too, everything below would be a
        // use-after-free.
        drop(prev);
        assert_eq!(
            pb.bar_background.as_ref(),
            STYLE_BACKGROUND_CONTENT_2688422633177340412_ITEMS,
            "the swapped-in bar aliased the memory that was just freed",
        );
        assert_eq!(pb.progressbar_state.percent_done, 0.0);
    }

    #[test]
    fn repeated_swaps_never_alias_or_leak_the_backgrounds() {
        let mut pb = ProgressBar::create(1.0);
        let (bar_ptr, _) = create_gradient_ptrs();
        for i in 0..1_000_usize {
            let want = i % 8 + 1;
            pb.set_bar_background(solid(want));
            let prev = pb.swap_with_default();

            assert_eq!(prev.bar_background.len(), want, "round {i} handed back the wrong buffer");
            assert_eq!(pb.progressbar_state.percent_done, 0.0);
            assert_eq!(pb.bar_background.as_ptr(), bar_ptr);
        }
    }

    // ------------------------------------------------------------------
    // set_/with_ background
    // ------------------------------------------------------------------

    #[test]
    fn each_background_setter_touches_exactly_one_field() {
        let mut pb = ProgressBar::create(50.0);
        let bar_ptr = pb.bar_background.as_ptr();
        pb.set_container_background(solid(3));
        assert_eq!(pb.container_background.len(), 3);
        assert_eq!(
            pb.bar_background.as_ptr(),
            bar_ptr,
            "set_container_background clobbered the bar background",
        );

        let mut pb = ProgressBar::create(50.0);
        let container_ptr = pb.container_background.as_ptr();
        pb.set_bar_background(solid(5));
        assert_eq!(pb.bar_background.len(), 5);
        assert_eq!(
            pb.container_background.as_ptr(),
            container_ptr,
            "set_bar_background clobbered the container background",
        );
        assert_eq!(pb.progressbar_state.percent_done, 50.0);
        assert_eq!(raw(pb.height), 15_000);
    }

    #[test]
    fn the_builder_forms_are_exactly_their_setters() {
        let a = ProgressBar::create(7.5)
            .with_bar_background(solid(3))
            .with_container_background(solid(2))
            .with_height(PixelValue::px(-4.5));

        let mut b = ProgressBar::create(7.5);
        b.set_bar_background(solid(3));
        b.set_container_background(solid(2));
        b.set_height(PixelValue::px(-4.5));

        assert_eq!(a.bar_background.as_ref(), b.bar_background.as_ref());
        assert_eq!(a.container_background.as_ref(), b.container_background.as_ref());
        assert_eq!(a.height, b.height);
        assert_eq!(
            a.progressbar_state.percent_done,
            b.progressbar_state.percent_done,
        );
        assert_eq!(
            a.progressbar_state.display_percentage,
            b.progressbar_state.display_percentage,
        );
    }

    #[test]
    fn an_empty_background_stays_an_empty_declaration() {
        let pb = ProgressBar::create(0.0)
            .with_bar_background(StyleBackgroundContentVec::from_vec(Vec::new()))
            .with_container_background(StyleBackgroundContentVec::new());

        assert!(pb.bar_background.is_empty());
        assert_eq!(pb.bar_background.len(), 0);
        assert!(pb.container_background.is_empty());

        let dom = pb.dom();
        assert_eq!(
            background_of(bar(&dom)),
            Some(Vec::new()),
            "an empty background must reach the DOM as an empty layer list, not vanish",
        );
        assert_eq!(background_of(&dom), Some(Vec::new()));
    }

    #[test]
    fn a_background_with_spare_capacity_keeps_its_allocation_intact() {
        // The free path rebuilds a `Vec` from (ptr, len, cap); a `cap` that drifted
        // to `len` frees the wrong layout.
        let mut v = Vec::with_capacity(64);
        v.push(StyleBackgroundContent::Color(ColorU {
            r: 1,
            g: 2,
            b: 3,
            a: 4,
        }));
        let bg = StyleBackgroundContentVec::from_vec(v);
        assert_eq!(bg.len(), 1);
        assert!(bg.capacity() >= 64, "from_vec lost the spare capacity: {}", bg.capacity());

        let pb = ProgressBar::create(0.0).with_container_background(bg);
        assert_eq!(pb.container_background.len(), 1);
        assert!(
            pb.container_background.capacity() >= 64,
            "the setter rewrote the buffer's capacity",
        );
    }

    #[test]
    fn a_very_large_background_is_neither_truncated_nor_copied() {
        let big = solid(10_000);
        let ptr = big.as_ptr();
        let pb = ProgressBar::create(50.0).with_bar_background(big);
        assert_eq!(pb.bar_background.len(), 10_000);
        assert_eq!(pb.bar_background.as_ptr(), ptr, "the setter deep-copied a 10k-layer background");

        let dom = pb.dom();
        assert_eq!(
            background_of(bar(&dom)).map(|v| v.len()),
            Some(10_000),
            "the background was truncated on the way into the DOM",
        );
    }

    #[test]
    fn overwriting_a_background_releases_the_previous_one() {
        // 500 replacements of an owned buffer: a setter that forgot to drop the old
        // value leaks, and one that dropped it twice aborts.
        let n = 500_usize;
        let mut pb = ProgressBar::create(0.0);
        for i in 1..=n {
            pb.set_bar_background(solid(i % 16 + 1));
            pb.set_container_background(solid(i % 4 + 1));
        }
        // The surviving background is whatever the LAST iteration installed, so the
        // expected length is derived from `n` rather than hard-coded.
        assert_eq!(pb.bar_background.len(), n % 16 + 1);
        assert_eq!(pb.container_background.len(), n % 4 + 1);
    }

    #[test]
    fn cloning_deep_copies_owned_backgrounds_but_shares_static_ones() {
        let pb = ProgressBar::create(3.0).with_bar_background(solid(4));
        let copy = pb.clone();

        assert_ne!(
            copy.bar_background.as_ptr(),
            pb.bar_background.as_ptr(),
            "Clone shared an owned heap buffer — dropping both would double-free it",
        );
        assert_eq!(
            copy.container_background.as_ptr(),
            pb.container_background.as_ptr(),
            "the static gradient is never freed, so the clone should keep sharing it",
        );

        drop(pb);
        assert_eq!(copy.bar_background.len(), 4);
        assert_eq!(
            copy.bar_background.as_ref()[3],
            StyleBackgroundContent::Color(ColorU {
                r: 3,
                g: 1,
                b: 2,
                a: 255,
            }),
            "the clone read back garbage after the original was dropped",
        );
    }

    // ------------------------------------------------------------------
    // set_height / with_height
    // ------------------------------------------------------------------

    #[test]
    fn set_height_stores_every_pixel_value_verbatim() {
        for h in adversarial_heights() {
            let mut pb = ProgressBar::create(0.0);
            pb.set_height(h);
            assert_eq!(pb.height.metric, h.metric, "set_height changed the unit of {h:?}");
            assert_eq!(raw(pb.height), raw(h), "set_height re-encoded {h:?}");
            // and nothing else moved
            assert_eq!(pb.progressbar_state.percent_done, 0.0);
            assert_eq!(pb.bar_background.len(), 1);
        }
    }

    #[test]
    fn an_out_of_range_height_saturates_instead_of_wrapping() {
        let mut pb = ProgressBar::create(0.0);

        // `FloatValue::new` computes `value * 1000.0` in `f32` (which overflows to
        // an infinity) and then casts to `isize` — a saturating cast, so the result
        // is a bound, never a wrapped negative.
        pb.set_height(PixelValue::px(f32::MAX));
        assert_eq!(raw(pb.height), isize::MAX, "an overflowing height wrapped instead of saturating");
        assert!(pb.height.number.get().is_finite(), "the saturated height decoded to a non-finite f32");

        pb.set_height(PixelValue::px(f32::INFINITY));
        assert_eq!(raw(pb.height), isize::MAX);

        pb.set_height(PixelValue::px(f32::MIN));
        assert_eq!(raw(pb.height), isize::MIN);

        pb.set_height(PixelValue::px(f32::NEG_INFINITY));
        assert_eq!(raw(pb.height), isize::MIN);

        pb.set_height(PixelValue::px(f32::NAN));
        assert_eq!(raw(pb.height), 0, "a NaN height must land on 0, not on an arbitrary integer");

        pb.set_height(PixelValue::px(-0.0));
        assert_eq!(raw(pb.height), 0, "-0.0 must encode to the same 0 as +0.0");

        // Below the 1/1000 resolution everything truncates to zero, deterministically.
        pb.set_height(PixelValue::px(0.0004));
        assert_eq!(raw(pb.height), 0);
        pb.set_height(PixelValue::px(f32::MIN_POSITIVE));
        assert_eq!(raw(pb.height), 0);
    }

    #[test]
    fn with_height_is_set_height_and_leaves_the_rest_alone() {
        for h in adversarial_heights() {
            let a = ProgressBar::create(42.0).with_height(h);
            let mut b = ProgressBar::create(42.0);
            b.set_height(h);

            assert_eq!(a.height, b.height, "with_height disagreed with set_height for {h:?}");
            assert_eq!(raw(a.height), raw(h));
            assert_eq!(a.progressbar_state.percent_done, 42.0);
            assert_eq!(
                a.bar_background.as_ptr(),
                b.bar_background.as_ptr(),
                "with_height reallocated the background",
            );
        }
    }

    // ------------------------------------------------------------------
    // ProgressBar::dom
    // ------------------------------------------------------------------

    #[test]
    fn dom_is_a_container_div_with_exactly_two_leaf_children() {
        let dom = ProgressBar::create(50.0).dom();

        assert!(matches!(dom.root.get_node_type(), NodeType::Div));
        assert_eq!(kids(&dom).len(), 2, "the progress bar must render bar + remaining");
        assert!(kids(bar(&dom)).is_empty(), "the bar must stay a leaf");
        assert!(kids(remaining(&dom)).is_empty(), "the remaining space must stay a leaf");

        // A cached child count that is too small makes `convert_dom_into_compact_dom`
        // under-allocate its arenas and panic on out-of-bounds writes.
        assert_eq!(dom.estimated_total_children, 2);

        assert_eq!(classes(&dom), vec!["__azul-native-progress-bar-container".to_string()]);
        assert_eq!(classes(bar(&dom)), vec!["__azul-native-progress-bar-bar".to_string()]);
        assert_eq!(
            classes(remaining(&dom)),
            vec!["__azul-native-progress-bar-remaining".to_string()],
        );
    }

    #[test]
    fn dom_clamps_every_out_of_range_percentage_into_zero_to_one_hundred() {
        // (input, bar width, remaining width) — widths in 1/1000 of a percent.
        const CASES: [(f32, isize, isize); 12] = [
            (0.0, 0, 100_000),
            (-0.0, 0, 100_000),
            (50.0, 50_000, 50_000),
            (100.0, 100_000, 0),
            (-1.0, 0, 100_000),
            (101.0, 100_000, 0),
            (-1e30, 0, 100_000),
            (1e30, 100_000, 0),
            (f32::MAX, 100_000, 0),
            (f32::MIN, 0, 100_000),
            (f32::INFINITY, 100_000, 0),
            (f32::NEG_INFINITY, 0, 100_000),
        ];

        for (input, bar_width, remaining_width) in CASES {
            let dom = ProgressBar::create(input).dom();
            let b = width_of(bar(&dom)).expect("the bar must declare a width");
            let r = width_of(remaining(&dom)).expect("the remaining space must declare a width");

            assert_eq!(b.metric, SizeMetric::Percent, "the bar must size in %, not {:?}", b.metric);
            assert_eq!(r.metric, SizeMetric::Percent, "the gap must size in %, not {:?}", r.metric);
            assert_eq!(raw(b), bar_width, "bar width for input {input}");
            assert_eq!(raw(r), remaining_width, "remaining width for input {input}");
            assert!(raw(b) >= 0 && raw(r) >= 0, "a negative width escaped for input {input}");
        }
    }

    #[test]
    fn dom_collapses_a_nan_percentage_to_two_empty_children() {
        // `f32::clamp` propagates NaN rather than clamping it, and the `f32 -> isize`
        // cast inside `FloatValue::new` then turns it into 0. The documented result:
        // BOTH children get 0% — the bar renders as an empty container instead of
        // falling back to 0%/100%. It does not panic, and it is deterministic.
        let dom = ProgressBar::create(f32::NAN).dom();
        let b = width_of(bar(&dom)).expect("the bar must declare a width");
        let r = width_of(remaining(&dom)).expect("the remaining space must declare a width");

        assert_eq!(raw(b), 0);
        assert_eq!(raw(r), 0);
        assert_eq!(b.metric, SizeMetric::Percent);
        assert_eq!(r.metric, SizeMetric::Percent);
        assert_eq!(kids(&dom).len(), 2, "a NaN percentage must not change the tree shape");
    }

    #[test]
    fn dom_splits_the_container_exactly_for_whole_percentages() {
        for i in 0..=100_isize {
            let dom = ProgressBar::create(i as f32).dom();
            let b = raw(width_of(bar(&dom)).unwrap());
            let r = raw(width_of(remaining(&dom)).unwrap());

            assert_eq!(b, i * 1000, "the bar is not {i}% wide");
            assert_eq!(
                b + r,
                100_000,
                "the two halves do not add up to the container at {i}%",
            );
        }
    }

    #[test]
    fn dom_loses_at_most_the_encoding_truncation_for_fractional_percentages() {
        // Each side is truncated to 1/1000 of a percent independently, so the pair
        // may under-fill by two ticks — but never overflow the container, and never
        // go negative.
        for p in [
            0.0005_f32,
            0.5,
            1.0 / 3.0,
            33.333,
            66.667,
            99.999,
            99.9999,
            f32::EPSILON,
            f32::MIN_POSITIVE,
        ] {
            let dom = ProgressBar::create(p).dom();
            let b = raw(width_of(bar(&dom)).unwrap());
            let r = raw(width_of(remaining(&dom)).unwrap());

            assert!(
                (0..=100_000).contains(&b) && (0..=100_000).contains(&r),
                "a width left 0%..100% for {p}: {b} / {r}",
            );
            assert!(
                (b + r - 100_000).abs() <= 10,
                "the two halves drifted apart for {p}: {b} + {r}",
            );
        }
    }

    #[test]
    fn dom_routes_each_background_to_its_own_node() {
        let bar_bg = solid(3);
        let container_bg = solid(5);
        let bar_ptr = bar_bg.as_ptr();
        let container_ptr = container_bg.as_ptr();

        let dom = ProgressBar::create(25.0)
            .with_bar_background(bar_bg)
            .with_container_background(container_bg)
            .dom();

        assert_eq!(
            background_of(&dom).map(|v| v.len()),
            Some(5),
            "the container lost (or swapped) its background",
        );
        assert_eq!(
            background_of(bar(&dom)).map(|v| v.len()),
            Some(3),
            "the bar lost (or swapped) its background",
        );
        assert_eq!(
            background_of(remaining(&dom)),
            None,
            "the remaining space must not paint anything",
        );

        // The bar background is *moved* into the DOM — same allocation, no copy.
        assert_eq!(
            background_ptr(bar(&dom)),
            Some(bar_ptr),
            "the bar background was copied instead of moved",
        );
        // The container background is cloned, because `self` — and with it the
        // original buffer — is dropped at the end of `dom()`. Handing the DOM the
        // same pointer would be a use-after-free.
        assert_ne!(
            background_ptr(&dom),
            Some(container_ptr),
            "the DOM kept a pointer into a buffer that `dom()` then freed",
        );
    }

    #[test]
    fn dom_forwards_any_height_to_the_container_and_to_nobody_else() {
        for h in adversarial_heights() {
            let dom = ProgressBar::create(50.0).with_height(h).dom();
            let got = height_of(&dom).expect("the container must declare a height");

            assert_eq!(got.metric, h.metric, "the height unit changed on the way into the DOM");
            assert_eq!(raw(got), raw(h), "the height was re-encoded on the way into the DOM");
            assert_eq!(height_of(bar(&dom)), None, "the bar must not declare its own height");
            assert_eq!(
                height_of(remaining(&dom)),
                None,
                "the remaining space must not declare its own height",
            );
            assert_eq!(width_of(&dom), None, "the container must not declare a width");
        }
    }

    #[test]
    fn dom_declares_the_expected_style_blocks_and_no_property_twice() {
        let dom = ProgressBar::create(50.0).with_bar_background(solid(1)).dom();

        assert_eq!(inline_props(&dom).len(), 23, "the container style block drifted");
        assert_eq!(inline_props(bar(&dom)).len(), 10, "the bar style block drifted");

        let props = inline_props(remaining(&dom));
        assert_eq!(props.len(), 1, "the remaining space grew a style block: {props:?}");
        assert!(
            matches!(&props[0], CssProperty::Width(_)),
            "the remaining space must only declare its width",
        );

        // A property declared twice means one of the two is silently dead, and which
        // one wins depends on cascade order.
        for node in [&dom, bar(&dom), remaining(&dom)] {
            let mut seen = HashSet::new();
            for p in inline_props(node) {
                assert!(
                    seen.insert(core::mem::discriminant(&p)),
                    "duplicate declaration of {p:?}",
                );
            }
        }
    }

    #[test]
    fn dom_chrome_lengths_are_all_absolute_pixels() {
        // Only the two child widths are relative. A border or radius that slipped
        // into `em`/`%` would resolve against the parent font or box and either
        // vanish or blow up.
        let dom = ProgressBar::create(50.0).dom();
        for node in [&dom, bar(&dom), remaining(&dom)] {
            for p in inline_props(node) {
                for length in lengths_of(&p) {
                    assert_eq!(
                        length.metric,
                        SizeMetric::Px,
                        "{p:?} declares a relative length: {length:?}",
                    );
                }
            }
        }
    }

    #[test]
    fn dom_ignores_display_percentage() {
        // The field is public and settable, but nothing in `dom()` reads it: the
        // rendered tree has to be byte-identical either way.
        let mut with_label = ProgressBar::create(40.0);
        with_label.progressbar_state.display_percentage = true;
        let without_label = ProgressBar::create(40.0);

        assert_eq!(
            with_label.dom(),
            without_label.dom(),
            "display_percentage started changing the tree",
        );
    }

    #[test]
    fn dom_is_deterministic_for_equal_inputs() {
        let a = ProgressBar::create(37.5)
            .with_bar_background(solid(2))
            .with_height(PixelValue::px(7.25))
            .dom();
        let b = ProgressBar::create(37.5)
            .with_bar_background(solid(2))
            .with_height(PixelValue::px(7.25))
            .dom();

        assert_eq!(a, b, "two identically-built progress bars rendered differently");
    }

    #[test]
    fn dom_survives_every_extreme_percentage_and_background_size() {
        for p in ADVERSARIAL_PERCENTS.into_iter().chain([f32::NAN]) {
            for layers in [0_usize, 1, 64] {
                let dom = ProgressBar::create(p)
                    .with_bar_background(solid(layers))
                    .with_container_background(solid(layers))
                    .with_height(PixelValue::px(f32::MAX))
                    .dom();

                assert_eq!(kids(&dom).len(), 2, "shape changed for {p} / {layers} layers");
                assert_eq!(dom.estimated_total_children, 2);
                assert_eq!(background_of(bar(&dom)).map(|v| v.len()), Some(layers));
                assert_eq!(background_of(&dom).map(|v| v.len()), Some(layers));

                let b = width_of(bar(&dom)).expect("the bar must declare a width");
                assert_eq!(b.metric, SizeMetric::Percent);
                assert!((0..=100_000).contains(&raw(b)), "width out of range for {p}");
            }
        }
    }
}