forked from Blendman974/faceclaw
Add glanceboard widget for T3code
This commit is contained in:
1 parent
043e5e74ce
commit
49a49d68ce
12 files changed
+453
-34
No files matched your search
@@ -1,11 +1,24 @@
|
||||
import { getStringSetting } from "../../native/settings-store";
|
||||
import { getBooleanSetting, getStringSetting } from "../../native/settings-store";
|
||||
import { QUADRANT_LAYOUT, SIX_SLOT_LAYOUT } from "./layout";
|
||||
|
||||
/** Configuration-only query usable by workers without loading widget/UI modules. */
|
||||
export function isTerminalWidgetConfigured(): boolean {
|
||||
/** Whether the current layout has `widgetId` in any slot (configuration only, enabled or not). */
|
||||
function isGlanceWidgetConfigured(widgetId: string): boolean {
|
||||
const layout = getStringSetting("glanceboard.layout", "2x2") === "2x3" ? SIX_SLOT_LAYOUT : QUADRANT_LAYOUT;
|
||||
// Both layouts store their slots under the original quadrants key. None
|
||||
// of the default slots needs a worker, so absent keys cannot retain one.
|
||||
return layout.slots.some((_, index) =>
|
||||
getStringSetting(`glanceboard.${QUADRANT_LAYOUT.id}.slot.${index}`, "none") === "terminal");
|
||||
getStringSetting(`glanceboard.${QUADRANT_LAYOUT.id}.slot.${index}`, "none") === widgetId);
|
||||
}
|
||||
|
||||
/** Configuration-only query usable by workers without loading widget/UI modules. */
|
||||
export function isTerminalWidgetConfigured(): boolean {
|
||||
return isGlanceWidgetConfigured("terminal");
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the T3 Code widget can show: it is in a slot and the Glanceboard is
|
||||
* enabled (the T3 worker holds its server connections open only then).
|
||||
*/
|
||||
export function isT3CodeWidgetInUse(): boolean {
|
||||
return getBooleanSetting("glanceboard.enabled", false) && isGlanceWidgetConfigured("t3code");
|
||||
}
|
||||
@@ -5,13 +5,14 @@ import { glanceWidgetSpans } from "./widgets";
|
||||
|
||||
export type GlanceSlotChoice = GlanceWidgetId | "none";
|
||||
|
||||
const SLOT_CHOICES: readonly GlanceSlotChoice[] = ["none", "system-card", "calendar", "terminal", "nightscout", "compass", "music"];
|
||||
const SLOT_CHOICES: readonly GlanceSlotChoice[] = ["none", "system-card", "calendar", "terminal", "t3code", "nightscout", "compass", "music"];
|
||||
|
||||
const SLOT_CHOICE_LABELS: Record<GlanceSlotChoice, string> = {
|
||||
none: "Empty",
|
||||
"system-card": "System card",
|
||||
calendar: "Calendar",
|
||||
terminal: "Terminal",
|
||||
t3code: "T3 Code",
|
||||
nightscout: "Nightscout",
|
||||
compass: "Compass",
|
||||
music: "Music",
|
||||
|
||||
@@ -27,7 +27,7 @@ export interface GlanceWidget {
|
||||
paint(image: GrayImage): void;
|
||||
}
|
||||
|
||||
export type GlanceWidgetId = "system-card" | "nightscout" | "compass" | "music" | "calendar" | "terminal";
|
||||
export type GlanceWidgetId = "system-card" | "nightscout" | "compass" | "music" | "calendar" | "terminal" | "t3code";
|
||||
|
||||
export type GlanceWidgetDefinition = {
|
||||
id: GlanceWidgetId;
|
||||
|
||||
@@ -4,6 +4,7 @@ import { CompassWidget } from "./compass-widget";
|
||||
import { MusicWidget } from "./music-widget";
|
||||
import { NightscoutWidget } from "./nightscout-widget";
|
||||
import { SystemCardWidget } from "./system-card";
|
||||
import { T3CodeWidget } from "./t3code-widget";
|
||||
import { TerminalWidget } from "./terminal-widget";
|
||||
|
||||
/** Every widget a board slot can hold. */
|
||||
@@ -11,6 +12,7 @@ export const GLANCE_WIDGETS: readonly GlanceWidgetDefinition[] = [
|
||||
{ id: "system-card", label: "System card", create: () => new SystemCardWidget() },
|
||||
{ id: "calendar", label: "Calendar", tall: true, create: () => new CalendarWidget() },
|
||||
{ id: "terminal", label: "Terminal", tall: true, create: () => new TerminalWidget() },
|
||||
{ id: "t3code", label: "T3 Code", tall: true, create: () => new T3CodeWidget() },
|
||||
{ id: "nightscout", label: "Nightscout", create: () => new NightscoutWidget() },
|
||||
{ id: "compass", label: "Compass", create: () => new CompassWidget() },
|
||||
{ id: "music", label: "Music", create: () => new MusicWidget() },
|
||||
|
||||
@@ -0,0 +1,122 @@
|
||||
import { type GrayImage, type UiFont } from "../../../graphics/image";
|
||||
import { truncateText } from "../../../graphics/textwrap";
|
||||
import { lineStep } from "../../../ui/metrics";
|
||||
import { onWorkerStateChanged, readWorkerState } from "../../../ui/shell/worker-state";
|
||||
import { loadEnvironments } from "../../t3code/t3-environments";
|
||||
import { glanceSummary, T3_GLANCE_STATE_KEY, type T3GlanceRow, type T3GlanceSnapshot } from "../../t3code/t3-glance";
|
||||
import { formatAge, threadMarker } from "../../t3code/t3-model";
|
||||
import { glanceFont } from "../glance-font";
|
||||
import { type GlanceWidget } from "../widget";
|
||||
|
||||
const PAD = 8;
|
||||
/** A row fits while its glyph line clears this margin above the bottom edge. */
|
||||
const BOTTOM_MARGIN = 2;
|
||||
const MINUTE_MS = 60_000;
|
||||
|
||||
/**
|
||||
* T3 Code's agent threads at a glance: how many need you and how many are
|
||||
* working, then the threads themselves in the app's order (needs-you, then
|
||||
* working, then recent) with the app's status marks. Approvals and questions
|
||||
* say so at the right edge; other rows show how long ago they were active.
|
||||
* Painted from the snapshot the T3 worker publishes; the app's boot hook
|
||||
* keeps that worker connected while this widget is on an enabled board.
|
||||
*/
|
||||
export class T3CodeWidget implements GlanceWidget {
|
||||
private snapshot = readWorkerState(T3_GLANCE_STATE_KEY) as T3GlanceSnapshot | undefined;
|
||||
private unsubscribe: (() => void) | null = null;
|
||||
private tick: ReturnType<typeof setInterval> | null = null;
|
||||
|
||||
start(requestRender: () => void): void {
|
||||
this.stop();
|
||||
this.snapshot = readWorkerState(T3_GLANCE_STATE_KEY) as T3GlanceSnapshot | undefined;
|
||||
this.unsubscribe = onWorkerStateChanged(T3_GLANCE_STATE_KEY, (snapshot) => {
|
||||
this.snapshot = snapshot as T3GlanceSnapshot | undefined;
|
||||
requestRender();
|
||||
});
|
||||
// Keeps the ages honest while the board stays up.
|
||||
this.tick = setInterval(requestRender, MINUTE_MS);
|
||||
}
|
||||
|
||||
stop(): void {
|
||||
this.unsubscribe?.();
|
||||
this.unsubscribe = null;
|
||||
if (this.tick !== null) clearInterval(this.tick);
|
||||
this.tick = null;
|
||||
}
|
||||
|
||||
paint(image: GrayImage): void {
|
||||
const font = glanceFont.small();
|
||||
const step = lineStep(font);
|
||||
const textWidth = image.width - 2 * PAD;
|
||||
const snapshot = this.snapshot;
|
||||
let y = PAD;
|
||||
|
||||
const title = "T3 Code";
|
||||
image.drawText(font, PAD, y, title, 150);
|
||||
const summary = snapshot ? glanceSummary(snapshot) : "";
|
||||
if (summary) {
|
||||
const text = truncateText(font, summary, textWidth - font.measureText(title) - 12);
|
||||
image.drawText(font, image.width - PAD - font.measureText(text), y, text, snapshot!.needsYou ? 230 : 140);
|
||||
}
|
||||
y += step;
|
||||
|
||||
const message = !snapshot
|
||||
? noWorkerMessage()
|
||||
: !snapshot.configured
|
||||
? "Pair a computer in the T3 Code app"
|
||||
: snapshot.status || (!snapshot.rows.length ? "No active threads" : "");
|
||||
if (message) {
|
||||
image.drawText(font, PAD, y, truncateText(font, message, textWidth), 120);
|
||||
return;
|
||||
}
|
||||
|
||||
const rows = snapshot!.rows;
|
||||
// Rows are placed a line step apart but only need their line height to fit.
|
||||
const capacity = Math.max(0, Math.floor((image.height - BOTTOM_MARGIN - y - font.lineHeight) / step) + 1);
|
||||
const hidden = rows.length + snapshot!.more - capacity;
|
||||
const shown = hidden > 0 ? rows.slice(0, Math.max(0, capacity - 1)) : rows;
|
||||
const nowMs = Date.now();
|
||||
const markerWidth = font.measureText("W") + 7;
|
||||
for (const row of shown) {
|
||||
paintRow(image, font, row, PAD, y, textWidth, markerWidth, nowMs);
|
||||
y += step;
|
||||
}
|
||||
if (hidden > 0) {
|
||||
image.drawText(font, PAD + markerWidth, y, `+${rows.length + snapshot!.more - shown.length} more`, 110);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Before the T3 worker publishes anything (or after it stopped for lack of anything to connect to). */
|
||||
function noWorkerMessage(): string {
|
||||
const environments = loadEnvironments();
|
||||
if (!environments.length) return "Pair a computer in the T3 Code app";
|
||||
if (!environments.some((environment) => environment.enabled)) return "Disconnected in the T3 Code app";
|
||||
return "Connecting…";
|
||||
}
|
||||
|
||||
function rowRightText(row: T3GlanceRow, nowMs: number): string {
|
||||
if (row.status === "approval") return row.requestKind === "file-change" ? "approve edit" : "approve";
|
||||
if (row.status === "input") return "question";
|
||||
return formatAge(row.activityMs, nowMs);
|
||||
}
|
||||
|
||||
function paintRow(
|
||||
image: GrayImage,
|
||||
font: UiFont,
|
||||
row: T3GlanceRow,
|
||||
x: number,
|
||||
y: number,
|
||||
width: number,
|
||||
markerWidth: number,
|
||||
nowMs: number,
|
||||
): void {
|
||||
const { marker, value: markerValue } = threadMarker(row.status, { unread: row.unread });
|
||||
if (marker) image.drawText(font, x, y, marker, markerValue);
|
||||
const needsYou = row.status === "approval" || row.status === "input";
|
||||
const right = rowRightText(row, nowMs);
|
||||
const rightWidth = right ? font.measureText(right) + 8 : 0;
|
||||
if (right) image.drawText(font, x + width - font.measureText(right), y, right, needsYou ? 230 : 110);
|
||||
const value = needsYou ? 255 : row.status === "working" || row.unread ? 215 : 165;
|
||||
image.drawText(font, x + markerWidth, y, truncateText(font, row.title, Math.max(10, width - markerWidth - rightWidth)), value);
|
||||
}
|
||||
Reference in new issue
Block a user