Layout

Trait Layout 

Source
pub trait Layout: Debug {
    // Required methods
    fn size_that_fits(
        &self,
        proposal: ProposalSize,
        children: &[&dyn SubView],
    ) -> Size;
    fn place(&self, bounds: Rect, children: &[&dyn SubView]) -> Vec<Rect>;

    // Provided method
    fn stretch_axis(&self) -> StretchAxis { ... }
}
Expand description

A layout algorithm for arranging child views.

Layouts receive a size proposal from their parent, query their children to determine sizes, and then place children within the final bounds.

§Two-Phase Layout

  1. Sizing (size_that_fits): Determine how big this container should be given a proposal
  2. Placement (place): Position children within the final bounds

§Note on Safe Area

Safe area handling is intentionally not part of the Layout trait. Safe area is a platform-specific concept handled by backends. Views can use the IgnoresSafeArea metadata to opt out of safe area insets.

Required Methods§

Source

fn size_that_fits( &self, proposal: ProposalSize, children: &[&dyn SubView], ) -> Size

Calculate the size this layout wants given a proposal.

The layout can query children multiple times with different proposals to determine optimal sizing.

§Arguments
  • proposal - The size proposed by the parent
  • children - References to child proxies for size queries
Source

fn place(&self, bounds: Rect, children: &[&dyn SubView]) -> Vec<Rect>

Place children within the given bounds.

Called after sizing is complete. Returns a rect for each child specifying its position and size within bounds.

§Arguments
  • bounds - The rectangle this layout should fill
  • children - References to child proxies (may query sizes again)

Provided Methods§

Source

fn stretch_axis(&self) -> StretchAxis

Which axis this container stretches to fill available space.

  • VStack: .horizontal (fills available width, intrinsic height)
  • HStack: .vertical (fills available height, intrinsic width)
  • ZStack: .both (fills all available space)
  • Other layouts: .none by default

This allows parent containers to know whether to expand this container to fill available space on the cross axis.

Implementors§