Skip to content

The agent that grows with you

fix(dashboard): enable touch scrollback in chat

andyst-dev1ac3f97fbe09
diff --git a/web/src/lib/chat-panel-collapsed.ts b/web/src/lib/chat-panel-collapsed.ts
new file mode 100644
index 00000000000..da03a70bcfb
--- /dev/null
+++ b/web/src/lib/chat-panel-collapsed.ts
@@ -0,0 +1,21 @@
+/**
+ * Collapse state for the desktop chat side panel (model + sessions),
+ * persisted in localStorage so the choice survives reloads.
+ *
+ * Pure helpers so ChatPage stays under its FILE_LINES cap and the
+ * persistence stays testable without React.
+ */
+
+const CHAT_PANEL_COLLAPSED_KEY = "hermes-chat-panel-collapsed";
+
+/** The panel's collapsed flag remembered from the previous visit. */
+export function readChatPanelCollapsed(): boolean {
+ return localStorage.getItem(CHAT_PANEL_COLLAPSED_KEY) === "1";
+}
+
+/** Flip the collapsed flag and persist the new choice. */
+export function toggleChatPanelCollapsed(previous: boolean): boolean {
+ const next = !previous;
+ localStorage.setItem(CHAT_PANEL_COLLAPSED_KEY, next ? "1" : "0");
+ return next;
+}
diff --git a/web/src/lib/terminal-touch-scroll.test.ts b/web/src/lib/terminal-touch-scroll.test.ts
new file mode 100644
index 00000000000..c22406830c8
--- /dev/null
+++ b/web/src/lib/terminal-touch-scroll.test.ts
@@ -0,0 +1,70 @@
+import { describe, expect, it, vi } from "vitest";
+
+import { installTerminalTouchScroll } from "./terminal-touch-scroll";
+
+function touchEvent(type: string, y: number): TouchEvent {
+ const ev = new Event(type, { bubbles: true, cancelable: true }) as TouchEvent;
+ const touch = { clientY: y } as Touch;
+
+ Object.defineProperty(ev, "touches", { value: type === "touchend" ? [] : [touch] });
+ Object.defineProperty(ev, "changedTouches", { value: [touch] });
+
+ return ev;
+}
+
+function term(viewportY: number, baseY: number) {
+ const terminal = {
+ buffer: { active: { baseY, viewportY } },
+ scrollLines: vi.fn((amount: number) => {
+ const active = terminal.buffer.active;
+ active.viewportY = Math.max(0, Math.min(active.baseY, active.viewportY + amount));
+ }),
+ };
+
+ return terminal;
+}
+
+describe("installTerminalTouchScroll", () => {
+ it("converts one-finger vertical drags into xterm scrollback lines", () => {
+ const host = new EventTarget() as HTMLElement;
+ const terminal = term(20, 100);
+ const cleanup = installTerminalTouchScroll(host, terminal);
+
+ host.dispatchEvent(touchEvent("touchstart", 200));
+ const move = touchEvent("touchmove", 164);
+ host.dispatchEvent(move);
+
+ expect(terminal.scrollLines).toHaveBeenCalledWith(2);
+ expect(move.defaultPrevented).toBe(true);
+
+ cleanup();
+ });
+
+ it("lets the outer page handle touch drags at the terminal boundary", () => {
+ const host = new EventTarget() as HTMLElement;
+ const terminal = term(0, 100);
+ const cleanup = installTerminalTouchScroll(host, terminal);
+
+ host.dispatchEvent(touchEvent("touchstart", 100));
+ const move = touchEvent("touchmove", 136);
+ host.dispatchEvent(move);
+
+ expect(terminal.scrollLines).not.toHaveBeenCalled();
+ expect(move.defaultPrevented).toBe(false);
+
+ cleanup();
+ });
+
+ it("removes listeners on cleanup", () => {
+ const host = new EventTarget() as HTMLElement;
+ const terminal = term(20, 100);
+ const cleanup = installTerminalTouchScroll(host, terminal);
+
+ cleanup();
+
+ host.dispatchEvent(touchEvent("touchstart", 200));
+ host.dispatchEvent(touchEvent("touchmove", 164));
+
+ expect(terminal.scrollLines).not.toHaveBeenCalled();
+ });
+});
diff --git a/web/src/lib/terminal-touch-scroll.ts b/web/src/lib/terminal-touch-scroll.ts
new file mode 100644
index 00000000000..818ed834e0e
--- /dev/null
+++ b/web/src/lib/terminal-touch-scroll.ts
@@ -0,0 +1,114 @@
+interface ScrollableTerminal {
+ buffer: {
+ active: {
+ baseY: number;
+ viewportY: number;
+ };
+ };
+ scrollLines: (amount: number) => void;
+}
+
+const TOUCH_SCROLL_PX_PER_LINE = 18;
+const TOUCH_SCROLL_MIN_PX = 2;
+const CAPTURE = { capture: true } as const;
+const PASSIVE_CAPTURE = { capture: true, passive: true } as const;
+const ACTIVE_CAPTURE = { capture: true, passive: false } as const;
+
+function firstTouchY(ev: TouchEvent): number | null {
+ const touch = ev.touches[0] ?? ev.changedTouches[0];
+
+ return touch ? touch.clientY : null;
+}
+
+function canScrollTerminal(term: ScrollableTerminal, amount: number): boolean {
+ const { baseY, viewportY } = term.buffer.active;
+
+ if (amount < 0) {
+ return viewportY > 0;
+ }
+
+ if (amount > 0) {
+ return viewportY < baseY;
+ }
+
+ return false;
+}
+
+/**
+ * Add touch-drag scrollback support for dashboard xterm panes.
+ *
+ * xterm.js handles wheel/trackpad scrollback for the embedded TUI, but iPadOS
+ * WebKit does not translate a finger drag inside the terminal into wheel events
+ * or native scrollbar dragging. Mirror the wheel path by converting vertical
+ * touch movement into `scrollLines()` while there is xterm scrollback available;
+ * at the top/bottom boundary we leave the event alone so the outer dashboard
+ * page can still move above the software keyboard.
+ */
+export function installTerminalTouchScroll(host: HTMLElement, term: ScrollableTerminal): () => void {
+ let lastY: number | null = null;
+ let residualPx = 0;
+
+ const reset = () => {
+ lastY = null;
+ residualPx = 0;
+ };
+
+ const onTouchStart = (ev: TouchEvent) => {
+ if (ev.touches.length !== 1) {
+ reset();
+ return;
+ }
+
+ lastY = firstTouchY(ev);
+ residualPx = 0;
+ };
+
+ const onTouchMove = (ev: TouchEvent) => {
+ if (ev.touches.length !== 1 || lastY === null) {
+ reset();
+ return;
+ }
+
+ const nextY = firstTouchY(ev);
+ if (nextY === null) {
+ reset();
+ return;
+ }
+
+ const deltaPx = lastY - nextY;
+ lastY = nextY;
+
+ if (Math.abs(deltaPx) < TOUCH_SCROLL_MIN_PX) {
+ return;
+ }
+
+ residualPx += deltaPx;
+ const lines = residualPx / TOUCH_SCROLL_PX_PER_LINE;
+ const wholeLines = lines < 0 ? Math.ceil(lines) : Math.floor(lines);
+
+ if (wholeLines === 0) {
+ return;
+ }
+
+ if (!canScrollTerminal(term, wholeLines)) {
+ return;
+ }
+
+ residualPx -= wholeLines * TOUCH_SCROLL_PX_PER_LINE;
+ term.scrollLines(wholeLines);
+ ev.preventDefault();
+ ev.stopPropagation();
+ };
+
+ host.addEventListener("touchstart", onTouchStart, PASSIVE_CAPTURE);
+ host.addEventListener("touchmove", onTouchMove, ACTIVE_CAPTURE);
+ host.addEventListener("touchcancel", reset, CAPTURE);
+ host.addEventListener("touchend", reset, CAPTURE);
+
+ return () => {
+ host.removeEventListener("touchstart", onTouchStart, PASSIVE_CAPTURE);
+ host.removeEventListener("touchmove", onTouchMove, ACTIVE_CAPTURE);
+ host.removeEventListener("touchcancel", reset, CAPTURE);
+ host.removeEventListener("touchend", reset, CAPTURE);
+ };
+}
diff --git a/web/src/pages/ChatPage.tsx b/web/src/pages/ChatPage.tsx
index 328b99ba38b..9baf9304985 100644
--- a/web/src/pages/ChatPage.tsx
+++ b/web/src/pages/ChatPage.tsx
@@ -37,6 +37,7 @@ import { useI18n } from "@/i18n";
import { api } from "@/lib/api";
import { readStoredWorkspace, writeStoredWorkspace } from "@/lib/chat-workspaces";
import { latchChatActivation } from "@/lib/chat-activation";
+import { readChatPanelCollapsed, toggleChatPanelCollapsed } from "@/lib/chat-panel-collapsed";
import { copyTextToClipboard } from "@/lib/clipboard";
import { normalizeSessionTitle } from "@/lib/chat-title";
import { createPtyCompositionForwarder } from "@/lib/pty-composition";
@@ -76,6 +77,7 @@ import {
parseResumeControlMessage,
shouldFollowPtyOutput,
} from "@/lib/pty-scroll";
+import { installTerminalTouchScroll } from "@/lib/terminal-touch-scroll";
import { uploadChatImage } from "@/lib/chatImagePaste";
import { attachChatImageDropListeners } from "@/lib/chat-image-drop";
import { maybeReloadForLoopbackWsAuthFailure } from "@/lib/dashboard-auth-reload";
@@ -198,8 +200,7 @@ export default function ChatPage({ isActive = true }: { isActive?: boolean }) {
const connectingTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
const ptyInputLineRef = useRef("");
const mobileReplacementInputUntilRef = useRef(0);
- const [ptyState, setPtyState] =
- useState<PtyConnectionState>("connecting");
+ const [ptyState, setPtyState] = useState<PtyConnectionState>("connecting");
const ptyStateRef = useRef<PtyConnectionState>("connecting");
// True until the first real PTY payload arrives for a resumed session.
// Covers the blank terminal + blinking-cursor window so users don't think
@@ -288,14 +289,10 @@ export default function ChatPage({ isActive = true }: { isActive?: boolean }) {
// Collapse toggle for the desktop chat side panel (model + sessions),
// persisted in localStorage so the choice survives reloads.
const [chatPanelCollapsed, setChatPanelCollapsed] = useState(
- () => localStorage.getItem("hermes-chat-panel-collapsed") === "1",
+ readChatPanelCollapsed,
);
const toggleChatPanel = useCallback(() => {
- setChatPanelCollapsed((prev) => {
- const next = !prev;
- localStorage.setItem("hermes-chat-panel-collapsed", next ? "1" : "0");
- return next;
- });
+ setChatPanelCollapsed(toggleChatPanelCollapsed);
}, []);
const { setEnd, setTitle } = usePageHeader();
const [sessionTitleState, setSessionTitleState] = useState<{
@@ -783,6 +780,7 @@ export default function ChatPage({ isActive = true }: { isActive?: boolean }) {
ev.stopPropagation();
return false;
});
+ const cleanupTouchScroll = installTerminalTouchScroll(host, term);
const unicode11 = new Unicode11Addon();
term.loadAddon(unicode11);
@@ -1557,6 +1555,7 @@ export default function ChatPage({ isActive = true }: { isActive?: boolean }) {
onScrollDisposable?.dispose();
mobileInputCleanup?.();
compositionForwarder.dispose();
+ cleanupTouchScroll();
handleBrowserDropCleanup();
if (metricsDebounce) clearTimeout(metricsDebounce);
window.removeEventListener("resize", scheduleSyncTerminalMetrics);