Glanceboard Terminal widget doesn't re-sort terminals while open

This commit is contained in:
James Babcock committed 2026-10-14 01:21:51 -07:00
1 parent 2f880479be
commit 8ae4b423b6
1 file changed
+22 -1
@@ -24,6 +24,10 @@ const BOTTOM_MARGIN = 2;
* The indicator animates on the hub's cadence while any session is active; * 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 * each row carries its own activity deadline, so nothing needs the worker
* once the snapshot is in. * 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 { export class TerminalWidget implements GlanceWidget {
private snapshot = readWorkerState(TERMINAL_SESSIONS_STATE_KEY) as TerminalSessionsSnapshot | undefined; private snapshot = readWorkerState(TERMINAL_SESSIONS_STATE_KEY) as TerminalSessionsSnapshot | undefined;
@@ -31,12 +35,17 @@ export class TerminalWidget implements GlanceWidget {
private requestRender: (() => void) | null = null; private requestRender: (() => void) | null = null;
private phase = 0; private phase = 0;
private animationTimer: ReturnType<typeof setInterval> | null = null; 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 { start(requestRender: () => void): void {
this.requestRender = requestRender; this.requestRender = requestRender;
this.rowOrder.clear();
this.snapshot = readWorkerState(TERMINAL_SESSIONS_STATE_KEY) as TerminalSessionsSnapshot | undefined; this.snapshot = readWorkerState(TERMINAL_SESSIONS_STATE_KEY) as TerminalSessionsSnapshot | undefined;
this.rankNewRows();
this.unsubscribe = onWorkerStateChanged(TERMINAL_SESSIONS_STATE_KEY, (snapshot) => { this.unsubscribe = onWorkerStateChanged(TERMINAL_SESSIONS_STATE_KEY, (snapshot) => {
this.snapshot = snapshot as TerminalSessionsSnapshot; this.snapshot = snapshot as TerminalSessionsSnapshot;
this.rankNewRows();
this.syncAnimation(); this.syncAnimation();
requestRender(); requestRender();
}); });
@@ -51,6 +60,15 @@ export class TerminalWidget implements GlanceWidget {
this.animationTimer = null; 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 { private anyActive(nowMs = Date.now()): boolean {
return Boolean(this.snapshot?.hosts.some((host) => host.sessions.some((row) => isSessionRowActive(row, nowMs)))); return Boolean(this.snapshot?.hosts.some((host) => host.sessions.some((row) => isSessionRowActive(row, nowMs))));
} }
@@ -107,7 +125,10 @@ export class TerminalWidget implements GlanceWidget {
image.drawText(font, PAD, y, truncateText(font, host.name, textWidth), 110); image.drawText(font, PAD, y, truncateText(font, host.name, textWidth), 110);
y += step; y += step;
} }
for (const row of host.sessions) { 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; if (!fits(y)) return;
drawSessionRow(image, font, { drawSessionRow(image, font, {
x: GUTTER_X, x: GUTTER_X,