panelayout

package
v1.12.0 Latest Latest
Warning

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

Go to latest
Published: Sep 2, 2026 License: MIT Imports: 4 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 panelayout owns the presentation-neutral pane tree shared by project and global workspace previews. It knows structure, geometry, and the default placement policy, but nothing about terminals, documents, issues, rendering, or persistence.

Index

Constants

View Source
const (
	// MaxGridColumns bounds how many side-by-side columns one layout may span.
	// It is a sanity bound for the vocabulary, not the real constraint — the
	// per-kind floors refuse most large grids on ordinary terminals first.
	MaxGridColumns = 4
	// MaxGridRows bounds how many panes one column may stack.
	MaxGridRows = 4
)

The layout vocabulary is columns of stacked panes: a layout is 1.. MaxGridColumns columns and each column stacks 1..MaxGridRows panes. Cells are addressed col.row, both 1-based, left to right then top to bottom, so "2.1" is the second column's top pane. This is a vocabulary over the binary tree, not a second structure: GridOf projects a tree onto it and the plan functions compile placements back into ordinary splits.

View Source
const (
	GridColumnCapMessage = "four columns of panes at a time; close one first"
	GridRowCapMessage    = "four panes in a column at a time; close one first"
	LiveCapMessage       = "two live terminals at a time; close one first"
)

The caps are rules a caller reports, not clicks that quietly do nothing: these are the refusal strings for each cap, worded to stand alone in a toast or an agent-facing ack. LiveCapMessage is the planner-level twin of the workspace's shell-cap toast; both say the same rule so neither can drift into promising a fifth live terminal.

View Source
const (
	MoveUnchangedMessage = "that move leaves the layout unchanged"
	MoveFitMessage       = "the window is too small to split"
)
View Source
const (
	KindNamePrimary  = "primary"
	KindNameFile     = "file"
	KindNameIssue    = "issue"
	KindNameDiff     = "diff"
	KindNameResource = "resource"
	KindNameShell    = "shell"
	KindNameNote     = "note"
)

The layout vocabulary's wire names, shared by `sidecar layout` descriptors, get reports, and the ack items: one spelling per kind so an agent can read a layout and speak it back without translating. KindByName and Name are the only spellings; callers never hard-code the strings.

View Source
const LiveLeafCap = 2

LiveLeafCap is how many live leaves may be on screen in one tree at once: the host's primary terminal plus one peer. Live leaves cost a control-mode subscription and a tmux resize per geometry change, so the cap is a refusal the caller reports, never a silent drop.

View Source
const Terminal = Primary

Terminal is the persisted-value and source compatibility alias used by the existing Workspace hosts. Primary deliberately retains its numeric value.

Variables

View Source
var ContentPaneTarget = Target{Kind: TargetLeaf}

ContentPaneTarget names the single content window of a surface that has no pane tree — git's diff pane, the file browser's preview, the conversation message list, the notes editor. Those surfaces have one list and one content pane and nothing to give a leaf an id, so they all share this entry.

Functions

func AtRingEnd added in v1.2.0

func AtRingEnd(ring []Target, current Target, reverse bool) bool

AtRingEnd reports that current is the last stop of the ring in the direction a cycle is about to move: the final entry going forward, the first one going back. It is the question a shell-owned stop has to ask before it can join a surface's Tab cycle — the notification centre uses it to insert itself between a surface's last window and its first, without either side growing a second cycle. A current target the ring does not contain is not at the end: the cycle has not started yet.

func ClampRatio

func ClampRatio(ratio int) int

func Duplicable added in v1.3.0

func Duplicable(kind Kind) bool

Duplicable reports that a second leaf of this kind is a second thing, not the same thing shown again. Document/Issue/Diff/Resource/Note swap their content in place, so opening one twice retargets the leaf that exists; a Shell open is a new session, so it always splits.

func GridColumnsAtCap added in v1.7.0

func GridColumnsAtCap(root *Node) bool

GridColumnsAtCap reports that the tree's layout already spans every column the vocabulary allows. It reads the projection, exactly as LiveCapReached reads the leaf walk.

func GridRowAtCap added in v1.7.0

func GridRowAtCap(root *Node, col int) bool

GridRowAtCap reports that column col of the tree's layout is full.

func IsLive added in v1.3.0

func IsLive(kind Kind) bool

IsLive reports that a leaf of this kind drives a live terminal session.

func LayoutPanes

func LayoutPanes(root *Node, box Box, floors Floors) (leaves []Placement, dividers []Divider, fits bool)

func LeafCount added in v1.3.0

func LeafCount(node *Node) int

LeafCount is how many content leaves the tree holds. It is the number a sidebar row's layout badge is derived from, so a live tree and the tree the same workspace persisted are counted by one rule.

func LiveCapReached added in v1.3.0

func LiveCapReached(root *Node) bool

LiveCapReached reports that no further live leaf fits in this tree. Hosts use it to explain the refusal instead of leaving PlanOpen's false unexplained.

func LiveLeafCount added in v1.3.0

func LiveLeafCount(node *Node) int

LiveLeafCount is how many live leaves the tree currently holds.

func MaxID

func MaxID(node *Node) int

func SetRatio

func SetRatio(root *Node, splitID, ratio int) bool

Types

type Axis

type Axis int
const (
	Columns Axis = iota
	Rows
)

type Box

type Box = termpreview.Box

type Cell added in v1.7.0

type Cell struct {
	Col int
	Row int
}

Cell addresses one pane of the grid: Col 1-based left to right, Row 1-based top to bottom.

func ParseCell added in v1.7.0

func ParseCell(s string) (Cell, bool)

ParseCell reads a "col" or "col.row" address (both 1-based). A bare column means row 1, which is how a one-past-the-end column append is asked for.

func (Cell) String added in v1.7.0

func (c Cell) String() string

String renders the cell as its "col.row" address.

type Direction added in v1.11.0

type Direction int

Direction is a movement in the projected grid. It is presentation-neutral: key bindings, modals, and commands translate their own input into one of these values before asking for a destination.

const (
	DirectionLeft Direction = iota
	DirectionRight
	DirectionUp
	DirectionDown
)

type Divider

type Divider struct {
	SplitID int
	Axis    Axis
	Box     Box
}

type Floor

type Floor struct{ Width, Height int }

type Floors

type Floors struct {
	Primary Floor
	// Terminal is the compatibility field for existing Workspace callers.
	// Primary wins when both are populated.
	Terminal Floor
	Doc      Floor
	Issue    Floor
	Diff     Floor
	Resource Floor
	Shell    Floor
	Note     Floor
}

type Grid added in v1.7.0

type Grid struct {
	Columns []GridColumn
	// contains filtered or unexported fields
}

Grid is a tree that resolves to the columns-of-rows vocabulary.

func GridOf added in v1.7.0

func GridOf(root *Node) *Grid

GridOf projects a tree onto the columns-of-rows vocabulary. Same-axis nesting flattens: a three-pane column is Rows splits chained inside Rows, and columns chain inside Columns. A tree that alternates axes below the column/row seam — a Columns split nested inside a column's row stack — escapes the vocabulary and returns nil; such a tree is still valid, it just has no grid answer.

func (*Grid) Cell added in v1.7.0

func (g *Grid) Cell(col, row int) *Node

Cell names the leaf occupying col.row (1-based), or nil when no pane is there.

func (*Grid) ColumnCount added in v1.7.0

func (g *Grid) ColumnCount() int

ColumnCount is how many columns the layout spans.

func (*Grid) ColumnsAtCap added in v1.7.0

func (g *Grid) ColumnsAtCap() bool

ColumnsAtCap reports that another column cannot open beside this layout.

func (*Grid) Root added in v1.7.0

func (g *Grid) Root() *Node

Root is the tree the projection came from; splitting it on Columns appends a column.

func (*Grid) RowCount added in v1.7.0

func (g *Grid) RowCount(col int) int

RowCount is how many panes column col (1-based) stacks, or 0 when there is no such column.

func (*Grid) RowsAtCap added in v1.7.0

func (g *Grid) RowsAtCap(col int) bool

RowsAtCap reports that column col (1-based) cannot take another row.

type GridColumn added in v1.7.0

type GridColumn struct {
	Node  *Node
	Cells []*Node
}

GridColumn is one column of the projected layout: Node spans the whole column (splitting it on Rows appends below), Cells are its leaves top to bottom (the cells a col.row address resolves against).

type Kind

type Kind int
const (
	// Primary is the host's owned content. In Workspaces that content is a
	// terminal; app content decks may host any plugin without changing the pane
	// tree's persisted vocabulary.
	Primary Kind = iota
	Document
	Issue
	Diff
	// Resource is the single leaf kind every external terminal resource
	// provider shares. The extension point is which resource is recognized
	// and resolved, not which windows exist, so a Jira ticket and a CI build
	// become tabs in one leaf rather than two pane kinds.
	Resource
	// Shell is a live terminal leaf the user created, distinct from Primary:
	// Primary is the one terminal the host owns, Shell is a peer session the
	// tree hosts beside it. Keeping them apart is what lets a host have more
	// than one live terminal without the tree learning what a terminal is.
	Shell
	// Note is a read-only td note card. Added after Shell so existing numeric
	// Kind values stay stable; persistence uses the string "note".
	Note
)

func KindByName added in v1.7.0

func KindByName(name string) (Kind, bool)

KindByName resolves one wire name to its kind. ok is false for anything the vocabulary does not name.

func (Kind) Name added in v1.7.0

func (k Kind) Name() string

Name is the kind's wire name in the layout vocabulary.

type Layout

type Layout struct {
	Leaves   []Placement
	Dividers []Divider
	Zoomed   bool
}

func LayoutTree

func LayoutTree(root *Node, box Box, floors Floors, focus int) (Layout, bool)

func LayoutTreeWithZoom added in v1.11.0

func LayoutTreeWithZoom(root *Node, box Box, floors Floors, focus, requestedZoom int) (Layout, bool)

LayoutTreeWithZoom gives a valid requested leaf the whole box. A missing request falls through to the ordinary layout, including its existing focused-leaf fallback when the complete tree cannot fit.

type MoveDestination added in v1.11.0

type MoveDestination struct {
	Cell      Cell
	OuterEdge OuterColumnEdge
}

MoveDestination is either a cell in the pre-move grid or an outer column edge. Exactly one form must be populated.

func MoveDirection added in v1.11.0

func MoveDirection(root *Node, leafID int, direction Direction) (MoveDestination, bool)

MoveDirection compiles one directional step against the pre-move grid. Vertical boundaries have no destination. Horizontal boundaries retain an explicit outer edge so PlanMove can distinguish an identity move from a cap refusal after source extraction.

type MoveOutcome added in v1.11.0

type MoveOutcome struct {
	Status MoveStatus
	Plan   MovePlan
	Reason string
}

MoveOutcome says whether a request moves, is already satisfied, or is refused. Reason is user-facing for unchanged and refused outcomes.

func PlanMove added in v1.11.0

func PlanMove(root *Node, leafID int, destination MoveDestination, box Box, floors Floors) MoveOutcome

PlanMove extracts a leaf on a clone, translates the destination from the visible pre-move grid, and accepts only a result that fits the real surface. The input tree is never mutated.

type MovePlan added in v1.11.0

type MovePlan struct {
	LeafID          int
	Placement       OpenPlan
	CarriedRatio    int
	HasCarriedRatio bool
}

MovePlan is a structural edit compiled against the tree passed to PlanMove. Placement is resolved after source removal; callers must not reinterpret it against the pre-move grid.

type MoveStatus added in v1.11.0

type MoveStatus int
const (
	MoveRefused MoveStatus = iota
	MoveUnchanged
	MoveMoved
)

type Node

type Node struct {
	ID        int
	Kind      Kind
	ContentID int
	Split     *Split
}

func ApplyMove added in v1.11.0

func ApplyMove(root *Node, plan MovePlan) (*Node, int)

ApplyMove applies a previously accepted plan while retaining the exact leaf pointer and ID. Host-owned state keyed by either identity therefore follows the pane instead of being reconstructed.

func ApplyPlan added in v1.7.0

func ApplyPlan(root *Node, plan OpenPlan, newLeaf *Node) (*Node, int)

ApplyPlan grafts newLeaf where plan says: retargets are the caller's to honor, so this is SplitLeaf with the plan's own axis and ordering.

func Clone

func Clone(node *Node) *Node

Clone returns a detached copy suitable for trial layout before committing a split that may not fit the current viewport.

func Close

func Close(root *Node, leafID int) (*Node, int)

func Find

func Find(root *Node, id int) *Node

func FirstOfContent added in v1.3.0

func FirstOfContent(node *Node, kind Kind, contentID int) *Node

FirstOfContent names the leaf already showing one content id of a kind, or nil. A live session is never shown in two leaves, so an open that names a session already on screen retargets it rather than splitting.

func FirstOfKind

func FirstOfKind(node *Node, kind Kind) *Node

func SplitLeaf

func SplitLeaf(root *Node, targetID int, axis Axis, newLeaf *Node) (*Node, int)

SplitLeaf divides the node targetID names — a leaf for the ordinary split, or an internal node (a column's subtree, the root) when a placement appends to a stack or starts a column. The divided subtree becomes the split's first child and newLeaf its second; the tree is spliced in place and the new leaf's ID is returned as the focus. Anything invalid — an unknown target, a malformed or overlapping candidate, colliding IDs — leaves the tree untouched and returns the target unchanged.

type OpenPlan

type OpenPlan struct {
	Retarget int
	Split    int
	Axis     Axis
	NewFirst bool
}

OpenPlan is the shared click-placement answer. Retarget names an existing leaf of the requested kind; otherwise Split names the node to divide — a leaf for an ordinary split, or a column's subtree (even the root) when the grid rule appends to a stack or starts a column. NewFirst puts newLeaf above the divided subtree instead of below it, which is how an insert at an occupied cell pushes the occupant down.

func ApplyAxisOverride added in v1.0.0

func ApplyAxisOverride(plan OpenPlan, split string) OpenPlan

ApplyAxisOverride rewrites a split plan's axis from the CLI --split flag. auto / empty leave PlanOpen's axis alone. A retarget is unchanged: --split never forces a second leaf of a kind that already exists, and it never retargets the named leaf onto the terminal.

func PlanOpen

func PlanOpen(root *Node, kind Kind, boxes map[int]Box) (OpenPlan, bool)

PlanOpen keeps the primary content in a full-height left column: the first content opens beside it, a different content kind stacks in the right column, and once two content panes are on screen the grid rule takes over — the emptiest column takes the next pane (so the fourth pane forms a 2×2 rather than a third stacked row), full columns give way to a new column, and the caps refuse. boxes may be nil; they only break ties for trees that escape the grid vocabulary, where the legacy largest-content-leaf rule still answers.

func PlanOpenAt added in v1.7.0

func PlanOpenAt(root *Node, kind Kind, contentID int, cell Cell) (OpenPlan, string)

PlanOpenAt plans an open at an explicit cell, the planner entry behind the CLI's --at. The cell is a requirement, not a preference: a kind whose open would retarget an existing leaf is refused rather than placed elsewhere, because landing the pane somewhere else is precisely what --at must not do.

Resolution against the current grid:

  • An occupied cell inserts at that position: the new pane takes the cell and the occupant — with everything already below it — shifts down one row (a Rows split on the occupant, new pane first).
  • One row past the column's end appends at the bottom of that column.
  • One column past the end (row must be 1) appends a new column.
  • Anything further out, a column past MaxGridRows, or a tree that escapes the grid vocabulary is refused with the reason to show.

The returned string is empty when a plan was made; otherwise it is the visible reason, ready for a toast or an ack.

func PlanOpenContent added in v1.3.0

func PlanOpenContent(root *Node, kind Kind, contentID int, boxes map[int]Box) (OpenPlan, bool)

PlanOpenContent is PlanOpen for a named piece of content. A duplicable kind never retargets a leaf showing something else — every open is a new session — but it does retarget the leaf already showing this exact content, which is what keeps one tmux session out of two leaves.

type OuterColumnEdge added in v1.11.0

type OuterColumnEdge int

OuterColumnEdge names the two placements Cell cannot express symmetrically.

const (
	NoOuterColumnEdge OuterColumnEdge = iota
	BeforeFirstColumn
	AfterLastColumn
)

type Placement

type Placement struct {
	Node *Node
	Box  Box
}

type Split

type Split struct {
	Axis  Axis
	Ratio int
	A, B  *Node
}

type Target added in v1.0.0

type Target struct {
	Kind TargetKind
	Leaf int
}

Target is a single focusable window. Leaf is meaningful only for TargetLeaf.

func CycleTarget added in v1.0.0

func CycleTarget(ring []Target, current Target, reverse bool) Target

CycleTarget returns the next target after current, wrapping at both ends. A current target that is not in the ring starts the cycle at the first entry (the last when reverse); an empty ring leaves focus where it is.

func Ring added in v1.0.0

func Ring(root *Node, sidebarVisible bool, shellVisible ...bool) []Target

Ring lists the focusable windows in cycle order: the sidebar (when visible), then every passive tree leaf in placement order, then every live Shell leaf. Appending Shell leaves preserves the historical cycle order while making a live terminal's pane-tree leaf ID its only focus identity.

func RingStart added in v1.2.0

func RingStart(ring []Target, reverse bool) (Target, bool)

RingStart is the entry a cycle resumes at when focus comes back to a surface from a shell-owned stop: the first window going forward, the last going back.

func TwoPaneRing added in v1.2.0

func TwoPaneRing(listVisible, contentVisible bool) []Target

TwoPaneRing is Ring for the surfaces whose layout is a list beside one content pane rather than a tree. Either window may be off screen — a hidden sidebar, a content pane with nothing selected to draw — and a window that is not drawn is not a stop, exactly as with the tree ring.

It exists so that "the ring" has one definition for those surfaces too: the shell inserts the notification centre at the wrap point of whatever this returns, and the surface's own Tab toggle stays its own.

type TargetKind added in v1.0.0

type TargetKind int

TargetKind names the sort of window a focus target refers to.

const (
	// TargetSidebar is the workspace list beside the preview.
	TargetSidebar TargetKind = iota
	// TargetLeaf is a pane-tree leaf, identified by Target.Leaf.
	TargetLeaf
)

Jump to

Keyboard shortcuts

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