layout

package
v0.8.3 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2026 License: Apache-2.0 Imports: 2 Imported by: 0 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 small flexbox-style layout engine.

It implements the useful core of the flexbox mental model — rows and columns, gap, padding, margin, fixed/percent/auto sizes with min/max, grow/shrink, align/justify, absolute positioning and scroll content extents — without any CSS parsing or browser compatibility goals.

Sizes are border-box: Width includes padding and border. The engine knows nothing about drawing, text or events; leaves report their content size through the Measurer interface.

Index

Constants

This section is empty.

Variables

View Source
var Auto = Length{}

Auto is the automatic length.

Functions

This section is empty.

Types

type Align

type Align uint8

Align positions items on the cross axis.

const (
	AlignAuto Align = iota // inherit from the container (AlignSelf) / stretch (AlignItems)
	AlignStart
	AlignCenter
	AlignEnd
	AlignStretch
	// AlignBaseline lines up the first baselines of a row's items: the first
	// line of text in each (see Baseliner). An item with no text is aligned at
	// its bottom edge, as CSS synthesizes a baseline. In a column, and in a
	// grid, it is AlignStart.
	AlignBaseline
)

type Baseliner added in v0.6.1

type Baseliner interface {
	Baseline(width float32) (float32, bool)
}

Baseliner is implemented by a leaf with text. Baseline returns the distance from the top of its content box to the baseline of its first line, laid out width wide, and false when it has no text.

type Direction

type Direction uint8

Direction is the main axis of a container.

const (
	Column Direction = iota // children stack top to bottom (the default)
	Row                     // children flow left to right
)

type Edges

type Edges struct{ Top, Right, Bottom, Left float32 }

Edges are per-side insets.

type Engine

type Engine struct {

	// Stats from the last Compute call.
	Measured int // measure calls that did real work
	CacheHit int
	Arranged int // nodes whose children were (re)positioned
	// contains filtered or unexported fields
}

Engine runs layout passes. Reuse one Engine across frames to keep its scratch memory.

func (*Engine) Compute

func (e *Engine) Compute(root *Node, width, height float32)

Compute lays out root to fill exactly width × height.

Only invalidated subtrees, and subtrees whose size changed, are recomputed.

type Grid

type Grid struct {
	Columns, Rows []Length
}

Grid is the tracks of a grid: the size of each column, and of the first rows (the rest fit their content). Sizes are Px, Percent of the grid's content box, Auto (fit the content) or Fr (a share of the space left).

type Justify

type Justify uint8

Justify distributes items on the main axis.

const (
	JustifyStart Justify = iota
	JustifyCenter
	JustifyEnd
	JustifySpaceBetween
	JustifySpaceAround
	JustifySpaceEvenly
)

type Length

type Length struct {
	Value float32
	Unit  Unit
}

Length is a size or offset.

func AutoFill added in v0.6.0

func AutoFill(min float32) Length

AutoFill returns a column track that repeats as many times as fit the grid's width, each column at least min wide and all of them sharing the width equally, as CSS's repeat(auto-fill, minmax(min, 1fr)) does. It must be the grid's only column track. A grid of indefinite width has one column.

func Fr

func Fr(v float32) Length

Fr returns a flexible grid track that takes v shares of the space the other tracks leave.

func Percent

func Percent(v float32) Length

Percent returns a percentage length (100 = full containing block).

func Px

func Px(v float32) Length

Px returns a length in logical pixels.

func (Length) IsAuto

func (l Length) IsAuto() bool

IsAuto reports whether l is automatic.

type Measurer

type Measurer interface {
	Measure(maxWidth float32) (width, height float32)
}

Measurer reports the natural size of a leaf's content (text, images...). maxWidth is the available content width; it may be +Inf. Implementations should wrap to maxWidth where they can and return the size they need.

type MinContenter

type MinContenter interface {
	MinContentWidth() (width float32, wraps bool)
}

MinContenter is implemented by a leaf whose content wraps. MinContentWidth returns the min-content width of the content: the width of its widest unbreakable run, the narrowest it can be without breaking inside a word (CSS min-content size). wraps is false when the content does not wrap at all, such as a NoWrap text, or when the leaf has no words to break. Such a leaf is sized by Measure(0) alone, as a leaf that does not implement MinContenter is, and it is not held to the width of its container.

type Node

type Node struct {
	Style    Style
	Children []*Node
	// Measure, when set, makes the node a leaf whose content size comes from it.
	Measure Measurer

	// Results, written by Compute. X and Y are relative to the parent's
	// border box (before any scrolling); W and H are the border-box size.
	X, Y, W, H float32
	// ContentW and ContentH are the extent of the children including this
	// node's padding — the scrollable size when it exceeds W/H.
	ContentW, ContentH float32

	// Overhang is how far the node's own drawing may extend beyond its box on
	// every side. The caller sets it. GrowOverhang is the extra scale the node
	// is drawn at, 0.05 for 1.05, applied about the box's center.
	Overhang, GrowOverhang float32
	// Ink is the rectangle, relative to the node's own origin, that the node
	// and its subtree may draw into. Floating children are left out, since
	// overlays are drawn from their own list. A child that clips contributes
	// only its own box (plus its overhang), and a clipping node limits its
	// children to its box on the clipped axes. Written by Compute.
	InkX0, InkY0, InkX1, InkY1 float32
	// contains filtered or unexported fields
}

Node is one box in the layout tree.

func (*Node) GridColumns added in v0.6.1

func (n *Node) GridColumns() int

GridColumns returns the number of columns n's grid has at its laid-out width: as many as an AutoFill track made, or the number of its tracks. It is 0 when n is not a grid.

func (*Node) Invalidate

func (n *Node) Invalidate()

Invalidate discards n's cached measurements and arrangement. Call it on a changed node and every ancestor up to the root; new nodes start invalid.

type Overflow

type Overflow uint8

Overflow controls clipping and scrolling.

const (
	OverflowVisible Overflow = iota
	OverflowHidden
	OverflowScroll
)

type Position

type Position uint8

Position selects in-flow or absolute positioning.

const (
	Relative Position = iota
	Absolute
	// Floating is laid out like Absolute but sized against the whole viewport, ignores insets, and is excluded from the parent's content extent; the caller positions it.
	Floating
)

type Style

type Style struct {
	Direction Direction
	Width     Length
	Height    Length
	MinWidth  Length
	MinHeight Length
	MaxWidth  Length
	MaxHeight Length

	Padding Edges
	Margin  Edges
	// Border is the border width of each side; it insets content like padding.
	Border Edges
	Gap    float32

	// Wrap lets items flow onto further lines when the main axis is full;
	// Gap separates lines as well as items.
	Wrap bool

	Grow   float32
	Shrink float32
	Basis  Length

	AlignItems Align
	AlignSelf  Align
	Justify    Justify

	Position                 Position
	Top, Right, Bottom, Left Length

	// Hidden takes the node out of layout: it takes no space, its subtree is
	// not laid out, and it adds nothing to its parent's content extent or ink.
	// Its own W and H are zero while it is hidden.
	Hidden bool

	// FitWidth keeps an item with an automatic width at its content's width in a
	// column, instead of stretching it to the column's width. In a row it has no
	// effect, since its width is along the main axis.
	FitWidth bool
	// ContentMin makes an item's fixed width a ceiling for its automatic minimum
	// instead of a floor: in a shrinking row the item may shrink to its content's
	// min-content width, as CSS's min(specified width, content) allows. The default
	// keeps a fixed width as the floor.
	ContentMin bool
	// SoftWidth marks Width as a default that a grid cell may override: the
	// item fills its cell as an item with an automatic width does. Elsewhere
	// the width applies.
	SoftWidth bool
	// MatchParentWidth makes a Floating child at least as wide as its parent's
	// border box, so a popup can be as wide as the control it opens from. Its
	// content is laid out at that width.
	MatchParentWidth bool

	OverflowX, OverflowY Overflow

	// Grid, when it has columns, lays out the children in a grid instead of a
	// row or column (see grid.go). ColSpan and RowSpan are how many columns and
	// rows a grid item covers; 0 means 1.
	Grid             *Grid
	ColSpan, RowSpan int
}

Style holds everything that affects layout.

func (Style) Equal

func (s Style) Equal(o Style) bool

Equal reports whether s and o lay out the same: as ==, but comparing the grid tracks by value.

type Unit

type Unit uint8

Unit is the unit of a Length.

const (
	// Auto sizes from content (or stretches, depending on alignment).
	UnitAuto Unit = iota
	UnitPx
	// UnitPercent is relative to the containing block's content box.
	UnitPercent
	// UnitFr is a share of a grid's free space (see Fr); elsewhere it is Auto.
	UnitFr
	// UnitAutoFill is a grid's only column track that repeats as often as it
	// fits (see AutoFill); elsewhere it is Auto.
	UnitAutoFill
)

Jump to

Keyboard shortcuts

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