layout

package
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 10, 2026 License: MIT Imports: 5 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func Overlay

func Overlay(base, layer string) string

Overlay draws layer centered over base as an ANSI-aware cell composite.

func OverlayOrigin

func OverlayOrigin(base, layer string) (x, y int)

OverlayOrigin is where Overlay places layer's top-left cell. Overlay calls this, so the two cannot drift apart.

func Solve

func Solve(root Node, width, height int) map[string]Rect

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

func SolveAndCompose(root Node, width, height int) (string, map[string]Rect)

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

type Col struct {
	NodeBase
	Children []Node
}

Col lays children out vertically (main axis = height).

func (Col) GetChildren

func (c Col) GetChildren() []Node

func (Col) GetGap

func (c Col) GetGap() int

func (Col) IsHorizontal

func (c Col) IsHorizontal() bool

func (Col) Render

func (c Col) Render(rect Rect) string

Render composes the column into a self-contained block sized to rect, delegating to the single walk via SolveAndCompose. See Row.Render for the rationale, including why the childless case stays on the direct path.

func (Col) Size

func (c Col) Size() (Size, Size)

type Node

type Node interface {
	Size() (w, h Size)
	Render(r Rect) string
}

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.

func (NodeBase) GetID

func (n NodeBase) GetID() string

func (NodeBase) IsOverlay

func (n NodeBase) IsOverlay() bool

func (NodeBase) Size

func (n NodeBase) Size() (Size, Size)

type Rect

type Rect struct{ X, Y, W, H int }

func (Rect) Contains

func (r Rect) Contains(x, y int) bool

func (Rect) Inset

func (r Rect) Inset(n int) Rect

func (Rect) Intersect

func (r Rect) Intersect(s Rect) Rect

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

type Row struct {
	NodeBase
	Children []Node
}

Row lays children out horizontally (main axis = width). Embed NodeBase for geometry. Children participate in distribution via their Size() constraints.

func (Row) GetChildren

func (r Row) GetChildren() []Node

Introspection for Solve and overlay pass.

func (Row) GetGap

func (r Row) GetGap() int

func (Row) IsHorizontal

func (r Row) IsHorizontal() bool

func (Row) Render

func (r Row) Render(rect Rect) string

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.

func (Row) Size

func (r Row) Size() (Size, Size)

type Size

type Size struct {
	Kind   SizeKind
	Value  int
	Weight float64
}

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.

func Auto

func Auto() Size

func Fixed

func Fixed(n int) Size

func Grow

func Grow(w float64) Size

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.

func NewSpacer

func NewSpacer() *Spacer

func (*Spacer) Render

func (s *Spacer) Render(r Rect) string

func (*Spacer) WithBackground

func (s *Spacer) WithBackground(c color.Color) *Spacer

type Text

type Text struct {
	NodeBase
	String string
}

Text is a leaf carrying a pre-rendered string. Auto-sizes to the string's dimensions unless explicit W/H is set on the embedded NodeBase.

func (Text) Render

func (t Text) Render(r Rect) string

func (Text) Size

func (t Text) Size() (Size, Size)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL