tawasuyu

tawasuyu/taffy

A high performance rust-powered UI layout library

Rust 1077HTML 1008Markdown 12TOML 83209 Definitions

⎇ 8437796cc084de78ee04e2d940086954c924cb98 src/compute/grid/track_sizing.rs Rust · 1409 lines · 66.4 KB History Who touched what raw

Feature flag calc values (#806) 8437796cc · Nico Burns ·

1//! Implements the track sizing algorithm
2//! <https://www.w3.org/TR/css-grid-1/#layout-algorithm>
3use super::types::{GridItem, GridTrack, TrackCounts};
4use crate::geometry::{AbstractAxis, Line, Size};
5use crate::style::{AlignContent, AlignSelf, AvailableSpace};
6use crate::style_helpers::TaffyMinContent;
7use crate::tree::{LayoutPartialTree, LayoutPartialTreeExt, SizingMode};
8use crate::util::sys::{f32_max, f32_min, Vec};
9use crate::util::{MaybeMath, ResolveOrZero};
10use crate::CompactLength;
11use core::cmp::Ordering;
12
13/// Takes an axis, and a list of grid items sorted firstly by whether they cross a flex track
14/// in the specified axis (items that don't cross a flex track first) and then by the number
15/// of tracks they cross in specified axis (ascending order).
16struct ItemBatcher {
17    /// The axis in which the ItemBatcher is operating. Used when querying properties from items.
18    axis: AbstractAxis,
19    /// The starting index of the current batch
20    index_offset: usize,
21    /// The span of the items in the current batch
22    current_span: u16,
23    /// Whether the current batch of items cross a flexible track
24    current_is_flex: bool,
25}
26
27impl ItemBatcher {
28    /// Create a new ItemBatcher for the specified axis
29    #[inline(always)]
30    fn new(axis: AbstractAxis) -> Self {
31        ItemBatcher { index_offset: 0, axis, current_span: 1, current_is_flex: false }
32    }
33
34    /// This is basically a manual version of Iterator::next which passes `items`
35    /// in as a parameter on each iteration to work around borrow checker rules
36    #[inline]
37    fn next<'items>(&mut self, items: &'items mut [GridItem]) -> Option<(&'items mut [GridItem], bool)> {
38        if self.current_is_flex || self.index_offset >= items.len() {
39            return None;
40        }
41
42        let item = &items[self.index_offset];
43        self.current_span = item.span(self.axis);
44        self.current_is_flex = item.crosses_flexible_track(self.axis);
45
46        let next_index_offset = if self.current_is_flex {
47            items.len()
48        } else {
49            items
50                .iter()
51                .position(|item: &GridItem| {
52                    item.crosses_flexible_track(self.axis) || item.span(self.axis) > self.current_span
53                })
54                .unwrap_or(items.len())
55        };
56
57        let batch_range = self.index_offset..next_index_offset;
58        self.index_offset = next_index_offset;
59
60        let batch = &mut items[batch_range];
61        Some((batch, self.current_is_flex))
62    }
63}
64
65/// This struct captures a bunch of variables which are used to compute the intrinsic sizes of children so that those variables
66/// don't have to be passed around all over the place below. It then has methods that implement the intrinsic sizing computations
67struct IntrisicSizeMeasurer<'tree, 'oat, Tree, EstimateFunction>
68where
69    Tree: LayoutPartialTree,
70    EstimateFunction: Fn(&GridTrack, Option<f32>, &Tree) -> Option<f32>,
71{
72    /// The layout tree
73    tree: &'tree mut Tree,
74    /// The tracks in the opposite axis to the one we are currently sizing
75    other_axis_tracks: &'oat [GridTrack],
76    /// A function that computes an estimate of an other-axis track's size which is passed to
77    /// the child size measurement functions
78    get_track_size_estimate: EstimateFunction,
79    /// The axis we are currently sizing
80    axis: AbstractAxis,
81    /// The available grid space
82    inner_node_size: Size<Option<f32>>,
83}
84
85impl<Tree, EstimateFunction> IntrisicSizeMeasurer<'_, '_, Tree, EstimateFunction>
86where
87    Tree: LayoutPartialTree,
88    EstimateFunction: Fn(&GridTrack, Option<f32>, &Tree) -> Option<f32>,
89{
90    /// Compute the available_space to be passed to the child sizing functions
91    /// These are estimates based on either the max track sizing function or the provisional base size in the opposite
92    /// axis to the one currently being sized.
93    /// https://www.w3.org/TR/css-grid-1/#algo-overview
94    #[inline(always)]
95    fn available_space(&self, item: &mut GridItem) -> Size<Option<f32>> {
96        item.available_space_cached(
97            self.axis,
98            self.other_axis_tracks,
99            self.inner_node_size.get(self.axis.other()),
100            |track, basis| (self.get_track_size_estimate)(track, basis, self.tree),
101        )
102    }
103
104    /// Compute the item's resolved margins for size contributions. Horizontal percentage margins always resolve
105    /// to zero if the container size is indefinite as otherwise this would introduce a cyclic dependency.
106    #[inline(always)]
107    fn margins_axis_sums_with_baseline_shims(&self, item: &GridItem) -> Size<f32> {
108        item.margins_axis_sums_with_baseline_shims(self.inner_node_size.width, self.tree)
109    }
110
111    /// Simple pass-through function to `LayoutPartialTreeExt::calc`
112    #[inline(always)]
113    fn calc(&self, val: *const (), basis: f32) -> f32 {
114        self.tree.calc(val, basis)
115    }
116
117    /// Retrieve the item's min content contribution from the cache or compute it using the provided parameters
118    #[inline(always)]
119    fn min_content_contribution(&mut self, item: &mut GridItem) -> f32 {
120        let available_space = self.available_space(item);
121        let margin_axis_sums = self.margins_axis_sums_with_baseline_shims(item);
122        let contribution =
123            item.min_content_contribution_cached(self.axis, self.tree, available_space, self.inner_node_size);
124        contribution + margin_axis_sums.get(self.axis)
125    }
126
127    /// Retrieve the item's max content contribution from the cache or compute it using the provided parameters
128    #[inline(always)]
129    fn max_content_contribution(&mut self, item: &mut GridItem) -> f32 {
130        let available_space = self.available_space(item);
131        let margin_axis_sums = self.margins_axis_sums_with_baseline_shims(item);
132        let contribution =
133            item.max_content_contribution_cached(self.axis, self.tree, available_space, self.inner_node_size);
134        contribution + margin_axis_sums.get(self.axis)
135    }
136
137    /// The minimum contribution of an item is the smallest outer size it can have.
138    /// Specifically:
139    ///   - If the item’s computed preferred size behaves as auto or depends on the size of its containing block in the relevant axis:
140    ///     Its minimum contribution is the outer size that would result from assuming the item’s used minimum size as its preferred size;
141    ///   - Else the item’s minimum contribution is its min-content contribution.
142    ///
143    /// Because the minimum contribution often depends on the size of the item’s content, it is considered a type of intrinsic size contribution.
144    #[inline(always)]
145    fn minimum_contribution(&mut self, item: &mut GridItem, axis_tracks: &[GridTrack]) -> f32 {
146        let available_space = self.available_space(item);
147        let margin_axis_sums = self.margins_axis_sums_with_baseline_shims(item);
148        let contribution =
149            item.minimum_contribution_cached(self.tree, self.axis, axis_tracks, available_space, self.inner_node_size);
150        contribution + margin_axis_sums.get(self.axis)
151    }
152}
153
154/// To make track sizing efficient we want to order tracks
155/// Here a placement is either a Line<i16> representing a row-start/row-end or a column-start/column-end
156#[inline(always)]
157pub(super) fn cmp_by_cross_flex_then_span_then_start(
158    axis: AbstractAxis,
159) -> impl FnMut(&GridItem, &GridItem) -> Ordering {
160    move |item_a: &GridItem, item_b: &GridItem| -> Ordering {
161        match (item_a.crosses_flexible_track(axis), item_b.crosses_flexible_track(axis)) {
162            (false, true) => Ordering::Less,
163            (true, false) => Ordering::Greater,
164            _ => {
165                let placement_a = item_a.placement(axis);
166                let placement_b = item_b.placement(axis);
167                match placement_a.span().cmp(&placement_b.span()) {
168                    Ordering::Less => Ordering::Less,
169                    Ordering::Greater => Ordering::Greater,
170                    Ordering::Equal => placement_a.start.cmp(&placement_b.start),
171                }
172            }
173        }
174    }
175}
176
177/// When applying the track sizing algorithm and estimating the size in the other axis for content sizing items
178/// we should take into account align-content/justify-content if both the grid container and all items in the
179/// other axis have definite sizes. This function computes such a per-gutter additional size adjustment.
180#[inline(always)]
181pub(super) fn compute_alignment_gutter_adjustment(
182    alignment: AlignContent,
183    axis_inner_node_size: Option<f32>,
184    get_track_size_estimate: impl Fn(&GridTrack, Option<f32>) -> Option<f32>,
185    tracks: &[GridTrack],
186) -> f32 {
187    if tracks.len() <= 1 {
188        return 0.0;
189    }
190
191    // As items never cross the outermost gutters in a grid, we can simplify our calculations by treating
192    // AlignContent::Start and AlignContent::End the same
193    let outer_gutter_weight = match alignment {
194        AlignContent::Start => 1,
195        AlignContent::FlexStart => 1,
196        AlignContent::End => 1,
197        AlignContent::FlexEnd => 1,
198        AlignContent::Center => 1,
199        AlignContent::Stretch => 0,
200        AlignContent::SpaceBetween => 0,
201        AlignContent::SpaceAround => 1,
202        AlignContent::SpaceEvenly => 1,
203    };
204
205    let inner_gutter_weight = match alignment {
206        AlignContent::FlexStart => 0,
207        AlignContent::Start => 0,
208        AlignContent::FlexEnd => 0,
209        AlignContent::End => 0,
210        AlignContent::Center => 0,
211        AlignContent::Stretch => 0,
212        AlignContent::SpaceBetween => 1,
213        AlignContent::SpaceAround => 2,
214        AlignContent::SpaceEvenly => 1,
215    };
216
217    if inner_gutter_weight == 0 {
218        return 0.0;
219    }
220
221    if let Some(axis_inner_node_size) = axis_inner_node_size {
222        let free_space = tracks
223            .iter()
224            .map(|track| get_track_size_estimate(track, Some(axis_inner_node_size)))
225            .sum::<Option<f32>>()
226            .map(|track_size_sum| f32_max(0.0, axis_inner_node_size - track_size_sum))
227            .unwrap_or(0.0);
228
229        let weighted_track_count =
230            (((tracks.len() - 3) / 2) * inner_gutter_weight as usize) + (2 * outer_gutter_weight as usize);
231
232        return (free_space / weighted_track_count as f32) * inner_gutter_weight as f32;
233    }
234
235    0.0
236}
237
238/// Convert origin-zero coordinates track placement in grid track vector indexes
239#[inline(always)]
240pub(super) fn resolve_item_track_indexes(items: &mut [GridItem], column_counts: TrackCounts, row_counts: TrackCounts) {
241    for item in items {
242        item.column_indexes = item.column.map(|line| line.into_track_vec_index(column_counts) as u16);
243        item.row_indexes = item.row.map(|line| line.into_track_vec_index(row_counts) as u16);
244    }
245}
246
247/// Determine (in each axis) whether the item crosses any flexible tracks
248#[inline(always)]
249pub(super) fn determine_if_item_crosses_flexible_or_intrinsic_tracks(
250    items: &mut Vec<GridItem>,
251    columns: &[GridTrack],
252    rows: &[GridTrack],
253) {
254    for item in items {
255        item.crosses_flexible_column =
256            item.track_range_excluding_lines(AbstractAxis::Inline).any(|i| columns[i].is_flexible());
257        item.crosses_intrinsic_column =
258            item.track_range_excluding_lines(AbstractAxis::Inline).any(|i| columns[i].has_intrinsic_sizing_function());
259        item.crosses_flexible_row =
260            item.track_range_excluding_lines(AbstractAxis::Block).any(|i| rows[i].is_flexible());
261        item.crosses_intrinsic_row =
262            item.track_range_excluding_lines(AbstractAxis::Block).any(|i| rows[i].has_intrinsic_sizing_function());
263    }
264}
265
266/// Track sizing algorithm
267/// Note: Gutters are treated as empty fixed-size tracks for the purpose of the track sizing algorithm.
268#[allow(clippy::too_many_arguments)]
269pub(super) fn track_sizing_algorithm<Tree: LayoutPartialTree>(
270    tree: &mut Tree,
271    axis: AbstractAxis,
272    axis_min_size: Option<f32>,
273    axis_max_size: Option<f32>,
274    axis_alignment: AlignContent,
275    other_axis_alignment: AlignContent,
276    available_grid_space: Size<AvailableSpace>,
277    inner_node_size: Size<Option<f32>>,
278    axis_tracks: &mut [GridTrack],
279    other_axis_tracks: &mut [GridTrack],
280    items: &mut [GridItem],
281    get_track_size_estimate: fn(&GridTrack, Option<f32>, &Tree) -> Option<f32>,
282    has_baseline_aligned_item: bool,
283) {
284    // 11.4 Initialise Track sizes
285    // Initialize each track’s base size and growth limit.
286    initialize_track_sizes(tree, axis_tracks, inner_node_size.get(axis));
287
288    // 11.5.1 Shim item baselines
289    if has_baseline_aligned_item {
290        resolve_item_baselines(tree, axis, items, inner_node_size);
291    }
292
293    // If all tracks have base_size = growth_limit, then skip the rest of this function.
294    // Note: this can only happen both track sizing function have the same fixed track sizing function
295    if axis_tracks.iter().all(|track| track.base_size == track.growth_limit) {
296        return;
297    }
298
299    // Pre-computations for 11.5 Resolve Intrinsic Track Sizes
300
301    // Compute an additional amount to add to each spanned gutter when computing item's estimated size in the
302    // in the opposite axis based on the alignment, container size, and estimated track sizes in that axis
303    let gutter_alignment_adjustment = compute_alignment_gutter_adjustment(
304        other_axis_alignment,
305        inner_node_size.get(axis.other()),
306        |track, basis| get_track_size_estimate(track, basis, tree),
307        other_axis_tracks,
308    );
309    if other_axis_tracks.len() > 3 {
310        let len = other_axis_tracks.len();
311        let inner_gutter_tracks = other_axis_tracks[2..len].iter_mut().step_by(2);
312        for track in inner_gutter_tracks {
313            track.content_alignment_adjustment = gutter_alignment_adjustment;
314        }
315    }
316
317    // 11.5 Resolve Intrinsic Track Sizes
318    resolve_intrinsic_track_sizes(
319        tree,
320        axis,
321        axis_tracks,
322        other_axis_tracks,
323        items,
324        available_grid_space.get(axis),
325        inner_node_size,
326        get_track_size_estimate,
327    );
328
329    // 11.6. Maximise Tracks
330    // Distributes free space (if any) to tracks with FINITE growth limits, up to their limits.
331    maximise_tracks(axis_tracks, inner_node_size.get(axis), available_grid_space.get(axis));
332
333    // For the purpose of the final two expansion steps ("Expand Flexible Tracks" and "Stretch auto Tracks"), we only want to expand
334    // into space generated by the grid container's size (as defined by either it's preferred size style or by it's parent node through
335    // something like stretch alignment), not just any available space. To do this we map definite available space to AvailableSpace::MaxContent
336    // in the case that inner_node_size is None
337    let axis_available_space_for_expansion = if let Some(available_space) = inner_node_size.get(axis) {
338        AvailableSpace::Definite(available_space)
339    } else {
340        match available_grid_space.get(axis) {
341            AvailableSpace::MinContent => AvailableSpace::MinContent,
342            AvailableSpace::MaxContent | AvailableSpace::Definite(_) => AvailableSpace::MaxContent,
343        }
344    };
345
346    // 11.7. Expand Flexible Tracks
347    // This step sizes flexible tracks using the largest value it can assign to an fr without exceeding the available space.
348    expand_flexible_tracks(
349        tree,
350        axis,
351        axis_tracks,
352        items,
353        axis_min_size,
354        axis_max_size,
355        axis_available_space_for_expansion,
356        inner_node_size,
357    );
358
359    // 11.8. Stretch auto Tracks
360    // This step expands tracks that have an auto max track sizing function by dividing any remaining positive, definite free space equally amongst them.
361    if axis_alignment == AlignContent::Stretch {
362        stretch_auto_tracks(axis_tracks, axis_min_size, axis_available_space_for_expansion);
363    }
364}
365
366/// Whether it is a minimum or maximum size's space being distributed
367/// This controls behaviour of the space distribution algorithm when distributing beyond limits
368/// See "distributing space beyond limits" at https://www.w3.org/TR/css-grid-1/#extra-space
369#[derive(Copy, Clone, Debug, PartialEq, Eq)]
370enum IntrinsicContributionType {
371    /// It's a minimum size's space being distributed
372    Minimum,
373    /// It's a maximum size's space being distributed
374    Maximum,
375}
376
377/// Add any planned base size increases to the base size after a round of distributing space to base sizes
378/// Reset the planed base size increase to zero ready for the next round.
379#[inline(always)]
380fn flush_planned_base_size_increases(tracks: &mut [GridTrack]) {
381    for track in tracks {
382        track.base_size += track.base_size_planned_increase;
383        track.base_size_planned_increase = 0.0;
384    }
385}
386
387/// Add any planned growth limit increases to the growth limit after a round of distributing space to growth limits
388/// Reset the planed growth limit increase to zero ready for the next round.
389#[inline(always)]
390fn flush_planned_growth_limit_increases(tracks: &mut [GridTrack], set_infinitely_growable: bool) {
391    for track in tracks {
392        if track.growth_limit_planned_increase > 0.0 {
393            track.growth_limit = if track.growth_limit == f32::INFINITY {
394                track.base_size + track.growth_limit_planned_increase
395            } else {
396                track.growth_limit + track.growth_limit_planned_increase
397            };
398            track.infinitely_growable = set_infinitely_growable;
399        } else {
400            track.infinitely_growable = false;
401        }
402        track.growth_limit_planned_increase = 0.0
403    }
404}
405
406/// 11.4 Initialise Track sizes
407/// Initialize each track’s base size and growth limit.
408#[inline(always)]
409fn initialize_track_sizes(
410    tree: &impl LayoutPartialTree,
411    axis_tracks: &mut [GridTrack],
412    axis_inner_node_size: Option<f32>,
413) {
414    for track in axis_tracks.iter_mut() {
415        // For each track, if the track’s min track sizing function is:
416        // - A fixed sizing function
417        //     Resolve to an absolute length and use that size as the track’s initial base size.
418        //     Note: Indefinite lengths cannot occur, as they’re treated as auto.
419        // - An intrinsic sizing function
420        //     Use an initial base size of zero.
421        track.base_size = track
422            .min_track_sizing_function
423            .definite_value(axis_inner_node_size, |val, basis| tree.calc(val, basis))
424            .unwrap_or(0.0);
425
426        // For each track, if the track’s max track sizing function is:
427        // - A fixed sizing function
428        //     Resolve to an absolute length and use that size as the track’s initial growth limit.
429        // - An intrinsic sizing function
430        //     Use an initial growth limit of infinity.
431        // - A flexible sizing function
432        //     Use an initial growth limit of infinity.
433        track.growth_limit = track
434            .max_track_sizing_function
435            .definite_value(axis_inner_node_size, |val, basis| tree.calc(val, basis))
436            .unwrap_or(f32::INFINITY);
437
438        // In all cases, if the growth limit is less than the base size, increase the growth limit to match the base size.
439        if track.growth_limit < track.base_size {
440            track.growth_limit = track.base_size;
441        }
442    }
443}
444
445/// 11.5.1 Shim baseline-aligned items so their intrinsic size contributions reflect their baseline alignment.
446fn resolve_item_baselines(
447    tree: &mut impl LayoutPartialTree,
448    axis: AbstractAxis,
449    items: &mut [GridItem],
450    inner_node_size: Size<Option<f32>>,
451) {
452    // Sort items by track in the other axis (row) start position so that we can iterate items in groups which
453    // are in the same track in the other axis (row)
454    let other_axis = axis.other();
455    items.sort_by_key(|item| item.placement(other_axis).start);
456
457    // Iterate over grid rows
458    let mut remaining_items = &mut items[0..];
459    while !remaining_items.is_empty() {
460        // Get the row index of the current row
461        let current_row = remaining_items[0].placement(other_axis).start;
462
463        // Find the item index of the first item that is in a different row (or None if we've reached the end of the list)
464        let next_row_first_item =
465            remaining_items.iter().position(|item| item.placement(other_axis).start != current_row);
466
467        // Use this index to split the `remaining_items` slice in two slices:
468        //    - A `row_items` slice containing the items (that start) in the current row
469        //    - A new `remaining_items` consisting of the remainder of the `remaining_items` slice
470        //      that hasn't been split off into `row_items
471        let row_items = if let Some(index) = next_row_first_item {
472            let (row_items, tail) = remaining_items.split_at_mut(index);
473            remaining_items = tail;
474            row_items
475        } else {
476            let row_items = remaining_items;
477            remaining_items = &mut [];
478            row_items
479        };
480
481        // Count how many items in *this row* are baseline aligned
482        // If a row has one or zero items participating in baseline alignment then baseline alignment is a no-op
483        // for those items and we skip further computations for that row
484        let row_baseline_item_count = row_items.iter().filter(|item| item.align_self == AlignSelf::Baseline).count();
485        if row_baseline_item_count <= 1 {
486            continue;
487        }
488
489        // Compute the baselines of all items in the row
490        for item in row_items.iter_mut() {
491            let measured_size_and_baselines = tree.perform_child_layout(
492                item.node,
493                Size::NONE,
494                inner_node_size,
495                Size::MIN_CONTENT,
496                SizingMode::InherentSize,
497                Line::FALSE,
498            );
499
500            let baseline = measured_size_and_baselines.first_baselines.y;
501            let height = measured_size_and_baselines.size.height;
502
503            item.baseline = Some(
504                baseline.unwrap_or(height)
505                    + item.margin.top.resolve_or_zero(inner_node_size.width, |val, basis| tree.calc(val, basis)),
506            );
507        }
508
509        // Compute the max baseline of all items in the row
510        let row_max_baseline =
511            row_items.iter().map(|item| item.baseline.unwrap_or(0.0)).max_by(|a, b| a.total_cmp(b)).unwrap();
512
513        // Compute the baseline shim for each item in the row
514        for item in row_items.iter_mut() {
515            item.baseline_shim = row_max_baseline - item.baseline.unwrap_or(0.0);
516        }
517    }
518}
519
520/// 11.5 Resolve Intrinsic Track Sizes
521#[allow(clippy::too_many_arguments)]
522fn resolve_intrinsic_track_sizes<Tree: LayoutPartialTree>(
523    tree: &mut Tree,
524    axis: AbstractAxis,
525    axis_tracks: &mut [GridTrack],
526    other_axis_tracks: &[GridTrack],
527    items: &mut [GridItem],
528    axis_available_grid_space: AvailableSpace,
529    inner_node_size: Size<Option<f32>>,
530    get_track_size_estimate: impl Fn(&GridTrack, Option<f32>, &Tree) -> Option<f32>,
531) {
532    // Step 1. Shim baseline-aligned items so their intrinsic size contributions reflect their baseline alignment.
533
534    // Already done at this point. See resolve_item_baselines function.
535
536    // Step 2.
537
538    // The track sizing algorithm requires us to iterate through the items in ascendeding order of the number of
539    // tracks they span (first items that span 1 track, then items that span 2 tracks, etc).
540    // To avoid having to do multiple iterations of the items, we pre-sort them into this order.
541    items.sort_by(cmp_by_cross_flex_then_span_then_start(axis));
542
543    // Step 2, Step 3 and Step 4
544    // 2 & 3. Iterate over items that don't cross a flex track. Items should have already been sorted in ascending order
545    // of the number of tracks they span. Step 2 is the 1 track case and has an optimised implementation
546    // 4. Next, repeat the previous step instead considering (together, rather than grouped by span size) all items
547    // that do span a track with a flexible sizing function while
548
549    // Compute item's intrinsic (content-based) sizes
550    // Note: For items with a specified minimum size of auto (the initial value), the minimum contribution is usually equivalent
551    // to the min-content contribution—but can differ in some cases, see §6.6 Automatic Minimum Size of Grid Items.
552    // Also, minimum contribution <= min-content contribution <= max-content contribution.
553
554    let axis_inner_node_size = inner_node_size.get(axis);
555    let flex_factor_sum = axis_tracks.iter().map(|track| track.flex_factor()).sum::<f32>();
556    let mut item_sizer =
557        IntrisicSizeMeasurer { tree, other_axis_tracks, axis, inner_node_size, get_track_size_estimate };
558
559    let mut batched_item_iterator = ItemBatcher::new(axis);
560    while let Some((batch, is_flex)) = batched_item_iterator.next(items) {
561        // 2. Size tracks to fit non-spanning items: For each track with an intrinsic track sizing function and not a flexible sizing function,
562        // consider the items in it with a span of 1:
563        let batch_span = batch[0].placement(axis).span();
564        if !is_flex && batch_span == 1 {
565            for item in batch.iter_mut() {
566                let track_index = item.placement_indexes(axis).start + 1;
567                let track = &axis_tracks[track_index as usize];
568
569                // Handle base sizes
570                let new_base_size = match track.min_track_sizing_function.0.tag() {
571                    CompactLength::MIN_CONTENT_TAG => {
572                        f32_max(track.base_size, item_sizer.min_content_contribution(item))
573                    }
574                    // If the container size is indefinite and has not yet been resolved then percentage sized
575                    // tracks should be treated as min-content (this matches Chrome's behaviour and seems sensible)
576                    CompactLength::PERCENT_TAG => {
577                        if axis_inner_node_size.is_none() {
578                            f32_max(track.base_size, item_sizer.min_content_contribution(item))
579                        } else {
580                            track.base_size
581                        }
582                    }
583                    CompactLength::MAX_CONTENT_TAG => {
584                        f32_max(track.base_size, item_sizer.max_content_contribution(item))
585                    }
586                    CompactLength::AUTO_TAG => {
587                        let space = match axis_available_grid_space {
588                            // QUIRK: The spec says that:
589                            //
590                            //   If the grid container is being sized under a min- or max-content constraint, use the items’ limited
591                            //   min-content contributions in place of their minimum contributions here.
592                            //
593                            // However, in practice browsers only seem to apply this rule if the item is not a scroll container
594                            // (note that overflow:hidden counts as a scroll container), giving the automatic minimum size of scroll
595                            // containers (zero) precedence over the min-content contributions.
596                            AvailableSpace::MinContent | AvailableSpace::MaxContent
597                                if !item.overflow.get(axis).is_scroll_container() =>
598                            {
599                                let axis_minimum_size = item_sizer.minimum_contribution(item, axis_tracks);
600                                let axis_min_content_size = item_sizer.min_content_contribution(item);
601                                let limit = track
602                                    .max_track_sizing_function
603                                    .definite_limit(axis_inner_node_size, |val, basis| item_sizer.calc(val, basis));
604                                axis_min_content_size.maybe_min(limit).max(axis_minimum_size)
605                            }
606                            _ => item_sizer.minimum_contribution(item, axis_tracks),
607                        };
608                        f32_max(track.base_size, space)
609                    }
610                    CompactLength::LENGTH_TAG => {
611                        // Do nothing as it's not an intrinsic track sizing function
612                        track.base_size
613                    }
614                    // Handle calc() like percentage
615                    #[cfg(feature = "calc")]
616                    _ if track.min_track_sizing_function.0.is_calc() => {
617                        if axis_inner_node_size.is_none() {
618                            f32_max(track.base_size, item_sizer.min_content_contribution(item))
619                        } else {
620                            track.base_size
621                        }
622                    }
623                    _ => unreachable!(),
624                };
625                let track = &mut axis_tracks[track_index as usize];
626                track.base_size = new_base_size;
627
628                // Handle growth limits
629                if track.max_track_sizing_function.is_fit_content() {
630                    // If item is not a scroll container, then increase the growth limit to at least the
631                    // size of the min-content contribution
632                    if !item.overflow.get(axis).is_scroll_container() {
633                        let min_content_contribution = item_sizer.min_content_contribution(item);
634                        track.growth_limit_planned_increase =
635                            f32_max(track.growth_limit_planned_increase, min_content_contribution);
636                    }
637
638                    // Always increase the growth limit to at least the size of the *fit-content limited*
639                    // max-cotent contribution
640                    let fit_content_limit = track.fit_content_limit(axis_inner_node_size);
641                    let max_content_contribution =
642                        f32_min(item_sizer.max_content_contribution(item), fit_content_limit);
643                    track.growth_limit_planned_increase =
644                        f32_max(track.growth_limit_planned_increase, max_content_contribution);
645                } else if track.max_track_sizing_function.is_max_content_alike()
646                    || track.max_track_sizing_function.uses_percentage() && axis_inner_node_size.is_none()
647                {
648                    // If the container size is indefinite and has not yet been resolved then percentage sized
649                    // tracks should be treated as auto (this matches Chrome's behaviour and seems sensible)
650                    track.growth_limit_planned_increase =
651                        f32_max(track.growth_limit_planned_increase, item_sizer.max_content_contribution(item));
652                } else if track.max_track_sizing_function.is_intrinsic() {
653                    track.growth_limit_planned_increase =
654                        f32_max(track.growth_limit_planned_increase, item_sizer.min_content_contribution(item));
655                }
656            }
657
658            for track in axis_tracks.iter_mut() {
659                if track.growth_limit_planned_increase > 0.0 {
660                    track.growth_limit = if track.growth_limit == f32::INFINITY {
661                        track.growth_limit_planned_increase
662                    } else {
663                        f32_max(track.growth_limit, track.growth_limit_planned_increase)
664                    };
665                }
666                track.infinitely_growable = false;
667                track.growth_limit_planned_increase = 0.0;
668                if track.growth_limit < track.base_size {
669                    track.growth_limit = track.base_size;
670                }
671            }
672
673            continue;
674        }
675
676        let use_flex_factor_for_distribution = is_flex && flex_factor_sum != 0.0;
677
678        // 1. For intrinsic minimums:
679        // First increase the base size of tracks with an intrinsic min track sizing function
680        for item in batch.iter_mut().filter(|item| item.crosses_intrinsic_track(axis)) {
681            // ...by distributing extra space as needed to accommodate these items’ minimum contributions.
682            //
683            // QUIRK: The spec says that:
684            //
685            //   If the grid container is being sized under a min- or max-content constraint, use the items’ limited min-content contributions
686            //   in place of their minimum contributions here.
687            //
688            // However, in practice browsers only seem to apply this rule if the item is not a scroll container (note that overflow:hidden counts as
689            // a scroll container), giving the automatic minimum size of scroll containers (zero) precedence over the min-content contributions.
690            let space = match axis_available_grid_space {
691                AvailableSpace::MinContent | AvailableSpace::MaxContent
692                    if !item.overflow.get(axis).is_scroll_container() =>
693                {
694                    let axis_minimum_size = item_sizer.minimum_contribution(item, axis_tracks);
695                    let axis_min_content_size = item_sizer.min_content_contribution(item);
696                    let limit = item.spanned_track_limit(axis, axis_tracks, axis_inner_node_size, &|val, basis| {
697                        item_sizer.calc(val, basis)
698                    });
699                    axis_min_content_size.maybe_min(limit).max(axis_minimum_size)
700                }
701                _ => item_sizer.minimum_contribution(item, axis_tracks),
702            };
703            let tracks = &mut axis_tracks[item.track_range_excluding_lines(axis)];
704            if space > 0.0 {
705                let has_intrinsic_min_track_sizing_function = |track: &GridTrack| {
706                    track
707                        .min_track_sizing_function
708                        .definite_value(axis_inner_node_size, |val, basis| item_sizer.calc(val, basis))
709                        .is_none()
710                };
711                if item.overflow.get(axis).is_scroll_container() {
712                    let fit_content_limit =
713                        move |track: &GridTrack| track.fit_content_limited_growth_limit(axis_inner_node_size);
714                    distribute_item_space_to_base_size(
715                        is_flex,
716                        use_flex_factor_for_distribution,
717                        space,
718                        tracks,
719                        has_intrinsic_min_track_sizing_function,
720                        fit_content_limit,
721                        IntrinsicContributionType::Minimum,
722                    );
723                } else {
724                    distribute_item_space_to_base_size(
725                        is_flex,
726                        use_flex_factor_for_distribution,
727                        space,
728                        tracks,
729                        has_intrinsic_min_track_sizing_function,
730                        |track| track.growth_limit,
731                        IntrinsicContributionType::Minimum,
732                    );
733                }
734            }
735        }
736        flush_planned_base_size_increases(axis_tracks);
737
738        // 2. For content-based minimums:
739        // Next continue to increase the base size of tracks with a min track sizing function of min-content or max-content
740        // by distributing extra space as needed to account for these items' min-content contributions.
741        let has_min_or_max_content_min_track_sizing_function =
742            move |track: &GridTrack| track.min_track_sizing_function.is_min_or_max_content();
743        for item in batch.iter_mut() {
744            let space = item_sizer.min_content_contribution(item);
745            let tracks = &mut axis_tracks[item.track_range_excluding_lines(axis)];
746            if space > 0.0 {
747                if item.overflow.get(axis).is_scroll_container() {
748                    let fit_content_limit =
749                        move |track: &GridTrack| track.fit_content_limited_growth_limit(axis_inner_node_size);
750                    distribute_item_space_to_base_size(
751                        is_flex,
752                        use_flex_factor_for_distribution,
753                        space,
754                        tracks,
755                        has_min_or_max_content_min_track_sizing_function,
756                        fit_content_limit,
757                        IntrinsicContributionType::Minimum,
758                    );
759                } else {
760                    distribute_item_space_to_base_size(
761                        is_flex,
762                        use_flex_factor_for_distribution,
763                        space,
764                        tracks,
765                        has_min_or_max_content_min_track_sizing_function,
766                        |track| track.growth_limit,
767                        IntrinsicContributionType::Minimum,
768                    );
769                }
770            }
771        }
772        flush_planned_base_size_increases(axis_tracks);
773
774        // 3. For max-content minimums:
775
776        // If the grid container is being sized under a max-content constraint, continue to increase the base size of tracks with
777        // a min track sizing function of auto or max-content by distributing extra space as needed to account for these items'
778        // limited max-content contributions.
779
780        // Define fit_content_limited_growth_limit function. This is passed to the distribute_space_up_to_limits
781        // helper function, and is used to compute the limit to distribute up to for each track.
782        // Wrapping the method on GridTrack is necessary in order to resolve percentage fit-content arguments.
783        if axis_available_grid_space == AvailableSpace::MaxContent {
784            /// Whether a track:
785            ///   - has an Auto MIN track sizing function
786            ///   - Does not have a MinContent MAX track sizing function
787            ///
788            /// The latter condition was added in order to match Chrome. But I believe it is due to the provision
789            /// under minmax here https://www.w3.org/TR/css-grid-1/#track-sizes which states that:
790            ///
791            ///    "If the max is less than the min, then the max will be floored by the min (essentially yielding minmax(min, min))"
792            #[inline(always)]
793            fn has_auto_min_track_sizing_function(track: &GridTrack) -> bool {
794                track.min_track_sizing_function.is_auto() && !track.max_track_sizing_function.is_min_content()
795            }
796
797            /// Whether a track has a MaxContent min track sizing function
798            #[inline(always)]
799            fn has_max_content_min_track_sizing_function(track: &GridTrack) -> bool {
800                track.min_track_sizing_function.is_max_content()
801            }
802
803            for item in batch.iter_mut() {
804                let axis_max_content_size = item_sizer.max_content_contribution(item);
805                let limit = item.spanned_track_limit(axis, axis_tracks, axis_inner_node_size, &|val, basis| {
806                    item_sizer.calc(val, basis)
807                });
808                let space = axis_max_content_size.maybe_min(limit);
809                let tracks = &mut axis_tracks[item.track_range_excluding_lines(axis)];
810                if space > 0.0 {
811                    // If any of the tracks spanned by the item have a MaxContent min track sizing function then
812                    // distribute space only to those tracks. Otherwise distribute space to tracks with an Auto min
813                    // track sizing function.
814                    //
815                    // Note: this prioritisation of MaxContent over Auto is not mentioned in the spec (which suggests that
816                    // we ought to distribute space evenly between MaxContent and Auto tracks). But it is implemented like
817                    // this in both Chrome and Firefox (and it does have a certain logic to it), so we implement it too for
818                    // compatibility.
819                    //
820                    // See: https://www.w3.org/TR/css-grid-1/#track-size-max-content-min
821                    if tracks.iter().any(has_max_content_min_track_sizing_function) {
822                        distribute_item_space_to_base_size(
823                            is_flex,
824                            use_flex_factor_for_distribution,
825                            space,
826                            tracks,
827                            has_max_content_min_track_sizing_function,
828                            |_| f32::INFINITY,
829                            IntrinsicContributionType::Maximum,
830                        );
831                    } else {
832                        let fit_content_limited_growth_limit =
833                            move |track: &GridTrack| track.fit_content_limited_growth_limit(axis_inner_node_size);
834                        distribute_item_space_to_base_size(
835                            is_flex,
836                            use_flex_factor_for_distribution,
837                            space,
838                            tracks,
839                            has_auto_min_track_sizing_function,
840                            fit_content_limited_growth_limit,
841                            IntrinsicContributionType::Maximum,
842                        );
843                    }
844                }
845            }
846            flush_planned_base_size_increases(axis_tracks);
847        }
848
849        // In all cases, continue to increase the base size of tracks with a min track sizing function of max-content by distributing
850        // extra space as needed to account for these items' max-content contributions.
851        let has_max_content_min_track_sizing_function =
852            move |track: &GridTrack| track.min_track_sizing_function.is_max_content();
853        for item in batch.iter_mut() {
854            let axis_max_content_size = item_sizer.max_content_contribution(item);
855            let space = axis_max_content_size;
856            let tracks = &mut axis_tracks[item.track_range_excluding_lines(axis)];
857            if space > 0.0 {
858                distribute_item_space_to_base_size(
859                    is_flex,
860                    use_flex_factor_for_distribution,
861                    space,
862                    tracks,
863                    has_max_content_min_track_sizing_function,
864                    |track| track.growth_limit,
865                    IntrinsicContributionType::Maximum,
866                );
867            }
868        }
869        flush_planned_base_size_increases(axis_tracks);
870
871        // 4. If at this point any track’s growth limit is now less than its base size, increase its growth limit to match its base size.
872        for track in axis_tracks.iter_mut() {
873            if track.growth_limit < track.base_size {
874                track.growth_limit = track.base_size;
875            }
876        }
877
878        // If a track is a flexible track, then it has flexible max track sizing function
879        // It cannot also have an intrinsic max track sizing function, so these steps do not apply.
880        if !is_flex {
881            // 5. For intrinsic maximums: Next increase the growth limit of tracks with an intrinsic max track sizing function by
882            // distributing extra space as needed to account for these items' min-content contributions.
883            let has_intrinsic_max_track_sizing_function =
884                move |track: &GridTrack| !track.max_track_sizing_function.has_definite_value(axis_inner_node_size);
885            for item in batch.iter_mut() {
886                let axis_min_content_size = item_sizer.min_content_contribution(item);
887                let space = axis_min_content_size;
888                let tracks = &mut axis_tracks[item.track_range_excluding_lines(axis)];
889                if space > 0.0 {
890                    distribute_item_space_to_growth_limit(
891                        space,
892                        tracks,
893                        has_intrinsic_max_track_sizing_function,
894                        inner_node_size.get(axis),
895                    );
896                }
897            }
898            // Mark any tracks whose growth limit changed from infinite to finite in this step as infinitely growable for the next step.
899            flush_planned_growth_limit_increases(axis_tracks, true);
900
901            // 6. For max-content maximums: Lastly continue to increase the growth limit of tracks with a max track sizing function of max-content
902            // by distributing extra space as needed to account for these items' max-content contributions. However, limit the growth of any
903            // fit-content() tracks by their fit-content() argument.
904            let has_max_content_max_track_sizing_function = |track: &GridTrack| {
905                track.max_track_sizing_function.is_max_content_alike()
906                    || (track.max_track_sizing_function.uses_percentage() && axis_inner_node_size.is_none())
907            };
908            for item in batch.iter_mut() {
909                let axis_max_content_size = item_sizer.max_content_contribution(item);
910                let space = axis_max_content_size;
911                let tracks = &mut axis_tracks[item.track_range_excluding_lines(axis)];
912                if space > 0.0 {
913                    distribute_item_space_to_growth_limit(
914                        space,
915                        tracks,
916                        has_max_content_max_track_sizing_function,
917                        inner_node_size.get(axis),
918                    );
919                }
920            }
921            // Mark any tracks whose growth limit changed from infinite to finite in this step as infinitely growable for the next step.
922            flush_planned_growth_limit_increases(axis_tracks, false);
923        }
924    }
925
926    // Step 5. If any track still has an infinite growth limit (because, for example, it had no items placed
927    // in it or it is a flexible track), set its growth limit to its base size.
928    // NOTE: this step is super-important to ensure that the "Maximise Tracks" step doesn't affect flexible tracks
929    axis_tracks
930        .iter_mut()
931        .filter(|track| track.growth_limit == f32::INFINITY)
932        .for_each(|track| track.growth_limit = track.base_size);
933}
934
935/// 11.5.1. Distributing Extra Space Across Spanned Tracks
936/// https://www.w3.org/TR/css-grid-1/#extra-space
937#[inline(always)]
938fn distribute_item_space_to_base_size(
939    is_flex: bool,
940    use_flex_factor_for_distribution: bool,
941    space: f32,
942    tracks: &mut [GridTrack],
943    track_is_affected: impl Fn(&GridTrack) -> bool,
944    track_limit: impl Fn(&GridTrack) -> f32,
945    intrinsic_contribution_type: IntrinsicContributionType,
946) {
947    if is_flex {
948        let filter = |track: &GridTrack| track.is_flexible() && track_is_affected(track);
949        if use_flex_factor_for_distribution {
950            distribute_item_space_to_base_size_inner(
951                space,
952                tracks,
953                filter,
954                |track| track.flex_factor(),
955                track_limit,
956                intrinsic_contribution_type,
957            )
958        } else {
959            distribute_item_space_to_base_size_inner(
960                space,
961                tracks,
962                filter,
963                |_| 1.0,
964                track_limit,
965                intrinsic_contribution_type,
966            )
967        }
968    } else {
969        distribute_item_space_to_base_size_inner(
970            space,
971            tracks,
972            track_is_affected,
973            |_| 1.0,
974            track_limit,
975            intrinsic_contribution_type,
976        )
977    }
978
979    /// Inner function that doesn't account for differences due to distributing to flex items
980    /// This difference is handled by the closure passed in above
981    fn distribute_item_space_to_base_size_inner(
982        space: f32,
983        tracks: &mut [GridTrack],
984        track_is_affected: impl Fn(&GridTrack) -> bool,
985        track_distribution_proportion: impl Fn(&GridTrack) -> f32,
986        track_limit: impl Fn(&GridTrack) -> f32,
987        intrinsic_contribution_type: IntrinsicContributionType,
988    ) {
989        // Skip this distribution if there is either
990        //   - no space to distribute
991        //   - no affected tracks to distribute space to
992        if space == 0.0 || !tracks.iter().any(&track_is_affected) {
993            return;
994        }
995
996        // Define get_base_size function. This is passed to the distribute_space_up_to_limits helper function
997        // to indicate that it is the base size that is being distributed to.
998        let get_base_size = |track: &GridTrack| track.base_size;
999
1000        // 1. Find the space to distribute
1001        let track_sizes: f32 = tracks.iter().map(|track| track.base_size).sum();
1002        let extra_space: f32 = f32_max(0.0, space - track_sizes);
1003
1004        // 2. Distribute space up to limits:
1005        // Note: there are two exit conditions to this loop:
1006        //   - We run out of space to distribute (extra_space falls below THRESHOLD)
1007        //   - We run out of growable tracks to distribute to
1008
1009        /// Define a small constant to avoid infinite loops due to rounding errors. Rather than stopping distributing
1010        /// extra space when it gets to exactly zero, we will stop when it falls below this amount
1011        const THRESHOLD: f32 = 0.000001;
1012
1013        let extra_space = distribute_space_up_to_limits(
1014            extra_space,
1015            tracks,
1016            &track_is_affected,
1017            &track_distribution_proportion,
1018            get_base_size,
1019            &track_limit,
1020        );
1021
1022        // 3. Distribute remaining span beyond limits (if any)
1023        if extra_space > THRESHOLD {
1024            // When accommodating minimum contributions or accommodating min-content contributions:
1025            //   - any affected track that happens to also have an intrinsic max track sizing function;
1026            // When accommodating max-content contributions:
1027            //   - any affected track that happens to also have a max-content max track sizing function
1028            let mut filter = match intrinsic_contribution_type {
1029                IntrinsicContributionType::Minimum => {
1030                    (|track: &GridTrack| track.max_track_sizing_function.is_intrinsic()) as fn(&GridTrack) -> bool
1031                }
1032                IntrinsicContributionType::Maximum => {
1033                    (|track: &GridTrack| {
1034                        track.min_track_sizing_function.is_max_content()
1035                            || track.max_track_sizing_function.is_max_or_fit_content()
1036                    }) as fn(&GridTrack) -> bool
1037                }
1038            };
1039
1040            // If there are no such tracks (matching filter above), then use all affected tracks.
1041            let number_of_tracks =
1042                tracks.iter().filter(|track| track_is_affected(track)).filter(|track| filter(track)).count();
1043            if number_of_tracks == 0 {
1044                filter = (|_| true) as fn(&GridTrack) -> bool;
1045            }
1046
1047            distribute_space_up_to_limits(
1048                extra_space,
1049                tracks,
1050                filter,
1051                &track_distribution_proportion,
1052                get_base_size,
1053                &track_limit, // Should apply only fit-content limit here?
1054            );
1055        }
1056
1057        // 4. For each affected track, if the track’s item-incurred increase is larger than the track’s planned increase
1058        // set the track’s planned increase to that value.
1059        for track in tracks.iter_mut() {
1060            if track.item_incurred_increase > track.base_size_planned_increase {
1061                track.base_size_planned_increase = track.item_incurred_increase;
1062            }
1063
1064            // Reset the item_incurresed increase ready for the next space distribution
1065            track.item_incurred_increase = 0.0;
1066        }
1067    }
1068}
1069
1070/// 11.5.1. Distributing Extra Space Across Spanned Tracks
1071/// This is simplified (and faster) version of the algorithm for growth limits
1072/// https://www.w3.org/TR/css-grid-1/#extra-space
1073fn distribute_item_space_to_growth_limit(
1074    space: f32,
1075    tracks: &mut [GridTrack],
1076    track_is_affected: impl Fn(&GridTrack) -> bool,
1077    axis_inner_node_size: Option<f32>,
1078) {
1079    // Skip this distribution if there is either
1080    //   - no space to distribute
1081    //   - no affected tracks to distribute space to
1082    if space == 0.0 || tracks.iter().filter(|track| track_is_affected(track)).count() == 0 {
1083        return;
1084    }
1085
1086    // 1. Find the space to distribute
1087    let track_sizes: f32 = tracks
1088        .iter()
1089        .map(|track| if track.growth_limit == f32::INFINITY { track.base_size } else { track.growth_limit })
1090        .sum();
1091    let extra_space: f32 = f32_max(0.0, space - track_sizes);
1092
1093    // 2. Distribute space up to limits:
1094    // For growth limits the limit is either Infinity, or the growth limit itself. Which means that:
1095    //   - If there are any tracks with infinite limits then all space will be distributed to those track(s).
1096    //   - Otherwise no space will be distributed as part of this step
1097    let number_of_growable_tracks = tracks
1098        .iter()
1099        .filter(|track| track_is_affected(track))
1100        .filter(|track| {
1101            track.infinitely_growable || track.fit_content_limited_growth_limit(axis_inner_node_size) == f32::INFINITY
1102        })
1103        .count();
1104    if number_of_growable_tracks > 0 {
1105        let item_incurred_increase = extra_space / number_of_growable_tracks as f32;
1106        for track in tracks.iter_mut().filter(|track| track_is_affected(track)).filter(|track| {
1107            track.infinitely_growable || track.fit_content_limited_growth_limit(axis_inner_node_size) == f32::INFINITY
1108        }) {
1109            track.item_incurred_increase = item_incurred_increase;
1110        }
1111    } else {
1112        // 3. Distribute space beyond limits
1113        // If space remains after all tracks are frozen, unfreeze and continue to distribute space to the item-incurred increase
1114        // ...when handling any intrinsic growth limit: all affected tracks.
1115        distribute_space_up_to_limits(
1116            extra_space,
1117            tracks,
1118            track_is_affected,
1119            |_| 1.0,
1120            |track| if track.growth_limit == f32::INFINITY { track.base_size } else { track.growth_limit },
1121            move |track| track.fit_content_limit(axis_inner_node_size),
1122        );
1123    };
1124
1125    // 4. For each affected track, if the track’s item-incurred increase is larger than the track’s planned increase
1126    // set the track’s planned increase to that value.
1127    for track in tracks.iter_mut() {
1128        if track.item_incurred_increase > track.growth_limit_planned_increase {
1129            track.growth_limit_planned_increase = track.item_incurred_increase;
1130        }
1131
1132        // Reset the item_incurresed increase ready for the next space distribution
1133        track.item_incurred_increase = 0.0;
1134    }
1135}
1136
1137/// 11.6 Maximise Tracks
1138/// Distributes free space (if any) to tracks with FINITE growth limits, up to their limits.
1139#[inline(always)]
1140fn maximise_tracks(
1141    axis_tracks: &mut [GridTrack],
1142    axis_inner_node_size: Option<f32>,
1143    axis_available_grid_space: AvailableSpace,
1144) {
1145    let used_space: f32 = axis_tracks.iter().map(|track| track.base_size).sum();
1146    let free_space = axis_available_grid_space.compute_free_space(used_space);
1147    if free_space == f32::INFINITY {
1148        axis_tracks.iter_mut().for_each(|track| track.base_size = track.growth_limit);
1149    } else if free_space > 0.0 {
1150        distribute_space_up_to_limits(
1151            free_space,
1152            axis_tracks,
1153            |_| true,
1154            |_| 1.0,
1155            |track| track.base_size,
1156            move |track: &GridTrack| track.fit_content_limited_growth_limit(axis_inner_node_size),
1157        );
1158        for track in axis_tracks.iter_mut() {
1159            track.base_size += track.item_incurred_increase;
1160            track.item_incurred_increase = 0.0;
1161        }
1162    }
1163}
1164
1165/// 11.7. Expand Flexible Tracks
1166/// This step sizes flexible tracks using the largest value it can assign to an fr without exceeding the available space.
1167#[allow(clippy::too_many_arguments)]
1168#[inline(always)]
1169fn expand_flexible_tracks(
1170    tree: &mut impl LayoutPartialTree,
1171    axis: AbstractAxis,
1172    axis_tracks: &mut [GridTrack],
1173    items: &mut [GridItem],
1174    axis_min_size: Option<f32>,
1175    axis_max_size: Option<f32>,
1176    axis_available_space_for_expansion: AvailableSpace,
1177    inner_node_size: Size<Option<f32>>,
1178) {
1179    // First, find the grid’s used flex fraction:
1180    let flex_fraction = match axis_available_space_for_expansion {
1181        // If the free space is zero:
1182        //    The used flex fraction is zero.
1183        // Otherwise, if the free space is a definite length:
1184        //   The used flex fraction is the result of finding the size of an fr using all of the grid tracks and
1185        //   a space to fill of the available grid space.
1186        AvailableSpace::Definite(available_space) => {
1187            let used_space: f32 = axis_tracks.iter().map(|track| track.base_size).sum();
1188            let free_space = available_space - used_space;
1189            if free_space <= 0.0 {
1190                0.0
1191            } else {
1192                find_size_of_fr(axis_tracks, available_space)
1193            }
1194        }
1195        // If ... sizing the grid container under a min-content constraint the used flex fraction is zero.
1196        AvailableSpace::MinContent => 0.0,
1197        // Otherwise, if the free space is an indefinite length:
1198        AvailableSpace::MaxContent => {
1199            // The used flex fraction is the maximum of:
1200            let flex_fraction = f32_max(
1201                // For each flexible track, if the flexible track’s flex factor is greater than one,
1202                // the result of dividing the track’s base size by its flex factor; otherwise, the track’s base size.
1203                axis_tracks
1204                    .iter()
1205                    .filter(|track| track.max_track_sizing_function.is_fr())
1206                    .map(|track| {
1207                        let flex_factor = track.flex_factor();
1208                        if flex_factor > 1.0 {
1209                            track.base_size / flex_factor
1210                        } else {
1211                            track.base_size
1212                        }
1213                    })
1214                    .max_by(|a, b| a.total_cmp(b))
1215                    .unwrap_or(0.0),
1216                // For each grid item that crosses a flexible track, the result of finding the size of an fr using all the grid tracks
1217                // that the item crosses and a space to fill of the item’s max-content contribution.
1218                items
1219                    .iter_mut()
1220                    .filter(|item| item.crosses_flexible_track(axis))
1221                    .map(|item| {
1222                        let tracks = &axis_tracks[item.track_range_excluding_lines(axis)];
1223                        // TODO: plumb estimate of other axis size (known_dimensions) in here rather than just passing Size::NONE?
1224                        let max_content_contribution =
1225                            item.max_content_contribution_cached(axis, tree, Size::NONE, inner_node_size);
1226                        find_size_of_fr(tracks, max_content_contribution)
1227                    })
1228                    .max_by(|a, b| a.total_cmp(b))
1229                    .unwrap_or(0.0),
1230            );
1231
1232            // If using this flex fraction would cause the grid to be smaller than the grid container’s min-width/height (or larger than the
1233            // grid container’s max-width/height), then redo this step, treating the free space as definite and the available grid space as equal
1234            // to the grid container’s inner size when it’s sized to its min-width/height (max-width/height).
1235            // (Note: min_size takes precedence over max_size)
1236            let hypothetical_grid_size: f32 = axis_tracks
1237                .iter()
1238                .map(|track| {
1239                    if track.max_track_sizing_function.is_fr() {
1240                        let track_flex_factor = track.max_track_sizing_function.0.value();
1241                        f32_max(track.base_size, track_flex_factor * flex_fraction)
1242                    } else {
1243                        track.base_size
1244                    }
1245                })
1246                .sum();
1247            let axis_min_size = axis_min_size.unwrap_or(0.0);
1248            let axis_max_size = axis_max_size.unwrap_or(f32::INFINITY);
1249            if hypothetical_grid_size < axis_min_size {
1250                find_size_of_fr(axis_tracks, axis_min_size)
1251            } else if hypothetical_grid_size > axis_max_size {
1252                find_size_of_fr(axis_tracks, axis_max_size)
1253            } else {
1254                flex_fraction
1255            }
1256        }
1257    };
1258
1259    // For each flexible track, if the product of the used flex fraction and the track’s flex factor is greater
1260    // than the track’s base size, set its base size to that product.
1261    for track in axis_tracks.iter_mut().filter(|track| track.max_track_sizing_function.is_fr()) {
1262        let track_flex_factor = track.max_track_sizing_function.0.value();
1263        track.base_size = f32_max(track.base_size, track_flex_factor * flex_fraction);
1264    }
1265}
1266
1267/// 11.7.1. Find the Size of an fr
1268/// This algorithm finds the largest size that an fr unit can be without exceeding the target size.
1269/// It must be called with a set of grid tracks and some quantity of space to fill.
1270#[inline(always)]
1271fn find_size_of_fr(tracks: &[GridTrack], space_to_fill: f32) -> f32 {
1272    // Handle the trivial case where there is no space to fill
1273    // Do not remove as otherwise the loop below will loop infinitely
1274    if space_to_fill == 0.0 {
1275        return 0.0;
1276    }
1277
1278    // If the product of the hypothetical fr size (computed below) and any flexible track’s flex factor
1279    // is less than the track’s base size, then we must restart this algorithm treating all such tracks as inflexible.
1280    // We therefore wrap the entire algorithm in a loop, with an hypothetical_fr_size of INFINITY such that the above
1281    // condition can never be true for the first iteration.
1282    let mut hypothetical_fr_size = f32::INFINITY;
1283    let mut previous_iter_hypothetical_fr_size;
1284    loop {
1285        // Let leftover space be the space to fill minus the base sizes of the non-flexible grid tracks.
1286        // Let flex factor sum be the sum of the flex factors of the flexible tracks. If this value is less than 1, set it to 1 instead.
1287        // We compute both of these in a single loop to avoid iterating over the data twice
1288        let mut used_space = 0.0;
1289        let mut naive_flex_factor_sum = 0.0;
1290        for track in tracks.iter() {
1291            // Tracks for which flex_factor * hypothetical_fr_size < track.base_size are treated as inflexible
1292            if track.max_track_sizing_function.is_fr()
1293                && track.max_track_sizing_function.0.value() * hypothetical_fr_size >= track.base_size
1294            {
1295                naive_flex_factor_sum += track.max_track_sizing_function.0.value();
1296            } else {
1297                used_space += track.base_size;
1298            };
1299        }
1300        let leftover_space = space_to_fill - used_space;
1301        let flex_factor = f32_max(naive_flex_factor_sum, 1.0);
1302
1303        // Let the hypothetical fr size be the leftover space divided by the flex factor sum.
1304        previous_iter_hypothetical_fr_size = hypothetical_fr_size;
1305        hypothetical_fr_size = leftover_space / flex_factor;
1306
1307        // If the product of the hypothetical fr size and a flexible track’s flex factor is less than the track’s base size,
1308        // restart this algorithm treating all such tracks as inflexible.
1309        // We keep track of the hypothetical_fr_size
1310        let hypothetical_fr_size_is_valid = tracks.iter().all(|track| {
1311            if track.max_track_sizing_function.is_fr() {
1312                let flex_factor = track.max_track_sizing_function.0.value();
1313                flex_factor * hypothetical_fr_size >= track.base_size
1314                    || flex_factor * previous_iter_hypothetical_fr_size < track.base_size
1315            } else {
1316                true
1317            }
1318        });
1319        if hypothetical_fr_size_is_valid {
1320            break;
1321        }
1322    }
1323
1324    // Return the hypothetical fr size.
1325    hypothetical_fr_size
1326}
1327
1328/// 11.8. Stretch auto Tracks
1329/// This step expands tracks that have an auto max track sizing function by dividing any remaining positive, definite free space equally amongst them.
1330#[inline(always)]
1331fn stretch_auto_tracks(
1332    axis_tracks: &mut [GridTrack],
1333    axis_min_size: Option<f32>,
1334    axis_available_space_for_expansion: AvailableSpace,
1335) {
1336    let num_auto_tracks = axis_tracks.iter().filter(|track| track.max_track_sizing_function.is_auto()).count();
1337    if num_auto_tracks > 0 {
1338        let used_space: f32 = axis_tracks.iter().map(|track| track.base_size).sum();
1339
1340        // If the free space is indefinite, but the grid container has a definite min-width/height
1341        // use that size to calculate the free space for this step instead.
1342        let free_space = if axis_available_space_for_expansion.is_definite() {
1343            axis_available_space_for_expansion.compute_free_space(used_space)
1344        } else {
1345            match axis_min_size {
1346                Some(size) => size - used_space,
1347                None => 0.0,
1348            }
1349        };
1350        if free_space > 0.0 {
1351            let extra_space_per_auto_track = free_space / num_auto_tracks as f32;
1352            axis_tracks
1353                .iter_mut()
1354                .filter(|track| track.max_track_sizing_function.is_auto())
1355                .for_each(|track| track.base_size += extra_space_per_auto_track);
1356        }
1357    }
1358}
1359
1360/// Helper function for distributing space to tracks evenly
1361/// Used by both distribute_item_space_to_base_size and maximise_tracks steps
1362#[inline(always)]
1363fn distribute_space_up_to_limits(
1364    space_to_distribute: f32,
1365    tracks: &mut [GridTrack],
1366    track_is_affected: impl Fn(&GridTrack) -> bool,
1367    track_distribution_proportion: impl Fn(&GridTrack) -> f32,
1368    track_affected_property: impl Fn(&GridTrack) -> f32,
1369    track_limit: impl Fn(&GridTrack) -> f32,
1370) -> f32 {
1371    /// Define a small constant to avoid infinite loops due to rounding errors. Rather than stopping distributing
1372    /// extra space when it gets to exactly zero, we will stop when it falls below this amount
1373    const THRESHOLD: f32 = 0.01;
1374
1375    let mut space_to_distribute = space_to_distribute;
1376    while space_to_distribute > THRESHOLD {
1377        let track_distribution_proportion_sum: f32 = tracks
1378            .iter()
1379            .filter(|track| track_affected_property(track) + track.item_incurred_increase < track_limit(track))
1380            .filter(|track| track_is_affected(track))
1381            .map(&track_distribution_proportion)
1382            .sum();
1383
1384        if track_distribution_proportion_sum == 0.0 {
1385            break;
1386        }
1387
1388        // Compute item-incurred increase for this iteration
1389        let min_increase_limit = tracks
1390            .iter()
1391            .filter(|track| track_affected_property(track) + track.item_incurred_increase < track_limit(track))
1392            .filter(|track| track_is_affected(track))
1393            .map(|track| (track_limit(track) - track_affected_property(track)) / track_distribution_proportion(track))
1394            .min_by(|a, b| a.total_cmp(b))
1395            .unwrap(); // We will never pass an empty track list to this function
1396        let iteration_item_incurred_increase =
1397            f32_min(min_increase_limit, space_to_distribute / track_distribution_proportion_sum);
1398
1399        for track in tracks.iter_mut().filter(|track| track_is_affected(track)) {
1400            let increase = iteration_item_incurred_increase * track_distribution_proportion(track);
1401            if increase > 0.0 && track_affected_property(track) + increase <= track_limit(track) + THRESHOLD {
1402                track.item_incurred_increase += increase;
1403                space_to_distribute -= increase;
1404            }
1405        }
1406    }
1407
1408    space_to_distribute
1409}