layout

package
v0.13.6 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2026 License: EPL-2.0 Imports: 2 Imported by: 1 module, 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-like auto-layout for editors and design tools: rows and columns with gap, padding, alignment, justification, wrapping, min and max sizes, absolute children, and fixed, hug and fill sizes. It knows nothing of drawing or of any file format: leaves that depend on content (text) report their size through two callbacks. Only scene.go imports gfx (to apply frames to a gfx.Scene); the rest uses the standard library, so it can be copied into another module without that file.

Usage: build a tree of Node, call Layout on the root, read RX, RY, RW, RH of every node (relative to the parent's top left corner), or hand the tree to Apply to move gfx.Scene nodes.

Rules:

  • Dir is Row (children left to right, the default), Column, or Abs (all children at their own X, Y). In a Row or a Column a child with Absolute set leaves the flow: it takes no part in gap, fill shares or hug sizes, and sits at X, Y from the parent's top left corner (padding ignored).
  • A size is Fixed (a number), Hug (the content plus padding) or Fill (a share of the free main axis space of the parent, the whole cross axis of the parent). Fill children of one parent share the free space equally. An absolute child fills the parent's whole box.
  • MinW, MaxW, MinH, MaxH clamp the resolved size of any kind; zero Max means no limit, and Min wins over Max. A Fill child clamped by Max does not give the rest of its share to its siblings.
  • Widths are decided top down, from the intrinsic widths of the content; heights bottom up, from the widths. A Hug height, or a Fill height under a parent of unknown height, is the content's.
  • Gap goes between children. Padding is top, right, bottom, left.
  • Justify (main axis) is Start, Center, End or SpaceBetween; Align (cross axis) is Start, Center or End.
  • Wrap breaks the children of a Row or a Column into lines when they do not fit the main axis; LineGap goes between lines, Gap between children of a line, Justify and Align work inside a line, and lines start at the cross start. A Row wraps at its width, so give it a Fixed or Fill width, or a MaxW; a Hug width is one line. A Column wraps at its height, which must be known: Fixed, or Fill under a parent of known height. A Fill child of a wrapping node starts from its content size and takes an equal share of its line's free space, or gives back an equal share of the overflow.
  • Leaves (no children) with LeafW and LeafH set take their size from them: LeafW is the natural width, LeafH(w) the height at width w (text wrapping). A Hug text is as wide as LeafW, a Fill text, or a Hug one with MaxW, is wrapped by LeafH at the width it gets.

Not supported: per-child align overrides, percentages, Fill sizes with a fallback value, content alignment of wrapped lines.

Example

A toolbar of chips that wraps, with a badge pinned to its corner.

package main

import (
	"fmt"

	"github.com/haiodo/gowt/gfx/layout"
)

func main() {
	chip := func(w float64) *layout.Node {
		return &layout.Node{W: layout.Fixed(w), H: layout.Fixed(20)}
	}
	badge := &layout.Node{
		Absolute: true, X: 90, Y: -4,
		W: layout.Fixed(8), H: layout.Fixed(8),
	}
	bar := &layout.Node{
		Dir: layout.Row, Wrap: true, Gap: 6, LineGap: 4, Pad: [4]float64{4, 4, 4, 4},
		W:    layout.Fixed(100),
		Kids: []*layout.Node{chip(40), chip(40), chip(30), badge},
	}
	bar.Layout()
	for i, k := range bar.Kids {
		fmt.Printf("%d: x=%v y=%v w=%v h=%v\n", i, k.RX, k.RY, k.RW, k.RH)
	}
	fmt.Printf("bar: %v x %v\n", bar.RW, bar.RH)
}
Output:
0: x=4 y=4 w=40 h=20
1: x=50 y=4 w=40 h=20
2: x=4 y=28 w=30 h=20
3: x=90 y=-4 w=8 h=8
bar: 100 x 52

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// Hug is the size of the content plus padding.
	Hug = Size{}
	// Fill is an equal share of the free space of the parent's main axis, the whole cross axis.
	Fill = Size{Kind: KindFill}
)

Functions

func Apply

func Apply(tx *gfx.Tx, root *Node, target func(*Node) *gfx.Node)

Apply moves the scene nodes of a laid out tree to their resolved positions inside tx, so a frame shows the whole layout or none of it. target maps a layout node to the scene node it stands for, or nil for a node that has none; a nil target reads Tag as a *gfx.Node. A layout node without a scene node passes its offset on, so a scene node is placed relative to its nearest ancestor that has one, which is how the scene's own transforms work. Sizes (RW, RH) are not part of a scene node: read them from the layout tree for clips and pictures.

Example

Apply moves scene nodes to the resolved positions in one transaction. The layout tree here mirrors the app's tree; Tag links each layout node to its scene node.

package main

import (
	"fmt"

	"github.com/haiodo/gowt/gfx"
	"github.com/haiodo/gowt/gfx/layout"
)

func main() {
	scene := gfx.NewScene()
	left, right := gfx.NewNode(1), gfx.NewNode(2)
	scene.Update(func(tx *gfx.Tx) {
		tx.Add(nil, left)
		tx.Add(nil, right)
	})
	scene.Commit()

	box := func(g *gfx.Node) *layout.Node {
		return &layout.Node{
			Tag: g,
			W:   layout.Fixed(50),
			H:   layout.Fixed(30),
		}
	}
	root := &layout.Node{Gap: 10, Kids: []*layout.Node{box(left), box(right)}}
	root.Layout()

	scene.Update(func(tx *gfx.Tx) {
		layout.Apply(tx, root, func(n *layout.Node) *gfx.Node {
			g, _ := n.Tag.(*gfx.Node)
			return g
		})
	})
	scene.Commit()
	t := right.Transform()
	fmt.Println(t[4], t[5])
}
Output:
60 0
Example (Nested)

Layout nodes without a scene node pass their offset on: the scene is flat here, the layout nested. A nil target reads Tag as the *gfx.Node.

package main

import (
	"fmt"

	"github.com/haiodo/gowt/gfx"
	"github.com/haiodo/gowt/gfx/layout"
)

func main() {
	scene := gfx.NewScene()
	a, b := gfx.NewNode(1), gfx.NewNode(2)
	scene.Update(func(tx *gfx.Tx) {
		tx.Add(nil, a)
		tx.Add(nil, b)
	})
	scene.Commit()

	leaf := func(g *gfx.Node) *layout.Node {
		return &layout.Node{Tag: g, W: layout.Fixed(40), H: layout.Fixed(20)}
	}
	column := &layout.Node{Dir: layout.Column, Gap: 6, Kids: []*layout.Node{leaf(a), leaf(b)}}
	root := &layout.Node{Pad: layout.All(10), Kids: []*layout.Node{{W: layout.Fixed(30), H: layout.Fixed(30)}, column}, Gap: 5}
	root.Layout()

	scene.Update(func(tx *gfx.Tx) { layout.Apply(tx, root, nil) })
	scene.Commit()
	fmt.Println(a.Transform()[4], a.Transform()[5], b.Transform()[4], b.Transform()[5])
}
Output:
45 10 45 36

Types

type Align

type Align uint8

Align places children on an axis. SpaceBetween is for Justify only.

const (
	Start Align = iota
	Center
	End
	SpaceBetween
)

The alignments. SpaceBetween is for Justify only.

type Dir

type Dir uint8

Dir is how a node places its children.

const (
	Row Dir = iota
	Column
	Abs
)

The directions of a Node: children left to right, top to bottom, or each at its own X, Y.

type Insets

type Insets [4]float64

Insets are the four paddings of a Node: top, right, bottom, left.

func All

func All(v float64) Insets

All is the same padding v on every side.

func XY

func XY(x, y float64) Insets

XY is x on the left and the right, y on the top and the bottom.

type Kind

type Kind uint8

Kind is how a size is decided.

const (
	KindHug Kind = iota
	KindFixed
	KindFill
)

The kinds of Size. Use Fixed, Hug and Fill to make one.

type Node

type Node struct {
	W, H Size
	// Min and max sizes; a zero Max is no limit.
	MinW, MaxW, MinH, MaxH float64

	X, Y     float64 // position in an Abs parent, or of an Absolute child
	Absolute bool    // leaves the flow of a Row or a Column parent

	Dir     Dir
	Gap     float64
	Pad     Insets
	Align   Align
	Justify Align

	Wrap    bool    // break children into lines (Row, Column)
	LineGap float64 // between lines of a wrapping node

	Kids []*Node

	// LeafW and LeafH make a leaf depend on its content. LeafH is also called with the final width
	// when the height is already known, so a text can wrap itself at the width it got.
	LeafW func() float64
	LeafH func(w float64) float64

	// Tag is for the caller, for example the app's own node; Apply passes it back.
	Tag any

	// Results, relative to the parent's top left corner.
	RX, RY, RW, RH float64
	// contains filtered or unexported fields
}

Node is one box. Set the inputs, call Layout on the root, read RX, RY, RW, RH.

Example (Text)

Text measures itself: LeafW is its width on one line, LeafH its height at a given width.

package main

import (
	"fmt"

	"github.com/haiodo/gowt/gfx/layout"
)

func main() {
	const charW, lineH = 7.0, 16.0
	content := "a rather long caption"
	words := func(width float64) float64 { // greedy wrap by words
		lines, cur := 1.0, 0.0
		for _, w := range []string{"a", "rather", "long", "caption"} {
			n := float64(len(w)) * charW
			if cur > 0 && cur+charW+n > width {
				lines, cur = lines+1, 0
			}
			if cur > 0 {
				cur += charW
			}
			cur += n
		}
		return lines * lineH
	}
	text := &layout.Node{
		W:     layout.Fill,
		LeafW: func() float64 { return float64(len(content)) * charW },
		LeafH: words,
	}
	card := &layout.Node{
		Dir: layout.Column, Pad: [4]float64{8, 8, 8, 8},
		W:    layout.Fixed(96),
		Kids: []*layout.Node{text},
	}
	card.Layout()
	fmt.Printf("text %v x %v, card %v x %v\n", text.RW, text.RH, card.RW, card.RH)
}
Output:
text 80 x 48, card 96 x 64

func (*Node) Layout

func (n *Node) Layout()

Layout sizes n from its own W and H (Hug and Fill count as Hug at the root) and places everything below. It can be called again after the tree or the inputs changed.

type Size

type Size struct {
	Kind Kind
	V    float64 // the number of a Fixed size
}

Size is a width or a height: Fixed(v), Hug or Fill. The zero value is Hug.

func Fixed

func Fixed(v float64) Size

Fixed is a size of v points.

Jump to

Keyboard shortcuts

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