Documentation
¶
Overview ¶
Package layout is a flexbox-style layout engine for Flatte.
Every visual thing is a Node — an interface with two methods:
type Node interface {
Size() (w, h Size) // constraints for the parent's distribution
Render(r Rect) string // output at the solved rect
}
Concrete types embed NodeBase for shared geometry (W, H, Pad, Gap, Border) and add their own fields. Row distributes horizontally, Col vertically. Text carries a string. Widgets embed NodeBase and implement both methods.
There is no separate solver pass — containers distribute in their own Render via a shared distributeMain helper. Measurement happens naturally through Size() calls (each type computes its intrinsic size, querying children as needed).
Index ¶
- func Overlay(base, layer string) string
- func OverlayOrigin(base, layer string) (x, y int)
- func Solve(root Node, width, height int) map[string]Rect
- func SolveAndCompose(root Node, width, height int) (string, map[string]Rect)
- type Col
- type Node
- type NodeBase
- type Rect
- type Row
- type Size
- type SizeKind
- type Spacer
- type Text
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func OverlayOrigin ¶
OverlayOrigin is where Overlay places layer's top-left cell. Overlay calls this, so the two cannot drift apart.
func Solve ¶
Solve returns the id→rect geometry for a tree without painting. It is the geometry-only mode of the shared walk (buf nil), so its rects are identical to the ones SolveAndCompose records while composing.
func SolveAndCompose ¶
SolveAndCompose walks the tree exactly once. It distributes rects top-down (the same math as Solve) and, in the same pass, paints each leaf's Render(rect) into a cell buffer at its absolute position, recording the rect of every node that carries an ID. It returns the composed frame string and the id→rect geometry map.
This is the single-pass alternative to calling Solve and Render separately: geometry and pixels come from one traversal and one distribution computation, so they cannot drift. Because leaves are painted at their solved coordinates and clipped to the buffer, a leaf that overflows its rect cannot corrupt the frame, and a leaf that under-fills simply reveals whatever a parent painted underneath — there is no "every leaf must fill its rect exactly" invariant.
Types ¶
type Col ¶
Col lays children out vertically (main axis = height).
func (Col) GetChildren ¶
func (Col) IsHorizontal ¶
type Node ¶
Node is the primitive layout contract. Implementations should keep the two methods separate:
Size declares layout intent for each axis. It may return caller-authored constraints from NodeBase (Auto, Fixed, Grow), or measured natural content as SizeContent. It must not depend on a final Rect, because the parent calls Size before it knows the child's allocation.
Render draws the node inside the already-solved Rect. It should fill, clip, wrap, border, or join content to fit that rect, but it should not choose its own position or size in the parent.
In other words: Size answers "what space do I need or want?", while Render answers "given this exact space, what string do I produce?".
Note on cross-axis geometry: a child whose cross-axis Size is not Fixed stretches to fill the parent's cross axis (SizeContent included), so the rect recorded for a one-line ID'd Text in a Row spans the row's full height. That is correct for painting and coarse hit-testing; pin the cross axis with Fixed when a hit target must be exactly content-sized.
type NodeBase ¶
type NodeBase struct {
W, H Size
Pad int
Gap int
Bordered bool
Overlay bool
ID string
// PadTop/PadRight/PadBottom/PadLeft add per-side padding on top of the
// symmetric Pad (and inside the border, when Bordered). Extracted after
// two panes needed asymmetric insets: horizontal-only padding and
// "no top pad, one-row bottom pad" cannot be said with Pad alone.
PadTop, PadRight, PadBottom, PadLeft int
// Chrome, when set on a container, paints the node's own decoration
// (styled border, background, title) instead of the default Pad/Bordered
// fill. It receives the node's full solved rect and is painted before
// (under) the children. It does not change the layout inset: Pad and
// Bordered still declare the space the chrome occupies, so children are
// placed inside that inset regardless of what Chrome draws. Leaves ignore
// it — a leaf's Render owns all of its pixels already.
Chrome func(r Rect) string
}
NodeBase provides shared geometry for all concrete node types. Embed it and override Size/Render as needed.
type Rect ¶
type Rect struct{ X, Y, W, H int }
func (Rect) Intersect ¶
Intersect returns the overlapping region of r and s. If they do not overlap, it returns a zero-area rect anchored at the clamped corner. It is used to clamp a child's solved rect to its parent's inner box, so distribution overflow (e.g. two Fixed(8) children in a 10-wide row) can never yield a rect that escapes the parent or the frame — hit-testing trusts these rects.
type Row ¶
Row lays children out horizontally (main axis = width). Embed NodeBase for geometry. Children participate in distribution via their Size() constraints.
func (Row) GetChildren ¶
Introspection for Solve and overlay pass.
func (Row) IsHorizontal ¶
func (Row) Render ¶
Render composes the row into a self-contained block sized to rect. It delegates to the single walk via SolveAndCompose (using rect's dimensions at a local origin) so container distribution, chrome, clipping, and overlay compositing all follow one code path. Only the childless case takes the direct fillRect path — which also prevents the walk (whose leaf branch would otherwise Render a container) from recursing.
type Size ¶
Size is a one-axis layout constraint. Callers normally set Auto, Fixed, or Grow through the constructors below. SizeContent is produced by Size methods after measuring natural content, preserving the distinction between "caller forced this size" (Fixed) and "this is the content's natural size" (Content).
Weight is normally the proportional share a Grow child takes of a container's leftover main-axis space. For overlays it has a second, intentional meaning (see resolveLayerSize): a Grow overlay with Weight>=1 fills the whole viewport, while Weight<1 sizes to that fraction of the viewport. This dual use lets a caller express both "modal that is 60% of the screen" (Grow(0.6)) and "full-screen layer" (Grow(1)) without a separate field.
type SizeKind ¶
type SizeKind int
const ( // SizeAuto is the default "no explicit claim" input. If a node can measure // natural content on an auto axis, its Size method should return // SizeContent. If it cannot, auto contributes zero on the parent's main // axis and stretches on the cross axis. SizeAuto SizeKind = iota // SizeFixed is an exact caller-specified cell count. SizeFixed // SizeGrow takes a weighted share of the parent's remaining main-axis // space after fixed/content children are allocated. SizeGrow // SizeContent is a measured natural size, not usually a caller-authored // mode. Text and containers return it when an auto axis can be measured // from content or children. It behaves like fixed on the parent's main // axis, while still stretching on the cross axis. SizeContent )
type Spacer ¶
type Spacer struct {
NodeBase
// contains filtered or unexported fields
}
Spacer fills remaining space. Grows on both axes.