layout

package
v0.3.2-rc.1 Latest Latest
Warning

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

Go to latest
Published: Sep 13, 2026 License: MIT Imports: 3 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 models a tab's panes as a tree of splits and turns that tree into concrete screen rectangles.

Every operation here leaves the tree in the same shape, and the rest of the package and its callers rely on that:

  • a pane appears once, so closing it closes all of it and finding it finds the one the user is looking at;
  • a split holds two children or more, never one, so no divider is drawn with nothing on one side of it;
  • no split nests inside a split along its own axis, so three panes in a row share the row rather than one of them sharing it with a pair;
  • Compute tiles the box it is given exactly, leaving no cell to two panes and none to nobody, as long as there is a cell in it for each pane.

A tree read back from a saved layout can arrive in other shapes, and the operations cope with those rather than assuming them away.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Dir

type Dir int

Dir is the axis along which a split node arranges its children.

const (
	// Horizontal arranges children left to right, separated by a vertical rule.
	Horizontal Dir = iota
	// Vertical arranges children top to bottom.
	Vertical
)

type Direction

type Direction int

Direction is a focus movement direction.

const (
	Left Direction = iota
	Right
	Up
	Down
)

Focus movement directions.

type Edge

type Edge int

Edge names where a pane lands relative to the one it was dropped on.

const (
	// EdgeLeft puts the moved pane to the left of the target, and so on round.
	EdgeLeft Edge = iota
	EdgeRight
	EdgeTop
	EdgeBottom
)

Drop edges.

type Node

type Node struct {
	// ID names this node so a client can refer to it, for example to set the
	// weights of a split's children after a drag.
	ID string
	// Pane is the pane id when this node is a leaf.
	Pane string
	// Dir and Children are set when this node is a split.
	Dir      Dir
	Children []*Node
	// Weight is this node's share of its parent's space, relative to its
	// siblings. Zero is treated as 1.
	Weight float64
	// contains filtered or unexported fields
}

Node is either a leaf holding one pane, or a split holding children.

func Combine

func Combine(dst, src *Node, dir Dir) *Node

Combine returns a tree holding every pane of both trees, arranged along dir with src's panes after dst's. Neither tree is copied: the nodes come across as they are, so a pane keeps its node id and the splits inside each tree keep their shape and proportions.

The space is shared out a column at a time rather than a tree at a time, so merging a lone pane with a row of three gives four even columns. A tree that is already a split along dir is flattened into the result instead of nested inside it, for the same reason Split and InsertBeside append to an existing split: a row of two merged with a row of two is four columns, not a row of rows.

Half the room each is the obvious rule and the wrong one, because it compounds. Folding tabs in one at a time gave the last one half the width, the one before a quarter and the one before that an eighth, so gathering a project's tabs left the first of them a column wide — and dragging that back into shape means finding a divider that is no longer there to grab. Per column, a tab folded in behind four others is one of five even columns however it got there, and a divider the user has moved keeps its proportion against its own neighbours.

func Grid

func Grid(panes []string) *Node

Grid arranges panes into rows of even columns, in the order given.

A fan-out puts a dozen agents in one tab, and neither a row of twelve nor a stack of twelve is a tab anyone can read: at that width a terminal wraps every line it prints, and at that height it holds four lines at a time. Rows of columns keeps both dimensions usable, and it is the shape someone arranging a dozen panes by hand would end up dragging out.

The tree is built afresh each time, so a divider the user had moved in the old one is not carried over. That is the trade a caller makes by asking for a grid: it is for a tab being filled, where the alternative is each new pane halving the last one.

func NewLeaf

func NewLeaf(pane string) *Node

NewLeaf returns a leaf node for a pane.

func NewSplit

func NewSplit(dir Dir) *Node

NewSplit returns an empty split node along dir.

func Tile added in v0.2.3

func Tile(root *Node, box Rect) *Node

Tile rebuilds a tree as an even grid of the same panes, measured against box.

The panes fill the grid in tree order, as Grid fills it — unless every pane already sits in a cell of its own in that grid, when each keeps its cell. A tab already laid out in rows of columns then has its proportions evened and nothing moved. Filling it in tree order instead swapped two panes of a grid built a column at a time, since a column comes out of the tree whole, before the pane beside its top; tiling is the way back to a tidy tab, and one that was tidy already should not come back rearranged.

func (*Node) Compute

func (n *Node) Compute(r Rect)

Compute assigns rectangles to every node in the tree.

func (*Node) Count

func (n *Node) Count() int

Count returns the number of panes in the tree.

func (*Node) Find

func (n *Node) Find(pane string) *Node

Find returns the leaf holding the given pane, or nil.

func (*Node) FindNode

func (n *Node) FindNode(id string) *Node

FindNode returns the node with the given id, or nil.

func (*Node) Flatten added in v0.3.1

func (n *Node) Flatten()

Flatten splices every split nested in a split along its own axis into that split, sharing out the room the nested one held among its children so that every pane keeps the size it had.

No operation here builds that shape, but a tree read back from a saved layout can hold it: builds before Remove spliced a collapsed split into its parent left one behind every time, and a layout carries it from run to run for as long as nothing rewrites that part of it. A row nested in a row looks like one row and does not behave like one — a pane split beside one of its panes shares that pane's group rather than the row, coming out a fraction of the width its neighbours have, and the divider between the groups moves several panes at once.

func (*Node) InsertBeside

func (root *Node) InsertBeside(target, pane string, edge Edge) bool

InsertBeside places a pane on one edge of the leaf holding target. It returns false when the target is not in the tree.

Like Split, it appends to an existing split along the same axis rather than nesting a new one, so dropping a third pane to the right of two side by side gives three columns rather than a column containing a column.

func (*Node) IsLeaf

func (n *Node) IsLeaf() bool

IsLeaf reports whether the node holds a pane rather than children.

func (*Node) Leaves

func (n *Node) Leaves() []*Node

Leaves returns every leaf holding a pane, in tree order: each split's children in the order they are laid out, and each of those in full before the next one begins. That is not the order the tab reads in — a column beside a tall pane comes out whole, before the pane to the right of its top half.

A split with no children left looks structurally like a leaf but names no pane, which is what a tab emptied by a merge is holding. It is skipped, so it is not counted as a pane, offered as a drop target, or drawn as an empty frame by a client walking the tree.

func (*Node) MovePane

func (root *Node) MovePane(pane, target string, edge Edge) bool

MovePane moves an existing pane so that it sits on one edge of another, within the same tree. It returns false when either pane is missing, or when they are the same pane.

The removal happens first and the target is found again afterwards, because collapsing a split left with one child can move that child's contents into its parent and leave any pointer to it stale.

func (*Node) Neighbor

func (root *Node) Neighbor(pane string, dir Direction) string

Neighbor returns the pane adjacent to the given one in a direction, chosen by geometry so that focus movement matches what is on screen. Compute must have been called first.

func (*Node) PaneAt

func (root *Node) PaneAt(x, y int) string

PaneAt returns the pane whose rectangle contains the point, or "".

func (*Node) Panes

func (n *Node) Panes() []string

Panes returns the ids of every pane in the tree, in tree order.

func (*Node) Rect

func (n *Node) Rect() Rect

Rect returns the rectangle assigned by the last Compute call.

func (*Node) Remove

func (root *Node) Remove(pane string) bool

Remove deletes the pane's leaf, collapsing any split left with one child. It returns false if the pane is not in the tree.

func (*Node) Resize

func (root *Node) Resize(pane string, dir Dir, delta float64) bool

Resize shifts space between the pane and one of its siblings along the given axis. A positive delta always grows the named pane, whichever sibling pays for it: normally the next one along, or the previous one when the pane is last in its split.

delta is measured against an even share of the split, so 0.5 moves the divider by half of what one pane would get if they all shared equally, and a pane may not be squeezed below a tenth of that share.

func (*Node) Separators

func (n *Node) Separators() []Rect

Separators returns the vertical rules between horizontally adjacent panes, each as a 1-cell-wide rectangle.

func (*Node) SetChildWeights

func (n *Node) SetChildWeights(weights []float64) bool

SetChildWeights assigns weights to a split's children in order. It reports false if the node is not a split with a matching number of children, or if any weight is not a usable positive number.

The weights arrive from a divider drag in the browser, so they are checked in full before any of them is stored: a rejected set must leave the split exactly as it was rather than half updated. A weight that is not finite would poison every later Compute of the tree, so it is rejected too.

func (*Node) Split

func (root *Node) Split(pane, newPane string, dir Dir) bool

Split replaces the leaf holding pane with a split containing that pane and newPane, arranged along dir. It returns false if the pane is not in the tree, or if newPane is empty or already somewhere in it.

Splitting an existing split along the same axis appends a sibling instead of nesting, which keeps three-way splits evenly sized rather than lopsided.

func (*Node) SwapPanes

func (root *Node) SwapPanes(a, b string) bool

SwapPanes exchanges the positions of two panes, keeping every split and weight as it was. It returns false when either pane is missing.

type Rect

type Rect struct{ X, Y, W, H int }

Rect is a screen rectangle in cells.

func (Rect) Contains

func (r Rect) Contains(x, y int) bool

Contains reports whether the point lies inside the rectangle.

Jump to

Keyboard shortcuts

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