Skip to content

Commit edff460

Browse files
natemoo-reclaude
authored andcommitted
feat: emit OSC 22 pointer shapes in output when the terminal supports them (renderer-spec §7.9)
Elements declare open(id, { pointerShape }). While term.capabilities.pointerShape is true (raised by #132/#133 from the probe's OSC 22 query reply), each render resolves the shape under the pointer and appends `OSC 22 ; <shape> ST` to result.output when it changes. The host keeps writing one buffer (cowboyd on #101). The capability is the only gate; there is no createTerm option. Everything lives in TypeScript; the wasm module and the packed encoding are untouched, so there is no size or startup cost (#101 regressed createTerm ~20% and grew the bundle 8.3 KB). Without the capability the render path pays one boolean check and never reads pointerShape (a test pins this with a counting getter). With it, the directive walk runs only when the pointer is over something, and only a frame whose shape changed copies output to append the sequence. Resolution takes the last declaring id in Clay's pointer-over order: pre-order within the topmost layer the pointer reaches, so the innermost element wins and capture-mode floats hide what is beneath while passthrough floats defer to it. snapshot() records its declared shapes beside the packed bytes so pre-packed subtrees still participate. Values outside the kitty/CSS vocabulary are ignored (and rejected by validate()), so arbitrary strings never reach an OSC payload. Restore (dreyfus92 on #101): set-only OSC 22 cannot pop, so the emitted shape is the one piece of cross-frame state. It resets to "default" when the pointer leaves or is omitted, and update() returns the reset when the pointer-shape capability is withdrawn. Resize keeps it, since the terminal's pointer did not change.
1 parent c4487d7 commit edff460

6 files changed

Lines changed: 427 additions & 4 deletions

File tree

‎ops.ts‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
import type { PointerShape } from "./pointer-shape.ts";
2+
3+
export type { PointerShape };
4+
15
export type TransitionProperty =
26
| "x"
37
| "y"
@@ -410,6 +414,7 @@ export interface OpenElement {
410414
zIndex?: number;
411415
};
412416
transition?: Transition;
417+
pointerShape?: PointerShape;
413418
}
414419

415420
export type AttachPoint =
@@ -491,6 +496,7 @@ export interface Text {
491496
interface Snapshot {
492497
directive: typeof OP_SNAPSHOT;
493498
data: Uint8Array;
499+
shapes?: readonly (readonly [id: string, shape: string])[];
494500
}
495501

496502
export type Op = OpenElement | Text | CloseElement | Snapshot;
@@ -551,7 +557,19 @@ export function snapshot(ops: Op[]): Op {
551557
let size = packSize(ops);
552558
let buf = new ArrayBuffer(size);
553559
let words = pack(ops, buf, 0, size);
554-
return { directive: OP_SNAPSHOT, data: new Uint8Array(buf, 0, words * 4) };
560+
let data = new Uint8Array(buf, 0, words * 4);
561+
let shapes: (readonly [string, string])[] = [];
562+
for (let op of ops) {
563+
if (op.directive === OP_SNAPSHOT) {
564+
if (op.shapes) shapes.push(...op.shapes);
565+
} else if (
566+
op.directive === OP_OPEN_ELEMENT && op.pointerShape !== undefined
567+
) {
568+
shapes.push([op.id, op.pointerShape]);
569+
}
570+
}
571+
if (shapes.length === 0) return { directive: OP_SNAPSHOT, data };
572+
return { directive: OP_SNAPSHOT, data, shapes };
555573
}
556574

557575
const TP_X = 1;

‎pointer-shape.ts‎

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import type { Op } from "./ops.ts";
2+
3+
export const POINTER_SHAPES = [
4+
"default",
5+
"none",
6+
"context-menu",
7+
"help",
8+
"pointer",
9+
"progress",
10+
"wait",
11+
"cell",
12+
"crosshair",
13+
"text",
14+
"vertical-text",
15+
"alias",
16+
"copy",
17+
"move",
18+
"no-drop",
19+
"not-allowed",
20+
"grab",
21+
"grabbing",
22+
"e-resize",
23+
"n-resize",
24+
"ne-resize",
25+
"nw-resize",
26+
"s-resize",
27+
"se-resize",
28+
"sw-resize",
29+
"w-resize",
30+
"ew-resize",
31+
"ns-resize",
32+
"nesw-resize",
33+
"nwse-resize",
34+
"zoom-in",
35+
"zoom-out",
36+
] as const;
37+
38+
/**
39+
* A mouse pointer shape, named with the CSS `cursor` keyword vocabulary used
40+
* by the OSC 22 pointer shape protocol.
41+
*
42+
* @see {@link https://sw.kovidgoyal.net/kitty/pointer-shapes/ | kitty pointer shapes}
43+
*/
44+
export type PointerShape = typeof POINTER_SHAPES[number];
45+
46+
const KNOWN: ReadonlySet<string> = new Set(POINTER_SHAPES);
47+
48+
export function resolvePointerShape(
49+
ops: readonly Op[],
50+
over: readonly string[],
51+
): PointerShape {
52+
let best = -1;
53+
let shape: PointerShape = "default";
54+
for (let op of ops) {
55+
if ("data" in op) {
56+
if (!op.shapes) continue;
57+
for (let [id, value] of op.shapes) {
58+
if (!KNOWN.has(value)) continue;
59+
let i = over.lastIndexOf(id);
60+
if (i > best) {
61+
best = i;
62+
shape = value as PointerShape;
63+
}
64+
}
65+
} else if ("pointerShape" in op && op.pointerShape !== undefined) {
66+
if (!KNOWN.has(op.pointerShape)) continue;
67+
let i = over.lastIndexOf(op.id);
68+
if (i > best) {
69+
best = i;
70+
shape = op.pointerShape;
71+
}
72+
}
73+
}
74+
return shape;
75+
}
76+
77+
const encoder = new TextEncoder();
78+
79+
export function osc22(shape: PointerShape): Uint8Array {
80+
return encoder.encode(`\x1b]22;${shape}\x1b\\`);
81+
}

‎term.ts‎

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ import { type Op, pack } from "./ops.ts";
22
import { type BoundingBox, createTermNative } from "./term-native.ts";
33
import type { InputEvent } from "./input.ts";
44
import type { Capabilities, Rgb, TerminalInfo } from "./terminfo.ts";
5+
import {
6+
osc22,
7+
type PointerShape,
8+
resolvePointerShape,
9+
} from "./pointer-shape.ts";
510

611
export type { BoundingBox };
712

@@ -162,6 +167,7 @@ export interface Term {
162167

163168
export async function createTerm(options: TermOptions): Promise<Term> {
164169
let { width, height, terminfo } = options;
170+
let emittedShape: PointerShape = "default";
165171

166172
let currentCaps: RuntimeCapabilities = runtimeFromStatic(
167173
terminfo?.capabilities ?? {
@@ -224,9 +230,22 @@ export async function createTerm(options: TermOptions): Promise<Term> {
224230
native.length(native.statePtr),
225231
);
226232

227-
let current = new Set(
228-
options?.pointer ? native.getPointerOverIds() : [],
229-
);
233+
let overIds = options?.pointer ? native.getPointerOverIds() : [];
234+
let current = new Set(overIds);
235+
236+
if (currentCaps.pointerShape) {
237+
let shape: PointerShape = overIds.length > 0
238+
? resolvePointerShape(ops, overIds)
239+
: "default";
240+
if (shape !== emittedShape) {
241+
emittedShape = shape;
242+
let seq = osc22(shape);
243+
let joined = new Uint8Array(output.length + seq.length);
244+
joined.set(output);
245+
joined.set(seq, output.length);
246+
output = joined;
247+
}
248+
}
230249
let down = options?.pointer?.down ?? false;
231250
let events: PointerEvent[] = [];
232251

@@ -311,6 +330,14 @@ export async function createTerm(options: TermOptions): Promise<Term> {
311330
if (next.syncOutput !== currentCaps.syncOutput) {
312331
native.setCapabilities({ syncOutput: next.syncOutput });
313332
}
333+
if (
334+
currentCaps.pointerShape && !next.pointerShape &&
335+
emittedShape !== "default"
336+
) {
337+
emittedShape = "default";
338+
out.push(osc22("default"));
339+
}
340+
314341
currentCaps = next;
315342
if (bytes.length) out.push(bytes);
316343
}

0 commit comments

Comments
 (0)