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 ¶
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 ¶
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 Insets ¶
type Insets [4]float64
Insets are the four paddings of a Node: top, right, bottom, left.
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