mirror of
https://git.undefined-dev.fr/Blendman974/faceclaw.git
synced 2026-11-19 21:03:09 +00:00
148 lines
5.4 KiB
TypeScript
148 lines
5.4 KiB
TypeScript
import { type GrayImage } from "../../../graphics/image";
|
|
import { truncateText } from "../../../graphics/textwrap";
|
|
import { lineStep } from "../../../ui/metrics";
|
|
import { onWorkerStateChanged, readWorkerState } from "../../../ui/shell/worker-state";
|
|
import {
|
|
drawSessionRow,
|
|
isSessionRowActive,
|
|
SESSION_ACTIVITY_STEP_MS,
|
|
TERMINAL_SESSIONS_STATE_KEY,
|
|
type TerminalSessionsSnapshot,
|
|
} from "../../terminal/session-list";
|
|
import { glanceFont } from "../glance-font";
|
|
import { type GlanceWidget } from "../widget";
|
|
|
|
const PAD = 8;
|
|
const GUTTER_X = PAD;
|
|
/** A row fits while its glyph line clears this margin above the bottom edge. */
|
|
const BOTTOM_MARGIN = 2;
|
|
|
|
/**
|
|
* The Terminal app's session list at card size: the same hosts, sessions,
|
|
* activity indicators and open-window numbers as the hub window, painted
|
|
* with the same row painter from the snapshot the Terminal worker publishes.
|
|
* The indicator animates on the hub's cadence while any session is active;
|
|
* each row carries its own activity deadline, so nothing needs the worker
|
|
* once the snapshot is in.
|
|
*
|
|
* The worker sorts sessions most-recently-updated first; the widget keeps the
|
|
* order it first sees after the board opens so rows don't jump around while
|
|
* it is showing. Sessions that appear later go to the end of their host.
|
|
*/
|
|
export class TerminalWidget implements GlanceWidget {
|
|
private snapshot = readWorkerState(TERMINAL_SESSIONS_STATE_KEY) as TerminalSessionsSnapshot | undefined;
|
|
private unsubscribe: (() => void) | null = null;
|
|
private requestRender: (() => void) | null = null;
|
|
private phase = 0;
|
|
private animationTimer: ReturnType<typeof setInterval> | null = null;
|
|
/** Row key -> display rank, fixed from the first snapshot seen since start. */
|
|
private readonly rowOrder = new Map<string, number>();
|
|
|
|
start(requestRender: () => void): void {
|
|
this.requestRender = requestRender;
|
|
this.rowOrder.clear();
|
|
this.snapshot = readWorkerState(TERMINAL_SESSIONS_STATE_KEY) as TerminalSessionsSnapshot | undefined;
|
|
this.rankNewRows();
|
|
this.unsubscribe = onWorkerStateChanged(TERMINAL_SESSIONS_STATE_KEY, (snapshot) => {
|
|
this.snapshot = snapshot as TerminalSessionsSnapshot;
|
|
this.rankNewRows();
|
|
this.syncAnimation();
|
|
requestRender();
|
|
});
|
|
this.syncAnimation();
|
|
}
|
|
|
|
stop(): void {
|
|
this.requestRender = null;
|
|
this.unsubscribe?.();
|
|
this.unsubscribe = null;
|
|
if (this.animationTimer !== null) clearInterval(this.animationTimer);
|
|
this.animationTimer = null;
|
|
}
|
|
|
|
/** Give rows not seen yet the next ranks, in the snapshot's (recency) order. */
|
|
private rankNewRows(): void {
|
|
for (const host of this.snapshot?.hosts ?? []) {
|
|
for (const row of host.sessions) {
|
|
if (!this.rowOrder.has(row.key)) this.rowOrder.set(row.key, this.rowOrder.size);
|
|
}
|
|
}
|
|
}
|
|
|
|
private anyActive(nowMs = Date.now()): boolean {
|
|
return Boolean(this.snapshot?.hosts.some((host) => host.sessions.some((row) => isSessionRowActive(row, nowMs))));
|
|
}
|
|
|
|
/** Run the indicator clock only while something is active; the last tick clears expired ones. */
|
|
private syncAnimation(): void {
|
|
const shouldRun = this.anyActive();
|
|
if (shouldRun && this.animationTimer === null) {
|
|
this.phase = 0;
|
|
this.animationTimer = setInterval(() => {
|
|
this.phase = (this.phase + 1) % 2;
|
|
this.requestRender?.();
|
|
if (!this.anyActive()) this.syncAnimation();
|
|
}, SESSION_ACTIVITY_STEP_MS);
|
|
} else if (!shouldRun && this.animationTimer !== null) {
|
|
clearInterval(this.animationTimer);
|
|
this.animationTimer = null;
|
|
}
|
|
}
|
|
|
|
paint(image: GrayImage): void {
|
|
const font = glanceFont.small();
|
|
const step = lineStep(font);
|
|
const textWidth = image.width - 2 * PAD;
|
|
const snapshot = this.snapshot;
|
|
const nowMs = Date.now();
|
|
let y = PAD;
|
|
image.drawText(font, PAD, y, "Terminal", 150);
|
|
y += step;
|
|
|
|
if (!snapshot) {
|
|
image.drawText(font, PAD, y, "Open the Terminal app to connect", 120);
|
|
return;
|
|
}
|
|
const hosts = snapshot.hosts;
|
|
const sessionCount = hosts.reduce((sum, host) => sum + host.sessions.length, 0);
|
|
if (!hosts.length || !sessionCount) {
|
|
const text = !snapshot.configured
|
|
? "No connections configured"
|
|
: !hosts.length
|
|
? snapshot.status
|
|
: "No live sessions";
|
|
image.drawText(font, PAD, y, truncateText(font, text, textWidth), 120);
|
|
return;
|
|
}
|
|
|
|
// Rows are placed a line step apart but only need their line height to
|
|
// fit, so the last one may sit right against the bottom margin.
|
|
const fits = (top: number) => top + font.lineHeight <= image.height - BOTTOM_MARGIN;
|
|
const multiHost = hosts.length > 1;
|
|
for (const host of hosts) {
|
|
if (multiHost) {
|
|
if (!fits(y)) return;
|
|
image.drawText(font, PAD, y, truncateText(font, host.name, textWidth), 110);
|
|
y += step;
|
|
}
|
|
const rows = host.sessions
|
|
.slice()
|
|
.sort((a, b) => (this.rowOrder.get(a.key) ?? 0) - (this.rowOrder.get(b.key) ?? 0));
|
|
for (const row of rows) {
|
|
if (!fits(y)) return;
|
|
drawSessionRow(image, font, {
|
|
x: GUTTER_X,
|
|
y,
|
|
width: image.width - GUTTER_X - PAD,
|
|
label: row.label,
|
|
openGlyph: row.openGlyph,
|
|
active: isSessionRowActive(row, nowMs),
|
|
phase: this.phase,
|
|
value: 200,
|
|
});
|
|
y += step;
|
|
}
|
|
}
|
|
}
|
|
}
|