docs(specs): physical stack directions; only a canvas with onPointer takes input (pointer: true)

This commit is contained in:
OpenCode Agent 2026-11-22 00:27:05 +00:00
parent 7e383bba95
commit d7097dae16
7 changed files with 45 additions and 10 deletions

View file

@ -105,7 +105,7 @@ and drops `render`/`draw`/`frame` posts beyond 240 a second with a warning.
Event `id`s come from the nodes, where the prelude put them (widget templates attach handlers and never see ids): `button`
and `card` (with `id`) send `press`; `checkbox` (`boolean`), `slider` (`number`, on release), `select` (`string`, or
`string[]` when `multiple`), `input` (`string`, on commit or after 300 ms) and `carousel` (with `id`, new page index)
send `change`. `canvas` (with `id`) sends `pointer` with `{ kind: "down" | "move" | "up", x, y, width,
send `change`. `canvas` (with `pointer: true`) sends `pointer` with `{ kind: "down" | "move" | "up", x, y, width,
height }`. Keyboard, focus, full-screen and capture events (`specs/v2/widgets-input.md`) carry `id: ""`. The prelude
routes each event to the right handler or hook and drops the rest, so a client may send any event without harm.

View file

@ -114,6 +114,8 @@ export interface Canvas {
readonly values?: ReadonlyArray<number>
/** An image src shown where the canvas can't be drawn (a terminal, a shader that fails to compile). */
readonly poster?: string
/** Set by the prelude when the canvas has an `onPointer` handler: only then does it take pointer input. */
readonly pointer?: true
}
/** Where a child of a `stack` sits in it (widgets.md, Stack); ignored outside a stack. */
@ -170,7 +172,7 @@ export interface Rect {
readonly height: number
}
/** Pointer input on a canvas with an `id`: `move` only while down, at most once per frame. */
/** Pointer input on a canvas with `pointer: true`: `move` only while down, at most once per frame. */
export interface Pointer {
readonly kind: "down" | "move" | "up"
readonly x: number
@ -448,6 +450,7 @@ export const Node: Schema.Codec<Node> = Schema.Union([
),
values: optional(Schema.Array(Schema.Finite).check(Schema.isMaxLength(16))),
poster: optional(Schema.String),
pointer: optional(Schema.Literal(true)),
}).check(
Schema.makeFilter(
(canvas: { readonly wgsl?: string; readonly values?: ReadonlyArray<number> }) =>

View file

@ -54,7 +54,7 @@ function __widget_tree(root) {
const used = {}
const visit = (node, path) => {
if (!node || typeof node !== "object" || Array.isArray(node)) return node
const { key, id, onPress, onChange, onPointer, children, ...out } = node
const { key, id, pointer, onPress, onChange, onPointer, children, ...out } = node
const control = __WIDGET_CONTROLS[node.type]
if (control) {
const handler = node[control.handler]
@ -62,6 +62,8 @@ function __widget_tree(root) {
if (typeof handler === "function" || control.always || keyed) {
out.id = path
if (typeof handler === "function") handlers[path] = { event: control.event, handler, node: out }
// Only a canvas with a handler takes input; one with just a key has an id for `widget.draw`.
if (node.type === "canvas" && typeof handler === "function") out.pointer = true
if (keyed && canvases[String(key)] === undefined)
canvases[String(key)] = { id: path, wgsl: typeof node.wgsl === "string" }
}

File diff suppressed because one or more lines are too long

View file

@ -277,6 +277,7 @@ test("chart: bars grow in, then the canvas stops animating", async () => {
{
type: "canvas",
id: "0/1",
pointer: true,
height: 240,
fps: 60,
duration: 0.65,

View file

@ -190,7 +190,7 @@ test("a session draws with its current state and draw never changes it", async (
null,
)
await session.dispatch({ id: ":c", type: "pointer", value: { kind: "down", x: 50, y: 10, width: 200, height: 100 } })
expect(session.view).toEqual({ type: "canvas", id: ":c", height: 100, fps: 60 })
expect(session.view).toEqual({ type: "canvas", id: ":c", height: 100, fps: 60, pointer: true })
expect((await session.draw({ ...frame, id: ":c" })).commands).toEqual([["circle", 19800, 50, 4, { fill: "text" }]])
expect(session.state).toEqual({ x: 0.25, down: true })
})
@ -211,7 +211,7 @@ test("a stack layers its children, each placed with place and inset", async () =
expect(output.view).toEqual({
type: "stack",
children: [
{ type: "canvas", id: "0/:game", height: 200, place: "fill" },
{ type: "canvas", id: "0/:game", height: 200, place: "fill", pointer: true },
{ type: "text", text: "Score 3", place: "top-left", inset: 8 },
{ type: "button", id: "0/2", label: "Pause", place: "bottom-right", inset: { right: 12, bottom: 12 } },
],
@ -222,3 +222,24 @@ test("a stack layers its children, each placed with place and inset", async () =
expect(await invalid(`ui.stack([ui.text("x", { inset: -1 })])`)).toMatchObject({ kind: "invalid" })
expect(await invalid(`ui.stack([ui.text("x", { inset: { top: "4" } })])`)).toMatchObject({ kind: "invalid" })
})
test("only a canvas with onPointer takes input: the prelude marks it pointer: true", async () => {
const output = await step(
program(`
function view() {
return ui.stack([
ui.canvas({ key: "scene", height: 100 }),
ui.canvas({ height: 100, onPointer: (state) => state }),
ui.canvas({ height: 100, pointer: true }),
])
}
`),
{ phase: "init", data: null },
)
expect(output.view.type === "stack" && output.view.children).toEqual([
// A key gives an id for live widget.draw, not input; an author can't set pointer itself.
{ type: "canvas", id: "0/:scene", height: 100 },
{ type: "canvas", id: "0/1", height: 100, pointer: true },
{ type: "canvas", height: 100 },
])
})

View file

@ -212,7 +212,8 @@ type Node = (
| { type: "input"; id: string; label?: string; value: string; placeholder?: string; multiline?: boolean }
| { type: "divider" }
| { type: "canvas"; id?: string; height: number; width?: number; fps?: number; duration?: number; alt?: string;
poster?: string; wgsl?: string; values?: number[] } // values only with wgsl
poster?: string; wgsl?: string; values?: number[] // values only with wgsl
pointer?: true } // set by the prelude when it has onPointer: only then does it take input
| { type: "stack"; children: Node[] }
) & {
// Any node, as a child of a stack (see Stack); ignored elsewhere.
@ -250,11 +251,13 @@ over a canvas.
`widgets-fullscreen.md`), unless all its canvas children have `fill: false`, and its `fill` children take its full
size.
- **Pointer input:** the topmost child at the point that takes pointer input gets it: an interactive node (`button`,
`checkbox`, `slider`, `select`, `input`, a `card` or `carousel` with an `id`, a `canvas` with an `id`), or a container
`checkbox`, `slider`, `select`, `input`, a `card` or `carousel` with an `id`, a `canvas` with `pointer: true`), or a container
through one of its interactive descendants. Everything else lets it through to the layer below: `text`, `markdown`,
`image`, `divider`, a canvas without an `id`, and the empty parts of containers (they have no background). Pointer
`image`, `divider`, a canvas without `pointer: true`, and the empty parts of containers (they have no background). Pointer
capture and `close` work as anywhere else; a canvas's coordinates are its own box.
- **Order:** keyboard focus and screen readers follow the children's order.
- **Directions are physical:** `left`/`right` in `place` and the `left`/`right` sides of `inset` don't flip in a
right-to-left layout.
- **Clients that can't layer** (the TUI) show the children top to bottom in order, like a column, ignoring `place` and
`inset`.
@ -273,6 +276,11 @@ Everything else is the same for both: size and `width`, `fps` and `duration`, `a
where the canvas can't be drawn: the TUI, a shader that fails to compile), `fill` and full screen, `onPointer` and
pointer capture, `close`, and ids and keys. `fps` defaults to 0 for a 2D canvas and 60 for a GPU one.
A canvas takes input only with `pointer: true`, which the prelude sets when it has an `onPointer` handler. Without it (no
handler; a canvas with only a `key` has an `id` for live `widget.draw`, but no `pointer`) it sends no `pointer` events,
and in a stack taps pass through it to the layer below. It still gets `frame.pointer` in `draw` (hover and press visuals
of its own).
`draw` is pure: it can't change state. Animate with `frame.t`; keep anything that must persist in state via handlers.
Clients never call `draw`, or render a GPU canvas, while the canvas has a zero width or height (during layout, or
@ -321,7 +329,7 @@ type Gradient = { linear: [x1, y1, x2, y2]; stops: [offset: number, color: Color
| { radial: [cx, cy, r]; stops: [offset: number, color: Color][] }
```
Pointer input on a canvas with an `id` goes to its `onPointer` handler: clients send
Pointer input on a canvas with `pointer: true` goes to its `onPointer` handler: clients send
`{ id, type: "pointer", value: { kind: "down" | "move" | "up", x, y, width, height } }`, with `move` only while down
and at most once per frame. `width`/`height` are the canvas's logical size, so the handler can hit-test a layout that
follows the available width.