layout

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2026 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package layout is Nimbus Native's flexbox engine, in pure Go so the same layout runs on iOS, Android and (later) the web.

It follows the CSS Flexbox algorithm with Nimbus Native's defaults: column direction, flex-shrink 0, no automatic minimum size, every node a flex container. Results are in points (iOS) / dp (Android); hosts round to pixels. conformance_test.go checks it against Chromium's own layout.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Align

type Align uint8

Align positions children on the cross axis. On AlignSelf, AlignAuto means "use the parent's AlignItems"; on AlignItems it means Stretch.

const (
	AlignAuto Align = iota
	AlignStretch
	AlignStart
	AlignCenter
	AlignEnd
)

type Direction

type Direction uint8
const (
	Column Direction = iota
	Row
)

type Edges

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

Edges are per-side lengths in points.

func All

func All(v float32) Edges

All is the same length on every side.

type Justify

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

type MeasureFunc

type MeasureFunc func(width float32, wMode Mode, height float32, hMode Mode) (w, h float32)

MeasureFunc sizes a leaf (text, an image, a native control) within the space offered. A width or height whose mode is Undefined is not a limit.

type Mode

type Mode uint8

Mode says what a size offered to a node means.

const (
	Undefined Mode = iota // no limit: size to content
	Exactly               // the node must be this size
	AtMost                // size to content, but no larger than this
)

type Node

type Node struct {
	Style    Style
	Measure  MeasureFunc
	Children []*Node

	// Results. X and Y are the border box's offset from the parent's border
	// box; W and H its size. ContentW and ContentH are the extent of the
	// children (padding included) — a scroll view's content size.
	X, Y, W, H         float32
	ContentW, ContentH float32
	// contains filtered or unexported fields
}

Node is one box in the layout tree. Set Style, Children and, for leaves, Measure; call Calculate on the root; read the results.

func (*Node) Calculate

func (n *Node) Calculate(width, height float32)

Calculate lays out the tree rooted at n within width × height (the viewport). An undefined dimension (NaN) sizes the root to its content.

func (*Node) MarkDirty

func (n *Node) MarkDirty()

MarkDirty tells the engine n changed (style, children, or what its Measure returns) so the next Calculate lays out n and its ancestors again. New nodes start dirty.

type Position

type Position uint8
const (
	Relative Position = iota
	Absolute
)

type Style

type Style struct {
	Direction  Direction
	Justify    Justify
	AlignItems Align
	AlignSelf  Align

	Grow   float32
	Shrink float32
	Basis  Value

	Width, Height       Value
	MinWidth, MinHeight Value
	MaxWidth, MaxHeight Value

	Padding Edges
	Margin  Edges
	Border  Edges
	Gap     float32 // between children on the main axis

	Position                 Position
	Left, Top, Right, Bottom Value // insets; Auto means unset

	// Hidden removes the node from layout (display: none).
	Hidden bool
	// Scroll lays children out unconstrained along the main axis, as the
	// content of a scroll view. The node's own size is unaffected.
	Scroll bool
}

Style is everything that affects a node's layout. The zero Style is an auto-sized column that stretches its children.

type Unit

type Unit uint8

Unit says how a Value is measured.

const (
	Auto    Unit = iota // the zero Value: size from content or flex
	Point               // absolute, in points / dp
	Percent             // of the parent's inner size on that axis
)

type Value

type Value struct {
	V float32
	U Unit
}

Value is a length: Auto, points or a percentage.

func Pct

func Pct(v float32) Value

Pct is a percentage of the parent's inner size.

func Pt

func Pt(v float32) Value

Pt is a length in points.

Jump to

Keyboard shortcuts

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