gioser

tawasuyu/taffy

A high performance rust-powered UI layout library

HTML 5364Rust 86Markdown 12TOML 91841 Definiciones

⎇ 9915b967e9809dc57f210de8fe5f7510e66806d6 src/compute/grid/mod.rs Rust · 606 líneas · 25.8 KB Historia Quién tocó qué crudo

Stylo integration (#693) 9915b967e · Nico Burns ·

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