diff --git a/app/apps/nightscout/nightscout-app.ts b/app/apps/nightscout/nightscout-app.ts index 6458541a..01032319 100644 --- a/app/apps/nightscout/nightscout-app.ts +++ b/app/apps/nightscout/nightscout-app.ts @@ -84,7 +84,8 @@ function buildNightscoutTrayIcon(state: NightscoutState): GrayImage { const font = getDefaultSmallFont(); const nowMs = Date.now(); const label = state.latest ? `${state.latest.sgv}` : "--"; - const labelWidth = font.measureText(label); + // A TTF face measures fractionally; the icon's size and layout need whole pixels. + const labelWidth = Math.ceil(font.measureText(label)); const warning = evaluateNightscoutAlerts(state, loadNightscoutThresholds(), nowMs).any; const graphEnd = labelWidth + 4 + TRAY_GRAPH_WIDTH; const image = new GrayImage(graphEnd + (warning ? 24 : 0), TRAY_HEIGHT, 0); diff --git a/app/apps/terminal/terminal-app.worker.ts b/app/apps/terminal/terminal-app.worker.ts index 87bf9fdc..0f32c5d3 100644 --- a/app/apps/terminal/terminal-app.worker.ts +++ b/app/apps/terminal/terminal-app.worker.ts @@ -6,9 +6,15 @@ * connected host's heading launches a shell there), 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. + * - Selecting a session in the hub shows it in the hub's own window: a view + * takes the hub's place in `windows` under the same window id, and + * double-click puts the hub back. The window grows to the view's height + * meanwhile. Sessions already open in a window of their own focus it instead. + * - "terminal:view:N": a session in a window of its own, opened with "Open in + * new window" from the window menu (or by launching a session). + * + * Each view has its own websocket connection (the protocol allows one attached + * session per connection) and its own xterm emulator. * * Connections are g2mirror://@[:port] strings (g2mirrors:// for * TLS), stored as JSON in the settings store (see connections.ts). Each @@ -69,7 +75,7 @@ import { drawSubmenuIndicator, submenuItem, type MenuItem } from "../../ui/menu" import { Menu, type MenuDrawArgs } from "../../ui/menu-core"; import { centeredTextY, lineStep, listRowHeight, textInkHeight } from "../../ui/metrics"; import { WindowMenu, WindowMenuLayer } from "../../ui/window-menu"; -import { appViewportSize } from "../../ui/shell/geometry"; +import { appViewportSize, type WindowHeightMode } from "../../ui/shell/geometry"; import type { WorkerAppMessage, WorkerAppReply } from "../../ui/shell/worker-window"; import type { ToolResult, ToolSpec } from "../../assistant/tool-registry"; @@ -109,7 +115,10 @@ const LAUNCH_PRESETS_KEY = "terminal.launchPresets"; type BaseWindow = { windowId: string; surfaceId: string; - /** Shell-side window title, echoed back in the open-window message. */ + /** + * Window menu title: the shell-side window title, echoed back in the + * open-window message, or the session's label for a view in the hub's window. + */ title: string; /** Content viewport from the shell's open-window message; grid = viewport / cell. */ viewportWidth: number; @@ -155,6 +164,11 @@ type HubWindow = BaseWindow & { type ViewWindow = BaseWindow & { kind: "view"; + /** + * Set when this view is showing in the hub's window rather than one of its + * own: the hub it replaced, which double-click restores. + */ + hub: HubWindow | null; /** Grid font and integer cell pitch, captured at window creation. */ font: UiFont; cellWidth: number; @@ -202,8 +216,13 @@ type PendingView = { options: G2MirrorClientOptions; }; +type Viewport = { width: number; height: number }; + const windows = new Map(); const pendingViews = new Map(); +// Windows waiting on the shell's answer to a set-height-mode, by window id: +// what to do with the viewport it comes back with (see changeWindowHeight). +const pendingHeightChanges = new Map void>(); // 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; @@ -338,6 +357,12 @@ global.onmessage = (event: { data: WorkerAppMessage }) => { openWindow(message.windowId, message.surfaceId, message.title, message.viewport); break; case "resize-window": { + const heightChange = pendingHeightChanges.get(message.windowId); + if (heightChange) { + pendingHeightChanges.delete(message.windowId); + heightChange(message.viewport); + break; + } const window = windows.get(message.windowId); if (!window || (window.viewportWidth === message.viewport.width && window.viewportHeight === message.viewport.height)) break; window.viewportWidth = message.viewport.width; @@ -543,18 +568,24 @@ function openWindow(windowId: string, surfaceId: string, title: string, viewport updateHubAnimation(); } +/** Drop a view's connection for good: its window closed or the hub took its place. */ +function stopViewConnection(window: ViewWindow): void { + for (const unsubscribe of window.unsubscribers.splice(0)) { + unsubscribe(); + } + if (window.reconnectTimer) { + clearTimeout(window.reconnectTimer); + window.reconnectTimer = null; + } + window.client.stop(); +} + function closeWindow(windowId: string): void { const window = windows.get(windowId); if (!window) return; + pendingHeightChanges.delete(windowId); 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(); + stopViewConnection(window); } if (window.kind === "hub" && window.mode === "add") { // Window closed mid-add: shut the phone editor down. @@ -759,10 +790,11 @@ function routeBell(connectionId: string, socket: string, lastBellAtMs: number): } } 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. + // Wake to the belling session's view window, or the hub's window (whatever + // it is showing) 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 hubId = [...windows.values()].find((window) => window.kind === "hub" || window.hub)?.windowId ?? null; const target = viewId ?? hubId; if (target) post({ type: "wake-window", windowId: target }); } @@ -895,6 +927,7 @@ function createViewWindow( const client = new G2MirrorClient({ ...view.options, cols: gridCols, rows: gridRows }); const window: ViewWindow = { kind: "view", + hub: null, windowId, surfaceId, title, @@ -1053,12 +1086,41 @@ function connectedControls(): ControlConnection[] { return [...controls.values()].filter(isControlConnected); } +/** + * What the window menu's "Open in new window" does here, if it applies: to the + * session selected in the hub's list, or the session showing in the hub's + * window (which gives way to the list, as a session has one window at a time). + */ +function openInNewWindowAction(window: TerminalWindow): (() => void) | null { + if (window.kind === "hub") { + const row = hubMenu(window).selectedItem?.sessionRow; + if (!row) return null; + return () => { + if (!focusSessionWindow(row.control, row.session)) openViewWindowFor(row.control, row.session); + }; + } + const control = window.hub && controls.get(window.connectionId); + if (!control) return null; + return () => returnToHub(window, () => openViewWindow(control, window.socket, window.label)); +} + function windowMenuItems(window: TerminalWindow): MenuItem[] { - const items: MenuItem[] = [ + const items: MenuItem[] = []; + const openInNewWindow = openInNewWindowAction(window); + if (openInNewWindow) { + items.push({ + label: "Open in new window", + onSelect: (ctx) => { + ctx.stack.pop(); + openInNewWindow(); + }, + }); + } + items.push( submenuItem("Settings", (ctx) => { ctx.stack.push(new WindowMenuLayer("Terminal settings", settingsMenuItems())); }), - ]; + ); if (window.kind === "hub") { const connected = connectedControls(); const multiHost = connected.length > 1; @@ -1142,8 +1204,14 @@ function handleInput(window: TerminalWindow, event: InputEvent, frameId: number) 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. + // Inside the hub's sub-sections and a session shown in its window, + // double-click backs out one level; at the top level (and in views with + // windows of their own) it yields focus to the sidebar as everywhere. + if (window.kind === "view" && window.hub) { + frameTimings.finishFrame(frameId, "discarded: terminal returning to the session list"); + returnToHub(window); + return; + } if (window.kind === "hub" && window.mode === "add") { cancelAddConnection(window); renderAndSubmit(window, frameId); @@ -1233,6 +1301,8 @@ type HubItem = { active?: boolean; /** Glyph of the view window showing this session (the number column). */ openGlyph?: string | null; + /** The live session this row lists, for the window menu's "Open in new window". */ + sessionRow?: { control: ControlConnection; session: G2MirrorSession }; onSelect?: () => void; }; @@ -1371,13 +1441,9 @@ function hubSessionItems(window: HubWindow): HubItem[] { label: sessionLabel(session), active: isSessionActive(control.config.id, session.socket), openGlyph: viewGlyphForSocket(control.config.id, session.socket), + sessionRow: { control, session }, onSelect: () => { - const windowId = viewWindowIdForSocket(control.config.id, session.socket); - if (windowId) { - post({ type: "focus-window", windowId }); - } else { - openViewWindowFor(control, session); - } + if (!focusSessionWindow(control, session)) showSessionInHub(window, control, session); }, }); } @@ -1587,10 +1653,89 @@ function viewWindowIdForSocket(connectionId: string, socket: string): string | n return null; } +/** Focus the window already showing a session; false if it has none. */ +function focusSessionWindow(control: ControlConnection, session: G2MirrorSession): boolean { + const windowId = viewWindowIdForSocket(control.config.id, session.socket); + if (!windowId) return false; + post({ type: "focus-window", windowId }); + return true; +} + function openViewWindowFor(control: ControlConnection, session: G2MirrorSession): void { openViewWindow(control, session.socket, sessionLabel(session)); } +/** + * Ask the shell to move a window to another height band, then run `apply` with + * the viewport it answers with: only the shell knows what a band comes to (the + * bottom switcher's height lives on the main thread), and a view's grid goes + * into its websocket handshake, so the size has to be right before it starts. + * Ignored if the window is already waiting on an answer (a repeated gesture). + */ +function changeWindowHeight(windowId: string, heightMode: WindowHeightMode, apply: (viewport: Viewport) => void): void { + if (pendingHeightChanges.has(windowId)) return; + pendingHeightChanges.set(windowId, apply); + post({ type: "set-height-mode", windowId, heightMode }); +} + +/** Make `incoming` what its window id shows, in place of `outgoing`. */ +function replaceWindowContent(outgoing: TerminalWindow, incoming: TerminalWindow): void { + incoming.foreground = outgoing.foreground; + incoming.focused = outgoing.focused; + incoming.lastSubmittedFingerprint = ""; + // Its pending timers and emulator callbacks must not paint over the surface. + outgoing.foreground = false; + windows.set(incoming.windowId, incoming); + updateHubAnimation(); + renderAndSubmit(incoming, 0); +} + +/** A session was selected in the list: show it in the hub's own window. */ +function showSessionInHub(hub: HubWindow, control: ControlConnection, session: G2MirrorSession): void { + const options = clientOptionsFor(control.config); + if (!options) return; + const label = sessionLabel(session); + // Tall like a session in its own window (see openViewWindow). + changeWindowHeight(hub.windowId, "max", (viewport) => { + const view = createViewWindow(hub.windowId, hub.surfaceId, label, viewport, { + connectionId: control.config.id, + socket: session.socket, + label, + glyph: "", + options, + }); + view.hub = hub; + if (hub.foreground) activeViewId = view.windowId; + replaceWindowContent(hub, view); + }); +} + +/** + * Back from a session in the hub's window to the list. The list keeps the order + * it had, with this session's row selected: viewing a session bumps its + * recency, and re-sorting on every return would shuffle the list while the user + * steps through it. `then` runs once the session's connection is gone. + */ +function returnToHub(view: ViewWindow, then?: () => void): void { + const hub = view.hub; + if (!hub) return; + changeWindowHeight(view.windowId, "min", (viewport) => { + stopViewConnection(view); + view.emulator.dispose(); + if (activeViewId === view.windowId) activeViewId = null; + hub.viewportWidth = viewport.width; + hub.viewportHeight = viewport.height; + hub.menu?.resize(viewport); + const items = hubItems(hub); + const row = items.findIndex( + (item) => item.sessionRow?.control.config.id === view.connectionId && item.sessionRow.session.socket === view.socket, + ); + hubMenu(hub).setItems(items, row >= 0 ? row : undefined); + replaceWindowContent(view, hub); + then?.(); + }); +} + /** * 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 diff --git a/app/g2/ios-preview-controller.ts b/app/g2/ios-preview-controller.ts index 134019df..728714e5 100644 --- a/app/g2/ios-preview-controller.ts +++ b/app/g2/ios-preview-controller.ts @@ -802,7 +802,7 @@ export class IosPreviewController { const display = this.display if (!display) return await display.configureSurface(id, { ...appViewportRect(heightMode, appId), zOrder: 0, transparency: 'opaque' }) - await display.setSurfaceVisible(id, visible) + await display.setSurfaceVisible(id, typeof visible === 'function' ? visible() : visible) }, setSurfaceVisible: (id, visible) => this.setWindowSurfaceVisible(id, visible), removeSurface: id => this.removeWindowSurface(id), diff --git a/app/graphics/image.ts b/app/graphics/image.ts index f03c6f78..0792454e 100644 --- a/app/graphics/image.ts +++ b/app/graphics/image.ts @@ -4,6 +4,9 @@ import type { MenuHighlightAnimation } from "../ui/menu-highlight-motion"; import { Glyph } from "./bdffont"; import { wrapText } from "./textwrap"; +// This file is also built by tests/tsconfig.json, which has no DOM or Node types. +declare const console: { warn(message: string): void }; + export const G2_LENS_WIDTH = 640; export const G2_LENS_HEIGHT = 480; /** RayNeo iO screen (per eye) with the Faceclaw firmware: 540x220. */ @@ -159,9 +162,15 @@ export class GrayImage { private memoizedSourceHash: number | null = null; constructor(width: number, height: number, fill = 0) { - this.width = width; - this.height = height; - this.pixels = new Uint8Array(width * height); + // A fractional width would put most rows' `y * width + x` between pixels, + // silently losing them. TTF text measures fractionally, so this is easy to + // hit; the stack is there because the size alone doesn't identify the caller. + if (!Number.isInteger(width) || !Number.isInteger(height)) { + console.warn(`GrayImage: rounding up non-integer size ${width}x${height}\n${new Error().stack}`); + } + this.width = Math.ceil(width); + this.height = Math.ceil(height); + this.pixels = new Uint8Array(this.width * this.height); if (fill !== 0) { this.clear(fill); } diff --git a/app/ui/shell/worker-window.ts b/app/ui/shell/worker-window.ts index 5b1b010a..59728842 100644 --- a/app/ui/shell/worker-window.ts +++ b/app/ui/shell/worker-window.ts @@ -79,6 +79,16 @@ export type WorkerAppReply = /** Window height: the standard 288px band ("min", default) or full screen ("max"). */ heightMode?: WindowHeightMode; } + | { + /** + * Move a window to another height band at runtime. The host resizes its + * surface and then always answers with resize-window, even if the size + * came out the same, so the worker can wait for the new viewport. + */ + type: "set-height-mode"; + windowId: string; + heightMode: WindowHeightMode; + } | { type: "set-title"; windowId: string; title: string } | { type: "set-attention"; windowId: string; attention: boolean } /** App-driven animation phase for sidebar icons that support an activity cursor. */ @@ -193,8 +203,12 @@ export type WorkerAppHostOptions = { navigationSensors?: { handle(request: NavigationSensorRequest): void; stop(): void }; openUrl?: (url: string) => void; playBuzzerSequence?: (payload: Uint8Array) => Promise | void; - /** Create/refresh a window surface on the compositor (no-op when disconnected). */ - configureSurface: (surfaceId: string, visible: boolean, heightMode: WindowHeightMode) => Promise; + /** + * Create/refresh a window surface on the compositor (no-op when disconnected). + * `visible` may be a function, read once the surface is configured, for a + * window that can lose the foreground in the meantime. + */ + configureSurface: (surfaceId: string, visible: boolean | (() => boolean), heightMode: WindowHeightMode) => Promise; setSurfaceVisible: (surfaceId: string, visible: boolean) => void; removeSurface: (surfaceId: string) => void; requestShellRender: () => void; @@ -320,6 +334,9 @@ export class WorkerAppHost { heightMode: message.heightMode, }); break; + case "set-height-mode": + this.setWindowHeightMode(message.windowId, message.heightMode); + break; case "set-attention": shell.setWindowAttention(message.windowId, message.attention); break; @@ -465,7 +482,7 @@ export class WorkerAppHost { // (a game in progress, a route): the bottom switcher's height changes // with what it holds, and full-height windows give way to it. relayout: () => { - viewport = appViewportSize(heightMode, this.options.appId); + viewport = appViewportSize(window.heightMode, this.options.appId); this.post({ type: "resize-window", windowId: spec.windowId, viewport }); }, layoutSize: () => viewport, @@ -541,6 +558,24 @@ export class WorkerAppHost { return window; } + /** The worker asked for another height band (see set-height-mode). */ + private setWindowHeightMode(windowId: string, heightMode: WindowHeightMode): void { + const window = this.openWindows.has(windowId) ? shell.getWindows().find((w) => w.windowId === windowId) : undefined; + if (!window) return; + window.heightMode = heightMode; + // Resize the surface first, so the frame answering resize-window fits it. + void this.options + .configureSurface(window.surfaceId, () => shell.foregroundWindow()?.windowId === windowId, heightMode) + .catch((error) => { + console.error(`surface resize for ${windowId} failed: ${error}`); + }) + .then(() => { + window.relayout?.(); + // The chrome (top bar, bottom switcher) follows the foreground's band. + this.options.requestShellRender(); + }); + } + /** * Post a tool-call to the worker and resolve when its tool-result comes back. * Resolves with a tool error on timeout so a hung worker yields a tool error diff --git a/tests/app-layout.test.cjs b/tests/app-layout.test.cjs index aa238318..745e1cd8 100644 --- a/tests/app-layout.test.cjs +++ b/tests/app-layout.test.cjs @@ -984,7 +984,7 @@ test('Terminal resize keeps the view identity, negotiates its grid and skips pos client: { stop: () => calls.push('stop'), setViewport: (...size) => calls.push(size) }, }; const resize = resizeHandler('app/apps/terminal/terminal-app.worker.ts', { - windows: new Map([['test', window]]), clearTimeout: (timer) => calls.push(timer), + windows: new Map([['test', window]]), pendingHeightChanges: new Map(), clearTimeout: (timer) => calls.push(timer), TerminalEmulator: class { constructor(cols, rows) { this.cols = cols; this.rows = rows; } }, reconnectView: (view) => { assert.equal(view, window); calls.push('reconnect'); }, scheduleRender: () => calls.push('paint'), diff --git a/tests/gray-image.test.cjs b/tests/gray-image.test.cjs new file mode 100644 index 00000000..bce7b1b9 --- /dev/null +++ b/tests/gray-image.test.cjs @@ -0,0 +1,23 @@ +const test = require('node:test'); +const assert = require('node:assert/strict'); +const { GrayImage } = require('../.test-build/app/graphics/image.js'); + +test('a non-integer size rounds up, warns, and leaves every pixel addressable', t => { + const warn = t.mock.method(console, 'warn', () => {}); + const image = new GrayImage(78.625, 23.5, 7); + assert.deepEqual([image.width, image.height, image.pixels.length], [79, 24, 79 * 24]); + assert.ok(image.pixels.every(value => value === 7)); + for (let y = 0; y < image.height; y++) { + image.setPixel(78, y, 200); + assert.equal(image.getPixel(78, y), 200, `row ${y}`); + } + assert.equal(warn.mock.callCount(), 1); + assert.match(warn.mock.calls[0].arguments[0], /78\.625x23\.5/); +}); + +test('a whole-number size is kept as given, without a warning', t => { + const warn = t.mock.method(console, 'warn', () => {}); + const image = new GrayImage(79, 24); + assert.deepEqual([image.width, image.height], [79, 24]); + assert.equal(warn.mock.callCount(), 0); +}); diff --git a/tests/nightscout.test.cjs b/tests/nightscout.test.cjs index 8dcbb916..34340701 100644 --- a/tests/nightscout.test.cjs +++ b/tests/nightscout.test.cjs @@ -125,14 +125,14 @@ test('overflow warning backgrounds cover graph pixels and deferred graph labels' assert.equal(baked.getPixel(200, 8), 56, 'uncovered graph remains visible'); }); -function fixture(always = false) { +function fixture(always = false, GrayImage = Image, uiFont = font) { let time = now, icon, tick, changed, notify, renders = 0; const state = { ...healthy, available: true, latest: { sgv: 100, timestampMs: now }, history: [], units: 'mg/dL', delta: 0, direction: '', iob: 1, cob: 0, pumpStatus: '0m ago', configurationMissing: false }; const bridge = { snapshot: () => state, onStateChange(fn) { notify = fn; fn(state); } }; const settings = { loadNightscoutThresholds: () => limits, nightscoutAlwaysShowInTopBarSetting: { get: () => always }, onAnySettingChanged(fn) { changed = fn; }, isNightscoutSettingsConfigured: () => true }; const deps = { - '../../graphics/image': { GrayImage: Image }, - '../../graphics/ui-fonts': { getDefaultSmallFont: () => font, getDefaultLargeFont: () => font }, + '../../graphics/image': { GrayImage }, + '../../graphics/ui-fonts': { getDefaultSmallFont: () => uiFont, getDefaultLargeFont: () => uiFont }, '../../native/nightscout-bridge': { nightscoutBridge: bridge }, '../../ui/dashboard-settings': settings, './nightscout-alerts': alerts, @@ -181,6 +181,22 @@ test('tray is hidden by default and clears recovered measurement warnings immedi second.close(); assert.equal(f.icon(), null); }); +test('tray keeps every pixel row when a TTF font measures the reading fractionally', () => { + const { GrayImage } = require('../.test-build/app/graphics/image.js'); + // 8.875 is Roboto-Light 16's digit advance under iOS Core Text. + const f = fixture(true, GrayImage, { ...font, measureText: text => text.length * 8.875, drawText() {} }); + f.state.latest = { sgv: 142, timestampMs: now }; + // Spans the graph's full 60..200 range, so its line crosses every row. + f.state.history = [{ timestampMs: now - 10 * 60_000, sgv: 200 }, { timestampMs: now, sgv: 60 }]; + f.app.startNightscoutTrayIcon(); + const icon = f.icon(); + assert.ok(Number.isInteger(icon.width), `width ${icon.width}`); + for (let y = 0; y < icon.height; y++) { + const row = Array.from({ length: icon.width }, (_, x) => icon.getPixel(x, y)); + assert.ok(row.some(value => value !== 0), `row ${y} of the graph is drawn`); + } +}); + test('main page highlights only the breached field and removes highlights on recovery', () => { const f = fixture(); const page = load('app/apps/nightscout/nightscout.ts', { diff --git a/tests/terminal-activity.test.cjs b/tests/terminal-activity.test.cjs index 78a5be77..af8678c3 100644 --- a/tests/terminal-activity.test.cjs +++ b/tests/terminal-activity.test.cjs @@ -15,8 +15,8 @@ function worker() { const messages = []; const file = 'app/apps/terminal/terminal-app.worker.ts'; const source = ts.createSourceFile(file, read(file), ts.ScriptTarget.Latest, true); - const functions = new Set(['recencyKey', 'post', 'renderHubWindows', 'isSessionActive', 'isWindowSessionActive', 'hubAnimationShouldRun', 'syncWindowIconActivity', 'updateHubAnimation', 'noteSessionActivity', 'openWindow', 'closeWindow']); - const variables = new Set(['windows', 'pendingViews', 'activeViewId', 'sessionRecency', 'sessionActivity', 'ACTIVITY_ACTIVE_MS', 'HUB_ANIMATION_STEP_MS', 'hubAnimationPhase', 'hubAnimationTimer', 'windowIconActivity', 'screenOn']); + const functions = new Set(['recencyKey', 'post', 'stopViewConnection', 'renderHubWindows', 'isSessionActive', 'isWindowSessionActive', 'hubAnimationShouldRun', 'syncWindowIconActivity', 'updateHubAnimation', 'noteSessionActivity', 'openWindow', 'closeWindow']); + const variables = new Set(['windows', 'pendingViews', 'pendingHeightChanges', 'activeViewId', 'sessionRecency', 'sessionActivity', 'ACTIVITY_ACTIVE_MS', 'HUB_ANIMATION_STEP_MS', 'hubAnimationPhase', 'hubAnimationTimer', 'windowIconActivity', 'screenOn']); const selected = source.statements.filter(s => ts.isFunctionDeclaration(s) && functions.has(s.name?.text) || ts.isVariableStatement(s) && s.declarationList.declarations.some(d => variables.has(d.name.getText(source))) || diff --git a/tests/terminal-in-place-view.test.cjs b/tests/terminal-in-place-view.test.cjs new file mode 100644 index 00000000..fb5abe1a --- /dev/null +++ b/tests/terminal-in-place-view.test.cjs @@ -0,0 +1,483 @@ +const test = require('node:test'); +const assert = require('node:assert/strict'); +const { loader } = require('./helpers/load-typescript.cjs'); + +const HUB = 'terminal:hub'; +const MIN = { width: 552, height: 260 }; +const MAX = { width: 552, height: 452 }; +const font = { lineHeight: 12, measureText: (text) => text.length * 6, getGlyph: () => null }; +const tick = () => new Promise((resolve) => setTimeout(resolve, 0)); + +// A recording stand-in for the worker's images: enough to see what a frame shows. +function fakeImageModule() { + class GrayImage { + constructor(width, height) { + this.width = width; + this.height = height; + this.texts = []; + return new Proxy(this, { get: (target, key) => (key in target ? target[key] : () => {}) }); + } + drawText(_font, _x, _y, text) { this.texts.push(text); } + drawMenuSelection(row) { this.texts.push(...row.texts.map((text) => `[${text}]`)); } + } + return { GrayImage }; +} + +// Runs the real worker against the shell's message protocol, stubbing only what +// sits behind it: the websocket client, the emulator, the display and the menu host. +function terminal({ sessions = ['vim', 'top'], presets = '', bellWake = true } = {}) { + const replies = [], clients = [], frames = [], menus = []; + const global = { postMessage: (message) => replies.push(message) }; + + class FakeClient { + constructor(options) { + this.options = options; + this.stopped = false; + this.attached = null; + this.sent = []; + this.listeners = { state: [], bell: [], activity: [] }; + this.statusText = 'Connected.'; + clients.push(this); + } + state() { return { phase: this.stopped ? 'idle' : 'connected', status: this.statusText, serverName: 'host', sessions: sessions.map((title) => ({ socket: title, title, cwdHint: '' })) }; } + start() { this.stopped = false; this.listeners.state.forEach((listener) => listener(this.state())); } + stop() { this.stopped = true; } + setViewport(cols, rows) { this.viewport = [cols, rows]; } + connectSession(socket) { this.attached = socket; } + sendInput(text) { this.sent.push(text); } + submitInput(text) { this.sent.push(text); } + listSessions() {} + view() {} + requestHistory() {} + onStateChange(listener) { this.listeners.state.push(listener); return () => {}; } + onBell(listener) { this.listeners.bell.push(listener); return () => {}; } + onTitle() { return () => {}; } + onActivity(listener) { this.listeners.activity.push(listener); return () => {}; } + onSnapshot() { return () => {}; } + onHistoryLines() { return () => {}; } + onSessionAttached() { return () => {}; } + onTerminalData() { return () => {}; } + onSessionDetached() { return () => {}; } + } + + class FakeEmulator { + dispose() { this.disposed = true; } + write() {} + reset() {} + bufferLength() { return 0; } + lineAt() { return ''; } + cursorRow() { return 0; } + cursorCol() { return 0; } + } + + // Click runs the first item, the way the real menu does at its initial selection. + class FakeWindowMenu { + constructor(options) { this.options = options; this.opened = null; menus.push(this); } + isOpen() { return this.opened !== null; } + open(items = this.options.items()) { this.opened = items; } + close() { this.opened = null; } + resize(size) { this.options.size = size; } + paint() { this.options.items(); return [this.options.paintBase()]; } + async handleInput(event) { + if (event.type === 'click') { + await this.opened[0].onSelect({ stack: { pop: () => { this.opened = null; }, push() {} } }); + } + } + } + + const setting = (value) => ({ id: 'setting', get: () => value, set() {} }); + const image = fakeImageModule(); + const load = loader({ global, setTimeout, clearTimeout, setInterval, clearInterval }, { + '@nativescript/core/globals': {}, + './background': { hasTerminalBackgroundWork: () => false }, + '../../ui/shell/worker-lifecycle': { finishWorkerShutdown() {} }, + '../../graphics/image': image, + '../graphics/image': image, + '../../graphics/plane': { + planesFingerprint: (planes) => JSON.stringify([planes[0].width, planes[0].height, planes[0].texts]), + flattenPlanesWithDraws: (planes) => ({ image: { ...planes[0], width: planes[0].width, height: planes[0].height, texts: planes[0].texts, to8bppBuffer: () => ({ buffer: planes[0].texts }) }, draws: [] }), + }, + '../../graphics/glyph-wire': { prepareFrameDraws: () => null }, + '../../graphics/ui-fonts': { + getDefaultSmallFont: () => font, + getTerminalFontConfig: () => ({ font, cellWidth: 6, cellHeight: 12 }), + }, + '../../graphics/icons': { TERMINAL_ICON_GLYPHS: ['1', '2', '3'] }, + '../../native/frame-timings': { + startFrame: () => 1, finishFrame() {}, logFrame() {}, + span: (_frame, _name, run) => run(), runWithFrame: (_frame, run) => run(), + }, + '../../native/active-display': { + getActiveDisplay: () => ({ + submitSurfaceFrame: (texts, surfaceId, _x, _y, width, height) => frames.push({ surfaceId, width, height, texts }), + }), + }, + '../../ui/gestures': { GESTURE_DOUBLE_CLICK: 'double-tap' }, + '../../native/g2mirror-client': { G2MirrorClient: FakeClient }, + '../../native/settings-store': { onSettingsStoreChanged() {} }, + '../../ui/dashboard-settings': { + enumSettingMenuItem() {}, textSettingMenuItem() {}, toggleSettingMenuItem() {}, + terminalAutoReconnectSetting: setting(false), + terminalDisplayLayoutSetting: setting('default'), + terminalLaunchPresetsSetting: setting(presets), + terminalNewConnectionSetting: setting(''), + terminalWakeOnBellSetting: setting(bellWake), + }, + '../../ui/font-picker': { terminalFontPickerMenuItem() {} }, + './connections': { + TERMINAL_CONNECTIONS_KEY: 'terminal.connections', + connectionDisplayName: (config) => config.id, + loadConnections: () => [{ id: 'c1', url: 'ok', serverName: 'host', enabled: true }], + parseConnectionString: () => ({ host: 'host' }), + saveConnections() {}, updateConnection() {}, + }, + './terminal-emulator': { TerminalEmulator: FakeEmulator }, + '../../ui/menu': { drawSubmenuIndicator() {}, submenuItem: (label, onSelect) => ({ label, onSelect }) }, + '../../ui/window-menu': { WindowMenu: FakeWindowMenu, WindowMenuLayer: class {} }, + '../../ui/shell/geometry': { appViewportSize: () => MAX }, + }); + load('app/apps/terminal/terminal-app.worker.ts'); + + const send = (message) => global.onmessage({ data: message }); + const input = (type, windowId = HUB) => send({ type: 'input', windowId, event: { type }, frameId: 0, focused: true }); + const openWindow = (windowId, viewport) => { + send({ type: 'open-window', windowId, surfaceId: `window:${windowId}`, title: 'Terminal', viewport }); + send({ type: 'foreground', windowId, foreground: true, focused: true }); + }; + const repliesOf = (type) => replies.filter((reply) => reply.type === type); + return { + clients, frames, replies, menus, send, input, openWindow, repliesOf, + lastFrame: () => frames[frames.length - 1], + clearReplies: () => { replies.length = 0; }, + openMenu: (windowId = HUB) => input('short-then-long-press', windowId), + menuLabels: () => Array.from(menus.find((menu) => menu.isOpen()).opened, (item) => item.label), + closeMenu: () => menus.forEach((menu) => menu.close()), + // Past Manage Connections and Settings to the first session row. + selectFirstSession: () => { input('scroll-down'); input('scroll-down'); }, + answerHeight: (viewport, windowId = HUB) => send({ type: 'resize-window', windowId, viewport }), + }; +} + +function hubShowingList() { + const t = terminal(); + t.openWindow(HUB, MIN); + t.selectFirstSession(); + return t; +} + +function hubShowingSession() { + const t = hubShowingList(); + t.input('click'); + t.answerHeight(MAX); + t.clearReplies(); + return t; +} + +test('selecting a session shows it in the same window, at the height the shell answers with', () => { + const t = hubShowingList(); + assert.equal(t.clients.length, 1, 'just the control connection so far'); + assert.ok(t.lastFrame().texts.includes('[vim]') || t.lastFrame().texts.some((text) => text.includes('vim'))); + + t.input('click'); + assert.deepEqual(t.repliesOf('open-window-request'), [], 'no new window'); + assert.deepEqual(t.repliesOf('set-height-mode').map((reply) => reply.heightMode), ['max']); + assert.equal(t.clients.length, 1, 'no connection before the size is known'); + + t.answerHeight(MAX); + const view = t.clients[1]; + assert.deepEqual([view.options.cols, view.options.rows], [92, 37], 'grid from the answered viewport'); + assert.equal(view.attached, 'vim'); + assert.deepEqual([t.lastFrame().surfaceId, t.lastFrame().width, t.lastFrame().height], [`window:${HUB}`, MAX.width, MAX.height]); + assert.ok(!t.lastFrame().texts.includes('Terminal'), 'the list is gone'); +}); + +test('a repeated tap before the shell answers starts one view', () => { + const t = hubShowingList(); + t.input('click'); + t.input('click'); + assert.equal(t.repliesOf('set-height-mode').length, 1); + t.answerHeight(MAX); + assert.equal(t.clients.length, 2); +}); + +test('text input reaches the session shown in place', () => { + const t = hubShowingSession(); + t.send({ type: 'text-input', windowId: HUB, text: 'ls' }); + assert.deepEqual(t.clients[1].sent, ['ls']); +}); + +test('double-tap in a session shown in place goes back to the list, not the sidebar', () => { + const t = hubShowingSession(); + t.input('double-click'); + assert.deepEqual(t.repliesOf('yield-focus'), []); + assert.deepEqual(t.repliesOf('set-height-mode').map((reply) => reply.heightMode), ['min']); + assert.equal(t.clients[1].stopped, false, 'the session stays up until the window has resized'); + + t.answerHeight(MIN); + assert.equal(t.clients[1].stopped, true); + assert.deepEqual([t.lastFrame().width, t.lastFrame().height], [MIN.width, MIN.height]); + assert.ok(t.lastFrame().texts.includes('Terminal'), 'the list is back'); + assert.ok(t.lastFrame().texts.includes('[vim]'), 'with the session just viewed still selected'); + + t.input('double-click'); + assert.deepEqual(t.repliesOf('yield-focus').map((reply) => reply.windowId), [HUB], 'then double-tap yields as usual'); +}); + +test('coming back from a session keeps the list order despite its bumped recency, until the list next becomes visible', () => { + const t = hubShowingList(); + const sessionRows = () => t.lastFrame().texts.filter((text) => /vim|top/.test(text)); + t.input('scroll-down'); + assert.deepEqual(sessionRows(), ['vim', '[top]']); + t.input('click'); + t.answerHeight(MAX); + t.clients[0].listeners.activity.forEach((listener) => listener('top')); + + t.input('double-click'); + t.answerHeight(MIN); + assert.deepEqual(sessionRows(), ['vim', '[top]']); + + t.send({ type: 'foreground', windowId: HUB, foreground: false, focused: false }); + t.send({ type: 'foreground', windowId: HUB, foreground: true, focused: true }); + assert.deepEqual(sessionRows().map((text) => text.replace(/[[\]]/g, '')), ['top', 'vim'], 're-sorted as usual'); +}); + +test('the session just viewed stays selected when rows above it have shifted', () => { + const sessions = ['vim', 'top', 'htop']; + const t = terminal({ sessions }); + t.openWindow(HUB, MIN); + t.selectFirstSession(); + t.input('scroll-down'); + t.input('click'); + t.answerHeight(MAX); + // vim exits while top is on show, moving top's row up one (and htop into its place). + sessions.shift(); + t.clients[0].listeners.state.forEach((listener) => listener(t.clients[0].state())); + + t.input('double-click'); + t.answerHeight(MIN); + assert.ok(t.lastFrame().texts.includes('[top]')); +}); + +test('the window menu leads with Open in new window only while a session is selected', () => { + const t = terminal(); + t.openWindow(HUB, MIN); + t.openMenu(); + assert.equal(t.menuLabels()[0], 'Settings', 'Manage Connections is selected'); + t.closeMenu(); + t.input('scroll-down'); + t.openMenu(); + assert.equal(t.menuLabels()[0], 'Settings', 'Settings is selected'); + t.closeMenu(); + t.input('scroll-down'); + t.openMenu(); + assert.deepEqual(t.menuLabels().slice(0, 2), ['Open in new window', 'Settings']); +}); + +test('Open in new window on a selected session opens a window and leaves the list up', async () => { + const t = hubShowingList(); + t.openMenu(); + t.input('click'); + await tick(); + assert.deepEqual(t.repliesOf('set-height-mode'), []); + const [request] = t.repliesOf('open-window-request'); + assert.equal(request.title, 'vim'); + assert.equal(request.heightMode, 'max'); + assert.equal(request.focus, true); + assert.equal(t.repliesOf('open-window-request').length, 1); +}); + +test('selecting a session already in its own window focuses that window', async () => { + const t = hubShowingList(); + t.openMenu(); + t.input('click'); + await tick(); + const { windowId } = t.repliesOf('open-window-request')[0]; + t.send({ type: 'foreground', windowId: HUB, foreground: false, focused: false }); + t.openWindow(windowId, MAX); + t.send({ type: 'foreground', windowId: HUB, foreground: true, focused: true }); + t.clearReplies(); + + t.input('click'); + assert.deepEqual(t.repliesOf('focus-window').map((reply) => reply.windowId), [windowId]); + assert.deepEqual(t.repliesOf('set-height-mode'), []); +}); + +test('a session in place can move to a new window: the list comes back first, then the window opens', async () => { + const t = hubShowingSession(); + t.openMenu(); + assert.deepEqual(t.menuLabels().slice(0, 2), ['Open in new window', 'Settings']); + t.input('click'); + await tick(); + assert.deepEqual(t.repliesOf('set-height-mode').map((reply) => reply.heightMode), ['min']); + assert.deepEqual(t.repliesOf('open-window-request'), [], 'not while the old connection is up'); + + t.answerHeight(MIN); + assert.equal(t.clients[1].stopped, true); + const [request] = t.repliesOf('open-window-request'); + assert.equal(request.title, 'vim'); + assert.equal(request.iconGlyph, '1'); + assert.ok(t.lastFrame().texts.includes('Terminal')); +}); + +test('a session in its own window keeps its plain menu and double-tap yields focus', async () => { + const t = hubShowingList(); + t.openMenu(); + t.input('click'); + await tick(); + const { windowId } = t.repliesOf('open-window-request')[0]; + t.openWindow(windowId, MAX); + t.openMenu(windowId); + assert.equal(t.menuLabels()[0], 'Settings'); + t.closeMenu(); + t.input('double-click', windowId); + assert.deepEqual(t.repliesOf('yield-focus').map((reply) => reply.windowId), [windowId]); +}); + +test('a bell still finds the window while the hub is showing a session', () => { + const t = hubShowingSession(); + // A session with no view of its own wakes to the hub's window. + t.clients[0].listeners.bell.forEach((listener) => listener('top', Date.now() + 1000)); + assert.deepEqual(t.repliesOf('wake-window').map((reply) => reply.windowId), [HUB]); + t.clearReplies(); + + // The session on show: flagged only while the window is in the background. + t.send({ type: 'foreground', windowId: HUB, foreground: false, focused: false }); + t.clients[0].listeners.bell.forEach((listener) => listener('vim', Date.now() + 2000)); + assert.deepEqual(t.repliesOf('set-attention').map((reply) => reply.windowId), [HUB]); +}); + +test('a window backgrounded while waiting for the shell picks up the session without painting', () => { + const t = hubShowingList(); + t.input('click'); + t.send({ type: 'foreground', windowId: HUB, foreground: false, focused: false }); + const painted = t.frames.length; + t.answerHeight(MAX); + assert.equal(t.frames.length, painted); + t.send({ type: 'foreground', windowId: HUB, foreground: true, focused: true }); + assert.equal(t.lastFrame().height, MAX.height); +}); + +test('a list repaint scheduled before the swap does not paint over the session', async () => { + const sessions = ['vim', 'top']; + const t = terminal({ sessions }); + t.openWindow(HUB, MIN); + t.selectFirstSession(); + t.input('click'); + // A new session makes the list's next paint differ, so it would be submitted. + sessions.push('htop'); + t.clients[0].listeners.state.forEach((listener) => listener(t.clients[0].state())); + t.answerHeight(MAX); + await new Promise((resolve) => setTimeout(resolve, 80)); + assert.equal(t.lastFrame().height, MAX.height); + assert.ok(!t.lastFrame().texts.includes('Terminal')); +}); + +test('a session repaint scheduled before the swap does not paint over the list', async () => { + const t = hubShowingSession(); + t.input('double-click'); + t.clients[1].statusText = 'Reconnecting...'; + t.clients[1].listeners.state.forEach((listener) => listener(t.clients[1].state())); + t.answerHeight(MIN); + await new Promise((resolve) => setTimeout(resolve, 80)); + assert.equal(t.lastFrame().height, MIN.height); + assert.ok(t.lastFrame().texts.includes('Terminal')); +}); + +test('closing the window while the shell has not answered drops the change', () => { + const t = hubShowingList(); + t.input('click'); + t.send({ type: 'close-window', windowId: HUB }); + t.answerHeight(MAX); + assert.equal(t.clients.length, 1, 'no view connection started'); +}); + +test('closing the window while it shows a session stops that session', () => { + const t = hubShowingSession(); + t.send({ type: 'close-window', windowId: HUB }); + assert.equal(t.clients[1].stopped, true); +}); + +test('a later resize of a session in place reconnects at the new grid, and the list then gets its own size', () => { + const t = hubShowingSession(); + t.answerHeight({ width: 552, height: 400 }); + assert.equal(t.clients[1].stopped, false); + assert.deepEqual(t.clients[1].viewport, [92, 33]); + t.input('double-click'); + t.answerHeight({ width: 552, height: 240 }); + assert.equal(t.lastFrame().height, 240); +}); + +test('the shell host resizes the surface before answering with the new viewport', async () => { + const worker = { posted: [], postMessage(message) { this.posted.push(message); } }; + const surfaces = []; + const windows = []; + let renders = 0; + let foreground; + const load = loader({ global: { isIOS: false }, setTimeout, clearTimeout, Promise }, { + '../../graphics/image': {}, + './chrome-layer': { windowIcon() {} }, + '../../assistant/tool-registry': { toolRegistry: { removeAppTools() {} } }, + './geometry': { appViewportSize: (mode) => (mode === 'max' ? MAX : MIN) }, + '../../native/frame-timings': {}, + './worker-state': {}, + './shell': { shell: { + registerWindow: (window) => windows.push(window), + getWindows: () => windows, + foregroundWindow: () => foreground, + isWindowFocused: () => true, + } }, + }); + const { WorkerAppHost } = load('app/ui/shell/worker-window.ts'); + let finishConfigure = () => {}; + const host = new WorkerAppHost({ + appId: 'terminal', worker, + configureSurface: (surfaceId, visible, heightMode) => { + surfaces.push({ surfaceId, visible, heightMode }); + return heightMode === 'min' ? Promise.resolve() : new Promise((resolve) => { finishConfigure = resolve; }); + }, + requestShellRender: () => { renders++; }, + removeSurface() {}, + }); + worker.onmessage({ data: { type: 'worker-ready' } }); + const window = host.openWindow({ windowId: HUB, title: 'Terminal', iconLetter: 'T' }); + foreground = window; + await tick(); + worker.posted.length = 0; + renders = 0; + + worker.onmessage({ data: { type: 'set-height-mode', windowId: HUB, heightMode: 'max' } }); + assert.equal(window.heightMode, 'max', 'the chrome reads the band from here'); + const configured = surfaces[surfaces.length - 1]; + assert.equal(configured.heightMode, 'max'); + assert.equal(configured.visible(), true, 'asked late: the window may have lost the foreground'); + foreground = undefined; + assert.equal(configured.visible(), false); + foreground = window; + await tick(); + assert.deepEqual(worker.posted, [], 'no answer before the surface is ready'); + + finishConfigure(); + await tick(); + assert.deepEqual(worker.posted.map((message) => [message.type, message.viewport]), [['resize-window', MAX]]); + assert.equal(renders, 1); + + // Any later relayout (a switcher height change) keeps the new band. + worker.posted.length = 0; + window.relayout(); + assert.deepEqual(worker.posted.map((message) => message.viewport), [MAX]); + + // Unchanged band: still answered, so the worker can always wait on it. + worker.posted.length = 0; + worker.onmessage({ data: { type: 'set-height-mode', windowId: HUB, heightMode: 'min' } }); + await tick(); + worker.onmessage({ data: { type: 'set-height-mode', windowId: HUB, heightMode: 'min' } }); + await tick(); + assert.deepEqual(worker.posted.map((message) => message.viewport), [MIN, MIN]); + + // A closed window is not touched again. + window.close(); + const count = surfaces.length; + worker.onmessage({ data: { type: 'set-height-mode', windowId: HUB, heightMode: 'max' } }); + await tick(); + assert.equal(surfaces.length, count); +});