@devries-cameron20 / t3code-1 / commits / d8c7855a88
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(web): terminal mouse reports hit the drawn cell (#37148)

Co-authored-by: Julius Marminge <julius0216@outlook.com>

Wout Stiens committed Nov 15, 2026 at 11:30 UTC d8c7855a884c58715c2d66ca82a3334bd6bc398c
3 files changed +219 -10
apps/web/src/terminal/ghostty/core.test.ts
+55
@@ -485,3 +485,58 @@ describe("GhosttyTerminalCore scrollback", () => {
485 }
486 });
487 });
488 +
489 +describe("GhosttyTerminalCore mouse encoding", () => {
490 + it("reports the cell drawn under the pointer on a fractional grid", async () => {
491 + // The default 13px font measures a fractional 7.82666015625px cell; rows are 18px.
492 + const cellWidth = 7.82666015625;
493 + const cellHeight = 18;
494 + const core = await GhosttyTerminalCore.create(
495 + 54,
496 + 15,
497 + cellWidth,
498 + cellHeight,
499 + {
500 + foreground: { r: 255, g: 255, b: 255 },
501 + background: { r: 0, g: 0, b: 0 },
502 + cursor: { r: 255, g: 255, b: 255 },
503 + },
504 + () => {},
505 + );
506 + const mouse = (
507 + x: number,
508 + y: number,
509 + paddingTop: number,
510 + screenWidth = 437,
511 + action: "press" | "release" = "press",
512 + ) =>
513 + core.encodeMouse({
514 + action,
515 + button: 1,
516 + mods: 0,
517 + x,
518 + y,
519 + screenWidth,
520 + screenHeight: 279,
521 + cellWidth,
522 + cellHeight,
523 + paddingLeft: 4,
524 + paddingRight: 4,
525 + paddingTop,
526 + paddingBottom: 279 - paddingTop - 15 * cellHeight,
527 + anyButtonPressed: true,
528 + });
529 + try {
530 + core.write("\x1b[?1002h\x1b[?1006h");
531 + expect(mouse(218.5, 140, 4)).toBe("\x1b[<0;28;8M");
532 + expect(mouse(4 + 53.5 * cellWidth, 140, 4)).toBe("\x1b[<0;54;8M");
533 + expect(mouse(10, 4.5 + cellHeight + 0.2, 4.5)).toBe("\x1b[<0;1;2M");
534 + expect(mouse(304, 140, 4, 305)).toBe("\x1b[<0;37;8M");
535 + expect(mouse(500, 140, 4, 305, "release")).toBe("\x1b[<0;37;8m");
536 + core.write("\x1b[?1016h");
537 + expect(mouse(218.5, 22, 4.5)).toBe("\x1b[<0;215;17M");
538 + } finally {
539 + core.dispose();
540 + }
541 + });
542 +});
apps/web/src/terminal/ghostty/core.ts
+54 -10
@@ -165,6 +165,31 @@ export interface GhosttyMouseInput {
165 const decoder = new TextDecoder("utf-8", { ignoreBOM: true });
166 const encoder = new TextEncoder();
167
168 +// The encoder only takes whole-pixel cells, so cell reports rescale the pointer onto that grid.
169 +function encoderAxis(
170 + position: number,
171 + screen: number,
172 + cell: number,
173 + paddingStart: number,
174 + paddingEnd: number,
175 + pixelReports: boolean,
176 +) {
177 + const wholeCell = Math.max(1, Math.round(cell));
178 + if (pixelReports) {
179 + return { position, screen, cell: wholeCell, paddingStart, paddingEnd };
180 + }
181 + const scale = wholeCell / cell;
182 + const start = Math.round(paddingStart);
183 + const end = Math.round(paddingEnd);
184 + return {
185 + position: Math.max(0, start + (position - paddingStart) * scale),
186 + screen: start + end + Math.floor((screen - paddingStart - paddingEnd) / cell) * wholeCell,
187 + cell: wholeCell,
188 + paddingStart: start,
189 + paddingEnd: end,
190 + };
191 +}
192 +
193 function blend(foreground: GhosttyColor, background: GhosttyColor): GhosttyColor {
194 const channel = (front: number, back: number) => Math.floor((front * 155 + back * 100) / 255);
195 return {
@@ -638,18 +663,37 @@ export class GhosttyTerminalCore {
663 this.terminal,
664 );
665
666 + // SGR pixel reports (DEC 1016) must name surface pixels, so they skip the rescale.
667 + const pixelReports = this.modeEnabled(1016);
668 + const x = encoderAxis(
669 + input.x,
670 + input.screenWidth,
671 + input.cellWidth,
672 + input.paddingLeft,
673 + input.paddingRight,
674 + pixelReports,
675 + );
676 + const y = encoderAxis(
677 + input.y,
678 + input.screenHeight,
679 + input.cellHeight,
680 + input.paddingTop,
681 + input.paddingBottom,
682 + pixelReports,
683 + );
684 +
685 const sizeLayout = this.runtime.layout("GhosttyMouseEncoderSize");
686 const size = this.runtime.alloc(sizeLayout.size);
687 for (const [field, value] of [
688 ["size", sizeLayout.size],
645 - ["screen_width", input.screenWidth],
646 - ["screen_height", input.screenHeight],
647 - ["cell_width", input.cellWidth],
648 - ["cell_height", input.cellHeight],
649 - ["padding_top", input.paddingTop],
650 - ["padding_bottom", input.paddingBottom],
651 - ["padding_right", input.paddingRight],
652 - ["padding_left", input.paddingLeft],
689 + ["screen_width", x.screen],
690 + ["screen_height", y.screen],
691 + ["cell_width", x.cell],
692 + ["cell_height", y.cell],
693 + ["padding_top", y.paddingStart],
694 + ["padding_bottom", y.paddingEnd],
695 + ["padding_right", x.paddingEnd],
696 + ["padding_left", x.paddingStart],
697 ] as const) {
698 this.runtime.setField(size, "GhosttyMouseEncoderSize", field, Math.max(0, Math.round(value)));
699 }
@@ -675,8 +719,8 @@ export class GhosttyTerminalCore {
719 const positionLayout = this.runtime.layout("GhosttyMousePosition");
720 const position = this.runtime.alloc(positionLayout.size);
721 const positionView = this.runtime.view(position, positionLayout.size);
678 - positionView.setFloat32(positionLayout.fields.x!.offset, input.x, true);
679 - positionView.setFloat32(positionLayout.fields.y!.offset, input.y, true);
722 + positionView.setFloat32(positionLayout.fields.x!.offset, x.position, true);
723 + positionView.setFloat32(positionLayout.fields.y!.offset, y.position, true);
724 this.runtime.call("ghostty_mouse_event_set_position", this.mouseEvent, position);
725 this.runtime.free(position, positionLayout.size);
726
scripts/scenarios/terminal-drags-tmux-pane-divider.e2e.ts new
+110
@@ -0,0 +1,110 @@
1 +/// <reference lib="dom" />
2 +// The DOM lib types the function this scenario runs in the page.
3 +import { expect } from "e2e";
4 +import * as Effect from "effect/Effect";
5 +import * as FileSystem from "effect/FileSystem";
6 +
7 +import { run } from "./shell.ts";
8 +import { runPromise, test, WEB } from "./t3.ts";
9 +
10 +const SOCKET = `t3-scenario-divider-${process.pid}`;
11 +
12 +const tmux = (...args: ReadonlyArray<string>) =>
13 + runPromise(
14 + run("tmux", ["-L", SOCKET, ...args], {
15 + env: { PATH: process.env.PATH ?? "", HOME: process.env.HOME ?? "" },
16 + allowExit: [0, 1],
17 + }),
18 + );
19 +
20 +const paneWidths = async () =>
21 + (await tmux("list-panes", "-F", "#{pane_width}")).stdout.trim().split("\n").filter(Boolean);
22 +
23 +test(
24 + "Dragging a tmux pane divider in the web terminal resizes the panes",
25 + WEB,
26 + async ({ t3, app, browser, screen }) => {
27 + const project = await t3.project("demo");
28 + const thread = await t3.thread({ project, title: "Panes", messages: ["hello"] });
29 + const read = (file: string) =>
30 + runPromise(
31 + Effect.gen(function* () {
32 + const fs = yield* FileSystem.FileSystem;
33 + return yield* fs.readFileString(`${project}/${file}`);
34 + }).pipe(Effect.orElseSucceed(() => "")),
35 + );
36 + await t3.open(thread);
37 +
38 + await screen.getByRole("button", "Toggle terminal drawer").tap();
39 + const terminal = screen.getByLabel("Terminal input");
40 +
41 + // Input sent before the shell runs is dropped, so retype until it lands.
42 + await expect
43 + .poll(
44 + async () => {
45 + await terminal.focus();
46 + await browser.keyboard.type(`echo ready > '${project}/shell-ready'`);
47 + await browser.keyboard.press("Enter");
48 + return read("shell-ready");
49 + },
50 + { timeout: 30_000 },
51 + )
52 + .toBe("ready\n");
53 +
54 + try {
55 + await terminal.focus();
56 + await browser.keyboard.type(
57 + `tmux -L ${SOCKET} -f /dev/null new-session \\; set -g mouse on \\; set -g status off \\; split-window -h -l 30%`,
58 + );
59 + await browser.keyboard.press("Enter");
60 + await expect.poll(async () => (await paneWidths()).length, { timeout: 15_000 }).toBe(2);
61 + const [leftWidth] = await paneWidths();
62 +
63 + // Find the divider tmux drew from the canvas pixels of an empty row, not from cell math.
64 + const findDivider = () =>
65 + browser.evaluate(() => {
66 + const input = document.querySelector('textarea[aria-label="Terminal input"]');
67 + const canvas = input?.parentElement?.querySelector("canvas");
68 + const context = canvas?.getContext("2d");
69 + if (!canvas || !context) return null;
70 + const bounds = canvas.getBoundingClientRect();
71 + const ratio = canvas.width / bounds.width;
72 + const row = Math.floor(canvas.height * 0.75);
73 + const pixels = context.getImageData(0, row, canvas.width, 1).data;
74 + const lit: number[] = [];
75 + for (let x = 0; x < canvas.width; x += 1) {
76 + const offset = x * 4;
77 + const difference =
78 + Math.abs(pixels[offset]! - pixels[0]!) +
79 + Math.abs(pixels[offset + 1]! - pixels[1]!) +
80 + Math.abs(pixels[offset + 2]! - pixels[2]!);
81 + if (difference > 90) lit.push(x);
82 + }
83 + const first = lit[0];
84 + const last = lit.at(-1);
85 + if (first === undefined || last === undefined || last - first > 4 * ratio) return null;
86 + return {
87 + x: bounds.left + (first + last + 1) / 2 / ratio,
88 + y: bounds.top + (row + 0.5) / ratio,
89 + };
90 + });
91 + await expect.poll(findDivider, { timeout: 10_000 }).not.toBeNull();
92 + const divider = (await findDivider())!;
93 + await app.screenshot("before-drag");
94 +
95 + await browser.mouse.move(divider.x, divider.y);
96 + await browser.mouse.down();
97 + for (let step = 1; step <= 8; step += 1) {
98 + await browser.mouse.move(divider.x - step * 10, divider.y);
99 + }
100 + await browser.mouse.up();
101 +
102 + await app.screenshot("after-drag");
103 + await expect
104 + .poll(async () => Number((await paneWidths())[0]), { timeout: 5_000 })
105 + .toBeLessThan(Number(leftWidth));
106 + } finally {
107 + await tmux("kill-server");
108 + }
109 + },
110 +);