diff --git a/app/apps/terminal/terminal-app.worker.ts b/app/apps/terminal/terminal-app.worker.ts index a8fc6752..c5486312 100644 --- a/app/apps/terminal/terminal-app.worker.ts +++ b/app/apps/terminal/terminal-app.worker.ts @@ -297,6 +297,18 @@ let controlsInitialized = false; // created, so they get "now". const sessionRecency = new Map(); +// When each session's app last produced output, as LOCAL receive time of the +// server's `activity` push (phone clock — the wire timestamp is host-clock +// and can't be compared against Date.now() across machines). Keys are +// recencyKey(connectionId, socket). A session is "active" (hub rows show an +// animated indicator) while an activity push arrived within the last +// ACTIVITY_ACTIVE_MS; the wrapper reports at most one per 2s while output +// continues, so 5s of slack keeps the indicator lit through the gaps. +const sessionActivity = new Map(); +const ACTIVITY_ACTIVE_MS = 5_000; +// Alternation period of the hub's activity indicator. +const HUB_ANIMATION_STEP_MS = 800; + function recencyKey(connectionId: string, socket: string): string { return `${connectionId}\n${socket}`; } @@ -360,6 +372,7 @@ global.onmessage = (event: { data: WorkerAppMessage }) => { } if (window.foreground && window.kind === "hub") window.sessionOrder = []; if (window.foreground) renderAndSubmit(window, 0); + updateHubAnimation(); break; } case "screen": @@ -373,6 +386,7 @@ global.onmessage = (event: { data: WorkerAppMessage }) => { renderAndSubmit(window, 0); } } + updateHubAnimation(); break; case "tool-call": { const callId = message.callId; @@ -585,6 +599,9 @@ function startControl(control: ControlConnection): void { client.onTitle((socket) => { noteSessionUpdated(connectionId, socket); }), + client.onActivity((socket) => { + noteSessionActivity(connectionId, socket); + }), ); client.start(); } @@ -693,6 +710,63 @@ function renderHubWindows(): void { } } +// Hub activity animation: while a foregrounded hub lists at least one active +// session, a timer re-renders it so the per-row indicator alternates. The +// timer stops itself once every session's activity ages out (its final tick +// renders the rows indicator-free) or the hub leaves the foreground. +let hubAnimationPhase = 0; +let hubAnimationTimer: ReturnType | null = null; + +function isSessionActive(connectionId: string, socket: string): boolean { + const at = sessionActivity.get(recencyKey(connectionId, socket)); + return at !== undefined && Date.now() - at < ACTIVITY_ACTIVE_MS; +} + +function hubAnimationShouldRun(): boolean { + if (!screenOn) return false; + let hubVisible = false; + for (const window of windows.values()) { + if (window.kind === "hub" && window.foreground) hubVisible = true; + } + if (!hubVisible) return false; + const now = Date.now(); + for (const at of sessionActivity.values()) { + if (now - at < ACTIVITY_ACTIVE_MS) return true; + } + return false; +} + +/** Start or stop the animation timer to match the current state. */ +function updateHubAnimation(): void { + if (hubAnimationShouldRun()) { + if (hubAnimationTimer) return; + hubAnimationTimer = setInterval(() => { + hubAnimationPhase = (hubAnimationPhase + 1) % 2; + if (!hubAnimationShouldRun() && hubAnimationTimer) { + clearInterval(hubAnimationTimer); + hubAnimationTimer = null; + } + // Render even on the stopping tick, to clear expired indicators. + renderHubWindows(); + }, HUB_ANIMATION_STEP_MS); + } else if (hubAnimationTimer) { + clearInterval(hubAnimationTimer); + hubAnimationTimer = null; + } +} + +/** An activity push arrived for a session: mark it and (re)start the animation. */ +function noteSessionActivity(connectionId: string, socket: string): void { + const key = recencyKey(connectionId, socket); + const wasActive = isSessionActive(connectionId, socket); + sessionActivity.set(key, Date.now()); + sessionRecency.set(key, Date.now()); + updateHubAnimation(); + // A session just turned active: show its indicator now rather than up to + // one animation step later. + if (!wasActive) renderHubWindows(); +} + function createViewWindow( windowId: string, surfaceId: string, @@ -1017,6 +1091,8 @@ type HubItem = { label: string; /** Group heading (host name): drawn differently and skipped by selection. */ heading?: boolean; + /** Session with recent output: an animated indicator marks the row. */ + active?: boolean; onSelect?: () => void; }; @@ -1085,6 +1161,7 @@ function hubSessionItems(window: HubWindow): HubItem[] { const openWindowId = viewWindowIdForSocket(control.config.id, session.socket); items.push({ label: openWindowId ? `${sessionLabel(session)} [open]` : sessionLabel(session), + active: isSessionActive(control.config.id, session.socket), onSelect: () => { const windowId = viewWindowIdForSocket(control.config.id, session.socket); if (windowId) { @@ -1442,6 +1519,19 @@ function paintHub(window: HubWindow): GrayImage { drawSelectionHighlight(image, 20, y - 2, window.viewportWidth - 40, hubRowH - 1, window.focused, 8); } image.drawText(chromeFont(), 32, y + 2, item.label, selected ? 255 : 200); + if (item.active) { + // Activity indicator in the gutter left of the label, alternating + // filled/outline each animation step (drawn shapes, not a font glyph, + // so the two states render distinctly in every UI font). + const size = 6; + const iy = y + 2 + Math.max(0, ((font.lineHeight - size) / 2) | 0); + const value = selected ? 255 : 200; + if (hubAnimationPhase === 0) { + image.fillRect(22, iy, size, size, value); + } else { + image.drawRect(22, iy, size, size, value); + } + } } if (items.length > visibleRowCount) { drawListScrollbar( diff --git a/app/native/g2mirror-client.ts b/app/native/g2mirror-client.ts index c9ec9a78..b05112fc 100644 --- a/app/native/g2mirror-client.ts +++ b/app/native/g2mirror-client.ts @@ -25,6 +25,13 @@ export type G2MirrorSession = { cwdHint: string; /** Unix epoch ms of the terminal's last bell, or null if none observed. */ lastBellAt: number | null; + /** + * Unix epoch ms (host clock) of the app's last output, or null if none + * observed. Fresh to within the wrapper's 2s activity reporting interval + * while we're connected; compare against other host timestamps, not the + * phone clock (see the worker's local-receive-time activity tracking). + */ + lastOutputAt: number | null; /** Window title the app last set (xterm OSC 0/2), or null if none observed. */ title: string | null; }; @@ -95,6 +102,7 @@ export class G2MirrorClient { private readonly sessionAttachedListeners = new Set<(command: string) => void>(); private readonly sessionDetachedListeners = new Set<(reason: string) => void>(); private readonly bellListeners = new Set<(socket: string, lastBellAtMs: number) => void>(); + private readonly activityListeners = new Set<(socket: string, lastOutputAtMs: number) => void>(); private readonly titleListeners = new Set<(socket: string, title: string) => void>(); // Launch requests awaiting their reply, oldest first. The server answers // each `launch` with either `launched` or an `error` whose message starts @@ -144,6 +152,16 @@ export class G2MirrorClient { return () => this.bellListeners.delete(listener); } + /** + * Unsolicited output-activity notification for any monitored terminal + * (rate-limited by the wrapper to one per 2s per terminal, leading edge + * only — notifications stop as soon as the output does). + */ + onActivity(listener: (socket: string, lastOutputAtMs: number) => void): () => void { + this.activityListeners.add(listener); + return () => this.activityListeners.delete(listener); + } + /** Unsolicited title change for any monitored terminal. */ onTitle(listener: (socket: string, title: string) => void): () => void { this.titleListeners.add(listener); @@ -350,6 +368,7 @@ export class G2MirrorClient { pid: Number(item?.pid) || 0, cwdHint: String(item?.cwd_hint ?? ""), lastBellAt: typeof item?.last_bell_at === "number" ? item.last_bell_at : null, + lastOutputAt: typeof item?.last_output_at === "number" ? item.last_output_at : null, title: typeof item?.title === "string" ? item.title : null, })) .filter((session: G2MirrorSession) => session.socket.length > 0); @@ -367,6 +386,19 @@ export class G2MirrorClient { } return; } + case "activity": { + const socket = String(message.socket ?? ""); + const lastOutputAt = Number(message.last_output_at) || 0; + if (!socket || !lastOutputAt) return; + const session = this.sessions.find((s) => s.socket === socket); + if (session) session.lastOutputAt = lastOutputAt; + // No emitState: activity is frequent while an app is busy, and the + // terminal worker drives its own hub animation off the listener. + for (const listener of Array.from(this.activityListeners)) { + listener(socket, lastOutputAt); + } + return; + } case "title": { const socket = String(message.socket ?? ""); const title = String(message.title ?? "");