skathi-gpui 0.5.2

gpui, Zed's GPU-accelerated UI framework, as Skathi renders with it
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
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
use std::{
    hash::{Hash, Hasher},
    iter, mem,
    ops::Range,
    sync::Arc,
};

use crate::{
    AbsoluteLength, App, Background, BackgroundTag, BorderStyle, Bounds, ContentMask, CornerShape,
    CornerShapes, Corners,
    CornersRefinement, CursorStyle, DefiniteLength, DevicePixels, Edges, EdgesRefinement,
    EffectCrossFade, EffectSpec, Font,
    FontFallbacks, FontFeatures, FontStyle, FontWeight, GridLocation, Hsla, Length, Pixels, Point,
    PointRefinement, Rgba, SharedString, Size, SizeRefinement, Styled, TextRun, Window, black, phi,
    TransformSpec, TransformationMatrix, VnClipOutline, VnFillRule, VnPathVerb, point, px, quad,
    rems, size,
};
use collections::HashSet;
use refineable::Refineable;
#[cfg(feature = "schemars")]
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

/// Use this struct for interfacing with the 'debug_below' styling from your own elements.
/// If a parent element has this style set on it, then this struct will be set as a global in
/// GPUI.
#[cfg(debug_assertions)]
pub struct DebugBelow;

#[cfg(debug_assertions)]
impl crate::Global for DebugBelow {}

/// How to fit the image into the bounds of the element.
pub enum ObjectFit {
    /// The image will be stretched to fill the bounds of the element.
    Fill,
    /// The image will be scaled to fit within the bounds of the element.
    Contain,
    /// The image will be scaled to cover the bounds of the element.
    Cover,
    /// The image will be scaled down to fit within the bounds of the element.
    ScaleDown,
    /// The image will maintain its original size.
    None,
}

impl ObjectFit {
    /// Get the bounds of the image within the given bounds.
    pub fn get_bounds(
        &self,
        bounds: Bounds<Pixels>,
        image_size: Size<DevicePixels>,
    ) -> Bounds<Pixels> {
        let fitted = self.get_bounds_at(bounds, image_size, ObjectPosition::default());
        match self {
            ObjectFit::None => Bounds {
                origin: bounds.origin,
                size: fitted.size,
            },
            _ => fitted,
        }
    }

    /// The bounds of the image within `bounds`, placed in the free space by `position`
    /// (CSS `object-fit` with `object-position`).
    pub fn get_bounds_at(
        &self,
        bounds: Bounds<Pixels>,
        image_size: Size<DevicePixels>,
        position: ObjectPosition,
    ) -> Bounds<Pixels> {
        let image_size = image_size.map(|dimension| Pixels::from(u32::from(dimension)));
        let image_ratio = image_size.width / image_size.height;
        let bounds_ratio = bounds.size.width / bounds.size.height;
        let contain = || {
            if bounds_ratio > image_ratio {
                size(
                    image_size.width * (bounds.size.height / image_size.height),
                    bounds.size.height,
                )
            } else {
                size(
                    bounds.size.width,
                    image_size.height * (bounds.size.width / image_size.width),
                )
            }
        };

        let new_size = match self {
            ObjectFit::Fill => bounds.size,
            ObjectFit::Contain => contain(),
            ObjectFit::ScaleDown => {
                if image_size.width > bounds.size.width || image_size.height > bounds.size.height {
                    contain()
                } else {
                    image_size
                }
            }
            ObjectFit::Cover => {
                if bounds_ratio > image_ratio {
                    size(
                        bounds.size.width,
                        image_size.height * (bounds.size.width / image_size.width),
                    )
                } else {
                    size(
                        image_size.width * (bounds.size.height / image_size.height),
                        bounds.size.height,
                    )
                }
            }
            ObjectFit::None => image_size,
        };
        Bounds {
            origin: point(
                bounds.origin.x
                    + (bounds.size.width - new_size.width) * position.x_fraction
                    + position.x_offset,
                bounds.origin.y
                    + (bounds.size.height - new_size.height) * position.y_fraction
                    + position.y_offset,
            ),
            size: new_size,
        }
    }
}

/// CSS `object-position`: per axis, a fraction of the free space (box minus content, which may be
/// negative) plus an offset. The default is centred.
#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
pub struct ObjectPosition {
    /// Fraction of the horizontal free space.
    pub x_fraction: f32,
    /// Added to the horizontal placement.
    pub x_offset: Pixels,
    /// Fraction of the vertical free space.
    pub y_fraction: f32,
    /// Added to the vertical placement.
    pub y_offset: Pixels,
}

impl Default for ObjectPosition {
    fn default() -> Self {
        Self {
            x_fraction: 0.5,
            x_offset: Pixels::ZERO,
            y_fraction: 0.5,
            y_offset: Pixels::ZERO,
        }
    }
}

/// The minimum size of a column or row in a grid layout
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Default, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
pub enum GridTemplateMinSize {
    /// The column or row size may be 0
    #[default]
    Zero,
    /// The column or row size can be determined by the min content
    MinContent,
    /// The column or row size can be determined by the max content
    MaxContent,
}

/// A simplified representation of the grid-template-* value
#[derive(
    Copy, Clone, Refineable, PartialEq, Eq, PartialOrd, Ord, Debug, Default, Serialize, Deserialize,
)]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
pub struct GridTemplate {
    /// How this template directive should be repeated
    pub repeat: u16,
    /// The minimum size in the repeat(<>, minmax(_, 1fr)) equation
    pub min_size: GridTemplateMinSize,
}

/// One end of a CSS grid track's sizing function — the `a` or the `b` in
/// `minmax(a, b)`.
///
/// This is the full set taffy can express for a *definite* track. `fit-content()`
/// and `calc()` are absent: the first has no caller and the second is resolved
/// before it reaches here.
#[derive(Copy, Clone, PartialEq, Debug, Default, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
pub enum GridTrackSize {
    /// `auto`
    #[default]
    Auto,
    /// `min-content`
    MinContent,
    /// `max-content`
    MaxContent,
    /// An absolute length, in logical pixels.
    Px(f32),
    /// A percentage of the container's track-axis size, as a FRACTION (`50%`
    /// is `0.5`) — taffy's unit, not CSS's.
    Percent(f32),
    /// `<flex>` — `1fr` is `Fr(1.0)`. Only valid as the MAX of a track; a `fr`
    /// minimum is invalid CSS and is normalised to `Auto` by the caller.
    Fr(f32),
}

/// One track in a `grid-template-columns` / `grid-template-rows` list.
///
/// Always stored in its `minmax()` normal form, which is what CSS says every
/// track sizing function means: a bare `1fr` is `minmax(auto, 1fr)`, a bare
/// `200px` is `minmax(200px, 200px)`.
#[derive(Copy, Clone, PartialEq, Debug, Default, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
pub struct GridTrack {
    /// The track's minimum size.
    pub min: GridTrackSize,
    /// The track's maximum size.
    pub max: GridTrackSize,
}

impl GridTrack {
    /// `minmax(size, size)` — the normal form of a bare, definite track.
    pub fn fixed(size: GridTrackSize) -> Self {
        Self {
            min: size,
            max: size,
        }
    }

    /// `minmax(auto, <flex>)` — the normal form of a bare `<flex>` track.
    pub fn fr(flex: f32) -> Self {
        Self {
            min: GridTrackSize::Auto,
            max: GridTrackSize::Fr(flex),
        }
    }
}

/// vue-native fork: CSS `repeat(auto-fill | auto-fit, <tracks>)` inside a
/// track list — as many repetitions as fit the container, which taffy counts
/// at layout time. The repeated tracks are a run of the track list it belongs
/// to ([`Style::vn_grid_cols`] / [`Style::vn_grid_rows`]); CSS allows one such
/// repetition per list, and every other track in that list must then have a
/// fixed size, which the caller checks.
#[derive(Copy, Clone, PartialEq, Eq, Debug, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
pub struct GridAutoRepeat {
    /// `auto-fit` (the empty repetitions collapse) rather than `auto-fill`
    /// (they keep their size).
    pub fit: bool,
    /// The index in the track list of the first repeated track.
    pub start: u16,
    /// How many tracks of the list one repetition holds.
    pub len: u16,
}

/// CSS `grid-auto-flow`: which axis the auto-placement algorithm fills, and
/// whether it backfills holes.
#[derive(Copy, Clone, PartialEq, Eq, Debug, Default, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
pub enum GridAutoFlow {
    /// Fill each row in turn, adding rows as needed.
    #[default]
    Row,
    /// Fill each column in turn, adding columns as needed.
    Column,
    /// `row dense`
    RowDense,
    /// `column dense`
    ColumnDense,
}

/// CSS `outline`: a ring painted just outside the border box, taking no layout
/// space. See [`Style::vn_outline`].
#[derive(Copy, Clone, PartialEq, Debug, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
pub struct Outline {
    /// `outline-width`. Zero paints nothing.
    pub width: Pixels,
    /// `outline-offset` — the gap between the border box and the ring's INNER
    /// edge. May be negative, which pulls the ring inside the box.
    pub offset: Pixels,
    /// `outline-color`.
    pub color: Hsla,
    /// `outline-style`, reduced to what gpui's quad can draw.
    pub style: BorderStyle,
}

/// vue-native fork: one CSS `border-*-radius` corner, as its two radii. Each
/// is a length, a percentage of the box (`Fraction`), or a linear `calc()`
/// with one. See [`Style::vn_corner_radii`].
#[derive(Copy, Clone, PartialEq, Debug, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
pub struct VnCornerRadius {
    /// The horizontal radius; a percentage is of the box's width.
    pub x: DefiniteLength,
    /// The vertical radius; a percentage is of the box's height.
    pub y: DefiniteLength,
}

static VN_ELLIPSE_DRAWN_AS_CIRCLE: std::sync::atomic::AtomicBool =
    std::sync::atomic::AtomicBool::new(false);

/// vue-native fork: whether a corner was painted with a circle in place of
/// the ellipse CSS asks for, since the last call. vn-tree reads it to advise
/// once.
pub fn vn_take_ellipse_drawn_as_circle() -> bool {
    VN_ELLIPSE_DRAWN_AS_CIRCLE.swap(false, std::sync::atomic::Ordering::Relaxed)
}

/// vue-native fork: a box's painted corner radii, as CSS resolves them.
///
/// - A corner with a `relative` radius resolves it against `size`: the
///   horizontal radius against the width, the vertical against the height.
///   Any other corner takes its `lengths` radius on both axes.
/// - CSS's overlap rule (css-backgrounds-3 §5.5) applies: when the radii
///   along a side sum past its length, every radius is scaled by the
///   smallest such ratio. That is what makes Tailwind's `rounded-full`
///   (9999px) a pill.
/// - gpui's corners are circles, so a corner whose two radii still differ
///   takes the smaller one. `50%` on a box that is not square comes out as a
///   pill where a browser draws an ellipse, and
///   [`vn_take_ellipse_drawn_as_circle`] reports it.
/// - Each radius is finally clamped to half the shorter side, which the
///   shaders need because they pick a corner by quadrant. CSS lets a lone
///   large corner reach past the middle of the box.
pub fn vn_resolve_corner_radii(
    lengths: &Corners<AbsoluteLength>,
    relative: &Corners<Option<VnCornerRadius>>,
    size: Size<Pixels>,
    rem_size: Pixels,
) -> Corners<Pixels> {
    let (width, height) = (f32::from(size.width).max(0.), f32::from(size.height).max(0.));
    let pair = |length: &AbsoluteLength, relative: &Option<VnCornerRadius>| -> (f32, f32) {
        match relative {
            Some(radius) => (
                f32::from(radius.x.to_pixels(px(width).into(), rem_size)).max(0.),
                f32::from(radius.y.to_pixels(px(height).into(), rem_size)).max(0.),
            ),
            None => {
                let radius = f32::from(length.to_pixels(rem_size)).max(0.);
                (radius, radius)
            }
        }
    };
    let top_left = pair(&lengths.top_left, &relative.top_left);
    let top_right = pair(&lengths.top_right, &relative.top_right);
    let bottom_right = pair(&lengths.bottom_right, &relative.bottom_right);
    let bottom_left = pair(&lengths.bottom_left, &relative.bottom_left);

    let ratio = |side: f32, a: f32, b: f32| if a + b > side { side / (a + b) } else { 1. };
    let factor = ratio(width, top_left.0, top_right.0)
        .min(ratio(width, bottom_left.0, bottom_right.0))
        .min(ratio(height, top_left.1, bottom_left.1))
        .min(ratio(height, top_right.1, bottom_right.1));

    let circle = |(x, y): (f32, f32)| {
        let (x, y) = (x * factor, y * factor);
        let radius = x.min(y);
        if radius > 0. && (x - y).abs() > 0.5 {
            VN_ELLIPSE_DRAWN_AS_CIRCLE.store(true, std::sync::atomic::Ordering::Relaxed);
        }
        px(radius)
    };
    Corners {
        top_left: circle(top_left),
        top_right: circle(top_right),
        bottom_right: circle(bottom_right),
        bottom_left: circle(bottom_left),
    }
    .clamp_radii_for_quad_size(size)
}

/// vue-native fork: the box a `clip-path` is laid against (CSS Masking 1
/// §5.1, `<geometry-box>`). `fill-box` is the content box and `stroke-box` and
/// `view-box` the border box, as for any element with a CSS box.
#[derive(Copy, Clone, Default, PartialEq, Debug, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
pub enum VnClipBox {
    /// The margin box.
    Margin,
    /// The border box, the default.
    #[default]
    Border,
    /// The padding box.
    Padding,
    /// The content box.
    Content,
}

/// vue-native fork: a `<shape-radius>` of `circle()` or `ellipse()`.
#[derive(Copy, Clone, PartialEq, Debug, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
pub enum VnShapeRadius {
    /// A length. A percentage is of the reference box's width for an
    /// ellipse's horizontal radius, of its height for the vertical one, and
    /// of `sqrt(width² + height²) / sqrt(2)` for a circle's.
    Length(DefiniteLength),
    /// The distance from the centre to the reference box's closest side
    /// (in the radius's own axis, for an ellipse).
    ClosestSide,
    /// The distance to the farthest side.
    FarthestSide,
}

/// vue-native fork: CSS `clip-path` (CSS Masking 1 §5). The shapes a rounded
/// content mask can carry are one ([`Style::vn_clip_mask`]); `polygon()`,
/// `path()` and an ellipse that is no circle are an isolated layer composited
/// through the shape ([`Style::vn_clip_outline`], [`crate::VnClipGeometry`]).
#[derive(Clone, Default, PartialEq, Debug, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
pub enum VnClipPath {
    /// `none`: no clip.
    #[default]
    None,
    /// A `<geometry-box>` alone: that box, rounded as `border-radius` rounds
    /// it.
    Box(VnClipBox),
    /// `inset()`: the reference box less `insets`, with `radii` (`round`).
    /// A percentage inset is of the box's width (left and right) or height
    /// (top and bottom), and a percentage radius of the box's size, as
    /// Chromium resolves them.
    Inset {
        /// The box the insets are taken from.
        reference: VnClipBox,
        /// The insets: top, right, bottom, left.
        insets: [DefiniteLength; 4],
        /// The `round` radii: top left, top right, bottom right, bottom
        /// left. Zero for a square corner.
        radii: [VnCornerRadius; 4],
    },
    /// `circle()`, or `ellipse()`: a centre in the reference box and two
    /// radii. A circle is a rounded mask; an ellipse whose radii differ is
    /// its outline, four cubics, in an isolated layer. Both are exact.
    Ellipse {
        /// The box the centre and the radii are resolved against.
        reference: VnClipBox,
        /// The centre, from the reference box's top left corner: x, y.
        center: [DefiniteLength; 2],
        /// The horizontal and vertical radii; the same for a circle.
        radius: [VnShapeRadius; 2],
        /// `circle()`: a percentage radius is of the box's diagonal over
        /// `sqrt(2)`, and the closest or farthest side is of all four.
        circle: bool,
    },
    /// `polygon()`: its vertices, x and y from the reference box's top left
    /// corner, a percentage of its width and height.
    Polygon {
        /// The box the vertices are laid against.
        reference: VnClipBox,
        /// `nonzero` or `evenodd`.
        fill_rule: VnFillRule,
        /// The vertices, in order.
        points: Arc<[[DefiniteLength; 2]]>,
    },
    /// `path()`: SVG path data, in pixels from the reference box's top left
    /// corner (CSS Shapes 1 §3.1).
    Path {
        /// The box the path is laid against.
        reference: VnClipBox,
        /// `nonzero` or `evenodd`.
        fill_rule: VnFillRule,
        /// The path, absolute ([`VnPathVerb`]).
        verbs: Arc<[VnPathVerb]>,
    },
}

/// vue-native fork: a `circle()` or `ellipse()` resolved against its reference
/// box `rect`: the centre and the two radii (CSS Shapes 1 §3.1).
fn vn_clip_ellipse(
    rect: Bounds<Pixels>,
    center: &[DefiniteLength; 2],
    radius: &[VnShapeRadius; 2],
    circle: bool,
    rem_size: Pixels,
) -> (Point<Pixels>, Pixels, Pixels) {
    let (w, h) = (rect.size.width, rect.size.height);
    let resolve = |length: &DefiniteLength, base: Pixels| length.to_pixels(AbsoluteLength::Pixels(base), rem_size);
    let cx = rect.left() + resolve(&center[0], w);
    let cy = rect.top() + resolve(&center[1], h);
    let (near_x, far_x) = ((cx - rect.left()).abs(), (rect.right() - cx).abs());
    let (near_y, far_y) = ((cy - rect.top()).abs(), (rect.bottom() - cy).abs());
    // A circle's sides are all four, and its percentage is of the box's
    // diagonal over sqrt(2).
    let diagonal = px((f32::from(w).powi(2) + f32::from(h).powi(2)).sqrt() / 2f32.sqrt());
    let one = |r: &VnShapeRadius, horizontal: bool| -> Pixels {
        let (a, b) = if circle {
            (near_x.min(far_x).min(near_y.min(far_y)), near_x.max(far_x).max(near_y.max(far_y)))
        } else if horizontal {
            (near_x.min(far_x), near_x.max(far_x))
        } else {
            (near_y.min(far_y), near_y.max(far_y))
        };
        match r {
            VnShapeRadius::ClosestSide => a,
            VnShapeRadius::FarthestSide => b,
            VnShapeRadius::Length(l) => resolve(l, if circle { diagonal } else if horizontal { w } else { h }).max(px(0.)),
        }
    };
    (point(cx, cy), one(&radius[0], true), one(&radius[1], false))
}

/// The CSS styling that can be applied to an element via the `Styled` trait
#[derive(Clone, Refineable, Debug)]
#[refineable(Debug, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", refineable(JsonSchema))]
pub struct Style {
    /// What layout strategy should be used?
    pub display: Display,

    /// Should the element be painted on screen?
    pub visibility: Visibility,

    // Overflow properties
    /// How children overflowing their container should affect layout
    #[refineable]
    pub overflow: Point<Overflow>,
    /// How much space (in points) should be reserved for the scrollbars of `Overflow::Scroll` and `Overflow::Auto` nodes.
    pub scrollbar_width: AbsoluteLength,
    /// Whether both x and y axis should be scrollable at the same time.
    pub allow_concurrent_scroll: bool,
    /// Whether scrolling should be restricted to the input gesture's axis.
    ///
    /// Pixel-based scroll gestures are locked to their initially dominant axis. The lock may be
    /// released when the gesture changes direction strongly. Touch phases delimit gestures when
    /// available, with a timeout fallback for platforms that only emit moved events.
    ///
    /// This also prevents input from being remapped to another axis. For example, horizontal input
    /// will not scroll a container that only has vertical overflow enabled. Mouse wheel platforms
    /// typically report ordinary wheel input on the Y axis and Shift-modified input on the X axis.
    ///
    /// ## Motivation
    ///
    /// On the web when scrolling with the mouse wheel, scrolling up and down will always scroll the Y axis, even when
    /// the mouse is over a horizontally-scrollable element.
    ///
    /// The only way to scroll horizontally is to hold down `Shift` while scrolling, which then changes the scroll axis
    /// to the X axis.
    ///
    /// Currently, GPUI operates differently from the web in that it will scroll an element in either the X or Y axis
    /// when scrolling with just the mouse wheel. This causes problems when scrolling in a vertical list that contains
    /// horizontally-scrollable elements, as when you get to the horizontally-scrollable elements the scroll will be
    /// hijacked.
    ///
    /// Ideally we would match the web's behavior and not have a need for this, but right now we're adding this opt-in
    /// style property to limit the potential blast radius.
    pub restrict_scroll_to_axis: bool,

    // Position properties
    /// What should the `position` value of this struct use as a base offset?
    pub position: Position,
    /// How should the position of this element be tweaked relative to the layout defined?
    #[refineable]
    pub inset: Edges<Length>,

    // Size properties
    /// Sets the initial size of the item
    #[refineable]
    pub size: Size<Length>,
    /// Controls the minimum size of the item
    #[refineable]
    pub min_size: Size<Length>,
    /// Controls the maximum size of the item
    #[refineable]
    pub max_size: Size<Length>,
    /// Sets the preferred aspect ratio for the item. The ratio is calculated as width divided by height.
    /// `0` is CSS `auto`, and a negative ratio is `auto <ratio>`: a replaced element's natural ratio wins.
    pub aspect_ratio: Option<f32>,

    // Spacing Properties
    /// How large should the margin be on each side?
    #[refineable]
    pub margin: Edges<Length>,
    /// How large should the padding be on each side?
    #[refineable]
    pub padding: Edges<DefiniteLength>,
    /// How large should the border be on each side?
    #[refineable]
    pub border_widths: Edges<AbsoluteLength>,

    // Alignment properties
    /// How this node's children aligned in the cross/block axis?
    pub align_items: Option<AlignItems>,
    /// How this node should be aligned in the cross/block axis. Falls back to the parents [`AlignItems`] if not set
    pub align_self: Option<AlignSelf>,
    /// How should content contained within this item be aligned in the cross/block axis
    pub align_content: Option<AlignContent>,
    /// How should contained within this item be aligned in the main/inline axis
    pub justify_content: Option<JustifyContent>,
    /// How large should the gaps between items in a flex container be?
    #[refineable]
    pub gap: Size<DefiniteLength>,

    // Flexbox properties
    /// Which direction does the main axis flow in?
    pub flex_direction: FlexDirection,
    /// Should elements wrap, or stay in a single line?
    pub flex_wrap: FlexWrap,
    /// Sets the initial main axis size of the item
    pub flex_basis: Length,
    /// The relative rate at which this item grows when it is expanding to fill space, 0.0 is the default value, and this value must be positive.
    pub flex_grow: f32,
    /// The relative rate at which this item shrinks when it is contracting to fit into space, 1.0 is the default value, and this value must be positive.
    pub flex_shrink: f32,
    /// CSS `order`: a flex or grid item is laid out in ascending order, ties in document order.
    /// Painting, hit-testing and focus order keep document order.
    pub vn_order: i32,
    /// CSS `object-position` of an image painted in this box; `None` keeps
    /// [`ObjectFit::get_bounds`]'s placement.
    pub vn_object_position: Option<ObjectPosition>,

    /// The fill color of this element
    pub background: Option<Fill>,

    /// Extra background layers painted OVER [`Style::background`], bottom layer
    /// first — CSS's `background-image` stack, which sits above
    /// `background-color`.
    ///
    /// One quad per layer rather than a layer array inside the quad: painter's
    /// order already composites them in the right sequence, the scene's sort
    /// and cull need no new concept, and nothing about the shader changes. The
    /// cost is one extra primitive per layer, which is why a caller is expected
    /// to keep the stack short.
    ///
    /// CSS lists layers top-first; this vector is bottom-first, so the mapper
    /// reverses.
    pub background_layers: Vec<Background>,

    /// The border color of this element
    pub border_color: Option<Hsla>,

    /// CSS side colours override the shared colour independently.
    #[refineable]
    pub vn_border_colors: Edges<Option<Hsla>>,

    /// The border style of this element
    pub border_style: BorderStyle,

    /// The radius of the corners of this element
    #[refineable]
    pub corner_radii: Corners<AbsoluteLength>,

    /// vue-native fork: a CSS corner radius `corner_radii` cannot hold. That is a
    /// percentage of the box, a `calc()` with one, or an elliptical `a / b`
    /// corner. Where a corner has one, it replaces that corner's
    /// `corner_radii`, and it resolves against the box at paint
    /// ([`Style::corner_radii_for`]). vn-css writes both fields for every
    /// corner it maps, so the later declaration wins whichever it used.
    #[refineable]
    pub vn_corner_radii: Corners<Option<VnCornerRadius>>,

    /// vue-native fork: CSS `corner-shape`, the shape each corner's radius is drawn with.
    #[refineable]
    pub vn_corner_shapes: Corners<CornerShape>,

    /// Box shadow of the element
    pub box_shadow: Vec<BoxShadow>,

    /// vue-native fork: draw the outer shadows under the border box too.
    ///
    /// CSS clips an outer `box-shadow` to the outside of the border box (CSS
    /// Backgrounds 3 §7.1): the shadow is cast as if the box were opaque, so
    /// a translucent background shows what is behind the element, not the
    /// shadow. [`Window::paint_drop_shadows`] does that by default. This
    /// switch is for a shadow that stands in for the element's own painting:
    /// vue-native draws `filter: blur()` on a solid box as the box's own
    /// Gaussian shadow, whose middle must stay filled.
    pub vn_shadow_under_box: bool,

    /// CSS `backdrop-filter: blur(<length>)`: blur everything already painted
    /// behind this element's border box, clipped to `corner_radii`.
    ///
    /// `None` (and a non-positive radius) means no backdrop filter. Only the
    /// `blur()` filter function is modelled; the rest of the CSS filter
    /// function list is not.
    pub backdrop_blur: Option<Pixels>,

    /// The `--shading: <effect>(<args>)` custom property: shade this element's
    /// border box with one of the built-in effect pipelines.
    ///
    /// `None` means the property was never declared. `--shading: none` is
    /// [`EffectSpec::NONE`] — a *value*, because a refinement cannot spell
    /// "explicitly nothing" and that is what has to cancel an effect declared
    /// by an earlier rule in the cascade. Same shape as `backdrop-filter: none`
    /// mapping to a 0px radius.
    ///
    /// Deliberate divergence from CSS, documented for authors: a browser
    /// *inherits* custom properties down the subtree. vue-native applies
    /// `--shading` to the declaring element only — inheriting a GPU effect into
    /// every descendant is never what an author means, and a browser ignores
    /// the property entirely anyway, so nothing renders differently there.
    pub vn_effect: Option<EffectSpec>,

    /// A `--shading` transition between different effects: the outgoing one,
    /// painted beneath `vn_effect` while they cross-fade.
    pub vn_effect_fade: Option<EffectCrossFade>,

    /// CSS `transform`: rotate, scale and translate this element's PAINTED
    /// output — its background, borders, shadows, text and its whole subtree —
    /// about its own centre, changing no layout at all.
    ///
    /// `None` means the property was never declared; [`TransformSpec::IDENTITY`]
    /// is `transform: none`, a *value*, because a refinement cannot spell
    /// "explicitly nothing" and that is what has to cancel a transform declared
    /// by an earlier rule in the cascade. Same shape as `vn_effect` above, and
    /// as `backdrop-filter: none` mapping to a 0px radius.
    ///
    /// Layout is untouched on purpose, and that is the browser's rule rather
    /// than a shortcut: a transformed element still occupies exactly the box it
    /// laid out in, its siblings do not move, and its scroll container does not
    /// grow. It also means `translate()` here is NOT interchangeable with a
    /// margin offset — which is the divergence this field exists to retire.
    ///
    /// Hit-testing follows the picture: a hitbox records the matrix in force
    /// when it was inserted and the pointer is mapped back through the inverse,
    /// so a rotated button is clickable where it is *drawn*.
    pub vn_transform: Option<TransformSpec>,

    /// The text style of this element
    #[refineable]
    pub text: TextStyleRefinement,

    /// The mouse cursor style shown when the mouse pointer is over an element.
    pub mouse_cursor: Option<CursorStyle>,

    /// The opacity of this element
    pub opacity: Option<f32>,

    /// The grid columns of this element
    /// Roughly equivalent to the Tailwind `grid-cols-<number>`
    pub grid_cols: Option<GridTemplate>,

    /// The row span of this element
    /// Equivalent to the Tailwind `grid-rows-<number>`
    pub grid_rows: Option<GridTemplate>,

    /// The grid location of this element
    pub grid_location: Option<GridLocation>,

    /// CSS `grid-template-columns` as an ARBITRARY track list — `1fr auto`,
    /// `minmax(0, 1fr) 200px`, `repeat(3, 1fr)` (expanded by the caller).
    ///
    /// Takes precedence over [`Style::grid_cols`], which stays as the cheap
    /// `repeat(N, …)` shorthand the `Styled` helpers write. `None` means "this
    /// element declared no track list", not "no columns".
    pub vn_grid_cols: Option<Vec<GridTrack>>,

    /// CSS `grid-template-rows` as an arbitrary track list. See
    /// [`Style::vn_grid_cols`].
    pub vn_grid_rows: Option<Vec<GridTrack>>,

    /// The `repeat(auto-fill | auto-fit, …)` run of [`Style::vn_grid_cols`],
    /// if it has one. Meaningless without that list.
    pub vn_grid_cols_auto: Option<GridAutoRepeat>,

    /// The `repeat(auto-fill | auto-fit, …)` run of [`Style::vn_grid_rows`].
    pub vn_grid_rows_auto: Option<GridAutoRepeat>,

    /// CSS `grid-auto-columns`: a repeating pattern for implicit column tracks.
    pub vn_grid_auto_cols: Option<Vec<GridTrack>>,

    /// CSS `grid-auto-rows`: a repeating pattern for implicit row tracks.
    pub vn_grid_auto_rows: Option<Vec<GridTrack>>,

    /// CSS `grid-auto-flow` — the direction the auto-placement algorithm fills,
    /// and whether it packs densely.
    pub vn_grid_auto_flow: Option<GridAutoFlow>,

    /// CSS `justify-items` — the inline-axis alignment this container gives its
    /// children. (`align_items` is the block axis; taffy honours both, gpui only
    /// ever exposed the second.)
    pub vn_justify_items: Option<AlignItems>,

    /// CSS `justify-self` — this item's own inline-axis alignment, overriding
    /// its container's `justify-items`.
    pub vn_justify_self: Option<AlignSelf>,

    /// CSS `outline` — a ring painted OUTSIDE the border box that takes no
    /// layout space at all, which is what makes it (and not a border) the right
    /// primitive for a focus indicator.
    ///
    /// gpui has no outline in its scene; this is painted as one extra
    /// border-only quad at dilated bounds, after the element's own border, so it
    /// lands on top of the element and outside it. `None` = never declared.
    pub vn_outline: Option<Outline>,

    /// vue-native fork: CSS `mask` in the one shape the paint path can cut,
    /// the gradient-border idiom. That is two opaque layers, the top one
    /// clipped to the content box, composited with `exclude`:
    /// `mask: linear-gradient(#fff 0 0) content-box, linear-gradient(#fff 0 0);
    /// mask-composite: exclude`. What is left visible is the ring of padding
    /// and border around the content box.
    ///
    /// When set, the background and its layers paint only in that ring, as one
    /// quad each whose border widths are the border plus the padding
    /// (`color::BG_RING`). The border lies inside the ring and paints as
    /// usual. Not painted at all: the outer shadows and the outline, which lie
    /// outside the border box and which the mask cuts entirely, and the inset
    /// shadows, backdrop blur and effect quads, which the ring would show only
    /// a strip of. The children are NOT masked. vn-tree says so when there
    /// are any. A
    /// percentage padding resolves against the element's own width here,
    /// not its containing block's.
    pub vn_mask_ring: bool,

    /// vue-native fork: CSS `clip-path`, in the shapes a rounded content
    /// mask carries ([`VnClipPath`]). It clips the element's whole painting,
    /// its shadows, background, border, outline and subtree, and its hit
    /// test, inside its `transform`.
    pub vn_clip_path: VnClipPath,

    /// SVG color filtering of the whole painted subtree.
    pub vn_color_matrix: Option<crate::VnColorMatrix>,

    /// vue-native fork: paint this element into the snapshot of this id
    /// instead of the page ([`Window::vn_with_capture`]).
    pub vn_capture: Option<u64>,

    /// vue-native fork: paint the subtree into an isolated group, so a
    /// `plus-lighter` composite inside blends with the group only.
    pub vn_isolate: bool,

    /// vue-native fork: size containment, [`Style::CONTAIN_INLINE_SIZE`] and
    /// [`Style::CONTAIN_BLOCK_SIZE`]: the box is sized as if it were empty on that axis.
    pub vn_contain_size: u8,

    /// CSS backgrounds painted through the descendant glyph alpha, not the box.
    pub vn_text_fill: Option<VnTextFill>,

    /// CSS `mask` over the element's whole painting, inside its `clip-path`.
    pub vn_mask: Option<VnMask>,

    /// Whether to draw a red debugging outline around this element
    #[cfg(debug_assertions)]
    pub debug: bool,

    /// Whether to draw a red debugging outline around this element and all of its conforming children
    #[cfg(debug_assertions)]
    pub debug_below: bool,
}

impl Styled for StyleRefinement {
    fn style(&mut self) -> &mut StyleRefinement {
        self
    }
}

impl StyleRefinement {
    /// The grid location of this element
    pub fn grid_location_mut(&mut self) -> &mut GridLocation {
        self.grid_location.get_or_insert_default()
    }
}

/// The value of the visibility property, similar to the CSS property `visibility`
#[derive(Default, Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
pub enum Visibility {
    /// The element should be drawn as normal.
    #[default]
    Visible,
    /// The element should not be drawn, but should still take up space in the layout.
    Hidden,
}

/// The possible values of the box-shadow property
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
pub struct BoxShadow {
    /// What color should the shadow have?
    pub color: Hsla,
    /// How should it be offset from its element?
    pub offset: Point<Pixels>,
    /// How much should the shadow be blurred?
    pub blur_radius: Pixels,
    /// How much should the shadow spread?
    pub spread_radius: Pixels,
    /// Whether this is an inset shadow (drawn inside the element's bounds).
    pub inset: bool,
}

/// One CSS `text-shadow`, painted beneath the glyphs it is attached to.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
pub struct TextShadow {
    /// `None` is CSS `currentColor`: the color of the text it shadows.
    pub color: Option<Hsla>,
    /// The offset from the glyphs.
    pub offset: Point<Pixels>,
    /// The CSS blur radius: a Gaussian blur with a standard deviation of half of it.
    pub blur_radius: Pixels,
}

impl BoxShadow {
    /// Creates a new [`BoxShadow`] with the given offset and color, matching the order
    /// of the CSS `box-shadow` property. Use the builder methods to set blur radius,
    /// spread radius, and inset.
    pub fn new(offset_x: Pixels, offset_y: Pixels, color: Hsla) -> Self {
        Self {
            color,
            offset: point(offset_x, offset_y),
            blur_radius: px(0.),
            spread_radius: px(0.),
            inset: false,
        }
    }

    /// Sets the shadow blur radius.
    pub fn blur_radius(mut self, blur_radius: Pixels) -> Self {
        self.blur_radius = blur_radius;
        self
    }

    /// Sets the shadow spread radius.
    pub fn spread_radius(mut self, spread_radius: Pixels) -> Self {
        self.spread_radius = spread_radius;
        self
    }

    /// Marks the shadow as inset (drawn inside the element's bounds).
    pub fn inset(mut self) -> Self {
        self.inset = true;
        self
    }
}

/// How to handle whitespace in text
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
pub enum WhiteSpace {
    /// Normal line wrapping when text overflows the width of the element
    #[default]
    Normal,
    /// No line wrapping, text will overflow the width of the element
    Nowrap,
}

/// vue-native fork: CSS `overflow-wrap` (CSS Text 3 §5.5). May a line break
/// inside a word that does not fit on a line of its own?
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
pub enum OverflowWrap {
    /// Lines break only at soft wrap opportunities. A word wider than the
    /// line overflows it.
    #[default]
    Normal,
    /// A word that does not fit on a line of its own is broken where it
    /// overflows. The min-content size still counts the word whole.
    BreakWord,
    /// As `BreakWord`, and the min-content size counts those breaks too, so a
    /// flex item or a `min-content` box can be as narrow as one character.
    Anywhere,
}

/// vue-native fork: CSS `word-break` (CSS Text 3 §5.2).
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
pub enum WordBreak {
    /// The usual soft wrap opportunities.
    #[default]
    Normal,
    /// A line may break between any two letters, and the min-content size
    /// counts those breaks.
    BreakAll,
    /// CJK letters are kept together like the letters of any other word: no
    /// break between two of them.
    KeepAll,
    /// The legacy keyword: `normal` with `overflow-wrap: anywhere`, whatever
    /// `overflow-wrap` says.
    BreakWord,
}

/// vue-native fork: CSS `hyphens` (CSS Text 3 §6.1). There is no hyphenation
/// dictionary, so `auto` is the caller's to map to `Manual`.
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
pub enum Hyphens {
    /// A soft hyphen (U+00AD) is no break opportunity.
    None,
    /// A line may break after a soft hyphen, and shows a hyphen there.
    #[default]
    Manual,
}

/// vue-native fork: the rules a text breaks its lines under, from its style
/// ([`TextStyle::wrap`]). Part of the line layout cache's key.
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Hash)]
pub struct TextWrap {
    /// CSS `overflow-wrap`.
    pub overflow_wrap: OverflowWrap,
    /// CSS `word-break`.
    pub word_break: WordBreak,
    /// CSS `hyphens`.
    pub hyphens: Hyphens,
}

impl TextWrap {
    /// gpui's rules before the fork took CSS's: a word that does not fit on
    /// a line is broken where it overflows. What [`crate::WindowTextSystem::shape_text`]
    /// keeps for its callers, a text field among them (a `<textarea>` is
    /// `overflow-wrap: break-word` in every browser's UA sheet).
    pub const BREAK_WORD: Self = Self {
        overflow_wrap: OverflowWrap::BreakWord,
        word_break: WordBreak::Normal,
        hyphens: Hyphens::Manual,
    };

    /// May a word that does not fit be broken where it overflows?
    pub(crate) fn breaks_words(&self) -> bool {
        self.overflow_wrap != OverflowWrap::Normal || self.word_break == WordBreak::BreakWord
    }

    /// Do those breaks count for the min-content size?
    pub(crate) fn min_content_breaks_words(&self) -> bool {
        self.overflow_wrap == OverflowWrap::Anywhere || self.word_break == WordBreak::BreakWord
    }
}

/// How to truncate text that overflows the width of the element
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
pub enum TextOverflow {
    /// Truncate the text at the end when it doesn't fit, and represent this truncation by
    /// displaying the provided string (e.g., "very long te…").
    Truncate(SharedString),
    /// Truncate the text at the start when it doesn't fit, and represent this truncation by
    /// displaying the provided string at the beginning (e.g., "…ong text here").
    /// Typically more adequate for file paths where the end is more important than the beginning.
    TruncateStart(SharedString),
    /// Truncate the text in the middle when it doesn't fit, preserving both the start and end
    /// of the string (e.g., "long fi…name.rs"). Useful for filenames where both the prefix
    /// and the extension are important context.
    TruncateMiddle(SharedString),
}

/// How to align text within the element
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
pub enum TextAlign {
    /// Align the text to the left of the element
    #[default]
    Left,

    /// Center the text within the element
    Center,

    /// Align the text to the right of the element
    Right,
}

/// Paints a mask's layers into the border box it is given. With a tint (always
/// opaque), the layers paint in that colour as the element's whole picture
/// ([`VnMask::tint_leaf`]).
pub type VnMaskPainter = std::rc::Rc<dyn Fn(Bounds<Pixels>, Option<Hsla>, &mut Window, &mut App)>;

/// CSS `mask`: the element's painting, subtree included, is multiplied by the
/// alpha (or luminance) of what `paint` draws over its border box.
#[derive(Clone, Default, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
pub struct VnMask {
    /// `mask-mode: luminance`; alpha otherwise.
    pub luminance: bool,
    /// The element paints nothing but its background, and every layer can be
    /// drawn tinted: a solid background then paints as the mask in its colour,
    /// without an isolated layer.
    pub tint_leaf: bool,
    /// Identifies what `paint` draws, for comparing styles.
    pub key: u64,
    /// Draws the layers; no painter, no mask.
    #[serde(skip)]
    #[cfg_attr(feature = "schemars", schemars(skip))]
    pub paint: Option<VnMaskPainter>,
}

impl std::fmt::Debug for VnMask {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("VnMask")
            .field("luminance", &self.luminance)
            .field("tint_leaf", &self.tint_leaf)
            .field("key", &self.key)
            .finish_non_exhaustive()
    }
}

impl PartialEq for VnMask {
    fn eq(&self, other: &Self) -> bool {
        (self.luminance, self.tint_leaf, self.key) == (other.luminance, other.tint_leaf, other.key)
    }
}

/// A CSS background stack clipped to the element's descendant text.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
pub struct VnTextFill {
    /// Stable element identity; reopened/deferred text shares one alpha mask.
    pub group_id: u64,
    /// Backgrounds, bottom first, retaining their gradient and tile parameters.
    pub backgrounds: Vec<Background>,
}

/// A text background resolved against its element, in logical CSS pixels.
/// Carried in TextStyle so deferred drawing and paint-cache keys retain it.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
pub struct VnTextPaint {
    /// The authored stack.
    pub fill: VnTextFill,
    /// The background positioning box.
    pub bounds: [f32; 4],
    /// Inverse device-space element transform, row major followed by translation.
    pub inverse: [f32; 6],
    /// Device pixels per logical CSS pixel.
    pub scale_factor: f32,
    /// The owner scope's geometry, shared by every descendant glyph.
    #[serde(skip)]
    #[cfg_attr(feature = "schemars", schemars(skip))]
    pub geometry: Arc<crate::VnClipGeometry>,
    /// The owner scope's outer mask. Glyphs retain their local child masks.
    #[serde(skip)]
    #[cfg_attr(feature = "schemars", schemars(skip))]
    pub content_mask: ContentMask<crate::ScaledPixels>,
}

/// The properties that can be used to style text in GPUI
#[derive(Refineable, Clone, Debug, PartialEq)]
#[refineable(Debug, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", refineable(JsonSchema))]
pub struct TextStyle {
    /// The color of the text
    pub color: Hsla,

    /// Background clipped to transparent glyphs in this text scope.
    pub vn_text_paint: Option<Arc<VnTextPaint>>,

    /// The font family to use
    pub font_family: SharedString,

    /// The font features to use
    pub font_features: FontFeatures,

    /// The fallback fonts to use
    pub font_fallbacks: Option<FontFallbacks>,

    /// The font size to use, in pixels or rems.
    pub font_size: AbsoluteLength,

    /// The line height to use, in pixels or fractions
    pub line_height: DefiniteLength,

    /// The font weight, e.g. bold
    pub font_weight: FontWeight,

    /// The font style, e.g. italic
    pub font_style: FontStyle,

    /// The background color of the text
    pub background_color: Option<Hsla>,

    /// The underline style of the text
    pub underline: Option<UnderlineStyle>,

    /// The strikethrough style of the text
    pub strikethrough: Option<StrikethroughStyle>,

    /// How to handle whitespace in the text
    pub white_space: WhiteSpace,

    /// The text should be truncated if it overflows the width of the element
    pub text_overflow: Option<TextOverflow>,

    /// How the text should be aligned within the element
    pub text_align: TextAlign,

    /// The number of lines to display before truncating the text
    pub line_clamp: Option<usize>,

    /// Extra space after each typographic character unit — CSS
    /// `letter-spacing`. Already resolved to pixels: `em`-relative tracking is
    /// a function of the font size, which the caller has and this does not.
    pub letter_spacing: Pixels,

    /// Extra space after each word separator — CSS `word-spacing`.
    pub word_spacing: Pixels,

    /// vue-native fork: CSS `overflow-wrap`. Initial `normal`, as in CSS.
    pub overflow_wrap: OverflowWrap,

    /// vue-native fork: CSS `word-break`.
    pub word_break: WordBreak,

    /// vue-native fork: CSS `hyphens`.
    pub hyphens: Hyphens,

    /// vue-native fork: CSS `text-shadow`, first on top.
    pub text_shadow: Vec<TextShadow>,
}

impl Default for TextStyle {
    fn default() -> Self {
        TextStyle {
            color: black(),
            vn_text_paint: None,
            // todo(linux) make this configurable or choose better default
            font_family: ".SystemUIFont".into(),
            font_features: FontFeatures::default(),
            font_fallbacks: None,
            font_size: rems(1.).into(),
            line_height: phi(),
            font_weight: FontWeight::default(),
            font_style: FontStyle::default(),
            background_color: None,
            underline: None,
            strikethrough: None,
            white_space: WhiteSpace::Normal,
            text_overflow: None,
            text_align: TextAlign::default(),
            line_clamp: None,
            letter_spacing: px(0.),
            word_spacing: px(0.),
            overflow_wrap: OverflowWrap::default(),
            word_break: WordBreak::default(),
            hyphens: Hyphens::default(),
            text_shadow: Vec::new(),
        }
    }
}

impl TextStyle {
    /// vue-native fork: the line-breaking rules of this style.
    pub fn wrap(&self) -> TextWrap {
        TextWrap {
            overflow_wrap: self.overflow_wrap,
            word_break: self.word_break,
            hyphens: self.hyphens,
        }
    }

    /// Create a new text style with the given highlighting applied.
    pub fn highlight(mut self, style: impl Into<HighlightStyle>) -> Self {
        let style = style.into();
        if let Some(weight) = style.font_weight {
            self.font_weight = weight;
        }
        if let Some(style) = style.font_style {
            self.font_style = style;
        }

        if let Some(color) = style.color {
            self.color = self.color.blend(color);
        }

        if let Some(factor) = style.fade_out {
            self.color.fade_out(factor);
        }

        if let Some(background_color) = style.background_color {
            self.background_color = Some(background_color);
        }

        if let Some(underline) = style.underline {
            self.underline = Some(underline);
        }

        if let Some(strikethrough) = style.strikethrough {
            self.strikethrough = Some(strikethrough);
        }

        self
    }

    /// Get the font configured for this text style.
    pub fn font(&self) -> Font {
        Font {
            family: self.font_family.clone(),
            features: self.font_features.clone(),
            fallbacks: self.font_fallbacks.clone(),
            weight: self.font_weight,
            style: self.font_style,
        }
    }

    /// Returns the rounded line height in pixels.
    pub fn line_height_in_pixels(&self, rem_size: Pixels) -> Pixels {
        self.line_height.to_pixels(self.font_size, rem_size).round()
    }

    /// Convert this text style into a [`TextRun`], for the given length of the text.
    pub fn to_run(&self, len: usize) -> TextRun {
        TextRun {
            len,
            font: Font {
                family: self.font_family.clone(),
                features: self.font_features.clone(),
                fallbacks: self.font_fallbacks.clone(),
                weight: self.font_weight,
                style: self.font_style,
            },
            color: self.color,
            background_color: self.background_color,
            underline: self.underline,
            strikethrough: self.strikethrough,
            tracking: self.letter_spacing,
            word_spacing: self.word_spacing,
            text_shadow: self.text_shadow.clone(),
        }
    }
}

/// A highlight style to apply, similar to a `TextStyle` except
/// for a single font, uniformly sized and spaced text.
#[derive(Copy, Clone, Debug, Default, PartialEq)]
pub struct HighlightStyle {
    /// The color of the text
    pub color: Option<Hsla>,

    /// The font weight, e.g. bold
    pub font_weight: Option<FontWeight>,

    /// The font style, e.g. italic
    pub font_style: Option<FontStyle>,

    /// The background color of the text
    pub background_color: Option<Hsla>,

    /// The underline style of the text
    pub underline: Option<UnderlineStyle>,

    /// The underline style of the text
    pub strikethrough: Option<StrikethroughStyle>,

    /// Similar to the CSS `opacity` property, this will cause the text to be less vibrant.
    pub fade_out: Option<f32>,
}

impl Eq for HighlightStyle {}

impl Hash for HighlightStyle {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.color.hash(state);
        self.font_weight.hash(state);
        self.font_style.hash(state);
        self.background_color.hash(state);
        self.underline.hash(state);
        self.strikethrough.hash(state);
        state.write_u32(u32::from_be_bytes(
            self.fade_out.map(|f| f.to_be_bytes()).unwrap_or_default(),
        ));
    }
}

/// The one colour that best stands in for a background — the fill itself when
/// it is solid, and a gradient's first live stop otherwise. Used for the
/// transparent border that keeps a fill's antialiased corner from seaming.
fn representative_color(background: &Background) -> Hsla {
    match background.tag {
        BackgroundTag::Solid | BackgroundTag::PatternSlash | BackgroundTag::Checkerboard => {
            background.solid
        }
        BackgroundTag::LinearGradient
        | BackgroundTag::RadialGradient
        | BackgroundTag::ConicGradient => background
            .live_stops()
            .first()
            .map(|stop| stop.color)
            .unwrap_or_default(),
    }
}

impl Style {
    /// [`Style::vn_contain_size`]: the inline (horizontal) axis.
    pub const CONTAIN_INLINE_SIZE: u8 = 1;
    /// [`Style::vn_contain_size`]: the block (vertical) axis.
    pub const CONTAIN_BLOCK_SIZE: u8 = 2;

    /// Returns true if the style is visible and the background is opaque.
    pub fn has_opaque_background(&self) -> bool {
        self.background
            .as_ref()
            .is_some_and(|fill| fill.color().is_some_and(|color| !color.is_transparent()))
    }

    /// Get the text style in this element style.
    pub fn text_style(&self) -> Option<&TextStyleRefinement> {
        if self.text.is_some() {
            Some(&self.text)
        } else {
            None
        }
    }

    /// Text refinements with the background positioning box resolved for paint.
    /// Layout uses `text_style` because this scope must not change glyph shaping.
    pub fn text_style_for_bounds(&self, bounds: Bounds<Pixels>, window: &Window) -> Option<TextStyleRefinement> {
        let mut text = self.text.clone();
        if let Some(fill) = &self.vn_text_fill {
            text.vn_text_paint = Some(window.vn_text_paint(fill.clone(), bounds));
        }
        text.is_some().then_some(text)
    }

    /// vue-native fork: the corner radii this style paints on a box of `size`,
    /// percentages, the overlap rule and all. See [`vn_resolve_corner_radii`].
    pub fn corner_radii_for(&self, size: Size<Pixels>, rem_size: Pixels) -> Corners<Pixels> {
        vn_resolve_corner_radii(&self.corner_radii, &self.vn_corner_radii, size, rem_size)
    }

    /// Get the content mask for this element style, based on the given bounds.
    /// If the element does not hide its overflow, this will return `None`.
    pub fn overflow_mask(
        &self,
        bounds: Bounds<Pixels>,
        rem_size: Pixels,
    ) -> Option<ContentMask<Pixels>> {
        match self.overflow {
            Point {
                x: Overflow::Visible,
                y: Overflow::Visible,
            } => None,
            _ => {
                let mut min = bounds.origin;
                let mut max = bounds.bottom_right();
                let border_box = bounds;

                // vue-native fork: side by side, since each side can have its
                // own colour (`vn_border_colors`). Only the shared colour was
                // asked, so a box whose borders were all coloured per side
                // clipped to its border box and its content painted over them.
                let painted = self
                    .vn_resolved_border_colors()
                    .map(|color| !color.is_transparent());
                if painted.left {
                    min.x += self.border_widths.left.to_pixels(rem_size);
                }
                if painted.right {
                    max.x -= self.border_widths.right.to_pixels(rem_size);
                }
                if painted.top {
                    min.y += self.border_widths.top.to_pixels(rem_size);
                }
                if painted.bottom {
                    max.y -= self.border_widths.bottom.to_pixels(rem_size);
                }

                let bounds = match (
                    self.overflow.x == Overflow::Visible,
                    self.overflow.y == Overflow::Visible,
                ) {
                    // x and y both visible
                    (true, true) => return None,
                    // x visible, y hidden
                    (true, false) => Bounds::from_corners(
                        point(min.x, bounds.origin.y),
                        point(max.x, bounds.bottom_right().y),
                    ),
                    // x hidden, y visible
                    (false, true) => Bounds::from_corners(
                        point(bounds.origin.x, min.y),
                        point(bounds.bottom_right().x, max.y),
                    ),
                    // both hidden
                    (false, false) => Bounds::from_corners(min, max),
                };

                // vue-native fork: CSS clips overflow to the padding box's
                // ROUNDED shape, whose radii are the border radii less the
                // border widths. Each corner takes the larger of its two
                // borders off, a circle inside the elliptical corner CSS draws
                // for unequal ones. The insets are read off the mask's own
                // bounds, so a border the mask does not inset (a transparent
                // one, above) takes nothing off.
                let insets = Edges {
                    top: bounds.top() - border_box.top(),
                    right: border_box.right() - bounds.right(),
                    bottom: border_box.bottom() - bounds.bottom(),
                    left: bounds.left() - border_box.left(),
                };
                let outer = self.corner_radii_for(border_box.size, rem_size);
                let inner = |radius: Pixels, a: Pixels, b: Pixels| (radius - a.max(b)).max(px(0.));
                let corner_radii = Corners {
                    top_left: inner(outer.top_left, insets.top, insets.left),
                    top_right: inner(outer.top_right, insets.top, insets.right),
                    bottom_right: inner(outer.bottom_right, insets.bottom, insets.right),
                    bottom_left: inner(outer.bottom_left, insets.bottom, insets.left),
                }
                .clamp_radii_for_quad_size(bounds.size);

                Some(self.vn_shaped_mask(border_box, bounds, corner_radii, rem_size))
            }
        }
    }

    /// vue-native fork: the content mask for `rect` inside the border box `border_box`, whose
    /// round corners are `round_radii`. A corner with a CSS `corner-shape` is the inner edge of
    /// a border as wide as `rect`'s inset from the border box there (css-borders-4 §3.9.4).
    fn vn_shaped_mask(
        &self,
        border_box: Bounds<Pixels>,
        rect: Bounds<Pixels>,
        round_radii: Corners<Pixels>,
        rem_size: Pixels,
    ) -> ContentMask<Pixels> {
        let shapes = &self.vn_corner_shapes;
        if *shapes == Corners::default() {
            return ContentMask { bounds: rect, corner_radii: round_radii, corner_shapes: CornerShapes::default() };
        }
        let outer = self.corner_radii_for(border_box.size, rem_size);
        let pick = |round: Pixels, outer: Pixels, shape: CornerShape| if shape.is_round() { round } else { outer };
        let corner_radii = Corners {
            top_left: pick(round_radii.top_left, outer.top_left, shapes.top_left),
            top_right: pick(round_radii.top_right, outer.top_right, shapes.top_right),
            bottom_right: pick(round_radii.bottom_right, outer.bottom_right, shapes.bottom_right),
            bottom_left: pick(round_radii.bottom_left, outer.bottom_left, shapes.bottom_left),
        };
        let inset = |a: Pixels, b: Pixels| f32::from(a - b).max(0.);
        let (left, top) = (inset(rect.left(), border_box.left()), inset(rect.top(), border_box.top()));
        let (right, bottom) =
            (inset(border_box.right(), rect.right()), inset(border_box.bottom(), rect.bottom()));
        let insets = Corners {
            top_left: (left, top),
            top_right: (right, top),
            bottom_right: (right, bottom),
            bottom_left: (left, bottom),
        };
        ContentMask {
            bounds: rect,
            corner_radii,
            corner_shapes: CornerShapes::new(shapes).with_insets(insets),
        }
    }

    /// vue-native fork: the mask `clip-path` cuts this element's painting and
    /// hit test to ([`Style::vn_clip_path`]), for the border box `bounds`, in
    /// the element's own frame. `None` without a clip.
    ///
    /// A shape that is no rounded rectangle ([`Style::vn_clip_outline`])
    /// gives the rectangle around its outline here, which culls and cuts the
    /// isolated layer the outline is then composited through.
    pub fn vn_clip_mask(&self, bounds: Bounds<Pixels>, rem_size: Pixels) -> Option<ContentMask<Pixels>> {
        let clip = &self.vn_clip_path;
        let reference = match clip {
            VnClipPath::None => return None,
            VnClipPath::Polygon { .. } | VnClipPath::Path { .. } => {
                let outline = self.vn_clip_outline(bounds, rem_size)?;
                let rect = outline.control_bounds().unwrap_or(Bounds { origin: bounds.origin, size: Size::default() });
                return Some(ContentMask::rect(rect));
            }
            VnClipPath::Box(reference)
            | VnClipPath::Inset { reference, .. }
            | VnClipPath::Ellipse { reference, .. } => *reference,
        };
        let (rect, rounded) = self.vn_clip_box(reference, bounds, rem_size);
        let (w, h) = (rect.size.width, rect.size.height);
        let resolve = |length: &DefiniteLength, base: Pixels| length.to_pixels(AbsoluteLength::Pixels(base), rem_size);
        Some(match clip {
            VnClipPath::None | VnClipPath::Polygon { .. } | VnClipPath::Path { .. } => return None,
            VnClipPath::Box(VnClipBox::Margin) => ContentMask { bounds: rect, corner_radii: rounded, ..Default::default() },
            VnClipPath::Box(_) => self.vn_shaped_mask(bounds, rect, rounded, rem_size),
            VnClipPath::Inset { insets, radii, .. } => {
                // Chromium's `BasicShapeInset::GetPath`: the insets come off
                // the reference box, an empty side stays empty, and the radii
                // resolve against the reference box and are then scaled down
                // to fit the inset rectangle (css-backgrounds-3 §5.5).
                let [top, right, bottom, left] = insets;
                let (top, right) = (resolve(top, h), resolve(right, w));
                let (bottom, left) = (resolve(bottom, h), resolve(left, w));
                let shape = Bounds {
                    origin: point(rect.left() + left, rect.top() + top),
                    size: size((w - left - right).max(px(0.)), (h - top - bottom).max(px(0.))),
                };
                let pairs = radii.map(|r| {
                    let x = f32::from(resolve(&r.x, w)).max(0.);
                    let y = f32::from(resolve(&r.y, h)).max(0.);
                    (x, y)
                });
                let [tl, tr, br, bl] = pairs;
                let (sw, sh) = (f32::from(shape.size.width), f32::from(shape.size.height));
                let ratio = |side: f32, a: f32, b: f32| if a + b > side { side / (a + b) } else { 1. };
                let factor = ratio(sw, tl.0, tr.0)
                    .min(ratio(sw, bl.0, br.0))
                    .min(ratio(sh, tl.1, bl.1))
                    .min(ratio(sh, tr.1, br.1));
                let circle = |(x, y): (f32, f32)| px(x.min(y) * factor);
                let corner_radii = Corners {
                    top_left: circle(tl),
                    top_right: circle(tr),
                    bottom_right: circle(br),
                    bottom_left: circle(bl),
                }
                .clamp_radii_for_quad_size(shape.size);
                ContentMask { bounds: shape, corner_radii, ..Default::default() }
            }
            VnClipPath::Ellipse { center, radius, circle, .. } => {
                let (c, rx, ry) = vn_clip_ellipse(rect, center, radius, *circle, rem_size);
                let shape = Bounds { origin: point(c.x - rx, c.y - ry), size: size(rx * 2., ry * 2.) };
                // A circle is the rounded mask; an ellipse is its box here,
                // and its outline in an isolated layer.
                let corner_radii = if rx == ry { Corners::all(rx) } else { Corners::default() };
                ContentMask { bounds: shape, corner_radii, ..Default::default() }
            }
        })
    }

    /// vue-native fork: the outline of a `clip-path` that is no rounded
    /// rectangle (`polygon()`, `path()`, an ellipse whose radii differ), laid
    /// against its reference box for the border box `bounds`, in the
    /// element's own frame. `None` for no clip and for a shape
    /// [`Style::vn_clip_mask`] carries alone. The element paints into an
    /// isolated layer composited through it ([`Window::vn_with_clip_shape`]).
    pub fn vn_clip_outline(&self, bounds: Bounds<Pixels>, rem_size: Pixels) -> Option<VnClipOutline> {
        let resolve = |length: &DefiniteLength, base: Pixels| {
            f32::from(length.to_pixels(AbsoluteLength::Pixels(base), rem_size))
        };
        match &self.vn_clip_path {
            VnClipPath::Polygon { reference, fill_rule, points } => {
                let (rect, _) = self.vn_clip_box(*reference, bounds, rem_size);
                let (x0, y0) = (f32::from(rect.left()), f32::from(rect.top()));
                let (w, h) = (rect.size.width, rect.size.height);
                let mut verbs = Vec::with_capacity(points.len() + 1);
                for (i, [x, y]) in points.iter().enumerate() {
                    let p = [x0 + resolve(x, w), y0 + resolve(y, h)];
                    verbs.push(if i == 0 { VnPathVerb::MoveTo(p) } else { VnPathVerb::LineTo(p) });
                }
                if !verbs.is_empty() {
                    verbs.push(VnPathVerb::Close);
                }
                Some(VnClipOutline { fill_rule: *fill_rule, verbs })
            }
            VnClipPath::Path { reference, fill_rule, verbs } => {
                let (rect, _) = self.vn_clip_box(*reference, bounds, rem_size);
                let (x0, y0) = (f32::from(rect.left()), f32::from(rect.top()));
                let at = |p: [f32; 2]| [p[0] + x0, p[1] + y0];
                let verbs = verbs
                    .iter()
                    .map(|v| match *v {
                        VnPathVerb::MoveTo(p) => VnPathVerb::MoveTo(at(p)),
                        VnPathVerb::LineTo(p) => VnPathVerb::LineTo(at(p)),
                        VnPathVerb::QuadTo(c, p) => VnPathVerb::QuadTo(at(c), at(p)),
                        VnPathVerb::CubicTo(c1, c2, p) => VnPathVerb::CubicTo(at(c1), at(c2), at(p)),
                        VnPathVerb::Close => VnPathVerb::Close,
                    })
                    .collect();
                Some(VnClipOutline { fill_rule: *fill_rule, verbs })
            }
            VnClipPath::Ellipse { reference, center, radius, circle } => {
                let (rect, _) = self.vn_clip_box(*reference, bounds, rem_size);
                let (c, rx, ry) = vn_clip_ellipse(rect, center, radius, *circle, rem_size);
                if rx == ry {
                    return None;
                }
                // Four cubics, one per quadrant, k = 4/3 (sqrt(2) - 1).
                let (cx, cy, rx, ry) = (f32::from(c.x), f32::from(c.y), f32::from(rx), f32::from(ry));
                let (kx, ky) = (0.552_284_8 * rx, 0.552_284_8 * ry);
                use VnPathVerb::*;
                Some(VnClipOutline {
                    fill_rule: VnFillRule::NonZero,
                    verbs: vec![
                        MoveTo([cx + rx, cy]),
                        CubicTo([cx + rx, cy + ky], [cx + kx, cy + ry], [cx, cy + ry]),
                        CubicTo([cx - kx, cy + ry], [cx - rx, cy + ky], [cx - rx, cy]),
                        CubicTo([cx - rx, cy - ky], [cx - kx, cy - ry], [cx, cy - ry]),
                        CubicTo([cx + kx, cy - ry], [cx + rx, cy - ky], [cx + rx, cy]),
                        Close,
                    ],
                })
            }
            VnClipPath::None | VnClipPath::Box(_) | VnClipPath::Inset { .. } => None,
        }
    }

    /// vue-native fork: a `clip-path` reference box and its rounded corners,
    /// for the border box `bounds`. Inside the border box a corner loses the
    /// larger of its two insets, as `overflow_mask` rounds the padding box;
    /// the margin box's grows by the larger margin, where it is rounded at
    /// all. A percentage padding or margin resolves against the element's own
    /// width, not its containing block's.
    fn vn_clip_box(&self, reference: VnClipBox, bounds: Bounds<Pixels>, rem_size: Pixels) -> (Bounds<Pixels>, Corners<Pixels>) {
        let outer = self.corner_radii_for(bounds.size, rem_size);
        let width = AbsoluteLength::Pixels(bounds.size.width);
        let edges = match reference {
            VnClipBox::Border => return (bounds, outer),
            VnClipBox::Margin => {
                let margin = |l: &Length| match l {
                    Length::Definite(l) => -l.to_pixels(width, rem_size),
                    Length::Auto | Length::VnCalcSize(_) => px(0.),
                };
                Edges {
                    top: margin(&self.margin.top),
                    right: margin(&self.margin.right),
                    bottom: margin(&self.margin.bottom),
                    left: margin(&self.margin.left),
                }
            }
            VnClipBox::Padding | VnClipBox::Content => {
                let border = self.border_widths.to_pixels(rem_size);
                if reference == VnClipBox::Padding {
                    border
                } else {
                    let padding = self.padding.to_pixels(size(width, width), rem_size);
                    Edges {
                        top: border.top + padding.top,
                        right: border.right + padding.right,
                        bottom: border.bottom + padding.bottom,
                        left: border.left + padding.left,
                    }
                }
            }
        };
        let rect = Bounds::from_corners(
            point(bounds.left() + edges.left, bounds.top() + edges.top),
            point(
                (bounds.right() - edges.right).max(bounds.left() + edges.left),
                (bounds.bottom() - edges.bottom).max(bounds.top() + edges.top),
            ),
        );
        // A negative inset is an outset (the margin box).
        let corner = |radius: Pixels, a: Pixels, b: Pixels| {
            if a < px(0.) || b < px(0.) {
                if radius > px(0.) { radius - a.min(b) } else { px(0.) }
            } else {
                (radius - a.max(b)).max(px(0.))
            }
        };
        let corner_radii = Corners {
            top_left: corner(outer.top_left, edges.top, edges.left),
            top_right: corner(outer.top_right, edges.top, edges.right),
            bottom_right: corner(outer.bottom_right, edges.bottom, edges.right),
            bottom_left: corner(outer.bottom_left, edges.bottom, edges.left),
        }
        .clamp_radii_for_quad_size(rect.size);
        (rect, corner_radii)
    }

    /// The device-space matrix this style's CSS `transform` produces for an
    /// element laid out at `bounds`, or the identity when there is none.
    ///
    /// Shared by paint (which pushes it onto the primitives) and prepaint
    /// (which records it on the hitbox), so the picture and the hit test can
    /// never disagree about where the element is.
    pub fn transform_matrix(&self, bounds: Bounds<Pixels>, scale_factor: f32) -> TransformationMatrix
    {
        self.vn_transform
            .filter(|t| !t.is_identity())
            .map(|t| t.to_matrix(bounds, scale_factor))
            .unwrap_or_else(TransformationMatrix::unit)
    }

    /// Paints the background of an element styled with this style.
    pub fn paint(
        &self,
        bounds: Bounds<Pixels>,
        window: &mut Window,
        cx: &mut App,
        continuation: impl FnOnce(&mut Window, &mut App),
    ) {
        // CSS `transform` applies to this element's whole painted output AND
        // to its subtree, so it is pushed around everything below — including
        // `continuation`, which is where the children paint. Layout has already
        // happened at this point and is deliberately not consulted again: a
        // transform never moves a box, only its picture.
        //
        // vue-native fork: `clip-path` cuts everything below too, in the
        // element's transformed frame (`Style::vn_clip_mask`).
        //
        // A shape that is no rounded rectangle paints into an isolated layer
        // composited through it (`Window::vn_with_clip_shape`), inside its
        // box's mask.
        // A captured element paints untransformed; its transform travels with
        // the capture (`Window::vn_with_capture`).
        let transformation = if self.vn_capture.is_some() {
            TransformationMatrix::unit()
        } else {
            self.transform_matrix(bounds, window.scale_factor())
        };
        let clip = self.vn_clip_mask(bounds, window.rem_size());
        let outline = self.vn_clip_outline(bounds, window.rem_size());
        let element = window.vn_take_effect_element();
        let paint = |window: &mut Window| {
            window.with_content_mask(clip, |window| {
                window.vn_with_clip_shape(outline.as_ref(), |window| {
                    if let Some(tint) = self.vn_mask_tint()
                        && let Some(paint) = self.vn_mask.as_ref().and_then(|mask| mask.paint.clone())
                    {
                        if !tint.is_transparent() {
                            window.with_element_opacity(Some(tint.a), |window| {
                                paint(bounds, Some(Hsla { a: 1., ..tint }), window, cx)
                            });
                        }
                        return continuation(window, cx);
                    }
                    window.vn_with_mask(self.vn_mask.as_ref(), bounds, cx, |window, cx| {
                        window.vn_with_color_matrix(self.vn_color_matrix, |window| {
                            window.vn_with_isolation(self.vn_isolate, |window| {
                                self.paint_shaded(bounds, element, window, cx, continuation)
                            })
                        })
                    })
                })
            })
        };
        if !transformation.is_unit() {
            return window.with_element_transform(transformation, paint);
        }
        paint(window)
    }

    /// The colour a [`VnMask::tint_leaf`] mask paints in, when nothing but a
    /// solid background shows under it.
    fn vn_mask_tint(&self) -> Option<Hsla> {
        let mask = self.vn_mask.as_ref()?;
        if !mask.tint_leaf
            || mask.luminance
            || self.vn_color_matrix.is_some()
            || !self.background_layers.is_empty()
            || !self.box_shadow.is_empty()
            || self.is_border_visible()
            || self.backdrop_blur.is_some_and(|radius| radius > px(0.))
            || self.vn_effect.is_some()
            || self.vn_effect_fade.is_some()
            || self.vn_mask_ring
            || self.vn_outline.is_some_and(|o| o.width > px(0.) && !o.color.is_transparent())
        {
            return None;
        }
        match self.background.as_ref().and_then(Fill::color) {
            None => Some(Hsla::transparent_black()),
            Some(background) => background.as_solid(),
        }
    }

    /// The `--shading` effects this style paints, the outgoing one of a
    /// cross-fade first, each with its share of the element's opacity.
    fn vn_effects(&self, cx: &App) -> [Option<(EffectSpec, f32)>; 2] {
        // An effect whose output moves with `time` (an animated grain) keeps
        // asking for the next frame. That is decorative motion, so it honours
        // `App::reduce_motion` here — clearing the flag stops the repaint
        // request, which freezes the effect on its current frame rather than
        // removing it. Same behaviour vn-css's animator gives transitions.
        let reduce_motion = cx.reduce_motion();
        let layer = |mut spec: EffectSpec, weight: f32| {
            (spec.is_some() && !self.vn_mask_ring && weight > 0.).then(|| {
                spec.animated &= !reduce_motion;
                if reduce_motion {
                    spec.clock_mask = 0;
                }
                (spec, weight)
            })
        };
        let progress = self
            .vn_effect_fade
            .map_or(1., |fade| fade.progress.clamp(0., 1.));
        [
            self.vn_effect_fade
                .and_then(|fade| layer(fade.from, 1. - progress)),
            self.vn_effect.and_then(|spec| layer(spec, progress)),
        ]
    }

    /// An effect that reads the element's content (`[SkContent]`) takes
    /// everything the element paints, its subtree included, into a group it
    /// composites in its place; the element's own backdrop filter and
    /// backdrop effects read the page, so they paint before it.
    fn paint_shaded(
        &self,
        bounds: Bounds<Pixels>,
        element: u64,
        window: &mut Window,
        cx: &mut App,
        continuation: impl FnOnce(&mut Window, &mut App),
    ) {
        let effects = self.vn_effects(cx);
        let content = effects
            .iter()
            .flatten()
            .any(|(spec, _)| spec.flags & crate::EFFECT_FLAG_NEEDS_CONTENT != 0);
        if !content {
            return self.paint_untransformed(
                bounds,
                window,
                cx,
                continuation,
                &effects,
                element,
                false,
            );
        }
        let corner_radii = self.corner_radii_for(bounds.size, window.rem_size());
        self.paint_backdrop(bounds, corner_radii, window, cx, &effects, element);
        let shaded: Vec<_> = effects
            .iter()
            .flatten()
            .map(|(spec, weight)| {
                let textures = crate::resolve_effect_textures(spec, window, cx);
                (*spec, *weight, textures)
            })
            .collect();
        let shapes = self.vn_corner_shapes;
        window.vn_with_effect_content(bounds, corner_radii, shapes, &shaded, element, cx, |window, cx| {
            self.paint_untransformed(bounds, window, cx, continuation, &effects, element, true)
        })
    }

    /// Whether an effect of this style reads the element's content, whose
    /// deferred descendants then paint into it ([`Window::vn_with_defer_scope`]).
    pub fn vn_reads_content(&self) -> bool {
        [self.vn_effect, self.vn_effect_fade.map(|fade| fade.from)]
            .into_iter()
            .flatten()
            .any(|spec| spec.is_some() && spec.flags & crate::EFFECT_FLAG_NEEDS_CONTENT != 0)
    }

    /// CSS paints the filtered backdrop underneath the element's own
    /// background, so the blur goes in between the drop shadows and the
    /// background quad. The tint stays transparent — `background-color` is
    /// the quad below.
    ///
    /// A BACKDROP effect (`frost`) replaces the pixels behind the element and
    /// so paints exactly where `backdrop-filter` does. It also *has* to: it
    /// draws with blending disabled and owns every pixel of its quad, so
    /// painting it later would erase the background quad.
    fn paint_backdrop(
        &self,
        bounds: Bounds<Pixels>,
        corner_radii: Corners<Pixels>,
        window: &mut Window,
        cx: &mut App,
        effects: &[Option<(EffectSpec, f32)>; 2],
        element: u64,
    ) {
        if let Some(radius) = self.backdrop_blur
            && radius > crate::px(0.)
            && !self.vn_mask_ring
        {
            window.vn_paint_backdrop_blur_rect(
                bounds,
                corner_radii,
                self.vn_corner_shapes,
                crate::BackdropBlurEffect::new(radius),
            );
        }
        self.paint_effects(bounds, corner_radii, window, cx, effects, element, true);
    }

    /// The effects of one kind that do not read the element's content: the
    /// backdrop ones, or the source-over ones (`noise`, `glow`), which paint
    /// over the background but under the content — the only slot where film
    /// grain is visible on an opaque panel and where a glow's inner rim
    /// survives.
    #[allow(clippy::too_many_arguments)]
    fn paint_effects(
        &self,
        bounds: Bounds<Pixels>,
        corner_radii: Corners<Pixels>,
        window: &mut Window,
        cx: &mut App,
        effects: &[Option<(EffectSpec, f32)>; 2],
        element: u64,
        backdrop: bool,
    ) {
        for (spec, weight) in effects.iter().flatten() {
            if spec.flags & crate::EFFECT_FLAG_NEEDS_CONTENT == 0
                && (spec.flags & crate::EFFECT_FLAG_NEEDS_BACKDROP != 0) == backdrop
            {
                window.with_element_opacity((*weight < 1.).then_some(*weight), |window| {
                    window.vn_paint_textured_effect(
                        bounds,
                        corner_radii,
                        self.vn_corner_shapes,
                        *spec,
                        element,
                        cx,
                    )
                });
            }
        }
    }

    #[allow(clippy::too_many_arguments)]
    fn paint_untransformed(
        &self,
        bounds: Bounds<Pixels>,
        window: &mut Window,
        cx: &mut App,
        continuation: impl FnOnce(&mut Window, &mut App),
        effects: &[Option<(EffectSpec, f32)>; 2],
        element: u64,
        in_content: bool,
    ) {
        #[cfg(debug_assertions)]
        if self.debug_below {
            cx.set_global(DebugBelow)
        }

        #[cfg(debug_assertions)]
        if self.debug || cx.has_global::<DebugBelow>() {
            window.paint_quad(crate::outline(bounds, crate::red(), BorderStyle::default()));
        }

        let rem_size = window.rem_size();
        let corner_radii = self.corner_radii_for(bounds.size, rem_size);

        // vue-native fork: `vn_mask_ring` paints the background only in the
        // padding and border area. These are the border widths of the quads
        // that do it: the border plus the padding. A percentage padding
        // resolves against this box's width (see the field's doc).
        let ring_widths = self.vn_mask_ring.then(|| {
            let border = self.border_widths.to_pixels(rem_size);
            let width = AbsoluteLength::Pixels(bounds.size.width);
            let padding = self.padding.to_pixels(size(width, width), rem_size);
            Edges {
                top: border.top + padding.top,
                right: border.right + padding.right,
                bottom: border.bottom + padding.bottom,
                left: border.left + padding.left,
            }
        });
        let ring = ring_widths.is_some();

        if !ring {
            window.vn_paint_drop_shadows(
                bounds,
                corner_radii,
                self.vn_corner_shapes,
                &self.box_shadow,
                self.vn_shadow_under_box,
            );
        }

        if !in_content {
            self.paint_backdrop(bounds, corner_radii, window, cx, effects, element);
        }
        // The background quads: the whole box, or only the ring.
        let fill = |background: Background| match ring_widths {
            Some(widths) => (background.ring(), widths),
            None => (background, Edges::default()),
        };

        let background_color = self.background.as_ref().and_then(Fill::color);
        if background_color.is_some_and(|color| !color.is_transparent()) {
            let color = background_color.unwrap_or_default();
            let mut border_color = representative_color(&color);
            border_color.a = 0.;
            let (color, widths) = fill(color);
            window.paint_quad(quad(
                bounds,
                corner_radii,
                color,
                widths,
                border_color,
                self.border_style,
            ).corner_shapes(self.vn_corner_shapes));
        }

        // The `background-image` stack, bottom layer first, each one its own
        // quad over the colour below it. The AA-matching transparent border
        // trick above is deliberately NOT repeated per layer: it exists to stop
        // the bottom fill's antialiased corner showing a seam against the
        // border, and a second quad carrying it would double-blend that seam.
        for layer in &self.background_layers {
            if layer.is_transparent() {
                continue;
            }
            let mut border_color = representative_color(layer);
            border_color.a = 0.;
            let (layer, widths) = fill(*layer);
            window.paint_quad(quad(
                bounds,
                corner_radii,
                layer,
                widths,
                border_color,
                self.border_style,
            ).corner_shapes(self.vn_corner_shapes));
        }

        if !ring {
            window.vn_paint_inset_shadows(bounds, corner_radii, self.vn_corner_shapes, &self.box_shadow);
        }

        self.paint_effects(bounds, corner_radii, window, cx, effects, element, false);

        continuation(window, cx);

        if self.is_border_visible() {
            let border_widths = self.border_widths.to_pixels(rem_size);
            let colors = self.vn_resolved_border_colors();
            if colors.top == colors.right && colors.top == colors.bottom && colors.top == colors.left {
                window.paint_quad(quad(
                    bounds, corner_radii, Hsla::transparent_black(), border_widths,
                    colors.top, self.border_style,
                ).corner_shapes(self.vn_corner_shapes));
            } else {
                // Reuse the rounded/dashed border shader, partitioned along
                // the joins between outer and inner corners. Rectangular
                // strips would give rounded corners the wrong side colour.
                let clips = Self::vn_border_side_clips(bounds, border_widths);
                for (points, (color, width)) in clips.into_iter().zip([
                    (colors.top, border_widths.top), (colors.right, border_widths.right),
                    (colors.bottom, border_widths.bottom), (colors.left, border_widths.left),
                ]) {
                    if width <= px(0.) || color.is_transparent() || points.len() < 3 { continue; }
                    let mut verbs: Vec<_> = points.into_iter().enumerate().map(|(index, point)| {
                        if index == 0 { VnPathVerb::MoveTo(point) } else { VnPathVerb::LineTo(point) }
                    }).collect();
                    verbs.push(VnPathVerb::Close);
                    let outline = VnClipOutline { fill_rule: VnFillRule::NonZero, verbs };
                    window.vn_with_clip_shape(Some(&outline), |window| {
                        window.paint_quad(quad(bounds, corner_radii, Hsla::transparent_black(),
                            border_widths, color, self.border_style).corner_shapes(self.vn_corner_shapes));
                    });
                }
            }
        }

        // `outline` LAST, and at dilated bounds: CSS paints it outside the
        // border box, over everything, and it takes no layout space — so it is
        // one extra border-only quad rather than anything the box itself knows
        // about. `dilate` by (offset + width) puts the ring's INNER edge at
        // `offset` from the border box and its outer edge `width` beyond that,
        // which is what `outline-offset` means.
        //
        // The corner radius grows with the dilation, so the ring stays
        // concentric with a rounded box — the same rule Chromium applies.
        if let Some(outline) = self
            .vn_outline
            .filter(|o| o.width > px(0.) && !o.color.is_transparent() && !ring)
        {
            let grow = outline.offset + outline.width;
            let ring = bounds.dilate(grow);
            // Per corner: a square corner stays square (CSS does not round an
            // outline the box did not round), a rounded one grows with the ring.
            let radii = corner_radii.map(|r| {
                if r.0 <= 0.0 {
                    px(0.)
                } else {
                    px((r.0 + grow.0).max(0.0))
                }
            });
            window.paint_quad(quad(
                ring,
                radii,
                Hsla::transparent_black(),
                Edges::all(outline.width),
                outline.color,
                outline.style,
            ).corner_shapes(self.vn_corner_shapes));
        }

        #[cfg(debug_assertions)]
        if self.debug_below {
            cx.remove_global::<DebugBelow>();
        }
    }

    fn vn_resolved_border_colors(&self) -> Edges<Hsla> {
        let shared = self.border_color.unwrap_or_default();
        self.vn_border_colors.map(|color| color.unwrap_or(shared))
    }

    fn vn_border_side_clips(bounds: Bounds<Pixels>, widths: Edges<Pixels>) -> [Vec<[f32; 2]>; 4] {
        let (left, top, right, bottom) = (f32::from(bounds.left()), f32::from(bounds.top()),
            f32::from(bounds.right()), f32::from(bounds.bottom()));
        let widths = [widths.top, widths.right, widths.bottom, widths.left].map(|width| f32::from(width).max(0.));
        let distances = [[0., 1., -top], [-1., 0., right], [0., -1., bottom], [1., 0., -left]];
        std::array::from_fn(|side| {
            if widths[side] <= 0. { return Vec::new(); }
            let mut polygon = vec![[left, top], [right, top], [right, bottom], [left, bottom]];
            // Partition the FULL quad by distance / border width. Stopping
            // at inner rectangular corners cuts holes out of rounded arcs.
            // These bisectors keep the CSS joins through the inner corners
            // while extending far enough to cover circles and large radii.
            for other in 0..4 {
                if other == side || widths[other] <= 0. { continue; }
                let plane: [f32; 3] = std::array::from_fn(|index|
                    distances[side][index] * widths[other] - distances[other][index] * widths[side]);
                let distance = |point: [f32; 2]| plane[0] * point[0] + plane[1] * point[1] + plane[2];
                let Some(mut previous) = polygon.last().copied() else { break };
                let mut previous_distance = distance(previous);
                let mut clipped = Vec::with_capacity(polygon.len() + 1);
                for current in polygon {
                    let current_distance = distance(current);
                    if (previous_distance <= 0.) != (current_distance <= 0.) {
                        let fraction = previous_distance / (previous_distance - current_distance);
                        clipped.push([previous[0] + fraction * (current[0] - previous[0]),
                            previous[1] + fraction * (current[1] - previous[1])]);
                    }
                    if current_distance <= 0. { clipped.push(current); }
                    previous = current;
                    previous_distance = current_distance;
                }
                polygon = clipped;
            }
            polygon
        })
    }

    fn is_border_visible(&self) -> bool {
        self.vn_resolved_border_colors().any(|color| !color.is_transparent())
            && self.border_widths.any(|length| !length.is_zero())
    }
}

impl Default for Style {
    fn default() -> Self {
        Style {
            display: Display::Block,
            visibility: Visibility::Visible,
            overflow: Point {
                x: Overflow::Visible,
                y: Overflow::Visible,
            },
            allow_concurrent_scroll: false,
            restrict_scroll_to_axis: false,
            scrollbar_width: AbsoluteLength::default(),
            position: Position::Relative,
            inset: Edges::auto(),
            margin: Edges::<Length>::zero(),
            padding: Edges::<DefiniteLength>::zero(),
            border_widths: Edges::<AbsoluteLength>::zero(),
            size: Size::auto(),
            min_size: Size::auto(),
            max_size: Size::auto(),
            aspect_ratio: None,
            gap: Size::default(),
            // Alignment
            align_items: None,
            align_self: None,
            align_content: None,
            justify_content: None,
            // Flexbox
            flex_direction: FlexDirection::Row,
            flex_wrap: FlexWrap::NoWrap,
            flex_grow: 0.0,
            flex_shrink: 1.0,
            vn_order: 0,
            vn_object_position: None,
            flex_basis: Length::Auto,
            background: None,
            background_layers: Vec::new(),
            border_color: None,
            vn_border_colors: Edges::default(),
            border_style: BorderStyle::default(),
            corner_radii: Corners::default(),
            box_shadow: Default::default(),
            vn_shadow_under_box: false,
            backdrop_blur: None,
            vn_effect: None,
            vn_effect_fade: None,
            vn_transform: None,
            text: TextStyleRefinement::default(),
            mouse_cursor: None,
            opacity: None,
            grid_rows: None,
            grid_cols: None,
            grid_location: None,
            vn_grid_cols: None,
            vn_grid_rows: None,
            vn_grid_cols_auto: None,
            vn_grid_rows_auto: None,
            vn_grid_auto_cols: None,
            vn_grid_auto_rows: None,
            vn_grid_auto_flow: None,
            vn_justify_items: None,
            vn_justify_self: None,
            vn_outline: None,
            vn_mask_ring: false,
            vn_clip_path: VnClipPath::None,
            vn_color_matrix: None,
            vn_capture: None,
            vn_isolate: false,
            vn_contain_size: 0,
            vn_text_fill: None,
            vn_mask: None,
            vn_corner_radii: Corners::default(),
            vn_corner_shapes: Corners::default(),

            #[cfg(debug_assertions)]
            debug: false,
            #[cfg(debug_assertions)]
            debug_below: false,
        }
    }
}

/// The properties that can be applied to an underline.
#[derive(Refineable, Copy, Clone, Default, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
pub struct UnderlineStyle {
    /// The thickness of the underline.
    pub thickness: Pixels,

    /// The color of the underline.
    pub color: Option<Hsla>,

    /// Whether the underline should be wavy, like in a spell checker.
    pub wavy: bool,

    /// How far BELOW its default position the line sits — CSS
    /// `text-underline-offset`. Zero keeps the position the font's own metrics
    /// put it at, which is what every existing caller gets.
    pub offset: Pixels,
}

/// The properties that can be applied to a strikethrough.
#[derive(Refineable, Copy, Clone, Default, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
pub struct StrikethroughStyle {
    /// The thickness of the strikethrough.
    pub thickness: Pixels,

    /// The color of the strikethrough.
    pub color: Option<Hsla>,
}

/// The kinds of fill that can be applied to a shape.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
pub enum Fill {
    /// A solid color fill.
    Color(Background),
}

impl Fill {
    /// Unwrap this fill into a solid color, if it is one.
    ///
    /// If the fill is not a solid color, this method returns `None`.
    pub fn color(&self) -> Option<Background> {
        match self {
            Fill::Color(color) => Some(*color),
        }
    }
}

impl Default for Fill {
    fn default() -> Self {
        Self::Color(Background::default())
    }
}

impl From<Hsla> for Fill {
    fn from(color: Hsla) -> Self {
        Self::Color(color.into())
    }
}

impl From<Rgba> for Fill {
    fn from(color: Rgba) -> Self {
        Self::Color(color.into())
    }
}

impl From<Background> for Fill {
    fn from(background: Background) -> Self {
        Self::Color(background)
    }
}

impl From<TextStyle> for HighlightStyle {
    fn from(other: TextStyle) -> Self {
        Self::from(&other)
    }
}

impl From<&TextStyle> for HighlightStyle {
    fn from(other: &TextStyle) -> Self {
        Self {
            color: Some(other.color),
            font_weight: Some(other.font_weight),
            font_style: Some(other.font_style),
            background_color: other.background_color,
            underline: other.underline,
            strikethrough: other.strikethrough,
            fade_out: None,
        }
    }
}

impl HighlightStyle {
    /// Create a highlight style with just a color
    pub fn color(color: Hsla) -> Self {
        Self {
            color: Some(color),
            ..Default::default()
        }
    }
    /// Blend this highlight style with another.
    /// Non-continuous properties, like font_weight and font_style, are overwritten.
    #[must_use]
    pub fn highlight(self, other: HighlightStyle) -> Self {
        Self {
            color: other
                .color
                .map(|other_color| {
                    if let Some(color) = self.color {
                        color.blend(other_color)
                    } else {
                        other_color
                    }
                })
                .or(self.color),
            font_weight: other.font_weight.or(self.font_weight),
            font_style: other.font_style.or(self.font_style),
            background_color: other.background_color.or(self.background_color),
            underline: other.underline.or(self.underline),
            strikethrough: other.strikethrough.or(self.strikethrough),
            fade_out: other
                .fade_out
                .map(|source_fade| {
                    self.fade_out
                        .map(|dest_fade| (dest_fade * (1. + source_fade)).clamp(0., 1.))
                        .unwrap_or(source_fade)
                })
                .or(self.fade_out),
        }
    }
}

impl From<Hsla> for HighlightStyle {
    fn from(color: Hsla) -> Self {
        Self {
            color: Some(color),
            ..Default::default()
        }
    }
}

impl From<FontWeight> for HighlightStyle {
    fn from(font_weight: FontWeight) -> Self {
        Self {
            font_weight: Some(font_weight),
            ..Default::default()
        }
    }
}

impl From<FontStyle> for HighlightStyle {
    fn from(font_style: FontStyle) -> Self {
        Self {
            font_style: Some(font_style),
            ..Default::default()
        }
    }
}

impl From<Rgba> for HighlightStyle {
    fn from(color: Rgba) -> Self {
        Self {
            color: Some(color.into()),
            ..Default::default()
        }
    }
}

/// Combine and merge the highlights and ranges in the two iterators.
pub fn combine_highlights(
    a: impl IntoIterator<Item = (Range<usize>, HighlightStyle)>,
    b: impl IntoIterator<Item = (Range<usize>, HighlightStyle)>,
) -> impl Iterator<Item = (Range<usize>, HighlightStyle)> {
    let mut endpoints = Vec::new();
    let mut highlights = Vec::new();
    for (range, highlight) in a.into_iter().chain(b) {
        if !range.is_empty() {
            let highlight_id = highlights.len();
            endpoints.push((range.start, highlight_id, true));
            endpoints.push((range.end, highlight_id, false));
            highlights.push(highlight);
        }
    }
    endpoints.sort_unstable_by_key(|(position, _, _)| *position);
    let mut endpoints = endpoints.into_iter().peekable();

    let mut active_styles = HashSet::default();
    let mut ix = 0;
    iter::from_fn(move || {
        while let Some((endpoint_ix, highlight_id, is_start)) = endpoints.peek() {
            let prev_index = mem::replace(&mut ix, *endpoint_ix);
            if ix > prev_index && !active_styles.is_empty() {
                let current_style = active_styles
                    .iter()
                    .fold(HighlightStyle::default(), |acc, highlight_id| {
                        acc.highlight(highlights[*highlight_id])
                    });
                return Some((prev_index..ix, current_style));
            }

            if *is_start {
                active_styles.insert(*highlight_id);
            } else {
                active_styles.remove(highlight_id);
            }
            endpoints.next();
        }
        None
    })
}

/// Used to control how child nodes are aligned.
/// For Flexbox it controls alignment in the cross axis
/// For Grid it controls alignment in the block axis
///
/// [MDN](https://developer.mozilla.org/en-US/docs/Web/CSS/align-items)
#[derive(Copy, Clone, PartialEq, Eq, Debug, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
// Copy of taffy::style type of the same name, to derive JsonSchema.
pub enum AlignItems {
    /// Items are packed toward the start of the axis
    Start,
    /// Items are packed toward the end of the axis
    End,
    /// Items are packed towards the flex-relative start of the axis.
    ///
    /// For flex containers with flex_direction RowReverse or ColumnReverse this is equivalent
    /// to End. In all other cases it is equivalent to Start.
    FlexStart,
    /// Items are packed towards the flex-relative end of the axis.
    ///
    /// For flex containers with flex_direction RowReverse or ColumnReverse this is equivalent
    /// to Start. In all other cases it is equivalent to End.
    FlexEnd,
    /// Items are packed along the center of the cross axis
    Center,
    /// Items are aligned such as their baselines align
    Baseline,
    /// Stretch to fill the container
    Stretch,
}
/// Used to control how child nodes are aligned.
/// Does not apply to Flexbox, and will be ignored if specified on a flex container
/// For Grid it controls alignment in the inline axis
///
/// [MDN](https://developer.mozilla.org/en-US/docs/Web/CSS/justify-items)
pub type JustifyItems = AlignItems;
/// Used to control how the specified nodes is aligned.
/// Overrides the parent Node's `AlignItems` property.
/// For Flexbox it controls alignment in the cross axis
/// For Grid it controls alignment in the block axis
///
/// [MDN](https://developer.mozilla.org/en-US/docs/Web/CSS/align-self)
pub type AlignSelf = AlignItems;
/// Used to control how the specified nodes is aligned.
/// Overrides the parent Node's `JustifyItems` property.
/// Does not apply to Flexbox, and will be ignored if specified on a flex child
/// For Grid it controls alignment in the inline axis
///
/// [MDN](https://developer.mozilla.org/en-US/docs/Web/CSS/justify-self)
pub type JustifySelf = AlignItems;

/// Sets the distribution of space between and around content items
/// For Flexbox it controls alignment in the cross axis
/// For Grid it controls alignment in the block axis
///
/// [MDN](https://developer.mozilla.org/en-US/docs/Web/CSS/align-content)
#[derive(Copy, Clone, PartialEq, Eq, Debug, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
// Copy of taffy::style type of the same name, to derive JsonSchema.
pub enum AlignContent {
    /// Items are packed toward the start of the axis
    Start,
    /// Items are packed toward the end of the axis
    End,
    /// Items are packed towards the flex-relative start of the axis.
    ///
    /// For flex containers with flex_direction RowReverse or ColumnReverse this is equivalent
    /// to End. In all other cases it is equivalent to Start.
    FlexStart,
    /// Items are packed towards the flex-relative end of the axis.
    ///
    /// For flex containers with flex_direction RowReverse or ColumnReverse this is equivalent
    /// to Start. In all other cases it is equivalent to End.
    FlexEnd,
    /// Items are centered around the middle of the axis
    Center,
    /// Items are stretched to fill the container
    Stretch,
    /// The first and last items are aligned flush with the edges of the container (no gap)
    /// The gap between items is distributed evenly.
    SpaceBetween,
    /// The gap between the first and last items is exactly THE SAME as the gap between items.
    /// The gaps are distributed evenly
    SpaceEvenly,
    /// The gap between the first and last items is exactly HALF the gap between items.
    /// The gaps are distributed evenly in proportion to these ratios.
    SpaceAround,
    /// vue-native fork: CSS `safe center`. Centred while the content fits, and
    /// packed toward the start when it would overflow. HTML lays a `<button>`'s
    /// content out this way.
    SafeCenter,
}

/// Sets the distribution of space between and around content items
/// For Flexbox it controls alignment in the main axis
/// For Grid it controls alignment in the inline axis
///
/// [MDN](https://developer.mozilla.org/en-US/docs/Web/CSS/justify-content)
pub type JustifyContent = AlignContent;

/// Sets the layout used for the children of this node
///
/// The default values depends on on which feature flags are enabled. The order of precedence is: Flex, Grid, Block, None.
#[derive(Copy, Clone, PartialEq, Eq, Debug, Default, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
// Copy of taffy::style type of the same name, to derive JsonSchema.
pub enum Display {
    /// The children will follow the block layout algorithm
    Block,
    /// The children will follow the flexbox layout algorithm
    #[default]
    Flex,
    /// The children will follow the CSS Grid layout algorithm
    Grid,
    /// The children will not be laid out, and will follow absolute positioning
    None,
}

/// Controls whether flex items are forced onto one line or can wrap onto multiple lines.
///
/// Defaults to [`FlexWrap::NoWrap`]
///
/// [Specification](https://www.w3.org/TR/css-flexbox-1/#flex-wrap-property)
#[derive(Copy, Clone, PartialEq, Eq, Debug, Default, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
// Copy of taffy::style type of the same name, to derive JsonSchema.
pub enum FlexWrap {
    /// Items will not wrap and stay on a single line
    #[default]
    NoWrap,
    /// Items will wrap according to this item's [`FlexDirection`]
    Wrap,
    /// Items will wrap in the opposite direction to this item's [`FlexDirection`]
    WrapReverse,
}

/// The direction of the flexbox layout main axis.
///
/// There are always two perpendicular layout axes: main (or primary) and cross (or secondary).
/// Adding items will cause them to be positioned adjacent to each other along the main axis.
/// By varying this value throughout your tree, you can create complex axis-aligned layouts.
///
/// Items are always aligned relative to the cross axis, and justified relative to the main axis.
///
/// The default behavior is [`FlexDirection::Row`].
///
/// [Specification](https://www.w3.org/TR/css-flexbox-1/#flex-direction-property)
#[derive(Copy, Clone, PartialEq, Eq, Debug, Default, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
// Copy of taffy::style type of the same name, to derive JsonSchema.
pub enum FlexDirection {
    /// Defines +x as the main axis
    ///
    /// Items will be added from left to right in a row.
    #[default]
    Row,
    /// Defines +y as the main axis
    ///
    /// Items will be added from top to bottom in a column.
    Column,
    /// Defines -x as the main axis
    ///
    /// Items will be added from right to left in a row.
    RowReverse,
    /// Defines -y as the main axis
    ///
    /// Items will be added from bottom to top in a column.
    ColumnReverse,
}

/// How children overflowing their container should affect layout
///
/// In CSS the primary effect of this property is to control whether contents of a parent container that overflow that container should
/// be displayed anyway, be clipped, or trigger the container to become a scroll container. However it also has secondary effects on layout,
/// the main ones being:
///
///   - The automatic minimum size Flexbox/CSS Grid items with non-`Visible` overflow is `0` rather than being content based
///   - `Overflow::Scroll` nodes have space in the layout reserved for a scrollbar (width controlled by the `scrollbar_width` property)
///
/// In Taffy, we only implement the layout related secondary effects as we are not concerned with drawing/painting. The amount of space reserved for
/// a scrollbar is controlled by the `scrollbar_width` property. If this is `0` then `Scroll` behaves identically to `Hidden`.
///
/// <https://developer.mozilla.org/en-US/docs/Web/CSS/overflow>
#[derive(Copy, Clone, PartialEq, Eq, Debug, Default, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
// Copy of taffy::style type of the same name, to derive JsonSchema.
pub enum Overflow {
    /// The automatic minimum size of this node as a flexbox/grid item should be based on the size of its content.
    /// Content that overflows this node *should* contribute to the scroll region of its parent.
    #[default]
    Visible,
    /// The automatic minimum size of this node as a flexbox/grid item should be based on the size of its content.
    /// Content that overflows this node should *not* contribute to the scroll region of its parent.
    Clip,
    /// The automatic minimum size of this node as a flexbox/grid item should be `0`.
    /// Content that overflows this node should *not* contribute to the scroll region of its parent.
    Hidden,
    /// The automatic minimum size of this node as a flexbox/grid item should be `0`. Additionally, space should be reserved
    /// for a scrollbar. The amount of space reserved is controlled by the `scrollbar_width` property.
    /// Content that overflows this node should *not* contribute to the scroll region of its parent.
    Scroll,
}

/// The positioning strategy for this item.
///
/// This controls both how the origin is determined for the [`Style::position`] field,
/// and whether or not the item will be controlled by flexbox's layout algorithm.
///
/// WARNING: this enum follows the behavior of [CSS's `position` property](https://developer.mozilla.org/en-US/docs/Web/CSS/position),
/// which can be unintuitive.
///
/// [`Position::Relative`] is the default value, in contrast to the default behavior in CSS.
#[derive(Copy, Clone, PartialEq, Eq, Debug, Default, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
// Copy of taffy::style type of the same name, to derive JsonSchema.
pub enum Position {
    /// The offset is computed relative to the final position given by the layout algorithm.
    /// Offsets do not affect the position of any other items; they are effectively a correction factor applied at the end.
    #[default]
    Relative,
    /// The offset is computed relative to this item's closest positioned ancestor, if any.
    /// Otherwise, it is placed relative to the origin.
    /// No space is created for the item in the page layout, and its size will not be altered.
    ///
    /// WARNING: to opt-out of layouting entirely, you must use [`Display::None`] instead on your [`Style`] object.
    Absolute,
}

impl From<AlignItems> for taffy::style::AlignItems {
    fn from(value: AlignItems) -> Self {
        match value {
            AlignItems::Start => Self::START,
            AlignItems::End => Self::END,
            AlignItems::FlexStart => Self::FLEX_START,
            AlignItems::FlexEnd => Self::FLEX_END,
            AlignItems::Center => Self::CENTER,
            AlignItems::Baseline => Self::BASELINE,
            AlignItems::Stretch => Self::STRETCH,
        }
    }
}

impl From<AlignContent> for taffy::style::AlignContent {
    fn from(value: AlignContent) -> Self {
        match value {
            AlignContent::Start => Self::START,
            AlignContent::End => Self::END,
            AlignContent::FlexStart => Self::FLEX_START,
            AlignContent::FlexEnd => Self::FLEX_END,
            AlignContent::Center => Self::CENTER,
            AlignContent::Stretch => Self::STRETCH,
            AlignContent::SpaceBetween => Self::SPACE_BETWEEN,
            AlignContent::SpaceEvenly => Self::SPACE_EVENLY,
            AlignContent::SpaceAround => Self::SPACE_AROUND,
            AlignContent::SafeCenter => Self::SAFE_CENTER,
        }
    }
}

impl From<Display> for taffy::style::Display {
    fn from(value: Display) -> Self {
        match value {
            Display::Block => Self::Block,
            Display::Flex => Self::Flex,
            Display::Grid => Self::Grid,
            Display::None => Self::None,
        }
    }
}

impl From<FlexWrap> for taffy::style::FlexWrap {
    fn from(value: FlexWrap) -> Self {
        match value {
            FlexWrap::NoWrap => Self::NoWrap,
            FlexWrap::Wrap => Self::Wrap,
            FlexWrap::WrapReverse => Self::WrapReverse,
        }
    }
}

impl From<FlexDirection> for taffy::style::FlexDirection {
    fn from(value: FlexDirection) -> Self {
        match value {
            FlexDirection::Row => Self::Row,
            FlexDirection::Column => Self::Column,
            FlexDirection::RowReverse => Self::RowReverse,
            FlexDirection::ColumnReverse => Self::ColumnReverse,
        }
    }
}

impl From<Overflow> for taffy::style::Overflow {
    fn from(value: Overflow) -> Self {
        match value {
            Overflow::Visible => Self::Visible,
            Overflow::Clip => Self::Clip,
            Overflow::Hidden => Self::Hidden,
            Overflow::Scroll => Self::Scroll,
        }
    }
}

impl From<Position> for taffy::style::Position {
    fn from(value: Position) -> Self {
        match value {
            Position::Relative => Self::Relative,
            Position::Absolute => Self::Absolute,
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::{blue, green, px, red, yellow};

    use super::*;

    use util_macros::perf;

    #[perf]
    fn test_basic_highlight_style_combination() {
        let style_a = HighlightStyle::default();
        let style_b = HighlightStyle::default();
        let style_a = style_a.highlight(style_b);
        assert_eq!(
            style_a,
            HighlightStyle::default(),
            "Combining empty styles should not produce a non-empty style."
        );

        let style_b = HighlightStyle {
            color: Some(red()),
            strikethrough: Some(StrikethroughStyle {
                thickness: px(2.),
                color: Some(blue()),
            }),
            fade_out: Some(0.),
            font_style: Some(FontStyle::Italic),
            font_weight: Some(FontWeight(300.)),
            background_color: Some(yellow()),
            underline: Some(UnderlineStyle {
                thickness: px(2.),
                color: Some(red()),
                wavy: true,
                offset: px(0.),
            }),
        };
        let expected_style = style_b;

        let style_a = style_a.highlight(style_b);
        assert_eq!(
            style_a, expected_style,
            "Blending an empty style with another style should return the other style"
        );

        let style_b = style_b.highlight(Default::default());
        assert_eq!(
            style_b, expected_style,
            "Blending a style with an empty style should not change the style."
        );

        let style_c = expected_style;

        let style_d = HighlightStyle {
            color: Some(blue().alpha(0.7)),
            strikethrough: Some(StrikethroughStyle {
                thickness: px(4.),
                color: Some(crate::red()),
            }),
            fade_out: Some(0.),
            font_style: Some(FontStyle::Oblique),
            font_weight: Some(FontWeight(800.)),
            background_color: Some(green()),
            underline: Some(UnderlineStyle {
                thickness: px(4.),
                color: None,
                wavy: false,
                offset: px(0.),
            }),
        };

        let expected_style = HighlightStyle {
            color: Some(red().blend(blue().alpha(0.7))),
            strikethrough: Some(StrikethroughStyle {
                thickness: px(4.),
                color: Some(red()),
            }),
            // TODO this does not seem right
            fade_out: Some(0.),
            font_style: Some(FontStyle::Oblique),
            font_weight: Some(FontWeight(800.)),
            background_color: Some(green()),
            underline: Some(UnderlineStyle {
                thickness: px(4.),
                color: None,
                wavy: false,
                offset: px(0.),
            }),
        };

        let style_c = style_c.highlight(style_d);
        assert_eq!(
            style_c, expected_style,
            "Blending styles should blend properties where possible and override all others"
        );
    }

    #[perf]
    fn test_combine_highlights() {
        assert_eq!(
            combine_highlights(
                [
                    (0..5, green().into()),
                    (4..10, FontWeight::BOLD.into()),
                    (15..20, yellow().into()),
                ],
                [
                    (2..6, FontStyle::Italic.into()),
                    (1..3, blue().into()),
                    (21..23, red().into()),
                ]
            )
            .collect::<Vec<_>>(),
            [
                (
                    0..1,
                    HighlightStyle {
                        color: Some(green()),
                        ..Default::default()
                    }
                ),
                (
                    1..2,
                    HighlightStyle {
                        color: Some(blue()),
                        ..Default::default()
                    }
                ),
                (
                    2..3,
                    HighlightStyle {
                        color: Some(blue()),
                        font_style: Some(FontStyle::Italic),
                        ..Default::default()
                    }
                ),
                (
                    3..4,
                    HighlightStyle {
                        color: Some(green()),
                        font_style: Some(FontStyle::Italic),
                        ..Default::default()
                    }
                ),
                (
                    4..5,
                    HighlightStyle {
                        color: Some(green()),
                        font_weight: Some(FontWeight::BOLD),
                        font_style: Some(FontStyle::Italic),
                        ..Default::default()
                    }
                ),
                (
                    5..6,
                    HighlightStyle {
                        font_weight: Some(FontWeight::BOLD),
                        font_style: Some(FontStyle::Italic),
                        ..Default::default()
                    }
                ),
                (
                    6..10,
                    HighlightStyle {
                        font_weight: Some(FontWeight::BOLD),
                        ..Default::default()
                    }
                ),
                (
                    15..20,
                    HighlightStyle {
                        color: Some(yellow()),
                        ..Default::default()
                    }
                ),
                (
                    21..23,
                    HighlightStyle {
                        color: Some(red()),
                        ..Default::default()
                    }
                )
            ]
        );
    }

    #[perf]
    fn test_text_style_refinement() {
        let mut style = Style::default();
        style.refine(&StyleRefinement::default().text_size(px(20.0)));
        style.refine(&StyleRefinement::default().font_weight(FontWeight::SEMIBOLD));

        assert_eq!(
            Some(AbsoluteLength::from(px(20.0))),
            style.text_style().unwrap().font_size
        );

        assert_eq!(
            Some(FontWeight::SEMIBOLD),
            style.text_style().unwrap().font_weight
        );
    }
}

/// vue-native fork: `AlignContent::SafeCenter` reaches taffy as `safe center`.
/// A block container centres content that fits and starts content that
/// overflows.
#[cfg(test)]
mod vn_object_position_tests {
    use super::*;
    use crate::{px, size};

    #[test]
    fn the_position_places_the_fitted_image_in_the_free_space() {
        let bounds = Bounds::new(point(px(10.), px(20.)), size(px(100.), px(50.)));
        let square = size(DevicePixels(40), DevicePixels(40));
        let at = |fit: ObjectFit, x: (f32, f32), y: (f32, f32)| {
            let position = ObjectPosition {
                x_fraction: x.0,
                x_offset: px(x.1),
                y_fraction: y.0,
                y_offset: px(y.1),
            };
            let b = fit.get_bounds_at(bounds, square, position);
            (f32::from(b.origin.x), f32::from(b.origin.y), f32::from(b.size.width), f32::from(b.size.height))
        };
        assert_eq!(at(ObjectFit::Contain, (0., 0.), (0., 0.)), (10., 20., 50., 50.));
        assert_eq!(at(ObjectFit::Contain, (1., -5.), (0.5, 0.)), (55., 20., 50., 50.));
        assert_eq!(at(ObjectFit::Cover, (0., 0.), (1., 0.)), (10., -30., 100., 100.));
        assert_eq!(at(ObjectFit::None, (0.25, 0.), (0., 3.)), (25., 23., 40., 40.));
        assert_eq!(at(ObjectFit::Fill, (0.5, 7.), (0.5, 0.)), (17., 20., 100., 50.));
        let legacy = ObjectFit::None.get_bounds(bounds, square);
        assert_eq!(legacy.origin, bounds.origin);
    }
}

#[cfg(test)]
mod vn_safe_center_tests {
    use crate::{AlignContent, Context, TestAppContext, Window, div, prelude::*, px, size};

    struct Boxes;

    impl Render for Boxes {
        fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
            let block = |name: &'static str, child_h: f32| {
                let mut d = div().w(px(100.)).h(px(100.));
                d.style().align_content = Some(AlignContent::SafeCenter);
                d.child(div().debug_selector(move || name.into()).w(px(10.)).h(px(child_h)))
            };
            div().child(block("FITS", 20.)).child(block("OVERFLOWS", 200.))
        }
    }

    #[gpui::test]
    fn safe_center_centres_what_fits_and_starts_what_overflows(cx: &mut TestAppContext) {
        let window = cx.open_window(size(px(400.), px(600.)), |_, _| Boxes);
        cx.run_until_parked();
        window
            .update(cx, |_, window, _| {
                let at = |name: &str| window.rendered_frame.debug_bounds.get(name).copied().unwrap();
                assert_eq!(at("FITS").origin.y, px(40.), "(100 - 20) / 2");
                assert_eq!(at("OVERFLOWS").origin.y, px(100.), "at the start of the second box");
            })
            .unwrap();
    }
}

/// vue-native fork: `Style::vn_mask_ring`.
#[cfg(test)]
mod vn_mask_ring_tests {
    use crate::{
        BoxShadow, Context, Hsla, TestAppContext, Window, color::BG_RING, div, point,
        prelude::*, px, size,
    };

    struct Card {
        ring: bool,
    }

    impl Render for Card {
        fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
            let mut d = div()
                .w(px(100.))
                .h(px(60.))
                .p(px(2.))
                .border_1()
                .border_color(Hsla::white())
                .bg(Hsla::red())
                .shadow(vec![BoxShadow {
                    color: Hsla::black(),
                    offset: point(px(0.), px(4.)),
                    blur_radius: px(8.),
                    spread_radius: px(0.),
                    inset: false,
                }]);
            d.style().vn_mask_ring = Some(self.ring);
            d
        }
    }

    /// The painted quads, and the frame's shadow count.
    fn paint(ring: bool, cx: &mut TestAppContext) -> (Vec<crate::Quad>, usize, f32) {
        let window = cx.open_window(size(px(300.), px(300.)), move |_, _| Card { ring });
        cx.run_until_parked();
        window
            .update(cx, |_, window, _| {
                let shadows = window.rendered_frame.scene.shadows.len();
                (window.painted_quads(), shadows, window.scale_factor())
            })
            .unwrap()
    }

    #[gpui::test]
    fn a_ring_mask_paints_the_background_in_the_padding_and_border_only(
        cx: &mut TestAppContext,
    ) {
        let (quads, shadows, scale) = paint(false, cx);
        let background = quads.iter().find(|q| q.background.as_solid() == Some(Hsla::red()));
        let background = background.expect("the background quad");
        assert_eq!(background.background.flags & BG_RING, 0);
        assert_eq!(background.border_widths.top.0, 0.0, "a plain background fills the box");
        assert_eq!(shadows, 1);

        let (quads, shadows, _) = paint(true, cx);
        let background = quads
            .iter()
            .find(|q| q.background.flags & BG_RING != 0)
            .expect("the background quad, flagged");
        assert_eq!(background.background.as_solid(), Some(Hsla::red()));
        for width in [
            background.border_widths.top,
            background.border_widths.right,
            background.border_widths.bottom,
            background.border_widths.left,
        ] {
            assert_eq!(width.0, 3.0 * scale, "1px border + 2px padding");
        }
        let border = quads
            .iter()
            .find(|q| q.border_color == Hsla::white())
            .expect("the border still paints");
        assert_eq!(border.border_widths.top.0, 1.0 * scale);
        assert_eq!(shadows, 0, "the mask cuts the outer shadow");
    }
}

/// vue-native fork: `overflow` under a `border-radius` clips to the rounded
/// padding box, and what is painted inside carries that mask.
#[cfg(test)]
mod vn_rounded_clip_tests {
    use crate::{
        Bounds, Context, Corners, DefiniteLength, Edges, Hsla, Overflow, Style, TestAppContext,
        VnCornerRadius, Window, div, point, prelude::*, px, size, vn_resolve_corner_radii,
        vn_take_ellipse_drawn_as_circle,
    };

    fn clipping(radius: f32, border: f32, border_color: Option<Hsla>) -> Style {
        Style {
            overflow: point(Overflow::Hidden, Overflow::Hidden),
            corner_radii: Corners::all(px(radius)).map(|radius| (*radius).into()),
            border_widths: Edges::all(px(border).into()),
            border_color,
            ..Style::default()
        }
    }

    #[test]
    fn an_overflow_clip_takes_the_padding_box_curve() {
        let rem = px(16.);
        let bounds = Bounds::new(point(px(10.), px(20.)), size(px(200.), px(160.)));

        let mask = clipping(24., 4., Some(Hsla::black())).overflow_mask(bounds, rem).unwrap();
        assert_eq!(mask.bounds, Bounds::new(point(px(14.), px(24.)), size(px(192.), px(152.))));
        assert_eq!(mask.corner_radii, Corners::all(px(20.)), "the radius less the border");

        let mask = clipping(24., 4., None).overflow_mask(bounds, rem).unwrap();
        assert_eq!(mask.bounds, bounds, "a transparent border is not inset");
        assert_eq!(mask.corner_radii, Corners::all(px(24.)), "so it takes nothing off");

        let mask = clipping(3., 4., Some(Hsla::black())).overflow_mask(bounds, rem).unwrap();
        assert_eq!(mask.corner_radii, Corners::all(px(0.)), "a border wider than the radius");

        let avatar = Bounds::new(point(px(0.), px(0.)), size(px(64.), px(64.)));
        let mask = clipping(9999., 0., None).overflow_mask(avatar, rem).unwrap();
        assert_eq!(mask.corner_radii, Corners::all(px(32.)), "a pill is a circle");

        let mask = clipping(0., 0., None).overflow_mask(bounds, rem).unwrap();
        assert!(!mask.is_rounded(), "no radius, the plain rect");
    }

    fn resolve(lengths: [f32; 4], relative: [Option<(DefiniteLength, DefiniteLength)>; 4], w: f32, h: f32) -> [f32; 4] {
        let corner = |r: Option<(DefiniteLength, DefiniteLength)>| r.map(|(x, y)| VnCornerRadius { x, y });
        let radii = vn_resolve_corner_radii(
            &Corners {
                top_left: px(lengths[0]).into(),
                top_right: px(lengths[1]).into(),
                bottom_right: px(lengths[2]).into(),
                bottom_left: px(lengths[3]).into(),
            },
            &Corners {
                top_left: corner(relative[0]),
                top_right: corner(relative[1]),
                bottom_right: corner(relative[2]),
                bottom_left: corner(relative[3]),
            },
            size(px(w), px(h)),
            px(16.),
        );
        [radii.top_left, radii.top_right, radii.bottom_right, radii.bottom_left].map(f32::from)
    }

    #[test]
    fn percentages_resolve_against_the_box_and_overlaps_scale() {
        let half = Some((DefiniteLength::Fraction(0.5), DefiniteLength::Fraction(0.5)));
        let _ = vn_take_ellipse_drawn_as_circle();

        assert_eq!(resolve([0.; 4], [half; 4], 64., 64.), [32.; 4], "50% of a square: a circle");
        assert!(!vn_take_ellipse_drawn_as_circle());

        // 50% of 120x48 is a 60x24 ellipse at each corner; gpui draws the
        // 24px circle, a pill, and says so.
        assert_eq!(resolve([0.; 4], [half; 4], 120., 48.), [24.; 4]);
        assert!(vn_take_ellipse_drawn_as_circle());

        // `rounded-full`: 9999px on every corner of 100x40 scales by 40/19998.
        assert_eq!(resolve([9999.; 4], [None; 4], 100., 40.), [20.; 4]);
        // `rounded-l-full`: only the left side overruns; both its corners
        // scale, and the right stays square.
        assert_eq!(resolve([9999., 0., 0., 9999.], [None; 4], 100., 40.), [20., 0., 0., 20.]);
        // Radii that fit stay as written.
        assert_eq!(resolve([8., 4., 0., 12.], [None; 4], 100., 40.), [8., 4., 0., 12.]);
        // 40 + 40 across a 60px top: both scale by 60/80, to 30.
        assert_eq!(resolve([40., 40., 0., 0.], [None; 4], 60., 200.), [30., 30., 0., 0.]);

        // `calc(50% - 4px)` and an elliptical `10px / 20px`.
        let calc = DefiniteLength::Calc { fraction: 0.5, pixels: px(-4.) };
        assert_eq!(resolve([0.; 4], [Some((calc, calc)); 4], 64., 64.), [28.; 4]);
        let ellipse = Some((DefiniteLength::Absolute(px(10.).into()), DefiniteLength::Absolute(px(20.).into())));
        assert_eq!(resolve([0.; 4], [ellipse; 4], 100., 100.), [10.; 4]);
        assert!(vn_take_ellipse_drawn_as_circle());
    }

    #[test]
    fn a_percentage_radius_clips_overflow_too() {
        let mut avatar = clipping(0., 0., None);
        let half = Some(VnCornerRadius {
            x: DefiniteLength::Fraction(0.5),
            y: DefiniteLength::Fraction(0.5),
        });
        avatar.vn_corner_radii = Corners {
            top_left: half,
            top_right: half,
            bottom_right: half,
            bottom_left: half,
        };
        let bounds = Bounds::new(point(px(0.), px(0.)), size(px(40.), px(40.)));
        let mask = avatar.overflow_mask(bounds, px(16.)).unwrap();
        assert_eq!(mask.corner_radii, Corners::all(px(20.)));
    }

    /// reka's AvatarRoot around its image, and a rounded card around a list
    /// that scrolls.
    struct Clips;

    impl Render for Clips {
        fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
            div()
                .child(
                    div()
                        .size(px(64.))
                        .rounded_full()
                        .overflow_hidden()
                        .child(div().size_full().bg(Hsla::red())),
                )
                .child(
                    div()
                        .w(px(200.))
                        .h(px(160.))
                        .rounded(px(24.))
                        .overflow_hidden()
                        .child(
                            div()
                                .id("list")
                                .size_full()
                                .overflow_y_scroll()
                                .child(div().w_full().h(px(400.)).bg(Hsla::green())),
                        ),
                )
        }
    }

    #[gpui::test]
    fn what_paints_inside_carries_the_rounded_mask(cx: &mut TestAppContext) {
        let window = cx.open_window(size(px(400.), px(400.)), |_, _| Clips);
        cx.run_until_parked();
        let (quads, scale) = window
            .update(cx, |_, window, _| (window.painted_quads(), window.scale_factor()))
            .unwrap();
        let quad = |color: Hsla| {
            quads
                .iter()
                .find(|q| q.background.as_solid() == Some(color))
                .copied()
                .expect("the quad")
        };

        let avatar = quad(Hsla::red());
        assert_eq!(avatar.content_mask.corner_radii, Corners::all(px(32.)).scale(scale));
        assert_eq!(avatar.corner_radii, Corners::default(), "the image itself is square");

        // The scroller's own mask is square and fills the card, so its
        // corners are the card's: the rows clip at the card's curve.
        let rows = quad(Hsla::green());
        assert_eq!(rows.content_mask.corner_radii, Corners::all(px(24.)).scale(scale));
        assert_eq!(rows.content_mask.bounds.size, size(px(200.), px(160.)).scale(scale));
    }
}

/// vue-native fork: `clip-path` is a rounded mask around the element's whole
/// painting (`Style::vn_clip_path`, `Style::vn_clip_mask`).
#[cfg(test)]
mod vn_clip_path_tests {
    use crate::{
        Bounds, BoxShadow, Context, Corners, DefiniteLength, Edges, Hsla, Length, Pixels, Style,
        TestAppContext, VnClipBox, VnClipPath, VnCornerRadius, VnShapeRadius, Window, div, point,
        prelude::*, px, relative, size,
    };

    const BOX: VnClipBox = VnClipBox::Border;

    fn len(v: f32) -> DefiniteLength {
        px(v).into()
    }

    fn round(x: DefiniteLength, y: DefiniteLength) -> [VnCornerRadius; 4] {
        [VnCornerRadius { x, y }; 4]
    }

    /// The mask's bounds, as `[x, y, w, h]`, and its four radii, to 0.01px.
    fn mask(clip: VnClipPath, style: Style, bounds: [f32; 4]) -> ([f32; 4], [f32; 4]) {
        let bounds = Bounds::new(point(px(bounds[0]), px(bounds[1])), size(px(bounds[2]), px(bounds[3])));
        let m = Style { vn_clip_path: clip, ..style }.vn_clip_mask(bounds, px(16.)).expect("a mask");
        let (b, r) = (m.bounds, m.corner_radii);
        let at = |v: Pixels| (f32::from(v) * 100.).round() / 100.;
        (
            [b.origin.x, b.origin.y, b.size.width, b.size.height].map(at),
            [r.top_left, r.top_right, r.bottom_right, r.bottom_left].map(at),
        )
    }

    fn inset(insets: [DefiniteLength; 4], radii: [VnCornerRadius; 4]) -> VnClipPath {
        VnClipPath::Inset { reference: BOX, insets, radii }
    }

    fn ellipse(center: [DefiniteLength; 2], radius: [VnShapeRadius; 2], circle: bool) -> VnClipPath {
        VnClipPath::Ellipse { reference: BOX, center, radius, circle }
    }

    #[test]
    fn inset_takes_its_insets_off_the_box_and_fits_its_radii() {
        let none = Style::default;
        let square = round(len(0.), len(0.));
        assert_eq!(Style::default().vn_clip_mask(Bounds::default(), px(16.)), None, "none is no mask");
        // `inset(10px 20% round 8px)` on a 200x100 box at (10, 20).
        let insets = [len(10.), relative(0.2), len(10.), relative(0.2)];
        assert_eq!(
            mask(inset(insets, round(len(8.), len(8.))), none(), [10., 20., 200., 100.]),
            ([50., 30., 120., 80.], [8.; 4])
        );
        // The screen-reader-only `inset(50%)` encloses nothing.
        let half = relative(0.5);
        assert_eq!(mask(inset([half; 4], square), none(), [0., 0., 30., 10.]).0, [15., 5., 0., 0.]);
        // Insets past the box leave it empty, not inside out.
        assert_eq!(mask(inset([len(80.); 4], square), none(), [0., 0., 100., 100.]).0, [80., 80., 0., 0.]);
        // `round 30px` on a 200x20 strip scales by 20 / (30 + 30). A
        // percentage radius is of the REFERENCE box: 10% of 200x100 is 20 by
        // 10, drawn as the 10px circle.
        let strip = [len(40.), len(0.), len(40.), len(0.)];
        assert_eq!(mask(inset(strip, round(len(30.), len(30.))), none(), [0., 0., 200., 100.]).1, [10.; 4]);
        let ten = relative(0.1);
        assert_eq!(mask(inset([len(0.); 4], round(ten, ten)), none(), [0., 0., 200., 100.]).1, [10.; 4]);
    }

    #[test]
    fn circle_and_ellipse_resolve_against_the_box() {
        let none = Style::default;
        let mid = [relative(0.5), relative(0.5)];
        // `circle(50%)` on a square is its disc.
        let circle = ellipse(mid, [VnShapeRadius::Length(relative(0.5)); 2], true);
        assert_eq!(mask(circle.clone(), none(), [0., 0., 60., 60.]), ([0., 0., 60., 60.], [30.; 4]));
        // On 200x100 the percentage is of sqrt(200^2 + 100^2) / sqrt(2), 158.11.
        assert_eq!(mask(circle, none(), [0., 0., 200., 100.]).1, [79.06; 4]);
        // `circle(closest-side at 25% 50%)`: 50 to the left, top and bottom.
        let at = [relative(0.25), relative(0.5)];
        assert_eq!(
            mask(ellipse(at, [VnShapeRadius::ClosestSide; 2], true), none(), [0., 0., 200., 100.]),
            ([0., 0., 100., 100.], [50.; 4])
        );
        let far = ellipse(at, [VnShapeRadius::FarthestSide; 2], true);
        assert_eq!(mask(far, none(), [0., 0., 200., 100.]).1, [150.; 4]);
        // `ellipse(closest-side farthest-side)`: each radius in its own axis.
        // The mask is its box; the ellipse is the outline (an isolated
        // layer), so it is exact.
        let e = ellipse(mid, [VnShapeRadius::ClosestSide, VnShapeRadius::FarthestSide], false);
        assert_eq!(mask(e.clone(), none(), [0., 0., 200., 100.]), ([0., 0., 200., 100.], [0.; 4]));
        let bounds = Bounds::new(point(px(0.), px(0.)), size(px(200.), px(100.)));
        let outline = Style { vn_clip_path: e, ..none() }.vn_clip_outline(bounds, px(16.)).expect("an outline");
        let g = crate::VnClipGeometry::new(&outline, 1., crate::TransformationMatrix::unit());
        assert!(g.contains([100., 50.]) && g.contains([2., 50.]) && g.contains([100., 2.]));
        assert!(!g.contains([20., 10.]), "the corner a capsule would keep");
        // `ellipse(10px 20% at right 10px center)`: `right 10px` is 100% - 10px.
        let right = [DefiniteLength::Calc { fraction: 1., pixels: px(-10.) }, relative(0.5)];
        let e = ellipse(right, [VnShapeRadius::Length(len(10.)), VnShapeRadius::Length(relative(0.2))], false);
        assert_eq!(mask(e, none(), [0., 0., 200., 100.]), ([180., 30., 20., 40.], [0.; 4]));
        // A circle has no outline: the rounded mask is all of it.
        let c = Style { vn_clip_path: ellipse(mid, [VnShapeRadius::ClosestSide; 2], true), ..none() };
        assert_eq!(c.vn_clip_outline(bounds, px(16.)), None);
    }

    #[test]
    fn a_box_alone_is_rounded_as_the_border_radius_rounds_it() {
        let style = || Style {
            corner_radii: Corners::all(px(24.)).map(|r| (*r).into()),
            border_widths: Edges::all(px(4.).into()),
            padding: Edges::all(px(6.).into()),
            margin: Edges { top: Length::Auto, ..Edges::all(len(8.).into()) },
            ..Style::default()
        };
        let at = [0., 0., 100., 80.];
        assert_eq!(mask(VnClipPath::Box(VnClipBox::Border), style(), at), ([0., 0., 100., 80.], [24.; 4]));
        assert_eq!(mask(VnClipPath::Box(VnClipBox::Padding), style(), at), ([4., 4., 92., 72.], [20.; 4]));
        assert_eq!(mask(VnClipPath::Box(VnClipBox::Content), style(), at), ([10., 10., 80., 60.], [14.; 4]));
        // An `auto` margin is none; a rounded corner grows by the margin.
        assert_eq!(mask(VnClipPath::Box(VnClipBox::Margin), style(), at), ([-8., 0., 116., 88.], [32.; 4]));
        // An inset's reference box: the content box less 5px.
        let clip = VnClipPath::Inset { reference: VnClipBox::Content, insets: [len(5.); 4], radii: round(len(0.), len(0.)) };
        assert_eq!(mask(clip, style(), at), ([15., 15., 70., 50.], [0.; 4]));
    }

    /// A clipped box with a border and a shadow, and a child that overflows
    /// it.
    struct Clipped;

    impl Render for Clipped {
        fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
            let mut d = div()
                .size(px(60.))
                .bg(Hsla::red())
                .border_2()
                .border_color(Hsla::white())
                .shadow(vec![BoxShadow {
                    color: Hsla::black(),
                    offset: point(px(0.), px(4.)),
                    blur_radius: px(8.),
                    spread_radius: px(0.),
                    inset: false,
                }])
                .child(div().size(px(100.)).bg(Hsla::green()));
            d.style().vn_clip_path = Some(VnClipPath::Ellipse {
                reference: VnClipBox::Border,
                center: [relative(0.5), relative(0.5)],
                radius: [VnShapeRadius::Length(relative(0.5)); 2],
                circle: true,
            });
            div().child(d)
        }
    }

    #[gpui::test]
    fn everything_the_element_paints_carries_the_mask(cx: &mut TestAppContext) {
        let window = cx.open_window(size(px(200.), px(200.)), |_, _| Clipped);
        cx.run_until_parked();
        let (quads, shadows, scale) = window
            .update(cx, |_, window, _| {
                (window.painted_quads(), window.rendered_frame.scene.shadows.clone(), window.scale_factor())
            })
            .unwrap();
        let disc = Bounds::new(point(px(0.), px(0.)), size(px(60.), px(60.))).scale(scale);
        let radius = Corners::all(px(30.)).scale(scale);
        for color in [Hsla::red(), Hsla::green()] {
            let q = quads.iter().find(|q| q.background.as_solid() == Some(color)).expect("the quad");
            assert_eq!((q.content_mask.bounds, q.content_mask.corner_radii), (disc, radius), "{color:?}");
        }
        let border = quads.iter().find(|q| q.border_color == Hsla::white()).expect("the border");
        assert_eq!(border.content_mask.bounds, disc);
        assert_eq!(shadows.len(), 1);
        assert_eq!((shadows[0].content_mask.bounds, shadows[0].content_mask.corner_radii), (disc, radius));
    }

    fn polygon(reference: VnClipBox, fill_rule: crate::VnFillRule, points: &[[DefiniteLength; 2]]) -> VnClipPath {
        VnClipPath::Polygon { reference, fill_rule, points: points.to_vec().into() }
    }

    fn outline(clip: VnClipPath, style: Style, at: [f32; 4]) -> crate::VnClipOutline {
        let bounds = Bounds::new(point(px(at[0]), px(at[1])), size(px(at[2]), px(at[3])));
        Style { vn_clip_path: clip, ..style }.vn_clip_outline(bounds, px(16.)).expect("an outline")
    }

    #[test]
    fn a_polygon_resolves_its_percentages_against_the_reference_box() {
        use crate::{VnFillRule, VnPathVerb::*};
        // `polygon(50% 0, 100% 100%, 0 100%)` on a 200x100 box at (10, 20).
        let triangle = [[relative(0.5), len(0.)], [relative(1.), relative(1.)], [len(0.), relative(1.)]];
        let o = outline(polygon(BOX, VnFillRule::NonZero, &triangle), Style::default(), [10., 20., 200., 100.]);
        assert_eq!(o.verbs, vec![MoveTo([110., 20.]), LineTo([210., 120.]), LineTo([10., 120.]), Close]);
        // The mask is the box around it, which culls.
        assert_eq!(
            mask(polygon(BOX, VnFillRule::NonZero, &triangle), Style::default(), [10., 20., 200., 100.]),
            ([10., 20., 200., 100.], [0.; 4])
        );
        // Against the content box: 4px border and 6px padding in.
        let style = || Style {
            border_widths: Edges::all(px(4.).into()),
            padding: Edges::all(px(6.).into()),
            ..Style::default()
        };
        let o = outline(polygon(VnClipBox::Content, VnFillRule::EvenOdd, &triangle), style(), [0., 0., 100., 100.]);
        assert_eq!(o.fill_rule, VnFillRule::EvenOdd);
        assert_eq!(o.verbs[0], MoveTo([50., 10.]));
        assert_eq!(o.verbs[1], LineTo([90., 90.]));
        // `calc()` and a value past the box.
        let past = [[DefiniteLength::Calc { fraction: 1., pixels: px(-10.) }, relative(1.5)]];
        assert_eq!(
            outline(polygon(BOX, VnFillRule::NonZero, &past), Style::default(), [0., 0., 100., 40.]).verbs[0],
            MoveTo([90., 60.])
        );
    }

    #[test]
    fn a_path_is_laid_from_the_reference_box_corner() {
        use crate::{VnFillRule, VnPathVerb::*};
        let verbs = vec![MoveTo([0., 0.]), QuadTo([10., 20.], [20., 0.]), Close];
        let path = |reference| VnClipPath::Path { reference, fill_rule: VnFillRule::NonZero, verbs: verbs.clone().into() };
        let o = outline(path(BOX), Style::default(), [5., 7., 50., 50.]);
        assert_eq!(o.verbs, vec![MoveTo([5., 7.]), QuadTo([15., 27.], [25., 7.]), Close]);
        let style = Style { padding: Edges::all(px(3.).into()), ..Style::default() };
        let o = outline(path(VnClipBox::Content), style, [0., 0., 50., 50.]);
        assert_eq!(o.verbs[0], MoveTo([3., 3.]));
        // The mask is the control points' box, the curve inside it.
        assert_eq!(mask(path(BOX), Style::default(), [5., 7., 50., 50.]).0, [5., 7., 20., 20.]);
    }

    /// A box clipped to a triangle, with a child in flow and a deferred one.
    struct Triangle;

    impl Render for Triangle {
        fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
            let mut d = div()
                .size(px(100.))
                .bg(Hsla::red())
                .cursor_pointer()
                .child(div().size(px(100.)).bg(Hsla::green()))
                .child(crate::deferred(div().absolute().top_0().left_0().size(px(50.)).bg(Hsla::blue())).clip_to_ancestors(true));
            d.style().vn_clip_path = Some(polygon(
                BOX,
                crate::VnFillRule::NonZero,
                &[[len(0.), len(0.)], [len(100.), len(0.)], [len(0.), len(100.)]],
            ));
            div().child(d)
        }
    }

    #[gpui::test]
    fn a_polygon_paints_into_an_isolated_layer_and_cuts_the_hit_test(cx: &mut TestAppContext) {
        let window = cx.open_window(size(px(200.), px(200.)), |_, _| Triangle);
        cx.run_until_parked();
        window
            .update(cx, |_, window, _| {
                let scale = window.scale_factor();
                let scene = &window.rendered_frame.scene;
                // One layer for the box and its child in flow, one re-opened
                // for the deferred child; each composites one group.
                assert_eq!(scene.vn_layers.len(), 2, "{:?}", scene.vn_layers);
                assert_eq!(scene.vn_group_count(), 3);
                let root: Vec<_> = scene.vn_batches(0).collect();
                assert!(root.iter().all(|b| matches!(b, crate::VnBatch::Layers(_))), "{root:?}");
                let colors = |group: usize| -> Vec<Hsla> {
                    scene
                        .vn_batches(group)
                        .filter_map(|b| match b {
                            crate::VnBatch::Primitives(crate::PrimitiveBatch::Quads(r)) => Some(r),
                            _ => None,
                        })
                        .flat_map(|r| scene.quads[r].iter().filter_map(|q| q.background.as_solid()).collect::<Vec<_>>())
                        .collect()
                };
                let mut groups: Vec<Vec<Hsla>> = scene.vn_layers.iter().map(|l| colors(l.group as usize)).collect();
                groups.sort_by_key(|g| g.len());
                assert_eq!(groups, vec![vec![Hsla::blue()], vec![Hsla::red(), Hsla::green()]]);
                // Each composite is the triangle's box out to whole pixels
                // and a pixel more for the edge, with the triangle's edges.
                for layer in &scene.vn_layers {
                    assert_eq!(layer.segment_count, 3);
                    assert_eq!(layer.fill_rule, 0);
                    assert!(layer.bounds.size.width.0 >= 100. * scale);
                }
                assert_eq!(scene.vn_clip_segments.len(), 6);
                // A renderer without layers still gets every primitive.
                let flat = scene.batches().count();
                assert!(flat >= 2, "{flat}");

                // The hitbox: in the triangle, and not past its hypotenuse.
                let frame = &window.rendered_frame;
                let clipped: Vec<_> = frame.hitboxes.iter().filter(|h| h.vn_clip.is_some()).collect();
                assert_eq!(clipped.len(), 1);
                let id = clipped[0].id;
                let hits = |x: f32, y: f32| frame.hit_test(point(px(x), px(y)), scale).ids.contains(&id);
                assert!(hits(10., 10.));
                assert!(hits(40., 40.));
                assert!(!hits(60., 60.), "inside the box, outside the triangle");
                assert!(!hits(150., 10.));
            })
            .unwrap();
    }

    fn quad_mask(tint_leaf: bool) -> crate::VnMask {
        crate::VnMask {
            luminance: false,
            tint_leaf,
            key: 1,
            paint: Some(std::rc::Rc::new(|bounds, tint, window: &mut Window, _: &mut crate::App| {
                let half = Bounds::new(bounds.origin, size(bounds.size.width / 2., bounds.size.height));
                window.paint_quad(crate::fill(half, tint.unwrap_or_else(Hsla::white)));
            })),
        }
    }

    /// A translucent box with a child and a deferred child under a mask, and an icon-like leaf.
    struct Masked;

    impl Render for Masked {
        fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
            let deferred = crate::deferred(div().absolute().top_0().left_0().size(px(20.)).bg(Hsla::black()));
            let mut boxed = div()
                .size(px(40.))
                .bg(Hsla::red())
                .opacity(0.5)
                .child(div().size(px(10.)).bg(Hsla::green()))
                .child(deferred.clip_to_ancestors(true));
            boxed.style().vn_mask = Some(quad_mask(false));
            let mut icon = div().size(px(20.)).bg(Hsla::blue());
            icon.style().vn_mask = Some(quad_mask(true));
            div().child(boxed).child(icon)
        }
    }

    #[gpui::test]
    fn a_mask_composites_its_group_through_the_mask_group_and_a_leaf_is_tinted(cx: &mut TestAppContext) {
        let window = cx.open_window(size(px(200.), px(200.)), |_, _| Masked);
        cx.run_until_parked();
        window
            .update(cx, |_, window, _| {
                let scene = &window.rendered_frame.scene;
                assert_eq!(scene.vn_layers.len(), 2, "the box, the deferred child, and no layer for the leaf");
                let colors = |group: usize| -> Vec<Hsla> {
                    scene
                        .vn_batches(group)
                        .filter_map(|b| match b {
                            crate::VnBatch::Primitives(crate::PrimitiveBatch::Quads(r)) => Some(r),
                            _ => None,
                        })
                        .flat_map(|r| scene.quads[r].iter().filter_map(|q| q.background.as_solid()).collect::<Vec<_>>())
                        .collect()
                };
                let mut groups = Vec::new();
                for layer in &scene.vn_layers {
                    assert_eq!(layer.fill_rule, 1 << 1, "an alpha mask over a nonzero box");
                    let content = colors(layer.group as usize);
                    assert!(content.iter().all(|c| (c.a - 0.5).abs() < 1e-3), "{content:?}");
                    assert_eq!(colors(layer.group as usize + 1), vec![Hsla::white()], "the mask is not faded");
                    groups.push(content.len());
                }
                groups.sort();
                assert_eq!(groups, vec![1, 2], "the deferred child paints through the mask again");
                let root = colors(0);
                assert_eq!(root, vec![Hsla::blue()], "the leaf paints the mask in its colour");
                let leaf = scene.quads.iter().find(|q| q.background.as_solid() == Some(Hsla::blue())).unwrap();
                assert_eq!(leaf.bounds.size.width.0, 10. * window.scale_factor());
            })
            .unwrap();
    }
}

#[cfg(test)]
mod vn_border_color_tests {
    use super::*;

    #[test]
    fn independent_border_colors_keep_shared_fallback_and_transparent_sides() {
        let mut style = Style { border_color: Some(Hsla::red()), ..Default::default() };
        style.vn_border_colors.left = Some(Hsla::transparent_black());
        style.vn_border_colors.top = Some(Hsla::blue());
        let colors = style.vn_resolved_border_colors();
        assert_eq!(colors.top, Hsla::blue());
        assert_eq!(colors.right, Hsla::red());
        assert!(colors.left.is_transparent());
    }

    /// A border coloured side by side, with no shared colour, still takes its
    /// width off the box its overflow is clipped to.
    #[test]
    fn overflow_is_clipped_inside_borders_coloured_per_side() {
        let mut style = Style::default();
        style.overflow = Point { x: Overflow::Hidden, y: Overflow::Hidden };
        style.border_widths = Edges::all(px(4.).into());
        style.vn_border_colors.top = Some(Hsla::blue());
        style.vn_border_colors.left = Some(Hsla::red());
        let bounds = Bounds::new(point(px(0.), px(0.)), size(px(100.), px(100.)));
        let mask = style.overflow_mask(bounds, px(16.)).unwrap();
        assert_eq!(mask.bounds, Bounds::from_corners(point(px(4.), px(4.)), point(px(100.), px(100.))));
    }

    #[test]
    fn colored_border_partitions_cover_rounded_arcs_with_width_weighted_joins() {
        let clips = Style::vn_border_side_clips(
            Bounds::new(point(px(0.), px(0.)), size(px(80.), px(80.))), Edges::all(px(8.)),
        );
        let contains = |polygon: &Vec<[f32; 2]>, point: [f32; 2]| {
            let mut sign = 0.;
            for (a, b) in polygon.iter().zip(polygon.iter().cycle().skip(1)) {
                let cross = (b[0] - a[0]) * (point[1] - a[1]) - (b[1] - a[1]) * (point[0] - a[0]);
                if cross.abs() < 0.0001 { continue; }
                if sign != 0. && cross.signum() != sign { return false; }
                sign = cross.signum();
            }
            polygon.len() >= 3
        };
        assert!(contains(&clips[0], [15., 13.]), "top rounded arc extends below the rectangular inner top");
        assert!(contains(&clips[3], [13., 15.]), "left rounded arc extends beyond the rectangular inner left");
        for y in 0..80 {
            for x in 0..80 {
                assert!(clips.iter().any(|polygon| contains(polygon, [x as f32 + 0.25, y as f32 + 0.25])),
                    "coloured side clips must cover the entire quad, including rounded corners");
            }
        }
        let clips = Style::vn_border_side_clips(
            Bounds::new(point(px(0.), px(0.)), size(px(100.), px(60.))),
            Edges { top: px(4.), right: px(8.), bottom: px(6.), left: px(2.) },
        );
        assert!(contains(&clips[0], [2., 3.]));
        assert!(contains(&clips[3], [2., 5.]), "top/left join uses their2:4width ratio");
    }
}