Learn · 07 of 09
Gestures and Canvas.
Canvas is the escape hatch for drawings that do not fit
the shape protocol: sparklines, plots, editors, pixel grids, and
arbitrary curves. Gestures attach to ordinary views, including
canvases, and receive pointer data in terminal cell space.
The terminal remains keyboard-first. Pointer support is additive:
use it for direct manipulation and inspection, but keep the main
workflow available from focus and commands.
Canvas coordinates
CanvasContext exposes continuous terminal-cell
coordinates. A point at x: 2.5 is halfway through the
third terminal cell. The active CanvasGrid decides how
those fractional samples are packed back into glyphs; the default
grid is Braille 2×4.
For dense grid work, Canvas(pixelGridWidth:height:pixels:mode:)
lets you write pre-resolved colors as full-cell or vertical
half-block pixels. For curves and plots, implement
CanvasDrawing.draw(into:) and use methods like
line(from:to:), fillRect, and
setPixel.
Gesture shape
TerminalUI includes the SwiftUI-shaped primitives:
TapGesture, SpatialTapGesture,
LongPressGesture, and DragGesture. Attach
them with .gesture(...), or use convenience modifiers
like .onTapGesture and .onLongPressGesture.
DragGesture.Value carries current location, start
location, translation, velocity, predicted end, pointer precision,
and the sampled path. Values are in cell-space coordinates, so they
compose directly with layout and canvas sizes.
@GestureState
@GestureState stores transient gesture state and resets
automatically when the gesture ends or the view leaves the tree. Use
it for "current drag" or "is pressing" state. Use @State
for committed model changes, like the appended samples in the
example.
Where to go next