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 ¶
- type Dir
- type Direction
- type Edge
- type Node
- func (n *Node) Compute(r Rect)
- func (n *Node) Count() int
- func (n *Node) Find(pane string) *Node
- func (n *Node) FindNode(id string) *Node
- func (root *Node) InsertBeside(target, pane string, edge Edge) bool
- func (n *Node) IsLeaf() bool
- func (n *Node) Leaves() []*Node
- func (root *Node) MovePane(pane, target string, edge Edge) bool
- func (root *Node) Neighbor(pane string, dir Direction) string
- func (root *Node) PaneAt(x, y int) string
- func (n *Node) Panes() []string
- func (n *Node) Rect() Rect
- func (root *Node) Remove(pane string) bool
- func (root *Node) Resize(pane string, dir Dir, delta float64) bool
- func (n *Node) Separators() []Rect
- func (n *Node) SetChildWeights(weights []float64) bool
- func (root *Node) Split(pane, newPane string, dir Dir) bool
- func (root *Node) SwapPanes(a, b string) bool
- type Rect
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
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 ¶
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 ¶
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 Tile ¶ added in v0.2.3
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) InsertBeside ¶
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) Leaves ¶
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 ¶
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 ¶
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) Remove ¶
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 ¶
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 ¶
Separators returns the vertical rules between horizontally adjacent panes, each as a 1-cell-wide rectangle.
func (*Node) SetChildWeights ¶
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 ¶
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.