ElixirOpentui.Layout (ElixirOpentui v0.2.0)

Copy Markdown View Source

Pure Elixir "Flexbox Lite" layout engine.

Computes layout for a tree of %ElixirOpentui.Element{} structs, producing a map of %{element_ref => %Rect{x, y, w, h}} results.

Implements the TUI-relevant subset of CSS Flexbox:

  • flex_direction: :row | :column | :row_reverse | :column_reverse

  • flex_grow, flex_shrink, flex_basis
  • flex_wrap: :no_wrap | :wrap | :wrap_reverse

  • justify_content: :flex_start | :flex_end | :center | :space_between | :space_around | :space_evenly

  • align_items, align_self: :flex_start | :flex_end | :center | :stretch

  • width, height (fixed, :auto, {:percent, p})
  • padding, margin, gap
  • position: :relative | :absolute (with top/left/right/bottom)

NOT implemented (not used in TUI): align-content, order, CSS grid.

Three-pass algorithm:

  1. Measure (bottom-up): compute intrinsic sizes
  2. Flex resolve (top-down): distribute space via grow/shrink
  3. Position (top-down): compute absolute coordinates

Summary

Functions

Compute layout for an element tree within the given available space.

Types

layout_result()

@type layout_result() :: %{required(reference()) => ElixirOpentui.Layout.Rect.t()}

Functions

compute(root, available_w, available_h)

Compute layout for an element tree within the given available space.

Returns a map keyed by element reference (using make_ref() assigned during layout) mapping to %Rect{} structs.