skathi-gpui 0.4.0

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
mod layout_bounds;
mod retained;
pub use retained::VnRetainedLayoutStats;

use layout_bounds::LayoutIdMap;

use crate::{
    AbsoluteLength, App, Bounds, DefiniteLength, Display, Edges, GridTemplate, Length, Pixels,
    Point, Size, Style, Window, size,
    util::{
        ceil_to_device_pixel, round_half_toward_zero, round_stroke_to_device_pixel,
        round_to_device_pixel,
    },
};
use collections::{FxHashMap, FxHashSet};
use std::{fmt::Debug, ops::Range};
use taffy::{
    TaffyTree, TraversePartialTree as _,
    geometry::{Point as TaffyPoint, Rect as TaffyRect, Size as TaffySize},
    prelude::{max_content, min_content},
    style::AvailableSpace as TaffyAvailableSpace,
    tree::NodeId,
};

#[cfg(feature = "stacker")]
type StackSafe<T> = stacksafe::StackSafe<T>;
#[cfg(not(feature = "stacker"))]
type StackSafe<T> = T;

type MeasureFn =
    dyn FnMut(Size<Option<Pixels>>, Size<AvailableSpace>, &mut Window, &mut App) -> Size<Pixels>;
type NodeMeasureFn = StackSafe<Box<MeasureFn>>;

struct NodeContext {
    measure: NodeMeasureFn,
}
pub struct TaffyLayoutEngine {
    taffy: TaffyTree<NodeContext>,
    absolute_layout_bounds: LayoutIdMap<Bounds<Pixels>>,
    /// Unrounded absolute border-box top-left per-node coordinate in device pixels.
    absolute_outer_origins: LayoutIdMap<Point<f32>>,
    retention: retained::Retention,
    computed_layouts: FxHashSet<LayoutId>,
    layout_bounds_scratch_space: Vec<LayoutId>,
    /// This frame's nonzero `vn_order`s, by node.
    orders: FxHashMap<LayoutId, i32>,
    calc_sizes: Vec<CalcSizeNode>,
}

const EXPECT_MESSAGE: &str = "we should avoid taffy layout errors by construction if possible";

impl TaffyLayoutEngine {
    pub fn new() -> Self {
        let mut taffy = TaffyTree::new();
        taffy.disable_rounding();
        TaffyLayoutEngine {
            taffy,
            absolute_layout_bounds: LayoutIdMap::default(),
            absolute_outer_origins: LayoutIdMap::default(),
            retention: retained::Retention::default(),
            computed_layouts: FxHashSet::default(),
            layout_bounds_scratch_space: Vec::new(),
            orders: FxHashMap::default(),
            calc_sizes: Vec::new(),
        }
    }

    pub fn clear(&mut self) {
        if self.retention.enabled {
            retained::finish_frame(self);
        } else {
            self.taffy.clear();
        }
        self.absolute_layout_bounds.clear();
        self.absolute_outer_origins.clear();
        self.computed_layouts.clear();
        self.orders.clear();
        self.calc_sizes.clear();
    }

    pub(crate) fn retention_enabled(&self) -> bool {
        self.retention.enabled
    }

    pub(crate) fn enable_retention(&mut self) {
        if !self.retention.enabled {
            assert_eq!(
                self.taffy.total_node_count(),
                0,
                "enable retained layout before the first layout request"
            );
            self.retention.enabled = true;
        }
    }

    pub(crate) fn profile_retention(&mut self, enabled: bool) {
        self.retention.stats = enabled.then(Default::default);
    }

    pub(crate) fn take_retention_stats(&mut self) -> Option<VnRetainedLayoutStats> {
        self.retention.stats.as_mut().map(std::mem::take)
    }

    pub fn request_layout(
        &mut self,
        style: Style,
        rem_size: Pixels,
        scale_factor: f32,
        children: &[LayoutId],
    ) -> LayoutId {
        self.request_layout_in_scope(None, style, rem_size, scale_factor, children)
    }

    pub(crate) fn request_layout_in_scope(
        &mut self,
        scope: Option<crate::GlobalElementId>,
        style: Style,
        rem_size: Pixels,
        scale_factor: f32,
        children: &[LayoutId],
    ) -> LayoutId {
        let taffy_style = style.to_taffy(rem_size, scale_factor);
        let reordered = self.order_modified(&style, children);
        let children = reordered.as_deref().unwrap_or(children);
        let id = if self.retention.enabled {
            retained::request_layout(self, scope, taffy_style, children)
        } else if children.is_empty() {
            self.taffy
                .new_leaf(taffy_style)
                .expect(EXPECT_MESSAGE)
                .into()
        } else {
            self.taffy
                // This is safe because LayoutId is repr(transparent) to taffy::tree::NodeId.
                .new_with_children(taffy_style, LayoutId::to_taffy_slice(children))
                .expect(EXPECT_MESSAGE)
                .into()
        };
        self.note_order(id, &style);
        id
    }

    fn note_order(&mut self, id: LayoutId, style: &Style) {
        if style.vn_order != 0 {
            self.orders.insert(id, style.vn_order);
        }
        if let Some(node) = CalcSizeNode::of(id, style) {
            self.calc_sizes.push(node);
        }
    }

    /// CSS's order-modified document order for a flex or grid container's children; `None` when
    /// it is document order.
    fn order_modified(&self, style: &Style, children: &[LayoutId]) -> Option<Vec<LayoutId>> {
        if self.orders.is_empty() || !matches!(style.display, Display::Flex | Display::Grid) {
            return None;
        }
        let order = |id: &LayoutId| self.orders.get(id).copied().unwrap_or(0);
        if children.windows(2).all(|w| order(&w[0]) <= order(&w[1])) {
            return None;
        }
        let mut sorted = children.to_vec();
        sorted.sort_by_key(order);
        Some(sorted)
    }

    pub fn request_measured_layout(
        &mut self,
        style: Style,
        rem_size: Pixels,
        scale_factor: f32,
        measure: impl FnMut(
            Size<Option<Pixels>>,
            Size<AvailableSpace>,
            &mut Window,
            &mut App,
        ) -> Size<Pixels>
        + 'static,
    ) -> LayoutId {
        self.request_measured_layout_in_scope(None, style, rem_size, scale_factor, measure)
    }

    pub(crate) fn request_measured_layout_in_scope(
        &mut self,
        scope: Option<crate::GlobalElementId>,
        style: Style,
        rem_size: Pixels,
        scale_factor: f32,
        measure: impl FnMut(
            Size<Option<Pixels>>,
            Size<AvailableSpace>,
            &mut Window,
            &mut App,
        ) -> Size<Pixels>
        + 'static,
    ) -> LayoutId {
        let taffy_style = style.to_taffy(rem_size, scale_factor);
        let id = if self.retention.enabled {
            retained::request_measured_layout(self, scope, taffy_style, measure)
        } else {
            let measure = Box::new(measure) as Box<MeasureFn>;
            #[cfg(feature = "stacker")]
            let measure = StackSafe::new(measure);

            self.taffy
                .new_leaf_with_context(taffy_style, NodeContext { measure })
                .expect(EXPECT_MESSAGE)
                .into()
        };
        self.note_order(id, &style);
        id
    }

    /// Treats any `auto` dimension of the given node's style as filling `size`.
    ///
    /// This is applied to window roots before layout so they behave like the
    /// root element on the web, which stretches to fill the initial containing
    /// block (the viewport) unless given an explicit size. Explicitly styled
    /// dimensions are preserved.
    pub fn stretch_auto_size_to_fill(
        &mut self,
        id: LayoutId,
        size: Size<Pixels>,
        scale_factor: f32,
    ) {
        if self.retention.enabled {
            retained::stretch(self, id, size, scale_factor);
            return;
        }
        let style = self.taffy.style(id.0).expect(EXPECT_MESSAGE);
        let stretch_width = style.size.width.is_auto();
        let stretch_height = style.size.height.is_auto();
        if !stretch_width && !stretch_height {
            return;
        }
        let mut style = style.clone();
        if stretch_width {
            style.size.width =
                taffy::style::Dimension::length(round_to_device_pixel(size.width.0, scale_factor));
        }
        if stretch_height {
            style.size.height =
                taffy::style::Dimension::length(round_to_device_pixel(size.height.0, scale_factor));
        }
        self.taffy.set_style(id.0, style).expect(EXPECT_MESSAGE);
    }

    // Used to understand performance
    #[allow(dead_code)]
    fn count_all_children(&self, parent: LayoutId) -> anyhow::Result<u32> {
        let mut count = 0;

        for child in self.taffy.children(parent.0)? {
            // Count this child.
            count += 1;

            // Count all of this child's children.
            count += self.count_all_children(LayoutId(child))?
        }

        Ok(count)
    }

    // Used to understand performance
    #[allow(dead_code)]
    fn max_depth(&self, depth: u32, parent: LayoutId) -> anyhow::Result<u32> {
        println!(
            "{parent:?} at depth {depth} has {} children",
            self.taffy.child_count(parent.0)
        );

        let mut max_child_depth = 0;

        for child in self.taffy.children(parent.0)? {
            max_child_depth = std::cmp::max(max_child_depth, self.max_depth(0, LayoutId(child))?);
        }

        Ok(depth + 1 + max_child_depth)
    }

    // Used to understand performance
    #[allow(dead_code)]
    fn get_edges(&self, parent: LayoutId) -> anyhow::Result<Vec<(LayoutId, LayoutId)>> {
        let mut edges = Vec::new();

        for child in self.taffy.children(parent.0)? {
            edges.push((parent, LayoutId(child)));

            edges.extend(self.get_edges(LayoutId(child))?);
        }

        Ok(edges)
    }

    /// vue-native fork: `id` is laid out as a root, so it has no parent.
    ///
    /// A retained node keeps the Taffy parent it had last frame until that
    /// parent is requested again with other children, or freed, and an
    /// unrequested one is only freed when the frame ends. A node that was a
    /// child and is a root this frame would be placed at the stale parent's
    /// origin by [`Self::layout_bounds`].
    pub(crate) fn detach_root(&mut self, id: LayoutId) {
        if let Some(parent) = self.taffy.parent(id.0) {
            self.taffy.remove_child(parent, id.0).expect(EXPECT_MESSAGE);
        }
    }

    #[cfg_attr(feature = "stacker", stacksafe::stacksafe)]
    pub fn compute_layout(
        &mut self,
        id: LayoutId,
        available_space: Size<AvailableSpace>,
        window: &mut Window,
        cx: &mut App,
    ) {
        // Leaving this here until we have a better instrumentation approach.
        // println!("Laying out {} children", self.count_all_children(id)?);
        // println!("Max layout depth: {}", self.max_depth(0, id)?);

        // Output the edges (branches) of the tree in Mermaid format for visualization.
        // println!("Edges:");
        // for (a, b) in self.get_edges(id)? {
        //     println!("N{} --> N{}", u64::from(a), u64::from(b));
        // }
        //

        if self.retention.enabled {
            self.detach_root(id);
        }

        if !self.computed_layouts.insert(id) {
            let stack = &mut self.layout_bounds_scratch_space;
            stack.push(id);
            while let Some(id) = stack.pop() {
                self.absolute_layout_bounds.remove(&id);
                self.absolute_outer_origins.remove(&id);
                stack.extend(
                    self.taffy
                        .children(id.into())
                        .expect(EXPECT_MESSAGE)
                        .into_iter()
                        .map(LayoutId::from),
                );
            }
        }

        let scale_factor = window.scale_factor();

        let transform = |v: AvailableSpace| match v {
            AvailableSpace::Definite(pixels) => {
                AvailableSpace::Definite(Pixels(pixels.0 * scale_factor))
            }
            AvailableSpace::MinContent => AvailableSpace::MinContent,
            AvailableSpace::MaxContent => AvailableSpace::MaxContent,
        };
        let available_space = size(
            transform(available_space.width),
            transform(available_space.height),
        );

        if self.retention.enabled {
            retained::prepare_compute(self, scale_factor);
            retained::replay(self, window, cx);
        }
        let calc_sizes = self.take_calc_sizes(id);
        if !calc_sizes.is_empty() {
            let requested = self.probe_calc_sizes(&calc_sizes);
            self.run_taffy(id, available_space, scale_factor, window, cx);
            let rem_size = window.rem_size();
            self.resolve_calc_sizes(&calc_sizes, requested, rem_size, scale_factor);
        }
        self.run_taffy(id, available_space, scale_factor, window, cx);
        if self.retention.enabled {
            retained::settle(self, window, cx);
        }
    }

    fn run_taffy(
        &mut self,
        id: LayoutId,
        available_space: Size<AvailableSpace>,
        scale_factor: f32,
        window: &mut Window,
        cx: &mut App,
    ) {
        let retention = &mut self.retention;
        self.taffy
            .compute_layout_with_measure(
                id.into(),
                available_space.into(),
                |input, id, node_context, style| {
                    taffy::compute_leaf_layout(
                        input,
                        style,
                        // vue-native fork: a leaf's padding, border and size
                        // can be a `DefiniteLength::Calc` too (taffy 0.15 asks
                        // the caller to resolve them here, not the tree).
                        resolve_calc_handle,
                        |known_dimensions, available_space| {
                            let Some(node_context) = node_context else {
                                return taffy::geometry::Size::default();
                            };

                            let known_dimensions = Size {
                                width: known_dimensions.width.map(|e| Pixels(e / scale_factor)),
                                height: known_dimensions.height.map(|e| Pixels(e / scale_factor)),
                            };

                            let available_space: Size<AvailableSpace> = available_space.into();
                            let untransform = |ev: AvailableSpace| match ev {
                                AvailableSpace::Definite(pixels) => {
                                    AvailableSpace::Definite(Pixels(pixels.0 / scale_factor))
                                }
                                AvailableSpace::MinContent => AvailableSpace::MinContent,
                                AvailableSpace::MaxContent => AvailableSpace::MaxContent,
                            };
                            let available_space = size(
                                untransform(available_space.width),
                                untransform(available_space.height),
                            );

                            if retention.enabled {
                                retained::note_measure(
                                    retention,
                                    id,
                                    known_dimensions,
                                    available_space,
                                );
                            }
                            let measured_size: Size<Pixels> = (node_context.measure)(
                                known_dimensions,
                                available_space,
                                window,
                                cx,
                            );
                            snap_measured_size_to_device_pixels(measured_size, scale_factor).into()
                        },
                    )
                },
            )
            .expect(EXPECT_MESSAGE);
    }

    /// vue-native fork: the `calc-size()` nodes under `root`; the others wait for their own root.
    fn take_calc_sizes(&mut self, root: LayoutId) -> Vec<CalcSizeNode> {
        if self.calc_sizes.is_empty() {
            return Vec::new();
        }
        let taffy = &self.taffy;
        let under = |mut node: NodeId| loop {
            if node == root.0 {
                return true;
            }
            match taffy.parent(node) {
                Some(parent) => node = parent,
                None => return false,
            }
        };
        let (mine, rest) = std::mem::take(&mut self.calc_sizes)
            .into_iter()
            .partition(|n| under(n.id.0));
        self.calc_sizes = rest;
        mine
    }

    /// The first pass lays each `calc-size()` axis out at its basis keyword, unclamped and (for a
    /// flex basis) unflexed, so the box's size there is the basis's `size`. Returns the styles
    /// as requested.
    fn probe_calc_sizes(&mut self, nodes: &[CalcSizeNode]) -> Vec<taffy::style::Style> {
        nodes
            .iter()
            .map(|node| {
                let requested = self.taffy.style(node.id.0).expect(EXPECT_MESSAGE).clone();
                let mut probe = requested.clone();
                for (axis, props) in [(0, &node.width), (1, &node.height)] {
                    let Some(first) = props.iter().flatten().next() else {
                        continue;
                    };
                    let size = basis_dimension(first.basis);
                    let none = taffy::style::LengthPercentageAuto::auto();
                    if axis == 0 {
                        probe.size.width = size;
                        probe.min_size.width = none;
                        probe.max_size.width = none;
                    } else {
                        probe.size.height = size;
                        probe.min_size.height = none;
                        probe.max_size.height = none;
                    }
                }
                if let (Some(basis), Some(_)) = (node.flex_basis, self.main_axis(node.id)) {
                    probe.flex_basis = basis_dimension(basis.basis);
                    probe.flex_grow = 0.;
                    probe.flex_shrink = 0.;
                }
                if probe != requested {
                    self.taffy.set_style(node.id.0, probe).expect(EXPECT_MESSAGE);
                }
                requested
            })
            .collect()
    }

    /// The second pass's styles: each `calc-size()` as the length its function gives for the
    /// measured basis, in the box-sizing box.
    fn resolve_calc_sizes(
        &mut self,
        nodes: &[CalcSizeNode],
        requested: Vec<taffy::style::Style>,
        rem_size: Pixels,
        scale_factor: f32,
    ) {
        for (node, mut style) in nodes.iter().zip(requested) {
            let layout = *self.taffy.layout(node.id.0).expect(EXPECT_MESSAGE);
            let content_box = style.box_sizing == taffy::style::BoxSizing::ContentBox;
            let inner = |outer: f32, a: f32, b: f32, c: f32, d: f32| {
                if content_box {
                    (outer - a - b - c - d).max(0.)
                } else {
                    outer
                }
            };
            let measured = [
                inner(
                    layout.size.width,
                    layout.padding.left,
                    layout.padding.right,
                    layout.border.left,
                    layout.border.right,
                ),
                inner(
                    layout.size.height,
                    layout.padding.top,
                    layout.padding.bottom,
                    layout.border.top,
                    layout.border.bottom,
                ),
            ];
            let resolve = |c: &crate::VnCalcSize, axis: usize| {
                calc_size_value(c, measured[axis] / scale_factor, rem_size, scale_factor)
            };
            let dimension = |v: Result<*const (), f32>| match v {
                Ok(handle) => taffy::style::Dimension::calc(handle),
                Err(length) => taffy::style::Dimension::length(length),
            };
            let bound = |v: Result<*const (), f32>| match v {
                Ok(handle) => taffy::style::LengthPercentageAuto::calc(handle),
                Err(length) => taffy::style::LengthPercentageAuto::length(length),
            };
            for axis in 0..2 {
                let [size, min, max] = if axis == 0 { node.width } else { node.height };
                let (s, lo, hi) = if axis == 0 {
                    (
                        &mut style.size.width,
                        &mut style.min_size.width,
                        &mut style.max_size.width,
                    )
                } else {
                    (
                        &mut style.size.height,
                        &mut style.min_size.height,
                        &mut style.max_size.height,
                    )
                };
                if let Some(c) = size {
                    *s = dimension(resolve(&c, axis));
                }
                if let Some(c) = min {
                    *lo = bound(resolve(&c, axis));
                }
                if let Some(c) = max {
                    *hi = bound(resolve(&c, axis));
                }
            }
            if let (Some(c), Some(axis)) = (node.flex_basis, self.main_axis(node.id)) {
                style.flex_basis = dimension(resolve(&c, axis));
            }
            if self.taffy.style(node.id.0).expect(EXPECT_MESSAGE) != &style {
                self.taffy.set_style(node.id.0, style).expect(EXPECT_MESSAGE);
            }
        }
    }

    /// The flex container's main axis for `id`, 0 for rows and 1 for columns.
    fn main_axis(&self, id: LayoutId) -> Option<usize> {
        let parent = self.taffy.parent(id.0)?;
        let style = self.taffy.style(parent).ok()?;
        use taffy::style::FlexDirection as F;
        (style.display == taffy::style::Display::Flex).then(|| match style.flex_direction {
            F::Row | F::RowReverse => 0,
            F::Column | F::ColumnReverse => 1,
        })
    }

    // Pixel snapping
    //
    // Painting primitives at non-integer pixel coordinates produces blurry
    // output. Pixel snapping converts layout coordinates into integer
    // device-pixel coordinates so painted edges land exactly on physical
    // pixel boundaries.
    //
    // Non-integer coordinates can arise for several reasons, including:
    //   - flex distribution, percentages, centering, and text measurement
    //     can produce fractional element sizes and positions;
    //   - at fractional scale factors (for example 125% or 150%), integer
    //     logical-pixel values can map to non-integer device-pixel values.
    //
    // We pixel-snap by rounding in device-pixel space, after multiplying
    // by `scale_factor`, so that snapping targets physical pixels. Bounds
    // are divided by `scale_factor` before being returned to GPUI.
    //
    // Midpoints are rounded toward zero. This is a stylistic choice: a
    // 1-logical-pixel line at 150% scale should render as 1 dp rather than
    // 2 dp.
    //
    // Pixel snapping is done in two phases:
    //
    //  1. Pre-layout metric snapping. Before Taffy computes layout, all
    //     authored absolute lengths are rounded in `to_taffy`. This
    //     includes borders, padding, gaps, and explicit sizes.
    //     Custom-measured leaf nodes have their measured sizes rounded up
    //     to integer device-pixel lengths.
    //
    //  2. Post-layout edge snapping. After Taffy resolves the tree, layout
    //     relationships such as flex shares, grid tracks, percentages, and
    //     centering can produce new fractional edge positions. Boxes now
    //     have edges in absolute coordinates, and snapping must decide
    //     where those edges land on the device-pixel grid.
    //
    // Ideally, post-layout snapping would satisfy:
    //
    //  - Edge closure. Two raw layout edges at the same absolute position
    //    should snap to the same pixel column.
    //  - Translation stability. A component's internal geometry should not
    //    change when it moves to a new absolute position.
    //
    // These goals are in tension because rounding is not associative.
    // The simple local schemes make different tradeoffs:
    //
    //  - Absolute edge rounding gives each window coordinate one answer,
    //    so coincident edges always close globally. But a span's snapped
    //    length is `round(far) - round(near)`, which may change by 1 dp
    //    as its absolute origin moves.
    //
    //  - Parent-relative edge rounding rounds each child inside its
    //    parent's coordinate space. This guarantees translation stability,
    //    but a shared edge reached through different parents can
    //    accumulate different rounding, causing non-closure between
    //    cousins.
    //
    //  - Length rounding rounds each width, height, and thickness
    //    independently and then places boxes from those rounded lengths.
    //    Sizes stay stable under translation, but neighboring boxes derive
    //    their shared boundary from different sources, so closure is not
    //    guaranteed.
    //
    // We apply absolute edge rounding for each element's outer box in
    // post-layout rounding to preserve closure. Border and padding widths
    // are not touched by post-layout rounding; they keep their pre-layout
    // rounded value so that they remain stable under translation.
    //
    // This gives both closure and translation stability in the case that
    // all local metrics are integer device-pixel lengths. Pre-layout
    // rounding covers that in most cases. The exception is metrics
    // resolved by layout relationships, such as percentages. Outer box
    // edges will still close globally, and painted border widths are still
    // snapped independently, but the raw content-box origin can carry a
    // 1dp residual into descendants.

    /// vue-native fork: whether the node is out of flow (`position:
    /// absolute`), which a scroll container measures without its padding.
    pub fn is_absolute(&self, id: LayoutId) -> bool {
        self.taffy
            .style(id.0)
            .is_ok_and(|style| style.position == taffy::style::Position::Absolute)
    }

    pub fn layout_bounds(&mut self, id: LayoutId, scale_factor: f32) -> Bounds<Pixels> {
        if let Some(layout) = self.absolute_layout_bounds.get(&id).cloned() {
            return layout;
        }

        let layout = self.taffy.layout(id.into()).expect(EXPECT_MESSAGE);
        let layout_location = layout.location;
        let layout_size = layout.size;
        let parent = self.taffy.parent(id.0);

        let absolute_outer_origin = match parent {
            Some(parent_id) => {
                let parent_id = LayoutId::from(parent_id);
                self.layout_bounds(parent_id, scale_factor);
                let parent_origin = *self
                    .absolute_outer_origins
                    .get(&parent_id)
                    .expect("parent absolute outer origin should be cached");
                parent_origin + Point::from(layout_location)
            }
            None => Point::from(layout_location),
        };
        self.absolute_outer_origins
            .insert(id, absolute_outer_origin);

        let absolute_far = absolute_outer_origin + Point::from(Size::from(layout_size));
        let snapped_bounds = Bounds::from_corners(
            absolute_outer_origin.map(round_half_toward_zero),
            absolute_far.map(round_half_toward_zero),
        );

        let bounds = (snapped_bounds / scale_factor).map(Pixels);
        self.absolute_layout_bounds.insert(id, bounds);
        bounds
    }
}

/// A unique identifier for a layout node, generated when requesting a layout from Taffy
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
#[repr(transparent)]
pub struct LayoutId(NodeId);

impl LayoutId {
    fn to_taffy_slice(node_ids: &[Self]) -> &[taffy::NodeId] {
        // SAFETY: LayoutId is repr(transparent) to taffy::tree::NodeId.
        unsafe { std::mem::transmute::<&[LayoutId], &[taffy::NodeId]>(node_ids) }
    }
}

impl std::hash::Hash for LayoutId {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        u64::from(self.0).hash(state);
    }
}

impl From<NodeId> for LayoutId {
    fn from(node_id: NodeId) -> Self {
        Self(node_id)
    }
}

impl From<LayoutId> for NodeId {
    fn from(layout_id: LayoutId) -> NodeId {
        layout_id.0
    }
}

fn snap_measured_size_to_device_pixels(size: Size<Pixels>, scale_factor: f32) -> Size<f32> {
    size.map(|d| ceil_to_device_pixel(d.0.max(0.0), scale_factor))
}

fn border_widths_to_taffy(
    widths: &Edges<AbsoluteLength>,
    rem_size: Pixels,
    scale_factor: f32,
) -> TaffyRect<taffy::style::LengthPercentage> {
    let snap = |w: &AbsoluteLength| {
        taffy::style::LengthPercentage::length(round_stroke_to_device_pixel(
            w.to_pixels(rem_size).0,
            scale_factor,
        ))
    };
    TaffyRect {
        top: snap(&widths.top),
        right: snap(&widths.right),
        bottom: snap(&widths.bottom),
        left: snap(&widths.left),
    }
}

trait ToTaffy<Output> {
    fn to_taffy(&self, rem_size: Pixels, scale_factor: f32) -> Output;
}

impl ToTaffy<taffy::style::Style> for Style {
    fn to_taffy(&self, rem_size: Pixels, scale_factor: f32) -> taffy::style::Style {
        use taffy::style_helpers::{fr, length, minmax, repeat};

        fn to_grid_line(
            placement: &Range<crate::GridPlacement>,
        ) -> taffy::Line<taffy::GridPlacement> {
            taffy::Line {
                start: placement.start.into(),
                end: placement.end.into(),
            }
        }

        fn to_grid_repeat<T: taffy::style::CheapCloneStr>(
            unit: &Option<GridTemplate>,
        ) -> Vec<taffy::GridTemplateComponent<T>> {
            unit.map(|template| {
                match template.min_size {
                    // grid-template-*: repeat(<number>, minmax(0, 1fr));
                    crate::GridTemplateMinSize::Zero => {
                        vec![repeat(
                            template.repeat,
                            vec![minmax(length(0.0_f32), fr(1.0_f32))],
                        )]
                    }
                    // grid-template-*: repeat(<number>, minmax(min-content, 1fr));
                    crate::GridTemplateMinSize::MinContent => {
                        vec![repeat(
                            template.repeat,
                            vec![minmax(min_content(), fr(1.0_f32))],
                        )]
                    }
                    // grid-template-*: repeat(<number>, minmax(0, max-content))
                    crate::GridTemplateMinSize::MaxContent => {
                        vec![repeat(
                            template.repeat,
                            vec![minmax(length(0.0_f32), max_content())],
                        )]
                    }
                }
            })
            .unwrap_or_default()
        }

        // An ARBITRARY track list (`1fr auto`, `minmax(0,1fr) 200px`), which
        // `to_grid_repeat`'s one-shape `repeat()` cannot express. Each track is
        // already in `minmax()` normal form, so this is a straight translation.
        fn to_track_size_min(size: crate::GridTrackSize) -> taffy::MinTrackSizingFunction {
            use crate::GridTrackSize as G;
            use taffy::MinTrackSizingFunction as M;
            match size {
                G::Auto => M::auto(),
                G::MinContent => M::min_content(),
                G::MaxContent => M::max_content(),
                G::Px(v) => M::length(v),
                G::Percent(v) => M::percent(v),
                // `<flex>` is not a valid track MINIMUM in CSS; it computes to
                // `auto` there, so it does here.
                G::Fr(_) => M::auto(),
            }
        }
        fn to_track_size_max(size: crate::GridTrackSize) -> taffy::MaxTrackSizingFunction {
            use crate::GridTrackSize as G;
            use taffy::MaxTrackSizingFunction as M;
            match size {
                G::Auto => M::auto(),
                G::MinContent => M::min_content(),
                G::MaxContent => M::max_content(),
                G::Px(v) => M::length(v),
                G::Percent(v) => M::percent(v),
                G::Fr(v) => M::fr(v),
            }
        }
        fn to_grid_tracks<T: taffy::style::CheapCloneStr>(
            tracks: &Option<Vec<crate::GridTrack>>,
            auto: Option<crate::GridAutoRepeat>,
        ) -> Option<Vec<taffy::GridTemplateComponent<T>>> {
            let track =
                |t: &crate::GridTrack| minmax(to_track_size_min(t.min), to_track_size_max(t.max));
            tracks.as_ref().map(|list| {
                // vue-native fork: one `repeat(auto-fill | auto-fit, …)` run,
                // which taffy expands against the container at layout time.
                let run = auto
                    .map(|a| a.start as usize..(a.start as usize + a.len as usize))
                    .filter(|run| !run.is_empty() && run.end <= list.len());
                let mut out = Vec::with_capacity(list.len());
                for (index, t) in list.iter().enumerate() {
                    match (&run, auto) {
                        (Some(run), Some(a)) if run.contains(&index) => {
                            if index == run.start {
                                let count = if a.fit {
                                    taffy::RepetitionCount::AutoFit
                                } else {
                                    taffy::RepetitionCount::AutoFill
                                };
                                out.push(repeat(
                                    count,
                                    list[run.clone()].iter().map(track).collect(),
                                ));
                            }
                        }
                        _ => out.push(taffy::GridTemplateComponent::Single(track(t))),
                    }
                }
                out
            })
        }

        taffy::style::Style {
            display: self.display.into(),
            overflow: self.overflow.into(),
            scrollbar_width: self.scrollbar_width.to_taffy(rem_size, scale_factor),
            position: self.position.into(),
            inset: self.inset.to_taffy(rem_size, scale_factor),
            size: self.size.to_taffy(rem_size, scale_factor),
            min_size: self.min_size.to_taffy(rem_size, scale_factor),
            max_size: self.max_size.to_taffy(rem_size, scale_factor),
            aspect_ratio: self
                .aspect_ratio
                .map(f32::abs)
                .filter(|ratio| ratio.is_finite() && *ratio > 0.0),
            margin: self.margin.to_taffy(rem_size, scale_factor),
            padding: self.padding.to_taffy(rem_size, scale_factor),
            border: border_widths_to_taffy(&self.border_widths, rem_size, scale_factor),
            align_items: self.align_items.map(Into::into).unwrap_or_default(),
            align_self: self.align_self.map(|x| x.into()),
            align_content: self.align_content.map(Into::into).unwrap_or_default(),
            justify_content: self.justify_content.map(Into::into).unwrap_or_default(),
            gap: self.gap.to_taffy(rem_size, scale_factor),
            flex_direction: self.flex_direction.into(),
            flex_wrap: self.flex_wrap.into(),
            flex_basis: self.flex_basis.to_taffy(rem_size, scale_factor),
            flex_grow: self.flex_grow,
            flex_shrink: self.flex_shrink,
            // An explicit track list wins over the `repeat(N, …)` shorthand.
            grid_template_rows: to_grid_tracks(&self.vn_grid_rows, self.vn_grid_rows_auto)
                .unwrap_or_else(|| to_grid_repeat(&self.grid_rows)),
            grid_template_columns: to_grid_tracks(&self.vn_grid_cols, self.vn_grid_cols_auto)
                .unwrap_or_else(|| to_grid_repeat(&self.grid_cols)),
            grid_auto_columns: self.vn_grid_auto_cols.as_ref().map(|tracks| tracks.iter()

                .map(|track| minmax(to_track_size_min(track.min), to_track_size_max(track.max)))
                .collect()).unwrap_or_default(),
            grid_auto_rows: self.vn_grid_auto_rows.as_ref().map(|tracks| tracks.iter()

                .map(|track| minmax(to_track_size_min(track.min), to_track_size_max(track.max)))
                .collect()).unwrap_or_default(),
            grid_auto_flow: match self.vn_grid_auto_flow.unwrap_or_default() {
                crate::GridAutoFlow::Row => taffy::GridAutoFlow::Row,
                crate::GridAutoFlow::Column => taffy::GridAutoFlow::Column,
                crate::GridAutoFlow::RowDense => taffy::GridAutoFlow::RowDense,
                crate::GridAutoFlow::ColumnDense => taffy::GridAutoFlow::ColumnDense,
            },
            justify_items: self.vn_justify_items.map(Into::into).unwrap_or_default(),
            justify_self: self.vn_justify_self.map(|x| x.into()),
            grid_row: self
                .grid_location
                .as_ref()
                .map(|location| to_grid_line(&location.row))
                .unwrap_or_default(),
            grid_column: self
                .grid_location
                .as_ref()
                .map(|location| to_grid_line(&location.column))
                .unwrap_or_default(),
            contain: {
                let mut contain = taffy::Contain::NONE;
                if self.vn_contain_size & Style::CONTAIN_INLINE_SIZE != 0 {
                    contain |= taffy::Contain::INLINE_SIZE;
                }
                if self.vn_contain_size & Style::CONTAIN_BLOCK_SIZE != 0 {
                    contain |= taffy::Contain::BLOCK_SIZE;
                }
                contain
            },
            ..Default::default()
        }
    }
}

impl ToTaffy<f32> for AbsoluteLength {
    fn to_taffy(&self, rem_size: Pixels, scale_factor: f32) -> f32 {
        round_to_device_pixel(self.to_pixels(rem_size).0, scale_factor)
    }
}

impl ToTaffy<taffy::style::LengthPercentageAuto> for Length {
    fn to_taffy(
        &self,
        rem_size: Pixels,
        scale_factor: f32,
    ) -> taffy::prelude::LengthPercentageAuto {
        match self {
            Length::Definite(length) => length.to_taffy(rem_size, scale_factor),
            Length::Auto | Length::VnCalcSize(_) => taffy::prelude::LengthPercentageAuto::auto(),
        }
    }
}

impl ToTaffy<taffy::style::Dimension> for Length {
    fn to_taffy(&self, rem_size: Pixels, scale_factor: f32) -> taffy::prelude::Dimension {
        match self {
            Length::Definite(length) => length.to_taffy(rem_size, scale_factor),
            Length::Auto => taffy::prelude::Dimension::auto(),
            Length::VnCalcSize(c) => basis_dimension(c.basis),
        }
    }
}

/// vue-native fork: a node whose sizing properties hold a `calc-size()` other than its basis
/// alone, which [`TaffyLayoutEngine::compute_layout`] lays out twice. Per axis: size, min, max.
struct CalcSizeNode {
    id: LayoutId,
    width: [Option<crate::VnCalcSize>; 3],
    height: [Option<crate::VnCalcSize>; 3],
    flex_basis: Option<crate::VnCalcSize>,
}

impl CalcSizeNode {
    fn of(id: LayoutId, style: &Style) -> Option<Self> {
        let calc = |l: &Length| match l {
            Length::VnCalcSize(c) if !c.is_basis() => Some(*c),
            _ => None,
        };
        let node = CalcSizeNode {
            id,
            width: [
                calc(&style.size.width),
                calc(&style.min_size.width),
                calc(&style.max_size.width),
            ],
            height: [
                calc(&style.size.height),
                calc(&style.min_size.height),
                calc(&style.max_size.height),
            ],
            flex_basis: calc(&style.flex_basis),
        };
        (node.width.iter().chain(&node.height).any(Option::is_some) || node.flex_basis.is_some())
            .then_some(node)
    }
}

/// `size * scale + offset` with `size` the measured basis in logical pixels, as [`calc_handle`]
/// returns it: a percentage in the offset still resolves against the containing block.
fn calc_size_value(
    c: &crate::VnCalcSize,
    size: f32,
    rem_size: Pixels,
    scale_factor: f32,
) -> Result<*const (), f32> {
    let scaled = c.scale * size;
    let (fraction, pixels) = match c.offset {
        DefiniteLength::Absolute(a) => (0., scaled + a.to_pixels(rem_size).0),
        DefiniteLength::Fraction(f) => (f, scaled),
        DefiniteLength::Calc { fraction, pixels } => (fraction, scaled + pixels.0),
    };
    if fraction == 0. {
        return Err(round_to_device_pixel(pixels.max(0.), scale_factor));
    }
    calc_handle(fraction, Pixels(pixels), scale_factor)
}

/// vue-native fork: the keyword a `calc-size()` is measured from; also its whole layout when it
/// is the identity (`calc-size(auto, size)`).
fn basis_dimension(basis: crate::VnSizeBasis) -> taffy::style::Dimension {
    use crate::VnSizeBasis as B;
    use taffy::style::Dimension as D;
    match basis {
        B::Auto => D::auto(),
        B::MinContent => D::min_content(),
        B::MaxContent => D::max_content(),
        B::FitContent => D::fit_content(),
        B::Content => D::content(),
    }
}

impl ToTaffy<taffy::style::LengthPercentage> for DefiniteLength {
    fn to_taffy(&self, rem_size: Pixels, scale_factor: f32) -> taffy::style::LengthPercentage {
        match self {
            DefiniteLength::Absolute(length) => length.to_taffy(rem_size, scale_factor),
            DefiniteLength::Fraction(fraction) => {
                taffy::style::LengthPercentage::percent(*fraction)
            }
            DefiniteLength::Calc { fraction, pixels } => {
                match calc_handle(*fraction, *pixels, scale_factor) {
                    Ok(handle) => taffy::style::LengthPercentage::calc(handle),
                    Err(pixels) => taffy::style::LengthPercentage::length(pixels),
                }
            }
        }
    }
}

impl ToTaffy<taffy::style::LengthPercentageAuto> for DefiniteLength {
    fn to_taffy(&self, rem_size: Pixels, scale_factor: f32) -> taffy::style::LengthPercentageAuto {
        match self {
            DefiniteLength::Absolute(length) => length.to_taffy(rem_size, scale_factor),
            DefiniteLength::Fraction(fraction) => {
                taffy::style::LengthPercentageAuto::percent(*fraction)
            }
            DefiniteLength::Calc { fraction, pixels } => {
                match calc_handle(*fraction, *pixels, scale_factor) {
                    Ok(handle) => taffy::style::LengthPercentageAuto::calc(handle),
                    Err(pixels) => taffy::style::LengthPercentageAuto::length(pixels),
                }
            }
        }
    }
}

impl ToTaffy<taffy::style::Dimension> for DefiniteLength {
    fn to_taffy(&self, rem_size: Pixels, scale_factor: f32) -> taffy::style::Dimension {
        match self {
            DefiniteLength::Absolute(length) => length.to_taffy(rem_size, scale_factor),
            DefiniteLength::Fraction(fraction) => taffy::style::Dimension::percent(*fraction),
            DefiniteLength::Calc { fraction, pixels } => {
                match calc_handle(*fraction, *pixels, scale_factor) {
                    Ok(handle) => taffy::style::Dimension::calc(handle),
                    Err(pixels) => taffy::style::Dimension::length(pixels),
                }
            }
        }
    }
}

/// vue-native fork: the taffy `calc()` handle for [`DefiniteLength::Calc`].
///
/// taffy treats a calc value as an opaque, 8-byte-aligned handle and asks its
/// tree to resolve it against the percentage basis (`resolve_calc_value`). A
/// pointer to a heap node would need an owner and a lifetime; the linear form
/// `fraction * basis + pixels` fits in the handle itself, so it is packed
/// there: the length's `f32` bits in the high half, the fraction's in the low
/// half with its three lowest mantissa bits cleared for taffy's tag (a relative
/// error under 2^-20). vue-native's vendored taffy (`vendor/taffy`, patched
/// into the vue-native workspace) decodes exactly this in
/// `TaffyView::resolve_calc_value`; the two halves are a contract. Upstream
/// taffy resolves every calc to zero, which is what this degrades to in a
/// build against it (only vue-native ever writes a `Calc`).
///
/// `Err(pixels)` when there is nothing to pack: a zero fraction is a plain
/// length, and taffy reserves the all-zero handle. On a 32-bit target the
/// handle has no room for two `f32`s, so the percentage half is dropped there.
fn calc_handle(fraction: f32, pixels: Pixels, scale_factor: f32) -> Result<*const (), f32> {
    let length = round_to_device_pixel(pixels.0, scale_factor);
    if fraction == 0. {
        return Err(length);
    }
    #[cfg(target_pointer_width = "64")]
    {
        let bits = ((length.to_bits() as u64) << 32) | (fraction.to_bits() & !0b111) as u64;
        if bits == 0 {
            return Err(length);
        }
        Ok(bits as usize as *const ())
    }
    #[cfg(not(target_pointer_width = "64"))]
    {
        Err(length)
    }
}

/// vue-native fork: the inverse of [`calc_handle`], for the leaves taffy 0.15
/// lays out through `compute_leaf_layout` with a caller-supplied resolver. It
/// must decode exactly what the vendored taffy's
/// `TaffyView::resolve_calc_value` does for every other node.
#[inline(always)]
fn resolve_calc_handle(val: *const (), basis: f32) -> f32 {
    #[cfg(target_pointer_width = "64")]
    {
        let bits = val as usize as u64;
        let length = f32::from_bits((bits >> 32) as u32);
        let fraction = f32::from_bits(bits as u32 & !0b111);
        fraction * basis + length
    }
    #[cfg(not(target_pointer_width = "64"))]
    {
        let _ = (val, basis);
        0.0
    }
}

impl ToTaffy<taffy::style::LengthPercentage> for AbsoluteLength {
    fn to_taffy(&self, rem_size: Pixels, scale_factor: f32) -> taffy::style::LengthPercentage {
        taffy::style::LengthPercentage::length(self.to_taffy(rem_size, scale_factor))
    }
}

impl ToTaffy<taffy::style::LengthPercentageAuto> for AbsoluteLength {
    fn to_taffy(&self, rem_size: Pixels, scale_factor: f32) -> taffy::style::LengthPercentageAuto {
        taffy::style::LengthPercentageAuto::length(self.to_taffy(rem_size, scale_factor))
    }
}

impl ToTaffy<taffy::style::Dimension> for AbsoluteLength {
    fn to_taffy(&self, rem_size: Pixels, scale_factor: f32) -> taffy::style::Dimension {
        taffy::style::Dimension::length(self.to_taffy(rem_size, scale_factor))
    }
}

impl<T, T2> From<TaffyPoint<T>> for Point<T2>
where
    T: Into<T2>,
    T2: Clone + Debug + Default + PartialEq,
{
    fn from(point: TaffyPoint<T>) -> Point<T2> {
        Point {
            x: point.x.into(),
            y: point.y.into(),
        }
    }
}

impl<T, T2> From<Point<T>> for TaffyPoint<T2>
where
    T: Into<T2> + Clone + Debug + Default + PartialEq,
{
    fn from(val: Point<T>) -> Self {
        TaffyPoint {
            x: val.x.into(),
            y: val.y.into(),
        }
    }
}

impl<T, U> ToTaffy<TaffySize<U>> for Size<T>
where
    T: ToTaffy<U> + Clone + Debug + Default + PartialEq,
{
    fn to_taffy(&self, rem_size: Pixels, scale_factor: f32) -> TaffySize<U> {
        TaffySize {
            width: self.width.to_taffy(rem_size, scale_factor),
            height: self.height.to_taffy(rem_size, scale_factor),
        }
    }
}

impl<T, U> ToTaffy<TaffyRect<U>> for Edges<T>
where
    T: ToTaffy<U> + Clone + Debug + Default + PartialEq,
{
    fn to_taffy(&self, rem_size: Pixels, scale_factor: f32) -> TaffyRect<U> {
        TaffyRect {
            top: self.top.to_taffy(rem_size, scale_factor),
            right: self.right.to_taffy(rem_size, scale_factor),
            bottom: self.bottom.to_taffy(rem_size, scale_factor),
            left: self.left.to_taffy(rem_size, scale_factor),
        }
    }
}

impl<T, U> From<TaffySize<T>> for Size<U>
where
    T: Into<U>,
    U: Clone + Debug + Default + PartialEq,
{
    fn from(taffy_size: TaffySize<T>) -> Self {
        Size {
            width: taffy_size.width.into(),
            height: taffy_size.height.into(),
        }
    }
}

impl<T, U> From<Size<T>> for TaffySize<U>
where
    T: Into<U> + Clone + Debug + Default + PartialEq,
{
    fn from(size: Size<T>) -> Self {
        TaffySize {
            width: size.width.into(),
            height: size.height.into(),
        }
    }
}

/// The space available for an element to be laid out in
#[derive(Copy, Clone, Default, Debug, Eq, PartialEq)]
pub enum AvailableSpace {
    /// The amount of space available is the specified number of pixels
    Definite(Pixels),
    /// The amount of space available is indefinite and the node should be laid out under a min-content constraint
    #[default]
    MinContent,
    /// The amount of space available is indefinite and the node should be laid out under a max-content constraint
    MaxContent,
}

impl AvailableSpace {
    /// Returns a `Size` with both width and height set to `AvailableSpace::MinContent`.
    ///
    /// This function is useful when you want to create a `Size` with the minimum content constraints
    /// for both dimensions.
    ///
    /// # Examples
    ///
    /// ```
    /// use gpui::AvailableSpace;
    /// let min_content_size = AvailableSpace::min_size();
    /// assert_eq!(min_content_size.width, AvailableSpace::MinContent);
    /// assert_eq!(min_content_size.height, AvailableSpace::MinContent);
    /// ```
    pub const fn min_size() -> Size<Self> {
        Size {
            width: Self::MinContent,
            height: Self::MinContent,
        }
    }
}

impl From<AvailableSpace> for TaffyAvailableSpace {
    fn from(space: AvailableSpace) -> TaffyAvailableSpace {
        match space {
            AvailableSpace::Definite(Pixels(value)) => TaffyAvailableSpace::Definite(value),
            AvailableSpace::MinContent => TaffyAvailableSpace::MinContent,
            AvailableSpace::MaxContent => TaffyAvailableSpace::MaxContent,
        }
    }
}

impl From<TaffyAvailableSpace> for AvailableSpace {
    fn from(space: TaffyAvailableSpace) -> AvailableSpace {
        match space {
            TaffyAvailableSpace::Definite(value) => AvailableSpace::Definite(Pixels(value)),
            TaffyAvailableSpace::MinContent => AvailableSpace::MinContent,
            TaffyAvailableSpace::MaxContent => AvailableSpace::MaxContent,
        }
    }
}

impl From<Pixels> for AvailableSpace {
    fn from(pixels: Pixels) -> Self {
        AvailableSpace::Definite(pixels)
    }
}

impl From<Size<Pixels>> for Size<AvailableSpace> {
    fn from(size: Size<Pixels>) -> Self {
        Size {
            width: AvailableSpace::Definite(size.width),
            height: AvailableSpace::Definite(size.height),
        }
    }
}

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

    #[test]
    fn flex_and_grid_lay_their_children_out_in_order_modified_document_order() {
        let mut engine = TaffyLayoutEngine::new();
        let leaf = |engine: &mut TaffyLayoutEngine, order: i32| {
            let style = Style { vn_order: order, ..Default::default() };
            engine.request_layout(style, Pixels(16.0), 1.0, &[])
        };
        let kids = [leaf(&mut engine, 2), leaf(&mut engine, -1), leaf(&mut engine, 0), leaf(&mut engine, 2)];
        let laid_out = |engine: &mut TaffyLayoutEngine, display: Display| {
            let style = Style { display, ..Default::default() };
            let root = engine.request_layout(style, Pixels(16.0), 1.0, &kids);
            engine.taffy.child_ids(root.into()).map(LayoutId::from).collect::<Vec<_>>()
        };
        let modified = vec![kids[1], kids[2], kids[0], kids[3]];
        assert_eq!(laid_out(&mut engine, Display::Flex), modified);
        assert_eq!(laid_out(&mut engine, Display::Grid), modified);
        assert_eq!(laid_out(&mut engine, Display::Block), kids.to_vec());
        engine.clear();
        let fresh = [leaf(&mut engine, 0), leaf(&mut engine, 0)];
        let root = engine.request_layout(Style { display: Display::Flex, ..Default::default() }, Pixels(16.0), 1.0, &fresh);
        assert!(engine.taffy.child_ids(root.into()).map(LayoutId::from).eq(fresh));
    }

    #[test]
    fn implicit_fractional_columns_share_the_container_width() {
        let mut tree = TaffyTree::<()>::new();
        let children: Vec<_> = (0..3).map(|_| tree.new_leaf(Default::default()).unwrap()).collect();
        let style = Style {
            display: crate::Display::Grid,
            size: Size {
                width: Length::Definite(Pixels(300.0).into()),
                height: Length::Definite(Pixels(40.0).into()),
            },
            vn_grid_auto_flow: Some(crate::GridAutoFlow::Column),
            vn_grid_auto_cols: Some(vec![crate::GridTrack::fr(1.0)]),
            ..Default::default()
        };
        let root = tree.new_with_children(style.to_taffy(Pixels(16.0), 1.0), &children).unwrap();
        tree.compute_layout(root, TaffySize {
            width: TaffyAvailableSpace::Definite(300.0), height: TaffyAvailableSpace::Definite(40.0),
        }).unwrap();
        for (index, child) in children.into_iter().enumerate() {
            let layout = tree.layout(child).unwrap();
            assert_eq!(layout.size.width, 100.0);
            assert_eq!(layout.location.x, index as f32 * 100.0);
        }
    }

    #[test]
    fn implicit_row_patterns_repeat_in_layout() {
        use crate::GridTrackSize as Track;
        let mut tree = TaffyTree::<()>::new();
        let children: Vec<_> = (0..5).map(|_| tree.new_leaf(Default::default()).unwrap()).collect();
        let style = Style {
            display: crate::Display::Grid,
            vn_grid_cols: Some(vec![crate::GridTrack::fixed(Track::Px(100.0))]),
            vn_grid_auto_rows: Some(vec![crate::GridTrack::fixed(Track::Px(20.0)), crate::GridTrack::fixed(Track::Px(35.0))]),
            ..Default::default()
        };
        let root = tree.new_with_children(style.to_taffy(Pixels(16.0), 1.0), &children).unwrap();
        tree.compute_layout(root, TaffySize {
            width: TaffyAvailableSpace::MaxContent, height: TaffyAvailableSpace::MaxContent,
        }).unwrap();
        for ((height, top), child) in [(20.0, 0.0), (35.0, 20.0), (20.0, 55.0), (35.0, 75.0), (20.0, 110.0)].into_iter().zip(children) {
            let layout = tree.layout(child).unwrap();
            assert_eq!(layout.size.height, height);
            assert_eq!(layout.location.y, top);
        }
    }

    #[test]
    fn implicit_auto_reset_replaces_a_fixed_row_pattern() {
        use crate::{GridTrack, GridTrackSize, Refineable, StyleRefinement};
        let mut style = Style {
            display: crate::Display::Grid,
            vn_grid_cols: Some(vec![GridTrack::fixed(GridTrackSize::Px(100.0))]),
            vn_grid_auto_rows: Some(vec![GridTrack::fixed(GridTrackSize::Px(50.0))]),
            ..Default::default()
        };
        style.refine(&StyleRefinement { vn_grid_auto_rows: Some(Vec::new()), ..Default::default() });
        let mut tree = TaffyTree::<()>::new();
        let children: Vec<_> = [17.0, 29.0].into_iter().map(|height| {
            let mut leaf_style = taffy::style::Style::default();
            leaf_style.size.height = taffy::style::Dimension::length(height);
            tree.new_leaf(leaf_style).unwrap()
        }).collect();
        let root = tree.new_with_children(style.to_taffy(Pixels(16.0), 1.0), &children).unwrap();
        tree.compute_layout(root, TaffySize {
            width: TaffyAvailableSpace::MaxContent, height: TaffyAvailableSpace::MaxContent,
        }).unwrap();
        assert_eq!(tree.layout(children[1]).unwrap().location.y, 17.0);
        assert_eq!(tree.layout(root).unwrap().size.height, 46.0);
    }

    #[test]
    fn border_widths_to_taffy_use_stroke_snapping() {
        let border_widths = Edges {
            top: Pixels(0.0).into(),
            right: Pixels(0.4).into(),
            bottom: Pixels(0.5).into(),
            left: Pixels(1.6).into(),
        };
        let taffy_border = border_widths_to_taffy(&border_widths, Pixels(16.0), 1.0);

        assert_eq!(
            taffy_border.top,
            taffy::style::LengthPercentage::length(0.0)
        );
        assert_eq!(
            taffy_border.right,
            taffy::style::LengthPercentage::length(1.0)
        );
        assert_eq!(
            taffy_border.bottom,
            taffy::style::LengthPercentage::length(1.0)
        );
        assert_eq!(
            taffy_border.left,
            taffy::style::LengthPercentage::length(2.0)
        );
    }
}