forked from Blendman974/faceclaw
<details> <summary>Prompts Used</summary> User: Thanks, looks pretty good. I notice that during the transition from the glanceboard back to off, the glasses display a flash of the main UI. Let's address that. AI: > Fixed and built. The cause was the hide order in the glance host: it hid the glance surface first, which made the compositor recomposite and transmit the sleeping UI's retained window frames, and only afterwards did the screen-off path blank the display. That one transmitted frame was the flash. > > The hide path in `app/g2/glance-host.ts` now blanks the compositor first and hides the surface second. Blanked, the surface change composites to the same black frame, so nothing extra goes to the glasses. The controller's screen-off path still runs afterwards to drop the wakelock and re-arm the 5-second suspend. Its own blank call is now redundant but harmless. > > The double-tap path is unchanged on purpose: there the surface hides while the shell is waking, so the retained UI appearing is the intended result. > > Typecheck and the full Android build both pass. I recorded the finding in `notes/glanceboard.md` with a new checklist item for the timeout and release transitions. Worth re-checking on hardware: both the 3-second timeout and the long-press release should now go straight to dark. <hr> User: Thanks. Next up, let's make a firmware mod so that we can capture tap and tab-and-hold from deep sleep. AI: > Done. Firmware revision 2 forwards tap, long-press, and long-press release from deep sleep, and the phone side consumes them. Everything builds and the tests pass, but nothing is flashed or hardware-tested yet. > > **How the firmware does it** > > Decompiling the display thread showed that with no app on screen, its touch branch launches the dashboard on double-tap or head-up, launches the stock Menu on tap-then-hold, and frees every other gesture unhandled. The call right after that idle gate at `0x45f01a` has the input record in r4 (raw source at +2, gesture subtype at +4). The new hook in `g2flash/patches/settings_ext.c` wraps that call. When Faceclaw holds the wake lease and the firmware is in the mode where a double-tap would have woken the dashboard, it reports subtype 0 (tap), 3 (long press) and 0xe (release) as settings-sid field-102 events 2, 3 and 4, carrying the raw source. The stock result is returned unchanged, so stock behavior is untouched. Unlike the double-tap wake, nothing stock is deferred, so there is no claim or fallback handshake. I verified in the patched image that the site is a branch into the blob, that the shim hands r4 to the C function, and that the function preserves r4. > > **Phone side** > > - Java parses the new events into ordinary sys-events with the protobuf source while the page is suspended, so TypeScript sees exactly the soft-sleep gestures and the Glanceboard logic needs no change. > - The firmware string is now `Faceclaw/2`, the app requires revision 2, and the embedded patch set was regenerated. Onboarding will offer the reflash. > - A Java test covers the event and source mapping, the wake event staying distinct, and malformed payloads. > > **Verification** > > | Check | Result | > |---|---| > | g2flash `build_cfw.sh` | verified, new output hash pinned | > | Faceclaw `build.sh` | success | > | `npm test` | new tests pass; only the pre-existing pinball and compass-style failures remain | > > **Two caveats** > > - I read "tap-and-hold" as press-and-hold, which is the long-press the Glanceboard uses. The firmware's distinct tap-then-hold gesture still opens the stock Menu app from sleep and is not forwarded. Say so if you wanted that one instead. > - Only the right lens notifies the phone. A double-tap on either temple wakes both lenses, so left-temple taps very likely reach the right lens's display thread while idle, but that is unverified until it runs on hardware. The checklist in `notes/glanceboard.md` has the new deep-sleep steps and the logcat line to look for. > > Nothing is committed in either repository. <hr> User: Thanks, that worked. Next up, there's also a screen-on trigger from the accelerometer detecting that you've tilted your head up 45deg. Let's make that trigger go to the glanceboard rather than the main app. (If this requires another firmware extension, go ahead and make it; otherwise use what's already there.) AI: > Done. It did need a firmware extension, so this is firmware revision 3 plus matching phone changes. Everything builds and tests pass; nothing is flashed or hardware-tested yet. > > **Why firmware was needed.** In deep sleep the head-up already reached the phone, but through the same deferred-wake event as a double-tap, so the phone could not tell them apart. In soft sleep, the first five seconds after the screen goes dark, the head-up never reached the phone at all: the display thread's idle gate drops it while an EvenHub page is on screen. > > **Firmware, revision 3** > > - **Deep sleep.** The idle policy's head-up display-start site now goes through its own wrapper. It runs the identical deferred-wake claim-and-ready handshake but reports field-102 event code 5 instead of 1. One deliberate difference: a head-up while a wake is already pending is ignored, instead of the double-tap's emergency stock-dashboard launch, so lifting your head right after a tap cannot flash the stock dashboard. > - **Soft sleep.** The idle-gate call is wrapped, ported from the old head-tilt branch with two extra guards mirroring the stock idle path: the glasses' head-up switch must be on and the stock busy check must be clear. It forwards the head-up as EvenHub sys-event 12 under the framebuffer lease. > - Both hooks and the two wrappers were verified by disassembling the patched image. The output hash is pinned and the embedded patch set in Faceclaw is regenerated. > > **Phone side** > > - Java decodes wake code 5 as a display-wake carrying the head-up event type, and rewrites the soft-sleep sys-event 12 into the same thing. The Glanceboard logic from earlier already treats that as a press, so a head-tilt now shows the board for the timeout instead of waking the main UI. With the Glanceboard toggle off, a head-tilt still wakes the regular UI as before. > - The app now requires firmware revision 3, so onboarding will offer the reflash. > - The Java test covers the new wake code alongside the tap, long-press, and release events. > > **One thing left as is.** Faceclaw does not push the head-up switch or angle to the glasses. The trigger uses whatever the Even app configured, which is what you have today. The old branch's setting for that is not merged. > > The hardware checklist in `notes/glanceboard.md` has the four head-tilt cases: deep sleep, soft sleep, screen on, and head-tilt right after a double-tap. <hr> User: Thanks. Next up, let's make a glanceboard widget for the Calendar. AI: > Waiting on the Android build; the widget, its registration, and the notes are done. <hr> User: Thanks. Next up, let's make a glanceboard widget for the Terminal app. This will show the same terminals (including, especially, activity indicators) as the terminal list window and reuse some code from it. If the window title is too long to fit, we truncate rather than wrap. Also, if a terminal has a window open, that window's number will be shown in a left column in the terminals list (both the terminal-list window version and the glanceboard version). This will replace the "[open]" tag currently at the end of the line. <hr> User: Continue AI: > The Terminal widget is in, the hub list has the new number column, and the build passes. Not yet tried on hardware. > > **Getting the list to the main thread.** The Terminal app lives in a worker, so the widget cannot read its state directly. I added a small generic channel: a worker can post a `publish-state` reply with a key and a JSON value, and the host stores it in a main-thread registry with change notifications. The Terminal worker publishes a session snapshot under `terminal:sessions` whenever the hub would repaint, deduplicated by content. Each row carries its label, the open view window's sidebar glyph, and an activity deadline in the phone's clock, so the widget can blink and expire indicators on its own 800 ms timer without further messages. > > **Shared row painter.** `app/apps/terminal/session-list.ts` now holds one `drawSessionRow` used by both the hub and the widget: activity square in the gutter, the window number column, then the label truncated to fit, never wrapped. The hub used to draw labels untruncated, so it gains that too. > > **The number column.** A session with an open view window now shows that window's sidebar glyph, "1", "2" and so on, in a column left of the label, in both the hub and the widget. The "[open]" suffix is gone from the list. The assistant's list-sessions tool text says "[open in window 2]" instead. > > **Widget layout** at 288x144: "Terminal" label, then host headings only when more than one host is connected, then session rows, as many as fit. Empty states cover no connections, connecting, and no live sessions. > > **One limitation to know about.** The snapshot exists only once the Terminal app has been opened in this app session, because that is when the worker and its g2mirror control clients start. Before that the widget says to open the Terminal app. Since the shell restores open apps at boot, a hub left open carries across restarts. Making the worker start its control clients at boot without a window is a possible follow-up if you want the widget live from launch. > > Checklist entry 7c in the design note covers the blink sync, the number column in both views, and truncation. Nothing is committed. <hr> User: Thanks. Next up, let's make it so that selecting a widget for a slot that's already used in another slot clears that slot, _except_ that certain widgets that support it can occupy both left slots or both right slots, in which case they render at double height and the line between the slots is removed. We'll implement double-height for the Terminal and Calendar widgets. AI: > Done. Slot uniqueness and double-height regions are in, with a node test for the pure part, and the build passes. > > **The rule** lives in two places that share one adjacency helper in the layout module: > > - **When a picker changes**, the config app clears every other slot holding the same widget, unless the widget is marked tall and that slot is vertically adjacent to the one just chosen. So picking Terminal for Bottom left while it is in Top left keeps both, and then picking it for Top right clears both left slots. This runs from the picker's change hook and goes through the settings broadcast, so the board and the other rows follow on their own. > - **When the board paints**, it resolves the slot choices into regions. A tall widget in two vertically adjacent slots becomes one 288x288 region and its widget gets the taller canvas. Dividers are drawn per region, so the line between the merged slots disappears. > > **Tall widgets.** Terminal and Calendar are marked tall. Both already lay out rows until they run out of height, so at double height they simply show more sessions or more events with no widget code changes. The other widgets stay single-slot. > > **Region diffing.** The board keys live regions by widget plus slots and restarts only what changed on a settings change. Switching a widget from single to tall restarts just that widget; unrelated widgets keep their subscriptions and hardware holds. > > **Verification.** `tests/glanceboard-layout.test.cjs` covers adjacency, left and right merges, non-spanning and diagonal duplicates staying separate, and empty boards. Typecheck and the Android build pass. The design note has a checklist step for the three picker cases. Nothing is committed. <hr> User: Thanks. Let's organize the Glanceboard configuration app into several screens. The entry page will have a title, a paragraph of text describing how the glanceboard works, and a menu with an "Enable Glanceboard" toggle, Preview, Select Contents, and Settings. Settings will contain: * "Show on tap", which instead of a toggle will be a duration (from a list of options 3s, 5s, 7s, 10s, default 5s) * "Show on long press", which will be a toggle * "Show on head tilt", which will be a toggle * "Show lines", which will toggle visibility of the quadrant-separator lines Fable 5.1: > The Glanceboard app is now three screens, the four settings are wired through, and the build and tests pass. > > **Entry page.** Title, a short paragraph on how the board works, and a menu drawn on the same page below it: Enable Glanceboard (toggle, the existing setting relabeled), Preview, Select Contents, Settings. Double-tap on the entry page yields to the sidebar as before; on the sub-screens it goes back. > > **Select Contents** is the slot pickers page from before, including the one-widget-per-board rule and double-height merging. > > **Settings** holds: > > - **Show on tap**: a duration picker, 3, 5, 7 or 10 seconds, default 5. The head-tilt uses the same duration since it behaves as a tap. > - **Show on long press**: toggle. Off means a hold while asleep does nothing. A release still passes through so a hold already in progress can end. > - **Show on head tilt**: toggle. Off sends the head-tilt wake to the regular UI, the pre-Glanceboard behavior. > - **Show lines**: toggle for the slot dividers. > > **Wiring.** The gesture policy moved out of the controller into the glance host, which asks the board provider for the four values. The timeout is handed to the state reducer per event, so the pure module stays testable; a new test covers the supplied timeout. The master toggle off still makes every sleep gesture a no-op. > > The design note's checklist has a step per setting. Nothing is committed. <hr> User: Thanks. Let's maket the menu half width, and use the space on the right for a preview with the quadrant-separator lines and the title of what's in each slot. Instead of "Select Contents" opening a separate page, it will move the focus to that view, so you can pick one of the quadrants there and tap to open a menu of what goes in that quadrant. The four-quadrants preview will show the lines between them; if the show-lines setting is disabled, they'll be reduced to min brightness and dotted (every other pixel). The preview will omit the line between quadrants as appropriate for double-height views. Fable 5.1: > The entry page now has the half-width menu on the left and the slot preview on the right, with Select Contents working inside the preview. Build and tests pass. > > **Preview.** The right half shows the board scaled down with its slot dividers and the name of the widget in each region, or "Empty". A double-height region shows one name and no line across it. With "Show lines" off, the dividers drop to the dimmest visible shade and light only every other pixel, so the layout still reads but signals off. > > **Select Contents.** Choosing it moves focus into the preview instead of opening a page. Scroll moves a highlight between the four quadrants, a tap opens a modal picker for that slot with the current choice marked, and picking applies the same one-widget-per-board rule as before. The preview updates as soon as a choice lands. Double-tap returns focus to the menu. While the preview has focus the menu's selection shows as outline only, following the shell's convention that a filled selection means it has input. > > **Shared geometry.** The divider layout moved out of the board into a pure helper in the layout module, so the live board and the preview draw the same lines, including the merged-region rule. A new test pins the four-slot and tall-Terminal cases. > > The design note's checklist has a step for the preview interactions. Nothing is committed. <hr> User: Thanks. One fix: while the quadrants-preview is selecte, double-tap back currently goes to the app-switcher, but should go to the glanceboard menu. We can also remove the gesture-help below the preview. Fable 5.1: > Fixed and built. The cause was the root wrapper that turns any double-tap at the app's root into a yield to the sidebar, which ran before the page could see the gesture. > > The home page is now the window's root without that wrapper and routes double-tap itself: with the preview focused it returns focus to the menu, with the menu focused it yields to the sidebar as before. The gesture hint under the preview is gone. > > Typecheck and the Android build pass. Nothing is committed. <hr> User: Thanks, looks good. Let's make the lines in the glanceboard (on the real glanceboard, not the configuration widget) dimmer. In the Terminal glanceboard widget, with my configured font size (Roboto Light 12), there is room for one more row. It does seem to be responsive (when I increased the font size to 18 the row count dropped accordingly) but the bottom boundary might be above where it could be. Let's add IOB to the Nightscout glanceboard widget. Also include any of cannula age, cartridge volume, battery voltage, and time since last loop that breach the limits configured in Nightscout settings (if they aren't already included), and double check that the glucose value is crossed out if it's too old. Fable 5.1: > All three done, typechecked and built. > > **Dimmer lines.** The live board's dividers dropped from shade 40 to 20, the dimmest level that survives the 4-bit quantization. The configuration preview keeps its own brighter lines. > > **Terminal rows.** The bottom bound was stricter than it needed to be: it required a full line step, which includes inter-line spacing, to fit above an 8 px bottom pad. Rows are still placed a step apart, but the last one now only needs its glyph line height to clear a 2 px margin, and the gap under the "Terminal" label lost two pixels. With a 12 px font that admits one more row. The Calendar widget got the same rule for consistency. > > **Nightscout widget.** IOB now follows the age line. Below that, only fields breaching the limits from Nightscout settings appear, inverted the way the app marks them: cannula age, reservoir volume, pump battery voltage, and loop age, one per line while room remains. Nothing shown means nothing is out of range. The strikethrough was already correct: the value is crossed at mid-height once the reading is older than the app's 15-minute limit, using the app's own helper. I added a comment saying so. > > Nothing is committed. </details>
1953 lines
71 KiB
TypeScript
1953 lines
71 KiB
TypeScript
/**
|
|
* Terminal app, hosted in its own worker thread. Window model:
|
|
* - "terminal:hub": the window opened from the launcher; shows the list of
|
|
* live g2mirror sessions across every connected host (grouped by host when
|
|
* more than one is connected), and hosts the Manage Connections section
|
|
* where g2mirror:// connections are added, removed, and toggled.
|
|
* - "terminal:view:N": opened by selecting a session in the hub; each has
|
|
* its own websocket connection (the protocol allows one attached session
|
|
* per connection) and its own xterm emulator.
|
|
*
|
|
* Connections are g2mirror://<token>@<host>[:port] strings (g2mirrors:// for
|
|
* TLS), stored as JSON in the settings store (see connections.ts). Each
|
|
* enabled connection gets a control websocket that backs the session list and
|
|
* carries unsolicited bell/title notifications; control connections live as
|
|
* long as the worker so bells keep flowing even if the hub window is closed.
|
|
* The host's server_name (from the init reply) is cached per connection and
|
|
* used as its display name, falling back to the connection string's
|
|
* host[:port] before the first successful handshake.
|
|
*
|
|
* Bells for a session with an open, non-foregrounded view window set that
|
|
* window's sidebar attention flag (cleared by the host on foregrounding).
|
|
* Frames are painted here and submitted directly to the Java compositor from
|
|
* this worker's thread.
|
|
*
|
|
* Auto-reconnect (Settings > Terminal, default on): while any terminal window
|
|
* is open, a dropped control connection reconnects with exponential backoff;
|
|
* the first session list after reconnect delivers bells that rang while
|
|
* disconnected (attention + wake) via their advanced lastBellAt. A dropped
|
|
* view connection reconnects immediately while visible, but a hidden view just
|
|
* marks itself stale and reconnects on its next foreground/screen-on — its
|
|
* re-attach snapshot resyncs contents and scrollback only once it's needed.
|
|
*/
|
|
import "@nativescript/core/globals";
|
|
import { GrayImage, type UiFont } from "../../graphics/image";
|
|
import { flattenPlanesWithDraws, planesFingerprint, type Plane } from "../../graphics/plane";
|
|
import { prepareFrameDraws } from "../../graphics/glyph-wire";
|
|
import { getDefaultSmallFont, getTerminalFontConfig } from "../../graphics/ui-fonts";
|
|
import { truncateText } from "../../graphics/textwrap";
|
|
import { TERMINAL_ICON_GLYPHS, type IconActivity } from "../../graphics/icons";
|
|
import {
|
|
drawSessionRow,
|
|
TERMINAL_SESSIONS_STATE_KEY,
|
|
type TerminalSessionsSnapshot,
|
|
} from "./session-list";
|
|
import * as frameTimings from "../../native/frame-timings";
|
|
import { getActiveDisplay } from "../../native/active-display";
|
|
import { GESTURE_DOUBLE_CLICK, type InputEvent } from "../../ui/gestures";
|
|
import { G2MirrorClient, type G2MirrorClientOptions, type G2MirrorSession, type G2MirrorState } from "../../native/g2mirror-client";
|
|
import { onSettingsStoreChanged } from "../../native/settings-store";
|
|
import { clamp } from "../../util/numeric-util";
|
|
import { terminalAutoReconnectSetting, terminalLaunchPresetsSetting, terminalNewConnectionSetting, terminalWakeOnBellSetting } from "../../ui/dashboard-settings";
|
|
import { connectionDisplayName, loadConnections, parseConnectionString, saveConnections, TERMINAL_CONNECTIONS_KEY, updateConnection, type TerminalConnection } from "./connections";
|
|
import { TerminalEmulator } from "./terminal-emulator";
|
|
import { drawListScrollbar, drawSelectionHighlight, scrollToKeepSelectionVisible, type MenuItem } from "../../ui/menu";
|
|
import { lineStep, listRowHeight } from "../../ui/metrics";
|
|
import { WindowMenu } from "../../ui/window-menu";
|
|
import { appViewportSize } from "../../ui/shell/geometry";
|
|
import type { WorkerAppMessage, WorkerAppReply } from "../../ui/shell/worker-window";
|
|
import type { ToolResult, ToolSpec } from "../../assistant/tool-registry";
|
|
|
|
declare const global: any;
|
|
declare const com: any;
|
|
|
|
// Cell geometry comes from the terminal font setting (Settings > Terminal >
|
|
// Font; the default is Terminus-12's 6x12). Each window derives its grid from
|
|
// the viewport in its open-window message (the hub is min-height, session
|
|
// views full-height). Grid dimensions are baked into each session's websocket
|
|
// handshake, so windows and control connections capture the cell size at
|
|
// creation — a font change applies to ones (re)opened afterwards.
|
|
function chromeFont(): UiFont {
|
|
return getDefaultSmallFont();
|
|
}
|
|
// Grid of a session view window (full-height); also declared on the control
|
|
// connections so sessions launched from presets come up at view size.
|
|
const VIEW_VIEWPORT = appViewportSize("max");
|
|
function viewGrid(): { cols: number; rows: number } {
|
|
const { cellWidth, cellHeight } = getTerminalFontConfig();
|
|
return {
|
|
cols: Math.floor(VIEW_VIEWPORT.width / cellWidth),
|
|
rows: Math.floor(VIEW_VIEWPORT.height / cellHeight),
|
|
};
|
|
}
|
|
const DEVICE_NAME = "Faceclaw G2";
|
|
const RENDER_COALESCE_MS = 33;
|
|
const HISTORY_PAGE = 200;
|
|
// Auto-reconnect backoff: doubles per failed attempt, resets on success.
|
|
const RECONNECT_MIN_DELAY_MS = 1_000;
|
|
const RECONNECT_MAX_DELAY_MS = 60_000;
|
|
// Storage key of terminalNewConnectionSetting (the Add-connection draft the
|
|
// phone text editor types into); its changes just repaint the add screen.
|
|
const NEW_CONNECTION_DRAFT_KEY = "terminal.newConnectionDraft";
|
|
|
|
type BaseWindow = {
|
|
windowId: string;
|
|
surfaceId: string;
|
|
/** Shell-side window title, echoed back in the open-window message. */
|
|
title: string;
|
|
/** Content viewport from the shell's open-window message; grid = viewport / cell. */
|
|
viewportWidth: number;
|
|
viewportHeight: number;
|
|
gridCols: number;
|
|
gridRows: number;
|
|
foreground: boolean;
|
|
/**
|
|
* Whether this window is the shell's input target (vs. the sidebar having
|
|
* focus). Pushed by the shell on every input/render/foreground message;
|
|
* every focus transition triggers one of those, so it never goes stale.
|
|
*/
|
|
focused: boolean;
|
|
renderScheduled: boolean;
|
|
lastSubmittedFingerprint: string;
|
|
/** Long-press window menu; created on first open. */
|
|
menu: WindowMenu | null;
|
|
};
|
|
|
|
/**
|
|
* What the hub window is showing: the session list, the Manage Connections
|
|
* section, or the Add-connection screen (type the g2mirror:// string on the
|
|
* phone, click to save).
|
|
*/
|
|
type HubMode = "sessions" | "connections" | "add";
|
|
|
|
type HubWindow = BaseWindow & {
|
|
kind: "hub";
|
|
mode: HubMode;
|
|
selectedIndex: number;
|
|
scrollRow: number;
|
|
/**
|
|
* Session recency keys in display order, captured when the window last
|
|
* became visible so the list doesn't reshuffle under the user while it's
|
|
* open. Cleared on foreground/screen-on; orderedSessions() rebuilds it
|
|
* lazily, sorting by recency and appending sessions that appear while
|
|
* visible.
|
|
*/
|
|
sessionOrder: string[];
|
|
/** Parse-failure message shown on the Add-connection screen. */
|
|
addError: string;
|
|
};
|
|
|
|
type ViewWindow = BaseWindow & {
|
|
kind: "view";
|
|
/** Grid font and integer cell pitch, captured at window creation. */
|
|
font: UiFont;
|
|
cellWidth: number;
|
|
cellHeight: number;
|
|
connectionId: string;
|
|
socket: string;
|
|
label: string;
|
|
/** Sidebar-icon character (">3"); "" if every glyph was taken at open time. */
|
|
glyph: string;
|
|
client: G2MirrorClient;
|
|
emulator: TerminalEmulator;
|
|
receivedData: boolean;
|
|
attachRequested: boolean;
|
|
status: string;
|
|
unsubscribers: Array<() => void>;
|
|
// Scrollback model. Absolute line indices span the archive and the emulator:
|
|
// indices < historyNext are archived lines; >= historyNext are emulator buffer
|
|
// line (index - historyNext). `archive` holds fetched lines [archiveStart,
|
|
// historyNext). `scrollTop` is the absolute index of the top visible line, or
|
|
// null to follow the live bottom.
|
|
historyNext: number;
|
|
historyOldest: number;
|
|
archive: string[];
|
|
archiveStart: number;
|
|
scrollTop: number | null;
|
|
historyFetchInFlight: boolean;
|
|
/**
|
|
* The websocket dropped and hasn't been reconnected yet. While the view is
|
|
* hidden this just sits set (content resync deferred); the next
|
|
* foreground/screen-on reconnects if auto-reconnect is enabled.
|
|
*/
|
|
needsReconnect: boolean;
|
|
reconnectTimer: ReturnType<typeof setTimeout> | null;
|
|
reconnectDelayMs: number;
|
|
};
|
|
|
|
type TerminalWindow = HubWindow | ViewWindow;
|
|
|
|
type PendingView = {
|
|
connectionId: string;
|
|
socket: string;
|
|
label: string;
|
|
glyph: string;
|
|
/** Client options captured when the view was requested from a live control. */
|
|
options: G2MirrorClientOptions;
|
|
};
|
|
|
|
const windows = new Map<string, TerminalWindow>();
|
|
const pendingViews = new Map<string, PendingView>();
|
|
// The view an assistant tool acts on when the terminal isn't foregrounded:
|
|
// the last view to be foregrounded or receive input.
|
|
let activeViewId: string | null = null;
|
|
let nextViewSerial = 1;
|
|
|
|
/**
|
|
* Assistant tools this app contributes, declared on the hub window (which the
|
|
* launcher opens and which persists for the app's life). All `open`-tier so
|
|
* "rerun the build" works while the terminal is backgrounded. send_input and
|
|
* read_screen act on the active view session (see resolveActiveView).
|
|
*/
|
|
const TERMINAL_TOOLS: ToolSpec[] = [
|
|
{
|
|
name: "list_sessions",
|
|
description: "List the live g2mirror terminal sessions the glasses can see, across every connected host.",
|
|
inputSchema: { type: "object", properties: {}, additionalProperties: false },
|
|
availability: "open",
|
|
},
|
|
{
|
|
name: "send_input",
|
|
description:
|
|
"Type a line into the active terminal session and submit it (as if typed and Enter pressed). Use to run a command in the terminal.",
|
|
inputSchema: {
|
|
type: "object",
|
|
properties: { text: { type: "string", description: "The line to type and run." } },
|
|
required: ["text"],
|
|
additionalProperties: false,
|
|
},
|
|
availability: "open",
|
|
},
|
|
{
|
|
name: "read_screen",
|
|
description: "Return the current visible contents of the active terminal session's screen.",
|
|
inputSchema: { type: "object", properties: {}, additionalProperties: false },
|
|
availability: "open",
|
|
},
|
|
{
|
|
name: "list_launch_presets",
|
|
description:
|
|
"List the named launch presets that can start a new terminal session on a host machine (for launch_session).",
|
|
inputSchema: { type: "object", properties: {}, additionalProperties: false },
|
|
availability: "open",
|
|
},
|
|
{
|
|
name: "launch_session",
|
|
description:
|
|
"Start a new terminal session on a connected host machine from a named launch preset (see list_launch_presets) and open a window viewing it.",
|
|
inputSchema: {
|
|
type: "object",
|
|
properties: {
|
|
preset: { type: "string", description: "Name of the launch preset to start." },
|
|
host: {
|
|
type: "string",
|
|
description: "Which host to launch on (server name), when more than one is connected.",
|
|
},
|
|
},
|
|
required: ["preset"],
|
|
additionalProperties: false,
|
|
},
|
|
availability: "open",
|
|
timeoutMs: 15_000,
|
|
},
|
|
];
|
|
let screenOn = true;
|
|
|
|
/**
|
|
* One record per configured connection: the stored config plus the live
|
|
* control client (session listing for the hub, unsolicited bell/title
|
|
* notifications). `client` is null while the connection is disabled or the
|
|
* config doesn't parse.
|
|
*/
|
|
type ControlConnection = {
|
|
config: TerminalConnection;
|
|
client: G2MirrorClient | null;
|
|
state: G2MirrorState | null;
|
|
unsubscribers: Array<() => void>;
|
|
reconnectTimer: ReturnType<typeof setTimeout> | null;
|
|
reconnectDelayMs: number;
|
|
sessionsSeeded: boolean;
|
|
};
|
|
|
|
// Keyed by connection id; iteration order mirrors the stored list (rebuilt by
|
|
// syncControlsFromSettings). Populated once the app is first opened.
|
|
const controls = new Map<string, ControlConnection>();
|
|
let controlsInitialized = false;
|
|
|
|
// When each session last "updated", unix epoch ms: bells, title changes, and
|
|
// terminal output from open views. Keys are recencyKey(connectionId, socket).
|
|
// Sessions first seen in the initial list after (re)connect are seeded from
|
|
// their last bell, since we weren't watching; ones appearing later were just
|
|
// created, so they get "now".
|
|
const sessionRecency = new Map<string, number>();
|
|
|
|
// 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<string, number>();
|
|
const ACTIVITY_ACTIVE_MS = 5_000;
|
|
// Alternation period shared by hub rows and sidebar cursors.
|
|
const HUB_ANIMATION_STEP_MS = 800;
|
|
|
|
function recencyKey(connectionId: string, socket: string): string {
|
|
return `${connectionId}\n${socket}`;
|
|
}
|
|
|
|
function post(message: WorkerAppReply): void {
|
|
global.postMessage(message);
|
|
}
|
|
|
|
// The host queues messages until this arrives: posts to a worker whose bundle
|
|
// is still evaluating can be silently dropped (see WorkerAppHost). Top-level
|
|
// evaluation is synchronous, so the handler below is installed before any
|
|
// queued message can be delivered.
|
|
post({ type: "worker-ready" });
|
|
|
|
global.onmessage = (event: { data: WorkerAppMessage }) => {
|
|
const message = event.data;
|
|
switch (message.type) {
|
|
case "open-window":
|
|
openWindow(message.windowId, message.surfaceId, message.title, message.viewport);
|
|
break;
|
|
case "resize-window": {
|
|
const window = windows.get(message.windowId);
|
|
if (!window || (window.viewportWidth === message.viewport.width && window.viewportHeight === message.viewport.height)) break;
|
|
window.viewportWidth = message.viewport.width;
|
|
window.viewportHeight = message.viewport.height;
|
|
window.menu?.resize(message.viewport);
|
|
window.lastSubmittedFingerprint = "";
|
|
if (window.kind === "view") {
|
|
window.gridCols = Math.floor(window.viewportWidth / window.cellWidth);
|
|
window.gridRows = Math.floor(window.viewportHeight / window.cellHeight);
|
|
if (window.reconnectTimer) clearTimeout(window.reconnectTimer);
|
|
window.reconnectTimer = null;
|
|
window.client.stop();
|
|
window.client.setViewport(window.gridCols, window.gridRows);
|
|
window.emulator.dispose();
|
|
window.emulator = new TerminalEmulator(window.gridCols, window.gridRows);
|
|
window.receivedData = false;
|
|
window.archive = [];
|
|
window.scrollTop = null;
|
|
reconnectView(window);
|
|
}
|
|
scheduleRender(window);
|
|
break;
|
|
}
|
|
case "close-window":
|
|
closeWindow(message.windowId);
|
|
break;
|
|
case "input": {
|
|
const window = windows.get(message.windowId);
|
|
if (!window) {
|
|
frameTimings.finishFrame(message.frameId, "discarded: unknown terminal window");
|
|
break;
|
|
}
|
|
window.focused = message.focused;
|
|
// Marks the main-thread -> worker hop, which is otherwise an
|
|
// unexplained gap inside the shell's handle-input span.
|
|
frameTimings.logFrame(message.frameId, `input received in ${message.windowId} worker`);
|
|
handleInput(window, message.event as InputEvent, message.frameId);
|
|
break;
|
|
}
|
|
case "text-input": {
|
|
const window = windows.get(message.windowId);
|
|
// Attached view windows type into their terminal. The hub accepts text
|
|
// only on the Add-connection screen, where it fills the draft (voice
|
|
// input as an alternative to the phone keyboard). submitInput appends
|
|
// Enter ("\r") after a wrapper-side pause so paste-detecting apps
|
|
// (e.g. Claude Code) submit instead of inserting a newline.
|
|
if (window && window.kind === "view") {
|
|
window.client.submitInput(message.text);
|
|
} else if (window && window.kind === "hub" && window.mode === "add") {
|
|
terminalNewConnectionSetting.set(message.text);
|
|
scheduleRender(window);
|
|
}
|
|
break;
|
|
}
|
|
case "render": {
|
|
const window = windows.get(message.windowId);
|
|
if (!window) break;
|
|
window.focused = message.focused;
|
|
renderAndSubmit(window, 0);
|
|
break;
|
|
}
|
|
case "foreground": {
|
|
const window = windows.get(message.windowId);
|
|
if (!window) break;
|
|
window.foreground = message.foreground;
|
|
window.focused = message.focused;
|
|
if (window.foreground && window.kind === "view") {
|
|
activeViewId = window.windowId;
|
|
maybeReconnectView(window);
|
|
}
|
|
if (window.foreground && window.kind === "hub") window.sessionOrder = [];
|
|
if (window.foreground) renderAndSubmit(window, 0);
|
|
updateHubAnimation();
|
|
break;
|
|
}
|
|
case "screen":
|
|
screenOn = message.on;
|
|
if (screenOn) {
|
|
for (const window of windows.values()) {
|
|
if (!window.foreground) continue;
|
|
// Waking counts as becoming visible: let the session list re-sort.
|
|
if (window.kind === "hub") window.sessionOrder = [];
|
|
if (window.kind === "view") maybeReconnectView(window);
|
|
renderAndSubmit(window, 0);
|
|
}
|
|
}
|
|
updateHubAnimation();
|
|
break;
|
|
case "tool-call": {
|
|
const callId = message.callId;
|
|
Promise.resolve(handleTerminalTool(message.name, message.args))
|
|
.then((result) => post({ type: "tool-result", callId, result }))
|
|
.catch((error) =>
|
|
post({
|
|
type: "tool-result",
|
|
callId,
|
|
result: { ok: false, error: String((error as Error)?.message ?? error) },
|
|
}),
|
|
);
|
|
break;
|
|
}
|
|
}
|
|
};
|
|
|
|
// React to setting changes (connections edited here or in another isolate,
|
|
// toggles edited in the Settings app, the Add-connection draft typed on the
|
|
// phone).
|
|
onSettingsStoreChanged((key) => {
|
|
if (!key.startsWith("terminal.")) return;
|
|
switch (key) {
|
|
case TERMINAL_CONNECTIONS_KEY:
|
|
// Only once the app has actually been opened.
|
|
if (controlsInitialized) syncControlsFromSettings();
|
|
renderHubWindows();
|
|
return;
|
|
case NEW_CONNECTION_DRAFT_KEY:
|
|
// Live keystrokes from the phone editor; repaint the add screen.
|
|
renderHubWindows();
|
|
return;
|
|
case "terminal.autoReconnect":
|
|
if (!terminalAutoReconnectSetting.get()) {
|
|
cancelPendingReconnects();
|
|
} else if (windows.size > 0) {
|
|
for (const control of controls.values()) {
|
|
if ((control.state?.phase ?? "idle") === "failed") scheduleControlReconnect(control);
|
|
}
|
|
}
|
|
return;
|
|
default:
|
|
// launchPresets, wakeOnBell: no connection impact.
|
|
return;
|
|
}
|
|
});
|
|
|
|
/** Cancel scheduled retries (auto-reconnect turned off); stale views stay marked. */
|
|
function cancelPendingReconnects(): void {
|
|
for (const control of controls.values()) {
|
|
cancelControlReconnect(control);
|
|
}
|
|
for (const window of windows.values()) {
|
|
if (window.kind === "view" && window.reconnectTimer) {
|
|
clearTimeout(window.reconnectTimer);
|
|
window.reconnectTimer = null;
|
|
}
|
|
}
|
|
}
|
|
|
|
function openWindow(windowId: string, surfaceId: string, title: string, viewport: { width: number; height: number }): void {
|
|
const pendingView = pendingViews.get(windowId);
|
|
if (pendingView) {
|
|
pendingViews.delete(windowId);
|
|
windows.set(windowId, createViewWindow(windowId, surfaceId, title, viewport, pendingView));
|
|
updateHubAnimation();
|
|
renderHubWindows();
|
|
return;
|
|
}
|
|
const hubCell = getTerminalFontConfig();
|
|
windows.set(windowId, {
|
|
kind: "hub",
|
|
windowId,
|
|
surfaceId,
|
|
title,
|
|
viewportWidth: viewport.width,
|
|
viewportHeight: viewport.height,
|
|
gridCols: Math.floor(viewport.width / hubCell.cellWidth),
|
|
gridRows: Math.floor(viewport.height / hubCell.cellHeight),
|
|
foreground: false,
|
|
focused: false,
|
|
renderScheduled: false,
|
|
lastSubmittedFingerprint: "",
|
|
menu: null,
|
|
mode: "sessions",
|
|
selectedIndex: 0,
|
|
scrollRow: 0,
|
|
sessionOrder: [],
|
|
addError: "",
|
|
});
|
|
// The hub carries the app's assistant tools; declare them once it exists.
|
|
post({ type: "set-tools", windowId, tools: TERMINAL_TOOLS });
|
|
if (!controlsInitialized) {
|
|
syncControlsFromSettings();
|
|
}
|
|
updateHubAnimation();
|
|
}
|
|
|
|
function closeWindow(windowId: string): void {
|
|
const window = windows.get(windowId);
|
|
if (!window) return;
|
|
if (window.kind === "view") {
|
|
for (const unsubscribe of window.unsubscribers.splice(0)) {
|
|
unsubscribe();
|
|
}
|
|
if (window.reconnectTimer) {
|
|
clearTimeout(window.reconnectTimer);
|
|
window.reconnectTimer = null;
|
|
}
|
|
window.client.stop();
|
|
}
|
|
if (window.kind === "hub" && window.mode === "add") {
|
|
// Window closed mid-add: shut the phone editor down.
|
|
endAddConnection(window);
|
|
}
|
|
windows.delete(windowId);
|
|
windowIconActivity.delete(windowId);
|
|
updateHubAnimation();
|
|
// Auto-reconnect only runs while at least one terminal window is open.
|
|
if (windows.size === 0) {
|
|
for (const control of controls.values()) {
|
|
cancelControlReconnect(control);
|
|
}
|
|
}
|
|
if (activeViewId === windowId) activeViewId = null;
|
|
if (window.kind === "view") {
|
|
renderHubWindows();
|
|
}
|
|
}
|
|
|
|
/** Client options for a connection string; null if the string doesn't parse. */
|
|
function clientOptionsFor(connection: TerminalConnection): G2MirrorClientOptions | null {
|
|
const parsed = parseConnectionString(connection.url);
|
|
if (!parsed) return null;
|
|
return {
|
|
...parsed,
|
|
deviceName: DEVICE_NAME,
|
|
...viewGrid(),
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Reconcile the live control clients with the stored connection list: start
|
|
* enabled connections, stop disabled or removed ones, restart ones whose
|
|
* connection string changed. Called on first open and whenever the stored
|
|
* list changes (including our own edits). Rebuilds the map in stored order so
|
|
* UI iteration matches the list the user manages.
|
|
*/
|
|
function syncControlsFromSettings(): void {
|
|
controlsInitialized = true;
|
|
const stored = loadConnections();
|
|
const storedIds = new Set(stored.map((connection) => connection.id));
|
|
for (const [id, control] of Array.from(controls)) {
|
|
if (!storedIds.has(id)) {
|
|
stopControl(control);
|
|
controls.delete(id);
|
|
}
|
|
}
|
|
const previous = new Map(controls);
|
|
controls.clear();
|
|
for (const connection of stored) {
|
|
let control = previous.get(connection.id);
|
|
if (!control) {
|
|
control = {
|
|
config: connection,
|
|
client: null,
|
|
state: null,
|
|
unsubscribers: [],
|
|
reconnectTimer: null,
|
|
reconnectDelayMs: RECONNECT_MIN_DELAY_MS,
|
|
sessionsSeeded: false,
|
|
};
|
|
}
|
|
const urlChanged = control.config.url !== connection.url;
|
|
control.config = connection;
|
|
controls.set(connection.id, control);
|
|
if (connection.enabled && (urlChanged || !control.client)) {
|
|
startControl(control);
|
|
} else if (!connection.enabled && control.client) {
|
|
stopControl(control);
|
|
}
|
|
}
|
|
renderHubWindows();
|
|
}
|
|
|
|
function startControl(control: ControlConnection): void {
|
|
stopControl(control);
|
|
const options = clientOptionsFor(control.config);
|
|
if (!options) {
|
|
control.state = null;
|
|
return;
|
|
}
|
|
const client = new G2MirrorClient(options);
|
|
const connectionId = control.config.id;
|
|
control.client = client;
|
|
control.state = client.state();
|
|
control.sessionsSeeded = false;
|
|
control.unsubscribers.push(
|
|
client.onStateChange((state) => {
|
|
// Only the current client speaks for the control (stopControl clears
|
|
// listeners, but a stop() emits synchronously before that).
|
|
if (control.client !== client) return;
|
|
noteSessionListRecency(control, state);
|
|
control.state = state;
|
|
if (state.phase === "connected") {
|
|
control.reconnectDelayMs = RECONNECT_MIN_DELAY_MS;
|
|
maybeCacheServerName(control, state.serverName);
|
|
}
|
|
if (state.phase === "failed") scheduleControlReconnect(control);
|
|
renderHubWindows();
|
|
}),
|
|
client.onBell((socket, lastBellAtMs) => {
|
|
routeBell(connectionId, socket, lastBellAtMs);
|
|
}),
|
|
client.onTitle((socket) => {
|
|
noteSessionUpdated(connectionId, socket);
|
|
}),
|
|
client.onActivity((socket) => {
|
|
noteSessionActivity(connectionId, socket);
|
|
}),
|
|
);
|
|
client.start();
|
|
}
|
|
|
|
function stopControl(control: ControlConnection): void {
|
|
cancelControlReconnect(control);
|
|
for (const unsubscribe of control.unsubscribers.splice(0)) {
|
|
unsubscribe();
|
|
}
|
|
control.client?.stop();
|
|
control.client = null;
|
|
control.state = null;
|
|
}
|
|
|
|
/**
|
|
* A successful handshake reported the host's server_name; cache it on the
|
|
* stored connection so the hub can name the host even while disconnected.
|
|
* The resulting settings change re-runs syncControlsFromSettings, which sees
|
|
* an unchanged url and leaves the live client alone.
|
|
*/
|
|
function maybeCacheServerName(control: ControlConnection, serverName: string): void {
|
|
if (!serverName || control.config.serverName === serverName) return;
|
|
control.config.serverName = serverName;
|
|
updateConnection(control.config.id, { serverName });
|
|
}
|
|
|
|
function noteSessionUpdated(connectionId: string, socket: string): void {
|
|
sessionRecency.set(recencyKey(connectionId, socket), Date.now());
|
|
}
|
|
|
|
function noteSessionListRecency(control: ControlConnection, state: G2MirrorState): void {
|
|
const connectionId = control.config.id;
|
|
for (const session of state.sessions) {
|
|
const key = recencyKey(connectionId, session.socket);
|
|
const known = sessionRecency.get(key);
|
|
if (known === undefined) {
|
|
sessionRecency.set(
|
|
key,
|
|
Math.max(session.lastBellAt ?? 0, control.sessionsSeeded ? Date.now() : 0),
|
|
);
|
|
} else if ((session.lastBellAt ?? 0) > known) {
|
|
// A bell rang while we weren't connected to hear it (the live bell
|
|
// message would have advanced the recency); deliver it late so the
|
|
// missed attention flag / wake still happens.
|
|
routeBell(connectionId, session.socket, session.lastBellAt!);
|
|
}
|
|
}
|
|
if (state.sessions.length) control.sessionsSeeded = true;
|
|
}
|
|
|
|
/**
|
|
* Retry a control connection after a backoff delay. The delay doubles per
|
|
* scheduled attempt and resets when a connection reaches "connected" (or on
|
|
* a manual Connect). No-op when auto-reconnect is off, no terminal window is
|
|
* open, or the connection was disabled/removed meanwhile.
|
|
*/
|
|
function scheduleControlReconnect(control: ControlConnection): void {
|
|
if (control.reconnectTimer) return;
|
|
if (!terminalAutoReconnectSetting.get()) return;
|
|
if (windows.size === 0) return;
|
|
if (!control.config.enabled) return;
|
|
const delayMs = control.reconnectDelayMs;
|
|
control.reconnectDelayMs = Math.min(control.reconnectDelayMs * 2, RECONNECT_MAX_DELAY_MS);
|
|
control.reconnectTimer = setTimeout(() => {
|
|
control.reconnectTimer = null;
|
|
if (!terminalAutoReconnectSetting.get() || windows.size === 0) return;
|
|
if (controls.get(control.config.id) !== control || !control.config.enabled) return;
|
|
if ((control.state?.phase ?? "idle") === "failed") startControl(control);
|
|
}, delayMs);
|
|
}
|
|
|
|
function cancelControlReconnect(control: ControlConnection): void {
|
|
if (control.reconnectTimer) {
|
|
clearTimeout(control.reconnectTimer);
|
|
control.reconnectTimer = null;
|
|
}
|
|
}
|
|
|
|
function routeBell(connectionId: string, socket: string, lastBellAtMs: number): void {
|
|
const key = recencyKey(connectionId, socket);
|
|
const known = sessionRecency.get(key) ?? 0;
|
|
if (lastBellAtMs > known) sessionRecency.set(key, lastBellAtMs);
|
|
for (const window of windows.values()) {
|
|
if (
|
|
window.kind === "view" &&
|
|
window.connectionId === connectionId &&
|
|
window.socket === socket &&
|
|
!window.foreground
|
|
) {
|
|
post({ type: "set-attention", windowId: window.windowId, attention: true });
|
|
}
|
|
}
|
|
if (terminalWakeOnBellSetting.get()) {
|
|
// Wake to the belling session's view window, or the hub if it has none.
|
|
// The shell drops the message unless the glasses are actually asleep.
|
|
const viewId = viewWindowIdForSocket(connectionId, socket);
|
|
const hubId = [...windows.values()].find((window) => window.kind === "hub")?.windowId ?? null;
|
|
const target = viewId ?? hubId;
|
|
if (target) post({ type: "wake-window", windowId: target });
|
|
}
|
|
}
|
|
|
|
function renderHubWindows(): void {
|
|
for (const window of windows.values()) {
|
|
if (window.kind === "hub") scheduleRender(window);
|
|
}
|
|
// Every change the hub repaints for is one the published list may reflect.
|
|
publishSessionsSnapshot();
|
|
}
|
|
|
|
let lastPublishedSnapshot = "";
|
|
|
|
/**
|
|
* Publish the session list for main-thread consumers (the Glanceboard's
|
|
* Terminal widget): connected hosts with their sessions most-recently-updated
|
|
* first, each with its activity deadline (local clock, so the consumer can
|
|
* expire and animate it itself) and the glyph of its open view window. Posted
|
|
* only when the JSON actually changed.
|
|
*/
|
|
function publishSessionsSnapshot(): void {
|
|
const connected = connectedControls();
|
|
const snapshot: TerminalSessionsSnapshot = {
|
|
hosts: connected.map((control) => {
|
|
const connectionId = control.config.id;
|
|
const sessions = (control.state?.sessions ?? [])
|
|
.slice()
|
|
.sort(
|
|
(a, b) =>
|
|
(sessionRecency.get(recencyKey(connectionId, b.socket)) ?? 0) -
|
|
(sessionRecency.get(recencyKey(connectionId, a.socket)) ?? 0),
|
|
);
|
|
return {
|
|
name: connectionDisplayName(control.config),
|
|
sessions: sessions.map((session) => ({
|
|
key: recencyKey(connectionId, session.socket),
|
|
label: sessionLabel(session),
|
|
activeUntilMs: (sessionActivity.get(recencyKey(connectionId, session.socket)) ?? -ACTIVITY_ACTIVE_MS) + ACTIVITY_ACTIVE_MS,
|
|
openGlyph: viewGlyphForSocket(connectionId, session.socket),
|
|
})),
|
|
};
|
|
}),
|
|
status: sessionsStatusLine(),
|
|
configured: controls.size > 0,
|
|
};
|
|
const encoded = JSON.stringify(snapshot);
|
|
if (encoded === lastPublishedSnapshot) return;
|
|
lastPublishedSnapshot = encoded;
|
|
post({ type: "publish-state", key: TERMINAL_SESSIONS_STATE_KEY, state: snapshot });
|
|
}
|
|
|
|
// One animation clock for foreground hub rows and every terminal sidebar icon.
|
|
// Keep ticking while the sidebar can show activity, even with the hub closed.
|
|
let hubAnimationPhase = 0;
|
|
let hubAnimationTimer: ReturnType<typeof setInterval> | null = null;
|
|
const windowIconActivity = new Map<string, IconActivity>();
|
|
|
|
function isSessionActive(connectionId: string, socket: string): boolean {
|
|
const at = sessionActivity.get(recencyKey(connectionId, socket));
|
|
return at !== undefined && Date.now() - at < ACTIVITY_ACTIVE_MS;
|
|
}
|
|
|
|
function isWindowSessionActive(window: TerminalWindow): boolean {
|
|
if (window.kind === "view") return isSessionActive(window.connectionId, window.socket);
|
|
const now = Date.now();
|
|
return [...sessionActivity.values()].some((at) => now - at < ACTIVITY_ACTIVE_MS);
|
|
}
|
|
|
|
function hubAnimationShouldRun(): boolean {
|
|
return screenOn && [...windows.values()].some(isWindowSessionActive);
|
|
}
|
|
|
|
function syncWindowIconActivity(): void {
|
|
for (const window of windows.values()) {
|
|
const activity: IconActivity = screenOn && isWindowSessionActive(window)
|
|
? (hubAnimationPhase === 0 ? "on" : "off") : "idle";
|
|
if ((windowIconActivity.get(window.windowId) ?? "idle") === activity) continue;
|
|
windowIconActivity.set(window.windowId, activity);
|
|
post({ type: "set-icon-activity", windowId: window.windowId, activity });
|
|
}
|
|
}
|
|
|
|
/** Start or stop animation and publish changed icons, including expiry/wake. */
|
|
function updateHubAnimation(): void {
|
|
const shouldRun = hubAnimationShouldRun();
|
|
if (shouldRun && !hubAnimationTimer) hubAnimationPhase = 0;
|
|
syncWindowIconActivity();
|
|
if (shouldRun) {
|
|
if (hubAnimationTimer) return;
|
|
hubAnimationTimer = setInterval(() => {
|
|
hubAnimationPhase = (hubAnimationPhase + 1) % 2;
|
|
updateHubAnimation();
|
|
// 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,
|
|
title: string,
|
|
viewport: { width: number; height: number },
|
|
view: PendingView,
|
|
): ViewWindow {
|
|
const { connectionId, socket, label, glyph } = view;
|
|
// Capture the terminal font at open time: the grid dims go into the
|
|
// websocket handshake and the emulator, so they must not change under a
|
|
// live window (a font-setting change applies to the next window).
|
|
const cell = getTerminalFontConfig();
|
|
const gridCols = Math.floor(viewport.width / cell.cellWidth);
|
|
const gridRows = Math.floor(viewport.height / cell.cellHeight);
|
|
const client = new G2MirrorClient({ ...view.options, cols: gridCols, rows: gridRows });
|
|
const window: ViewWindow = {
|
|
kind: "view",
|
|
windowId,
|
|
surfaceId,
|
|
title,
|
|
viewportWidth: viewport.width,
|
|
viewportHeight: viewport.height,
|
|
gridCols,
|
|
gridRows,
|
|
font: cell.font,
|
|
cellWidth: cell.cellWidth,
|
|
cellHeight: cell.cellHeight,
|
|
foreground: false,
|
|
focused: false,
|
|
renderScheduled: false,
|
|
lastSubmittedFingerprint: "",
|
|
menu: null,
|
|
connectionId,
|
|
socket,
|
|
label,
|
|
glyph,
|
|
client,
|
|
emulator: new TerminalEmulator(gridCols, gridRows),
|
|
receivedData: false,
|
|
attachRequested: false,
|
|
status: "Connecting...",
|
|
unsubscribers: [],
|
|
historyNext: 0,
|
|
historyOldest: 0,
|
|
archive: [],
|
|
archiveStart: 0,
|
|
scrollTop: null,
|
|
historyFetchInFlight: false,
|
|
needsReconnect: false,
|
|
reconnectTimer: null,
|
|
reconnectDelayMs: RECONNECT_MIN_DELAY_MS,
|
|
};
|
|
window.unsubscribers.push(
|
|
client.onSnapshot((historyNext, historyOldest) => {
|
|
// A (re)snapshot resets the emulator, so reset the scroll model too:
|
|
// follow the bottom and drop any fetched archive (its splice may move).
|
|
window.historyNext = historyNext;
|
|
window.historyOldest = historyOldest;
|
|
window.archive = [];
|
|
window.archiveStart = historyNext;
|
|
window.scrollTop = null;
|
|
window.historyFetchInFlight = false;
|
|
maybePrefetchHistory(window);
|
|
scheduleRender(window);
|
|
}),
|
|
client.onHistoryLines((reply) => {
|
|
applyHistoryReply(window, reply);
|
|
scheduleRender(window);
|
|
}),
|
|
client.onStateChange((state) => {
|
|
if (state.phase === "connected" && !window.attachRequested) {
|
|
window.attachRequested = true;
|
|
client.connectSession(socket);
|
|
}
|
|
if (state.phase === "connected") window.reconnectDelayMs = RECONNECT_MIN_DELAY_MS;
|
|
if (state.phase === "failed") handleViewConnectionLost(window);
|
|
window.status = state.status;
|
|
scheduleRender(window);
|
|
}),
|
|
client.onSessionAttached(() => {
|
|
client.view();
|
|
scheduleRender(window);
|
|
}),
|
|
client.onTerminalData((data, kind) => {
|
|
if (kind === "snapshot") {
|
|
window.emulator.reset();
|
|
}
|
|
window.receivedData = true;
|
|
noteSessionUpdated(window.connectionId, window.socket);
|
|
window.emulator.write(data, () => scheduleRender(window));
|
|
}),
|
|
client.onSessionDetached((reason) => {
|
|
window.status = `Detached (${reason}).`;
|
|
scheduleRender(window);
|
|
}),
|
|
);
|
|
client.start();
|
|
return window;
|
|
}
|
|
|
|
/**
|
|
* The view's websocket dropped. While visible, retry on the backoff schedule;
|
|
* while hidden, leave it stale (needsReconnect) so contents and scrollback
|
|
* only resync once the view is next looked at. Bells still arrive via the
|
|
* control connection either way.
|
|
*/
|
|
function handleViewConnectionLost(window: ViewWindow): void {
|
|
window.needsReconnect = true;
|
|
if (window.foreground && screenOn && terminalAutoReconnectSetting.get()) {
|
|
scheduleViewReconnect(window);
|
|
}
|
|
}
|
|
|
|
function scheduleViewReconnect(window: ViewWindow): void {
|
|
if (window.reconnectTimer) return;
|
|
const delayMs = window.reconnectDelayMs;
|
|
window.reconnectDelayMs = Math.min(window.reconnectDelayMs * 2, RECONNECT_MAX_DELAY_MS);
|
|
window.reconnectTimer = setTimeout(() => {
|
|
window.reconnectTimer = null;
|
|
if (!windows.has(window.windowId) || !window.needsReconnect) return;
|
|
if (!terminalAutoReconnectSetting.get()) return;
|
|
// Went invisible while waiting: defer to the next foreground/screen-on.
|
|
if (!window.foreground || !screenOn) return;
|
|
reconnectView(window);
|
|
}, delayMs);
|
|
}
|
|
|
|
/** A stale view just became visible (foreground/screen-on): reconnect it now. */
|
|
function maybeReconnectView(window: ViewWindow): void {
|
|
if (!window.needsReconnect || window.reconnectTimer) return;
|
|
if (!terminalAutoReconnectSetting.get()) return;
|
|
window.reconnectDelayMs = RECONNECT_MIN_DELAY_MS;
|
|
reconnectView(window);
|
|
}
|
|
|
|
/**
|
|
* Restart the view's websocket. On success the connected handler re-attaches
|
|
* (attachRequested reset here) and the fresh snapshot resets the emulator and
|
|
* scrollback archive, which is the content resync.
|
|
*/
|
|
function reconnectView(window: ViewWindow): void {
|
|
window.needsReconnect = false;
|
|
window.attachRequested = false;
|
|
window.status = "Reconnecting...";
|
|
window.client.start();
|
|
scheduleRender(window);
|
|
}
|
|
|
|
/** The window's context menu, created lazily so window literals stay simple. */
|
|
function windowMenu(window: TerminalWindow): WindowMenu {
|
|
if (!window.menu) {
|
|
window.menu = new WindowMenu({
|
|
windowId: window.windowId,
|
|
post,
|
|
title: () => window.title,
|
|
items: () => windowMenuItems(window),
|
|
size: { width: window.viewportWidth, height: window.viewportHeight },
|
|
paintBase: () => paintContent(window),
|
|
isFocused: () => window.focused,
|
|
});
|
|
}
|
|
return window.menu;
|
|
}
|
|
|
|
/** Controls currently connected (handshake accepted), in stored order. */
|
|
function connectedControls(): ControlConnection[] {
|
|
return [...controls.values()].filter((control) => {
|
|
const phase = control.state?.phase;
|
|
return phase === "connected" || phase === "attached";
|
|
});
|
|
}
|
|
|
|
function windowMenuItems(window: TerminalWindow): MenuItem[] {
|
|
const items: MenuItem[] = [
|
|
{
|
|
label: "Settings",
|
|
onSelect: (ctx) => {
|
|
ctx.stack.pop();
|
|
post({ type: "open-settings", section: "Terminal" });
|
|
},
|
|
},
|
|
];
|
|
if (window.kind === "hub") {
|
|
const connected = connectedControls();
|
|
const multiHost = connected.length > 1;
|
|
for (const control of connected) {
|
|
for (const preset of launchPresetNames()) {
|
|
const label = multiHost
|
|
? `Launch ${preset} @ ${connectionDisplayName(control.config)}`
|
|
: `Launch ${preset}`;
|
|
items.push({
|
|
label,
|
|
onSelect: (ctx) => {
|
|
ctx.stack.pop();
|
|
launchAndOpenView(control, preset).catch((error) => {
|
|
// The hub status line also shows the server's error message.
|
|
console.warn(`terminal launch ${preset} failed: ${error}`);
|
|
});
|
|
},
|
|
});
|
|
}
|
|
}
|
|
} else {
|
|
if (window.client.state().phase === "failed") {
|
|
items.push({
|
|
label: "Reconnect",
|
|
onSelect: (ctx) => {
|
|
ctx.stack.pop();
|
|
if (window.reconnectTimer) {
|
|
clearTimeout(window.reconnectTimer);
|
|
window.reconnectTimer = null;
|
|
}
|
|
window.reconnectDelayMs = RECONNECT_MIN_DELAY_MS;
|
|
reconnectView(window);
|
|
},
|
|
});
|
|
}
|
|
items.push(
|
|
{
|
|
label: "Send <Enter>",
|
|
onSelect: (ctx) => {
|
|
ctx.stack.pop();
|
|
window.client.sendInput("\r");
|
|
},
|
|
},
|
|
{
|
|
label: "Send <Esc>",
|
|
onSelect: (ctx) => {
|
|
ctx.stack.pop();
|
|
window.client.sendInput("\u001b");
|
|
},
|
|
},
|
|
);
|
|
}
|
|
return items;
|
|
}
|
|
|
|
function handleInput(window: TerminalWindow, event: InputEvent, frameId: number): void {
|
|
if (window.kind === "view") activeViewId = window.windowId;
|
|
// An open window menu owns all input (it closes itself via pop).
|
|
if (window.menu?.isOpen()) {
|
|
window.menu
|
|
.handleInput(event)
|
|
.catch((error) => console.error(`terminal menu input failed: ${error}`))
|
|
.then(() => renderAndSubmit(window, frameId));
|
|
return;
|
|
}
|
|
if (event.type === "short-then-long-press") {
|
|
windowMenu(window).open();
|
|
renderAndSubmit(window, frameId);
|
|
return;
|
|
}
|
|
if (event.type === "double-click") {
|
|
// Inside the hub's sub-sections, double-click backs out one level; at the
|
|
// top level (and in views) it yields focus to the sidebar as everywhere.
|
|
if (window.kind === "hub" && window.mode === "add") {
|
|
cancelAddConnection(window);
|
|
renderAndSubmit(window, frameId);
|
|
return;
|
|
}
|
|
if (window.kind === "hub" && window.mode === "connections") {
|
|
setHubMode(window, "sessions");
|
|
renderAndSubmit(window, frameId);
|
|
return;
|
|
}
|
|
frameTimings.finishFrame(frameId, "discarded: terminal yielded focus");
|
|
post({ type: "yield-focus", windowId: window.windowId });
|
|
return;
|
|
}
|
|
if (window.kind === "hub") {
|
|
handleHubInput(window, event, frameId);
|
|
return;
|
|
}
|
|
// View windows: scroll gestures page through scrollback; text input arrives
|
|
// via the separate "text-input" message.
|
|
if (event.type === "scroll-up" || event.type === "scroll-down") {
|
|
handleViewScroll(window, event.type === "scroll-up" ? -1 : 1, frameId);
|
|
return;
|
|
}
|
|
frameTimings.finishFrame(frameId, "discarded: terminal view ignored input");
|
|
}
|
|
|
|
/** Top visible absolute line index when following the live bottom. */
|
|
function followTop(window: ViewWindow): number {
|
|
return window.historyNext + window.emulator.bufferLength() - window.gridRows;
|
|
}
|
|
|
|
function handleViewScroll(window: ViewWindow, direction: -1 | 1, frameId: number): void {
|
|
const page = Math.max(1, window.gridRows - 1);
|
|
const bottomTop = followTop(window);
|
|
const minTop = window.archiveStart;
|
|
const currentTop = window.scrollTop ?? bottomTop;
|
|
const newTop = clamp(currentTop + direction * page, Math.min(minTop, bottomTop), bottomTop);
|
|
// Snapping to (or below) the live bottom re-locks to follow mode.
|
|
window.scrollTop = newTop >= bottomTop ? null : newTop;
|
|
maybePrefetchHistory(window);
|
|
renderAndSubmit(window, frameId);
|
|
}
|
|
|
|
/** Fetch an older page of archive when the view nears the top of what's loaded. */
|
|
function maybePrefetchHistory(window: ViewWindow): void {
|
|
if (window.historyFetchInFlight) return;
|
|
if (window.archiveStart <= window.historyOldest) return; // nothing older retained
|
|
const top = window.scrollTop ?? followTop(window);
|
|
if (window.archive.length === 0 || top - window.archiveStart <= window.gridRows) {
|
|
window.historyFetchInFlight = true;
|
|
window.client.requestHistory(window.archiveStart, HISTORY_PAGE);
|
|
}
|
|
}
|
|
|
|
function applyHistoryReply(window: ViewWindow, reply: { start: number; oldest: number; lines: string[] }): void {
|
|
window.historyFetchInFlight = false;
|
|
window.historyOldest = reply.oldest;
|
|
if (!reply.lines.length) {
|
|
// Nothing older was returned; stop asking below what we already have.
|
|
window.historyOldest = window.archiveStart;
|
|
return;
|
|
}
|
|
// The page ends just before our request (archiveStart); prepend it.
|
|
if (reply.start < window.archiveStart) {
|
|
window.archive = reply.lines.concat(window.archive);
|
|
window.archiveStart = reply.start;
|
|
}
|
|
}
|
|
|
|
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;
|
|
/** Glyph of the view window showing this session (the number column). */
|
|
openGlyph?: string | null;
|
|
onSelect?: () => void;
|
|
};
|
|
|
|
/** Switch the hub between its sections, resetting selection state. */
|
|
function setHubMode(window: HubWindow, mode: HubMode): void {
|
|
window.mode = mode;
|
|
window.selectedIndex = 0;
|
|
window.scrollRow = 0;
|
|
window.addError = "";
|
|
}
|
|
|
|
/**
|
|
* One control connection's sessions in this hub window's display order:
|
|
* most-recently-updated first as of when the window became visible, with
|
|
* sessions that appeared since then at the end. The captured order lives in
|
|
* window.sessionOrder (cleared when the window becomes visible; keys are
|
|
* connection-scoped) so the list doesn't reshuffle while the user is looking
|
|
* at it.
|
|
*/
|
|
function orderedSessions(window: HubWindow, control: ControlConnection): G2MirrorSession[] {
|
|
const connectionId = control.config.id;
|
|
const sessions = control.state?.sessions ?? [];
|
|
const position = new Map<string, number>();
|
|
for (const key of window.sessionOrder) {
|
|
if (!position.has(key)) position.set(key, position.size);
|
|
}
|
|
const fresh = sessions
|
|
.filter((session) => !position.has(recencyKey(connectionId, session.socket)))
|
|
.sort(
|
|
(a, b) =>
|
|
(sessionRecency.get(recencyKey(connectionId, b.socket)) ?? 0) -
|
|
(sessionRecency.get(recencyKey(connectionId, a.socket)) ?? 0),
|
|
);
|
|
for (const session of fresh) {
|
|
const key = recencyKey(connectionId, session.socket);
|
|
position.set(key, position.size);
|
|
window.sessionOrder.push(key);
|
|
}
|
|
return sessions
|
|
.slice()
|
|
.sort(
|
|
(a, b) =>
|
|
position.get(recencyKey(connectionId, a.socket))! - position.get(recencyKey(connectionId, b.socket))!,
|
|
);
|
|
}
|
|
|
|
function hubItems(window: HubWindow): HubItem[] {
|
|
return window.mode === "connections" ? hubConnectionItems(window) : hubSessionItems(window);
|
|
}
|
|
|
|
function hubSessionItems(window: HubWindow): HubItem[] {
|
|
const items: HubItem[] = [
|
|
{
|
|
label: "Manage Connections",
|
|
onSelect: () => setHubMode(window, "connections"),
|
|
},
|
|
];
|
|
const connected = connectedControls();
|
|
const multiHost = connected.length > 1;
|
|
for (const control of connected) {
|
|
if (multiHost) {
|
|
items.push({ label: connectionDisplayName(control.config), heading: true });
|
|
}
|
|
const sessions = orderedSessions(window, control);
|
|
for (const session of sessions) {
|
|
items.push({
|
|
label: sessionLabel(session),
|
|
active: isSessionActive(control.config.id, session.socket),
|
|
openGlyph: viewGlyphForSocket(control.config.id, session.socket),
|
|
onSelect: () => {
|
|
const windowId = viewWindowIdForSocket(control.config.id, session.socket);
|
|
if (windowId) {
|
|
post({ type: "focus-window", windowId });
|
|
} else {
|
|
openViewWindowFor(control, session);
|
|
}
|
|
},
|
|
});
|
|
}
|
|
if (!sessions.length) {
|
|
items.push({
|
|
label: multiHost ? "(no live sessions)" : "(no live sessions; run g2mirror <command>)",
|
|
onSelect: () => control.client?.listSessions(),
|
|
});
|
|
}
|
|
}
|
|
// Disconnected/failed connections get a one-click Connect shortcut here;
|
|
// full management (remove, add) lives in Manage Connections.
|
|
for (const control of controls.values()) {
|
|
if (connected.includes(control)) continue;
|
|
if (control.config.enabled && control.state?.phase === "connecting") continue;
|
|
items.push({
|
|
label: `Connect ${connectionDisplayName(control.config)}`,
|
|
onSelect: () => connectControl(control),
|
|
});
|
|
}
|
|
return items;
|
|
}
|
|
|
|
function hubConnectionItems(window: HubWindow): HubItem[] {
|
|
const items: HubItem[] = [];
|
|
for (const control of controls.values()) {
|
|
items.push({
|
|
label: `${connectionDisplayName(control.config)} (${controlStatusWord(control)})`,
|
|
onSelect: () => openConnectionActions(window, control),
|
|
});
|
|
}
|
|
items.push(
|
|
{
|
|
label: "Add connection",
|
|
onSelect: () => beginAddConnection(window),
|
|
},
|
|
{
|
|
label: "Done",
|
|
onSelect: () => setHubMode(window, "sessions"),
|
|
},
|
|
);
|
|
return items;
|
|
}
|
|
|
|
function controlStatusWord(control: ControlConnection): string {
|
|
if (!control.config.enabled) return "off";
|
|
switch (control.state?.phase) {
|
|
case "connected":
|
|
case "attached":
|
|
return "connected";
|
|
case "connecting":
|
|
return "connecting";
|
|
case "failed":
|
|
return control.reconnectTimer ? "retrying" : "failed";
|
|
default:
|
|
return clientOptionsFor(control.config) ? "off" : "bad connection string";
|
|
}
|
|
}
|
|
|
|
/** Per-connection action menu (reuses the window-menu machinery). */
|
|
function openConnectionActions(window: HubWindow, control: ControlConnection): void {
|
|
const name = connectionDisplayName(control.config);
|
|
const items: MenuItem[] = [
|
|
control.config.enabled
|
|
? {
|
|
label: "Disconnect",
|
|
onSelect: (ctx) => {
|
|
ctx.stack.pop();
|
|
disconnectControl(control);
|
|
},
|
|
}
|
|
: {
|
|
label: "Connect",
|
|
onSelect: (ctx) => {
|
|
ctx.stack.pop();
|
|
connectControl(control);
|
|
},
|
|
},
|
|
{
|
|
label: `Remove ${name}`,
|
|
onSelect: (ctx) => {
|
|
ctx.stack.pop();
|
|
removeControl(control);
|
|
},
|
|
},
|
|
{
|
|
label: "Cancel",
|
|
onSelect: (ctx) => {
|
|
ctx.stack.pop();
|
|
},
|
|
},
|
|
];
|
|
windowMenu(window).open(items);
|
|
}
|
|
|
|
/** Enable and (re)connect now; also the manual retry path, so reset backoff. */
|
|
function connectControl(control: ControlConnection): void {
|
|
control.reconnectDelayMs = RECONNECT_MIN_DELAY_MS;
|
|
if (!control.config.enabled) {
|
|
control.config.enabled = true;
|
|
updateConnection(control.config.id, { enabled: true });
|
|
}
|
|
startControl(control);
|
|
renderHubWindows();
|
|
}
|
|
|
|
function disconnectControl(control: ControlConnection): void {
|
|
control.config.enabled = false;
|
|
updateConnection(control.config.id, { enabled: false });
|
|
stopControl(control);
|
|
renderHubWindows();
|
|
}
|
|
|
|
function removeControl(control: ControlConnection): void {
|
|
stopControl(control);
|
|
controls.delete(control.config.id);
|
|
saveConnections(loadConnections().filter((connection) => connection.id !== control.config.id));
|
|
renderHubWindows();
|
|
}
|
|
|
|
/**
|
|
* Enter the Add-connection screen: clear the draft and ask the shell to open
|
|
* the phone text editor on it. The user types (or voice-inputs) the
|
|
* g2mirror:// string, then clicks to save or double-clicks to cancel.
|
|
*/
|
|
function beginAddConnection(window: HubWindow): void {
|
|
terminalNewConnectionSetting.set("");
|
|
setHubMode(window, "add");
|
|
post({ type: "start-text-setting-edit", settingId: terminalNewConnectionSetting.id });
|
|
}
|
|
|
|
function saveAddConnection(window: HubWindow): void {
|
|
const url = terminalNewConnectionSetting.get();
|
|
if (!parseConnectionString(url)) {
|
|
window.addError = url
|
|
? "Not a g2mirror://token@host connection string."
|
|
: "Enter a g2mirror:// connection string first.";
|
|
scheduleRender(window);
|
|
return;
|
|
}
|
|
const connections = loadConnections();
|
|
connections.push({ id: `c${Date.now()}`, url, serverName: null, enabled: true });
|
|
saveConnections(connections);
|
|
endAddConnection(window);
|
|
setHubMode(window, "connections");
|
|
// Start it immediately rather than waiting for the settings-change tick.
|
|
syncControlsFromSettings();
|
|
}
|
|
|
|
function cancelAddConnection(window: HubWindow): void {
|
|
endAddConnection(window);
|
|
setHubMode(window, "connections");
|
|
}
|
|
|
|
/** Common teardown for leaving the add screen: clear draft, close phone editor. */
|
|
function endAddConnection(window: HubWindow): void {
|
|
window.addError = "";
|
|
terminalNewConnectionSetting.set("");
|
|
post({ type: "end-text-setting-edit" });
|
|
}
|
|
|
|
/** Move the hub selection to the next selectable item in `direction`. */
|
|
function moveHubSelection(window: HubWindow, items: HubItem[], direction: -1 | 1): void {
|
|
let index = window.selectedIndex + direction;
|
|
while (index >= 0 && index < items.length && !items[index]!.onSelect) {
|
|
index += direction;
|
|
}
|
|
if (index >= 0 && index < items.length) {
|
|
window.selectedIndex = index;
|
|
}
|
|
}
|
|
|
|
/** Clamp the selection into range and off heading rows (prefer moving down). */
|
|
function clampHubSelection(window: HubWindow, items: HubItem[]): void {
|
|
if (!items.length) {
|
|
window.selectedIndex = 0;
|
|
return;
|
|
}
|
|
let index = Math.max(0, Math.min(window.selectedIndex, items.length - 1));
|
|
if (!items[index]!.onSelect) {
|
|
let forward = index;
|
|
while (forward < items.length && !items[forward]!.onSelect) forward++;
|
|
let backward = index;
|
|
while (backward >= 0 && !items[backward]!.onSelect) backward--;
|
|
index = forward < items.length ? forward : Math.max(0, backward);
|
|
}
|
|
window.selectedIndex = index;
|
|
}
|
|
|
|
function handleHubInput(window: HubWindow, event: InputEvent, frameId: number): void {
|
|
if (window.mode === "add") {
|
|
if (event.type === "click") {
|
|
saveAddConnection(window);
|
|
renderAndSubmit(window, frameId);
|
|
return;
|
|
}
|
|
frameTimings.finishFrame(frameId, "discarded: terminal add-connection ignored input");
|
|
return;
|
|
}
|
|
const items = hubItems(window);
|
|
clampHubSelection(window, items);
|
|
switch (event.type) {
|
|
case "scroll-up":
|
|
moveHubSelection(window, items, -1);
|
|
renderAndSubmit(window, frameId);
|
|
return;
|
|
case "scroll-down":
|
|
moveHubSelection(window, items, 1);
|
|
renderAndSubmit(window, frameId);
|
|
return;
|
|
case "click": {
|
|
const item = items[window.selectedIndex];
|
|
item?.onSelect?.();
|
|
renderAndSubmit(window, frameId);
|
|
return;
|
|
}
|
|
default:
|
|
frameTimings.finishFrame(frameId, "discarded: terminal hub ignored input");
|
|
return;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Window id of the view already showing this session's terminal, if any.
|
|
* Includes views that were requested but whose surface hasn't opened yet, so
|
|
* a quick double-select can't spawn two windows for one session.
|
|
*/
|
|
/** Sidebar glyph of the (open or opening) view window on a session, or null. */
|
|
function viewGlyphForSocket(connectionId: string, socket: string): string | null {
|
|
for (const window of windows.values()) {
|
|
if (window.kind === "view" && window.connectionId === connectionId && window.socket === socket) {
|
|
return window.glyph || null;
|
|
}
|
|
}
|
|
for (const pending of pendingViews.values()) {
|
|
if (pending.connectionId === connectionId && pending.socket === socket) return pending.glyph || null;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
function viewWindowIdForSocket(connectionId: string, socket: string): string | null {
|
|
for (const window of windows.values()) {
|
|
if (window.kind === "view" && window.connectionId === connectionId && window.socket === socket) {
|
|
return window.windowId;
|
|
}
|
|
}
|
|
for (const [windowId, pending] of pendingViews) {
|
|
if (pending.connectionId === connectionId && pending.socket === socket) return windowId;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
function openViewWindowFor(control: ControlConnection, session: G2MirrorSession): void {
|
|
openViewWindow(control, session.socket, sessionLabel(session));
|
|
}
|
|
|
|
/**
|
|
* Lowest unused sidebar-icon character for a new view window (its terminal
|
|
* icon shows ">N"). Glyphs free up when their window closes, since usage is
|
|
* recomputed from the live windows; "" (plain ">_" icon) if all are taken.
|
|
*/
|
|
function allocateViewGlyph(): string {
|
|
const used = new Set<string>();
|
|
for (const window of windows.values()) {
|
|
if (window.kind === "view") used.add(window.glyph);
|
|
}
|
|
for (const pending of pendingViews.values()) used.add(pending.glyph);
|
|
for (const glyph of TERMINAL_ICON_GLYPHS) {
|
|
if (!used.has(glyph)) return glyph;
|
|
}
|
|
return "";
|
|
}
|
|
|
|
function openViewWindow(control: ControlConnection, socket: string, label: string): void {
|
|
const options = clientOptionsFor(control.config);
|
|
if (!options) return;
|
|
const windowId = `terminal:view:${nextViewSerial++}`;
|
|
const glyph = allocateViewGlyph();
|
|
pendingViews.set(windowId, { connectionId: control.config.id, socket, label, glyph, options });
|
|
post({
|
|
type: "open-window-request",
|
|
windowId,
|
|
title: label,
|
|
iconLetter: "T",
|
|
icon: "terminal",
|
|
iconGlyph: glyph || undefined,
|
|
focus: true,
|
|
// Default to tall sessions; the app display setting can override this.
|
|
heightMode: "max",
|
|
});
|
|
}
|
|
|
|
/** Preset names the user listed in Settings > Terminal (the wire protocol has no way to enumerate the server's). */
|
|
function launchPresetNames(): string[] {
|
|
const names: string[] = [];
|
|
for (const piece of terminalLaunchPresetsSetting.get().split(",")) {
|
|
const name = piece.trim();
|
|
if (name && !names.includes(name)) names.push(name);
|
|
}
|
|
return names;
|
|
}
|
|
|
|
/** Launch a preset on a host and open a view window on the new session. */
|
|
async function launchAndOpenView(control: ControlConnection, preset: string): Promise<string> {
|
|
if (!control.client) throw new Error("Not connected to the g2mirror server.");
|
|
const socket = await control.client.launchSession(preset);
|
|
openViewWindow(control, socket, preset);
|
|
return socket;
|
|
}
|
|
|
|
function paint(window: TerminalWindow): Plane[] {
|
|
return windowMenu(window).paint();
|
|
}
|
|
|
|
function paintContent(window: TerminalWindow): GrayImage {
|
|
return window.kind === "hub" ? paintHub(window) : paintView(window);
|
|
}
|
|
|
|
function paintHub(window: HubWindow): GrayImage {
|
|
if (window.mode === "add") {
|
|
return paintAddConnection(window);
|
|
}
|
|
const image = new GrayImage(window.viewportWidth, window.viewportHeight, 0);
|
|
const font = chromeFont();
|
|
const step = lineStep(font);
|
|
// No border box: the shell chrome (top bar + sidebar) already frames the app.
|
|
// Title and status share the top line.
|
|
const title = window.mode === "connections" ? "Terminal - Connections" : "Terminal";
|
|
image.drawText(font, 18, 10, title, 220);
|
|
const statusX = 18 + font.measureText(title) + 16;
|
|
image.drawText(font, statusX, 10, truncateText(font, hubStatusLine(window), Math.max(0, window.viewportWidth - statusX - 12)), 170);
|
|
|
|
let listTop = 16 + step;
|
|
if (window.mode === "sessions" && controls.size === 0) {
|
|
image.drawText(font, 24, listTop, "Add a g2mirror:// connection to get started, see:", 150);
|
|
image.drawText(font, 24, listTop + step, "https://github.com/jimrandomh/g2mirror", 190);
|
|
listTop += 2 * step + 6;
|
|
}
|
|
|
|
const items = hubItems(window);
|
|
clampHubSelection(window, items);
|
|
const hubRowH = listRowHeight(chromeFont());
|
|
const visibleRowCount = Math.max(1, ((window.viewportHeight - 6 - listTop) / hubRowH) | 0);
|
|
window.scrollRow = scrollToKeepSelectionVisible(window.scrollRow, window.selectedIndex, visibleRowCount, items.length);
|
|
const lastVisibleRow = Math.min(items.length, window.scrollRow + visibleRowCount);
|
|
for (let index = window.scrollRow; index < lastVisibleRow; index++) {
|
|
const y = listTop + (index - window.scrollRow) * hubRowH;
|
|
const item = items[index]!;
|
|
if (item.heading) {
|
|
image.drawText(chromeFont(), 20, y + 2, item.label, 140);
|
|
continue;
|
|
}
|
|
const selected = index === window.selectedIndex;
|
|
if (selected) {
|
|
// Match the shell convention: fill only when this window has focus, so
|
|
// an outline-only selection signals the sidebar owns input.
|
|
drawSelectionHighlight(image, 20, y - 2, window.viewportWidth - 40, hubRowH - 1, window.focused, 8);
|
|
}
|
|
// Activity gutter, open-window number column, then the label, truncated
|
|
// to the row (shared with the Glanceboard's Terminal widget).
|
|
drawSessionRow(image, chromeFont(), {
|
|
x: 22,
|
|
y: y + 2,
|
|
width: window.viewportWidth - 22 - 26,
|
|
label: item.label,
|
|
openGlyph: item.openGlyph ?? null,
|
|
active: Boolean(item.active),
|
|
phase: hubAnimationPhase,
|
|
value: selected ? 255 : 200,
|
|
});
|
|
}
|
|
if (items.length > visibleRowCount) {
|
|
drawListScrollbar(
|
|
image,
|
|
window.viewportWidth - 10,
|
|
listTop,
|
|
visibleRowCount * hubRowH - 4,
|
|
window.scrollRow,
|
|
visibleRowCount,
|
|
items.length,
|
|
);
|
|
}
|
|
|
|
return image;
|
|
}
|
|
|
|
function paintAddConnection(window: HubWindow): GrayImage {
|
|
const image = new GrayImage(window.viewportWidth, window.viewportHeight, 0);
|
|
const font = chromeFont();
|
|
const step = lineStep(font);
|
|
image.drawText(font, 18, 10, "Add connection", 220);
|
|
const instructionsY = 20 + step;
|
|
image.drawText(font, 24, instructionsY, "Type the g2mirror://token@host string in the", 170);
|
|
image.drawText(font, 24, instructionsY + step, "phone app (or use voice input from the menu).", 170);
|
|
const draftY = instructionsY + 3 * step;
|
|
const draft = terminalNewConnectionSetting.get();
|
|
image.drawText(font, 24, draftY, truncateLabel(draft || "(empty)", 52), 220);
|
|
if (window.addError) {
|
|
image.drawText(font, 24, draftY + step + 6, window.addError, 150);
|
|
}
|
|
image.drawText(font, 24, window.viewportHeight - font.lineHeight - 12, `click save ${GESTURE_DOUBLE_CLICK} cancel`, 110);
|
|
return image;
|
|
}
|
|
|
|
function truncateLabel(text: string, maxLength: number): string {
|
|
return text.length <= maxLength ? text : `${text.slice(0, Math.max(0, maxLength - 3))}...`;
|
|
}
|
|
|
|
function hubStatusLine(window: HubWindow): string {
|
|
if (window.mode === "connections") {
|
|
return "Select a connection to connect, disconnect, or remove.";
|
|
}
|
|
return sessionsStatusLine();
|
|
}
|
|
|
|
/** The sessions view's status line (also published with the session snapshot). */
|
|
function sessionsStatusLine(): string {
|
|
if (controls.size === 0) {
|
|
return "No connections configured.";
|
|
}
|
|
const anyRetrying = [...controls.values()].some((control) => control.reconnectTimer);
|
|
if (controls.size === 1) {
|
|
const control = [...controls.values()][0]!;
|
|
const status = control.config.enabled ? (control.state?.status ?? "Not connected.") : "Disconnected.";
|
|
return anyRetrying ? `${status} Retrying...` : status;
|
|
}
|
|
const summary = `${connectedControls().length} of ${controls.size} hosts connected.`;
|
|
return anyRetrying ? `${summary} Retrying...` : summary;
|
|
}
|
|
|
|
/**
|
|
* Draw one terminal row at a fixed integer cell pitch, so glyph columns stay
|
|
* aligned with the emulator's grid arithmetic even when a TTF face has
|
|
* fractional advances. Identical to drawText for the 6px bitmap default.
|
|
*/
|
|
function drawGridRow(
|
|
image: GrayImage,
|
|
font: UiFont,
|
|
cellWidth: number,
|
|
rowY: number,
|
|
text: string,
|
|
value: number,
|
|
): void {
|
|
let col = 0;
|
|
for (const char of text) {
|
|
const codePoint = char.codePointAt(0) ?? 32;
|
|
if (codePoint !== 32) {
|
|
const glyph = font.getGlyph(codePoint);
|
|
if (glyph && glyph.bbxWidth > 0) {
|
|
image.drawGlyph(font, glyph, col * cellWidth, rowY, value);
|
|
}
|
|
}
|
|
col++;
|
|
}
|
|
}
|
|
|
|
function paintView(window: ViewWindow): GrayImage {
|
|
const image = new GrayImage(window.viewportWidth, window.viewportHeight, 0);
|
|
if (!window.receivedData) {
|
|
const font = chromeFont();
|
|
image.drawText(font, 24, 110, window.status, 170);
|
|
return image;
|
|
}
|
|
|
|
const rows = window.gridRows;
|
|
const historyNext = window.historyNext;
|
|
const bufferLength = window.emulator.bufferLength();
|
|
const bottomTop = historyNext + bufferLength - rows;
|
|
const following = window.scrollTop === null;
|
|
const top = following ? bottomTop : clamp(window.scrollTop!, window.archiveStart, bottomTop);
|
|
|
|
// Draw the cursor only if its line is within the visible window.
|
|
const cursorScreenRow = historyNext + window.emulator.cursorRow() - top;
|
|
if (cursorScreenRow >= 0 && cursorScreenRow < rows) {
|
|
image.fillRect(window.emulator.cursorCol() * window.cellWidth, cursorScreenRow * window.cellHeight, window.cellWidth, window.cellHeight, 70);
|
|
}
|
|
|
|
// Stale content stays visible across a disconnect, so flag it: a status
|
|
// line over the bottom row whenever the session isn't actually attached.
|
|
// Text is deferred glyphs (always on top of raster), so the covered rows
|
|
// must be suppressed rather than painted over.
|
|
const statusBannerY = window.client.state().phase !== "attached"
|
|
? window.viewportHeight - window.cellHeight
|
|
: null;
|
|
|
|
for (let row = 0; row < rows; row++) {
|
|
const rowY = row * window.cellHeight;
|
|
if (statusBannerY !== null && rowY + window.cellHeight > statusBannerY) continue;
|
|
const absolute = top + row;
|
|
let text = "";
|
|
if (absolute >= historyNext) {
|
|
const bufferIndex = absolute - historyNext;
|
|
if (bufferIndex >= 0 && bufferIndex < bufferLength) text = window.emulator.lineAt(bufferIndex);
|
|
} else if (absolute >= window.archiveStart) {
|
|
text = window.archive[absolute - window.archiveStart] ?? "";
|
|
}
|
|
if (text.length) drawGridRow(image, window.font, window.cellWidth, rowY, text, 200);
|
|
}
|
|
|
|
if (!following) {
|
|
drawScrollIndicator(image, top, window.archiveStart, bottomTop);
|
|
}
|
|
if (statusBannerY !== null) {
|
|
image.fillRect(0, statusBannerY, window.viewportWidth, window.cellHeight, 0);
|
|
image.drawText(chromeFont(), 0, statusBannerY, window.status, 170);
|
|
}
|
|
return image;
|
|
}
|
|
|
|
/** Right-edge scrollbar showing the view position within the scrollback. */
|
|
function drawScrollIndicator(image: GrayImage, top: number, minTop: number, maxTop: number): void {
|
|
const trackX = image.width - 3;
|
|
image.fillRect(trackX, 0, 3, image.height, 30);
|
|
const fraction = clamp((top - minTop) / Math.max(1, maxTop - minTop), 0, 1);
|
|
const thumbHeight = 24;
|
|
const thumbY = Math.round((image.height - thumbHeight) * fraction);
|
|
image.fillRect(trackX, thumbY, 3, thumbHeight, 150);
|
|
}
|
|
|
|
/** Coalesce bursty repaint triggers (terminal output) into ~30fps renders. */
|
|
function scheduleRender(window: TerminalWindow): void {
|
|
if (window.renderScheduled) return;
|
|
window.renderScheduled = true;
|
|
setTimeout(() => {
|
|
window.renderScheduled = false;
|
|
renderAndSubmit(window, 0);
|
|
}, RENDER_COALESCE_MS);
|
|
}
|
|
|
|
function renderAndSubmit(window: TerminalWindow, inputFrameId: number): void {
|
|
if (!window.foreground || !screenOn) {
|
|
frameTimings.finishFrame(inputFrameId, "discarded: terminal window not visible");
|
|
return;
|
|
}
|
|
const frameId = inputFrameId > 0 ? inputFrameId : frameTimings.startFrame(`render:${window.windowId}`);
|
|
try {
|
|
const paintStartedAtMs = Date.now();
|
|
const planes = frameTimings.span(frameId, "paint", () =>
|
|
frameTimings.runWithFrame(frameId, () => paint(window)),
|
|
);
|
|
const paintMs = Date.now() - paintStartedAtMs;
|
|
const fingerprint = planesFingerprint(planes);
|
|
if (fingerprint === window.lastSubmittedFingerprint) {
|
|
frameTimings.finishFrame(frameId, "discarded: terminal content unchanged");
|
|
return;
|
|
}
|
|
const communicator = getActiveDisplay();
|
|
if (!communicator) {
|
|
frameTimings.finishFrame(frameId, "discarded: no active display");
|
|
return;
|
|
}
|
|
const { image, draws } = frameTimings.span(frameId, "flatten", () => flattenPlanesWithDraws(planes));
|
|
const buffer = frameTimings.span(frameId, "to8bpp", () => image.to8bppBuffer());
|
|
communicator.submitSurfaceFrame(
|
|
buffer.buffer,
|
|
window.surfaceId,
|
|
0,
|
|
0,
|
|
image.width,
|
|
image.height,
|
|
fingerprint,
|
|
paintMs,
|
|
frameId,
|
|
frameTimings.span(frameId, "prepareFrameDraws", () => prepareFrameDraws(draws)),
|
|
);
|
|
window.lastSubmittedFingerprint = fingerprint;
|
|
} catch (error) {
|
|
frameTimings.finishFrame(frameId, "discarded: terminal render failed");
|
|
console.error(`terminal worker render failed: ${error}`);
|
|
}
|
|
}
|
|
|
|
function sessionLabel(session: G2MirrorSession): string {
|
|
if (session.title) {
|
|
return session.title;
|
|
}
|
|
const hint = session.cwdHint.replace(/^_+/, "").replace(/_+/g, "/");
|
|
return hint || "session";
|
|
}
|
|
|
|
/** Dispatch an assistant tool-call (unprefixed name) to its handler. */
|
|
function handleTerminalTool(name: string, args: any): ToolResult | Promise<ToolResult> {
|
|
switch (name) {
|
|
case "list_sessions":
|
|
return toolListSessions();
|
|
case "send_input":
|
|
return toolSendInput(args);
|
|
case "read_screen":
|
|
return toolReadScreen();
|
|
case "list_launch_presets":
|
|
return toolListLaunchPresets();
|
|
case "launch_session":
|
|
return toolLaunchSession(args);
|
|
default:
|
|
return { ok: false, error: `Unknown terminal tool: ${name}` };
|
|
}
|
|
}
|
|
|
|
function toolListLaunchPresets(): ToolResult {
|
|
const presets = launchPresetNames();
|
|
if (!presets.length) {
|
|
return { ok: true, content: "No launch presets configured (Settings > Terminal > Launch presets)." };
|
|
}
|
|
return { ok: true, content: presets.map((preset) => `- ${preset}`).join("\n") };
|
|
}
|
|
|
|
/**
|
|
* Pick the host a launch_session call targets: by (partial) server-name match
|
|
* when `host` is given, else the sole connected host; ambiguity is an error
|
|
* listing the choices.
|
|
*/
|
|
function resolveLaunchControl(host: string): ControlConnection | { error: string } {
|
|
const connected = connectedControls();
|
|
if (!connected.length) return { error: "Not connected to any g2mirror server." };
|
|
if (host) {
|
|
const matches = connected.filter((control) =>
|
|
connectionDisplayName(control.config).toLowerCase().includes(host.toLowerCase()),
|
|
);
|
|
if (matches.length === 1) return matches[0]!;
|
|
return {
|
|
error: `Host "${host}" ${matches.length ? "is ambiguous among" : "does not match any of"}: ${connected
|
|
.map((control) => connectionDisplayName(control.config))
|
|
.join(", ")}`,
|
|
};
|
|
}
|
|
if (connected.length === 1) return connected[0]!;
|
|
return {
|
|
error: `Multiple hosts connected; pass host: ${connected
|
|
.map((control) => connectionDisplayName(control.config))
|
|
.join(", ")}`,
|
|
};
|
|
}
|
|
|
|
async function toolLaunchSession(args: any): Promise<ToolResult> {
|
|
const preset = String(args?.preset ?? "").trim();
|
|
if (!preset) return { ok: false, error: "launch_session requires a preset name." };
|
|
const control = resolveLaunchControl(String(args?.host ?? "").trim());
|
|
if ("error" in control) return { ok: false, error: control.error };
|
|
const socket = await launchAndOpenView(control, preset);
|
|
return { ok: true, content: `Launched "${preset}" (session ${socket}) and opened a window viewing it.` };
|
|
}
|
|
|
|
function toolListSessions(): ToolResult {
|
|
const connected = connectedControls();
|
|
const multiHost = connected.length > 1;
|
|
const lines: string[] = [];
|
|
for (const control of connected) {
|
|
const sessions = control.state?.sessions ?? [];
|
|
if (multiHost) {
|
|
lines.push(`${connectionDisplayName(control.config)}:`);
|
|
}
|
|
for (const session of sessions) {
|
|
const glyph = viewGlyphForSocket(control.config.id, session.socket);
|
|
const open = glyph ? ` [open in window ${glyph}]` : viewWindowIdForSocket(control.config.id, session.socket) ? " [open]" : "";
|
|
lines.push(`- ${sessionLabel(session)}${open}`);
|
|
}
|
|
if (!sessions.length && multiHost) {
|
|
lines.push("- (no live sessions)");
|
|
}
|
|
}
|
|
if (!lines.length) {
|
|
return { ok: true, content: "No live terminal sessions (run g2mirror <command> on a host)." };
|
|
}
|
|
return { ok: true, content: lines.join("\n") };
|
|
}
|
|
|
|
function toolSendInput(args: any): ToolResult {
|
|
const text = String(args?.text ?? "");
|
|
if (!text) return { ok: false, error: "send_input requires non-empty text." };
|
|
const view = resolveActiveView();
|
|
if (!view) return { ok: false, error: "No terminal session is open to send input to." };
|
|
view.client.submitInput(text);
|
|
return { ok: true, content: `Sent to ${view.label}.` };
|
|
}
|
|
|
|
function toolReadScreen(): ToolResult {
|
|
const view = resolveActiveView();
|
|
if (!view) return { ok: false, error: "No terminal session is open." };
|
|
const length = view.emulator.bufferLength();
|
|
const start = Math.max(0, length - view.gridRows);
|
|
const lines: string[] = [];
|
|
for (let index = start; index < length; index++) {
|
|
lines.push(view.emulator.lineAt(index));
|
|
}
|
|
const text = lines.join("\n").replace(/\s+$/, "");
|
|
return { ok: true, content: text || "(screen is empty)" };
|
|
}
|
|
|
|
/**
|
|
* The terminal view an `open`-tier tool acts on: a foregrounded view if any,
|
|
* else the last view to be active, else the sole open view. Null when no view
|
|
* is open (the model gets a tool error it can relay).
|
|
*/
|
|
function resolveActiveView(): ViewWindow | null {
|
|
const views: ViewWindow[] = [];
|
|
let foreground: ViewWindow | null = null;
|
|
for (const window of windows.values()) {
|
|
if (window.kind !== "view") continue;
|
|
views.push(window);
|
|
if (window.foreground) foreground = window;
|
|
}
|
|
if (foreground) return foreground;
|
|
if (activeViewId) {
|
|
const window = windows.get(activeViewId);
|
|
if (window && window.kind === "view") return window;
|
|
}
|
|
return views.length === 1 ? views[0]! : null;
|
|
}
|