package toffee

All checked builds pass
  1. Overview
  2. Docs
CSS layout engine for OCaml (Flexbox, Grid, Block)

Install

dune-project
 Dependency

Authors

Maintainers

Sources

mosaic-0.3.0.tbz
sha256=a92cbbcbe016fb19fb0a749222da0e459a9567aef3f4c7340d9bd6b71c53ed6f
sha512=6a7ab054605538bb31a2b73a1a6d093981a2704c9f0af5852221c05735290d74aca7c7282298c4d0f91e86a800e2ccd94b4918af883cd963449c758eb6b272b3

doc/src/toffee_compute_grid/toffee_compute_grid.ml.html

Source file toffee_compute_grid.ml

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
(* Grid layout algorithm implementation This module implements the CSS Grid
   Level 1 specification https://www.w3.org/TR/css-grid-1/ *)

module Style = Toffee_style
module Tree = Toffee_tree
open Toffee_geometry
open Style
open Tree
module Implicit_grid = Implicit_grid
module Explicit_grid = Explicit_grid
module Grid_track_counts = Grid_track_counts
module Cell_occupancy = Cell_occupancy
module Grid_item = Grid_item
module Placement = Placement
module Named = Named

(* Grid layout algorithm This consists of a few phases: - Resolving the explicit
   grid - Placing items (which also resolves the implicit grid) - Track
   (row/column) sizing - Alignment & Final item placement *)
(* The space a re-run sizes the tracks into: for percentage tracks of a
   container whose size was indefinite, its content box, which its content
   just sized *)
let percentage_rerun_space (outer_node_size : float option size) columns rows
    (container_content_box : Sizef.t) (available_grid_space : _ size) =
  Size.
    {
      width =
        (if
           Option.is_none outer_node_size.width
           && Array.exists Grid_track.uses_percentage columns
         then Available_space.Definite container_content_box.width
         else available_grid_space.width);
      height =
        (if
           Option.is_none outer_node_size.height
           && Array.exists Grid_track.uses_percentage rows
         then Available_space.Definite container_content_box.height
         else available_grid_space.height);
    }

(* [size], a border-box size, less [inset] where it is known. *)
let content_box_size (inset : Rectf.t) (size : float option size) =
  match size with
  | { width = None; height = None } -> size
  | _ -> Sizef.maybe_sub (Rectf.sum_axes inset) size

let compute_grid_layout (type t)
    (module Tree : LAYOUT_PARTIAL_TREE with type t = t) ~tree ~node ~inputs =
  let known_dimensions = Layout_input.known_dimensions inputs in
  let parent_size = Layout_input.parent_size inputs in
  let available_space = Layout_input.available_space inputs in
  let run_mode = Layout_input.run_mode inputs in

  let style = Tree.get_core_container_style tree node in

  (* 1. Compute "available grid space" *)
  (* https://www.w3.org/TR/css-grid-1/#available-grid-space *)
  let aspect_ratio = Style.aspect_ratio style in
  let calc = Tree.resolve_calc_value tree in
  let padding =
    Style.padding style
    |> Length_percentage.resolve_rect_or_zero parent_size.width calc
  in
  let border =
    Style.border style
    |> Length_percentage.resolve_rect_or_zero parent_size.width calc
  in
  let padding_border = Rectf.add padding border in
  let padding_border_size = Rectf.sum_axes padding_border in
  let box_sizing_adjustment =
    if Style.box_sizing style = Box_sizing.Content_box then padding_border_size
    else Sizef.zero
  in

  let min_size =
    Style.min_size style
    |> (fun dims ->
    Size.
      {
        width = Dimension.maybe_resolve dims.width parent_size.width calc;
        height = Dimension.maybe_resolve dims.height parent_size.height calc;
      })
    |> Size.apply_aspect_ratio aspect_ratio
    |> Sizef.maybe_add box_sizing_adjustment
  in
  let max_size =
    Style.max_size style
    |> (fun dims ->
    Size.
      {
        width = Dimension.maybe_resolve dims.width parent_size.width calc;
        height = Dimension.maybe_resolve dims.height parent_size.height calc;
      })
    |> Size.apply_aspect_ratio aspect_ratio
    |> Sizef.maybe_add box_sizing_adjustment
  in
  let style_size =
    Style.size style
    |> (fun dims ->
    Size.
      {
        width = Dimension.maybe_resolve dims.width parent_size.width calc;
        height = Dimension.maybe_resolve dims.height parent_size.height calc;
      })
    |> Size.apply_aspect_ratio aspect_ratio
    |> Sizef.maybe_add box_sizing_adjustment
  in
  let preferred_size =
    if Layout_input.sizing_mode inputs = Sizing_mode.Inherent_size then
      style_size
    else Size.none
  in

  (* Scrollbar gutters are reserved when the `overflow` property is set to
     `Overflow::Scroll`. However, the axes are switched (transposed) because a
     node that scrolls vertically needs *horizontal* space to be reserved for a
     scrollbar *)
  let scrollbar_gutter =
    let overflow = Style.overflow style in
    Point.transpose overflow
    |> Point.map (function
      | Overflow.Scroll -> Style.scrollbar_width style
      | _ -> 0.0)
  in
  (* TODO: make side configurable based on the `direction` property *)
  let content_box_inset = padding_border in
  let content_box_inset =
    {
      content_box_inset with
      right = content_box_inset.right +. scrollbar_gutter.x;
      bottom = content_box_inset.bottom +. scrollbar_gutter.y;
    }
  in
  (* Track sizing works in the content box, so the border-box minimum and
     maximum sizes it stretches and limits the tracks by lose the inset *)
  let inner_min_size = content_box_size content_box_inset min_size
  and inner_max_size = content_box_size content_box_inset max_size in

  let align_content =
    Option.value (Style.align_content style) ~default:Align_content.Stretch
  in
  let justify_content =
    Option.value (Style.justify_content style) ~default:Align_content.Stretch
  in
  let align_items = Style.align_items style in
  let justify_items = Style.justify_items style in

  (* Note: we avoid accessing the grid rows/columns methods more than once as
     this can cause an expensive-ish computation In OCaml, we pass the style
     directly to functions that need it rather than extracting these values
     early *)
  (* A grid whose height is auto sizes its rows under a max-content
     constraint, as Chromium does: the space its parent has below it does not
     bound them, and in the block axis its min-content size is its
     max-content size (CSS Sizing 3, 5.1). A maximum height still bounds
     them. *)
  let available_space =
    match
      ( (Size.choose_first known_dimensions preferred_size).height,
        available_space.height )
    with
    | None, (Available_space.Definite _ | Available_space.Min_content)
      when Option.is_none max_size.height ->
        { available_space with height = Available_space.Max_content }
    | _ -> available_space
  in
  let constrained_available_space =
    ( Size.map2
        (fun opt_size space ->
          match opt_size with
          | Some size -> Available_space.Definite size
          | None -> space)
        (Size.choose_first known_dimensions preferred_size)
        available_space
    |> fun space ->
      Size.
        {
          width =
            Available_space.clamp_or_self space.width min_size.width
              max_size.width;
          height =
            Available_space.clamp_or_self space.height min_size.height
              max_size.height;
        } )
    |> fun space ->
    Size.
      {
        width =
          Available_space.max_or_self space.width
            (Some padding_border_size.width);
        height =
          Available_space.max_or_self space.height
            (Some padding_border_size.height);
      }
  in

  let available_grid_space =
    Size.
      {
        width =
          Available_space.map_definite_value constrained_available_space.width
            (fun space -> space -. Rectf.horizontal_axis_sum content_box_inset);
        height =
          Available_space.map_definite_value constrained_available_space.height
            (fun space -> space -. Rectf.vertical_axis_sum content_box_inset);
      }
  in

  let outer_node_size =
    Size.choose_first known_dimensions preferred_size
    |> Size.clamp_option min_size max_size
    |> Sizef.maybe_max padding_border_size
  in
  let inner_node_size =
    Size.
      {
        width =
          Option.map
            (fun space -> space -. Rectf.horizontal_axis_sum content_box_inset)
            outer_node_size.width;
        height =
          Option.map
            (fun space -> space -. Rectf.vertical_axis_sum content_box_inset)
            outer_node_size.height;
      }
  in

  (* Short-circuit if we can compute size early *)
  match (run_mode, outer_node_size.width, outer_node_size.height) with
  | Run_mode.Compute_size, Some width, Some height ->
      Layout_output.from_outer_size Sizef.{ width; height }
  (* Nor when the width is known and only the width is requested. *)
  | Run_mode.Compute_size, Some width, None
    when Layout_input.axis inputs = Requested_axis.Horizontal ->
      Layout_output.from_outer_size Sizef.{ width; height = 0.0 }
  | _ ->
      (* 2. Resolve the explicit grid *)

      (* This is very similar to the inner_node_size except if the
         inner_node_size is not definite but the node has a min- or max- size
         style then that will be used in its place. *)
      let auto_fit_container_size =
        Size.choose_first outer_node_size max_size |> fun s ->
        Size.choose_first s min_size
        |> Size.clamp_option min_size max_size
        |> Sizef.maybe_max padding_border_size
        |> fun s -> Sizef.maybe_sub (Rectf.sum_axes content_box_inset) s
      in

      (* Determine auto-repeat strategy based on container size constraints *)
      let auto_repeat_fit_strategy =
        Size.choose_first outer_node_size max_size
        |> Size.map (function
          | Some _ -> Explicit_grid.Max_repetitions_that_do_not_overflow
          | None -> Explicit_grid.Min_repetitions_that_do_overflow)
      in

      (* Compute the number of rows and columns in the explicit grid template *)
      let col_auto_repetition_count, grid_template_col_count =
        Explicit_grid.compute_explicit_grid_size_in_axis ~style
          ~auto_fit_container_size:auto_fit_container_size.width
          ~auto_fit_strategy:auto_repeat_fit_strategy.width
          ~resolve_calc_value:calc ~axis:Absolute_axis.Horizontal
      in
      let row_auto_repetition_count, grid_template_row_count =
        Explicit_grid.compute_explicit_grid_size_in_axis ~style
          ~auto_fit_container_size:auto_fit_container_size.height
          ~auto_fit_strategy:auto_repeat_fit_strategy.height
          ~resolve_calc_value:calc ~axis:Absolute_axis.Vertical
      in

      (* Create named line resolver *)
      let name_resolver =
        Named.create style col_auto_repetition_count row_auto_repetition_count
      in

      let explicit_col_count =
        max grid_template_col_count (Named.area_column_count name_resolver)
      in
      let explicit_row_count =
        max grid_template_row_count (Named.area_row_count name_resolver)
      in

      Named.set_explicit_column_count name_resolver explicit_col_count;
      Named.set_explicit_row_count name_resolver explicit_row_count;

      (* 3. Implicit Grid: Estimate Track Counts *)
      (* Estimate the number of rows and columns in the implicit grid (= the entire grid)
         This is necessary as part of placement. Doing it early here is a perf optimisation to reduce allocations. *)
      let est_col_counts, est_row_counts =
        let child_count = Tree.child_count tree node in
        let child_styles =
          Array.make child_count (Tree.get_core_container_style tree node)
        in
        for i = 0 to child_count - 1 do
          let child_id = Tree.get_child_id tree node i in
          child_styles.(i) <- Tree.get_core_container_style tree child_id
        done;
        let child_styles_iter =
          Seq.unfold
            (fun idx ->
              if idx >= child_count then None
              else Some (child_styles.(idx), idx + 1))
            0
        in
        Implicit_grid.compute_grid_size_estimate ~explicit_col_count
          ~explicit_row_count ~child_styles_iter
      in

      (* 4. Grid Item Placement *)
      (* Match items (children) to a definite grid position (row start/end and column start/end position) *)
      let items = ref [] in
      let cell_occupancy_matrix =
        Cell_occupancy.with_track_counts est_col_counts est_row_counts
      in

      Placement.place_grid_items
        (module Tree)
        ~cell_occupancy_matrix ~items ~tree ~parent_node:node
        ~grid_auto_flow:(Style.grid_auto_flow style)
        ~align_items ~justify_items ~named_line_resolver:name_resolver;

      (* Extract track counts from previous step (auto-placement can expand the
         number of tracks) *)
      let final_col_counts =
        Cell_occupancy.track_counts cell_occupancy_matrix
          Absolute_axis.Horizontal
      in
      let final_row_counts =
        Cell_occupancy.track_counts cell_occupancy_matrix Absolute_axis.Vertical
      in

      (* 5. Initialize Tracks *)
      (* Initialize (explicit and implicit) grid tracks (and gutters)
         This resolves the min and max track sizing functions for all tracks and gutters *)
      let columns = ref [] in
      let rows = ref [] in
      Explicit_grid.initialize_grid_tracks ~tracks:columns
        ~counts:final_col_counts ~style ~axis:Absolute_axis.Horizontal
        ~track_has_items:(fun column_index ->
          Cell_occupancy.column_is_occupied cell_occupancy_matrix column_index);
      Explicit_grid.initialize_grid_tracks ~tracks:rows ~counts:final_row_counts
        ~style ~axis:Absolute_axis.Vertical ~track_has_items:(fun row_index ->
          Cell_occupancy.row_is_occupied cell_occupancy_matrix row_index);

      (* Convert lists to arrays for track sizing *)
      let columns = Array.of_list !columns in
      let rows = Array.of_list !rows in
      let items = Array.of_list !items in

      (* 6. Track Sizing *)

      (* Convert grid placements in origin-zero coordinates to indexes into the
         GridTrack (rows and columns) vectors This computation is relatively
         trivial, but it requires the final number of negative (implicit) tracks
         in each axis, and doing it up-front here means we don't have to keep
         repeating that calculation *)
      Track_sizing.resolve_item_track_indexes items final_col_counts
        final_row_counts;

      (* For each item, and in each axis, determine whether the item crosses any
         flexible (fr) tracks Record this as a boolean (per-axis) on each item
         for later use in the track-sizing algorithm *)
      Track_sizing.determine_if_item_crosses_flexible_or_intrinsic_tracks items
        columns rows;

      (* Determine if the grid has any baseline aligned items *)
      let has_baseline_aligned_item =
        Array.exists Grid_item.participates_in_baseline_alignment items
      in

      (* Run track sizing algorithm for Inline axis *)
      Track_sizing.track_sizing_algorithm
        (module Tree)
        tree Abstract_axis.Inline
        (Size.get Abstract_axis.Inline inner_min_size)
        (Size.get Abstract_axis.Inline inner_max_size)
        justify_content align_content available_grid_space inner_node_size
        columns rows items
        (fun track parent_size tree ->
          Grid.Track_sizing_function.Max.definite_value_with_calc
            track.Grid_track.track_sizing_function parent_size
            (Tree.resolve_calc_value tree))
        has_baseline_aligned_item;

      let initial_column_sum =
        Array.fold_left
          (fun acc track -> acc +. track.Grid_track.base_size)
          0.0 columns
      in
      let inner_node_size =
        {
          inner_node_size with
          width =
            Option.value inner_node_size.width ~default:initial_column_sum
            |> Option.some;
        }
      in

      (* Clear caches before block axis sizing *)
      Array.iter (fun item -> item.Grid_item.grid_area_size_cache <- None) items;

      (* Run track sizing algorithm for Block axis *)
      Track_sizing.track_sizing_algorithm
        (module Tree)
        tree Abstract_axis.Block
        (Size.get Abstract_axis.Block inner_min_size)
        (Size.get Abstract_axis.Block inner_max_size)
        align_content justify_content available_grid_space inner_node_size rows
        columns items
        (fun track _ _ -> Some track.Grid_track.base_size)
        false;

      (* TODO: Support baseline alignment in vertical axis *)
      let initial_row_sum =
        Array.fold_left
          (fun acc track -> acc +. track.Grid_track.base_size)
          0.0 rows
      in

      (* 6. Compute container size *)
      let resolved_style_size =
        Size.choose_first known_dimensions preferred_size
      in
      let container_border_box =
        Sizef.
          {
            width =
              (match Size.get Abstract_axis.Inline resolved_style_size with
                | Some w -> w
                | None ->
                    initial_column_sum
                    +. Rectf.horizontal_axis_sum content_box_inset)
              |> (fun w ->
              match (min_size.width, max_size.width) with
              | Some min_w, Some max_w -> Float.max min_w (Float.min max_w w)
              | Some min_w, None -> Float.max min_w w
              | None, Some max_w -> Float.min max_w w
              | None, None -> w)
              |> Float.max padding_border_size.width;
            height =
              (match Size.get Abstract_axis.Block resolved_style_size with
                | Some h -> h
                | None ->
                    initial_row_sum +. Rectf.vertical_axis_sum content_box_inset)
              |> (fun h ->
              match (min_size.height, max_size.height) with
              | Some min_h, Some max_h -> Float.max min_h (Float.min max_h h)
              | Some min_h, None -> Float.max min_h h
              | None, Some max_h -> Float.min max_h h
              | None, None -> h)
              |> Float.max padding_border_size.height;
          }
      in
      let container_content_box =
        Sizef.
          {
            width =
              Float.max 0.0
                (container_border_box.width
                -. Rectf.horizontal_axis_sum content_box_inset);
            height =
              Float.max 0.0
                (container_border_box.height
                -. Rectf.vertical_axis_sum content_box_inset);
          }
      in

      (* Whether an item's min-content contribution changed when the tracks
         of the other axis were sized, which re-runs the sizing of its axis
         and sizes a container sized by its content again *)
      let column_contribution_changed = ref false
      and row_contribution_changed = ref false in
      (* The container's size is now determined: the re-runs resolve
         percentages against its content box *)
      let inner_node_size =
        Size.
          {
            width = Some container_content_box.width;
            height = Some container_content_box.height;
          }
      in

      (* An item's min-content contribution in [axis], measured in its grid
         area now that the tracks of the other axis are sized. It replaces the
         item's cached contributions in [axis] and tells whether it changed. *)
      let contribution_changed item axis axis_tracks other_axis_tracks =
        let grid_area_size =
          Grid_item.grid_area_size item axis axis_tracks other_axis_tracks
            inner_node_size
            (fun track _ -> Some track.Grid_track.base_size)
            (Tree.resolve_calc_value tree)
        in
        let available_space = Size.set axis None grid_area_size in
        let contribution =
          Grid_item.min_content_contribution
            (module Tree)
            item axis tree grid_area_size available_space
        in
        let changed =
          Some contribution
          <> Size.get axis item.Grid_item.min_content_contribution_cache
        in
        item.Grid_item.grid_area_size_cache <- Some grid_area_size;
        item.Grid_item.min_content_contribution_cache <-
          Size.set axis (Some contribution)
            item.Grid_item.min_content_contribution_cache;
        item.Grid_item.max_content_contribution_cache <-
          Size.set axis None item.Grid_item.max_content_contribution_cache;
        item.Grid_item.minimum_contribution_cache <-
          Size.set axis None item.Grid_item.minimum_contribution_cache;
        changed
      in
      let any_contribution_changed axis axis_tracks other_axis_tracks =
        let crosses item =
          match axis with
          | Abstract_axis.Inline -> item.Grid_item.crosses_intrinsic_column
          | Abstract_axis.Block -> item.Grid_item.crosses_intrinsic_row
        in
        let changed = ref false in
        let len = Array.length items in
        let i = ref 0 in
        while (not !changed) && !i < len do
          let item = items.(!i) in
          if crosses item then
            changed :=
              contribution_changed item axis axis_tracks other_axis_tracks;
          incr i
        done;
        !changed
      in
      let clear_caches axis =
        Array.iter
          (fun item ->
            item.Grid_item.grid_area_size_cache <- None;
            item.Grid_item.min_content_contribution_cache <-
              Size.set axis None item.Grid_item.min_content_contribution_cache;
            item.Grid_item.max_content_contribution_cache <-
              Size.set axis None item.Grid_item.max_content_contribution_cache;
            item.Grid_item.minimum_contribution_cache <-
              Size.set axis None item.Grid_item.minimum_contribution_cache)
          items
      in

      (* Column sizing is re-run (once) if the container's width was
         indefinite and a column has a percentage sizing function, which
         resolved against nothing, or if the min-content contribution of an
         item crossing an intrinsic column changed now that the rows are
         sized. The contributions are re-measured from scratch on a re-run.
         TODO: Only re-run the tracks that need it. *)
      let rerun_column_sizing =
        Option.is_none outer_node_size.width
        && Array.exists Grid_track.uses_percentage columns
        ||
        (column_contribution_changed :=
           any_contribution_changed Abstract_axis.Inline columns rows;
         !column_contribution_changed)
      in
      (* Row sizing likewise, for the container's height and percentage
         rows, or for contributions the re-sized columns changed *)
      let rerun_row_sizing =
        Option.is_none outer_node_size.height
        && Array.exists Grid_track.uses_percentage rows
      in
      if rerun_column_sizing then (
        clear_caches Abstract_axis.Inline;
        Track_sizing.track_sizing_algorithm
          (module Tree)
          tree Abstract_axis.Inline
          (Size.get Abstract_axis.Inline inner_min_size)
          (Size.get Abstract_axis.Inline inner_max_size)
          justify_content align_content
          (percentage_rerun_space outer_node_size columns rows
             container_content_box available_grid_space)
          inner_node_size columns rows items
          (fun track _ _ -> Some track.Grid_track.base_size)
          has_baseline_aligned_item);
      let rerun_row_sizing =
        rerun_row_sizing
        || rerun_column_sizing
           &&
           (row_contribution_changed :=
              any_contribution_changed Abstract_axis.Block rows columns;
            !row_contribution_changed)
      in
      if rerun_row_sizing then (
        clear_caches Abstract_axis.Block;
        Track_sizing.track_sizing_algorithm
          (module Tree)
          tree Abstract_axis.Block
          (Size.get Abstract_axis.Block inner_min_size)
          (Size.get Abstract_axis.Block inner_max_size)
          align_content justify_content
          (percentage_rerun_space outer_node_size columns rows
             container_content_box available_grid_space)
          inner_node_size rows columns items
          (fun track _ _ -> Some track.Grid_track.base_size)
          false (* TODO: Support baseline alignment in vertical axis *));

      (* A container sized by its content takes the tracks' new sizes
           where an item's contribution changed them (without percentage
           tracks, which its old size resolved) *)
      let resize changed tracks style_size min max padding_border inset_sum size
          =
        if
          !changed
          && (not (Array.exists Grid_track.uses_percentage tracks))
          && Option.is_none style_size
        then
          let sum =
            Array.fold_left
              (fun acc track -> acc +. track.Grid_track.base_size)
              0.0 tracks
            +. inset_sum
          in
          let sum =
            match max with Some max -> Float.min max sum | None -> sum
          in
          let sum =
            match min with Some min -> Float.max min sum | None -> sum
          in
          Float.max sum padding_border
        else size
      in
      let container_border_box =
        Sizef.
          {
            width =
              resize column_contribution_changed columns
                (Size.get Abstract_axis.Inline resolved_style_size)
                min_size.width max_size.width padding_border_size.width
                (Rectf.horizontal_axis_sum content_box_inset)
                container_border_box.width;
            height =
              resize row_contribution_changed rows
                (Size.get Abstract_axis.Block resolved_style_size)
                min_size.height max_size.height padding_border_size.height
                (Rectf.vertical_axis_sum content_box_inset)
                container_border_box.height;
          }
      in
      let container_content_box =
        Sizef.
          {
            width =
              Float.max 0.0
                (container_border_box.width
                -. Rectf.horizontal_axis_sum content_box_inset);
            height =
              Float.max 0.0
                (container_border_box.height
                -. Rectf.vertical_axis_sum content_box_inset);
          }
      in

      (* If only the container's size has been requested *)
      if run_mode = Run_mode.Compute_size then
        Layout_output.from_outer_size container_border_box
      else (
        (* 8. Track Alignment *)

        (* Align columns *)
        Alignment.align_tracks
          ~grid_container_content_box_size:
            (Sizef.get Abstract_axis.Inline container_content_box)
          ~padding:Line.{ start = padding.left; end_ = padding.right }
          ~border:Line.{ start = border.left; end_ = border.right }
          ~tracks:columns ~track_alignment_style:justify_content;

        (* Align rows *)
        Alignment.align_tracks
          ~grid_container_content_box_size:
            (Sizef.get Abstract_axis.Block container_content_box)
          ~padding:Line.{ start = padding.top; end_ = padding.bottom }
          ~border:Line.{ start = border.top; end_ = border.bottom }
          ~tracks:rows ~track_alignment_style:align_content;

        (* 9. Size, Align, and Position Grid Items *)
        (* The container's scrollable overflow, gathered in its padding-box
           frame *)
        let is_scroll_container =
          let overflow = Style.overflow style in
          Overflow.is_container overflow.x || Overflow.is_container overflow.y
        in
        let overflow_acc = Toffee_compute_helpers.overflow_acc () in
        (* A grid's padding extends its track area, not its items: no margin
           extends an item's box *)
        let overflow_flags =
          Toffee_compute_helpers.overflow_flags ~reaches_empty_boxes:false
            ~is_scroll_container ~reversed_x:false ~reversed_y:false
            ~margins:No_margins ()
        in

        (* Sort items back into original order to allow them to be matched up
           with styles *)
        Array.sort
          (fun a b ->
            Int.compare a.Grid_item.source_order b.Grid_item.source_order)
          items;

        let container_alignment_styles =
          In_both_abs_axis.
            { horizontal = justify_items; vertical = align_items }
        in

        (* Position in-flow children (stored in items array) *)
        Array.iteri
          (fun index item ->
            let grid_area =
              {
                Rectf.top =
                  rows.(item.Grid_item.row_indexes.Line.start + 1)
                    .Grid_track.offset;
                bottom =
                  rows.(item.Grid_item.row_indexes.Line.end_).Grid_track.offset;
                left =
                  columns.(item.Grid_item.column_indexes.Line.start + 1)
                    .Grid_track.offset;
                right =
                  columns.(item.Grid_item.column_indexes.Line.end_)
                    .Grid_track.offset;
              }
            in
            let y_position, height =
              Alignment.align_and_position_item
                (module Tree)
                ~tree ~node:item.Grid_item.node ~order:index ~grid_area
                ~container_alignment_styles
                ~baseline_shim:item.Grid_item.baseline_shim ~overflow_acc
                ~overflow_flags ~overflow_origin:border
            in
            item.Grid_item.y_position <- y_position;
            item.Grid_item.height <- height)
          items;

        (* Position hidden and absolutely positioned children *)
        let order = ref (Array.length items) in
        for index = 0 to Tree.child_count tree node - 1 do
          let child = Tree.get_child_id tree node index in
          let child_style = Tree.get_core_container_style tree child in

          (* Position hidden child *)
          if Style.box_generation_mode child_style = Box_generation_mode.None
          then
            let _ =
              Tree.compute_child_layout tree child
                (Layout_input.make ~run_mode:Run_mode.Perform_layout
                   ~sizing_mode:Sizing_mode.Inherent_size
                   ~axis:Requested_axis.Both ~known_dimensions:Size.none
                   ~parent_size:Size.none
                   ~available_space:
                     Size.
                       {
                         width = Available_space.max_content;
                         height = Available_space.max_content;
                       }
                   ~vertical_margins_are_collapsible:Line.both_false)
            in
            incr order (* Position absolutely positioned child *)
          else if Style.position child_style = Position.Absolute then
            (* Convert grid-col-{start/end} into Option's of indexes into the
               columns vector *)
            let maybe_col_indexes =
              let grid_placement = Style.grid_column child_style in
              let resolved =
                Named.resolve_column_names name_resolver grid_placement
              in
              let oz_placement =
                Grid.Placement.Line.into_origin_zero resolved
                  (Grid_track_counts.explicit final_col_counts)
              in
              let resolved_abs =
                Named.resolve_absolutely_positioned_grid_tracks oz_placement
              in
              Line.map
                (fun maybe_line ->
                  Option.bind maybe_line (fun line ->
                      Grid_track_counts.oz_line_to_track final_col_counts line))
                resolved_abs
            in
            (* Convert grid-row-{start/end} into Option's of indexes into the
               row vector *)
            let maybe_row_indexes =
              let grid_placement = Style.grid_row child_style in
              let resolved =
                Named.resolve_row_names name_resolver grid_placement
              in
              let oz_placement =
                Grid.Placement.Line.into_origin_zero resolved
                  (Grid_track_counts.explicit final_row_counts)
              in
              let resolved_abs =
                Named.resolve_absolutely_positioned_grid_tracks oz_placement
              in
              Line.map
                (fun maybe_line ->
                  Option.bind maybe_line (fun line ->
                      Grid_track_counts.oz_line_to_track final_row_counts line))
                resolved_abs
            in

            let grid_area =
              Rectf.
                {
                  top =
                    (match maybe_row_indexes.start with
                    | Some index -> rows.(index).offset
                    | None -> border.top);
                  bottom =
                    (match maybe_row_indexes.end_ with
                    | Some index -> rows.(index).offset
                    | None ->
                        container_border_box.height -. border.bottom
                        -. scrollbar_gutter.y);
                  left =
                    (match maybe_col_indexes.start with
                    | Some index -> columns.(index).offset
                    | None -> border.left);
                  right =
                    (match maybe_col_indexes.end_ with
                    | Some index -> columns.(index).offset
                    | None ->
                        container_border_box.width -. border.right
                        -. scrollbar_gutter.x);
                }
            in

            (* TODO: Baseline alignment support for absolutely positioned
               items *)
            let _ =
              Alignment.align_and_position_item
                (module Tree)
                ~tree ~node:child ~order:!order ~grid_area
                ~container_alignment_styles ~baseline_shim:0.0 ~overflow_acc
                ~overflow_flags ~overflow_origin:border
            in
            incr order
        done;

        (* TODO: Set detailed grid information if needed *)

        (* A scroll container's own end padding is part of its scrollable
           overflow *)
        (if is_scroll_container then
           (* A scroll container's track area and its padding past it are
             part of its scrollable overflow *)
           let track_end tracks start =
             let n = Array.length tracks in
             if n = 0 then start
             else
               let last = tracks.(n - 1) in
               last.Grid_track.offset +. last.Grid_track.base_size
           in
           Toffee_compute_helpers.union overflow_acc ~left:0.0
             ~right:
               (track_end columns (border.left +. padding.left)
               -. border.left +. padding.right)
             ~top:0.0
             ~bottom:
               (track_end rows (border.top +. padding.top)
               -. border.top +. padding.bottom));
        let scrollable_overflow =
          Toffee_compute_helpers.finalize_overflow overflow_acc
            ~flags:overflow_flags ~size:container_border_box ~border
            ~client:
              (if is_scroll_container then
                 Sizef.
                   {
                     width =
                       container_border_box.width -. border.left -. border.right
                       -. scrollbar_gutter.x;
                     height =
                       container_border_box.height -. border.top
                       -. border.bottom -. scrollbar_gutter.y;
                   }
               else Sizef.zero)
        in

        (* If there are no items then return just the container size and its
           overflow (no baseline) *)
        if Array.length items = 0 then
          Layout_output.from_sizes_and_baselines container_border_box
            scrollable_overflow Point.none
        else
          (* Determine the grid container baseline(s) (currently we only compute
             the first baseline) *)
          let grid_container_baseline =
            (* Sort items by row start position so that we can iterate items in
               groups which are in the same row *)
            let sorted_items = Array.copy items in
            Array.sort
              (fun a b ->
                Int.compare a.Grid_item.row_indexes.start
                  b.Grid_item.row_indexes.start)
              sorted_items;

            (* Get the row index of the first row containing items *)
            let first_row = sorted_items.(0).Grid_item.row_indexes.start in

            (* Get all items in the first row *)
            let first_row_items =
              sorted_items |> Array.to_list
              |> List.filter (fun item ->
                  item.Grid_item.row_indexes.start = first_row)
              |> Array.of_list
            in

            (* The first item of the row in baseline alignment, else the
               row's first item *)
            let item =
              match
                Array.find_opt Grid_item.participates_in_baseline_alignment
                  first_row_items
              with
              | Some item -> item
              | None -> first_row_items.(0)
            in

            item.Grid_item.y_position
            +. Option.value item.Grid_item.baseline
                 ~default:item.Grid_item.height
          in

          Layout_output.from_sizes_and_baselines container_border_box
            scrollable_overflow
            Point.{ x = None; y = Some grid_container_baseline })