Documentation
¶
Overview ¶
Package surface opens, sizes, animates, focuses and closes the places a program can appear in Rex: centred modal layers, edge-anchored drawers that slide in, and splits beside a block. It also keeps a pool of pre-warmed detached blocks that can be placed with their content already rendered.
Everything goes through Rex's control connection (rexuikit/client), never the rex CLI: a CLI call costs ~76ms, a call on an open connection 0.1-3ms (docs/rex-behaviors.md, measured latencies). The package depends on the narrow Rex interface below rather than *client.Client so its decisions are table-testable against a fake.
Sizing is the reason this package exists. A layer opened without `size` starts at 80x24 and its first frame is drawn at that size, so every open here computes the usable cell size from the window and the chrome model in rex-behaviors.md `## Layer chrome` and passes it as `size`.
Ownership: the connection that creates a block owns it, and while a non-app client owns a block the Rex app's size requests are not applied (rex-behaviors.md; `session.disown_block`). Every block opened here is disowned once it is in place, so the app sizes it from then on. Disowning needs a connection attached with session.attach, not session.attach_all.
Index ¶
- Constants
- Variables
- func BoundsForCells(w WindowCells, cols, rows int, anchor Anchor) (b client.Bounds, ok bool)
- func FindWarm(blocks []client.BlockInfo, action string) (client.BlockInfo, bool)
- func IsWarm(b client.BlockInfo) bool
- func Usable(w WindowCells, b client.Bounds) client.Size
- func WarmActionPrefix(action string) string
- func WarmLabel(action, buildID string) string
- func WriteContext(path string, pc PlacementContext) error
- type Anchor
- type BlurPolicy
- type Edge
- type Handle
- func (h *Handle) BlockID() string
- func (h *Handle) Blur() BlurPolicy
- func (h *Handle) Bounds() client.Bounds
- func (h *Handle) Close(ctx context.Context) error
- func (h *Handle) Done() <-chan struct{}
- func (h *Handle) Kind() Kind
- func (h *Handle) LayerID() string
- func (h *Handle) Raise(ctx context.Context) error
- func (h *Handle) Resize(ctx context.Context, b client.Bounds) error
- func (h *Handle) WindowID() string
- type Kind
- type Option
- type Place
- type PlacementContext
- type Rex
- type Spec
- type Surface
- func (s *Surface) Open(ctx context.Context, spec Spec) (*Handle, error)
- func (s *Surface) Place(ctx context.Context, wb WarmBlock, bounds client.Bounds, title string, ...) (*Handle, error)
- func (s *Surface) Warm(ctx context.Context, spec Spec) (WarmBlock, error)
- func (s *Surface) Window(ctx context.Context, windowID string) (WindowCells, string, error)
- type WarmBlock
- type WindowCells
Constants ¶
const ( // FramePxCols and FramePxRows turn a window's cell count into its // pixel extent: P = cells * cell_px + frame_px. The rows value is not a // whole number of cells (130 rows at 14 px span 1866 px, not 1862), so // the offset is in pixels. FramePxCols = 21 FramePxRows = 46 // BorderPxCols and BorderPxRows are the pixels the border and title bar // take out of a layer. BorderPxCols = 20 BorderPxRows = 46 // EdgeRounding is how each layer edge is rounded to a pixel, and // SizeRounding how the remaining pixels become cells. EdgeRounding = "round" SizeRounding = "floor" )
Layer chrome constants, copied from docs/rex-behaviors.md `## Layer chrome`, the single source for them. The Rex app lays a floating layer out in pixels: it rounds each edge to a whole pixel on its own, takes the border and title bar out in pixels, and cuts the rest into whole cells. That is why the usable size depends on where the layer sits, not only on its fractional width (w=0.40 over 149 columns is 57 at x=0.40 and 58 at x=0.55). TestChromeConstantsMatchBehaviorsDoc fails if these drift from the document, and the live TestLayerChromeDelta re-proves the document against Rex. W5.T2 injects these same values into the generated Lua.
const ( InverseMarginPxCols = 0 InverseMarginPxRows = 5 )
Inverse margins: the pixels BoundsForCells leaves spare beyond what the forward model needs, per axis. They are not chrome; rex-behaviors.md `### Inverse for BoundsForCells` records them and why.
The rows margin was added when rows were under-determined: a centred 46x30 layer over 149x130 at 7x14 px (W6.T2) spanned exactly 30*14 + 44 px and the app gave 29 rows where the old model (1862 px, border 44) said 30. The review-fix re-measure (96 samples, rows swept in single pixels) pins rows to one model, FramePxRows and BorderPxRows above, which reproduces that 29. The 5 px stay because that fit, like the cols one, comes from a single window and cell size: they absorb a frame or border a few pixels off on another window. 5 px is under one row, so the layer never gets a row more than asked. Cols were pinned to one model from the start and include zero-spare samples the app honoured, so they keep none.
const WarmPrefix = "forge-warm:"
WarmPrefix starts the label of every warm block. It is the one spelling of it: forge's install code closes blocks by it, and forge's generated Lua gets it injected at install time.
Variables ¶
var ( // Centered is the Modal anchor. Centered = Anchor{H: Center, V: Center} // RightEdge, LeftEdge, TopEdge and BottomEdge are the Drawer anchors: // flush against one edge, centred along it. RightEdge = Anchor{H: End, V: Center} LeftEdge = Anchor{H: Start, V: Center} TopEdge = Anchor{H: Center, V: Start} BottomEdge = Anchor{H: Center, V: End} )
var ErrNoCellSize = errors.New("rexuikit/surface: terminal size has no cell pixel size")
ErrNoCellSize means a terminal size result carried no pixel cell size, so the chrome model has no input to work from. Guessing a cell size would hand the program a wrong first frame, which is what sizing exists to stop.
var ErrNoTiledBlock = errors.New("rexuikit/surface: window has no tiled block")
ErrNoTiledBlock means the window's tiled layer has no block to read the window's size from.
var ErrNotLayer = errors.New("rexuikit/surface: not a floating layer")
ErrNotLayer is returned by Resize and Raise on a Split handle.
Functions ¶
func BoundsForCells ¶
BoundsForCells turns a component's natural size into fractional bounds over window w: the smallest layer (to one pixel) that gives the program at least cols x rows with the inverse margins spare, placed by anchor and clamped to the window. Usable of the result is then at least cols x rows whenever ok is true. ok is false when the window is too small on either axis; that axis then gets the whole window, the most the layer can have, and Usable reports how short it falls.
The size is only guaranteed at the returned x and y: pass the bounds to new_layer unchanged.
func FindWarm ¶
FindWarm returns the oldest detached block warmed for action. Placement is checked as well as the label because a placed block keeps its label until Place relabels it; a lookup in that window must not pick the form on screen. Block ids are `block:<uuidv7>`, so the lexically smallest id is the oldest block (rex-behaviors.md).
func IsWarm ¶
IsWarm reports whether b is a warm block waiting to be placed: a warm label AND detached placement. A placed block can keep its warm label for a moment before Place relabels it, and that block is the form on screen.
func Usable ¶
func Usable(w WindowCells, b client.Bounds) client.Size
Usable is the cell size a program sees in a floating layer at bounds b over a window of w. It is never below 1x1: a zero or negative terminal size would be refused by the server and is meaningless to a program.
func WarmActionPrefix ¶
WarmActionPrefix starts the label of every warm block for action, whatever binary built it.
func WarmLabel ¶
WarmLabel is the label of a warm block for action built by buildID. buildID ties the block to the binary that started it, so a rebuilt binary never places a form drawn by the old one.
func WriteContext ¶
func WriteContext(path string, pc PlacementContext) error
WriteContext writes pc to path atomically: a temp file in the same directory renamed over path, so a warm program polling the file never reads half of it.
Types ¶
type Anchor ¶
type Anchor struct {
H, V Place
}
Anchor places a content-sized layer: H on the horizontal axis, V on the vertical one.
type BlurPolicy ¶
type BlurPolicy int
BlurPolicy says what a surface does when it loses focus. Rex has no focus-loss hook a surface could act on server-side, and terminal options carry no environment, so the policy is enforced inside the program by rexuikit/surface/program; the opener hands the same value to the program it launches (in its argv) and the Handle records it.
const ( // StayOpen keeps the surface open on blur: the default, for modals and // drawers that hold work in progress. StayOpen BlurPolicy = iota // CloseOnBlur ends the program on blur: for pickers and popovers that // are pointless once the user has looked away. CloseOnBlur )
type Handle ¶
type Handle struct {
// contains filtered or unexported fields
}
Handle is an open surface.
func (*Handle) Blur ¶
func (h *Handle) Blur() BlurPolicy
Blur is the policy the surface was opened with.
func (*Handle) Close ¶
Close closes the surface: a Drawer slides out first. A surface that is already gone is not an error.
func (*Handle) Done ¶
func (h *Handle) Done() <-chan struct{}
Done is closed once the surface is gone: its block closed, its layer was removed, Close returned, or the connection ended (after which nothing about the surface can be observed).
type Option ¶
type Option func(*Surface)
Option configures a Surface.
func WithAnimation ¶
WithAnimation sets the drawer animation: frames resize steps, every apart. The default is 12 steps at 16ms, which the user confirmed reads as smooth. frames < 1 makes a drawer open and close without animating.
type Place ¶
type Place int
Place is where a content-sized layer sits on one axis; Start is left or top.
type PlacementContext ¶
type PlacementContext struct {
OpenerBlockID string `json:"opener_block_id"`
Cwd string `json:"cwd"`
SessionID string `json:"session_id"`
WindowID string `json:"window_id"`
// Timestamp is Unix seconds: config Lua has only os.time, which has no
// finer clock (rex-behaviors.md).
Timestamp int64 `json:"timestamp"`
// WarmBlockID is the warm block being placed. Every warm program for
// the action polls the same file, so a program accepts a context only
// when this is its own block (Rex sets REX_BLOCK to each block's id).
// Empty in a context written by an older placer.
WarmBlockID string `json:"warm_block_id,omitempty"`
}
PlacementContext tells a warm program where it was placed. A warm program starts before anyone knows which pane will open it, so the placer (forge's generated Lua) writes this file just before placement and the program reads it when it gets focus.
func ReadContext ¶
func ReadContext(path string) (PlacementContext, error)
ReadContext reads a context file written by WriteContext or the Lua.
func (PlacementContext) For ¶
func (pc PlacementContext) For(self string) bool
For reports whether a warm program in block self may act on pc. A context naming another block is that block's placement. An empty self (the block is unknown) or an empty WarmBlockID (an older placer) accepts any.
type Rex ¶
type Rex interface {
View(ctx context.Context, sessionID string) (client.View, error)
TerminalSize(ctx context.Context, sessionID, blockID string) (client.TerminalSize, error)
NewLayer(ctx context.Context, a client.NewLayerArgs) (client.NewLayerResult, error)
ResizeLayer(ctx context.Context, sessionID, layerID string, b client.Bounds) (int64, error)
CloseLayer(ctx context.Context, sessionID, layerID string) (int64, error)
RaiseLayer(ctx context.Context, sessionID, layerID, relativeLayerID string) (int64, error)
NewSplit(ctx context.Context, a client.NewSplitArgs) (client.NewSplitResult, error)
NewBlock(ctx context.Context, a client.NewBlockArgs) (string, error)
MoveBlock(ctx context.Context, a client.MoveBlockArgs) (client.MoveBlockResult, error)
FocusBlock(ctx context.Context, sessionID, blockID string) (int64, error)
SetBlockLabel(ctx context.Context, sessionID, blockID, label string) (int64, error)
ListBlocks(ctx context.Context, sessionID string) ([]client.BlockInfo, error)
CloseBlock(ctx context.Context, sessionID, blockID string) (int64, error)
Call(ctx context.Context, method string, payload, out any) error
Events() <-chan client.Event
}
Rex is the part of *client.Client this package uses. Events may return nil (a client dialled with DisableEvents); Done then fires only on Close.
type Spec ¶
type Spec struct {
Kind Kind
// Argv is the program, run as the block's own process (shell "none"),
// so the block closes when it exits.
Argv []string
Cwd string
// Label is the block's label, which Rex shows as the layer's title.
Label string
// WindowID is the window to open in; empty means the active window.
WindowID string
// Bounds is [x, y, w, h] in window fractions. Zero bounds with a
// non-zero Cells sizes the layer to its content instead.
Bounds client.Bounds
// Cells is the content's natural size, used when Bounds is zero; the
// layer is placed by Anchor (zero Anchor is centred) for a Modal and by
// Edge for a Drawer.
Cells client.Size
Anchor Anchor
Edge Edge
Focus bool
Blur BlurPolicy
// Split only. Direction and Side default to horizontal and after; zero
// Ratio is the server's 0.5.
AnchorBlock string
Direction string
Side string
Ratio float64
}
Spec describes a surface to open.
type Surface ¶
type Surface struct {
// contains filtered or unexported fields
}
Surface opens surfaces in one Rex session over one connection. It is the only reader of the connection's Events channel: the client fails a connection whose events go undrained, so a second reader would race this one for block_closed.
func New ¶
New returns a Surface for sessionID. The connection must already be attached to the session (session.attach), which disown_block needs and which subscribes it to the events Done relies on.
func (*Surface) Open ¶
Open opens a surface. The returned handle's Done closes when its block closes (its program exited or someone closed it) or its layer goes away.
func (*Surface) Place ¶
func (s *Surface) Place(ctx context.Context, wb WarmBlock, bounds client.Bounds, title string, focus bool) (*Handle, error)
Place shows a warm block in a new floating layer at bounds. Rex's float_block refuses detached blocks (not_found), so placement is the sequence the live tests proved: new_layer with a placeholder, move_block the warm block beside it, block.close the placeholder, which leaves the warm block alone in the layer (~10-15ms in-server). The block is then relabelled to title, focused when asked (a detached block does not take focus when moved) and disowned.
title is what Rex's title bar shows for the layer: the label of the layer's only block. It is the form's own title, the same a cold open uses, so a pre-warmed form reads the same as a cold one. The plan for this package named a fixed `forge-live:<action>` label (LiveLabel) instead; the user chose the readable title (decisions.md, "Readable title"), so there is no LiveLabel. Nothing looks a placed block up by it: FindWarm needs the warm prefix AND detached placement, and the relabel also drops the warm prefix, so a placed form can never be picked again whatever its title says. An empty title is refused before anything is placed, because the block would keep its machine label in the title bar.
type WarmBlock ¶
type WarmBlock struct {
BlockID string
Label string
// Size is the cell size the program was started at.
Size client.Size
// WindowID is where Place opens the layer; empty means the active
// window at Place time.
WindowID string
}
WarmBlock is a detached block started by Warm.
type WindowCells ¶
WindowCells is what the chrome model needs to know about a window: its size in cells and the size of one cell in pixels. Both come from com.superlogical.terminal.size of a block in the window's tiled layer.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package program is the in-program half of rexuikit/surface: what a bubbletea program running inside a surface needs to see focus the way Rex delivers it.
|
Package program is the in-program half of rexuikit/surface: what a bubbletea program running inside a surface needs to see focus the way Rex delivers it. |