Skip to main content

Crate ui_layout

Crate ui_layout 

Source
Expand description

§ui_layout

Minimal CSS-like layout engine for UI frameworks. Designed for lightweight, fast, and portable applications (e.g. editors and IDEs).

Provides predictable layout behavior for custom GUI frameworks, editors, and experimental rendering engines.

§Getting Started

Basic usage follows a simple flow:

  1. Create a layout tree
  2. Run the layout engine
  3. Access computed layout results
use ui_layout::*;

let mut root = LayoutNode::new(Style::default());

// Compute layout using a viewport size.
LayoutEngine::layout(&mut root, 800.0, 600.0);

§Core Concepts

Layout behavior is configured through Style, which groups commonly used layout properties into focused categories:

  • Display — controls layout mode
  • SizeStyle — width and height constraints
  • Spacing — margin, padding, and borders
  • ItemStyle — item-specific flex behavior
  • justify_content / align_items — child alignment
  • flex_direction, row_gap, column_gap — flex container behavior

This organization keeps layout rules explicit and easy to reason about.

§Observing Layout Results

After layout computation, results are written into each LayoutNode through its layout_box field.

The resulting LayoutBox describes how the node was laid out:

  • LayoutBox::None — no layout result
  • LayoutBox::BlockBox(BoxModel) — standard box layout result
  • LayoutBox::InlineBox(InlineBox) — inline content that may span multiple lines

BoxModel contains absolute rectangles for different regions:

  • border_box — outer box including borders
  • padding_box — inner box including padding
  • content_box — content area
  • children_box — area occupied by child content

Inline layouts additionally provide LineSpan information for observing how a box is split across lines.

§Children

Layout trees contain children represented by LayoutChild:

  • LayoutChild::Node — a normal layout node
  • LayoutChild::Fragment — an inline-level fragment
  • LayoutChild::Object — a custom FlowLayouter object
  • LayoutChild::Custom — a custom [BlockLayouter] object

ItemFragment represents the smallest independently positioned piece of inline content.

Common fragment types:

  • Fragment — inline content with dimensions
  • LineBreak — forces a line break

During layout, fragments are wrapped in FragmentNode and assigned placement information, allowing inline content to be split and positioned across multiple lines.

§Custom Objects

The FlowLayouter trait allows custom types to participate directly in layout as inline-level objects. Objects report their intrinsic size via FlowLayouter::measure and perform inline layout via FlowLayouter::layout.

The [BlockLayouter] trait allows custom types to participate as block-level components. Components return their border-box Rect via [BlockLayouter::layout].

Structs§

BoxModel
Represents the layout box model of an element.
Display
Full representation of the CSS display property, split into outer and inner display types.
EdgeOption
FlowLayoutContext
Context passed to FlowLayouter::layout for inline flow participation.
Fragment
A splittable fragment of inline content.
FragmentNode
A fragment with associated layout placement information.
InlineBox
An inline box that may be split across multiple lines.
ItemStyle
LayoutBoxIntoIter
Iterator over the BoxModels represented by an owned LayoutBox.
LayoutBoxIter
Iterator over the BoxModels represented by a borrowed LayoutBox.
LayoutContext
Layout context passed to [FlowLayouter::measure].
LayoutEngine
LayoutNode
A node in the layout tree.
LineSpan
A span of an inline box on a single line.
MeasureResult
The measured size of a FlowLayouter object.
Placement
Layout result for an inline placement.
Rect
SizeStyle
Spacing
Style

Enums§

AlignItems
Axis
Axis orientation
BoxSizing
FlexDirection
InnerDisplay
Represents the inner display type of a box.
ItemFragment
An item fragment for inline layout.
JustifyContent
LayoutBox
Types of BoxModel.
LayoutChild
A unified layout item used during layout processing.
LayoutItem
Length
LengthOrAuto
OuterDisplay
Represents the outer display type of a box.

Traits§

FlowLayouter
A self-layouting object that participates in layout flows.