78609c005e
* feat(terminal): add touch-scroll helper for xterm scrollback Pure helper that translates single-finger vertical drags into `terminal.scrollLines(n)` calls. Handles threshold gating so taps still reach xterm, ignores multi-touch, yields to horizontal-dominant drags. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com> * feat(terminal): wire mobile touch scrollback into passthrough terminal Adds an `enableTouchScroll` prop on PassthroughTerminal and a thin hook that calls `attachTouchScroll` while the terminal is mounted. The two mobile mount points (terminal panel + chat-panel passthrough embed) opt in; desktop callers are unaffected. Fixes the bug where xterm.js's canvas absorbed touch gestures and left the scrollback unreachable. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com> * test(terminal): mobile e2e for passthrough touch-scroll Pixel 5-emulated Playwright spec: produce 200 lines of output, simulate a single-finger down-swipe, assert `terminal.buffer.active.viewportY` moved into the scrollback, then swipe back to return. Extends the e2e buffer reader to expose viewportY alongside `__xtermReadBuffer`. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com> * test(terminal): relax mobile swipe-back assertion against late shell output Replace the exact `.toBe(bottomViewportY)` check with `.toBeGreaterThanOrEqual` so the test doesn't flake if shell echo arrives between the snapshot and the post-swipe read. xterm clamps at the buffer boundary, so overshooting back-to-bottom is impossible. Addresses PR #1046 Claude review thread 3293258085. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com> * docs(terminal): document useTouchScroll's isTerminalReady invariant Adds a comment next to the eslint-disable explaining why the deps array can safely omit terminalRef/xtermRef: useTerminalInit only flips isTerminalReady false→true once per mount. A future refactor that recreates the terminal mid-mount must add a new dep. Addresses PR #1046 Claude review thread 3293258175. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com> * chore(terminal): clear all exposed buffer-reader fields on dispose clearBufferReader now nulls `__xtermGetFontFamily` and `__xtermGetFontSize` alongside the buffer readers. The two predated this PR and were never cleaned up; keeping the teardown symmetric with exposeBufferReader prevents stale references if the container is ever reused. Addresses PR #1046 Claude review thread 3293274674. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com> * test(terminal): reset document.body between touch-scroll cases Adds an afterEach to wipe jsdom's body so orphan divs from one test don't bleed into the next. Avoids cross-test pollution if a future case queries document.body for containers. Addresses PR #1046 Claude review thread 3293274769. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com> --------- Co-authored-by: Kandev Agent <agent@kandev.dev> Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com>
129 lines
4.5 KiB
TypeScript
129 lines
4.5 KiB
TypeScript
"use client";
|
||
|
||
import { memo, useCallback, useEffect, useState } from "react";
|
||
import { PassthroughTerminal } from "../passthrough-terminal";
|
||
import { setActiveTerminalSender } from "@/lib/terminal/mobile-active-terminal";
|
||
import { sendShellInput } from "@/lib/terminal/send-shell-input";
|
||
import { MobileTerminalsPicker } from "./mobile-terminals-section";
|
||
import { MobileTerminalsProvider, useMobileTerminalsContext } from "./mobile-terminals-context";
|
||
import type { Terminal as XtermTerminal } from "@xterm/xterm";
|
||
import type { Terminal } from "@/hooks/domains/session/use-terminals";
|
||
|
||
function TerminalSlot({
|
||
terminal,
|
||
sessionId,
|
||
environmentId,
|
||
isActive,
|
||
}: {
|
||
terminal: Terminal;
|
||
sessionId: string;
|
||
environmentId: string | null;
|
||
isActive: boolean;
|
||
}) {
|
||
// Track xterm + ws in state so the registration / data-routing effects
|
||
// re-run once each is ready (PassthroughTerminal initialises both
|
||
// asynchronously when the container starts at 0×0). A ref-based dep would
|
||
// silently miss those paths on first mount.
|
||
const [xterm, setXterm] = useState<XtermTerminal | null>(null);
|
||
const [ws, setWs] = useState<WebSocket | null>(null);
|
||
|
||
const handleWsReady = useCallback((nextWs: WebSocket) => {
|
||
setWs(nextWs);
|
||
// Drop the registry sender if this socket closes — otherwise the key-bar
|
||
// would route taps into a dead WS during reconnect, instead of falling
|
||
// through to sendShellInput's WS-fallback path.
|
||
nextWs.addEventListener(
|
||
"close",
|
||
() => setWs((current) => (current === nextWs ? null : current)),
|
||
{ once: true },
|
||
);
|
||
}, []);
|
||
|
||
// Forward OS-keyboard input through sendShellInput so the key-bar's sticky
|
||
// Ctrl/Shift modifiers are applied before the bytes hit the wire. Without
|
||
// this, AttachAddon would auto-send raw onData and modifiers would be a
|
||
// no-op for OS-keyboard typing on mobile.
|
||
useEffect(() => {
|
||
if (!isActive || !xterm) return;
|
||
const disposable = xterm.onData((data) => sendShellInput(sessionId, data));
|
||
return () => disposable.dispose();
|
||
}, [isActive, xterm, sessionId]);
|
||
|
||
// Register the active key-bar sender only while the dedicated WS is open.
|
||
// sendShellInput's fallback path runs whenever no sender is registered, so
|
||
// skipping registration during connect/reconnect lets the key-bar still
|
||
// dispatch instead of silently dropping keystrokes.
|
||
useEffect(() => {
|
||
if (!isActive || !ws || ws.readyState !== WebSocket.OPEN) return;
|
||
const sender = (data: string) => ws.send(new TextEncoder().encode(data));
|
||
setActiveTerminalSender(sender);
|
||
return () => setActiveTerminalSender(null);
|
||
}, [isActive, terminal.id, ws]);
|
||
|
||
return (
|
||
<div className={`absolute inset-0 ${isActive ? "block" : "hidden"}`}>
|
||
<PassthroughTerminal
|
||
mode="shell"
|
||
environmentId={environmentId}
|
||
terminalId={terminal.id}
|
||
label={terminal.label}
|
||
autoFocus={isActive}
|
||
disableWebgl
|
||
manualInputRouting
|
||
enableTouchScroll
|
||
onXtermReady={setXterm}
|
||
onWsReady={handleWsReady}
|
||
/>
|
||
</div>
|
||
);
|
||
}
|
||
|
||
function MobileTerminalPaneInner({ sessionId }: { sessionId: string | null }) {
|
||
const { terminals, terminalTabValue, environmentId } = useMobileTerminalsContext();
|
||
const activeId = terminals.find((t) => t.id === terminalTabValue)?.id ?? terminals[0]?.id;
|
||
|
||
if (!sessionId || !environmentId) {
|
||
return (
|
||
<div className="flex-1 flex items-center justify-center text-xs text-muted-foreground">
|
||
Terminal unavailable — no active session.
|
||
</div>
|
||
);
|
||
}
|
||
|
||
return (
|
||
<div className="flex-1 min-h-0 flex flex-col">
|
||
<div className="flex items-center px-1 py-2 border-b border-border">
|
||
<MobileTerminalsPicker sessionId={sessionId} fullWidth />
|
||
</div>
|
||
<div className="relative flex-1 min-h-0">
|
||
{terminals.length === 0 && (
|
||
<div className="absolute inset-0 flex items-center justify-center text-xs text-muted-foreground">
|
||
Starting terminal…
|
||
</div>
|
||
)}
|
||
{terminals.map((t) => (
|
||
<TerminalSlot
|
||
key={t.id}
|
||
terminal={t}
|
||
sessionId={sessionId}
|
||
environmentId={environmentId}
|
||
isActive={t.id === activeId}
|
||
/>
|
||
))}
|
||
</div>
|
||
</div>
|
||
);
|
||
}
|
||
|
||
export const MobileTerminalPane = memo(function MobileTerminalPane({
|
||
sessionId,
|
||
}: {
|
||
sessionId: string | null;
|
||
}) {
|
||
return (
|
||
<MobileTerminalsProvider sessionId={sessionId}>
|
||
<MobileTerminalPaneInner sessionId={sessionId} />
|
||
</MobileTerminalsProvider>
|
||
);
|
||
});
|