taffy/compute/grid/
mod.rs

1//! This module is a partial implementation of the CSS Grid Level 1 specification
2//! <https://www.w3.org/TR/css-grid-1>
3use crate::geometry::{AbsoluteAxis, AbstractAxis, InBothAbsAxis};
4use crate::geometry::{Line, Point, Rect, Size};
5use crate::style::{AlignItems, AlignSelf, AvailableSpace, Overflow, Position};
6use crate::tree::{Layout, LayoutInput, LayoutOutput, LayoutPartialTreeExt, NodeId, RunMode, SizingMode};
7use crate::util::debug::debug_log;
8use crate::util::sys::{f32_max, GridTrackVec, Vec};
9use crate::util::MaybeMath;
10use crate::util::{MaybeResolve, ResolveOrZero};
11use crate::{
12    style_helpers::*, AlignContent, BoxGenerationMode, BoxSizing, CoreStyle, GridContainerStyle, GridItemStyle,
13    JustifyContent, LayoutGridContainer,
14};
15use alignment::{align_and_position_item, align_tracks};
16use explicit_grid::{compute_explicit_grid_size_in_axis, initialize_grid_tracks, AutoRepeatStrategy};
17use implicit_grid::compute_grid_size_estimate;
18use placement::place_grid_items;
19use track_sizing::{
20    determine_if_item_crosses_flexible_or_intrinsic_tracks, resolve_item_track_indexes, track_sizing_algorithm,
21};
22use types::{CellOccupancyMatrix, GridTrack, NamedLineResolver};
23
24#[cfg(feature = "detailed_layout_info")]
25use types::{GridItem, GridTrackKind, TrackCounts};
26
27pub(crate) use types::{GridCoordinate, GridLine, OriginZeroLine};
28
29mod alignment;
30mod explicit_grid;
31mod implicit_grid;
32mod placement;
33mod track_sizing;
34mod types;
35mod util;
36
37/// Grid layout algorithm
38/// This consists of a few phases:
39///   - Resolving the explicit grid
40///   - Placing items (which also resolves the implicit grid)
41///   - Track (row/column) sizing
42///   - Alignment & Final item placement
43pub fn compute_grid_layout<Tree: LayoutGridContainer>(
44    tree: &mut Tree,
45    node: NodeId,
46    inputs: LayoutInput,
47) -> LayoutOutput {
48    let LayoutInput { known_dimensions, parent_size, available_space, run_mode, .. } = inputs;
49
50    let style = tree.get_grid_container_style(node);
51
52    // 1. Compute "available grid space"
53    // https://www.w3.org/TR/css-grid-1/#available-grid-space
54    let aspect_ratio = style.aspect_ratio();
55    let padding = style.padding().resolve_or_zero(parent_size.width, |val, basis| tree.calc(val, basis));
56    let border = style.border().resolve_or_zero(parent_size.width, |val, basis| tree.calc(val, basis));
57    let padding_border = padding + border;
58    let padding_border_size = padding_border.sum_axes();
59    let box_sizing_adjustment =
60        if style.box_sizing() == BoxSizing::ContentBox { padding_border_size } else { Size::ZERO };
61
62    let min_size = style
63        .min_size()
64        .maybe_resolve(parent_size, |val, basis| tree.calc(val, basis))
65        .maybe_apply_aspect_ratio(aspect_ratio)
66        .maybe_add(box_sizing_adjustment);
67    let max_size = style
68        .max_size()
69        .maybe_resolve(parent_size, |val, basis| tree.calc(val, basis))
70        .maybe_apply_aspect_ratio(aspect_ratio)
71        .maybe_add(box_sizing_adjustment);
72    let preferred_size = if inputs.sizing_mode == SizingMode::InherentSize {
73        style
74            .size()
75            .maybe_resolve(parent_size, |val, basis| tree.calc(val, basis))
76            .maybe_apply_aspect_ratio(style.aspect_ratio())
77            .maybe_add(box_sizing_adjustment)
78    } else {
79        Size::NONE
80    };
81
82    // Scrollbar gutters are reserved when the `overflow` property is set to `Overflow::Scroll`.
83    // However, the axis are switched (transposed) because a node that scrolls vertically needs
84    // *horizontal* space to be reserved for a scrollbar
85    let scrollbar_gutter = style.overflow().transpose().map(|overflow| match overflow {
86        Overflow::Scroll => style.scrollbar_width(),
87        _ => 0.0,
88    });
89    // TODO: make side configurable based on the `direction` property
90    let mut content_box_inset = padding_border;
91    content_box_inset.right += scrollbar_gutter.x;
92    content_box_inset.bottom += scrollbar_gutter.y;
93
94    let align_content = style.align_content().unwrap_or(AlignContent::Stretch);
95    let justify_content = style.justify_content().unwrap_or(JustifyContent::Stretch);
96    let align_items = style.align_items();
97    let justify_items = style.justify_items();
98
99    // Note: we avoid accessing the grid rows/columns methods more than once as this can
100    // cause an expensive-ish computation
101    let grid_template_columms = style.grid_template_columns();
102    let grid_template_rows = style.grid_template_rows();
103    let grid_auto_columms = style.grid_auto_columns();
104    let grid_auto_rows = style.grid_auto_rows();
105
106    let constrained_available_space = known_dimensions
107        .or(preferred_size)
108        .map(|size| size.map(AvailableSpace::Definite))
109        .unwrap_or(available_space)
110        .maybe_clamp(min_size, max_size)
111        .maybe_max(padding_border_size);
112
113    let available_grid_space = Size {
114        width: constrained_available_space
115            .width
116            .map_definite_value(|space| space - content_box_inset.horizontal_axis_sum()),
117        height: constrained_available_space
118            .height
119            .map_definite_value(|space| space - content_box_inset.vertical_axis_sum()),
120    };
121
122    let outer_node_size =
123        known_dimensions.or(preferred_size).maybe_clamp(min_size, max_size).maybe_max(padding_border_size);
124    let mut inner_node_size = Size {
125        width: outer_node_size.width.map(|space| space - content_box_inset.horizontal_axis_sum()),
126        height: outer_node_size.height.map(|space| space - content_box_inset.vertical_axis_sum()),
127    };
128
129    debug_log!("parent_size", dbg:parent_size);
130    debug_log!("outer_node_size", dbg:outer_node_size);
131    debug_log!("inner_node_size", dbg:inner_node_size);
132
133    if let (RunMode::ComputeSize, Some(width), Some(height)) = (run_mode, outer_node_size.width, outer_node_size.height)
134    {
135        return LayoutOutput::from_outer_size(Size { width, height });
136    }
137
138    let get_child_styles_iter =
139        |node| tree.child_ids(node).map(|child_node: NodeId| tree.get_grid_child_style(child_node));
140    let child_styles_iter = get_child_styles_iter(node);
141
142    // 2. Resolve the explicit grid
143
144    // This is very similar to the inner_node_size except if the inner_node_size is not definite but the node
145    // has a min- or max- size style then that will be used in it's place.
146    let auto_fit_container_size = outer_node_size
147        .or(max_size)
148        .or(min_size)
149        .maybe_clamp(min_size, max_size)
150        .maybe_max(padding_border_size)
151        .maybe_sub(content_box_inset.sum_axes());
152
153    // If the grid container has a definite size or max size in the relevant axis:
154    //   - then the number of repetitions is the largest possible positive integer that does not cause the grid to overflow the content
155    //     box of its grid container.
156    // Otherwise, if the grid container has a definite min size in the relevant axis:
157    //   - then the number of repetitions is the smallest possible positive integer that fulfills that minimum requirement
158    // Otherwise, the specified track list repeats only once.
159    let auto_repeat_fit_strategy = outer_node_size.or(max_size).map(|val| match val {
160        Some(_) => AutoRepeatStrategy::MaxRepetitionsThatDoNotOverflow,
161        None => AutoRepeatStrategy::MinRepetitionsThatDoOverflow,
162    });
163
164    // Compute the number of rows and columns in the explicit grid *template*
165    // (explicit tracks from grid_areas are computed separately below)
166    let (col_auto_repetition_count, grid_template_col_count) = compute_explicit_grid_size_in_axis(
167        &style,
168        auto_fit_container_size.width,
169        auto_repeat_fit_strategy.width,
170        |val, basis| tree.calc(val, basis),
171        AbsoluteAxis::Horizontal,
172    );
173    let (row_auto_repetition_count, grid_template_row_count) = compute_explicit_grid_size_in_axis(
174        &style,
175        auto_fit_container_size.height,
176        auto_repeat_fit_strategy.height,
177        |val, basis| tree.calc(val, basis),
178        AbsoluteAxis::Vertical,
179    );
180
181    // type CustomIdent<'a> = <<Tree as LayoutPartialTree>::CoreContainerStyle<'_> as CoreStyle>::CustomIdent;
182    let mut name_resolver = NamedLineResolver::new(&style, col_auto_repetition_count, row_auto_repetition_count);
183
184    let explicit_col_count = grid_template_col_count.max(name_resolver.area_column_count());
185    let explicit_row_count = grid_template_row_count.max(name_resolver.area_row_count());
186
187    name_resolver.set_explicit_column_count(explicit_col_count);
188    name_resolver.set_explicit_row_count(explicit_row_count);
189
190    // 3. Implicit Grid: Estimate Track Counts
191    // Estimate the number of rows and columns in the implicit grid (= the entire grid)
192    // This is necessary as part of placement. Doing it early here is a perf optimisation to reduce allocations.
193    let (est_col_counts, est_row_counts) =
194        compute_grid_size_estimate(explicit_col_count, explicit_row_count, child_styles_iter);
195
196    // 4. Grid Item Placement
197    // Match items (children) to a definite grid position (row start/end and column start/end position)
198    let mut items = Vec::with_capacity(tree.child_count(node));
199    let mut cell_occupancy_matrix = CellOccupancyMatrix::with_track_counts(est_col_counts, est_row_counts);
200    let in_flow_children_iter = || {
201        tree.child_ids(node)
202            .enumerate()
203            .map(|(index, child_node)| (index, child_node, tree.get_grid_child_style(child_node)))
204            .filter(|(_, _, style)| {
205                style.box_generation_mode() != BoxGenerationMode::None && style.position() != Position::Absolute
206            })
207    };
208    place_grid_items(
209        &mut cell_occupancy_matrix,
210        &mut items,
211        in_flow_children_iter,
212        style.grid_auto_flow(),
213        align_items.unwrap_or(AlignItems::Stretch),
214        justify_items.unwrap_or(AlignItems::Stretch),
215        &name_resolver,
216    );
217
218    // Extract track counts from previous step (auto-placement can expand the number of tracks)
219    let final_col_counts = *cell_occupancy_matrix.track_counts(AbsoluteAxis::Horizontal);
220    let final_row_counts = *cell_occupancy_matrix.track_counts(AbsoluteAxis::Vertical);
221
222    // 5. Initialize Tracks
223    // Initialize (explicit and implicit) grid tracks (and gutters)
224    // This resolves the min and max track sizing functions for all tracks and gutters
225    let mut columns = GridTrackVec::new();
226    let mut rows = GridTrackVec::new();
227    initialize_grid_tracks(&mut columns, final_col_counts, &style, AbsoluteAxis::Horizontal, |column_index| {
228        cell_occupancy_matrix.column_is_occupied(column_index)
229    });
230    initialize_grid_tracks(&mut rows, final_row_counts, &style, AbsoluteAxis::Vertical, |row_index| {
231        cell_occupancy_matrix.row_is_occupied(row_index)
232    });
233
234    drop(grid_template_rows);
235    drop(grid_template_columms);
236    drop(grid_auto_rows);
237    drop(grid_auto_columms);
238    drop(style);
239
240    // 6. Track Sizing
241
242    // Convert grid placements in origin-zero coordinates to indexes into the GridTrack (rows and columns) vectors
243    // This computation is relatively trivial, but it requires the final number of negative (implicit) tracks in
244    // each axis, and doing it up-front here means we don't have to keep repeating that calculation
245    resolve_item_track_indexes(&mut items, final_col_counts, final_row_counts);
246
247    // For each item, and in each axis, determine whether the item crosses any flexible (fr) tracks
248    // Record this as a boolean (per-axis) on each item for later use in the track-sizing algorithm
249    determine_if_item_crosses_flexible_or_intrinsic_tracks(&mut items, &columns, &rows);
250
251    // Determine if the grid has any baseline aligned items
252    let has_baseline_aligned_item = items.iter().any(|item| item.align_self == AlignSelf::Baseline);
253
254    // Run track sizing algorithm for Inline axis
255    track_sizing_algorithm(
256        tree,
257        AbstractAxis::Inline,
258        min_size.get(AbstractAxis::Inline),
259        max_size.get(AbstractAxis::Inline),
260        justify_content,
261        align_content,
262        available_grid_space,
263        inner_node_size,
264        &mut columns,
265        &mut rows,
266        &mut items,
267        |track: &GridTrack, parent_size: Option<f32>, tree: &Tree| {
268            track.max_track_sizing_function.definite_value(parent_size, |val, basis| tree.calc(val, basis))
269        },
270        has_baseline_aligned_item,
271    );
272    let initial_column_sum = columns.iter().map(|track| track.base_size).sum::<f32>();
273    inner_node_size.width = inner_node_size.width.or_else(|| initial_column_sum.into());
274
275    items.iter_mut().for_each(|item| item.available_space_cache = None);
276
277    // Run track sizing algorithm for Block axis
278    track_sizing_algorithm(
279        tree,
280        AbstractAxis::Block,
281        min_size.get(AbstractAxis::Block),
282        max_size.get(AbstractAxis::Block),
283        align_content,
284        justify_content,
285        available_grid_space,
286        inner_node_size,
287        &mut rows,
288        &mut columns,
289        &mut items,
290        |track: &GridTrack, _, _| Some(track.base_size),
291        false, // TODO: Support baseline alignment in the vertical axis
292    );
293    let initial_row_sum = rows.iter().map(|track| track.base_size).sum::<f32>();
294    inner_node_size.height = inner_node_size.height.or_else(|| initial_row_sum.into());
295
296    debug_log!("initial_column_sum", dbg:initial_column_sum);
297    debug_log!(dbg: columns.iter().map(|track| track.base_size).collect::<Vec<_>>());
298    debug_log!("initial_row_sum", dbg:initial_row_sum);
299    debug_log!(dbg: rows.iter().map(|track| track.base_size).collect::<Vec<_>>());
300
301    // 6. Compute container size
302    let resolved_style_size = known_dimensions.or(preferred_size);
303    let container_border_box = Size {
304        width: resolved_style_size
305            .get(AbstractAxis::Inline)
306            .unwrap_or_else(|| initial_column_sum + content_box_inset.horizontal_axis_sum())
307            .maybe_clamp(min_size.width, max_size.width)
308            .max(padding_border_size.width),
309        height: resolved_style_size
310            .get(AbstractAxis::Block)
311            .unwrap_or_else(|| initial_row_sum + content_box_inset.vertical_axis_sum())
312            .maybe_clamp(min_size.height, max_size.height)
313            .max(padding_border_size.height),
314    };
315    let container_content_box = Size {
316        width: f32_max(0.0, container_border_box.width - content_box_inset.horizontal_axis_sum()),
317        height: f32_max(0.0, container_border_box.height - content_box_inset.vertical_axis_sum()),
318    };
319
320    // If only the container's size has been requested
321    if run_mode == RunMode::ComputeSize {
322        return LayoutOutput::from_outer_size(container_border_box);
323    }
324
325    // 7. Resolve percentage track base sizes
326    // In the case of an indefinitely sized container these resolve to zero during the "Initialise Tracks" step
327    // and therefore need to be re-resolved here based on the content-sized content box of the container
328    if !available_grid_space.width.is_definite() {
329        for column in &mut columns {
330            let min: Option<f32> = column
331                .min_track_sizing_function
332                .resolved_percentage_size(container_content_box.width, |val, basis| tree.calc(val, basis));
333            let max: Option<f32> = column
334                .max_track_sizing_function
335                .resolved_percentage_size(container_content_box.width, |val, basis| tree.calc(val, basis));
336            column.base_size = column.base_size.maybe_clamp(min, max);
337        }
338    }
339    if !available_grid_space.height.is_definite() {
340        for row in &mut rows {
341            let min: Option<f32> = row
342                .min_track_sizing_function
343                .resolved_percentage_size(container_content_box.height, |val, basis| tree.calc(val, basis));
344            let max: Option<f32> = row
345                .max_track_sizing_function
346                .resolved_percentage_size(container_content_box.height, |val, basis| tree.calc(val, basis));
347            row.base_size = row.base_size.maybe_clamp(min, max);
348        }
349    }
350
351    // Column sizing must be re-run (once) if:
352    //   - The grid container's width was initially indefinite and there are any columns with percentage track sizing functions
353    //   - Any grid item crossing an intrinsically sized track's min content contribution width has changed
354    // TODO: Only rerun sizing for tracks that actually require it rather than for all tracks if any need it.
355    let mut rerun_column_sizing;
356
357    let has_percentage_column = columns.iter().any(|track| track.uses_percentage());
358    let parent_width_indefinite = !available_space.width.is_definite();
359    rerun_column_sizing = parent_width_indefinite && has_percentage_column;
360
361    if !rerun_column_sizing {
362        let min_content_contribution_changed =
363            items.iter_mut().filter(|item| item.crosses_intrinsic_column).any(|item| {
364                let available_space = item.available_space(
365                    AbstractAxis::Inline,
366                    &rows,
367                    inner_node_size.height,
368                    |track: &GridTrack, _| Some(track.base_size),
369                );
370                let new_min_content_contribution =
371                    item.min_content_contribution(AbstractAxis::Inline, tree, available_space, inner_node_size);
372
373                let has_changed = Some(new_min_content_contribution) != item.min_content_contribution_cache.width;
374
375                item.available_space_cache = Some(available_space);
376                item.min_content_contribution_cache.width = Some(new_min_content_contribution);
377                item.max_content_contribution_cache.width = None;
378                item.minimum_contribution_cache.width = None;
379
380                has_changed
381            });
382        rerun_column_sizing = min_content_contribution_changed;
383    } else {
384        // Clear intrisic width caches
385        items.iter_mut().for_each(|item| {
386            item.available_space_cache = None;
387            item.min_content_contribution_cache.width = None;
388            item.max_content_contribution_cache.width = None;
389            item.minimum_contribution_cache.width = None;
390        });
391    }
392
393    if rerun_column_sizing {
394        // Re-run track sizing algorithm for Inline axis
395        track_sizing_algorithm(
396            tree,
397            AbstractAxis::Inline,
398            min_size.get(AbstractAxis::Inline),
399            max_size.get(AbstractAxis::Inline),
400            justify_content,
401            align_content,
402            available_grid_space,
403            inner_node_size,
404            &mut columns,
405            &mut rows,
406            &mut items,
407            |track: &GridTrack, _, _| Some(track.base_size),
408            has_baseline_aligned_item,
409        );
410
411        // Row sizing must be re-run (once) if:
412        //   - The grid container's height was initially indefinite and there are any rows with percentage track sizing functions
413        //   - Any grid item crossing an intrinsically sized track's min content contribution height has changed
414        // TODO: Only rerun sizing for tracks that actually require it rather than for all tracks if any need it.
415        let mut rerun_row_sizing;
416
417        let has_percentage_row = rows.iter().any(|track| track.uses_percentage());
418        let parent_height_indefinite = !available_space.height.is_definite();
419        rerun_row_sizing = parent_height_indefinite && has_percentage_row;
420
421        if !rerun_row_sizing {
422            let min_content_contribution_changed =
423                items.iter_mut().filter(|item| item.crosses_intrinsic_column).any(|item| {
424                    let available_space = item.available_space(
425                        AbstractAxis::Block,
426                        &columns,
427                        inner_node_size.width,
428                        |track: &GridTrack, _| Some(track.base_size),
429                    );
430                    let new_min_content_contribution =
431                        item.min_content_contribution(AbstractAxis::Block, tree, available_space, inner_node_size);
432
433                    let has_changed = Some(new_min_content_contribution) != item.min_content_contribution_cache.height;
434
435                    item.available_space_cache = Some(available_space);
436                    item.min_content_contribution_cache.height = Some(new_min_content_contribution);
437                    item.max_content_contribution_cache.height = None;
438                    item.minimum_contribution_cache.height = None;
439
440                    has_changed
441                });
442            rerun_row_sizing = min_content_contribution_changed;
443        } else {
444            items.iter_mut().for_each(|item| {
445                // Clear intrisic height caches
446                item.available_space_cache = None;
447                item.min_content_contribution_cache.height = None;
448                item.max_content_contribution_cache.height = None;
449                item.minimum_contribution_cache.height = None;
450            });
451        }
452
453        if rerun_row_sizing {
454            // Re-run track sizing algorithm for Block axis
455            track_sizing_algorithm(
456                tree,
457                AbstractAxis::Block,
458                min_size.get(AbstractAxis::Block),
459                max_size.get(AbstractAxis::Block),
460                align_content,
461                justify_content,
462                available_grid_space,
463                inner_node_size,
464                &mut rows,
465                &mut columns,
466                &mut items,
467                |track: &GridTrack, _, _| Some(track.base_size),
468                false, // TODO: Support baseline alignment in the vertical axis
469            );
470        }
471    }
472
473    // 8. Track Alignment
474
475    // Align columns
476    align_tracks(
477        container_content_box.get(AbstractAxis::Inline),
478        Line { start: padding.left, end: padding.right },
479        Line { start: border.left, end: border.right },
480        &mut columns,
481        justify_content,
482    );
483    // Align rows
484    align_tracks(
485        container_content_box.get(AbstractAxis::Block),
486        Line { start: padding.top, end: padding.bottom },
487        Line { start: border.top, end: border.bottom },
488        &mut rows,
489        align_content,
490    );
491
492    // 9. Size, Align, and Position Grid Items
493
494    #[cfg_attr(not(feature = "content_size"), allow(unused_mut))]
495    let mut item_content_size_contribution = Size::ZERO;
496
497    // Sort items back into original order to allow them to be matched up with styles
498    items.sort_by_key(|item| item.source_order);
499
500    let container_alignment_styles = InBothAbsAxis { horizontal: justify_items, vertical: align_items };
501
502    // Position in-flow children (stored in items vector)
503    for (index, item) in items.iter_mut().enumerate() {
504        let grid_area = Rect {
505            top: rows[item.row_indexes.start as usize + 1].offset,
506            bottom: rows[item.row_indexes.end as usize].offset,
507            left: columns[item.column_indexes.start as usize + 1].offset,
508            right: columns[item.column_indexes.end as usize].offset,
509        };
510        #[cfg_attr(not(feature = "content_size"), allow(unused_variables))]
511        let (content_size_contribution, y_position, height) = align_and_position_item(
512            tree,
513            item.node,
514            index as u32,
515            grid_area,
516            container_alignment_styles,
517            item.baseline_shim,
518        );
519        item.y_position = y_position;
520        item.height = height;
521
522        #[cfg(feature = "content_size")]
523        {
524            item_content_size_contribution = item_content_size_contribution.f32_max(content_size_contribution);
525        }
526    }
527
528    // Position hidden and absolutely positioned children
529    let mut order = items.len() as u32;
530    (0..tree.child_count(node)).for_each(|index| {
531        let child = tree.get_child_id(node, index);
532        let child_style = tree.get_grid_child_style(child);
533
534        // Position hidden child
535        if child_style.box_generation_mode() == BoxGenerationMode::None {
536            drop(child_style);
537            tree.set_unrounded_layout(child, &Layout::with_order(order));
538            tree.perform_child_layout(
539                child,
540                Size::NONE,
541                Size::NONE,
542                Size::MAX_CONTENT,
543                SizingMode::InherentSize,
544                Line::FALSE,
545            );
546            order += 1;
547            return;
548        }
549
550        // Position absolutely positioned child
551        if child_style.position() == Position::Absolute {
552            // Convert grid-col-{start/end} into Option's of indexes into the columns vector
553            // The Option is None if the style property is Auto and an unresolvable Span
554            let maybe_col_indexes = name_resolver
555                .resolve_column_names(&child_style.grid_column())
556                .into_origin_zero(final_col_counts.explicit)
557                .resolve_absolutely_positioned_grid_tracks()
558                .map(|maybe_grid_line| {
559                    maybe_grid_line.and_then(|line: OriginZeroLine| line.try_into_track_vec_index(final_col_counts))
560                });
561            // Convert grid-row-{start/end} into Option's of indexes into the row vector
562            // The Option is None if the style property is Auto and an unresolvable Span
563            let maybe_row_indexes = name_resolver
564                .resolve_row_names(&child_style.grid_row())
565                .into_origin_zero(final_row_counts.explicit)
566                .resolve_absolutely_positioned_grid_tracks()
567                .map(|maybe_grid_line| {
568                    maybe_grid_line.and_then(|line: OriginZeroLine| line.try_into_track_vec_index(final_row_counts))
569                });
570
571            let grid_area = Rect {
572                top: maybe_row_indexes.start.map(|index| rows[index].offset).unwrap_or(border.top),
573                bottom: maybe_row_indexes
574                    .end
575                    .map(|index| rows[index].offset)
576                    .unwrap_or(container_border_box.height - border.bottom - scrollbar_gutter.y),
577                left: maybe_col_indexes.start.map(|index| columns[index].offset).unwrap_or(border.left),
578                right: maybe_col_indexes
579                    .end
580                    .map(|index| columns[index].offset)
581                    .unwrap_or(container_border_box.width - border.right - scrollbar_gutter.x),
582            };
583            drop(child_style);
584
585            // TODO: Baseline alignment support for absolutely positioned items (should check if is actuallty specified)
586            #[cfg_attr(not(feature = "content_size"), allow(unused_variables))]
587            let (content_size_contribution, _, _) =
588                align_and_position_item(tree, child, order, grid_area, container_alignment_styles, 0.0);
589            #[cfg(feature = "content_size")]
590            {
591                item_content_size_contribution = item_content_size_contribution.f32_max(content_size_contribution);
592            }
593
594            order += 1;
595        }
596    });
597
598    // Set detailed grid information
599    #[cfg(feature = "detailed_layout_info")]
600    tree.set_detailed_grid_info(
601        node,
602        DetailedGridInfo {
603            rows: DetailedGridTracksInfo::from_grid_tracks_and_track_count(final_row_counts, rows),
604            columns: DetailedGridTracksInfo::from_grid_tracks_and_track_count(final_col_counts, columns),
605            items: items.iter().map(DetailedGridItemsInfo::from_grid_item).collect(),
606        },
607    );
608
609    // If there are not items then return just the container size (no baseline)
610    if items.is_empty() {
611        return LayoutOutput::from_outer_size(container_border_box);
612    }
613
614    // Determine the grid container baseline(s) (currently we only compute the first baseline)
615    let grid_container_baseline: f32 = {
616        // Sort items by row start position so that we can iterate items in groups which are in the same row
617        items.sort_by_key(|item| item.row_indexes.start);
618
619        // Get the row index of the first row containing items
620        let first_row = items[0].row_indexes.start;
621
622        // Create a slice of all of the items start in this row (taking advantage of the fact that we have just sorted the array)
623        let first_row_items = &items[0..].split(|item| item.row_indexes.start != first_row).next().unwrap();
624
625        // Check if any items in *this row* are baseline aligned
626        let row_has_baseline_item = first_row_items.iter().any(|item| item.align_self == AlignSelf::Baseline);
627
628        let item = if row_has_baseline_item {
629            first_row_items.iter().find(|item| item.align_self == AlignSelf::Baseline).unwrap()
630        } else {
631            &first_row_items[0]
632        };
633
634        item.y_position + item.baseline.unwrap_or(item.height)
635    };
636
637    LayoutOutput::from_sizes_and_baselines(
638        container_border_box,
639        item_content_size_contribution,
640        Point { x: None, y: Some(grid_container_baseline) },
641    )
642}
643
644/// Information from the computation of grid
645#[derive(Debug, Clone, PartialEq)]
646#[cfg(feature = "detailed_layout_info")]
647pub struct DetailedGridInfo {
648    /// <https://drafts.csswg.org/css-grid-1/#grid-row>
649    pub rows: DetailedGridTracksInfo,
650    /// <https://drafts.csswg.org/css-grid-1/#grid-column>
651    pub columns: DetailedGridTracksInfo,
652    /// <https://drafts.csswg.org/css-grid-1/#grid-items>
653    pub items: Vec<DetailedGridItemsInfo>,
654}
655
656/// Information from the computation of grids tracks
657#[derive(Debug, Clone, PartialEq)]
658#[cfg(feature = "detailed_layout_info")]
659pub struct DetailedGridTracksInfo {
660    /// Number of leading implicit grid tracks
661    pub negative_implicit_tracks: u16,
662    /// Number of explicit grid tracks
663    pub explicit_tracks: u16,
664    /// Number of trailing implicit grid tracks
665    pub positive_implicit_tracks: u16,
666
667    /// Gutters between tracks
668    pub gutters: Vec<f32>,
669    /// The used size of the tracks
670    pub sizes: Vec<f32>,
671}
672
673#[cfg(feature = "detailed_layout_info")]
674impl DetailedGridTracksInfo {
675    /// Get the base_size of [`GridTrack`] with a kind [`types::GridTrackKind`]
676    #[inline(always)]
677    fn grid_track_base_size_of_kind(grid_tracks: &[GridTrack], kind: GridTrackKind) -> Vec<f32> {
678        grid_tracks
679            .iter()
680            .filter_map(|track| match track.kind == kind {
681                true => Some(track.base_size),
682                false => None,
683            })
684            .collect()
685    }
686
687    /// Get the sizes of the gutters
688    fn gutters_from_grid_track_layout(grid_tracks: &[GridTrack]) -> Vec<f32> {
689        DetailedGridTracksInfo::grid_track_base_size_of_kind(grid_tracks, GridTrackKind::Gutter)
690    }
691
692    /// Get the sizes of the tracks
693    fn sizes_from_grid_track_layout(grid_tracks: &[GridTrack]) -> Vec<f32> {
694        DetailedGridTracksInfo::grid_track_base_size_of_kind(grid_tracks, GridTrackKind::Track)
695    }
696
697    /// Construct DetailedGridTracksInfo from TrackCounts and GridTracks
698    fn from_grid_tracks_and_track_count(track_count: TrackCounts, grid_tracks: Vec<GridTrack>) -> Self {
699        DetailedGridTracksInfo {
700            negative_implicit_tracks: track_count.negative_implicit,
701            explicit_tracks: track_count.explicit,
702            positive_implicit_tracks: track_count.positive_implicit,
703            gutters: DetailedGridTracksInfo::gutters_from_grid_track_layout(&grid_tracks),
704            sizes: DetailedGridTracksInfo::sizes_from_grid_track_layout(&grid_tracks),
705        }
706    }
707}
708
709/// Grid area information from the placement algorithm
710///
711/// The values is 1-indexed grid line numbers bounding the area.
712/// This matches the Chrome and Firefox's format as of 2nd Jan 2024.
713#[derive(Debug, Clone, PartialEq)]
714#[cfg(feature = "detailed_layout_info")]
715pub struct DetailedGridItemsInfo {
716    /// row-start with 1-indexed grid line numbers
717    pub row_start: u16,
718    /// row-end with 1-indexed grid line numbers
719    pub row_end: u16,
720    /// column-start with 1-indexed grid line numbers
721    pub column_start: u16,
722    /// column-end with 1-indexed grid line numbers
723    pub column_end: u16,
724}
725
726/// Grid area information from the placement algorithm
727#[cfg(feature = "detailed_layout_info")]
728impl DetailedGridItemsInfo {
729    /// Construct from GridItems
730    #[inline(always)]
731    fn from_grid_item(grid_item: &GridItem) -> Self {
732        /// Conversion from the indexes of Vec<GridTrack> into 1-indexed grid line numbers. See [`GridItem::row_indexes`] or [`GridItem::column_indexes`]
733        #[inline(always)]
734        fn to_one_indexed_grid_line(grid_track_index: u16) -> u16 {
735            grid_track_index / 2 + 1
736        }
737
738        DetailedGridItemsInfo {
739            row_start: to_one_indexed_grid_line(grid_item.row_indexes.start),
740            row_end: to_one_indexed_grid_line(grid_item.row_indexes.end),
741            column_start: to_one_indexed_grid_line(grid_item.column_indexes.start),
742            column_end: to_one_indexed_grid_line(grid_item.column_indexes.end),
743        }
744    }
745}