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 ¶
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
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 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.
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 Length ¶
Length is a size or offset.
func AutoFill ¶ added in v0.6.0
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.
type Measurer ¶
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 ¶
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
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 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.
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 )