Function layout

Source
pub fn layout(
    parent: Rect,
    items: &[FlowItem],
    options: FlowOptions,
    output: &mut [Rect],
) -> Result<FlowReport, FlowError>
Expand description

Lay out a stable prefix left-to-right, then top-to-bottom, without allocation.

Rows greedily admit consecutive minimum widths and intervening gaps, with equality fitting. Each row shares space toward preferred widths in proportion to (preferred - minimum), independently of grow weights. After all preferred widths fit, remaining space is distributed by positive grow weight. Capped items relinquish their share to uncapped items. Zero weights and capped rows leave unused space at the right. The last row follows the same policy.

No partial-height row or undersized item is emitted. A minimum wider than the parent, exhausted height, or disabled wrapping ends the prefix. Empty parents produce zero rectangles. A short output buffer truncates the computed prefix without changing row widths; its length does not affect layout decisions. All items are validated and all fitting rows computed even with empty output. Only the written prefix changes on success; the output tail is untouched. Empty input succeeds and there is no retained state to reset.

Work is bounded by 256 items and at most one weighted saturation pass per item per row (O(n²) worst case); scratch is O(256) on the stack. Sums and weighted shares use f64 to avoid f32 overflow. Final widths round down, respecting the minimum/maximum; origins round up past the preceding edge plus gap. Space for later minimum widths is reserved backward, rounding inward. Stored extents and half-open f32 endpoints stay inside the parent and never overlap. Rounding may leave small slack or reduce a preferred width; it never relaxes a minimum. Unrepresentable geometry rejects the entire call, including earlier rows.

use ferravex_play::ui::Rect;
use ferravex_play::ui::flow::{layout, FlowItem, FlowOptions, FlowError};
let area = Rect { min: [0.; 2], size: [96., 96.] };
let items = [FlowItem::fixed(44.); 3];
let mut output = [area; 2];
let report = layout(area, &items, FlowOptions::default(), &mut output)?;
assert_eq!((report.written, report.overflow, report.rows), (2, 1, 1));
assert_eq!(output[1].min, [52., 0.]);
let before = output;
assert_eq!(layout(area, &[FlowItem::fixed(f32::NAN)],
                  FlowOptions::default(), &mut output),
           Err(FlowError::InvalidItem { index: 0 }));
assert_eq!(output, before); // Atomic failure.
let empty = layout(area, &[], FlowOptions::default(), &mut output)?;
assert_eq!(empty.written, 0); // Consume no stale output on the next frame.