Merge branch 'main' of https://github.com/jimrandomh/faceclaw into ring-health-fixes

This commit is contained in:
James Babcock committed 2026-10-06 13:32:49 -07:00
commit e12cfaaf38
309 files changed
+15906 -3352

No files matched your search

+4 -3
View File
@@ -108,6 +108,7 @@ class AiChatLayer implements Layer {
return;
case "long-press-release": this.draft.release(); return;
case "double-click": this.draft.cancel(); shell.yieldFocusToSidebar(); return;
default: return;
}
}
menuItems(): MenuItem[] {
@@ -123,20 +124,20 @@ class AiChatLayer implements Layer {
{ label: "Switch session", disabled: () => external || busy(), onSelect: (ctx) => submenu(ctx, "Sessions",
[...this.conversations.list()].reverse().map((item) => ({
label: `${item.id === record.id ? "✓ " : ""}${this.conversations.title(item)}`,
onSelect: (ctx) => { if (this.conversations.select(item.id)) ctx.stack.clearToBase(); },
onSelect: (pickCtx) => { if (this.conversations.select(item.id)) pickCtx.stack.clearToBase(); },
}))) },
{ label: `Model: ${assistantModelLabel(record.model)}`, disabled: () => external || busy(), onSelect: (ctx) => submenu(ctx, "Model",
ASSISTANT_MODEL_CHOICES.map((model) => ({
label: `${model === record.model ? "✓ " : ""}${assistantModelLabel(model)}`,
disabled: () => !this.conversations.available(model, record.reasoning),
onSelect: (ctx) => { if (this.conversations.configure(model, record.reasoning)) ctx.stack.clearToBase(); },
onSelect: (pickCtx) => { if (this.conversations.configure(model, record.reasoning)) pickCtx.stack.clearToBase(); },
}))) },
{ label: `Reasoning: ${record.reasoning}`, disabled: () => {
const llm = resolveAssistantModel(record.model, { anthropic: anthropicApiKeySetting.get(), openai: openAiApiKeySetting.get() });
return external || busy() || !llm?.effort;
}, onSelect: (ctx) => submenu(ctx, "Reasoning", (["default", "low", "medium", "high"] as ReasoningLevel[]).map((reasoning) => ({
label: `${reasoning === record.reasoning ? "✓ " : ""}${reasoning === "default" ? "Model default" : reasoning}`,
onSelect: (ctx) => { if (this.conversations.configure(record.model, reasoning)) ctx.stack.clearToBase(); },
onSelect: (pickCtx) => { if (this.conversations.configure(record.model, reasoning)) pickCtx.stack.clearToBase(); },
}))) },
...(record.session?.isTurnActive() ? [{ label: "Cancel response", onSelect: (ctx: LayerContext) => {
record.session?.cancel(); ctx.stack.clearToBase();
+2
View File
@@ -4,6 +4,7 @@ import launcherApp from "./launcher";
import timerApp from "./timer";
import calculatorApp from "./calculator";
import terminalApp from "./terminal";
import t3codeApp from "./t3code";
import filesApp from "./files";
import musicApp from "./music";
import nightscoutApp from "./nightscout";
@@ -38,6 +39,7 @@ export const ALL_APPS: readonly AppDefinition[] = [
timerApp,
calculatorApp,
terminalApp,
t3codeApp,
filesApp,
musicApp,
nightscoutApp,
+59 -6
View File
@@ -46,6 +46,12 @@ const CELL = 12;
const BOARD_X = 60;
const BOARD_Y = 10;
const PANEL_X = 230;
/**
* Height the layout above is drawn for: a min-height window's viewport under
* the standard top bar. A taller viewport (the status bar folded into the
* switcher row, a full-panel display) centers it vertically.
*/
const LAYOUT_HEIGHT = 260;
/**
* Gravity starts here and speeds up with score (see dropIntervalMs). Slow
@@ -284,6 +290,11 @@ global.onmessage = (event: { data: WorkerAppMessage }) => {
updateTickTimer(window);
}
break;
case "navigation-sensors":
case "resize-window":
case "text-input":
case "tool-call":
break;
}
};
@@ -349,7 +360,7 @@ function handleInput(window: BlocksWindow, event: InputEvent, frameId: number):
// An open window menu owns all input (it closes itself via pop); menus are
// list UIs, so watch swipes take their standard fallback meanings there.
if (window.menu?.isOpen()) {
window.menu
void window.menu
.handleInput(directionalFallback(event))
.catch((error) => console.error(`blocks menu input failed: ${error}`))
.then(() => renderAndSubmit(window, frameId));
@@ -596,7 +607,7 @@ function paint(window: BlocksWindow): Plane[] {
}
function paintContent(window: BlocksWindow): GrayImage {
const image = new GrayImage(window.viewportWidth, window.viewportHeight, 0);
const image = new GrayImage(window.viewportWidth, LAYOUT_HEIGHT, 0);
image.drawRect(BOARD_X - 2, BOARD_Y - 2, COLS * CELL + 4, ROWS * CELL + 4, 120);
if (window.phase === "paused") {
// Hide the board so pausing can't be used to study the stack.
@@ -605,9 +616,17 @@ function paintContent(window: BlocksWindow): GrayImage {
drawCenteredIn(image, smallFont, BOARD_X, COLS * CELL, BOARD_Y + 150, `${GESTURE_DOUBLE_CLICK} leave`, 150);
} else {
paintBoard(image, window);
if (window.phase === "game-over") paintGameOver(image, window);
if (window.phase === "game-over") paintGameOver(image);
}
paintPanel(image, window);
return centeredInViewport(window, image);
}
/** Place the fixed-height layout vertically centered in the window's viewport. */
function centeredInViewport(window: BlocksWindow, layout: GrayImage): GrayImage {
if (layout.height === window.viewportHeight) return layout;
const image = new GrayImage(window.viewportWidth, window.viewportHeight, 0);
layout.composeInto(image, 0, Math.max(0, Math.floor((window.viewportHeight - layout.height) / 2)));
return image;
}
@@ -634,10 +653,44 @@ function paintBoard(image: GrayImage, window: BlocksWindow): void {
}
function drawCell(image: GrayImage, x: number, y: number, shade: number): void {
image.fillRect(BOARD_X + x * CELL, BOARD_Y + y * CELL, CELL - 1, CELL - 1, shade);
drawTile(image, BOARD_X + x * CELL, BOARD_Y + y * CELL, shade);
}
function paintGameOver(image: GrayImage, window: BlocksWindow): void {
/**
* One beveled tile (CELL - 1 square, leaving a 1px gap): lit top and left
* edges, shadowed bottom and right, stepping in over two rings to a raised
* face. The face keeps the piece's shade, so pieces stay distinguishable by
* brightness after 4bpp quantization.
*/
function drawTile(image: GrayImage, x: number, y: number, shade: number): void {
const size = CELL - 1;
const lit = (amount: number): number => Math.round(shade + (255 - shade) * amount);
const dim = (factor: number): number => Math.round(shade * factor);
image.fillRect(x, y, size, size, dim(0.8));
drawBevelRing(image, x, y, size, lit(0.6), dim(0.4), shade);
drawBevelRing(image, x + 1, y + 1, size - 2, lit(0.3), dim(0.6), shade);
image.fillRect(x + 3, y + 3, size - 6, size - 6, shade);
}
/** A square's 1px edge: light along the top and left, dark along the bottom and right, mid at the two mixed corners. */
function drawBevelRing(
image: GrayImage,
x: number,
y: number,
size: number,
light: number,
dark: number,
mid: number,
): void {
image.fillRect(x, y, size, 1, light);
image.fillRect(x, y, 1, size, light);
image.fillRect(x, y + size - 1, size, 1, dark);
image.fillRect(x + size - 1, y, 1, size, dark);
image.setPixel(x + size - 1, y, mid);
image.setPixel(x, y + size - 1, mid);
}
function paintGameOver(image: GrayImage): void {
const boxY = BOARD_Y + 70;
image.fillRect(BOARD_X, boxY, COLS * CELL, 100, 0);
image.drawRect(BOARD_X + 4, boxY + 4, COLS * CELL - 8, 92, 150);
@@ -660,7 +713,7 @@ function paintPanel(image: GrayImage, window: BlocksWindow): void {
const offsetX = PANEL_X + Math.round((previewBoxSize - (maxX - minX + 1) * CELL) / 2);
const offsetY = previewY + Math.round((previewBoxSize - (maxY - minY + 1) * CELL) / 2);
for (const [cx, cy] of cells) {
image.fillRect(offsetX + (cx - minX) * CELL, offsetY + (cy - minY) * CELL, CELL - 1, CELL - 1, def.shade);
drawTile(image, offsetX + (cx - minX) * CELL, offsetY + (cy - minY) * CELL, def.shade);
}
const statX = PANEL_X + previewBoxSize + 40;
+1 -2
View File
@@ -96,8 +96,7 @@ export function createCalculatorAppWindow(options: InProcessAppOptions): InProce
},
];
let app: InProcessWindow;
app = createInProcessWindow({
const app = createInProcessWindow({
appId: "calculator",
windowId: CALCULATOR_WINDOW_ID,
title: "Calculator",
+1 -1
View File
@@ -24,7 +24,7 @@ import {
findDigits,
highlightedMatch,
} from "./digit-search";
import { type MathWorkload, longRunOffer as makeLongRunOffer, type LongRunOffer } from "./workload";
import { type MathWorkload } from "./workload";
import {
binomial,
factorizationText,
+1 -1
View File
@@ -322,7 +322,7 @@ export function digitalRoot(value: number): number {
export function isPalindrome(value: number): boolean {
const text = String(Math.abs(value));
return text === [...text].reverse().join("");
return text === text.split("").reverse().join("");
}
export function isPerfect(value: number): boolean {
+2 -2
View File
@@ -123,7 +123,7 @@ export class Polynomial {
*/
dividingByRoot(root: MathNumber): Polynomial {
if (this.degree < 1) return this;
const quotient: MathNumber[] = new Array(this.degree).fill(integer(0));
const quotient: MathNumber[] = Array.from({ length: this.degree }, () => integer(0));
let carry: MathNumber = integer(0);
for (let power = this.degree; power >= 1; power--) {
const coefficient = add(this.at(power), carry);
@@ -235,7 +235,7 @@ function addAt(value: MathNumber, power: number, coefficients: MathNumber[]): vo
function convolve(lhs: MathNumber[], rhs: MathNumber[]): MathNumber[] {
if (lhs.length === 0 || rhs.length === 0) return [integer(0)];
const result: MathNumber[] = new Array(lhs.length + rhs.length - 1).fill(integer(0));
const result: MathNumber[] = Array.from({ length: lhs.length + rhs.length - 1 }, () => integer(0));
for (let i = 0; i < lhs.length; i++) {
if (isZero(lhs[i]!)) continue;
for (let j = 0; j < rhs.length; j++) {
-1
View File
@@ -19,7 +19,6 @@ import {
multiplyExpr,
negate,
num,
numberValue,
relation as relationExpr,
spokenRelation,
subtractExpr,
@@ -29,12 +29,12 @@ import {
numberEquals,
rational,
reciprocal,
isZero,
} from "./math-number";
import { simplify } from "./simplifier";
import { differentiate, integrateNumeric } from "./derivative";
import { evaluateValue } from "./evaluator";
import { Polynomial } from "./polynomial";
import { isZero } from "./math-number";
/** Indefinite integral with respect to `variable`, without the constant. */
export function integrate(expression: MathExpression, variable: string): MathExpression | null {
+2 -7
View File
@@ -103,8 +103,7 @@ function render(expression: MathExpression, parentPrecedence: number): string {
return wrap(`1/${inner}`, parentPrecedence > PRECEDENCE.product);
}
const body =
`${render(expression.base, PRECEDENCE.power + 1)}` +
`^${render(expression.exponent, PRECEDENCE.power + 1)}`;
`${render(expression.base, PRECEDENCE.power + 1)}^${render(expression.exponent, PRECEDENCE.power + 1)}`;
return wrap(body, parentPrecedence > PRECEDENCE.power);
}
@@ -116,11 +115,7 @@ function render(expression: MathExpression, parentPrecedence: number): string {
}
case "relation":
return (
`${render(expression.lhs, PRECEDENCE.relation)} ` +
`${expression.relation} ` +
`${render(expression.rhs, PRECEDENCE.relation)}`
);
return `${render(expression.lhs, PRECEDENCE.relation)} ${expression.relation} ${render(expression.rhs, PRECEDENCE.relation)}`;
}
}
+10 -10
View File
@@ -26,14 +26,14 @@ export type SolutionFamily = {
baseExpression: MathExpression | null;
};
export function familyValues(family: SolutionFamily, lower: number, upper: number): number[] {
if (family.period <= 0) {
return family.base >= lower && family.base <= upper ? [family.base] : [];
export function familyValues(solutionFamily: SolutionFamily, lower: number, upper: number): number[] {
if (solutionFamily.period <= 0) {
return solutionFamily.base >= lower && solutionFamily.base <= upper ? [solutionFamily.base] : [];
}
const results: number[] = [];
let n = Math.floor((lower - family.base) / family.period);
let n = Math.floor((lower - solutionFamily.base) / solutionFamily.period);
for (;;) {
const value = family.base + n * family.period;
const value = solutionFamily.base + n * solutionFamily.period;
if (value > upper) break;
if (value >= lower) results.push(value);
n += 1;
@@ -42,14 +42,14 @@ export function familyValues(family: SolutionFamily, lower: number, upper: numbe
return results;
}
export function familyText(family: SolutionFamily): string {
const baseText = family.baseExpression
? plain(family.baseExpression)
: describeNumber(fromDecimal(family.base));
export function familyText(solutionFamily: SolutionFamily): string {
const baseText = solutionFamily.baseExpression
? plain(solutionFamily.baseExpression)
: describeNumber(fromDecimal(solutionFamily.base));
// The separator is load-bearing: "2*pi" run straight into the index reads
// as "2*pin", a different and nonsensical expression to anyone scanning it
// on the lens.
const periodText = describePi(family.period);
const periodText = describePi(solutionFamily.period);
return `${baseText} + ${periodText}·n`;
}
+1 -2
View File
@@ -21,7 +21,6 @@ export const CALENDAR_SURFACE_ID = "window:calendar";
*/
export function createCalendarAppWindow(options: InProcessAppOptions): InProcessWindow {
let requesting = false;
let app: InProcessWindow;
let unsubscribe = () => {};
let tick: ReturnType<typeof setInterval> | null = null;
@@ -37,7 +36,7 @@ export function createCalendarAppWindow(options: InProcessAppOptions): InProcess
.finally(() => { requesting = false; });
};
app = createInProcessWindow({
const app = createInProcessWindow({
appId: "calendar",
windowId: CALENDAR_WINDOW_ID,
title: "Calendar",
+1 -1
View File
@@ -80,7 +80,7 @@ export class CompassCalibrationLayer implements Layer {
// The crosshair marks where the wearer is looking, so its horizontal
// position must be the true centre of the display, not of the app
// viewport the sidebar has pushed to the right. Vertically it just sits in
// viewport a side strip has pushed off centre. Vertically it just sits in
// the free space between the instructions and the readouts; the window
// band's own placement on screen is the wearer's Display setting and
// doesn't affect the heading being aimed.
+6 -4
View File
@@ -38,6 +38,9 @@ const TOP_PAD = 2;
const BOTTOM_PAD = 4;
/** Clearance between the rose's widest point and the viewport edge. */
const EDGE_PAD = 8;
/** Inset of the top-right magnetometer calibration readout from the window edges. */
const ACCURACY_TOP_PAD = TOP_PAD + 4;
const ACCURACY_RIGHT_PAD = EDGE_PAD + 4;
/** Cap on the rose's radius, so a tall window doesn't get a comical one. */
const MAX_ROSE_RADIUS = 98;
@@ -161,7 +164,7 @@ class CompassLayer implements Layer {
const readoutHeight = headingFont.lineHeight;
// One column centred on the display's true centre, so the rose sits where
// the wearer is looking rather than 32px right of it.
// the wearer is looking rather than 32px beside it (with a side strip).
const cx = screenCenterInViewportX();
// The rose is sized and placed first — it hangs off the bottom edge — and
@@ -197,7 +200,7 @@ class CompassLayer implements Layer {
y += smallStep;
}
const accuracyText = magneticAccuracyText(this.diagnostics);
image.drawText(small, width - EDGE_PAD - small.measureText(accuracyText), TOP_PAD, accuracyText, 175);
image.drawText(small, width - ACCURACY_RIGHT_PAD - small.measureText(accuracyText), ACCURACY_TOP_PAD, accuracyText, 175);
return image;
}
@@ -211,10 +214,9 @@ class CompassLayer implements Layer {
}
export function createCompassAppWindow(options: InProcessAppOptions): InProcessWindow {
let app: InProcessWindow;
let requestRender = () => {};
const layer = new CompassLayer(() => requestRender());
app = createInProcessWindow({
const app = createInProcessWindow({
appId: "compass",
windowId: COMPASS_WINDOW_ID,
title: "Compass",
+1 -1
View File
@@ -2,7 +2,7 @@ import { getDefaultMediumFont, getDefaultSmallFont } from "../../graphics/ui-fon
import { GrayImage } from "../../graphics/image";
import { clamp } from "../../util/numeric-util";
import { addImuListener, imuSourceLabel, setImuReportEnabled, type ImuReading } from "../../native/imu";
import { GESTURE_DOUBLE_CLICK, type InputEvent } from "../../ui/gestures";
import { type InputEvent } from "../../ui/gestures";
import { Layer, type LayerContext } from "../../ui/layers";
import { shell } from "../../ui/shell/shell";
+1 -1
View File
@@ -1,7 +1,7 @@
import { getDefaultSmallFont } from "../../graphics/ui-fonts";
import { GrayImage } from "../../graphics/image";
import { Layer, type LayerActions, type LayerContext } from "../../ui/layers";
import { GESTURE_CLICK, GESTURE_DOUBLE_CLICK, GESTURE_SCROLL, type InputEvent } from "../../ui/gestures";
import { GESTURE_CLICK, type InputEvent } from "../../ui/gestures";
import { Menu, type MenuDrawArgs } from "../../ui/menu-core";
import { centeredTextY, tightRowHeight } from "../../ui/metrics";
import {
+17 -18
View File
@@ -1,4 +1,4 @@
import { MenuLayer, drawSubmenuIndicator, type MenuItem } from "../../ui/menu";
import { MenuLayer, submenuItem, type MenuItem } from "../../ui/menu";
import { ScreenTestLayer } from "./screen-test";
import { InputEventsLayer } from "./input-events";
import { shell } from "../../ui/shell/shell";
@@ -6,14 +6,13 @@ import { BuzzerDemoLayer } from "./buzzer-demo";
import { AccelerometerDemoLayer } from "./accelerometer-demo";
import { BandwidthBenchmarkLayer } from "./bandwidth-benchmark";
import { LightSensorDemoLayer } from "./light-sensor-demo";
import { MicrophonesMonitorLayer } from "./microphones-monitor";
import { ResourceUsageLayer } from "./resource-usage";
import { RingStatusLayer } from "./ring-status";
import { LoadAppFromQrLayer, LoadAppFromUrlLayer } from "./load-app";
import { unicodeTestMenu } from "./unicode-test";
import { type AppContext } from "../app-definition";
import { getDefaultSmallFont } from "../../graphics/ui-fonts";
import { appViewportSize } from "../../ui/shell/geometry";
import { LIST_ROW_TEXT_INSET } from "../../ui/metrics";
import {
createInProcessWindow,
YieldAtRootLayer,
@@ -36,22 +35,10 @@ const MENU_LAYOUT = {
opaque: true,
};
/** A row that opens a nested page: the label plus a right-edge ">". */
function submenuItem(label: string, onSelect: MenuItem["onSelect"]): MenuItem {
return {
label,
onSelect,
render: ({ image, x, y, width, height, selected, disabled, text }) => {
const font = getDefaultSmallFont();
const value = disabled ? 70 : selected ? 255 : 200;
image.drawText(font, x, y + LIST_ROW_TEXT_INSET, text, value);
drawSubmenuIndicator(image, font, x, y, width, height, value);
},
};
}
type OpenPage = (ctx: Parameters<MenuItem["onSelect"]>[0]) => void;
/** The diagnostic demos, one level down from the root menu. */
function debugTestsMenu(openInputEvents: (ctx: Parameters<MenuItem["onSelect"]>[0]) => void): MenuLayer {
function debugTestsMenu(openInputEvents: OpenPage, openMicrophones: OpenPage): MenuLayer {
return new MenuLayer(
"Debug tests",
[
@@ -94,6 +81,12 @@ function debugTestsMenu(openInputEvents: (ctx: Parameters<MenuItem["onSelect"]>[
ctx.stack.push(new RingStatusLayer(DEVELOPER_WINDOW_ID, ctx.actions.requestRender));
},
},
{
label: "Microphones",
disabled: global.isIOS,
description: global.isIOS ? "Not available on iOS yet." : undefined,
onSelect: openMicrophones,
},
{
label: "BLE bandwidth",
onSelect: (ctx) => {
@@ -116,6 +109,7 @@ function debugTestsMenu(openInputEvents: (ctx: Parameters<MenuItem["onSelect"]>[
*/
export function createDeveloperAppWindow(appContext: AppContext, options: InProcessAppOptions): InProcessWindow {
let inputEvents: InputEventsLayer | null = null;
let microphones: MicrophonesMonitorLayer | null = null;
const menu = new MenuLayer(
"Developer",
[
@@ -150,6 +144,11 @@ export function createDeveloperAppWindow(appContext: AppContext, options: InProc
() => { if (inputEvents === page) inputEvents = null; });
inputEvents = page;
inputCtx.stack.push(page);
}, (micCtx) => {
const page = new MicrophonesMonitorLayer(DEVELOPER_WINDOW_ID, micCtx.actions.requestRender,
() => { if (microphones === page) microphones = null; });
microphones = page;
micCtx.stack.push(page);
}));
}),
],
@@ -164,7 +163,7 @@ export function createDeveloperAppWindow(appContext: AppContext, options: InProc
icon: "wrench",
closeable: true,
actions: options.actions,
menuItems: () => inputEvents?.menuItems() ?? [],
menuItems: () => inputEvents?.menuItems() ?? microphones?.menuItems() ?? [],
// Dictating a URL is the one thing worth speaking at in this app; the
// load pages take the text and every other page ignores it.
receiveTextInput: (text) => {
+426
View File
@@ -0,0 +1,426 @@
import { getDefaultSmallFont } from "../../graphics/ui-fonts";
import { GrayImage } from "../../graphics/image";
import { clamp } from "../../util/numeric-util";
import {
addAudioMonitorListeners,
audioMonitorCommunicator,
isFaceclawAudioCaptureActive,
type AudioArm,
} from "../../native/audio-monitor";
import { micArrayController } from "../microphones/mic-control";
import {
MIC_RET_NONE,
STOCK_PACKET_BYTES,
decodeMicStatus,
decodeMicStreamFrame,
splitInterleavedPcm16,
type MicStatus,
type MicStreamFrame,
} from "../microphones/mic-protocol";
import { GESTURE_DOUBLE_CLICK, type InputEvent } from "../../ui/gestures";
import { type MenuItem } from "../../ui/menu";
import { Layer, type LayerContext } from "../../ui/layers";
import { shell } from "../../ui/shell/shell";
// Packet rate / byte rate / largest gap are measured over this trailing window.
const RATE_WINDOW_MS = 2_000;
// Activity strip: packet counts per bucket over the last HISTORY_MS.
const HISTORY_MS = 20_000;
const BUCKET_MS = 500;
const BUCKETS = HISTORY_MS / BUCKET_MS;
// Repaint cadence; also how often a replaced communicator is re-subscribed.
const TICK_MS = 250;
// Stock packet trailer (see Lc3PacketFramer.kt): SSR, angle, counter.
const STOCK_SSR_OFFSET = 200;
const STOCK_ANGLE_OFFSET = 202;
const STOCK_COUNTER_OFFSET = 204;
// Counter gaps at or beyond this are late/duplicate copies, not losses.
const STOCK_LATE_GAP = 128;
const LEVEL_FLOOR_DB = -90;
// wallMs (JS receipt time) windows the rates; arrivalMs (the session core's
// elapsed-realtime stamp, taken on the BLE thread) measures gaps without the
// main-thread hop's jitter.
type Arrival = { wallMs: number; arrivalMs: number; bytes: number };
type ArmStats = {
packets: number;
stockPackets: number;
smPackets: number;
otherPackets: number;
lastOtherBytes: number;
lastArrivalWallMs: number | null;
recent: Arrival[];
buckets: number[];
stockCounter: number | null;
stockLost: number;
stockDuplicate: number;
stockAngle: number;
stockSsr: number;
frame: MicStreamFrame | null;
smSeq: number | null;
smSeqGaps: number;
channelDb: number[];
status: MicStatus | null;
statusAtMs: number | null;
};
function emptyStats(): ArmStats {
return {
packets: 0,
stockPackets: 0,
smPackets: 0,
otherPackets: 0,
lastOtherBytes: 0,
lastArrivalWallMs: null,
recent: [],
buckets: Array.from({ length: BUCKETS }, () => 0),
stockCounter: null,
stockLost: 0,
stockDuplicate: 0,
stockAngle: 0,
stockSsr: 0,
frame: null,
smSeq: null,
smSeqGaps: 0,
channelDb: [],
status: null,
statusAtMs: null,
};
}
/**
* Passive microphone monitor: shows what the glasses are sending on the
* audio characteristic, per temple, without enabling anything. Open it, then
* activate the mics from another app (Microphones, Transcribe, voice input)
* and come back: collection continues while this page is hidden, so the
* counters and the 20 s activity strip show whether that activation produced
* a stream, what kind (stock mono LC3 or CFW 'SM' frames), and how healthy it
* is. CFW mic_control status records are shown when they arrive: after a
* CONFIGURE, with the periodic battery read, or from this window's "Query mic
* status" menu item (a status request only; it touches no mic hardware).
* Only the right temple can send status; each lens's debug overlay shows its
* own counters.
*/
export class MicrophonesMonitorLayer implements Layer {
private readonly stats: Record<"L" | "R", ArmStats> = { L: emptyStats(), R: emptyStats() };
private unknownArmPackets = 0;
private bucketStartMs = Date.now();
private communicator: any = null;
private unsubscribe: (() => void) | null = null;
private timer: ReturnType<typeof setInterval> | null = null;
private removed = false;
constructor(
private readonly windowId: string,
private readonly requestRender: () => void,
private readonly onClosed: () => void = () => {},
) {
this.tick();
this.timer = setInterval(() => this.tick(), TICK_MS);
}
onRemoved(): void {
this.removed = true;
if (this.timer !== null) {
clearInterval(this.timer);
this.timer = null;
}
this.unsubscribe?.();
this.unsubscribe = null;
this.communicator = null;
this.onClosed();
}
private tick(): void {
if (this.removed) return;
const communicator = audioMonitorCommunicator();
if (communicator !== this.communicator) {
this.unsubscribe?.();
this.communicator = communicator;
this.unsubscribe = addAudioMonitorListeners(
communicator,
(data, arm, arrivalMs) => this.onPacket(data, arm, arrivalMs),
(body, arm) => this.onStatus(body, arm),
);
}
this.advanceBuckets(Date.now());
if (shell.isWindowVisible(this.windowId)) this.requestRender();
}
private advanceBuckets(nowMs: number): void {
while (nowMs - this.bucketStartMs >= BUCKET_MS) {
this.bucketStartMs += BUCKET_MS;
for (const stats of Object.values(this.stats)) {
stats.buckets.shift();
stats.buckets.push(0);
}
}
}
private onPacket(data: Uint8Array, arm: AudioArm, arrivalMs: number): void {
if (this.removed) return;
if (arm === "?") {
this.unknownArmPackets++;
return;
}
const stats = this.stats[arm];
const nowMs = Date.now();
this.advanceBuckets(nowMs);
stats.packets++;
stats.buckets[BUCKETS - 1]!++;
stats.recent.push({ wallMs: nowMs, arrivalMs, bytes: data.length });
stats.lastArrivalWallMs = nowMs;
const frame = decodeMicStreamFrame(data);
if (frame) {
this.onStreamFrame(stats, frame);
} else if (data.length === STOCK_PACKET_BYTES) {
this.onStockPacket(stats, data);
} else {
stats.otherPackets++;
stats.lastOtherBytes = data.length;
}
}
private onStockPacket(stats: ArmStats, data: Uint8Array): void {
stats.stockPackets++;
const counter = data[STOCK_COUNTER_OFFSET]!;
if (stats.stockCounter !== null) {
const gap = (counter - stats.stockCounter) & 0xff;
if (gap === 0 || gap >= STOCK_LATE_GAP) {
stats.stockDuplicate++;
return;
}
stats.stockLost += gap - 1;
}
stats.stockCounter = counter;
stats.stockSsr = readS16(data, STOCK_SSR_OFFSET);
stats.stockAngle = readS16(data, STOCK_ANGLE_OFFSET);
}
private onStreamFrame(stats: ArmStats, frame: MicStreamFrame): void {
stats.smPackets++;
if (stats.smSeq !== null && frame.sequence !== ((stats.smSeq + 1) & 0xffff)) stats.smSeqGaps++;
stats.smSeq = frame.sequence;
stats.frame = frame;
const channels = splitInterleavedPcm16(frame);
stats.channelDb = channels ? channels.map(rmsDb) : [];
}
private onStatus(body: Uint8Array, arm: AudioArm): void {
if (this.removed || arm === "?") return;
const status = decodeMicStatus(body);
if (!status) return;
this.stats[arm].status = status;
this.stats[arm].statusAtMs = Date.now();
}
paint(ctx: LayerContext): GrayImage {
const font = getDefaultSmallFont();
const line = font.lineHeight;
const { width, height } = ctx.stack.getBaseSize();
const image = new GrayImage(width, height, 0);
const nowMs = Date.now();
image.drawText(font, 16, 5, "Microphones (passive)", 230);
const capture = isFaceclawAudioCaptureActive() ? "Faceclaw capture: on" : "Faceclaw capture: off";
image.drawText(font, width - 16 - font.measureText(capture), 5, capture, 170);
if (!this.communicator) {
image.drawText(font, 16, 5 + line * 2, "Glasses not connected.", 190);
image.drawText(font, 16, height - 16, `${GESTURE_DOUBLE_CLICK} back`, 110);
return image;
}
const gutter = 16;
const columnWidth = Math.floor((width - gutter * 3) / 2);
const stripHeight = 22;
const top = 5 + line + 6;
const bottom = height - 16 - stripHeight - 8;
this.paintArm(image, "L", "Left", gutter, top, columnWidth, bottom, nowMs);
this.paintArm(image, "R", "Right", gutter * 2 + columnWidth, top, columnWidth, bottom, nowMs);
const stripY = height - 16 - stripHeight - 4;
const stripMax = Math.max(1, ...this.stats.L.buckets, ...this.stats.R.buckets);
drawStrip(image, this.stats.L.buckets, gutter, stripY, columnWidth, stripHeight, stripMax);
drawStrip(image, this.stats.R.buckets, gutter * 2 + columnWidth, stripY, columnWidth, stripHeight, stripMax);
const footer = this.unknownArmPackets > 0
? `last ${HISTORY_MS / 1000} s unattributed packets ${this.unknownArmPackets}`
: `packets per ${BUCKET_MS} ms, last ${HISTORY_MS / 1000} s`;
image.drawText(font, gutter, height - 16, footer, 110);
const back = `${GESTURE_DOUBLE_CLICK} back tap reset`;
image.drawText(font, width - gutter - font.measureText(back), height - 16, back, 110);
return image;
}
private paintArm(
image: GrayImage,
arm: "L" | "R",
label: string,
x: number,
top: number,
width: number,
bottom: number,
nowMs: number,
): void {
const font = getDefaultSmallFont();
const line = font.lineHeight;
const stats = this.stats[arm];
const cutoff = nowMs - RATE_WINDOW_MS;
while (stats.recent.length > 0 && stats.recent[0]!.wallMs < cutoff) stats.recent.shift();
const rate = (stats.recent.length * 1000) / RATE_WINDOW_MS;
const byteRate = stats.recent.reduce((sum, arrival) => sum + arrival.bytes, 0) / RATE_WINDOW_MS;
let maxGap = 0;
for (let i = 1; i < stats.recent.length; i++) {
maxGap = Math.max(maxGap, stats.recent[i]!.arrivalMs - stats.recent[i - 1]!.arrivalMs);
}
const rows: Array<[string, number]> = [];
const streaming = stats.recent.length > 0;
rows.push([`${label}: ${streaming ? `${rate.toFixed(1)} pkt/s ${byteRate.toFixed(1)} kB/s` : "silent"}`, streaming ? 235 : 150]);
const age = stats.lastArrivalWallMs === null ? "never" : `${formatAge(nowMs - stats.lastArrivalWallMs)} ago`;
rows.push([`last ${age}${streaming && stats.recent.length > 1 ? ` max gap ${maxGap} ms` : ""}`, 170]);
rows.push([`total ${stats.packets}: stock ${stats.stockPackets} SM ${stats.smPackets} other ${stats.otherPackets}${stats.otherPackets ? ` (${stats.lastOtherBytes} B)` : ""}`, 170]);
if (stats.stockPackets > 0) {
rows.push([`stock LC3: lost ${stats.stockLost} dup ${stats.stockDuplicate}`, 190]);
rows.push([` angle ${stats.stockAngle}° ssr ${stats.stockSsr}`, 170]);
}
const frame = stats.frame;
if (frame) {
const flags = [frame.truncated ? "trunc" : "", stats.smSeqGaps ? `seq gaps ${stats.smSeqGaps}` : ""].filter(Boolean).join(" ");
rows.push([`SM: ${frame.channelCount}ch ${frame.sampleRateHz / 1000} kHz ${frame.codec} ${frame.payload.length} B ${flags}`, 190]);
rows.push([` angle ${frame.angleDegrees}° ssr ${frame.ssr}`, 170]);
}
const status = stats.status;
if (status) {
const armed = status.hardwareArmed ? "armed" : status.active ? "configured" : "inactive";
rows.push([`CFW ${armed}: ${status.source} mask ${status.channelMask} frames ${status.framesEmitted}`, 190]);
rows.push([` ${status.codec} ${status.effectiveRateHz / 1000} kHz ${formatAge(nowMs - (stats.statusAtMs ?? nowMs))} ago`, 150]);
const diag = status.diagnostics;
if (diag) {
rows.push([` tap ${diag.tapCalls} calls last ${diag.tapLastBytes} B @slot ${diag.tapLastSlot}`, 190]);
rows.push([` skip idle ${diag.skipIdle} lease ${diag.skipLease} alloc ${diag.skipAlloc}`, 170]);
rows.push([
` reg ${formatRet(diag.registerResult)} unreg ${formatRet(diag.unregisterResult)} ntf ${formatRet(diag.notifyResult)} slots ${diag.slots.map(formatSlot).join("")} lease ${diag.leaseRemainingS} s`,
170,
]);
} else {
rows.push([" (no tap diagnostics: older firmware)", 120]);
}
} else {
rows.push(["CFW status: none seen", 120]);
}
let y = top;
for (const [text, shade] of rows) {
if (y + line > bottom) return;
image.drawText(font, x, y, fitText(font, text, width), shade);
y += line;
}
// Per-channel RMS level bars for raw PCM frames.
for (let channel = 0; channel < stats.channelDb.length; channel++) {
if (y + line > bottom) return;
const db = stats.channelDb[channel]!;
const tag = `ch${channel} ${db <= LEVEL_FLOOR_DB ? "-inf" : db.toFixed(0)} dB`;
image.drawText(font, x, y, tag, 190);
const barX = x + font.measureText("ch0 -100 dB") + 6;
const barWidth = Math.max(0, width - (barX - x));
const fill = Math.round(clamp((db - LEVEL_FLOOR_DB) / -LEVEL_FLOOR_DB, 0, 1) * barWidth);
image.drawRect(barX, y + 3, barWidth, line - 6, 60);
if (fill > 0) image.fillRect(barX, y + 3, fill, line - 6, db > -3 ? 255 : 170);
y += line;
}
}
/** Window context-menu entries while this page is open. */
menuItems(): MenuItem[] {
return [
{
label: "Query mic status",
onSelect: (ctx) => {
micArrayController.query();
ctx.stack.pop();
},
},
{
label: "Reset counters",
onSelect: (ctx) => {
this.resetCounters();
ctx.stack.pop();
},
},
];
}
private resetCounters(): void {
this.stats.L = emptyStats();
this.stats.R = emptyStats();
this.unknownArmPackets = 0;
this.requestRender();
}
async handleInput(event: InputEvent, ctx: LayerContext): Promise<void> {
if (event.type === "double-click") {
ctx.stack.pop();
} else if (event.type === "click") {
this.resetCounters();
}
}
}
function formatRet(value: number): string {
return value === MIC_RET_NONE ? "-" : String(value);
}
/** Same letters as the firmware overlay: - empty, S another callback, T our tap. */
function formatSlot(state: "empty" | "other" | "tap"): string {
return state === "tap" ? "T" : state === "other" ? "S" : "-";
}
function readS16(data: Uint8Array, offset: number): number {
const value = data[offset]! | (data[offset + 1]! << 8);
return value >= 0x8000 ? value - 0x10000 : value;
}
function rmsDb(samples: Float32Array): number {
let sumSquares = 0;
for (let i = 0; i < samples.length; i++) sumSquares += samples[i]! * samples[i]!;
const rms = Math.sqrt(sumSquares / Math.max(1, samples.length));
return rms > 0 ? Math.max(LEVEL_FLOOR_DB, 20 * Math.log10(rms)) : LEVEL_FLOOR_DB;
}
function formatAge(ms: number): string {
if (ms < 1_000) return `${Math.max(0, Math.round(ms))} ms`;
if (ms < 60_000) return `${(ms / 1000).toFixed(1)} s`;
return `${Math.round(ms / 60_000)} min`;
}
function fitText(font: ReturnType<typeof getDefaultSmallFont>, text: string, width: number): string {
if (font.measureText(text) <= width) return text;
let end = text.length;
while (end > 0 && font.measureText(`${text.slice(0, end)}…`) > width) end--;
return `${text.slice(0, end)}…`;
}
function drawStrip(
image: GrayImage,
buckets: readonly number[],
x: number,
y: number,
width: number,
height: number,
max: number,
): void {
image.drawRect(x, y, width, height, 45);
const barWidth = width / buckets.length;
for (let i = 0; i < buckets.length; i++) {
const count = buckets[i]!;
if (count <= 0) continue;
const barHeight = Math.max(1, Math.round((count / max) * (height - 2)));
const left = x + Math.floor(i * barWidth);
const right = x + Math.floor((i + 1) * barWidth) - 1;
image.fillRect(left + 1, y + height - 1 - barHeight, Math.max(1, right - left), barHeight, 200);
}
}
+2
View File
@@ -64,6 +64,8 @@ export class ScreenTestLayer implements Layer {
case "double-click":
ctx.stack.pop();
return;
default:
return;
}
}
}
+2
View File
@@ -236,6 +236,8 @@ export class UnicodeSampleLayer implements Layer {
case "double-click":
ctx.stack.pop();
return;
default:
return;
}
}
+2 -2
View File
@@ -1,4 +1,4 @@
import { Application } from "@nativescript/core";
import { Utils } from "@nativescript/core";
export function hmacSha256Base64(secret: string, message: string): string {
const charset = java.nio.charset.StandardCharsets.UTF_8;
@@ -10,7 +10,7 @@ export function hmacSha256Base64(secret: string, message: string): string {
}
export function getPhoneOpenUdid(): string {
const resolver = Application.android.context?.getContentResolver();
const resolver = Utils.android.getApplicationContext()?.getContentResolver();
const id = resolver ? String(android.provider.Settings.Secure.getString(resolver, android.provider.Settings.Secure.ANDROID_ID) ?? "") : "";
if (!id) throw new Error("Android did not provide a device ID for EvenHub authentication.");
return id;
+10
View File
@@ -51,9 +51,19 @@ export function installedEvenHubPackageId(appId: string): string | null {
}
}
// The parsed index for the last stored value: the launcher and the watch
// state both list installed apps on every refresh, and the sort's
// localeCompare dominated the cost.
let parsedIndex: { raw: string; apps: InstalledEvenHubApp[] } | null = null;
export function getInstalledEvenHubApps(): InstalledEvenHubApp[] {
const raw = getStringSetting(STORAGE_KEY, "");
if (!raw) return [];
if (parsedIndex?.raw !== raw) parsedIndex = { raw, apps: parseInstalledIndex(raw) };
return parsedIndex.apps.map((app) => ({ ...app }));
}
function parseInstalledIndex(raw: string): InstalledEvenHubApp[] {
try {
const parsed = JSON.parse(raw);
if (!Array.isArray(parsed)) return [];
+49 -6
View File
@@ -6,11 +6,12 @@
*
* Glasses-first: launching does NOT take over the phone screen. The phone
* stays on the Faceclaw dashboard; an app's phone UI is shown on demand
* (window menu -> "Show phone UI"), overlaying the dashboard, and the Back
* button returns to it. Apps are kept alive until explicitly closed (no memory
* eviction yet).
* (window menu -> "Show phone UI", or the app-icon button in the phone's
* action bar while the app is foreground on the glasses), overlaying the
* dashboard, and the Back button or the overlay's close button returns to it.
* Apps are kept alive until explicitly closed (no memory eviction yet).
*/
import { Application } from "@nativescript/core";
import { Application, type ImageSource } from "@nativescript/core";
import { type AppContext } from "../app-definition";
import {
appFilesDirPath,
@@ -23,6 +24,8 @@ import { EvenHubSession } from "./session";
import { createEvenHubWindow } from "./evenhub-window";
import { createEvenHubWebView, type EvenHubWebView } from "./webview";
import { shell } from "../../ui/shell/shell";
import { GrayImage } from "../../graphics/image";
import { grayImageToWhiteIconSource } from "../../native/gray-image-preview";
import {
readEvenHubPackageManifest,
installedEvenHubAppId,
@@ -36,12 +39,31 @@ type RunningApp = {
packageId: string;
session: EvenHubSession;
webView: EvenHubWebView;
/** The app's icon for the phone action bar, rendered on first use. */
phoneIcon?: ImageSource | null;
};
/** The phone action bar's button for opening the foreground app's phone UI. */
export type PhoneUiButton = { windowId: string; title: string; icon: ImageSource | null };
/** Pixel size the action-bar icon is rendered at (~21dp at xxhdpi). */
const PHONE_ICON_SIZE = 63;
const running = new Map<string, RunningApp>();
let nextSerial = 1;
let phoneShownWindowId: string | null = null;
let backHandlerRegistered = false;
const phoneShownListeners = new Set<() => void>();
/** Called whenever an app's phone UI is shown or hidden. Returns an unsubscribe. */
export function onPhoneShownChanged(listener: () => void): () => void {
phoneShownListeners.add(listener);
return () => phoneShownListeners.delete(listener);
}
function notifyPhoneShownChanged(): void {
for (const listener of phoneShownListeners) listener();
}
/** Intercept Android Back while an app's phone UI is overlaying the dashboard. */
function ensureBackHandler(): void {
@@ -112,6 +134,7 @@ async function startApp(
if (phoneShownWindowId === windowId) {
webView.hideOnPhone();
phoneShownWindowId = null;
notifyPhoneShownChanged();
}
webView.destroy();
cleanup?.();
@@ -205,15 +228,35 @@ export function showOnPhone(windowId: string): void {
const app = running.get(windowId);
if (!app) return;
if (phoneShownWindowId && phoneShownWindowId !== windowId) hidePhone();
app.webView.showOnPhone();
app.webView.showOnPhone(hidePhone);
phoneShownWindowId = windowId;
notifyPhoneShownChanged();
}
/** Return the phone to the dashboard (Back, or the shown app closing). */
/** Return the phone to the dashboard (Back, the close button, or the shown app closing). */
export function hidePhone(): void {
if (!phoneShownWindowId) return;
running.get(phoneShownWindowId)?.webView.hideOnPhone();
phoneShownWindowId = null;
notifyPhoneShownChanged();
}
/**
* The foreground glasses window, when it is an EvenHub app whose phone UI
* isn't showing: the phone action bar then offers a button to show it.
*/
export function phoneUiButton(): PhoneUiButton | null {
const window = shell.foregroundWindow();
const app = window ? running.get(window.windowId) : undefined;
if (!window || !app || phoneShownWindowId === app.windowId) return null;
if (app.phoneIcon === undefined) {
// The same artwork as the glasses sidebar (installed icon, else the
// generic package icon), drawn white.
const image = new GrayImage(PHONE_ICON_SIZE, PHONE_ICON_SIZE, 0);
window.drawIcon(image, 0, 0, PHONE_ICON_SIZE);
app.phoneIcon = grayImageToWhiteIconSource(image);
}
return { windowId: app.windowId, title: window.title, icon: app.phoneIcon };
}
export function runningAppCount(): number {
+27 -20
View File
@@ -1,6 +1,6 @@
import { getDefaultSmallFont } from "../../graphics/ui-fonts";
import { GrayImage } from "../../graphics/image";
import { truncateText } from "../../graphics/textwrap";
import { truncateText, wrapText } from "../../graphics/textwrap";
import { type InputEvent } from "../../ui/gestures";
import { Layer, type LayerContext, type PaintBelow } from "../../ui/layers";
import { drawListScrollbar, drawSelectionHighlight } from "../../ui/menu";
@@ -8,9 +8,9 @@ import { permissionDetail, permissionLabel, type EvenHubPermission } from "./per
import { openPrivacyPolicyOnPhone } from "./privacy-policy";
import { lineStep, listRowHeight } from "../../ui/metrics";
const DIALOG_X = 8;
const DIALOG_Y = 8;
const DIALOG_WIDTH = 272;
/** Minimum gap between the dialog and the edges of the window it's centered in. */
const DIALOG_MARGIN = 8;
const DIALOG_WIDTH = 320;
const PADDING = 10;
const PERM_GAP = 4;
@@ -27,6 +27,8 @@ const PERM_GAP = 4;
* list on screen.
*/
export class EvenHubPermissionDialogLayer implements Layer {
/** Floats in front of the window content, like the context menus. */
readonly depth = 2;
/** Starts on Allow; the optional privacy-policy action is inserted after it. */
private selectedIndex = 0;
/** First content row drawn; rows above it are scrolled off. */
@@ -50,10 +52,12 @@ export class EvenHubPermissionDialogLayer implements Layer {
paint(ctx: LayerContext, paintBelow: PaintBelow): GrayImage {
const font = getDefaultSmallFont();
const { height: viewportHeight } = ctx.stack.getBaseSize();
const { width: viewportWidth, height: viewportHeight } = ctx.stack.getBaseSize();
const image = paintBelow();
const textWidth = DIALOG_WIDTH - 2 * PADDING - 4;
const left = DIALOG_X + PADDING + 2;
const dialogWidth = Math.min(DIALOG_WIDTH, viewportWidth - 2 * DIALOG_MARGIN);
const dialogX = ((viewportWidth - dialogWidth) / 2) | 0;
const textWidth = dialogWidth - 2 * PADDING - 4;
const left = dialogX + PADDING + 2;
const headerStep = lineStep(font) + 2;
const permLabelStep = lineStep(font) + 1;
@@ -80,15 +84,17 @@ export class EvenHubPermissionDialogLayer implements Layer {
for (let index = 0; index < this.permissions.length; index++) {
const permission = this.permissions[index]!;
const detail = permissionDetail(permission);
// One row per wrapped line, so the explanation scrolls line by line.
const detailLines = detail ? wrapText(font, detail, textWidth - 8) : [];
const gapBelow = PERM_GAP + (index === this.permissions.length - 1 ? 2 : 0);
rows.push({
height: permLabelStep + (detail ? 0 : gapBelow),
height: permLabelStep + (detailLines.length ? 0 : gapBelow),
draw: (top) => image.drawText(font, left, top, truncateText(font, permissionLabel(permission.name), textWidth), 220),
});
if (detail) {
for (let line = 0; line < detailLines.length; line++) {
rows.push({
height: permDetailStep + gapBelow,
draw: (top) => image.drawText(font, left + 8, top, truncateText(font, detail, textWidth - 8), 140),
height: permDetailStep + (line === detailLines.length - 1 ? gapBelow : 0),
draw: (top) => image.drawText(font, left + 8, top, detailLines[line]!, 140),
});
}
}
@@ -98,15 +104,16 @@ export class EvenHubPermissionDialogLayer implements Layer {
draw: (top) => {
const selected = this.atBottom() && index === this.selectedIndex;
if (selected) {
drawSelectionHighlight(image, DIALOG_X + 12, top, DIALOG_WIDTH - 24, actionRowH - 1, focused, 8);
drawSelectionHighlight(image, dialogX + 12, top, dialogWidth - 24, actionRowH - 1, focused, 8);
}
image.drawText(font, DIALOG_X + 22, top + 3, actions[index]!, selected ? 255 : 200);
image.drawText(font, dialogX + 22, top + 3, actions[index]!, selected ? 255 : 200);
},
});
}
const contentHeight = rows.reduce((sum, row) => sum + row.height, 0);
const height = Math.min(contentHeight + 2 * PADDING, viewportHeight - 2 * DIALOG_Y);
const height = Math.min(contentHeight + 2 * PADDING, viewportHeight - 2 * DIALOG_MARGIN);
const dialogY = ((viewportHeight - height) / 2) | 0;
const visibleHeight = height - 2 * PADDING;
// The bottommost scroll position: the largest suffix of rows that fits.
@@ -120,13 +127,13 @@ export class EvenHubPermissionDialogLayer implements Layer {
this.firstRow = Math.max(0, Math.min(this.firstRow, maxFirstRow));
// Fill 1 (transparent color key is 0), outline for the dialog edge.
image.fillRoundedRect(DIALOG_X, DIALOG_Y, DIALOG_WIDTH, height, 1);
image.drawRoundedRect(DIALOG_X, DIALOG_Y, DIALOG_WIDTH, height, 72);
image.fillRoundedRect(dialogX, dialogY, dialogWidth, height, 1);
image.drawRoundedRect(dialogX, dialogY, dialogWidth, height, 72);
let y = DIALOG_Y + PADDING;
let y = dialogY + PADDING;
for (let index = this.firstRow; index < rows.length; index++) {
const row = rows[index]!;
if (y + row.height > DIALOG_Y + PADDING + visibleHeight) break;
if (y + row.height > dialogY + PADDING + visibleHeight) break;
row.draw(y);
y += row.height;
}
@@ -134,8 +141,8 @@ export class EvenHubPermissionDialogLayer implements Layer {
if (maxFirstRow > 0) {
drawListScrollbar(
image,
DIALOG_X + DIALOG_WIDTH - 7,
DIALOG_Y + PADDING,
dialogX + dialogWidth - 7,
dialogY + PADDING,
visibleHeight,
this.firstRow,
rows.length - maxFirstRow,
+1 -1
View File
@@ -22,7 +22,7 @@ export type EvenHubPermissionName =
| "album"
| "camera"
| "fs"
| string;
| (string & {});
export type EvenHubPermission = {
name: EvenHubPermissionName;
+68 -27
View File
@@ -110,30 +110,61 @@ export const EVENHUB_BRIDGE_INJECT_SCRIPT = `
};
})();
(function () {
// Host-driven timers. Chromium heavily throttles a page's own
// setTimeout/setInterval when the phone screen is off, which slows
// timer-driven apps to a crawl. Replace them with a queue fired by
// window.__fcTimerTick(), which the native host calls at ~60Hz regardless of
// Host-driven timers and requestAnimationFrame. Chromium heavily throttles a
// page's own setTimeout/setInterval when the phone screen is off, and pauses
// or throttles a hidden page's rAF, which slows timer-driven apps to a crawl
// and stalls canvas apps that render on a rAF loop. Replace them with queues
// fired by window.__fcTick(), which the native host calls regardless of
// screen state (host-initiated evaluateJavascript escapes the throttle).
if (window.__fcTimerTick) return;
// (The glasses render via BLE, not the phone's display, so vsync alignment
// doesn't matter — a steady frame rate does.)
//
// __fcTick returns how many ms until it next has work (-1: none), and the
// host sleeps until then instead of evaluating JS at 60Hz for an idle page.
// Work added outside a tick that is due before the host's next tick wakes it
// through the bridge.
if (window.__fcTick) return;
var FRAME_MS = 16;
var timers = new Map();
var nextId = 1;
var rafs = new Map();
var nextRafId = 1;
var inTick = false;
// When the host will next call __fcTick; Infinity while it is idle.
var hostNextTickAt = Infinity;
function needTickBy(due) {
if (inTick || due >= hostNextTickAt) return;
hostNextTickAt = Date.now();
var bridge = window.__faceclawEvenHub;
if (bridge && bridge.wakeTimers) bridge.wakeTimers();
}
window.setTimeout = function (cb, delay) {
var args = Array.prototype.slice.call(arguments, 2);
var id = nextId++;
timers.set(id, { cb: cb, args: args, due: Date.now() + (+delay || 0), interval: 0 });
var due = Date.now() + (+delay || 0);
timers.set(id, { cb: cb, args: args, due: due, interval: 0 });
needTickBy(due);
return id;
};
window.setInterval = function (cb, delay) {
var args = Array.prototype.slice.call(arguments, 2);
var id = nextId++;
var d = +delay || 0;
timers.set(id, { cb: cb, args: args, due: Date.now() + d, interval: d });
var due = Date.now() + d;
timers.set(id, { cb: cb, args: args, due: due, interval: d });
needTickBy(due);
return id;
};
window.clearTimeout = function (id) { timers.delete(id); };
window.clearInterval = function (id) { timers.delete(id); };
window.__fcTimerTick = function () {
window.requestAnimationFrame = function (cb) {
var id = nextRafId++;
rafs.set(id, cb);
needTickBy(Date.now() + FRAME_MS);
return id;
};
window.cancelAnimationFrame = function (id) { rafs.delete(id); };
function runTimers() {
var now = Date.now();
var dueIds = [];
timers.forEach(function (entry, id) { if (entry.due <= now) dueIds.push(id); });
@@ -151,24 +182,8 @@ export const EVENHUB_BRIDGE_INJECT_SCRIPT = `
}
try { entry.cb.apply(null, entry.args); } catch (e) { if (window.console) console.error(e); }
}
};
})();
(function () {
// Host-driven requestAnimationFrame, same idea as the timers above: a hidden
// page's rAF is paused/throttled with the screen off, stalling canvas apps
// that render on a rAF loop. Drive it from window.__fcRafTick(), called by the
// native ticker each frame. (The glasses render via BLE, not the phone's
// display, so vsync alignment doesn't matter — a steady frame rate does.)
if (window.__fcRafTick) return;
var rafs = new Map();
var nextRafId = 1;
window.requestAnimationFrame = function (cb) {
var id = nextRafId++;
rafs.set(id, cb);
return id;
};
window.cancelAnimationFrame = function (id) { rafs.delete(id); };
window.__fcRafTick = function () {
}
function runRafs() {
if (rafs.size === 0) return;
var ts = window.performance && performance.now ? performance.now() : Date.now();
// Snapshot and clear: callbacks that re-request during this frame run on
@@ -178,6 +193,32 @@ export const EVENHUB_BRIDGE_INJECT_SCRIPT = `
current.forEach(function (cb) {
try { cb(ts); } catch (e) { if (window.console) console.error(e); }
});
}
function inTickDo(fn) {
inTick = true;
try { fn(); } finally { inTick = false; }
}
// Fixed-rate entry points for hosts that don't use __fcTick's schedule (the
// iOS host calls both every frame).
window.__fcTimerTick = function () { inTickDo(runTimers); };
window.__fcRafTick = function () { inTickDo(runRafs); };
window.__fcTick = function () {
inTick = true;
try {
runTimers();
runRafs();
} finally {
inTick = false;
}
var now = Date.now();
var delay = rafs.size ? FRAME_MS : -1;
timers.forEach(function (entry) {
var d = entry.due - now;
if (delay < 0 || d < delay) delay = d;
});
if (delay >= 0) delay = Math.max(FRAME_MS, Math.ceil(delay));
hostNextTickAt = delay < 0 ? Infinity : now + delay;
return delay;
};
})();
`;
@@ -1136,7 +1177,7 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH
}
return out;
};
if (requested.length === 0 || requested.every((id) => this.grantedApiKeys.has(id))) {
if (requested.every((id) => this.grantedApiKeys.has(id))) {
return Promise.resolve(buildGrant());
}
if (!this.windowHooks) return Promise.resolve({});
+1 -1
View File
@@ -174,7 +174,7 @@ export class EvenHubStoreLayer implements Layer {
for (const [index, line] of wrapText(font, message, width - LIST_X * 2).entries()) {
image.drawText(font, LIST_X, headerH + 16 + index * lineStep(font), line, 190);
}
image.drawText(font, LIST_X, height - font.lineHeight - 4, `${GESTURE_CLICK} edit credentials`, 105);
image.drawText(font, LIST_X, height - font.lineHeight - 4, `${GESTURE_CLICK} log in`, 105);
return image;
}
+1 -1
View File
@@ -73,7 +73,7 @@ export async function checkForEvenHubUpdates(
onProgress?: (done: number, total: number) => void,
): Promise<EvenHubAppUpdate[]> {
const installed = getInstalledEvenHubApps();
const results: EvenHubAppUpdate[] = new Array(installed.length);
const results: EvenHubAppUpdate[] = [];
let next = 0;
let done = 0;
const worker = async () => {
+11 -13
View File
@@ -22,11 +22,13 @@ import { FACECLAW_VERSION } from "../../version";
declare const android: any;
declare const com: any;
declare const java: any;
export type EvenHubWebView = {
evaluateJs: (js: string) => void;
destroy: () => void;
showOnPhone: () => void;
/** Show the app's phone UI; `onClose` runs when its top bar's close button is tapped. */
showOnPhone: (onClose: () => void) => void;
hideOnPhone: () => void;
/** The native android.webkit.WebView, for the host. */
native: unknown;
@@ -96,16 +98,7 @@ export function createEvenHubWebView(session: EvenHubSession): EvenHubWebView {
webView.addJavascriptInterface(new com.faceclaw.app.FaceclawEvenHubJsBridge(listener), "__faceclawEvenHub");
// Surface the app's console in logcat (tag FaceclawEvenHubConsole).
const ChromeClient = (android.webkit.WebChromeClient as any).extend({
onConsoleMessage: (message: any): boolean => {
android.util.Log.i(
"FaceclawEvenHubConsole",
`${message.message()} (${message.sourceId()}:${message.lineNumber()})`,
);
return true;
},
});
webView.setWebChromeClient(new ChromeClient());
webView.setWebChromeClient(new com.faceclaw.app.FaceclawEvenHubChromeClient());
const nativeHost = com.faceclaw.app.FaceclawEvenHubWebViewHost.getInstance();
nativeHost.attach(activity, webView);
@@ -127,8 +120,13 @@ export function createEvenHubWebView(session: EvenHubSession): EvenHubWebView {
console.warn(`evenhub webview destroy failed: ${error}`);
}
},
showOnPhone: () =>
nativeHost.showOnPhone(Application.android?.foregroundActivity ?? Application.android?.startActivity ?? null, webView),
showOnPhone: (onClose: () => void) =>
nativeHost.showOnPhone(
Application.android?.foregroundActivity ?? Application.android?.startActivity ?? null,
webView,
session.manifest.name,
new java.lang.Runnable({ run: onClose }),
),
hideOnPhone: () => nativeHost.hideOnPhone(),
};
}
+18 -2
View File
@@ -2,6 +2,7 @@ import { readTextFile, type DirectoryEntry } from "../../native/file-access";
import { isFontFile } from "../../native/font-files";
import { installFontFile, isFontInstalled } from "../../graphics/installed-fonts";
import { isDecodableImageFile } from "../../native/image-files";
import { isPlayableVideoFile } from "../../native/video-player";
import { readEvenHubPackageManifest } from "../evenhub/installed-apps";
import { EvenHubPermissionDialogLayer } from "../evenhub/permission-dialog";
import { FileBrowserLayer } from "./file-browser";
@@ -35,17 +36,21 @@ export type FilesAppOptions = InProcessAppOptions & {
installEhpkApp: (path: string) => Promise<void> | void;
/** Open a font file's previewer as its own shell window. */
openFontWindow: (title: string, path: string) => void;
/** Open a video file's player (settings panel first) as its own shell window. */
openVideoWindow: (title: string, path: string) => void;
};
/**
* The Files app's launcher-opened window: a file browser over Places
* (bookmarks and storage roots). Picking any file opens an info dialog with
* metadata plus, for viewable types (text, images), the open actions.
* metadata plus, for viewable types (text, images, video), the open actions.
*/
export function createFilesAppWindow(options: FilesAppOptions): InProcessWindow {
let created: InProcessWindow | null = null;
const browser = new FileBrowserLayer({
isSupportedFile: (name) => TEXT_FILE.test(name) || isFontFile(name) || EHPK_FILE.test(name) || (!global.isIOS && isDecodableImageFile(name)),
isSupportedFile: (name) =>
TEXT_FILE.test(name) || isFontFile(name) || EHPK_FILE.test(name) ||
(!global.isIOS && (isDecodableImageFile(name) || isPlayableVideoFile(name))),
// The browser handles double-click itself (up a level), so it is not
// wrapped in YieldAtRootLayer; it yields explicitly from the top level.
onLeave: () => shell.yieldFocusToSidebar(),
@@ -230,6 +235,17 @@ function fileOpenActions(entry: DirectoryEntry, options: FilesAppOptions): FileI
},
];
}
if (isPlayableVideoFile(entry.name)) {
return [
{
label: "Play video",
onSelect: (ctx) => {
ctx.stack.pop();
options.openVideoWindow(entry.name, entry.path);
},
},
];
}
if (isFontFile(entry.name)) {
// Install copies into the app-internal fonts directory, making the face
// available in the Settings font pickers. The row relabels itself with
-1
View File
@@ -1,4 +1,3 @@
import { type BdfFont } from "../../graphics/bdffont";
import { getDefaultSmallFont } from "../../graphics/ui-fonts";
import { truncateText } from "../../graphics/textwrap";
import { GrayImage } from "../../graphics/image";
+13
View File
@@ -13,6 +13,7 @@ import {
FILES_SURFACE_ID,
FILES_WINDOW_ID,
} from "./files-app";
import { createVideoPlayerWindow } from "./video-window";
// Document windows are closeable and non-singleton; the serial keeps their
// windowIds unique.
@@ -51,6 +52,17 @@ function openFontDocumentWindow(ctx: AppContext, title: string, path: string): v
});
}
function openVideoWindow(ctx: AppContext, title: string, path: string): void {
const windowId = `files:doc:${nextDocumentSerial++}`;
void ctx
.launchInProcessApp(windowId, `window:${windowId}`, (options) =>
createVideoPlayerWindow(windowId, title, path, options),
)
.catch((error) => {
ctx.appendLog(`video window failed: ${error}`);
});
}
const filesApp: AppDefinition = {
appId: "files",
title: "Files",
@@ -78,6 +90,7 @@ const filesApp: AppDefinition = {
}
},
openFontWindow: (title, path) => openFontDocumentWindow(ctx, title, path),
openVideoWindow: (title, path) => openVideoWindow(ctx, title, path),
}),
),
// A document arriving via Android's Share intent opens as its own window.
+86
View File
@@ -0,0 +1,86 @@
/**
* Where a video goes in the player window, for each size choice. Pure
* geometry, so it is unit-tested without the NativeScript runtime.
*/
export type VideoSize = "small" | "medium" | "large" | "full";
export const VIDEO_SIZES: readonly VideoSize[] = ["small", "medium", "large", "full"];
export type Size = { width: number; height: number };
export type Rect = Size & { x: number; y: number };
/**
* Bounding boxes for the fixed sizes; a video is fitted inside, keeping its
* aspect. Large and full fit the whole viewport instead (full in a tall
* window). Bigger pictures cost the glasses link more per frame, so smaller
* sizes play at a higher frame rate.
*/
const FIXED_BOXES: Partial<Record<VideoSize, Size>> = {
small: { width: 160, height: 120 },
medium: { width: 240, height: 180 },
};
export function videoSizeLabel(size: VideoSize): string {
switch (size) {
case "small":
return "Small";
case "medium":
return "Medium";
case "large":
return "Large";
case "full":
return "Full screen";
}
}
/** Full screen needs the tall window band; the rest play in the standard one. */
export function videoHeightMode(size: VideoSize): "min" | "max" {
return size === "full" ? "max" : "min";
}
/**
* The largest size with the video's aspect ratio that fits in box, with even
* dimensions (the glasses' delta encoder works in 2-pixel rows and 4-pixel
* columns) of at least 2. Upscales small videos too: the size is the
* viewer's choice.
*/
export function fitVideo(video: Size, box: Size): Size {
if (video.width <= 0 || video.height <= 0) return { width: evenFloor(box.width), height: evenFloor(box.height) };
const scale = Math.min(box.width / video.width, box.height / video.height);
return {
width: evenFloor(Math.min(box.width, Math.round(video.width * scale))),
height: evenFloor(Math.min(box.height, Math.round(video.height * scale))),
};
}
/**
* The video's rect within a viewport, centered. x snaps to a multiple of 4
* and y to a multiple of 2 so frame deltas don't straddle an extra encoder
* block at each edge.
*/
export function videoRect(size: VideoSize, video: Size, viewport: Size): Rect {
const fixed = FIXED_BOXES[size];
const box = fixed
? { width: Math.min(fixed.width, viewport.width), height: Math.min(fixed.height, viewport.height) }
: viewport;
const fitted = fitVideo(video, box);
return {
x: Math.max(0, Math.floor((viewport.width - fitted.width) / 8) * 4),
y: Math.max(0, Math.floor((viewport.height - fitted.height) / 4) * 2),
...fitted,
};
}
/** "3:52", or "1:02:03" past an hour. */
export function formatMediaTime(ms: number): string {
const total = Math.max(0, Math.floor(ms / 1000));
const hours = Math.floor(total / 3600);
const minutes = Math.floor((total % 3600) / 60);
const seconds = String(total % 60).padStart(2, "0");
return hours > 0 ? `${hours}:${String(minutes).padStart(2, "0")}:${seconds}` : `${minutes}:${seconds}`;
}
function evenFloor(value: number): number {
return Math.max(2, Math.floor(value / 2) * 2);
}
+443
View File
@@ -0,0 +1,443 @@
import { getDefaultSmallFont } from "../../graphics/ui-fonts";
import { GrayImage } from "../../graphics/image";
import { truncateText, wrapText } from "../../graphics/textwrap";
import { PHOTO_GAMMA } from "../../native/image-files";
import { probeVideo, VideoPlayer, type VideoInfo, type VideoPlaybackState } from "../../native/video-player";
import { ConfigSettingBoolean, ConfigSettingEnum } from "../../ui/dashboard-settings";
import { type InputEvent } from "../../ui/gestures";
import { type Layer, type LayerContext } from "../../ui/layers";
import { drawRightValueMenuItem, drawToggleMenuItem } from "../../ui/menu";
import { Menu, type MenuDrawArgs } from "../../ui/menu-core";
import { LIST_ROW_TEXT_INSET, lineStep, listRowHeight } from "../../ui/metrics";
import {
createInProcessWindow,
YieldAtRootLayer,
type InProcessAppOptions,
type InProcessWindow,
} from "../../ui/shell/in-process-window";
import { appViewportSize } from "../../ui/shell/geometry";
import {
formatMediaTime,
VIDEO_SIZES,
videoHeightMode,
videoRect,
videoSizeLabel,
type Rect,
type VideoSize,
} from "./video-layout";
export const videoSizeSetting = new ConfigSettingEnum<VideoSize>({
id: "video-size",
label: "Size",
storageKey: "video.size",
defaultValue: "large",
values: VIDEO_SIZES,
formatValue: videoSizeLabel,
});
export const videoAudioSetting = new ConfigSettingBoolean({
id: "video-audio",
label: "Audio",
storageKey: "video.audio",
defaultValue: true,
description: "Play the video's sound on the phone's current audio output.",
});
export const videoMonochromeSetting = new ConfigSettingBoolean({
id: "video-monochrome",
label: "Monochrome",
storageKey: "video.monochrome",
defaultValue: false,
description: "Show only black and white: far less to send per frame, so a higher frame rate.",
});
/** How far one ring scroll seeks. */
const SEEK_STEP_MS = 10_000;
const MARGIN_X = 18;
const TITLE_Y = 10;
const PANEL_WIDTH = 340;
/**
* One opened video: its probe result, the native player, and the window
* glue. The window's base layer is the settings panel; playing pushes the
* playback layer, and backing out of it (or the video ending) returns to the
* panel with the position kept for Resume.
*/
class VideoSession {
readonly probe: VideoInfo | { error: string };
private player: VideoPlayer | null = null;
private window: InProcessWindow | null = null;
private playback: VideoPlaybackLayer | null = null;
/** Where Resume starts; 0 = from the beginning. */
resumeMs = 0;
/** The last playback error, shown on the panel until the next Play. */
error: string | null = null;
constructor(
readonly path: string,
readonly title: string,
readonly surfaceId: string,
) {
this.probe = probeVideo(path);
}
get info(): VideoInfo | null {
return "error" in this.probe ? null : this.probe;
}
attach(window: InProcessWindow): void {
this.window = window;
}
isPlaying(): boolean {
return this.player?.isPlaying() ?? false;
}
positionMs(): number {
return this.player?.positionMs() ?? this.resumeMs;
}
/** Current rect for a size choice, in the viewport of the band that size plays in. */
rectFor(size: VideoSize, viewport: { width: number; height: number }): Rect {
const info = this.info;
return videoRect(size, info ? { width: info.width, height: info.height } : viewport, viewport);
}
play(ctx: LayerContext, fromStart: boolean): void {
const window = this.window;
const info = this.info;
if (!window || !info) return;
this.error = null;
const size = videoSizeSetting.get();
window.setHeightMode(videoHeightMode(size));
const viewport = ctx.stack.getBaseSize();
const rect = this.rectFor(size, viewport);
const player = this.ensurePlayer();
this.playback = new VideoPlaybackLayer(this, rect, viewport);
ctx.stack.push(this.playback);
player.start({
path: this.path,
surfaceId: this.surfaceId,
rect,
monochrome: videoMonochromeSetting.get(),
gamma: PHOTO_GAMMA,
audio: info.hasAudio && videoAudioSetting.get(),
startMs: fromStart ? 0 : this.resumeMs,
});
}
togglePause(): void {
const player = this.player;
if (!player) return;
if (player.isPlaying()) player.pause();
else player.resume();
}
seekBy(deltaMs: number): void {
const player = this.player;
if (!player) return;
const duration = this.info?.durationMs ?? 0;
const target = Math.max(0, player.positionMs() + deltaMs);
player.seekTo(duration > 0 ? Math.min(target, duration) : target);
}
/** The window went to the background or the screen went off: stop, keep the place. */
pauseForHost(): void {
if (!this.player?.isPlaying()) return;
this.player.pause();
// Paint the paused overlay now, so the retained surface shows it on return.
this.window?.requestRender();
}
/** Leave the playback layer for the panel, remembering where to resume. */
returnToPanel(resumeMs: number): void {
const player = this.player;
if (player) {
player.pause();
player.stop();
}
this.resumeMs = resumeMs;
const window = this.window;
const playback = this.playback;
this.playback = null;
if (window && playback) window.stack.popThrough(playback);
window?.setHeightMode("min");
window?.requestRender();
}
lastFrame(rect: Rect): GrayImage | null {
return this.player?.lastFrame(rect.width, rect.height) ?? null;
}
release(): void {
this.player?.release();
this.player = null;
}
private ensurePlayer(): VideoPlayer {
if (!this.player) {
this.player = new VideoPlayer({
onState: (state, positionMs) => this.onPlayerState(state, positionMs),
onError: (message) => {
this.error = message;
this.returnToPanel(this.positionMs());
},
});
}
return this.player;
}
private onPlayerState(state: VideoPlaybackState, _positionMs: number): void {
if (state === "ended") {
this.returnToPanel(0);
return;
}
// A paused seek decoded its new frame: repaint it under the overlay.
if (state === "paused") this.window?.requestRender();
}
}
type PanelRow = { kind: "size" } | { kind: "audio" } | { kind: "monochrome" } | { kind: "play"; fromStart: boolean };
/**
* The pre-play settings panel (the window's root): picture size, audio and
* monochrome, all remembered for the next video, then Play (or Resume plus
* Play from start once a position is kept).
*/
class VideoSettingsLayer implements Layer {
private readonly menu = new Menu<PanelRow>({
wrap: false,
rowGap: 1,
highlight: { radius: 6 },
getHeight: () => listRowHeight(getDefaultSmallFont()),
draw: (args) => this.drawRow(args),
});
constructor(private readonly session: VideoSession) {}
private rows(): PanelRow[] {
if (!this.session.info) return [];
const rows: PanelRow[] = [{ kind: "size" }, { kind: "audio" }, { kind: "monochrome" }];
rows.push({ kind: "play", fromStart: false });
if (this.session.resumeMs > 0) rows.push({ kind: "play", fromStart: true });
return rows;
}
paint(ctx: LayerContext): GrayImage {
const font = getDefaultSmallFont();
const { width, height } = ctx.stack.getBaseSize();
const image = new GrayImage(width, height, 0);
image.drawText(font, MARGIN_X, TITLE_Y, truncateText(font, this.session.title, width - 2 * MARGIN_X), 220);
const step = lineStep(font);
let y = TITLE_Y + step;
const info = this.session.info;
const errors: string[] = [];
if (!info) errors.push(`Can't play this video: ${(this.session.probe as { error: string }).error}`);
if (this.session.error) errors.push(`Playback stopped: ${this.session.error}`);
if (info) {
const facts = [`${info.width}x${info.height}`, formatMediaTime(info.durationMs), info.hasAudio ? "sound" : "no sound"];
image.drawText(font, MARGIN_X, y, facts.join(" · "), 130);
y += step;
}
for (const error of errors) {
for (const line of wrapText(font, error, width - 2 * MARGIN_X, { breakLongWords: true }).slice(0, 3)) {
image.drawText(font, MARGIN_X, y, line, 200);
y += step;
}
}
y += 6;
const rows = this.rows();
const selected = this.menu.selectedItem;
const index = selected ? rows.findIndex((row) => sameRow(row, selected)) : -1;
// Land on Resume/Play by default: the settings usually stay as they were.
const playIndex = rows.findIndex((row) => row.kind === "play");
this.menu.setItems(rows, index >= 0 ? index : playIndex >= 0 ? playIndex : undefined);
const panelWidth = Math.min(PANEL_WIDTH, width - 2 * (MARGIN_X - 6));
this.menu.paint(image, { x: MARGIN_X - 6, y, width: panelWidth, height: Math.max(0, height - y - 4) },
ctx.stack.isFocused());
return image;
}
async handleInput(event: InputEvent, ctx: LayerContext): Promise<void> {
switch (event.type) {
case "scroll-up":
case "scroll-down":
await this.menu.handleInput(event);
return;
case "click": {
const row = this.menu.selectedItem;
if (row) this.activate(row, ctx);
return;
}
default:
return;
}
}
private activate(row: PanelRow, ctx: LayerContext): void {
switch (row.kind) {
case "size":
videoSizeSetting.set(videoSizeSetting.next());
return;
case "audio":
if (this.session.info?.hasAudio) videoAudioSetting.toggle();
return;
case "monochrome":
videoMonochromeSetting.toggle();
return;
case "play":
this.session.play(ctx, row.fromStart);
return;
}
}
private drawRow({ image, item, x, y, width, selected }: MenuDrawArgs<PanelRow>): void {
const font = getDefaultSmallFont();
const textX = x + 10;
const textWidth = width - 20;
switch (item.kind) {
case "size": {
const size = videoSizeSetting.get();
// The picture size this choice gets in its own band (full screen plays in the tall window).
const rect = this.session.rectFor(size, appViewportSize(videoHeightMode(size)));
drawRightValueMenuItem(image, font, textX, y, textWidth, "Size",
`${videoSizeLabel(size)} ${rect.width}x${rect.height}`);
return;
}
case "audio": {
const hasAudio = this.session.info?.hasAudio ?? false;
drawToggleMenuItem(image, font, textX, y, textWidth, hasAudio ? "Audio" : "Audio (none in file)",
hasAudio && videoAudioSetting.get(), selected);
return;
}
case "monochrome":
drawToggleMenuItem(image, font, textX, y, textWidth, "Monochrome", videoMonochromeSetting.get(), selected);
return;
case "play": {
const resumeMs = this.session.resumeMs;
const label = item.fromStart ? "Play from start" : resumeMs > 0 ? `Resume from ${formatMediaTime(resumeMs)}` : "Play";
image.drawText(font, textX, y + LIST_ROW_TEXT_INSET, label, selected ? 255 : 200);
return;
}
}
}
}
function sameRow(a: PanelRow, b: PanelRow): boolean {
return a.kind === b.kind && (a.kind !== "play" || b.kind !== "play" || a.fromStart === b.fromStart);
}
/**
* The playing video. The native player draws frames into the video rect on
* its own; this layer paints only when the window renders (input, pause,
* focus): black around the latest frame, plus a status box while paused.
* Click pauses/resumes, scroll seeks 10s, double-click returns to the panel.
*/
class VideoPlaybackLayer implements Layer {
private leaving = false;
constructor(
private readonly session: VideoSession,
private readonly rect: Rect,
/** The viewport the rect was laid out in. */
private readonly viewport: { width: number; height: number },
) {}
paint(ctx: LayerContext): GrayImage {
const { width, height } = ctx.stack.getBaseSize();
if ((width !== this.viewport.width || height !== this.viewport.height) && !this.leaving) {
// The window was relaid out (display mode change) and the native player's rect no
// longer fits the surface: back out to the panel, which lays out afresh on Play.
this.leaving = true;
setTimeout(() => this.session.returnToPanel(this.session.positionMs()), 0);
}
const image = new GrayImage(width, height, 0);
const frame = this.session.lastFrame(this.rect);
if (frame) image.bitBlt(frame, this.rect.x, this.rect.y);
if (!this.session.isPlaying()) this.drawPausedBox(image, frame === null);
return image;
}
async handleInput(event: InputEvent): Promise<void> {
switch (event.type) {
case "click":
this.session.togglePause();
return;
case "scroll-up":
this.session.seekBy(-SEEK_STEP_MS);
return;
case "scroll-down":
this.session.seekBy(SEEK_STEP_MS);
return;
case "double-click":
this.session.returnToPanel(this.session.positionMs());
return;
default:
return;
}
}
/**
* "Paused 1:23 / 3:52" over a progress bar, below the picture when there is
* room (small sizes), else over its bottom edge.
*/
private drawPausedBox(image: GrayImage, loading: boolean): void {
const font = getDefaultSmallFont();
const duration = this.session.info?.durationMs ?? 0;
const position = this.session.positionMs();
const text = loading ? "Loading..." : `Paused ${formatMediaTime(position)} / ${formatMediaTime(duration)}`;
const boxWidth = Math.min(image.width - 8, font.measureText(text) + 24);
const boxHeight = font.lineHeight + 14;
const rect = this.rect;
const x = Math.round(rect.x + (rect.width - boxWidth) / 2);
const below = rect.y + rect.height + 4;
const y = below + boxHeight <= image.height ? below : Math.max(0, rect.y + rect.height - boxHeight - 6);
image.fillRoundedRect(x, y, boxWidth, boxHeight, 1, 6);
image.drawRoundedRect(x, y, boxWidth, boxHeight, 72, 6);
image.drawText(font, x + 12, y + 5, text, 220);
if (!loading && duration > 0) {
const barWidth = boxWidth - 24;
image.fillRect(x + 12, y + boxHeight - 5, barWidth, 2, 40);
image.fillRect(x + 12, y + boxHeight - 5, Math.round((barWidth * Math.min(position, duration)) / duration), 2, 200);
}
}
}
/** A video file's player as its own window, opened from the Files app. */
export function createVideoPlayerWindow(
windowId: string,
title: string,
path: string,
options: InProcessAppOptions,
): InProcessWindow {
const session = new VideoSession(path, title, `window:${windowId}`);
const window = createInProcessWindow({
appId: "files",
windowId,
title,
iconLetter: "V",
icon: "film",
closeable: true,
actions: options.actions,
baseLayer: new YieldAtRootLayer(new VideoSettingsLayer(session)),
// A playing video is the thing being watched: no idle screen-off.
keepsScreenOn: () => session.isPlaying(),
onForegroundChanged: (foreground) => {
if (!foreground) session.pauseForHost();
},
setScreenOn: (on) => {
if (!on) session.pauseForHost();
},
submitFrame: options.submitFrame,
setSurfaceVisible: options.setSurfaceVisible,
removeSurface: options.removeSurface,
reconfigureSurface: options.reconfigureSurface,
onClosed: () => {
session.release();
options.onClosed();
},
});
session.attach(window);
return window;
}
+27 -12
View File
@@ -17,8 +17,9 @@
* fall back to raster (the planner needs a non-negative origin), which is
* small and rare.
*
* Controls (a watch swipe in either direction is the primary flap):
* ring-press flaps immediately; watch swipes/clicks also flap. Double-click pauses;
* Controls: a touch-down on the ring or watch pad (ring-press) flaps
* immediately; a click, a watch swipe in either direction or a crown turn
* without one flaps too. Double-click pauses;
* tap-then-hold opens the window menu. Ready / paused / game over: click or
* a swipe starts or resumes, double-click yields focus. Losing input focus
* mid-flight (a shell overlay such as the system menu or a notification,
@@ -45,6 +46,7 @@ import {
GESTURE_CLICK,
GESTURE_DOUBLE_CLICK,
type InputEvent,
PressTracker,
} from "../../ui/gestures";
import { clamp } from "../../util/numeric-util";
import {
@@ -153,6 +155,8 @@ type FlappyWindow = {
lastMinorSfxAtMs: number;
soundOn: boolean;
lastSubmittedFingerprint: string;
/** Which gestures already flapped on their ring-press. */
presses: PressTracker;
};
const windows = new Map<string, FlappyWindow>();
@@ -210,6 +214,7 @@ global.onmessage = (event: { data: WorkerAppMessage }) => {
lastMinorSfxAtMs: 0,
soundOn: loadSoundEnabled("flappy"),
lastSubmittedFingerprint: "",
presses: new PressTracker(),
};
resetGame(window);
windows.set(message.windowId, window);
@@ -282,6 +287,10 @@ global.onmessage = (event: { data: WorkerAppMessage }) => {
syncTickTimer(window);
}
break;
case "navigation-sensors":
case "text-input":
case "tool-call":
break;
}
};
@@ -374,10 +383,11 @@ function windowMenu(window: FlappyWindow): WindowMenu {
// --- Input ------------------------------------------------------------------
function handleInput(window: FlappyWindow, event: InputEvent, frameId: number): void {
const followsPress = window.presses.followsPress(event);
// An open window menu owns all input (it closes itself via pop); menus are
// list UIs, so watch swipes take their standard fallback meanings there.
if (window.menu?.isOpen()) {
window.menu
void window.menu
.handleInput(directionalFallback(event))
.catch((error) => console.error(`flappy menu input failed: ${error}`))
.then(() => renderAndSubmit(window, frameId));
@@ -398,7 +408,7 @@ function handleInput(window: FlappyWindow, event: InputEvent, frameId: number):
switch (window.phase) {
case "ready":
case "playing":
handleFlightInput(window, event, frameId);
handleFlightInput(window, event, frameId, followsPress);
break;
case "dying":
// Let the tumble finish; a flap mashed during it must not restart.
@@ -410,22 +420,24 @@ function handleInput(window: FlappyWindow, event: InputEvent, frameId: number):
}
}
/** Input while ready or flying: the flap gestures, plus pause. */
function handleFlightInput(window: FlappyWindow, event: InputEvent, frameId: number): void {
/**
* Input while ready or flying: the flap gestures, plus pause. `followsPress`
* marks a gesture whose touch-down already flapped (see PressTracker).
*/
function handleFlightInput(window: FlappyWindow, event: InputEvent, frameId: number, followsPress: boolean): void {
switch (event.type) {
case "ring-press":
flap(window);
break;
// Either swipe direction flaps: on the watch the flap is a reflex, and
// which way the thumb went shouldn't matter.
case "swipe-up":
case "swipe-down":
case "ring-press":
flap(window);
break;
case "click":
// The ring already flapped on down; its later click must not flap again.
if (event.source !== "ring") flap(window);
if (!followsPress) flap(window);
break;
case "scroll-up":
if (event.source === "watch") flap(window);
if (event.source === "watch" && !followsPress) flap(window);
break;
case "double-click":
if (window.phase === "playing") {
@@ -720,6 +732,9 @@ function paintHud(image: GrayImage, window: FlappyWindow): void {
`${GESTURE_CLICK} new game ${GESTURE_DOUBLE_CLICK} leave`,
]);
break;
case "playing":
case "dying":
break;
}
}
+8 -5
View File
@@ -58,9 +58,6 @@ function rankOf(card: number): number {
function isRed(card: number): boolean {
return (card & 1) === 1;
}
function cardLabel(card: number): string {
return RANK_CHARS[rankOf(card)]! + SUIT_CHARS[suitOf(card)]!;
}
const CARD_W = 60;
const CARD_H = 36;
@@ -208,6 +205,13 @@ global.onmessage = (event: { data: WorkerAppMessage }) => {
if (window.foreground) renderAndSubmit(window, 0);
break;
}
case "input-focus":
case "navigation-sensors":
case "resize-window":
case "screen":
case "text-input":
case "tool-call":
break;
}
};
@@ -285,7 +289,7 @@ function handleInput(window: FreecellWindow, event: InputEvent, frameId: number)
// An open window menu owns all input (it closes itself via pop); menus are
// list UIs, so watch swipes take their standard fallback meanings there.
if (window.menu?.isOpen()) {
window.menu
void window.menu
.handleInput(directionalFallback(event))
.catch((error) => console.error(`freecell menu input failed: ${error}`))
.then(() => renderAndSubmit(window, frameId));
@@ -792,7 +796,6 @@ function paintCard(
if (selected) image.drawRoundedRect(x + 1, y + 1, CARD_W - 2, CARD_H - 2, 255, 3);
const rankText: string = RANK_CHARS[rankOf(card)]!
const suitText: string = SUIT_CHARS[suitOf(card)]!;
const labelText = rankText + suitText;
image.drawText(labelFont, x + 5, y, rankText, 255);
image.drawText(suitFont, x + 5 + labelFont.measureText(rankText), y + 3, suitText, 150);
if (isRed(card)) {
+17 -4
View File
@@ -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");
}
+8
View File
@@ -0,0 +1,8 @@
import { UiFontOverride } from "../../graphics/ui-fonts";
/**
* The font Glanceboard widgets paint with: the Glanceboard's own Font
* setting, or the Display UI font while that is unset. Its small, medium and
* large roles carry the same line-height guarantees as getDefault*Font.
*/
export const glanceFont = new UiFontOverride("glanceboard.font");
+10 -4
View File
@@ -2,6 +2,7 @@ import { GrayImage } from "../../graphics/image";
import { truncateText, wrapText } from "../../graphics/textwrap";
import { getDefaultSmallFont } from "../../graphics/ui-fonts";
import { enumSettingMenuItem, onAnySettingChanged, toggleSettingMenuItem } from "../../ui/dashboard-settings";
import { uiFontOverridePickerMenuItem } from "../../ui/font-picker";
import { type InputEvent } from "../../ui/gestures";
import { type Layer, type LayerContext } from "../../ui/layers";
import { drawSelectionHighlight, isMenuItemDisabled, MenuLayer, openModalMenu, type MenuItem } from "../../ui/menu";
@@ -10,6 +11,7 @@ import { LIST_ROW_TEXT_INSET, lineStep, listRowHeight } from "../../ui/metrics";
import { createInProcessWindow, type InProcessAppOptions, type InProcessWindow } from "../../ui/shell/in-process-window";
import { shell } from "../../ui/shell/shell";
import { GlanceBoard } from "./board";
import { glanceFont } from "./glance-font";
import {
clearConflictingSlots,
glanceboardEnabledSetting,
@@ -298,7 +300,7 @@ function openSlotPicker(ctx: LayerContext, slotIndex: number): void {
openModalMenu(ctx, setting.label, items, Math.max(0, setting.values.indexOf(current)));
}
/** Settings: layout, sleep gestures, duration, slot lines, and stereo depth. */
/** Settings: layout, sleep gestures, duration, slot lines, widget font, and stereo depth. */
function openGlanceSettings(ctx: LayerContext): void {
const items: MenuItem[] = [
enumSettingMenuItem(glanceLayoutSetting),
@@ -306,6 +308,11 @@ function openGlanceSettings(ctx: LayerContext): void {
toggleSettingMenuItem(glanceShowOnLongPressSetting),
toggleSettingMenuItem(glanceShowOnHeadTiltSetting),
toggleSettingMenuItem(glanceShowLinesSetting),
uiFontOverridePickerMenuItem(glanceFont, {
title: "Glanceboard font",
description:
"Typeface for the Glanceboard's widgets. Same as UI font follows the Font setting in Settings > Customization.",
}),
enumSettingMenuItem(glanceDepthSetting),
];
ctx.stack.push(new MenuLayer("Glanceboard settings", items, pageMenuLayout(ctx.stack.getBaseSize().width)));
@@ -344,7 +351,6 @@ class GlancePreviewLayer implements Layer {
export function createGlanceboardAppWindow(options: InProcessAppOptions): InProcessWindow {
let previewVisible = false;
const heightMode = () => previewVisible && glanceLayoutSetting.get() === "2x3" ? "max" as const : "medium" as const;
let unsubscribeSettings: (() => void) | undefined;
const app = createInProcessWindow({
appId: "glanceboard",
windowId: GLANCEBOARD_WINDOW_ID,
@@ -367,10 +373,10 @@ export function createGlanceboardAppWindow(options: InProcessAppOptions): InProc
removeSurface: options.removeSurface,
reconfigureSurface: options.reconfigureSurface,
onClosed: () => {
unsubscribeSettings?.();
unsubscribeSettings();
options.onClosed();
},
});
unsubscribeSettings = onAnySettingChanged(() => app.setHeightMode(heightMode()));
const unsubscribeSettings = onAnySettingChanged(() => app.setHeightMode(heightMode()));
return app;
}
+2 -1
View File
@@ -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", "health"];
const SLOT_CHOICES: readonly GlanceSlotChoice[] = ["none", "system-card", "calendar", "terminal", "t3code", "nightscout", "compass", "music", "health"];
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",
+1 -1
View File
@@ -27,7 +27,7 @@ export interface GlanceWidget {
paint(image: GrayImage): void;
}
export type GlanceWidgetId = "system-card" | "nightscout" | "compass" | "music" | "calendar" | "terminal" | "health";
export type GlanceWidgetId = "system-card" | "nightscout" | "compass" | "music" | "calendar" | "terminal" | "t3code" | "health";
export type GlanceWidgetDefinition = {
id: GlanceWidgetId;
@@ -1,10 +1,10 @@
import { type GrayImage, type UiFont } from "../../../graphics/image";
import { truncateText } from "../../../graphics/textwrap";
import { getDefaultMediumFont, getDefaultSmallFont } from "../../../graphics/ui-fonts";
import { hasCalendarPermission } from "../../../native/calendar-permissions";
import { getCalendarReadState, onCalendarChanged, readUpcomingEvents, type CalendarEvent } from "../../../native/calendar";
import { lineStep } from "../../../ui/metrics";
import { dayHeaderLabel, formatEventTime } from "../../calendar/calendar";
import { glanceFont } from "../glance-font";
import { type GlanceWidget } from "../widget";
const PAD = 8;
@@ -38,8 +38,8 @@ export class CalendarWidget implements GlanceWidget {
}
paint(image: GrayImage): void {
const small = getDefaultSmallFont();
const medium = getDefaultMediumFont();
const small = glanceFont.small();
const medium = glanceFont.medium();
const step = lineStep(small);
const textWidth = image.width - 2 * PAD;
@@ -1,11 +1,11 @@
import { type GrayImage } from "../../../graphics/image";
import { getDefaultMediumFont, getDefaultSmallFont } from "../../../graphics/ui-fonts";
import { hasLocationPermission } from "../../../native/location-permissions";
import { addCompassListener, COMPASS_CHANGED, setCompassEnabled } from "../../../native/compass";
import { isCompassCalibrated, normalizeHeading } from "../../compass/calibration";
import { cardinalDirection, createCompassBackground, drawCompassRose, layoutCompassRose, TICK_HEIGHT } from "../../compass/compass-rose";
import { getDeclinationAvailability, onDeclinationChanged, refreshDeclination } from "../../compass/declination";
import { getNorthReference, resolveHeading } from "../../compass/heading";
import { glanceFont } from "../glance-font";
import { type GlanceWidget } from "../widget";
/** Owner token for the refcounted magnetometer, distinct from the Compass app's. */
@@ -63,8 +63,8 @@ export class CompassWidget implements GlanceWidget {
}
paint(image: GrayImage): void {
const medium = getDefaultMediumFont();
const small = getDefaultSmallFont();
const medium = glanceFont.medium();
const small = glanceFont.small();
const { width, height } = image;
const heading = this.rawHeading === null ? null : resolveHeading(this.rawHeading).displayDegrees;
const headingText = heading === null ? "--°" : `${Math.round(heading)}° ${cardinalDirection(heading)}`;
+3 -1
View File
@@ -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";
import { HealthWidget } from "./health-widget";
@@ -12,13 +13,14 @@ 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() },
{ id: "health", label: "Health", create: () => new HealthWidget() },
];
export function findGlanceWidget(id: GlanceWidgetId | string): GlanceWidgetDefinition | null {
export function findGlanceWidget(id: GlanceWidgetId | (string & {})): GlanceWidgetDefinition | null {
return GLANCE_WIDGETS.find((widget) => widget.id === id) ?? null;
}
+4 -4
View File
@@ -1,9 +1,9 @@
import { GrayImage } from "../../../graphics/image";
import { renderIcon } from "../../../graphics/icons";
import { getDefaultMediumFont, getDefaultSmallFont } from "../../../graphics/ui-fonts";
import { truncateText } from "../../../graphics/textwrap";
import { mediaControllerBridge, type MediaControllerState } from "../../../native/media-controller";
import { clamp } from "../../../util/numeric-util";
import { glanceFont } from "../glance-font";
import { type GlanceWidget } from "../widget";
const PAD = 8;
@@ -75,8 +75,8 @@ export class MusicWidget implements GlanceWidget {
}
paint(image: GrayImage): void {
const medium = getDefaultMediumFont();
const small = getDefaultSmallFont();
const medium = glanceFont.medium();
const small = glanceFont.small();
const media = mediaControllerBridge.snapshot();
this.media = media;
if (!media.available || (!media.title && !media.artist)) {
@@ -142,7 +142,7 @@ export class MusicWidget implements GlanceWidget {
image.drawRect(dx - 1, dy - 1, this.art.width + 2, this.art.height + 2, 60);
} else {
image.drawRect(ART_X, artY, ART_SIZE, ART_SIZE, 60);
const font = getDefaultSmallFont();
const font = glanceFont.small();
image.drawText(font, ART_X + Math.round((ART_SIZE - font.measureText("no art")) / 2),
artY + Math.round((ART_SIZE - font.lineHeight) / 2), "no art", 90);
}
@@ -1,5 +1,4 @@
import { GrayImage } from "../../../graphics/image";
import { getDefaultLargeFont, getDefaultSmallFont } from "../../../graphics/ui-fonts";
import { truncateText } from "../../../graphics/textwrap";
import { nightscoutBridge, type NightscoutState } from "../../../native/nightscout-bridge";
import { isNightscoutSettingsConfigured, loadNightscoutThresholds } from "../../../ui/dashboard-settings";
@@ -13,6 +12,7 @@ import {
formatDelta,
isNightscoutPointStale,
} from "../../nightscout/nightscout";
import { glanceFont } from "../glance-font";
import { type GlanceWidget } from "../widget";
const PAD = 8;
@@ -48,8 +48,8 @@ export class NightscoutWidget implements GlanceWidget {
}
paint(image: GrayImage): void {
const small = getDefaultSmallFont();
const large = getDefaultLargeFont();
const small = glanceFont.small();
const large = glanceFont.large();
const state = this.state;
const nowMs = Date.now();
const step = lineStep(small);
+77 -23
View File
@@ -1,8 +1,14 @@
import { BATTERY_ICON_WIDTH, drawBattery } from "../../../graphics/battery";
import {
BATTERY_ICON_WIDTH,
DENSE_BATTERY_BLOCK_HEIGHT,
drawBattery,
drawDenseBatteries,
type BatteryDevice,
} from "../../../graphics/battery";
import { type GrayImage, type UiFont } from "../../../graphics/image";
import { getDefaultLargeFont, getDefaultMediumFont, getDefaultSmallFont } from "../../../graphics/ui-fonts";
import { onAndroidNotificationPosted, readActiveNotificationIcons } from "../../../native/notification-icons";
import { readPhoneBatteryState } from "../../../native/phone-battery";
import { weatherBridge } from "../../../native/weather";
import { formatClockDate, formatClockTime } from "../../../ui/clock-format";
import {
batteryDisplayModeSetting,
@@ -20,6 +26,9 @@ import { truncateText } from "../../../graphics/textwrap";
import { timerEngine } from "../../timer/timer-engine";
import { formatCountdown, sortTimers, timerDisplayName, timerPhase, timerRemainingMs } from "../../timer/timer-model";
import { lineStep } from "../../../ui/metrics";
import { drawWeatherReading, measureWeatherReading, weatherReading } from "../../weather/weather-icons";
import { weatherShowOnSystemCardSetting } from "../../weather/weather-settings";
import { glanceFont } from "../glance-font";
import { type GlanceWidget } from "../widget";
const PAD = 8;
@@ -27,15 +36,20 @@ const NOTIFICATION_ICON_SIZE = 24;
const NOTIFICATION_ICON_GAP = 4;
const BATTERY_ITEM_GAP = 12;
const BATTERY_LABEL_VALUE = 150;
/** Gap between the battery block and the weather reading below it. */
const WEATHER_GAP = 6;
type BatteryItem = { label: string; percent: number; charging: boolean };
type BatteryItem = { label: string; device: BatteryDevice; percent: number; charging: boolean };
/** A block drawn against the card's right edge: its left edge and its rows. */
type RightBlock = { left: number; top: number; bottom: number };
/**
* Date and time, the phone's notification icons, the phone/watch/G2/R1
* battery indicators (the Wear OS watch's only while one is reachable), and
* battery indicators (the Wear OS watch's only while one is reachable), the
* current weather (when the Weather app's "Show on system card" is on), and
* the Timers app's countdowns: the top bar's contents plus timers, laid out
* for a card. Battery style and per-device visibility
* follow Settings > Display > Battery indicators, so the card agrees with
* follow Settings > Customization > Battery indicators, so the card agrees with
* the bar. Countdowns tick once a second only while one is running.
*/
export class SystemCardWidget implements GlanceWidget {
@@ -45,6 +59,7 @@ export class SystemCardWidget implements GlanceWidget {
private unsubscribeNotifications: (() => void) | null = null;
private unsubscribeSettings: (() => void) | null = null;
private unsubscribeTimers: (() => void) | null = null;
private unsubscribeWeather: (() => void) | null = null;
start(requestRender: () => void): void {
this.requestRender = requestRender;
@@ -55,6 +70,8 @@ export class SystemCardWidget implements GlanceWidget {
this.syncSecondTick();
this.requestRender?.();
});
// The Weather app's indicator keeps the bridge refreshing; this only listens.
this.unsubscribeWeather = weatherBridge.onStateChange(() => this.requestRender?.());
this.syncSecondTick();
}
@@ -70,6 +87,8 @@ export class SystemCardWidget implements GlanceWidget {
this.unsubscribeSettings = null;
this.unsubscribeTimers?.();
this.unsubscribeTimers = null;
this.unsubscribeWeather?.();
this.unsubscribeWeather = null;
}
/** Countdowns change every second; run that clock only while one is running. */
@@ -97,14 +116,20 @@ export class SystemCardWidget implements GlanceWidget {
}
paint(image: GrayImage): void {
const large = getDefaultLargeFont();
const medium = getDefaultMediumFont();
const small = getDefaultSmallFont();
const large = glanceFont.large();
const medium = glanceFont.medium();
const small = glanceFont.small();
const now = new Date();
// Clock and date down the left; batteries take the right edge.
const batteryLeft = drawBatteryBlock(image, small, image.width - PAD, PAD);
const textWidth = Math.max(0, batteryLeft - BATTERY_ITEM_GAP - PAD);
// Clock and date down the left; batteries take the right edge, with the
// weather right-aligned beneath them. Text on the left stops short of
// whichever of those shares its rows.
const battery = drawBatteryBlock(image, small, image.width - PAD, PAD);
const weatherTop = battery.bottom > battery.top ? battery.bottom + WEATHER_GAP : PAD;
const weather = drawWeatherBlock(image, medium, image.width - PAD, weatherTop, now.getTime());
const textRight = (top: number, bottom: number): number =>
weather && weather.top < bottom && weather.bottom > top ? Math.min(battery.left, weather.left) : battery.left;
const textWidth = Math.max(0, textRight(PAD, PAD + large.lineHeight) - BATTERY_ITEM_GAP - PAD);
const timeText = formatClockTime(now);
const timeFont = large.measureText(timeText) <= textWidth ? large : medium;
let y = PAD;
@@ -116,7 +141,8 @@ export class SystemCardWidget implements GlanceWidget {
// Notification icons along the bottom, as many as fit; the Timers app's
// countdowns take the band between the date and the icons.
const iconY = image.height - PAD - NOTIFICATION_ICON_SIZE;
drawTimers(image, small, PAD, y, textWidth, iconY - 4, now.getTime());
const timersWidth = Math.max(0, textRight(y, iconY - 4) - BATTERY_ITEM_GAP - PAD);
drawTimers(image, small, PAD, y, timersWidth, iconY - 4, now.getTime());
const maxIcons = Math.max(0, ((image.width - 2 * PAD + NOTIFICATION_ICON_GAP) / (NOTIFICATION_ICON_SIZE + NOTIFICATION_ICON_GAP)) | 0);
if (maxIcons > 0) {
const { icons, stale } = readActiveNotificationIcons(maxIcons, renderPassAllowsStaleData());
@@ -130,19 +156,25 @@ export class SystemCardWidget implements GlanceWidget {
function collectBatteryItems(): BatteryItem[] {
const items: BatteryItem[] = [];
const push = (visibility: BatteryIndicatorVisibility, label: string, percent: number | null, charging: boolean | null) => {
const push = (
visibility: BatteryIndicatorVisibility,
label: string,
device: BatteryDevice,
percent: number | null,
charging: boolean | null,
) => {
if (percent === null || !Number.isFinite(percent)) return;
const clamped = Math.max(0, Math.min(100, Math.round(percent)));
if (!batteryIndicatorVisible(visibility, clamped)) return;
items.push({ label, percent: clamped, charging: Boolean(charging) });
items.push({ label, device, percent: clamped, charging: Boolean(charging) });
};
const phone = readPhoneBatteryState();
push(phoneBatteryVisibilitySetting.get(), "Phone", phone.battery, phone.charging);
push(phoneBatteryVisibilitySetting.get(), "Phone", "phone", phone.battery, phone.charging);
const levels = shell.getBatteryLevels();
push(watchBatteryVisibilitySetting.get(), "Watch", levels.watch, levels.watchCharging);
push(glassesBatteryVisibilitySetting.get(), "G2", levels.headset, levels.headsetCharging);
push(watchBatteryVisibilitySetting.get(), "Watch", "watch", levels.watch, levels.watchCharging);
push(glassesBatteryVisibilitySetting.get(), "G2", "glasses", levels.headset, levels.headsetCharging);
if (levels.ring !== null && Number.isInteger(levels.ring) && levels.ring >= 0 && levels.ring <= 100) {
push(ringBatteryVisibilitySetting.get(), "R1", levels.ring, levels.ringCharging);
push(ringBatteryVisibilitySetting.get(), "R1", "ring", levels.ring, levels.ringCharging);
}
return items;
}
@@ -150,16 +182,22 @@ function collectBatteryItems(): BatteryItem[] {
/**
* The battery indicators right-aligned at `right`, in the configured style.
* Side-by-side styles put the label beside the gauge (or percentage) on one
* line; stacked styles centre the label above it. Returns the block's left
* edge (or `right` when nothing is shown).
* line; stacked styles centre the label above it; the dense style swaps
* labels for device icons and stacks two indicators per column, as in the
* top bar. Returns the block's extent (zero-height at `right` when nothing
* is shown).
*/
function drawBatteryBlock(image: GrayImage, font: UiFont, right: number, top: number): number {
function drawBatteryBlock(image: GrayImage, font: UiFont, right: number, top: number): RightBlock {
const items = collectBatteryItems();
if (!items.length) return right;
if (!items.length) return { left: right, top, bottom: top };
const mode = batteryDisplayModeSetting.get();
if (mode === "dense") {
return { left: drawDenseBatteries(image, items, right, top), top, bottom: top + DENSE_BATTERY_BLOCK_HEIGHT };
}
const percentage = mode === "percentage" || mode === "stacked-percentage";
const stacked = mode === "stacked" || mode === "stacked-percentage";
const gaugeHeight = drawBattery(0, false).height;
const valueHeight = percentage ? font.lineHeight : Math.max(font.lineHeight, gaugeHeight);
let x = right;
for (let index = items.length - 1; index >= 0; index--) {
const item = items[index]!;
@@ -178,7 +216,23 @@ function drawBatteryBlock(image: GrayImage, font: UiFont, right: number, top: nu
}
x -= BATTERY_ITEM_GAP;
}
return x + BATTERY_ITEM_GAP;
return { left: x + BATTERY_ITEM_GAP, top, bottom: top + (stacked ? font.lineHeight + 2 : 0) + valueHeight };
}
/**
* The current weather's condition icon and temperature, right-aligned at
* `right` from `top`, while the system-card weather setting is on and a
* fresh reading exists. Returns the block's extent, or null when not drawn.
*/
function drawWeatherBlock(image: GrayImage, font: UiFont, right: number, top: number, nowMs: number): RightBlock | null {
if (!weatherShowOnSystemCardSetting.get()) return null;
const reading = weatherReading(weatherBridge.snapshot(), nowMs);
if (!reading) return null;
const iconSize = font.lineHeight + 4;
const height = Math.max(iconSize, font.lineHeight);
const left = right - Math.ceil(measureWeatherReading(reading, font, iconSize));
drawWeatherReading(image, reading, font, iconSize, left, top, height, 200);
return { left, top, bottom: top + height };
}
function drawBatteryValue(
@@ -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);
}
@@ -1,6 +1,5 @@
import { type GrayImage } from "../../../graphics/image";
import { truncateText } from "../../../graphics/textwrap";
import { getDefaultSmallFont } from "../../../graphics/ui-fonts";
import { lineStep } from "../../../ui/metrics";
import { onWorkerStateChanged, readWorkerState } from "../../../ui/shell/worker-state";
import {
@@ -10,6 +9,7 @@ import {
TERMINAL_SESSIONS_STATE_KEY,
type TerminalSessionsSnapshot,
} from "../../terminal/session-list";
import { glanceFont } from "../glance-font";
import { type GlanceWidget } from "../widget";
const PAD = 8;
@@ -26,7 +26,7 @@ const BOTTOM_MARGIN = 2;
* once the snapshot is in.
*/
export class TerminalWidget implements GlanceWidget {
private snapshot: TerminalSessionsSnapshot | undefined = readWorkerState(TERMINAL_SESSIONS_STATE_KEY);
private snapshot = readWorkerState(TERMINAL_SESSIONS_STATE_KEY) as TerminalSessionsSnapshot | undefined;
private unsubscribe: (() => void) | null = null;
private requestRender: (() => void) | null = null;
private phase = 0;
@@ -34,9 +34,9 @@ export class TerminalWidget implements GlanceWidget {
start(requestRender: () => void): void {
this.requestRender = requestRender;
this.snapshot = readWorkerState(TERMINAL_SESSIONS_STATE_KEY);
this.unsubscribe = onWorkerStateChanged<TerminalSessionsSnapshot>(TERMINAL_SESSIONS_STATE_KEY, (snapshot) => {
this.snapshot = snapshot;
this.snapshot = readWorkerState(TERMINAL_SESSIONS_STATE_KEY) as TerminalSessionsSnapshot | undefined;
this.unsubscribe = onWorkerStateChanged(TERMINAL_SESSIONS_STATE_KEY, (snapshot) => {
this.snapshot = snapshot as TerminalSessionsSnapshot;
this.syncAnimation();
requestRender();
});
@@ -72,7 +72,7 @@ export class TerminalWidget implements GlanceWidget {
}
paint(image: GrayImage): void {
const font = getDefaultSmallFont();
const font = glanceFont.small();
const step = lineStep(font);
const textWidth = image.width - 2 * PAD;
const snapshot = this.snapshot;
+10 -5
View File
@@ -1,11 +1,10 @@
import { type BdfFont } from "../../graphics/bdffont";
import { getDefaultSmallFont } from "../../graphics/ui-fonts";
import { truncateText } from "../../graphics/textwrap";
import { GrayImage } from "../../graphics/image";
import { type Plane } from "../../graphics/plane";
import { renderIcon, type IconName } from "../../graphics/icons";
import { InputEvent } from "../../ui/gestures";
import { IconGrid, ICON_GRID_ICON_SIZE } from "../../ui/icon-grid";
import { IconGrid, ICON_GRID_BAND_INSET_X, ICON_GRID_HIGHLIGHT_INSET_Y, ICON_GRID_ICON_SIZE } from "../../ui/icon-grid";
import { Layer, LayerActions, LayerContext } from "../../ui/layers";
import { MenuLayer, type MenuItem } from "../../ui/menu";
import { WINDOW_MENU_LAYOUT } from "../../ui/window-menu";
@@ -45,6 +44,11 @@ export const LAUNCHER_WINDOW_ID = "launcher";
export const LAUNCHER_SURFACE_ID = "window:launcher";
const GRID_TOP = 6;
/**
* The root grid's top: its first row's highlight band then sits as far
* below the window's top edge as it is from the side edges.
*/
const ROOT_GRID_TOP = ICON_GRID_BAND_INSET_X - ICON_GRID_HIGHLIGHT_INSET_Y;
/** One cell of the grid: an app, or a folder holding some of the apps. */
type LauncherGridEntry =
@@ -259,12 +263,13 @@ class LauncherGridLayer implements Layer {
const { width, height } = ctx.stack.getBaseSize();
const image = new GrayImage(width, height, 0);
// The folder-name header band scales with the font.
const gridTop = this.currentFolder !== null ? GRID_TOP + font.lineHeight + 4 : GRID_TOP;
const gridTop = this.currentFolder !== null ? GRID_TOP + font.lineHeight + 4 : ROOT_GRID_TOP;
if (this.currentFolder !== null) {
image.drawText(font, 8, GRID_TOP - 2, truncateText(font, this.currentFolder, width - 16), 160);
}
const gridBottom = height - 4;
this.grid.paint(image, { x: 0, y: gridTop, width, height: gridBottom - gridTop }, ctx.stack.isFocused());
// The grid runs to the window's bottom edge, so a row cut off there for
// scrolling meets the window's border rather than a blank strip.
this.grid.paint(image, { x: 0, y: gridTop, width, height: height - gridTop }, ctx.stack.isFocused());
return image;
}
+3 -3
View File
@@ -57,7 +57,7 @@ export function loadMicConfig(): MicConfig {
return {
...defaults,
...parsed,
hostMics: { ...defaults.hostMics, ...(parsed.hostMics ?? {}) },
hostMics: { ...defaults.hostMics, ...parsed.hostMics },
};
} catch {
return defaultMicConfig();
@@ -72,8 +72,8 @@ class MicArrayController {
private readonly statusListeners = new Set<(statuses: MicArrayStatuses) => void>();
private statuses: MicArrayStatuses = { left: null, right: null };
// The Java listener proxy must stay referenced or it gets GC'd.
private statusProxy: any | null = null;
private proxyCommunicator: any | null = null;
private statusProxy: any = null;
private proxyCommunicator: any = null;
private leaseTimer: ReturnType<typeof setInterval> | null = null;
private armed = false;
+68 -12
View File
@@ -12,8 +12,8 @@
*
* Per-temple: each arm is its own endpoint and its own 2-mic array (front
* mic near the hinge = chanMask bit0, rear mic near the touchpad = bit1);
* the same config goes to both temples. Stereo payloads arrive as two
* CONCATENATED channel blocks (front then rear), not interleaved. Sessions
* the same config goes to both temples. Stereo payloads are INTERLEAVED PCM16
* (ch0, ch1, ch0, ch1, ...), the capture dispatch buffer verbatim. Sessions
* hold a 90 s lease; renew with op RENEW while streaming.
*
* Pure module: byte codecs only, no platform imports (the BLE plumbing lives
@@ -54,7 +54,7 @@ export type MicConfig = {
/**
* Host-side per-microphone selection. Each temple streams its full
* front+rear stereo pair over BLE (wire channelMask stays 0b11); the phone
* splits the concatenated channels and this mask decides which of the four
* deinterleaves the channels and this mask decides which of the four
* mics feed levels-for-use, beamforming, captions, and recording. A temple
* with both mics off is sent STOP so its stream doesn't waste bandwidth.
*/
@@ -168,8 +168,42 @@ export type MicStatus = {
framesEmitted: number;
/** The rate actually in effect (16 kHz on current firmware). */
effectiveRateHz: number;
/** Tap diagnostics; absent from firmware that sends only the 21-byte body. */
diagnostics: MicStatusDiagnostics | null;
};
/** Result of a firmware call the temple has not made yet. */
export const MIC_RET_NONE = 127;
/** Occupant of one stock PCM app slot. */
export type MicSlotState = "empty" | "other" | "tap";
/**
* Counters from the status extension; they reset on each CONFIGURE. The
* firmware debug overlay shows the same values on each lens.
*/
export type MicStatusDiagnostics = {
/** Calls into the PCM tap (any outcome). */
tapCalls: number;
tapLastBytes: number;
tapLastSlot: number;
/** Tap calls dropped because the session was inactive or unarmed. */
skipIdle: number;
/** Tap calls dropped because the streaming lease had lapsed. */
skipLease: number;
/** Tap calls dropped because the frame buffer allocation failed. */
skipAlloc: number;
/** Signed firmware results, or MIC_RET_NONE when not called yet. */
registerResult: number;
unregisterResult: number;
notifyResult: number;
/** Stock PCM slot table, read live: [slot 0, slot 1]. */
slots: [MicSlotState, MicSlotState];
leaseRemainingS: number;
};
const MIC_STATUS_DIAGNOSTICS_BYTES = 40;
function le16(data: Uint8Array, offset: number): number {
return data[offset]! | (data[offset + 1]! << 8);
}
@@ -210,6 +244,27 @@ export function decodeMicStatus(body: Uint8Array): MicStatus | null {
side: body[14] === 2 ? "left" : "right",
framesEmitted: le32(body, 15),
effectiveRateHz: le16(body, 19) * 100,
diagnostics: body.length >= MIC_STATUS_DIAGNOSTICS_BYTES ? decodeMicDiagnostics(body) : null,
};
}
const SLOT_STATES: MicSlotState[] = ["empty", "other", "tap", "other"];
function decodeMicDiagnostics(body: Uint8Array): MicStatusDiagnostics {
const s8 = (offset: number) => (body[offset]! >= 0x80 ? body[offset]! - 0x100 : body[offset]!);
const table = body[37]!;
return {
tapCalls: le32(body, 21),
tapLastBytes: le16(body, 25),
tapLastSlot: body[27]!,
skipIdle: le16(body, 28),
skipLease: le16(body, 30),
skipAlloc: le16(body, 32),
registerResult: s8(34),
unregisterResult: s8(35),
notifyResult: s8(36),
slots: [SLOT_STATES[table & 3]!, SLOT_STATES[(table >> 2) & 3]!],
leaseRemainingS: le16(body, 38),
};
}
@@ -260,25 +315,26 @@ export function decodeMicStreamFrame(data: Uint8Array): MicStreamFrame | null {
/**
* Split a raw PCM16 SM payload into per-channel Float32Arrays in [-1, 1].
* Channels arrive as CONCATENATED blocks (front then rear), matching the
* firmware's stereo callback, which dispatches two back-to-back extraction
* results rather than interleaving.
* Channels arrive INTERLEAVED: the firmware forwards the stock capture
* dispatch buffer, which the stock algo reads as ch0 = pcm[2i], ch1 =
* pcm[2i+1] (in samples). Channel 0 is assumed to be the front mic; that
* mapping is not yet confirmed on hardware.
*/
export function splitConcatenatedPcm16(frame: MicStreamFrame): Float32Array[] | null {
export function splitInterleavedPcm16(frame: MicStreamFrame): Float32Array[] | null {
if (frame.codec !== "raw" || frame.format !== "pcm16" || frame.channelCount < 1) {
return null;
}
const channels = frame.channelCount;
const bytesPerChannel = Math.floor(frame.payload.length / channels) & ~1;
const samplesPerChannel = bytesPerChannel / 2;
const frameBytes = channels * 2;
const samplesPerChannel = Math.floor(frame.payload.length / frameBytes);
if (samplesPerChannel <= 0) return null;
const out: Float32Array[] = [];
for (let channel = 0; channel < channels; channel++) {
const base = channel * bytesPerChannel;
const samples = new Float32Array(samplesPerChannel);
for (let index = 0; index < samplesPerChannel; index++) {
const lo = frame.payload[base + index * 2]!;
const hi = frame.payload[base + index * 2 + 1]!;
const offset = index * frameBytes + channel * 2;
const lo = frame.payload[offset]!;
const hi = frame.payload[offset + 1]!;
let value = lo | (hi << 8);
if (value >= 0x8000) value -= 0x10000;
samples[index] = value / 32768;
+8 -8
View File
@@ -16,7 +16,6 @@ import {
type HeadOrientation,
} from "./dsp";
import { micArrayController, micControlSupported, loadMicConfig, saveMicConfig } from "./mic-control";
import type { MicConfig } from "./mic-protocol";
import { micModelPath } from "./mic-models";
import {
MIC_CHANNEL_KEYS,
@@ -24,9 +23,10 @@ import {
isStockAudioPacket,
micChannelKey,
micChannelLabel,
splitConcatenatedPcm16,
splitInterleavedPcm16,
templeActive,
type MicChannelKey,
type MicConfig,
type MicSide,
} from "./mic-protocol";
import {
@@ -158,8 +158,8 @@ class MicSession {
// Captions state.
private captionLines: CaptionLine[] = [];
private captionEngine: any | null = null;
private captionListenerProxy: any | null = null;
private captionEngine: any = null;
private captionListenerProxy: any = null;
private captionsActive = false;
private engineStartWallMs = 0;
private sessionRowId = -1;
@@ -168,7 +168,7 @@ class MicSession {
private conversationLang = "und";
// Recording state.
private recorder: any | null = null;
private recorder: any = null;
private recordingPath = "";
// Stock path subscriptions.
@@ -177,7 +177,7 @@ class MicSession {
private offCompass: (() => void) | null = null;
// Extended path.
private forwardingProxy: any | null = null;
private forwardingProxy: any = null;
private temples = new Map<MicSide, TempleAudioPipeline>();
private templeAngles: { left: number | null; right: number | null } = { left: null, right: null };
private templeSsr: { left: number; right: number } = { left: 0, right: 0 };
@@ -193,7 +193,7 @@ class MicSession {
private offVoiceActivity: (() => void) | null = null;
// Phone-side spectral noise suppressor (FaceclawNoiseSuppressor).
private suppressor: any | null = null;
private suppressor: any = null;
private suppressorEngine = "";
// True between the caption engine's speech-start event and the utterance
@@ -586,7 +586,7 @@ class MicSession {
return;
}
const side: MicSide = arm === "L" ? "left" : "right";
const channels = splitConcatenatedPcm16(frame);
const channels = splitInterleavedPcm16(frame);
if (!channels || channels.length === 0) return;
// Meter every arriving channel — a host-disabled mic still shows its
+2 -16
View File
@@ -1,14 +1,13 @@
import { imageFromAsciiArt } from "../../graphics/image";
import { getDefaultSmallFont } from "../../graphics/ui-fonts";
import { appViewportSize } from "../../ui/shell/geometry";
import { LIST_ROW_TEXT_INSET } from "../../ui/metrics";
import {
MenuLayer,
TextPageLayer,
drawRightValueMenuItem,
drawSubmenuIndicator,
drawToggleMenuItem,
openModalMenu,
submenuItem,
type MenuItem,
} from "../../ui/menu";
import { enumSettingMenuItem, toggleSettingMenuItem } from "../../ui/dashboard-settings";
@@ -60,7 +59,7 @@ const TRAY_ICON = imageFromAsciiArt(
" # # ",
" ### ### ",
],
220,
{ "#": 220 },
);
const MENU_LAYOUT = {
@@ -73,19 +72,6 @@ const MENU_LAYOUT = {
opaque: true,
};
function submenuItem(label: string, onSelect: MenuItem["onSelect"]): MenuItem {
return {
label,
onSelect,
render: ({ image, x, y, width, height, selected, disabled, text }) => {
const font = getDefaultSmallFont();
const value = disabled ? 70 : selected ? 255 : 200;
image.drawText(font, x, y + LIST_ROW_TEXT_INSET, text, value);
drawSubmenuIndicator(image, font, x, y, width, height, value);
},
};
}
/** Persist a config change and re-apply it live when the array is streaming. */
function updateMicConfig(mutate: (config: MicConfig) => MicConfig): void {
const updated = mutate(loadMicConfig());
+2 -2
View File
@@ -8,7 +8,7 @@ declare const java: any;
* translation to the phone's default language beneath it.
*/
let languageIdentifier: any | null = null;
let languageIdentifier: any = null;
// Translators are expensive to build (model download on first use); cache by
// "src>dst" and keep the proxies referenced so they aren't GC'd mid-flight.
const translators = new Map<string, any>();
@@ -59,7 +59,7 @@ export function identifyLanguage(text: string): Promise<string> {
});
}
function translatorFor(sourceLang: string, targetLang: string): any | null {
function translatorFor(sourceLang: string, targetLang: string): any {
const key = `${sourceLang}>${targetLang}`;
const cached = translators.get(key);
if (cached) return cached;
@@ -250,6 +250,11 @@ global.onmessage = (event: { data: WorkerAppMessage }) => {
syncClock(window);
}
break;
case "navigation-sensors":
case "resize-window":
case "text-input":
case "tool-call":
break;
}
};
@@ -350,7 +355,7 @@ function handleInput(window: MinesweeperWindow, event: InputEvent, frameId: numb
// An open window menu owns all input (it closes itself via pop); menus are
// list UIs, so watch swipes take their standard fallback meanings there.
if (window.menu?.isOpen()) {
window.menu
void window.menu
.handleInput(directionalFallback(event))
.catch((error) => console.error(`minesweeper menu input failed: ${error}`))
.then(() => renderAndSubmit(window, frameId));
+80 -26
View File
@@ -1,5 +1,5 @@
import { getDefaultMediumFont, getDefaultSmallFont } from "../../graphics/ui-fonts";
import { GrayImage } from "../../graphics/image";
import { GrayImage, type UiFont } from "../../graphics/image";
import { wrapText, truncateText } from "../../graphics/textwrap";
import { clamp } from "../../util/numeric-util";
import { directionalFallback, GESTURE_CLICK, GESTURE_DOUBLE_CLICK, GESTURE_SCROLL_DOWN, GESTURE_SCROLL_UP, type InputEvent, isDirectionalInput } from "../../ui/gestures";
@@ -20,8 +20,9 @@ const ART_SIZE = 112;
const ART_X = 8; // left margin matches the top margin
const ART_Y = 8;
const META_X = ART_X + ART_SIZE + 14;
const PROGRESS_TIME_Y = ART_Y + 100;
const PROGRESS_BAR_Y = ART_Y + 116;
/** Clear rows between the time labels' baseline (digits have no descenders) and the bar. */
const PROGRESS_TIME_GAP = 3;
const LIST_TOP = 136;
/** Gap between the menus' last row band and the viewport bottom. */
const LIST_BOTTOM_MARGIN = 4;
@@ -47,6 +48,8 @@ type FocusColumn = "actions" | "playlist";
* Music controller app: metadata + album art + transport controls for the
* active Android media session (any player that publishes one), plus the
* player's queue when it exposes one (scroll to a track, click to jump).
* With no session, the front page lists the apps whose library can be
* browsed (click one to open its MediaBrowseLayer).
*/
class MusicAppLayer implements Layer {
// Watch swipes skip tracks: right = next, left = previous.
@@ -66,6 +69,19 @@ class MusicAppLayer implements Layer {
getHeight: () => tightRowHeight(getDefaultSmallFont()),
draw: (args) => this.drawQueueItem(args),
});
/** The no-session front page's picker of apps whose library can be browsed. */
private readonly libraryMenu = new Menu<MediaBrowserApp>({
wrap: false,
rowGap: 1,
highlight: { radius: 4 },
getHeight: () => tightRowHeight(getDefaultSmallFont()),
draw: (args) => this.drawLibraryApp(args),
});
/**
* Whether the browsable-app list has been re-queried since the no-session
* page last appeared (apps installed since the cached query then show up).
*/
private libraryAppsFresh = false;
private art: GrayImage | null = null;
private artKey = "";
@@ -93,16 +109,31 @@ class MusicAppLayer implements Layer {
}
if (!media.available) {
const apps = mediaBrowserBridge.listBrowsableApps(!this.libraryAppsFresh);
this.libraryAppsFresh = true;
this.libraryMenu.setItems(apps);
image.drawText(font, 24, 16, "No active media session.", 180);
if (mediaBrowserBridge.listBrowsableApps().length) {
image.drawText(font, 24, 22 + lineStep(font), "Click to browse a music app's library,", 150);
image.drawText(font, 24, 22 + 2 * lineStep(font), "or start playback on the phone.", 150);
image.drawText(font, 20, height - 16, `${GESTURE_CLICK} browse`, 110);
} else {
image.drawText(font, 24, 22 + lineStep(font), "Start playback in another app on the phone.", 150);
const prompt = apps.length
? "Browse a music app's library, or start playback on the phone."
: "Start playback in another app on the phone.";
let y = 22 + lineStep(font);
for (const line of wrapText(font, prompt, width - 48)) {
image.drawText(font, 24, y, line, 150);
y += lineStep(font);
}
if (apps.length) {
const listTop = y + 6;
const listHeight = height - listTop - LIST_BOTTOM_MARGIN;
this.libraryMenu.paint(
image,
{ x: 24 - LIST_TEXT_INSET, y: listTop - 1, width: width - 48 + 2 * LIST_TEXT_INSET - 4, height: listHeight },
ctx.stack.isFocused(),
);
this.libraryMenu.drawScrollbar(image, width - 12, listTop, listHeight - 4);
}
return image;
}
this.libraryAppsFresh = false;
this.drawArt(image, media);
@@ -113,7 +144,7 @@ class MusicAppLayer implements Layer {
// (title, second title line, artist, album, player app); when the font is
// too large for all five, the title gets one truncated line instead of
// wrapping to two, and the rest shift up a slot.
const maxLines = Math.floor((PROGRESS_TIME_Y - metaTop - 4 - font.lineHeight) / metaStep) + 1;
const maxLines = Math.floor((progressTimeY(font) - metaTop - 4 - font.lineHeight) / metaStep) + 1;
const titleSlots = maxLines >= 5 ? 2 : 1;
const title = media.title || "Unknown title";
const titleLines =
@@ -171,6 +202,13 @@ class MusicAppLayer implements Layer {
}
}
private drawLibraryApp({ image, item: app, x, y, width, height, selected }: MenuDrawArgs<MediaBrowserApp>): void {
const font = getDefaultSmallFont();
const value = selected ? 255 : 200;
image.drawText(font, x + LIST_TEXT_INSET, centeredTextY(font, y, height), truncateText(font, app.appName, width - 2 * LIST_TEXT_INSET - 16), value);
drawSubmenuIndicator(image, font, x, y, width, height, value);
}
private drawQueueItem({ image, item, x, y, width, height, selected }: MenuDrawArgs<MediaQueueItem>): void {
const font = getDefaultSmallFont();
const label = `${item.active ? "> " : " "}${item.title || "(untitled)"}`;
@@ -197,8 +235,12 @@ class MusicAppLayer implements Layer {
return;
}
if (!media.available) {
if (event.type === "click") {
this.openBrowse(ctx);
this.libraryMenu.setItems(mediaBrowserBridge.listBrowsableApps());
if (event.type === "scroll-up" || event.type === "scroll-down") {
await this.libraryMenu.handleInput(event);
} else if (event.type === "click") {
const app = this.libraryMenu.selectedItem;
if (app) this.pushLibraryBrowser(ctx, app);
}
return;
}
@@ -252,23 +294,15 @@ class MusicAppLayer implements Layer {
/**
* Open the media-library browser (the Android Auto MediaBrowserService
* path): with one browsable player installed go straight to its library,
* otherwise offer a picker first.
* path) from the now-playing page: with one browsable player installed go
* straight to its library, otherwise offer a picker first. (With no session
* the picker is the front page itself.)
*/
private openBrowse(ctx: LayerContext): void {
const apps = mediaBrowserBridge.listBrowsableApps(true);
if (!apps.length) return;
const pushBrowser = (target: LayerContext, app: MediaBrowserApp) => {
target.stack.push(
new MediaBrowseLayer({
app,
onPlayed: (browseCtx) => browseCtx.stack.pop(),
onLeave: (browseCtx) => browseCtx.stack.pop(),
}),
);
};
if (apps.length === 1) {
pushBrowser(ctx, apps[0]!);
this.pushLibraryBrowser(ctx, apps[0]!);
return;
}
ctx.stack.push(
@@ -278,13 +312,23 @@ class MusicAppLayer implements Layer {
label: app.appName,
onSelect: (menuCtx: LayerContext) => {
menuCtx.stack.pop();
pushBrowser(menuCtx, app);
this.pushLibraryBrowser(menuCtx, app);
},
})),
),
);
}
private pushLibraryBrowser(ctx: LayerContext, app: MediaBrowserApp): void {
ctx.stack.push(
new MediaBrowseLayer({
app,
onPlayed: (browseCtx) => browseCtx.stack.pop(),
onLeave: (browseCtx) => browseCtx.stack.pop(),
}),
);
}
private buildActions(media: MediaControllerState, queue: MediaQueueItem[]): MusicAction[] {
const volume = mediaControllerBridge.getMediaVolumePercent();
return [
@@ -316,8 +360,9 @@ class MusicAppLayer implements Layer {
const font = getDefaultSmallFont();
const elapsed = formatMediaTime(media.positionMs);
const duration = formatMediaTime(media.durationMs);
image.drawText(font, META_X, PROGRESS_TIME_Y, elapsed, 140);
image.drawText(font, META_X + width - font.measureText(duration), PROGRESS_TIME_Y, duration, 140);
const timeY = progressTimeY(font);
image.drawText(font, META_X, timeY, elapsed, 140);
image.drawText(font, META_X + width - font.measureText(duration), timeY, duration, 140);
const barY = PROGRESS_BAR_Y;
image.drawRect(META_X, barY, width, 5, 55);
@@ -481,6 +526,15 @@ export function createMusicAppWindow(options: InProcessAppOptions): InProcessWin
return app;
}
/**
* Line top of the elapsed/duration labels: their baseline sits a fixed gap
* above the progress bar, so the labels hug the bar whatever the font's
* height (a taller face grows upward into the metadata's line budget).
*/
function progressTimeY(font: UiFont): number {
return PROGRESS_BAR_Y - PROGRESS_TIME_GAP - font.ascent;
}
function formatMediaTime(milliseconds: number): string {
const totalSeconds = Math.max(0, Math.floor(milliseconds / 1_000));
const seconds = totalSeconds % 60;
+1 -1
View File
@@ -1,6 +1,6 @@
/**
* Saved and recent destinations for the Navigate app: Home and Work (plain
* address settings, also editable in the Settings app), custom named
* address settings), custom named
* destinations, and a recency list of resolved places. Pure settings-store
* accessors, usable from the worker and the main thread alike.
*/
+38 -36
View File
@@ -19,6 +19,7 @@ import {
drawRightValueMenuItem,
drawSubmenuIndicator,
drawToggleMenuItem,
submenuItem,
type MenuItem,
} from "../../ui/menu";
import { Menu, type MenuDrawArgs } from "../../ui/menu-core";
@@ -63,7 +64,16 @@ import {
GESTURE_SHORT_THEN_LONG_PRESS,
type InputEvent,
} from "../../ui/gestures";
import { LocationTracker, type TrackedLocation } from "./navigation-sensors";
import {
addCompassListener,
COMPASS_CHANGED,
handleNavigationSensorEvent,
LocationTracker,
magneticDeclinationDegrees,
setCompassEnabled,
type CompassEvent,
type TrackedLocation,
} from "./navigation-sensors";
import {
fetchRoute,
fetchStaticMapGray,
@@ -75,14 +85,11 @@ import {
type RouteProfile,
type StaticMapCamera,
} from "../../native/mapbox";
import { addCompassListener, COMPASS_CHANGED, setCompassEnabled, type CompassEvent } from "./navigation-sensors";
import { magneticDeclinationDegrees, handleNavigationSensorEvent } from "./navigation-sensors";
import { calibrateHeading, normalizeHeading } from "../compass/calibration";
import { bearingDegrees, haversineMeters, RouteFollower, type RouteProgress } from "./route-follower";
import { drawManeuverGlyph } from "./maneuver-icons";
declare const global: any;
declare const com: any;
const MAP_SIZE = 260;
const PANEL_X = MAP_SIZE + 10;
@@ -256,8 +263,8 @@ function post(message: WorkerAppReply): void {
global.postMessage(message);
}
// Destinations edited on the phone (the text editor, or the Settings app's
// Home/Work rows) repaint the idle list / the edit screen live.
// Destinations edited on the phone (the text editor) repaint the idle list /
// the edit screen live.
onSettingsStoreChanged((key) => {
// The token key repaints too: the phone editor writes it live during
// Edit token, and the idle page changes shape once one is set.
@@ -376,6 +383,8 @@ global.onmessage = (event: { data: WorkerAppMessage }) => {
);
break;
}
case "input-focus":
break;
}
};
@@ -914,7 +923,7 @@ function describeRouteStatus(): string {
* display and saved-destination settings (Home, Work, custom named places), and the
* recent-destinations preference.
*/
function windowMenuItems(win: NavWindow): MenuItem[] {
function windowMenuItems(): MenuItem[] {
const items: MenuItem[] = [];
if (phase !== "idle") {
items.push({
@@ -930,16 +939,9 @@ function windowMenuItems(win: NavWindow): MenuItem[] {
enumSettingMenuItem(navigateDisplayModeSetting),
enumSettingMenuItem(navigateVerticalPositionSetting),
);
items.push({
label: "Saved destinations",
onSelect: (ctx) => {
ctx.stack.push(new WindowMenuLayer("Saved destinations", savedDestinationMenuItems()));
},
render: ({ image, x, y, width, height, text }) => {
image.drawText(smallFont, x, y + LIST_ROW_TEXT_INSET, text, 200);
drawSubmenuIndicator(image, smallFont, x - 10, y, width + 20, height, 150);
},
});
items.push(submenuItem("Saved destinations", (ctx) => {
ctx.stack.push(new WindowMenuLayer("Saved destinations", savedDestinationMenuItems()));
}));
items.push({
label: navigateRememberRecentSetting.label,
onSelect: () => {
@@ -993,15 +995,15 @@ function savedDestinationMenuItems(): MenuItem[] {
const work = saved.find((d) => d.id === WORK_DESTINATION_ID);
items.push(addressMenuItem("Home", HOME_DESTINATION_ID, home?.address ?? ""));
items.push(addressMenuItem("Work", WORK_DESTINATION_ID, work?.address ?? ""));
for (const destination of saved) {
if (destination.builtin) continue;
for (const dest of saved) {
if (dest.builtin) continue;
items.push({
label: destination.name,
label: dest.name,
onSelect: (ctx) => {
ctx.stack.push(new WindowMenuLayer(destination.name, customDestinationMenuItems(destination)));
ctx.stack.push(new WindowMenuLayer(dest.name, customDestinationMenuItems(dest)));
},
render: ({ image, x, y, width, height }) => {
drawAddressRow(image, x, y, width, destination.name, destination.address, true);
drawAddressRow(image, x, y, width, dest.name, dest.address, true);
drawSubmenuIndicator(image, smallFont, x - 10, y, width + 20, height, 150);
},
});
@@ -1028,14 +1030,14 @@ function addressMenuItem(label: string, id: string, address: string): MenuItem {
};
}
function customDestinationMenuItems(destination: SavedDestination): MenuItem[] {
function customDestinationMenuItems(dest: SavedDestination): MenuItem[] {
return [
{
label: "Navigate there",
disabled: !destination.address,
disabled: !dest.address,
onSelect: (ctx) => {
closeMenusAnd(ctx, () => {
void startNavigationTo({ kind: "query", query: destination.address, label: destination.name }, profile).catch(
void startNavigationTo({ kind: "query", query: dest.address, label: dest.name }, profile).catch(
() => {},
);
});
@@ -1044,16 +1046,16 @@ function customDestinationMenuItems(destination: SavedDestination): MenuItem[] {
{
label: "Set address",
onSelect: (ctx) => {
closeMenusAnd(ctx, () => beginAddressEdit(destination.id, destination.name, destination.address));
closeMenusAnd(ctx, () => beginAddressEdit(dest.id, dest.name, dest.address));
},
},
{
label: "Rename",
onSelect: (ctx) => {
closeMenusAnd(ctx, () => {
navigateDestinationNameDraftSetting.set(destination.name);
beginEdit(navigateDestinationNameDraftSetting, `Rename ${destination.name}`, (name) => {
if (name) updateCustomDestination(destination.id, { name });
navigateDestinationNameDraftSetting.set(dest.name);
beginEdit(navigateDestinationNameDraftSetting, `Rename ${dest.name}`, (name) => {
if (name) updateCustomDestination(dest.id, { name });
});
});
},
@@ -1062,7 +1064,7 @@ function customDestinationMenuItems(destination: SavedDestination): MenuItem[] {
label: "Remove",
onSelect: (ctx) => {
closeMenusAnd(ctx, () => {
removeCustomDestination(destination.id);
removeCustomDestination(dest.id);
syncIdleList();
});
},
@@ -1195,10 +1197,10 @@ function idleEntries(): IdleEntry[] {
run: () => { void startMap(); },
}];
entries.push(...loadSavedDestinations()
.filter((destination) => destination.address)
.map((destination): IdleEntry => ({ kind: "saved", destination })));
for (const destination of loadRecentDestinations()) {
entries.push({ kind: "recent", destination });
.filter((dest) => dest.address)
.map((dest): IdleEntry => ({ kind: "saved", destination: dest })));
for (const dest of loadRecentDestinations()) {
entries.push({ kind: "recent", destination: dest });
}
return entries;
}
@@ -1250,7 +1252,7 @@ function windowMenu(win: NavWindow): WindowMenu {
windowId: win.windowId,
post,
title: () => win.title,
items: () => windowMenuItems(win),
items: () => windowMenuItems(),
size: { width: win.viewportWidth, height: win.viewportHeight },
paintBase: () => paintContent(win),
isFocused: () => win.focused,
@@ -1261,7 +1263,7 @@ function windowMenu(win: NavWindow): WindowMenu {
function handleInput(win: NavWindow, event: InputEvent, frameId: number): void {
if (win.menu?.isOpen()) {
win.menu
void win.menu
.handleInput(event)
.catch((error) => console.error(`navigate menu input failed: ${error}`))
.then(() => renderAndSubmit(win, frameId));
+2 -2
View File
@@ -46,7 +46,7 @@ export class RouteFollower {
constructor(route: Route) {
this.route = route;
this.vertexMeters = new Array(route.coordinates.length);
this.vertexMeters = Array.from({ length: route.coordinates.length }, () => 0);
let along = 0;
this.vertexMeters[0] = 0;
for (let i = 1; i < route.coordinates.length; i++) {
@@ -58,7 +58,7 @@ export class RouteFollower {
// Each step's maneuver happens where the previous step's travel ends, so
// the maneuver point of step k sits at the sum of steps 0..k-1 distances.
// (Slight drift vs the geometry is fine at guidance granularity.)
this.stepStartMeters = new Array(route.steps.length);
this.stepStartMeters = Array.from({ length: route.steps.length }, () => 0);
let stepAlong = 0;
for (let i = 0; i < route.steps.length; i++) {
this.stepStartMeters[i] = Math.min(stepAlong, this.totalMeters);
+35 -7
View File
@@ -12,7 +12,8 @@
* keep the BLE payload down to the dirty region around the ball.
*
* Controls (ball ready): scroll sets launch power, click launches. In play:
* ring-press flips both flippers for 300 ms; click launches from the plunger,
* a touch-down (ring-press) flips both flippers for 300 ms, as does a click
* without one; click launches from the plunger,
* double-click pauses. Swipes no longer trigger individual flippers;
* up/down raise and lower launch power at the plunger.
* Paused/game over: click resumes or starts a new game, double-click yields
@@ -42,6 +43,7 @@ import {
GESTURE_DOUBLE_CLICK,
GESTURE_SCROLL,
type InputEvent,
PressTracker,
} from "../../ui/gestures";
import { clamp } from "../../util/numeric-util";
@@ -61,6 +63,12 @@ const TOP = 4;
const BOTTOM = 256;
const LANE_X = 232;
const PANEL_X = 310;
/**
* Height the table and panel are drawn for: a min-height window's viewport
* under the standard top bar. A taller viewport (the status bar folded into
* the switcher row, a full-panel display) centers the layout vertically.
*/
const LAYOUT_HEIGHT = 260;
const BALL_R = 6;
/** Ball resting spot on the plunger, and where launches start. */
@@ -273,6 +281,8 @@ type PinballWindow = {
lastMinorSfxAtMs: number;
soundOn: boolean;
lastSubmittedFingerprint: string;
/** Which gestures already flipped on their ring-press. */
presses: PressTracker;
};
const windows = new Map<string, PinballWindow>();
@@ -336,6 +346,7 @@ global.onmessage = (event: { data: WorkerAppMessage }) => {
lastMinorSfxAtMs: 0,
soundOn: loadSoundEnabled("pinball"),
lastSubmittedFingerprint: "",
presses: new PressTracker(),
};
windows.set(message.windowId, window);
break;
@@ -400,6 +411,11 @@ global.onmessage = (event: { data: WorkerAppMessage }) => {
syncTickTimer(window);
}
break;
case "navigation-sensors":
case "resize-window":
case "text-input":
case "tool-call":
break;
}
};
@@ -491,10 +507,11 @@ function windowMenu(window: PinballWindow): WindowMenu {
}
function handleInput(window: PinballWindow, event: InputEvent, frameId: number): void {
const followsPress = window.presses.followsPress(event);
// An open window menu owns all input (it closes itself via pop); menus are
// list UIs, so watch swipes take their standard fallback meanings there.
if (window.menu?.isOpen()) {
window.menu
void window.menu
.handleInput(directionalFallback(event))
.catch((error) => console.error(`pinball menu input failed: ${error}`))
.then(() => renderAndSubmit(window, frameId));
@@ -502,13 +519,14 @@ function handleInput(window: PinballWindow, event: InputEvent, frameId: number):
}
if (window.phase === "playing") {
handlePlayingInput(window, event, frameId);
handlePlayingInput(window, event, frameId, followsPress);
} else {
handleIdleInput(window, event, frameId);
}
}
function handlePlayingInput(window: PinballWindow, event: InputEvent, frameId: number): void {
/** `followsPress` marks a gesture whose touch-down already flipped (see PressTracker). */
function handlePlayingInput(window: PinballWindow, event: InputEvent, frameId: number, followsPress: boolean): void {
const ready = window.ballState === "ready";
switch (event.type) {
case "scroll-up":
@@ -539,7 +557,7 @@ function handlePlayingInput(window: PinballWindow, event: InputEvent, frameId: n
case "click":
if (ready) {
launchBall(window);
} else if (event.source !== "ring") {
} else if (!followsPress) {
flip(window, window.flippers[0]!);
flip(window, window.flippers[1]!);
}
@@ -737,6 +755,8 @@ function stepFlippers(window: PinballWindow, dt: number): void {
flipper.state = "rest";
}
break;
case "rest":
break;
}
}
}
@@ -880,8 +900,8 @@ let staticBackground: GrayImage | null = null;
/** Everything that never changes: walls, guides, slings, outlines, labels. */
function getStaticBackground(window: PinballWindow): GrayImage {
if (staticBackground) return staticBackground;
const image = new GrayImage(window.viewportWidth, window.viewportHeight, 0);
if (staticBackground?.width === window.viewportWidth) return staticBackground;
const image = new GrayImage(window.viewportWidth, LAYOUT_HEIGHT, 0);
for (const segment of SEGMENTS) {
image.drawLine(segment.x0, segment.y0, segment.x1, segment.y1, segment.shade);
}
@@ -981,6 +1001,14 @@ function paintContent(window: PinballWindow): GrayImage {
`${GESTURE_CLICK} new game`,
]);
}
return centeredInViewport(window, image);
}
/** Place the fixed-height layout vertically centered in the window's viewport. */
function centeredInViewport(window: PinballWindow, layout: GrayImage): GrayImage {
if (layout.height === window.viewportHeight) return layout;
const image = new GrayImage(window.viewportWidth, window.viewportHeight, 0);
layout.composeInto(image, 0, Math.max(0, Math.floor((window.viewportHeight - layout.height) / 2)));
return image;
}
+1 -1
View File
@@ -121,7 +121,7 @@ async function roamPost(endpoint: "q" | "pull" | "write", body: object): Promise
base = API_BASE;
continue;
}
throw new Error(`Roam API request failed: ${(error as Error)?.message ?? error}`);
throw new Error(`Roam API request failed: ${(error as Error)?.message ?? error}`, { cause: error });
}
if (response.status >= 300 && response.status < 400) {
const location = response.headers?.get?.("Location") ?? response.headers?.get?.("location");
+7 -4
View File
@@ -52,7 +52,6 @@ import {
} from "./roam-doc";
declare const global: any;
declare const com: any;
const HEADER_HEIGHT = 30;
const BOTTOM_MARGIN = 6;
@@ -252,6 +251,10 @@ global.onmessage = (event: { data: WorkerAppMessage }) => {
);
break;
}
case "input-focus":
case "navigation-sensors":
case "resize-window":
break;
}
};
@@ -339,7 +342,7 @@ function windowMenu(win: RoamWindow): WindowMenu {
windowId: win.windowId,
post,
title: () => win.title,
items: () => menuItems(win),
items: () => menuItems(),
size: { width: win.viewportWidth, height: win.viewportHeight },
paintBase: () => paintContent(win),
isFocused: () => win.focused,
@@ -348,7 +351,7 @@ function windowMenu(win: RoamWindow): WindowMenu {
return win.menu;
}
function menuItems(win: RoamWindow): MenuItem[] {
function menuItems(): MenuItem[] {
const items: MenuItem[] = [];
const today = todayRef();
if (currentRef?.uid !== today.uid) {
@@ -415,7 +418,7 @@ function endSettingEdit(): void {
function handleInput(win: RoamWindow, event: InputEvent, frameId: number): void {
if (win.menu?.isOpen()) {
win.menu
void win.menu
.handleInput(event)
.catch((error) => console.error(`roam menu input failed: ${error}`))
.then(() => renderAndSubmit(win, frameId));
+1 -1
View File
@@ -22,7 +22,7 @@ export type SettingsAppWindow = {
/** The full in-process window record, for hosts that manage window lifecycle. */
inProcess: InProcessWindow;
requestRender: () => void;
/** Select a section in the left column by label (e.g. "Terminal"). */
/** Select a section in the left column by label (e.g. "Voice"). */
focusSection: (label: string) => void;
/** Whether the glasses-side text-setting editor is the top layer. */
isTextEditorOnTop: () => boolean;
+11
View File
@@ -0,0 +1,11 @@
import { isT3CodeWidgetInUse } from "../glanceboard/configuration";
import { loadEnvironments } from "./t3-environments";
/**
* Keep the T3 worker (and its server connections) running with no window
* open, for the Glanceboard widget. The app's boot hook spawns the worker
* whenever this turns true; the worker reports idle once it turns false.
*/
export function hasT3BackgroundWork(): boolean {
return isT3CodeWidgetInUse() && loadEnvironments().some((environment) => environment.enabled);
}
+42
View File
@@ -0,0 +1,42 @@
import { launchWorkerAppWindow, type AppDefinition } from "../app-definition";
import { onSettingsStoreChanged } from "../../native/settings-store";
import { hasT3BackgroundWork } from "./background";
import { T3_ENVIRONMENTS_KEY } from "./t3-environments";
/** The one place the worker is constructed (string-literal path for the webpack worker loader). */
const createWorker = () => new Worker("./t3code-app.worker");
/** The boot hook's settings listener; replaced if a controller boots again. */
let stopWatchingSettings: (() => void) | null = null;
const t3codeApp: AppDefinition = {
appId: "t3code",
title: "T3 Code",
icon: "t3code",
launch: (ctx) =>
launchWorkerAppWindow(ctx, {
createWorker,
windowId: "t3code:main",
title: "T3 Code",
iconLetter: "T3",
icon: "t3code",
}),
/**
* The Glanceboard's T3 Code widget shows what this app's worker knows, so
* start the worker (windowless) whenever the widget is in use: at startup,
* and when the widget, the board, or the paired environments change. The
* worker reports idle by itself once nothing needs it.
*/
boot: (ctx) => {
const ensureWorker = () => {
if (hasT3BackgroundWork()) ctx.ensureWorkerHost(createWorker);
};
ensureWorker();
stopWatchingSettings?.();
stopWatchingSettings = onSettingsStoreChanged((key) => {
if (key.startsWith("glanceboard.") || key === T3_ENVIRONMENTS_KEY) ensureWorker();
});
},
};
export default t3codeApp;
+174
View File
@@ -0,0 +1,174 @@
/**
* HTTP half of connecting to a T3 Code environment: the unauthenticated
* environment descriptor, the one-time pairing-token exchange (RFC 8693, form
* encoded), and short-lived WebSocket tickets.
*
* Direct pairings use a plain Bearer token: 30 days, no refresh. When it
* expires (or is revoked) the ticket request fails with 401 and the user has
* to pair again with a fresh `t3 pair` link.
*/
/** The orchestration protocol this client speaks (the server answers 426 to any other). */
export const T3_ORCHESTRATION_PROTOCOL = 2;
/** What every ordinary pairing grants; asking for a scope the link doesn't grant burns the token. */
const STANDARD_SCOPES = "orchestration:read orchestration:operate terminal:operate review:write relay:read";
const HTTP_TIMEOUT_MS = 12_000;
/** The subset of fetch these calls need, so tests and Node scripts can pass their own. */
export type T3Fetch = (
url: string,
init: { method: string; headers: Record<string, string>; body?: string },
) => Promise<{ status: number; ok: boolean; text(): Promise<string> }>;
export type EnvironmentDescriptor = {
environmentId: string;
label: string;
serverVersion: string;
orchestrationProtocolVersion: number;
capabilities: Record<string, unknown>;
};
export type PairedCredential = {
accessToken: string;
/** Epoch ms. */
expiresAtMs: number;
};
/** An HTTP failure; `authFailed` means the stored token is no good (re-pair). */
export class T3HttpError extends Error {
constructor(message: string, readonly status: number, readonly authFailed = false) {
super(message);
this.name = "T3HttpError";
}
}
/** Form/query encoding (not URLSearchParams, which the NativeScript runtime mishandles). */
function buildQuery(params: Record<string, string>): string {
return Object.keys(params)
.map((key) => `${encodeURIComponent(key)}=${encodeURIComponent(params[key]!)}`)
.join("&");
}
async function request(
fetchImpl: T3Fetch,
url: string,
init: { method: string; headers: Record<string, string>; body?: string },
): Promise<{ status: number; ok: boolean; body: string }> {
let timer: ReturnType<typeof setTimeout> | null = null;
const timeout = new Promise<never>((_resolve, reject) => {
timer = setTimeout(() => reject(new T3HttpError("Timed out reaching the server.", 0)), HTTP_TIMEOUT_MS);
});
try {
const response = await Promise.race([fetchImpl(url, init), timeout]);
const body = await Promise.race([response.text(), timeout]);
return { status: response.status, ok: response.ok, body };
} catch (error) {
if (error instanceof T3HttpError) throw error;
throw new T3HttpError(`Can't reach the server: ${(error as Error)?.message ?? error}`, 0);
} finally {
if (timer) clearTimeout(timer);
}
}
function parseJson(body: string): any {
try {
return JSON.parse(body);
} catch {
return null;
}
}
/** Human message for T3's error envelope ({_tag, code, reason, traceId}). */
function describeFailure(status: number, body: string, what: string): string {
const parsed = parseJson(body);
const reason = typeof parsed?.reason === "string" ? parsed.reason : typeof parsed?.code === "string" ? parsed.code : "";
switch (reason) {
case "invalid_credential":
return what === "pair"
? "That pairing link was already used or has expired. Run `t3 pair` for a new one."
: "The server no longer accepts this device's sign-in. Pair it again.";
case "missing_credential":
return "The server asked for a sign-in. Pair this device again.";
case "scope_not_granted":
return "That pairing link doesn't grant enough access. Run `t3 pair` for a new one.";
}
if (status === 426 || parsed?.code === "orchestration_protocol_incompatible") {
return "This T3 Code server speaks a different protocol version. Update T3 Code and Faceclaw.";
}
return `Server error (HTTP ${status}${reason ? `, ${reason}` : ""}).`;
}
export async function fetchEnvironmentDescriptor(fetchImpl: T3Fetch, httpBaseUrl: string): Promise<EnvironmentDescriptor> {
const response = await request(fetchImpl, `${httpBaseUrl}.well-known/t3/environment`, { method: "GET", headers: {} });
const parsed = parseJson(response.body);
if (!response.ok || !parsed || typeof parsed.environmentId !== "string") {
throw new T3HttpError(
response.ok ? "That address doesn't look like a T3 Code server." : describeFailure(response.status, response.body, "descriptor"),
response.status,
);
}
return {
environmentId: parsed.environmentId,
label: typeof parsed.label === "string" ? parsed.label : "",
serverVersion: typeof parsed.serverVersion === "string" ? parsed.serverVersion : "",
orchestrationProtocolVersion: typeof parsed.orchestrationProtocolVersion === "number" ? parsed.orchestrationProtocolVersion : 1,
capabilities: parsed.capabilities && typeof parsed.capabilities === "object" ? parsed.capabilities : {},
};
}
/** Trade a one-time pairing credential for a durable bearer token. */
export async function exchangePairingCredential(
fetchImpl: T3Fetch,
httpBaseUrl: string,
credential: string,
client: { label: string; os: string },
): Promise<PairedCredential> {
const body = buildQuery({
grant_type: "urn:ietf:params:oauth:grant-type:token-exchange",
subject_token: credential,
subject_token_type: "urn:t3:params:oauth:token-type:environment-bootstrap",
requested_token_type: "urn:ietf:params:oauth:token-type:access_token",
scope: STANDARD_SCOPES,
client_label: client.label,
client_device_type: "mobile",
client_os: client.os,
});
const response = await request(fetchImpl, `${httpBaseUrl}oauth/token`, {
method: "POST",
headers: { "Content-Type": "application/x-www-form-urlencoded" },
body,
});
const parsed = parseJson(response.body);
if (!response.ok || typeof parsed?.access_token !== "string") {
throw new T3HttpError(describeFailure(response.status, response.body, "pair"), response.status, response.status === 401);
}
const expiresIn = typeof parsed.expires_in === "number" ? parsed.expires_in : 30 * 24 * 3600;
return { accessToken: parsed.access_token, expiresAtMs: Date.now() + expiresIn * 1000 };
}
/** A WebSocket ticket (valid ~5 minutes); fetch a fresh one for every connect. */
export async function fetchWebSocketTicket(fetchImpl: T3Fetch, httpBaseUrl: string, accessToken: string): Promise<string> {
const response = await request(fetchImpl, `${httpBaseUrl}api/auth/websocket-ticket`, {
method: "POST",
headers: { Authorization: `Bearer ${accessToken}` },
});
const parsed = parseJson(response.body);
if (!response.ok || typeof parsed?.ticket !== "string") {
throw new T3HttpError(describeFailure(response.status, response.body, "ticket"), response.status, response.status === 401);
}
return parsed.ticket;
}
/** The /ws URL for a ticket. */
export function webSocketUrl(wsBaseUrl: string, ticket: string, clientOs: string): string {
return `${wsBaseUrl}ws?${buildQuery({
wsTicket: ticket,
clientSurface: "mobile",
clientDeviceType: "phone",
clientOs,
connectionMethod: "direct",
orchestrationProtocol: String(T3_ORCHESTRATION_PROTOCOL),
})}`;
}
+425
View File
@@ -0,0 +1,425 @@
/**
* One live connection to a paired T3 Code environment: fetches a WebSocket
* ticket with the stored bearer token, opens the RPC socket, waits for the
* server-config snapshot (checking it is the environment we paired with),
* then keeps the thread list (subscribeShell) and any open threads
* (subscribeThread) live. Owns its own retry loop: dropped connections come
* back with jittered exponential backoff and resume every stream from its
* last sequence. A rejected token stops retrying until the user pairs again.
*
* Transport and HTTP are injected, so this runs unchanged under Node.
*/
import {
fetchWebSocketTicket,
T3HttpError,
webSocketUrl,
type T3Fetch,
} from "./t3-auth";
import { ShellModel, ThreadDetailModel, type T3ServerProvider } from "./t3-model";
import { T3RpcConnection, T3RpcError, type RpcSocketFactory, type T3Subscription } from "./t3-rpc";
/** A paired environment as stored on the phone. */
export type StoredEnvironment = {
/** Local id (stable across re-pairing). */
id: string;
/** The server's own id, checked on every connect. */
environmentId: string;
/** The server's label (its machine name). */
label: string;
httpBaseUrl: string;
wsBaseUrl: string;
/** Bearer token from pairing (30-day lifetime, no refresh). */
accessToken: string;
expiresAtMs: number;
/** Connect while the app is open. */
enabled: boolean;
};
export type EnvironmentPhase =
| "idle"
| "connecting"
| "connected"
/** Waiting to try again after a failure. */
| "retrying"
/** The token was rejected or expired: pair again. */
| "auth-failed";
export type T3ClientDeps = {
fetch: T3Fetch;
openSocket: RpcSocketFactory;
/** Reported to the server as the client OS ("Android", "iOS"). */
clientOs: string;
log?: (message: string) => void;
};
const RECONNECT_MIN_DELAY_MS = 1_000;
const RECONNECT_MAX_DELAY_MS = 60_000;
/** How long the socket may stay open without delivering the config snapshot. */
const READY_TIMEOUT_MS = 20_000;
/** Resubscribe delay after a stream fails on a healthy connection (e.g. a server buffer overflow). */
const STREAM_RETRY_MS = 250;
export type ThreadHandle = {
readonly model: ThreadDetailModel;
/** The subscription failed for a reason other than the connection dropping. */
readonly error: () => string;
close(): void;
};
type ThreadEntry = {
model: ThreadDetailModel;
subscription: T3Subscription | null;
retryTimer: ReturnType<typeof setTimeout> | null;
error: string;
listeners: Set<() => void>;
refs: number;
};
export class T3EnvironmentClient {
readonly shell = new ShellModel();
providers: T3ServerProvider[] = [];
settings: Record<string, any> | null = null;
private phaseValue: EnvironmentPhase = "idle";
private statusValue = "";
private connection: T3RpcConnection | null = null;
private shellSubscription: T3Subscription | null = null;
private shellRetryTimer: ReturnType<typeof setTimeout> | null = null;
private readyTimer: ReturnType<typeof setTimeout> | null = null;
private reconnectTimer: ReturnType<typeof setTimeout> | null = null;
private reconnectDelayMs = RECONNECT_MIN_DELAY_MS;
/** Bumped on every connect attempt and stop, so stale async work can tell it lost. */
private generation = 0;
private running = false;
private readonly listeners = new Set<() => void>();
private readonly threads = new Map<string, ThreadEntry>();
constructor(
public config: StoredEnvironment,
private readonly deps: T3ClientDeps,
) {}
get phase(): EnvironmentPhase {
return this.phaseValue;
}
/** Human-readable connection status ("" when connected and healthy). */
get status(): string {
return this.statusValue;
}
/** Connected, with the thread list loaded. */
get ready(): boolean {
return this.phaseValue === "connected" && this.shell.sequence !== null;
}
get retryScheduled(): boolean {
return this.reconnectTimer !== null;
}
/** Any change worth repainting for: phase, status, thread list, config. */
onChange(listener: () => void): () => void {
this.listeners.add(listener);
return () => this.listeners.delete(listener);
}
/** Start connecting (no-op while already running). */
start(): void {
if (this.running) return;
this.running = true;
this.reconnectDelayMs = RECONNECT_MIN_DELAY_MS;
void this.connect();
}
/** Disconnect and stop retrying. Open thread handles stay registered and resume on the next start. */
stop(): void {
this.running = false;
this.generation++;
this.clearTimers();
this.teardownConnection("Disconnected.");
this.setPhase("idle", "");
}
/** Retry right away (manual reconnect), resetting backoff. */
retryNow(): void {
this.stop();
this.start();
}
/** Replace the stored config (e.g. re-paired); reconnects if anything connection-relevant changed. */
updateConfig(config: StoredEnvironment): void {
const reconnect =
config.accessToken !== this.config.accessToken ||
config.httpBaseUrl !== this.config.httpBaseUrl ||
config.environmentId !== this.config.environmentId;
this.config = config;
if (reconnect && this.running) this.retryNow();
else this.emit();
}
/**
* Subscribe to a thread's detail stream. Handles are reference counted:
* every openThread needs its own close().
*/
openThread(threadId: string, onChange: () => void): ThreadHandle {
let entry = this.threads.get(threadId);
if (!entry) {
entry = { model: new ThreadDetailModel(threadId), subscription: null, retryTimer: null, error: "", listeners: new Set(), refs: 0 };
this.threads.set(threadId, entry);
if (this.phaseValue === "connected") this.subscribeThread(threadId, entry);
}
const current = entry;
current.refs++;
current.listeners.add(onChange);
let closed = false;
return {
model: current.model,
error: () => current.error,
close: () => {
if (closed) return;
closed = true;
current.listeners.delete(onChange);
if (--current.refs > 0) return;
current.subscription?.cancel();
if (current.retryTimer) clearTimeout(current.retryTimer);
this.threads.delete(threadId);
},
};
}
/** orchestration.dispatchCommand; resolves to the committed event sequence. */
async dispatch(command: Record<string, unknown>): Promise<number> {
const connection = this.requireConnection();
const result = await connection.call<{ sequence?: number }>("orchestration.dispatchCommand", command);
return typeof result?.sequence === "number" ? result.sequence : 0;
}
/** orchestration.launchThread; resolves to the new thread's id. */
async launchThread(payload: Record<string, unknown>): Promise<string> {
const connection = this.requireConnection();
const result = await connection.call<{ threadId?: string }>("orchestration.launchThread", payload);
return String(result?.threadId ?? payload.threadId);
}
private requireConnection(): T3RpcConnection {
if (this.phaseValue !== "connected" || !this.connection) {
throw new T3RpcError("disconnected", `Not connected to ${this.config.label || "the server"}.`);
}
return this.connection;
}
// -------------------------------------------------------------------------
// Connection lifecycle
private async connect(): Promise<void> {
const generation = ++this.generation;
this.clearTimers();
this.teardownConnection("Reconnecting.");
if (this.config.expiresAtMs && this.config.expiresAtMs <= Date.now()) {
this.setPhase("auth-failed", "Sign-in expired. Pair this device again.");
return;
}
this.setPhase("connecting", "Connecting...");
let ticket: string;
try {
ticket = await fetchWebSocketTicket(this.deps.fetch, this.config.httpBaseUrl, this.config.accessToken);
} catch (error) {
if (generation !== this.generation) return;
if (error instanceof T3HttpError && error.authFailed) {
this.setPhase("auth-failed", error.message);
return;
}
this.scheduleReconnect(errorMessage(error));
return;
}
if (generation !== this.generation) return;
const connection = new T3RpcConnection({
url: webSocketUrl(this.config.wsBaseUrl, ticket, this.deps.clientOs),
openSocket: this.deps.openSocket,
});
this.connection = connection;
connection.onStateChange((state, reason) => {
if (state !== "closed" || generation !== this.generation) return;
this.handleConnectionLost(reason);
});
this.readyTimer = setTimeout(() => {
if (generation === this.generation) connection.close("The server didn't finish connecting.");
}, READY_TIMEOUT_MS);
let ready = false;
connection.subscribe(
"subscribeServerConfig",
{},
(values) => {
if (generation !== this.generation) return;
for (const value of values) {
const event = value as Record<string, any>;
if (event?.type === "snapshot") {
const config = event.config ?? {};
const environmentId = config.environment?.environmentId;
if (this.config.environmentId && environmentId && environmentId !== this.config.environmentId) {
this.generation++;
this.teardownConnection("Wrong server.");
this.setPhase("auth-failed", "A different T3 Code server answered at this address. Pair it again.");
return;
}
this.providers = Array.isArray(config.providers) ? config.providers : [];
this.settings = config.settings ?? null;
if (typeof config.environment?.label === "string" && config.environment.label && config.environment.label !== this.config.label) {
this.config = { ...this.config, label: config.environment.label };
}
if (!ready) {
ready = true;
this.handleReady(generation);
}
} else if (event?.type === "providerStatuses" && Array.isArray(event.payload?.providers)) {
this.providers = event.payload.providers;
} else if (event?.type === "settingsUpdated" && event.payload?.settings) {
this.settings = event.payload.settings;
}
}
this.emit();
},
(error) => {
// The session is unusable without its config stream (T3's own client
// treats this as the session closing).
if (generation !== this.generation || error?.kind === "disconnected") return;
connection.close(`Server config stream ended${error ? `: ${error.message}` : "."}`);
},
);
}
private handleReady(generation: number): void {
if (this.readyTimer) clearTimeout(this.readyTimer);
this.readyTimer = null;
this.reconnectDelayMs = RECONNECT_MIN_DELAY_MS;
this.setPhase("connected", "");
this.deps.log?.(`t3code: connected to ${this.config.label} (${this.config.httpBaseUrl})`);
this.subscribeShell(generation);
for (const [threadId, entry] of this.threads) this.subscribeThread(threadId, entry);
}
private subscribeShell(generation: number): void {
const connection = this.connection;
if (!connection || generation !== this.generation) return;
this.shell.synchronized = false;
this.shellSubscription?.cancel();
const payload: Record<string, unknown> = { requestCompletionMarker: true };
if (this.shell.sequence !== null) payload.afterSequence = this.shell.sequence;
this.shellSubscription = connection.subscribe(
"orchestration.subscribeShell",
payload,
(values) => {
if (generation !== this.generation) return;
let changed = false;
for (const value of values) changed = this.shell.apply(value) || changed;
if (changed) this.emit();
},
(error) => {
this.shellSubscription = null;
if (generation !== this.generation || error?.kind === "disconnected") return;
this.deps.log?.(`t3code: shell stream ended: ${error?.message ?? "completed"}`);
this.shellRetryTimer = setTimeout(() => {
this.shellRetryTimer = null;
this.subscribeShell(generation);
}, STREAM_RETRY_MS);
},
);
}
private subscribeThread(threadId: string, entry: ThreadEntry): void {
const connection = this.connection;
if (!connection || this.phaseValue !== "connected") return;
const generation = this.generation;
entry.subscription?.cancel();
entry.model.synchronized = false;
const payload: Record<string, unknown> = { threadId, requestCompletionMarker: true, acceptBoundedSnapshot: true };
if (entry.model.sequence !== null) payload.afterSequence = entry.model.sequence;
const notify = () => {
for (const listener of [...entry.listeners]) listener();
};
const subscription = connection.subscribe(
"orchestration.subscribeThread",
payload,
(values) => {
if (generation !== this.generation || this.threads.get(threadId) !== entry) return;
let changed = false;
for (const value of values) changed = entry.model.apply(value) || changed;
if (entry.error) {
entry.error = "";
changed = true;
}
if (changed) notify();
},
(error) => {
if (entry.subscription === subscription) entry.subscription = null;
if (generation !== this.generation || this.threads.get(threadId) !== entry || error?.kind === "disconnected") return;
entry.error = error ? error.message : "The thread stream ended.";
notify();
if (error?.kind === "fail" || error?.kind === "interrupt") {
entry.retryTimer = setTimeout(() => {
entry.retryTimer = null;
if (this.threads.get(threadId) === entry) this.subscribeThread(threadId, entry);
}, STREAM_RETRY_MS * 4);
}
},
);
entry.subscription = subscription;
}
private handleConnectionLost(reason: string): void {
this.connection = null;
this.shellSubscription = null;
for (const entry of this.threads.values()) entry.subscription = null;
if (!this.running) return;
this.scheduleReconnect(reason);
}
private scheduleReconnect(reason: string): void {
if (!this.running) return;
const delay = Math.round(this.reconnectDelayMs * (0.75 + Math.random() * 0.5));
this.reconnectDelayMs = Math.min(this.reconnectDelayMs * 2, RECONNECT_MAX_DELAY_MS);
this.setPhase("retrying", reason);
this.deps.log?.(`t3code: ${this.config.label}: ${reason} (retrying in ${delay}ms)`);
const generation = this.generation;
this.reconnectTimer = setTimeout(() => {
this.reconnectTimer = null;
if (generation === this.generation && this.running) void this.connect();
}, delay);
}
private teardownConnection(reason: string): void {
const connection = this.connection;
this.connection = null;
this.shellSubscription = null;
for (const entry of this.threads.values()) entry.subscription = null;
connection?.close(reason);
}
private clearTimers(): void {
for (const timer of [this.readyTimer, this.shellRetryTimer]) if (timer) clearTimeout(timer);
if (this.reconnectTimer) clearTimeout(this.reconnectTimer);
this.readyTimer = null;
this.shellRetryTimer = null;
this.reconnectTimer = null;
for (const entry of this.threads.values()) {
if (entry.retryTimer) clearTimeout(entry.retryTimer);
entry.retryTimer = null;
}
}
private setPhase(phase: EnvironmentPhase, status: string): void {
if (this.phaseValue === phase && this.statusValue === status) return;
this.phaseValue = phase;
this.statusValue = status;
this.emit();
}
private emit(): void {
for (const listener of [...this.listeners]) listener();
}
}
export function errorMessage(error: unknown): string {
return String((error as Error)?.message ?? error);
}
+49
View File
@@ -0,0 +1,49 @@
import { getStringSetting, setStringSetting } from "../../native/settings-store";
import type { StoredEnvironment } from "./t3-client";
/**
* Paired T3 Code environments, stored as JSON under one settings key (like
* the Terminal app's connections). Each entry holds the bearer token issued
* at pairing; it lasts 30 days and is never shown on screen.
*/
export const T3_ENVIRONMENTS_KEY = "t3code.environments";
export function loadEnvironments(): StoredEnvironment[] {
const raw = getStringSetting(T3_ENVIRONMENTS_KEY, "");
if (!raw) return [];
try {
const parsed = JSON.parse(raw);
if (!Array.isArray(parsed)) return [];
return parsed
.map((item: any): StoredEnvironment => ({
id: String(item?.id ?? ""),
environmentId: String(item?.environmentId ?? ""),
label: String(item?.label ?? ""),
httpBaseUrl: String(item?.httpBaseUrl ?? ""),
wsBaseUrl: String(item?.wsBaseUrl ?? ""),
accessToken: String(item?.accessToken ?? ""),
expiresAtMs: Number(item?.expiresAtMs) || 0,
enabled: item?.enabled !== false,
}))
.filter((environment) => environment.id && environment.httpBaseUrl && environment.accessToken);
} catch {
return [];
}
}
export function saveEnvironments(environments: StoredEnvironment[]): void {
setStringSetting(T3_ENVIRONMENTS_KEY, JSON.stringify(environments));
}
/** Insert or replace (by local id). */
export function upsertEnvironment(environment: StoredEnvironment): void {
const environments = loadEnvironments();
const index = environments.findIndex((candidate) => candidate.id === environment.id);
if (index >= 0) environments[index] = environment;
else environments.push(environment);
saveEnvironments(environments);
}
export function removeEnvironment(id: string): void {
saveEnvironments(loadEnvironments().filter((environment) => environment.id !== id));
}
+103
View File
@@ -0,0 +1,103 @@
/**
* The T3 Code thread list as the Glanceboard shows it: the T3 worker builds
* this snapshot from its live connections and publishes it through the
* host's worker-state channel; the T3 Code widget paints it on the main
* thread. Plain JSON (it crosses postMessage) with timestamps rather than
* formatted ages, so the widget's "5m" is right whenever it paints.
*
* No NativeScript imports: this module is bundled into the worker as well.
*/
import {
isThreadUnread,
sectionThreads,
threadActivityMs,
threadStatus,
type T3ThreadShell,
type T3ThreadStatus,
} from "./t3-model";
/** Worker-state key the T3 worker publishes its snapshot under. */
export const T3_GLANCE_STATE_KEY = "t3code:threads";
/** More than a double-height slot can show; the widget says how many it left out. */
const MAX_ROWS = 24;
export type T3GlanceRow = {
/** Environment id + thread id. */
key: string;
title: string;
status: T3ThreadStatus;
/** A run finished since the thread was last visited (on any T3 client). */
unread: boolean;
/** For approvals: what kind ("command", "file-change", ...). */
requestKind: string | null;
/** Last activity, epoch ms (server clock). */
activityMs: number;
};
export type T3GlanceSnapshot = {
/** Any environment is paired. */
configured: boolean;
/** Why there is nothing to show when no environment is connected ("Connecting…", ...); "" otherwise. */
status: string;
needsYou: number;
working: number;
/** Needs-you first, then working, then recent; settled and snoozed threads left out. */
rows: T3GlanceRow[];
/** Listed threads beyond `rows`. */
more: number;
};
export type T3GlanceEnvironment = {
id: string;
ready: boolean;
/** "connecting", "retrying", "auth-failed", ... (T3EnvironmentClient phase). */
phase: string;
enabled: boolean;
threads: Iterable<T3ThreadShell>;
};
export function buildGlanceSnapshot(environments: readonly T3GlanceEnvironment[], nowMs: number): T3GlanceSnapshot {
const owner = new Map<T3ThreadShell, string>();
for (const environment of environments) {
if (!environment.ready) continue;
for (const thread of environment.threads) owner.set(thread, environment.id);
}
const sections = sectionThreads(owner.keys(), nowMs);
const listed = [...sections.attention, ...sections.working, ...sections.recent];
const rows = listed.slice(0, MAX_ROWS).map((thread): T3GlanceRow => {
const status = threadStatus(thread);
return {
key: `${owner.get(thread)}:${thread.id}`,
title: thread.title || "Untitled",
status,
unread: isThreadUnread(thread),
requestKind: status === "approval" ? (thread.pendingRuntimeRequest?.kind ?? null) : null,
activityMs: threadActivityMs(thread),
};
});
return {
configured: environments.length > 0,
status: environments.some((environment) => environment.ready) ? "" : environmentsStatus(environments),
needsYou: sections.attention.length,
working: sections.working.filter((thread) => threadStatus(thread) === "working").length,
rows,
more: listed.length - rows.length,
};
}
function environmentsStatus(environments: readonly T3GlanceEnvironment[]): string {
if (!environments.length) return "";
if (environments.every((environment) => !environment.enabled)) return "Disconnected";
if (environments.some((environment) => environment.phase === "connecting" || environment.phase === "connected")) return "Connecting…";
if (environments.every((environment) => !environment.enabled || environment.phase === "auth-failed")) return "Pair again in the T3 Code app";
return "Can't reach T3 Code";
}
/** "1 needs you · 2 working" (or "" when neither). */
export function glanceSummary(snapshot: Pick<T3GlanceSnapshot, "needsYou" | "working">): string {
return [
snapshot.needsYou ? `${snapshot.needsYou} need${snapshot.needsYou === 1 ? "s" : ""} you` : "",
snapshot.working ? `${snapshot.working} working` : "",
].filter(Boolean).join(" · ");
}
+924
View File
@@ -0,0 +1,924 @@
/**
* Client-side state for T3 Code's orchestration (v2) streams, independent of
* transport and rendering: the shell (projects + thread list) reducer, the
* per-thread detail reducer, thread status / ordering for the glasses list,
* one-line summaries of timeline items, and the dispatch-command builders.
*
* Mirrors T3's own client runtime (packages/client-runtime/src/state/
* shellReducer.ts, orchestrationV2Projection.ts, models.ts; apps/mobile's
* threadListV2.ts and threadActivity.ts) in reduced form. Wire objects are
* typed loosely: only the fields read here are declared, and everything is
* treated as possibly missing so newer/older servers degrade gracefully.
*/
// ---------------------------------------------------------------------------
// Wire shapes (subset)
export type T3ProjectShell = {
id: string;
title: string;
workspaceRoot?: string;
defaultModelSelection?: T3ModelSelection | null;
};
export type T3ModelSelection = { instanceId: string; model: string; options?: unknown[] };
export type T3ThreadShell = {
id: string;
projectId: string;
title: string;
providerInstanceId?: string | null;
modelSelection?: T3ModelSelection | null;
status?: string;
activityRunStatus?: string | null;
activeRunId?: string | null;
latestRunId?: string | null;
activeProviderThreadId?: string | null;
latestRunRequestedAt?: string | null;
latestRunCompletedAt?: string | null;
lastVisitedAt?: string | null;
pendingRuntimeRequest?: { id: string; kind: string; createdAt?: string } | null;
pendingBackgroundTasks?: Array<{ kind?: string }>;
lastError?: string | null;
lastErrorClass?: string | null;
usageLimitResetAt?: string | null;
latestUserMessageAt?: string | null;
latestUserAuthoredMessageAt?: string | null;
hasActionableProposedPlan?: boolean;
createdAt?: string;
updatedAt?: string;
archivedAt?: string | null;
deletedAt?: string | null;
settledOverride?: string | null;
settledAt?: string | null;
snoozedUntil?: string | null;
snoozedAt?: string | null;
lineage?: { relationshipToParent?: string | null } | null;
};
export type T3Run = {
id: string;
status: string;
userMessageId?: string | null;
requestedAt?: string | null;
queuePosition?: number | null;
};
export type T3RunAttempt = { id: string; runId: string; rootNodeId?: string | null; status: string };
export type T3RuntimeRequest = {
id: string;
kind: string;
status: string;
responseCapability?: { type: string } | null;
};
export type T3ConversationMessage = {
id: string;
role?: string;
text?: string;
delegatedCompletion?: unknown;
notification?: unknown;
};
export type T3PlanStep = { id?: string; text: string; status: string };
export type T3Plan = {
id: string;
kind: string;
runId?: string | null;
status?: string;
steps?: T3PlanStep[];
};
export type T3ApprovalOption = { decision: string; label: string; warning?: string };
export type T3Question = {
id: string;
header?: string;
question: string;
options?: Array<{ label: string; description?: string; value?: string }>;
multiSelect?: boolean;
allowCustomAnswer?: boolean;
required?: boolean;
};
/** A timeline row. Fields beyond the common ones depend on `type`. */
export type T3TurnItem = {
id: string;
threadId: string;
runId: string | null;
nodeId?: string | null;
ordinal: number;
status: string;
title?: string | null;
type: string;
// user_message / assistant_message / reasoning
text?: string;
streaming?: boolean;
inputIntent?: string;
// command_execution
input?: unknown;
exitCode?: number;
outputIndicatesFailure?: boolean;
// file_change
fileName?: string;
additions?: number;
deletions?: number;
changes?: unknown[];
// file_search / web_search
pattern?: string;
patterns?: string[];
// dynamic_tool
toolName?: string | null;
// approval_request / user_input_request
requestId?: string;
requestKind?: string;
prompt?: string;
appName?: string;
options?: T3ApprovalOption[];
questions?: T3Question[];
responseMode?: string;
// notification / system_notice / run_interrupt_result
summary?: string;
detail?: string;
message?: string;
// error
failure?: { class?: string; message?: string; code?: string | null } | null;
// compaction
beforeTokenCount?: number;
afterTokenCount?: number;
// handoff
toModel?: string;
// proposed_plan
markdown?: string;
};
type ProjectedTurnItem = { visibility?: string; item: T3TurnItem };
// ---------------------------------------------------------------------------
// Shell (projects + thread list)
export class ShellModel {
readonly projects = new Map<string, T3ProjectShell>();
readonly threads = new Map<string, T3ThreadShell>();
/** Last applied sequence (the resume cursor), or null before the first snapshot. */
sequence: number | null = null;
/** The server finished replaying and the stream is live. */
synchronized = false;
/** Apply one subscribeShell stream item; returns whether anything changed. */
apply(raw: unknown): boolean {
const item = raw as Record<string, any>;
switch (item?.kind) {
case "synchronized":
this.synchronized = true;
return true;
case "snapshot": {
const snapshot = item.snapshot ?? {};
if (Array.isArray(item.resolvedRepositoryIdentityRoots)) {
// Metadata-only refresh (carries threads: []): never a replacement.
return false;
}
this.projects.clear();
this.threads.clear();
for (const project of snapshot.projects ?? []) this.projects.set(project.id, project);
for (const thread of snapshot.threads ?? []) this.threads.set(thread.id, thread);
this.sequence = typeof snapshot.snapshotSequence === "number" ? snapshot.snapshotSequence : 0;
return true;
}
case "project.updated":
case "project.removed":
case "thread.updated":
case "thread.removed": {
const sequence = typeof item.sequence === "number" ? item.sequence : null;
if (sequence !== null && this.sequence !== null && sequence <= this.sequence) return false;
if (item.kind === "project.updated" && item.project) this.projects.set(item.project.id, item.project);
else if (item.kind === "project.removed") this.projects.delete(item.projectId);
else if (item.kind === "thread.updated" && item.thread) {
if (item.location === "archive") this.threads.delete(item.thread.id);
else this.threads.set(item.thread.id, item.thread);
} else if (item.kind === "thread.removed") this.threads.delete(item.threadId);
if (sequence !== null) this.sequence = sequence;
return true;
}
default:
return false;
}
}
}
// ---------------------------------------------------------------------------
// Thread status and list ordering (as T3's mobile thread list derives them)
export type T3ThreadStatus =
/** A provider approval is waiting on the user. */
| "approval"
/** The provider asked the user a question. */
| "input"
| "working"
/** Parked on background work (subagent, monitor) that will wake it. */
| "waiting"
| "failed"
/** Failed on a usage limit. */
| "limited"
| "ready";
const ACTIVE_RUN_STATUSES = new Set(["preparing", "queued", "starting", "running", "waiting"]);
const WAKING_BACKGROUND_KINDS = new Set(["subagent", "monitor", "background_task"]);
export function threadStatus(thread: T3ThreadShell): T3ThreadStatus {
const request = thread.pendingRuntimeRequest;
if (request && request.kind !== "user_input" && request.kind !== "auth_refresh") return "approval";
if (request?.kind === "user_input") return "input";
if (thread.latestRunId == null && thread.activeProviderThreadId == null) return "ready";
const background =
thread.status !== "failed" &&
(thread.pendingBackgroundTasks ?? []).some((task) => WAKING_BACKGROUND_KINDS.has(task.kind ?? ""));
const runStatus = background ? "idle" : (thread.activityRunStatus ?? thread.status ?? "idle");
if (ACTIVE_RUN_STATUSES.has(runStatus)) return "working";
if (runStatus === "idle") return "waiting";
if (runStatus === "failed") return thread.lastErrorClass === "usage_limit" ? "limited" : "failed";
return "ready";
}
export function threadStatusLabel(status: T3ThreadStatus): string {
switch (status) {
case "approval":
return "Needs approval";
case "input":
return "Has a question";
case "working":
return "Working";
case "waiting":
return "Waiting";
case "failed":
return "Failed";
case "limited":
return "Usage limit";
case "ready":
return "Ready";
}
}
const STATUS_MARKERS: Record<T3ThreadStatus, { marker: string; value: number }> = {
approval: { marker: "!", value: 255 },
input: { marker: "?", value: 255 },
working: { marker: "…", value: 210 },
waiting: { marker: "…", value: 130 },
failed: { marker: "x", value: 200 },
limited: { marker: "x", value: 160 },
ready: { marker: "", value: 0 },
};
/**
* The one-glyph status mark drawn left of a thread title (in the app's list
* and on the Glanceboard), with its gray level: ! approval, ? question,
* … working, x failed, ● finished since last visited, ✓ settled.
*/
export function threadMarker(status: T3ThreadStatus, flags: { settled?: boolean; unread?: boolean } = {}): { marker: string; value: number } {
if (status === "ready" && flags.settled) return { marker: "✓", value: 110 };
if (status === "ready" && flags.unread) return { marker: "●", value: 220 };
return STATUS_MARKERS[status];
}
function timeMs(value: string | null | undefined): number {
if (!value) return 0;
const parsed = Date.parse(value);
return Number.isFinite(parsed) ? parsed : 0;
}
/** A run finished since the user (on any client) last looked at the thread. */
export function isThreadUnread(thread: T3ThreadShell): boolean {
const completed = timeMs(thread.latestRunCompletedAt);
const visited = timeMs(thread.lastVisitedAt);
return completed > 0 && visited > 0 && completed > visited;
}
export function isThreadSettled(thread: T3ThreadShell): boolean {
return thread.settledOverride === "settled";
}
export function isThreadSnoozed(thread: T3ThreadShell, nowMs: number): boolean {
if (timeMs(thread.snoozedUntil) <= nowMs) return false;
// A raised hand (question, approval, failure, or a run finishing after the
// snooze) brings it back.
const status = threadStatus(thread);
if (status === "approval" || status === "input" || status === "failed") return false;
return !(timeMs(thread.latestRunCompletedAt) > timeMs(thread.snoozedAt));
}
/** Archived, deleted and provider-run subagent threads stay out of the list. */
export function isThreadListed(thread: T3ThreadShell): boolean {
return !thread.archivedAt && !thread.deletedAt && thread.lineage?.relationshipToParent !== "subagent";
}
/** When the thread last saw activity worth sorting by. */
export function threadActivityMs(thread: T3ThreadShell): number {
return timeMs(thread.latestUserMessageAt) || timeMs(thread.updatedAt) || timeMs(thread.createdAt);
}
function settledMs(thread: T3ThreadShell): number {
return timeMs(thread.settledAt) || threadActivityMs(thread);
}
export type ThreadSection = "attention" | "working" | "recent" | "snoozed" | "settled";
export type ThreadSections = Record<ThreadSection, T3ThreadShell[]>;
/**
* The glasses list: threads that need the user first, then running ones
* (ordered by when the user last wrote, so finishing doesn't reshuffle
* them), then everything else by recent activity; snoozed and settled
* threads are kept apart.
*/
export function sectionThreads(threads: Iterable<T3ThreadShell>, nowMs: number): ThreadSections {
const sections: ThreadSections = { attention: [], working: [], recent: [], snoozed: [], settled: [] };
for (const thread of threads) {
if (!isThreadListed(thread)) continue;
const status = threadStatus(thread);
if (status === "approval" || status === "input") sections.attention.push(thread);
else if (isThreadSnoozed(thread, nowMs)) sections.snoozed.push(thread);
else if (isThreadSettled(thread)) sections.settled.push(thread);
else if (status === "working" || status === "waiting") sections.working.push(thread);
else sections.recent.push(thread);
}
const byId = (a: T3ThreadShell, b: T3ThreadShell) => a.id.localeCompare(b.id);
sections.attention.sort((a, b) => timeMs(b.pendingRuntimeRequest?.createdAt) - timeMs(a.pendingRuntimeRequest?.createdAt) || byId(a, b));
const sentMs = (thread: T3ThreadShell) => timeMs(thread.latestUserAuthoredMessageAt) || timeMs(thread.latestRunRequestedAt);
sections.working.sort((a, b) => sentMs(b) - sentMs(a) || byId(a, b));
sections.recent.sort((a, b) => threadActivityMs(b) - threadActivityMs(a) || byId(a, b));
sections.snoozed.sort((a, b) => timeMs(a.snoozedUntil) - timeMs(b.snoozedUntil) || byId(a, b));
sections.settled.sort((a, b) => settledMs(b) - settledMs(a) || byId(a, b));
return sections;
}
/** "now", "5m", "3h", "2d", "Mar 4" style age. */
export function formatAge(thenMs: number, nowMs: number): string {
if (!thenMs) return "";
const seconds = Math.max(0, Math.round((nowMs - thenMs) / 1000));
if (seconds < 60) return "now";
const minutes = Math.round(seconds / 60);
if (minutes < 60) return `${minutes}m`;
const hours = Math.round(minutes / 60);
if (hours < 24) return `${hours}h`;
const days = Math.round(hours / 24);
if (days < 14) return `${days}d`;
const date = new Date(thenMs);
const months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
return `${months[date.getMonth()]} ${date.getDate()}`;
}
// ---------------------------------------------------------------------------
// Thread detail (subscribeThread)
const MAX_ITEMS_KEPT = 400;
export class ThreadDetailModel {
/** Last applied sequence (the resume cursor), or null before the first snapshot. */
sequence: number | null = null;
synchronized = false;
deleted = false;
thread: Record<string, any> | null = null;
readonly runs = new Map<string, T3Run>();
readonly attempts = new Map<string, T3RunAttempt>();
readonly runtimeRequests = new Map<string, T3RuntimeRequest>();
readonly messages = new Map<string, T3ConversationMessage>();
readonly plans = new Map<string, T3Plan>();
/** This thread's own timeline rows, by id. */
readonly items = new Map<string, T3TurnItem>();
/** Rows inherited from a forked-from thread: a frozen prefix. */
private inherited: T3TurnItem[] = [];
/** Bounded-window bookkeeping: older history exists that we didn't load. */
hasMoreHistory = false;
private historyCursor: string | null = null;
private latestLocalTurnOrdinal: number | null = null;
constructor(readonly threadId: string) {}
/** Apply one subscribeThread stream item; returns whether anything changed. */
apply(raw: unknown): boolean {
const item = raw as Record<string, any>;
switch (item?.kind) {
case "synchronized":
this.synchronized = true;
return true;
case "snapshot":
this.applySnapshot(item);
return true;
case "event": {
const sequence = typeof item.sequence === "number" ? item.sequence : null;
if (sequence !== null && this.sequence !== null && sequence <= this.sequence) return false;
if (sequence !== null) this.sequence = sequence;
return this.applyEvent(item.event ?? {});
}
default:
return false;
}
}
private applySnapshot(item: Record<string, any>): void {
const projection = item.projection ?? {};
this.sequence = typeof item.snapshotSequence === "number" ? item.snapshotSequence : 0;
this.thread = projection.thread ?? null;
this.deleted = Boolean(this.thread?.deletedAt);
const fill = <T extends { id: string }>(map: Map<string, T>, values: T[] | undefined) => {
map.clear();
for (const value of values ?? []) map.set(value.id, value);
};
fill(this.runs, projection.runs);
fill(this.attempts, projection.attempts);
fill(this.runtimeRequests, projection.runtimeRequests);
fill(this.messages, projection.messages);
fill(this.plans, projection.plans);
this.items.clear();
this.inherited = [];
for (const row of (projection.visibleTurnItems ?? []) as ProjectedTurnItem[]) {
if (!row?.item) continue;
if (row.visibility === "local") this.items.set(row.item.id, row.item);
else this.inherited.push(row.item);
}
this.hasMoreHistory = Boolean(item.hasMoreHistory);
this.historyCursor = typeof item.historyCursor === "string" ? item.historyCursor : null;
this.latestLocalTurnOrdinal = typeof item.latestLocalTurnOrdinal === "number" ? item.latestLocalTurnOrdinal : null;
}
private applyEvent(event: Record<string, any>): boolean {
const payload = event.payload;
const type = String(event.type ?? "");
if (type === "thread.deleted") {
this.deleted = true;
return true;
}
if (type.startsWith("thread.")) {
if (payload && typeof payload === "object") this.thread = payload;
return true;
}
switch (type) {
case "run.created":
case "run.updated":
this.runs.set(payload.id, payload);
return true;
case "run-attempt.created":
case "run-attempt.updated":
this.attempts.set(payload.id, payload);
return true;
case "runtime-request.updated":
this.runtimeRequests.set(payload.id, payload);
return true;
case "message.updated":
this.messages.set(payload.id, payload);
return true;
case "plan.updated":
this.plans.set(payload.id, payload);
return true;
case "turn-item.updated":
return this.upsertItem(payload as T3TurnItem);
default:
// Unknown and uninteresting events still advance the cursor.
return false;
}
}
private upsertItem(item: T3TurnItem): boolean {
if (!item?.id || item.threadId !== this.threadId) return false;
const partial = this.hasMoreHistory || this.historyCursor !== null;
if (!this.items.has(item.id) && partial) {
// Updates to rows from history we never loaded.
if (this.latestLocalTurnOrdinal !== null && item.ordinal <= this.latestLocalTurnOrdinal) return false;
const oldest = this.oldestLocalOrdinal();
if (oldest !== null && item.ordinal < oldest) return false;
}
this.items.set(item.id, item);
if (partial && (this.latestLocalTurnOrdinal === null || item.ordinal > this.latestLocalTurnOrdinal)) {
this.latestLocalTurnOrdinal = item.ordinal;
}
if (this.items.size > MAX_ITEMS_KEPT) this.trimOldest();
return true;
}
private oldestLocalOrdinal(): number | null {
let oldest: number | null = null;
for (const item of this.items.values()) if (oldest === null || item.ordinal < oldest) oldest = item.ordinal;
return oldest;
}
/** A long-lived subscription keeps growing; drop the oldest rows (they're history now). */
private trimOldest(): void {
const sorted = [...this.items.values()].sort(compareItems);
for (const item of sorted.slice(0, sorted.length - MAX_ITEMS_KEPT)) this.items.delete(item.id);
this.hasMoreHistory = true;
this.inherited = [];
}
/** Visible rows in display order (inherited fork history first). */
timeline(): T3TurnItem[] {
const local = [...this.items.values()].filter((item) => this.isVisible(item)).sort(compareItems);
return [...this.inherited, ...local];
}
private isVisible(item: T3TurnItem): boolean {
const run = item.runId ? this.runs.get(item.runId) : undefined;
if (run?.status === "rolled_back") return false;
if (item.type === "user_message" && item.inputIntent === "queued_turn" && run?.status === "cancelled") return false;
if (item.type === "run_interrupt_result" && item.runId && item.nodeId) {
const superseded = [...this.attempts.values()].some(
(attempt) => attempt.runId === item.runId && attempt.rootNodeId === item.nodeId && attempt.status === "superseded",
);
if (superseded) {
const hasRequest = [...this.items.values()].some(
(candidate) => candidate.type === "run_interrupt_request" && candidate.runId === item.runId,
);
if (!hasRequest) return false;
}
}
return true;
}
/** The run a Stop should interrupt, if any. */
activeRunId(): string | null {
let active: T3Run | null = null;
for (const run of this.runs.values()) {
if (["preparing", "starting", "running", "waiting"].includes(run.status)) {
if (!active || timeMs(run.requestedAt) >= timeMs(active.requestedAt)) active = run;
}
}
return active?.id ?? null;
}
/** Pending approvals and questions, oldest first, paired with their timeline rows. */
pendingRequests(): PendingRequest[] {
const result: PendingRequest[] = [];
const items = [...this.items.values()].sort(compareItems);
for (const request of this.runtimeRequests.values()) {
if (request.status !== "pending") continue;
if (request.kind === "auth_refresh" || request.kind === "dynamic_tool_call") continue;
const type = request.kind === "user_input" ? "user_input_request" : "approval_request";
const item = [...items].reverse().find((candidate) => candidate.type === type && candidate.requestId === request.id);
if (!item) continue;
const capability = request.responseCapability?.type ?? "live";
result.push({
request,
item,
kind: request.kind === "user_input" ? "question" : "approval",
answerable: request.kind === "user_input" ? capability !== "not_resumable" : capability === "live",
messageMode: item.responseMode === "message" || capability === "message",
});
}
return result.sort((a, b) => compareItems(a.item, b.item));
}
/** Follow-ups waiting for the active run to finish (they have no timeline row yet). */
queuedMessages(): string[] {
return [...this.runs.values()]
.filter((run) => run.status === "queued" && run.userMessageId)
.sort((a, b) => (a.queuePosition ?? 0) - (b.queuePosition ?? 0))
.map((run) => this.messages.get(run.userMessageId!))
.filter((message): message is T3ConversationMessage => Boolean(message && !message.delegatedCompletion && !message.notification))
.map((message) => message.text ?? "");
}
/** The current todo list (the latest run's, else the newest one). */
todoList(): T3Plan | null {
const todos = [...this.plans.values()].filter((plan) => plan.kind === "todo_list" && plan.steps?.length);
if (!todos.length) return null;
const latestRunId = this.thread?.latestRunId ?? null;
return todos.find((plan) => plan.runId === latestRunId) ?? todos[todos.length - 1]!;
}
}
export type PendingRequest = {
request: T3RuntimeRequest;
item: T3TurnItem;
kind: "approval" | "question";
/** False when the provider session can't take an answer any more (interrupt instead). */
answerable: boolean;
/** Message-mode questions: answers must be non-empty strings. */
messageMode: boolean;
};
function compareItems(a: T3TurnItem, b: T3TurnItem): number {
return a.ordinal - b.ordinal || String(a.id).localeCompare(String(b.id));
}
// ---------------------------------------------------------------------------
// Timeline presentation
export type TimelineEntry =
| { kind: "user"; id: string; text: string; queued?: boolean }
| { kind: "assistant"; id: string; text: string; streaming: boolean }
| { kind: "activity"; id: string; label: string; detail: string; state: ActivityState }
| { kind: "request"; id: string; label: string; detail: string; resolved: boolean };
export type ActivityState = "running" | "done" | "failed" | "stopped";
function activityState(item: T3TurnItem): ActivityState {
switch (item.status) {
case "pending":
case "running":
case "waiting":
return "running";
case "failed":
return "failed";
case "cancelled":
case "interrupted":
return "stopped";
default:
return "done";
}
}
function capitalize(text: string): string {
return text ? text[0]!.toUpperCase() + text.slice(1) : text;
}
function oneLine(text: string | null | undefined): string {
return (text ?? "").replace(/\s+/g, " ").trim();
}
function baseName(path: string): string {
return path.split(/[\\/]/).filter(Boolean).pop() ?? path;
}
/** `bash -lc '<script>'` → `<script>` (what T3's work log shows). */
export function commandDisplayText(command: string): string {
const trimmed = command.trim();
const match = /^(?:\S*\/)?(?:ba|z|da)?sh\s+-l?c\s+(['"])([\s\S]*)\1$/.exec(trimmed);
return match ? match[2]!.trim() || trimmed : trimmed;
}
/** One-line label and optional detail for a non-message timeline row (null to hide it). */
export function summarizeActivity(item: T3TurnItem): { label: string; detail: string } | null {
const title = oneLine(item.title);
switch (item.type) {
case "command_execution": {
const input = typeof item.input === "string" ? oneLine(commandDisplayText(item.input)) : "";
if (input === "Preparing workspace") return null;
return { label: input || title || "Command", detail: "" };
}
case "file_change": {
const count = item.changes?.length ?? 0;
const label = title || (count > 1 ? `Changed ${count} files` : `Changed ${baseName(item.fileName ?? "a file")}`);
const stats = item.additions != null || item.deletions != null ? `+${item.additions ?? 0}/-${item.deletions ?? 0}` : "";
return { label: capitalize(label), detail: stats };
}
case "file_search":
return { label: capitalize(title || "Searched files"), detail: oneLine(item.pattern) };
case "web_search":
return { label: capitalize(title || "Searched the web"), detail: oneLine((item.patterns ?? []).join(", ")) };
case "dynamic_tool":
return { label: capitalize(title || oneLine(item.toolName) || "Tool call"), detail: "" };
case "reasoning":
return { label: activityState(item) === "running" ? "Thinking" : "Thought", detail: "" };
case "subagent":
return { label: capitalize(title || "Subagent"), detail: "" };
case "notification":
return { label: oneLine(item.summary) || "Notification", detail: oneLine(item.detail) };
case "system_notice":
return { label: oneLine(item.message) || "Notice", detail: "" };
case "error":
if (item.status === "cancelled" && item.failure?.code === "workspace_preparation_failed") return null;
return {
label: item.failure?.class === "usage_limit" ? "Usage limit reached" : "Provider error",
detail: oneLine(item.failure?.message),
};
case "run_interrupt_result":
return { label: "Run interrupted", detail: oneLine(item.message) };
case "compaction":
return item.beforeTokenCount != null && item.afterTokenCount != null
? { label: `Context compacted ${item.beforeTokenCount} → ${item.afterTokenCount} tokens`, detail: "" }
: { label: "Compacting context", detail: "" };
case "handoff":
return { label: "Context handed off", detail: oneLine(item.toModel) };
case "fork":
return { label: "Thread forked", detail: "" };
case "thread_created":
return { label: "Thread created", detail: "" };
case "proposed_plan":
return { label: "Proposed plan", detail: oneLine(item.markdown).slice(0, 200) };
case "todo_list":
case "checkpoint":
case "run_interrupt_request":
return null;
default:
return title ? { label: capitalize(title), detail: "" } : null;
}
}
/** Approval / question rows: what's being asked. */
export function describeRequest(item: T3TurnItem): { label: string; detail: string } {
if (item.type === "user_input_request") {
const questions = (item.questions ?? []).map((question) => oneLine(question.question)).filter(Boolean);
return { label: "Question", detail: questions.join(" · ") };
}
const what = item.requestKind === "file-change" ? "Approve file change" : item.requestKind === "command" ? "Approve command" : "Approval requested";
return { label: item.appName ? `${what} (${item.appName})` : what, detail: oneLine(item.prompt) };
}
/** Timeline rows → display entries (messages, activities, requests), plus queued follow-ups at the end. */
export function timelineEntries(detail: ThreadDetailModel): TimelineEntry[] {
const entries: TimelineEntry[] = [];
for (const item of detail.timeline()) {
switch (item.type) {
case "user_message":
entries.push({ kind: "user", id: item.id, text: item.text ?? "" });
break;
case "assistant_message":
if ((item.text ?? "").trim() || item.streaming) {
entries.push({ kind: "assistant", id: item.id, text: item.text ?? "", streaming: Boolean(item.streaming) });
}
break;
case "approval_request":
case "user_input_request": {
const { label, detail: what } = describeRequest(item);
entries.push({ kind: "request", id: item.id, label, detail: what, resolved: activityState(item) !== "running" });
break;
}
default: {
const summary = summarizeActivity(item);
if (summary) entries.push({ kind: "activity", id: item.id, ...summary, state: activityState(item) });
}
}
}
detail.queuedMessages().forEach((text, index) => entries.push({ kind: "user", id: `queued:${index}`, text, queued: true }));
return entries;
}
/**
* Light Markdown cleanup for a monochrome text view: drop code fences and
* emphasis markers, show links as their text, turn list markers into bullets.
*/
export function plainMarkdown(text: string): string {
return text
.replace(/\r/g, "")
.split("\n")
.filter((line) => !/^\s*```/.test(line))
.map((line) =>
line
.replace(/^(\s*)#{1,6}\s+/, "$1")
.replace(/^(\s*)[-*+]\s+/, "$1· ")
.replace(/\[([^\]]+)\]\([^)]*\)/g, "$1")
.replace(/(\*\*|__)(.+?)\1/g, "$2")
.replace(/`([^`]+)`/g, "$1"),
)
.join("\n")
.replace(/\n{3,}/g, "\n\n")
.trim();
}
// ---------------------------------------------------------------------------
// Commands (orchestration.dispatchCommand payloads)
/** RFC 4122 v4 UUID; T3 clients use these for command, message and thread ids. */
export function uuidv4(random: () => number = Math.random): string {
const hex = "0123456789abcdef";
let out = "";
for (let i = 0; i < 36; i++) {
if (i === 8 || i === 13 || i === 18 || i === 23) out += "-";
else if (i === 14) out += "4";
else if (i === 19) out += hex[(Math.floor(random() * 16) & 0x3) | 0x8];
else out += hex[Math.floor(random() * 16)];
}
return out;
}
/**
* A follow-up message. While a run is active the server queues it; with
* steer set, the server steers the active turn when the provider supports it
* (and otherwise queues or interrupt-restarts), like T3's own clients.
*/
export function messageDispatchCommand(threadId: string, text: string, options: { steer?: boolean } = {}): Record<string, unknown> {
return {
type: "message.dispatch",
commandId: uuidv4(),
createdBy: "user",
creationSource: "mobile",
threadId,
messageId: uuidv4(),
text,
attachments: [],
dispatchMode: { type: "start_immediately" },
...(options.steer ? { deliveryIntent: "auto" } : {}),
};
}
export function approvalResponseCommand(threadId: string, requestId: string, decision: string): Record<string, unknown> {
return { type: "runtime-request.respond", commandId: uuidv4(), threadId, requestId, decision };
}
export function questionAnswerCommand(threadId: string, requestId: string, answers: Record<string, string | string[]>): Record<string, unknown> {
return { type: "runtime-request.respond", commandId: uuidv4(), threadId, requestId, answers };
}
export function interruptCommand(threadId: string, runId: string): Record<string, unknown> {
// holdQueue (as T3's clients send it): queued follow-ups wait instead of starting.
return { type: "run.interrupt", commandId: uuidv4(), threadId, runId, holdQueue: true };
}
export function visitCommand(threadId: string, visitedAt: string): Record<string, unknown> {
return { type: "thread.visit", commandId: uuidv4(), threadId, visitedAt };
}
export function settleCommand(threadId: string, settle: boolean): Record<string, unknown> {
return settle
? { type: "thread.settle", commandId: uuidv4(), threadId }
: { type: "thread.unsettle", commandId: uuidv4(), threadId, reason: "user" };
}
/** The approval choices to offer: the request's own, else T3 mobile's defaults. */
export function approvalOptions(item: T3TurnItem): T3ApprovalOption[] {
if (item.options?.length) return item.options;
return [
{ decision: "accept", label: "Allow once" },
{ decision: "acceptForSession", label: "Allow for session" },
{ decision: "decline", label: "Decline" },
];
}
/** What an option answers a question with: its value if it has one, else its label. */
export function optionAnswer(option: { label: string; value?: string }): string {
return option.value ?? option.label;
}
// ---------------------------------------------------------------------------
// New threads
export type T3ServerProvider = {
instanceId: string;
displayName?: string;
driver?: string;
enabled?: boolean;
installed?: boolean;
auth?: { status?: string };
models?: Array<{ slug: string; name?: string; isDefault?: boolean; isLegacy?: boolean }>;
};
function providerUsable(provider: T3ServerProvider): boolean {
return provider.enabled !== false && provider.installed !== false && provider.auth?.status !== "unauthenticated";
}
export function providerDisplayName(providers: readonly T3ServerProvider[], instanceId: string | null | undefined): string {
if (!instanceId) return "";
const provider = providers.find((candidate) => candidate.instanceId === instanceId);
return provider?.displayName || provider?.driver || instanceId;
}
/**
* Model for a new thread, the way T3's mobile app defaults it: project
* override, environment default, the project's legacy default, then the
* first usable provider's default model, then any usable model.
*/
export function defaultModelSelection(
settings: Record<string, any> | null,
providers: readonly T3ServerProvider[],
project: T3ProjectShell,
): T3ModelSelection | null {
const usable = (selection: T3ModelSelection | null | undefined): selection is T3ModelSelection => {
if (!selection?.instanceId || !selection.model) return false;
const provider = providers.find((candidate) => candidate.instanceId === selection.instanceId);
return provider ? providerUsable(provider) : providers.length === 0;
};
const candidates = [
settings?.projectSettingsOverrides?.[project.id]?.defaultModelSelection,
settings?.defaultModelSelection,
project.defaultModelSelection,
];
for (const candidate of candidates) if (usable(candidate)) return candidate;
for (const pickDefault of [true, false]) {
for (const provider of providers) {
if (!providerUsable(provider)) continue;
const model = (provider.models ?? []).find((entry) => !entry.isLegacy && (!pickDefault || entry.isDefault));
if (model) return { instanceId: provider.instanceId, model: model.slug };
}
}
return null;
}
/** First line of a prompt as a provisional title (the server generates a better one). */
export function provisionalTitle(text: string): string {
const collapsed = oneLine(text);
if (!collapsed) return "New thread";
return collapsed.length > 72 ? `${collapsed.slice(0, 71).trimEnd()}…` : collapsed;
}
export function launchThreadPayload(
project: T3ProjectShell,
text: string,
modelSelection: T3ModelSelection,
settings: Record<string, any> | null,
): Record<string, unknown> {
const runtimeMode = settings?.projectSettingsOverrides?.[project.id]?.defaultRuntimeMode ?? settings?.defaultRuntimeMode ?? "full-access";
return {
commandId: uuidv4(),
creationSource: "mobile",
threadId: uuidv4(),
projectId: project.id,
title: provisionalTitle(text),
generateTitle: true,
modelSelection,
runtimeMode,
interactionMode: "default",
workspaceStrategy: { type: "root" },
initialMessage: { messageId: uuidv4(), text, attachments: [] },
};
}
+157
View File
@@ -0,0 +1,157 @@
/**
* Pairing-link parsing for T3 Code environments, mirroring T3's own
* resolveRemotePairingTarget (packages/shared/src/remote.ts) without URL /
* URLSearchParams (the NativeScript runtime's URLSearchParams is unreliable).
*
* Accepted forms:
* - direct link from `t3 serve` / `t3 pair`: http://192.168.1.20:3773/pair#token=7JBMA3AYC28V
* (the token may also be a ?token= query param)
* - hosted link: https://app.t3.codes/pair?host=<backend url>&label=...#token=...
* - T3 mobile QR payload: t3code://...?pairingUrl=<url-encoded pairing link>
* - "<host[:port]> <code>" typed by hand (http for IP literals and localhost,
* https otherwise, like T3's mobile app)
*/
export type PairingTarget = {
/** One-time pairing credential (12 characters from `t3 pair`). */
credential: string;
/** Backend origin with a trailing slash, e.g. "http://192.168.1.20:3773/". */
httpBaseUrl: string;
/** The same origin as ws:// or wss://. */
wsBaseUrl: string;
/** Label from a hosted link, if any. */
label: string;
};
export type PairingParseResult = { ok: true; target: PairingTarget } | { ok: false; error: string };
type ParsedUrl = {
scheme: string;
/** host[:port], brackets kept for IPv6. */
authority: string;
query: Map<string, string>;
fragment: Map<string, string>;
};
const URL_PATTERN = /^([a-zA-Z][a-zA-Z\d+.-]*):\/\/([^/?#\s]*)([^?#\s]*)(?:\?([^#\s]*))?(?:#(\S*))?$/;
function decodeComponent(value: string): string {
try {
return decodeURIComponent(value.replace(/\+/g, " "));
} catch {
return value;
}
}
function parseParams(text: string | undefined): Map<string, string> {
const params = new Map<string, string>();
if (!text) return params;
for (const part of text.split("&")) {
if (!part) continue;
const eq = part.indexOf("=");
const key = decodeComponent(eq < 0 ? part : part.slice(0, eq));
const value = eq < 0 ? "" : decodeComponent(part.slice(eq + 1));
if (!params.has(key)) params.set(key, value);
}
return params;
}
function parseUrl(text: string): ParsedUrl | null {
const match = URL_PATTERN.exec(text.trim());
if (!match) return null;
// Drop any userinfo; T3 links never carry it.
const authority = match[2]!.replace(/^.*@/, "");
if (!authority) return null;
return {
scheme: match[1]!.toLowerCase(),
authority,
query: parseParams(match[4]),
fragment: parseParams(match[5]),
};
}
/** Lower-case the host (not the port) and drop default ports, like URL does. */
function normalizeAuthority(scheme: "http" | "https", authority: string): string {
const match = /^(\[[^\]]+\]|[^:]+)(?::(\d+))?$/.exec(authority);
if (!match) return authority.toLowerCase();
const host = match[1]!.toLowerCase();
const port = match[2];
if (!port || (scheme === "http" && port === "80") || (scheme === "https" && port === "443")) return host;
return `${host}:${port}`;
}
function bases(scheme: string, authority: string): { httpBaseUrl: string; wsBaseUrl: string } | null {
const httpScheme = scheme === "ws" ? "http" : scheme === "wss" ? "https" : scheme;
if (httpScheme !== "http" && httpScheme !== "https") return null;
const normalized = normalizeAuthority(httpScheme, authority);
return {
httpBaseUrl: `${httpScheme}://${normalized}/`,
wsBaseUrl: `${httpScheme === "https" ? "wss" : "ws"}://${normalized}/`,
};
}
function isIpLiteralOrLocal(host: string): boolean {
const hostname = host.replace(/:\d+$/, "").replace(/^\[|\]$/g, "");
if (hostname.includes(":")) return true;
if (hostname.toLowerCase() === "localhost") return true;
const octets = hostname.split(".");
return octets.length === 4 && octets.every((octet) => /^\d{1,3}$/.test(octet) && Number(octet) <= 255);
}
/** The backend named by a hosted link's host param, which may omit its scheme. */
function hostedBackend(host: string): { httpBaseUrl: string; wsBaseUrl: string } | null {
const trimmed = host.trim().replace(/^\/+/, "");
const withScheme = /^[a-zA-Z][a-zA-Z\d+.-]*:\/\//.test(trimmed) ? trimmed : `https://${trimmed}`;
const parsed = parseUrl(withScheme);
return parsed ? bases(parsed.scheme, parsed.authority) : null;
}
const NOT_A_LINK = "Paste the pairing link from `t3 pair` (it looks like http://<host>:<port>/pair#token=...).";
export function parsePairingInput(input: string): PairingParseResult {
let text = input.trim();
if (!text) return { ok: false, error: NOT_A_LINK };
// T3 mobile's QR payload wraps the real pairing link.
const wrapped = parseUrl(text);
if (wrapped?.scheme === "t3code") {
const inner = wrapped.query.get("pairingUrl")?.trim();
if (!inner) return { ok: false, error: "That t3code:// link has no pairing URL in it." };
text = inner;
}
// "host[:port] CODE" typed by hand.
const pair = /^(\S+)\s+([A-Za-z0-9]{6,})$/.exec(text);
if (pair && !pair[1]!.includes("#")) {
const hostText = pair[1]!;
const withScheme = hostText.includes("://") ? hostText : `${isIpLiteralOrLocal(hostText) ? "http" : "https"}://${hostText}`;
const parsed = parseUrl(withScheme);
const resolved = parsed && bases(parsed.scheme, parsed.authority);
if (!resolved) return { ok: false, error: NOT_A_LINK };
return { ok: true, target: { credential: pair[2]!, label: "", ...resolved } };
}
const url = parseUrl(text);
if (!url) return { ok: false, error: NOT_A_LINK };
if (!["http", "https", "ws", "wss"].includes(url.scheme)) {
return { ok: false, error: `Unsupported link type "${url.scheme}:".` };
}
const token = (url.fragment.get("token") || url.query.get("token") || "").trim();
const hostParam = url.query.get("host")?.trim() ?? "";
if (hostParam && token) {
const backend = hostedBackend(hostParam);
if (!backend) return { ok: false, error: `Can't read the server address "${hostParam}" in that link.` };
return { ok: true, target: { credential: token, label: url.query.get("label")?.trim() ?? "", ...backend } };
}
if (!token) {
return { ok: false, error: "That link has no pairing token. Run `t3 pair` on the server for a fresh link." };
}
const resolved = bases(url.scheme, url.authority);
if (!resolved) return { ok: false, error: NOT_A_LINK };
return { ok: true, target: { credential: token, label: "", ...resolved } };
}
/** "host:port" for display (never includes credentials). */
export function baseUrlLabel(httpBaseUrl: string): string {
return httpBaseUrl.replace(/^[a-z]+:\/\//, "").replace(/\/$/, "");
}
+341
View File
@@ -0,0 +1,341 @@
/**
* Minimal client for T3 Code's WebSocket RPC (Effect RPC, JSON
* serialization), without the Effect library. One instance owns one socket;
* reconnecting means making a new instance (the caller owns retry policy).
*
* Wire format (one JSON message per text frame; the server also accepts an
* array of messages in one frame):
* -> {"_tag":"Request","id":N,"tag":"<method>","payload":{...},"headers":[]}
* -> {"_tag":"Ack","requestId":N} after EVERY Chunk, or the stream stalls
* -> {"_tag":"Interrupt","requestId":N} cancel a call or subscription
* -> {"_tag":"Ping"} keepalive; the server only answers
* <- {"_tag":"Chunk","requestId":N,"values":[...]}
* <- {"_tag":"Exit","requestId":N,"exit":{"_tag":"Success","value":...}}
* <- {"_tag":"Exit","requestId":N,"exit":{"_tag":"Failure","cause":[{"_tag":"Fail","error":...}|{"_tag":"Die","defect":...}|{"_tag":"Interrupt"}]}}
* <- {"_tag":"Defect","defect":...} connection-level; fails every pending call
* <- {"_tag":"Pong"}
*
* The server never pings and has no idle timeout, so dead sockets are found
* the way T3's own client finds them: a Ping every 5s, and three unanswered
* in a row close the connection.
*/
export type RpcSocketHandlers = {
onOpen(): void;
onMessage(text: string): void;
/** The socket closed or failed; called at most once. */
onClose(reason: string): void;
};
export type RpcSocket = {
send(text: string): void;
close(): void;
};
export type RpcSocketFactory = (url: string, handlers: RpcSocketHandlers) => RpcSocket;
export type T3RpcErrorKind =
/** A typed failure from the handler (error carries its `_tag`). */
| "fail"
/** A defect: unknown method, payload decode failure, handler crash. */
| "die"
/** The call or subscription was interrupted (by us or the server). */
| "interrupt"
/** The socket closed (or never opened) before the call finished. */
| "disconnected";
export class T3RpcError extends Error {
constructor(
readonly kind: T3RpcErrorKind,
message: string,
/** For "fail": the typed error object as sent (has a `_tag`). */
readonly error: unknown = null,
) {
super(message);
this.name = "T3RpcError";
}
/** The typed error's `_tag` (e.g. "EnvironmentAuthorizationError"), for "fail" errors. */
get errorTag(): string | null {
const tag = (this.error as { _tag?: unknown } | null)?._tag;
return typeof tag === "string" ? tag : null;
}
}
export type T3RpcState = "connecting" | "open" | "closed";
export type T3RpcOptions = {
url: string;
openSocket: RpcSocketFactory;
pingIntervalMs?: number;
maxMissedPongs?: number;
/** Close the attempt if the socket hasn't opened by then. */
openTimeoutMs?: number;
};
export type T3Subscription = {
/** Interrupt the stream server-side and stop delivering values. */
cancel(): void;
};
type PendingCall = {
kind: "call";
resolve: (value: unknown) => void;
reject: (error: T3RpcError) => void;
};
type PendingStream = {
kind: "stream";
onValues: (values: unknown[]) => void;
/** Called once: null for a successful end, else why it stopped. */
onEnd: (error: T3RpcError | null) => void;
};
const DEFAULT_PING_INTERVAL_MS = 5_000;
const DEFAULT_MAX_MISSED_PONGS = 3;
const DEFAULT_OPEN_TIMEOUT_MS = 15_000;
export class T3RpcConnection {
private readonly socket: RpcSocket;
private state: T3RpcState = "connecting";
private closeReason = "";
private nextId = 0;
private readonly pending = new Map<number, PendingCall | PendingStream>();
private readonly stateListeners = new Set<(state: T3RpcState, reason: string) => void>();
private pingTimer: ReturnType<typeof setInterval> | null = null;
private openTimer: ReturnType<typeof setTimeout> | null = null;
private missedPongs = 0;
/** Frames sent while still connecting, flushed on open. */
private outbox: string[] = [];
constructor(private readonly options: T3RpcOptions) {
this.openTimer = setTimeout(() => this.shutdown("Timed out connecting."), options.openTimeoutMs ?? DEFAULT_OPEN_TIMEOUT_MS);
this.socket = options.openSocket(options.url, {
onOpen: () => this.handleOpen(),
onMessage: (text) => this.handleFrame(text),
onClose: (reason) => this.shutdown(reason || "Connection closed."),
});
}
getState(): T3RpcState {
return this.state;
}
/** Why the connection closed ("" while connecting or open). */
getCloseReason(): string {
return this.closeReason;
}
onStateChange(listener: (state: T3RpcState, reason: string) => void): () => void {
this.stateListeners.add(listener);
return () => this.stateListeners.delete(listener);
}
/** A unary call; rejects with T3RpcError. Calls made while connecting go out once the socket opens. */
call<T = unknown>(tag: string, payload: unknown = {}): Promise<T> {
if (this.state === "closed") {
return Promise.reject(new T3RpcError("disconnected", this.closeReason || "Not connected."));
}
const id = this.nextId++;
return new Promise<T>((resolve, reject) => {
this.pending.set(id, { kind: "call", resolve: resolve as (value: unknown) => void, reject });
this.send({ _tag: "Request", id, tag, payload, headers: [] });
});
}
/**
* A streaming subscription. Every chunk is acked as soon as it is
* delivered. onEnd fires exactly once (unless cancel() comes first): null
* when the stream completed, else the error that stopped it (including
* "disconnected" when the socket drops).
*/
subscribe(
tag: string,
payload: unknown,
onValues: (values: unknown[]) => void,
onEnd: (error: T3RpcError | null) => void,
): T3Subscription {
if (this.state === "closed") {
const error = new T3RpcError("disconnected", this.closeReason || "Not connected.");
setTimeout(() => onEnd(error), 0);
return { cancel: () => {} };
}
const id = this.nextId++;
this.pending.set(id, { kind: "stream", onValues, onEnd });
this.send({ _tag: "Request", id, tag, payload, headers: [] });
return {
cancel: () => {
if (!this.pending.delete(id)) return;
if (this.state !== "closed") this.send({ _tag: "Interrupt", requestId: id });
},
};
}
/** Close the socket; pending calls fail as "disconnected". */
close(reason = "Closed."): void {
this.shutdown(reason);
}
private handleOpen(): void {
if (this.state !== "connecting") return;
this.clearOpenTimer();
this.state = "open";
this.missedPongs = 0;
this.pingTimer = setInterval(() => this.ping(), this.options.pingIntervalMs ?? DEFAULT_PING_INTERVAL_MS);
const queued = this.outbox;
this.outbox = [];
for (const text of queued) this.sendText(text);
this.emitState();
}
private ping(): void {
if (this.state !== "open") return;
if (this.missedPongs >= (this.options.maxMissedPongs ?? DEFAULT_MAX_MISSED_PONGS)) {
this.shutdown("Server stopped responding.");
return;
}
this.missedPongs++;
this.send({ _tag: "Ping" });
}
private handleFrame(text: string): void {
if (this.state === "closed") return;
let parsed: unknown;
try {
parsed = JSON.parse(text);
} catch {
return;
}
const messages = Array.isArray(parsed) ? parsed : [parsed];
for (const message of messages) {
// A handler may have closed the connection mid-batch.
if (this.getState() === "closed") return;
this.handleMessage(message as Record<string, unknown>);
}
}
private handleMessage(message: Record<string, unknown>): void {
// Any traffic proves the socket is alive.
this.missedPongs = 0;
switch (message?._tag) {
case "Pong":
return;
case "Chunk": {
const requestId = message.requestId as number;
const entry = this.pending.get(requestId);
// Ack even for a stream we've cancelled: the server blocks the
// stream on it until the interrupt lands.
this.send({ _tag: "Ack", requestId });
if (entry?.kind !== "stream") return;
const values = Array.isArray(message.values) ? (message.values as unknown[]) : [];
try {
entry.onValues(values);
} catch (error) {
// (console via globalThis: this file also builds for Node tests without DOM typings.)
(globalThis as { console?: { error(message: string): void } }).console?.error(`t3 rpc: stream handler failed: ${error}`);
}
return;
}
case "Exit": {
const requestId = message.requestId as number;
const entry = this.pending.get(requestId);
if (!entry) return;
this.pending.delete(requestId);
const exit = message.exit as { _tag?: string; value?: unknown; cause?: unknown } | undefined;
const error = exit?._tag === "Success" ? null : causeToError(exit?.cause);
if (entry.kind === "call") {
if (error) entry.reject(error);
else entry.resolve(exit?.value ?? null);
} else {
entry.onEnd(error);
}
return;
}
case "Defect":
this.failAll(new T3RpcError("die", `Server defect: ${describeDefect(message.defect)}`));
return;
default:
return;
}
}
private send(message: unknown): void {
if (this.state === "closed") return;
const text = JSON.stringify(message);
if (this.state === "connecting") this.outbox.push(text);
else this.sendText(text);
}
private sendText(text: string): void {
try {
this.socket.send(text);
} catch (error) {
this.shutdown(`Send failed: ${error}`);
}
}
private failAll(error: T3RpcError): void {
const entries = [...this.pending.values()];
this.pending.clear();
for (const entry of entries) {
if (entry.kind === "call") entry.reject(error);
else entry.onEnd(error);
}
}
private shutdown(reason: string): void {
if (this.state === "closed") return;
this.state = "closed";
this.closeReason = reason;
this.outbox = [];
this.clearOpenTimer();
if (this.pingTimer) clearInterval(this.pingTimer);
this.pingTimer = null;
try {
this.socket.close();
} catch {
// Already gone.
}
this.failAll(new T3RpcError("disconnected", reason));
this.emitState();
}
private clearOpenTimer(): void {
if (this.openTimer) clearTimeout(this.openTimer);
this.openTimer = null;
}
private emitState(): void {
for (const listener of [...this.stateListeners]) listener(this.state, this.closeReason);
}
}
/** Map an Exit failure cause (array of Fail/Die/Interrupt reasons) to one error, preferring typed failures. */
export function causeToError(cause: unknown): T3RpcError {
const reasons = Array.isArray(cause) ? (cause as Array<Record<string, unknown>>) : [];
const fail = reasons.find((reason) => reason?._tag === "Fail");
if (fail) return new T3RpcError("fail", describeTypedError(fail.error), fail.error);
const die = reasons.find((reason) => reason?._tag === "Die");
if (die) return new T3RpcError("die", describeDefect(die.defect));
if (reasons.some((reason) => reason?._tag === "Interrupt")) return new T3RpcError("interrupt", "Interrupted.");
return new T3RpcError("die", "Request failed.");
}
function describeTypedError(error: unknown): string {
if (error && typeof error === "object") {
const record = error as Record<string, unknown>;
if (typeof record.message === "string" && record.message) return record.message;
if (typeof record.detail === "string" && record.detail) return record.detail;
if (typeof record._tag === "string") return record._tag;
}
return String(error);
}
function describeDefect(defect: unknown): string {
if (typeof defect === "string") return defect.split("\n")[0]!;
if (defect && typeof defect === "object") {
const record = defect as Record<string, unknown>;
if (typeof record.message === "string") return record.message;
}
return String(defect);
}
+132
View File
@@ -0,0 +1,132 @@
/**
* Lays a T3 thread's timeline out as wrapped, shaded text lines for the
* glasses: user messages dim with a "You:" lead, assistant replies bright
* (light Markdown cleanup), tool activity as dim one-liners, approvals and
* questions highlighted. Long runs of tool activity collapse to a count plus
* the latest few steps, so the conversation stays readable.
*/
import { wrapText, type WrapFont } from "../../graphics/textwrap";
import { plainMarkdown, type TimelineEntry } from "./t3-model";
export type TranscriptLine = {
text: string;
/** Gray level 0..255. */
value: number;
/** Left inset in pixels. */
indent: number;
};
/** Activity runs longer than this collapse to a count plus the last ACTIVITY_TAIL. */
const ACTIVITY_COLLAPSE_AT = 4;
const ACTIVITY_TAIL = 2;
const ACTIVITY_INDENT = 10;
const USER_VALUE = 170;
const ASSISTANT_VALUE = 240;
const ACTIVITY_VALUE = 120;
const REQUEST_VALUE = 255;
const RESOLVED_REQUEST_VALUE = 140;
type Block = { key: string; build: () => TranscriptLine[] };
/** Per-entry wrap cache: re-wrapping a long transcript on every streamed update is wasted work. */
export class TranscriptLayout {
private cache = new Map<string, TranscriptLine[]>();
private font: WrapFont | null = null;
private width = 0;
layout(entries: readonly TimelineEntry[], font: WrapFont, width: number): TranscriptLine[] {
if (font !== this.font || width !== this.width) {
this.cache.clear();
this.font = font;
this.width = width;
}
const next = new Map<string, TranscriptLine[]>();
const lines: TranscriptLine[] = [];
for (const block of blocks(entries, font, width)) {
let built = this.cache.get(block.key);
if (!built) built = block.build();
next.set(block.key, built);
if (lines.length) lines.push({ text: "", value: 0, indent: 0 });
lines.push(...built);
}
this.cache = next;
return lines;
}
}
function wrapped(font: WrapFont, text: string, width: number, value: number, indent = 0): TranscriptLine[] {
return wrapText(font, text, Math.max(1, width - indent)).map((line) => ({ text: line, value, indent }));
}
function activityLine(font: WrapFont, entry: Extract<TimelineEntry, { kind: "activity" }>, width: number): TranscriptLine[] {
const suffix = entry.state === "running" ? " …" : entry.state === "failed" ? " (failed)" : entry.state === "stopped" ? " (stopped)" : "";
const text = `· ${entry.label}${entry.detail ? ` ${entry.detail}` : ""}${suffix}`;
// One line each: activity is context, not content.
const [first] = wrapText(font, text, Math.max(1, width - ACTIVITY_INDENT));
const line = first ?? "";
const truncated = line.length < text.length ? `${line.replace(/\s+$/, "")}…` : line;
return [{ text: truncated, value: entry.state === "failed" ? 170 : ACTIVITY_VALUE, indent: ACTIVITY_INDENT }];
}
function blocks(entries: readonly TimelineEntry[], font: WrapFont, width: number): Block[] {
const result: Block[] = [];
let index = 0;
while (index < entries.length) {
const entry = entries[index]!;
if (entry.kind === "activity") {
// A run of consecutive activity rows forms one block.
const run: Array<Extract<TimelineEntry, { kind: "activity" }>> = [];
while (index < entries.length && entries[index]!.kind === "activity") {
run.push(entries[index] as Extract<TimelineEntry, { kind: "activity" }>);
index++;
}
const key = `activity:${run.map((item) => `${item.id}|${item.state}|${item.label}|${item.detail}`).join("\n")}`;
result.push({
key,
build: () => {
if (run.length < ACTIVITY_COLLAPSE_AT) return run.flatMap((item) => activityLine(font, item, width));
const hidden = run.length - ACTIVITY_TAIL;
const failed = run.slice(0, hidden).filter((item) => item.state === "failed").length;
return [
{ text: `· ${hidden} earlier steps${failed ? ` (${failed} failed)` : ""}`, value: ACTIVITY_VALUE - 20, indent: ACTIVITY_INDENT },
...run.slice(hidden).flatMap((item) => activityLine(font, item, width)),
];
},
});
continue;
}
index++;
switch (entry.kind) {
case "user":
result.push({
key: `user:${entry.id}:${entry.text}`,
build: () => wrapped(font, `${entry.queued ? "You (queued)" : "You"}: ${entry.text.trim()}`, width, USER_VALUE),
});
break;
case "assistant":
result.push({
key: `assistant:${entry.id}:${entry.streaming}:${entry.text}`,
build: () => {
const text = plainMarkdown(entry.text);
const lines = text ? wrapped(font, text, width, ASSISTANT_VALUE) : [];
if (entry.streaming) lines.push({ text: "…", value: ASSISTANT_VALUE, indent: 0 });
return lines;
},
});
break;
case "request":
result.push({
key: `request:${entry.id}:${entry.resolved}:${entry.detail}`,
build: () => {
const value = entry.resolved ? RESOLVED_REQUEST_VALUE : REQUEST_VALUE;
const lines = wrapped(font, `${entry.resolved ? "" : "→ "}${entry.label}${entry.resolved ? " (answered)" : ""}`, width, value);
if (entry.detail) lines.push(...wrapped(font, entry.detail, width, entry.resolved ? ACTIVITY_VALUE : 210, ACTIVITY_INDENT));
return lines;
},
});
break;
}
}
return result;
}
File diff suppressed because it is too large. Load diff
+6 -3
View File
@@ -1,11 +1,14 @@
import { wrapText, type WrapFont } from '../../graphics/textwrap';
/** Left edge of the hub's title line (and of a wrapped status block under it). */
export const HUB_TITLE_X = 12;
/** Keep short status on the title row; give long errors their full width. */
export function layoutHubHeader(font: WrapFont, title: string, status: string, width: number, step: number) {
const inlineX = 18 + font.measureText(title) + 16;
const inlineX = HUB_TITLE_X + font.measureText(title) + 16;
const inline = !/[\r\n]/.test(status) && font.measureText(status) <= Math.max(0, width - inlineX - 12);
const lines = inline ? [status] : wrapText(font, status, Math.max(1, width - 30));
const x = inline ? inlineX : 18;
const lines = inline ? [status] : wrapText(font, status, Math.max(1, width - HUB_TITLE_X - 12));
const x = inline ? inlineX : HUB_TITLE_X;
const y = inline ? 10 : 10 + step;
return { x, y, lines, listTop: inline ? 16 + step : 16 + step * (1 + lines.length) };
}
+131 -63
View File
@@ -1,9 +1,10 @@
/**
* 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; selecting a host's heading launches a shell
* there), and hosts the Manage Connections section
* live g2mirror sessions grouped under a heading per configured host
* (unconnected hosts show their state and a Connect row; selecting a
* 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
@@ -23,7 +24,7 @@
* 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
* Auto-reconnect (window menu > Settings, 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
@@ -38,8 +39,7 @@ 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 { layoutHubHeader } from "./hub-header";
import { truncateText } from "../../graphics/textwrap";
import { HUB_TITLE_X, layoutHubHeader } from "./hub-header";
import { TERMINAL_ICON_GLYPHS, type IconActivity } from "../../graphics/icons";
import {
drawSessionRow,
@@ -52,21 +52,31 @@ 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 {
enumSettingMenuItem,
terminalAutoReconnectSetting,
terminalDisplayModeSetting,
terminalLaunchPresetsSetting,
terminalNewConnectionSetting,
terminalVerticalPositionSetting,
terminalWakeOnBellSetting,
textSettingMenuItem,
toggleSettingMenuItem,
} from "../../ui/dashboard-settings";
import { terminalFontPickerMenuItem } from "../../ui/font-picker";
import { connectionDisplayName, loadConnections, parseConnectionString, saveConnections, TERMINAL_CONNECTIONS_KEY, updateConnection, type TerminalConnection } from "./connections";
import { TerminalEmulator } from "./terminal-emulator";
import { type MenuItem } from "../../ui/menu";
import { drawSubmenuIndicator, submenuItem, type MenuItem } from "../../ui/menu";
import { Menu, type MenuDrawArgs } from "../../ui/menu-core";
import { lineStep, listRowHeight } from "../../ui/metrics";
import { WindowMenu } from "../../ui/window-menu";
import { centeredTextY, lineStep, listRowHeight, textInkHeight } from "../../ui/metrics";
import { WindowMenu, WindowMenuLayer } 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 >
// Cell geometry comes from the terminal font setting (window menu > Settings >
// 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
@@ -94,6 +104,8 @@ 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";
// Storage key of terminalLaunchPresetsSetting, edited from the window menu.
const LAUNCH_PRESETS_KEY = "terminal.launchPresets";
type BaseWindow = {
windowId: string;
@@ -429,12 +441,14 @@ global.onmessage = (event: { data: WorkerAppMessage }) => {
);
break;
}
case "input-focus":
case "navigation-sensors":
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).
// the window menu's settings, the Add-connection draft typed on the phone).
onSettingsStoreChanged((key) => {
if (key.startsWith("glanceboard.")) {
reportIdle();
@@ -461,8 +475,17 @@ onSettingsStoreChanged((key) => {
}
}
return;
case LAUNCH_PRESETS_KEY:
// Live keystrokes from the phone editor (Settings > Launch presets in
// the window menu): repaint its edit page, and the hub, whose host
// headings launch presets.
for (const window of windows.values()) {
if (window.menu?.isOpen()) scheduleRender(window);
}
renderHubWindows();
return;
default:
// launchPresets, wakeOnBell: no connection impact.
// wakeOnBell: no connection impact.
return;
}
});
@@ -538,6 +561,8 @@ function closeWindow(windowId: string): void {
// Window closed mid-add: shut the phone editor down.
endAddConnection(window);
}
// Likewise for a setting being edited from the window menu.
window.menu?.close();
windows.delete(windowId);
windowIconActivity.delete(windowId);
updateHubAnimation();
@@ -1018,23 +1043,22 @@ function windowMenu(window: TerminalWindow): WindowMenu {
return window.menu;
}
/** Whether a control's handshake has been accepted (it can list and launch sessions). */
function isControlConnected(control: ControlConnection): boolean {
const phase = control.state?.phase;
return phase === "connected" || phase === "attached";
}
/** 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";
});
return [...controls.values()].filter(isControlConnected);
}
function windowMenuItems(window: TerminalWindow): MenuItem[] {
const items: MenuItem[] = [
{
label: "Settings",
onSelect: (ctx) => {
ctx.stack.pop();
post({ type: "open-settings", section: "Terminal" });
},
},
submenuItem("Settings", (ctx) => {
ctx.stack.push(new WindowMenuLayer("Terminal settings", settingsMenuItems()));
}),
];
if (window.kind === "hub") {
const connected = connectedControls();
@@ -1088,11 +1112,27 @@ function windowMenuItems(window: TerminalWindow): MenuItem[] {
return items;
}
/**
* The app's settings (the window menu's Settings submenu). Connections are
* managed in the hub's Manage Connections section instead.
*/
function settingsMenuItems(): MenuItem[] {
return [
enumSettingMenuItem(terminalDisplayModeSetting),
enumSettingMenuItem(terminalVerticalPositionSetting),
terminalFontPickerMenuItem(),
// Opens the phone text editor; the settings listener repaints its page.
textSettingMenuItem(terminalLaunchPresetsSetting),
toggleSettingMenuItem(terminalAutoReconnectSetting),
toggleSettingMenuItem(terminalWakeOnBellSetting),
];
}
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
void window.menu
.handleInput(event)
.catch((error) => console.error(`terminal menu input failed: ${error}`))
.then(() => renderAndSubmit(window, frameId));
@@ -1183,6 +1223,14 @@ type HubItem = {
* when it has an onSelect (launch a shell on that host).
*/
heading?: boolean;
/**
* Top-level action (Manage Connections, Settings): drawn
* unindented like a heading but at full brightness, rather than as an
* indented session row.
*/
topLevel?: boolean;
/** Opens a nested menu: a right-edge ">" marks the row. */
submenu?: boolean;
/** Session with recent output: an animated indicator marks the row. */
active?: boolean;
/** Glyph of the view window showing this session (the number column). */
@@ -1192,8 +1240,12 @@ type HubItem = {
/** Vertical offset of a row's text line from the top of its selection box. */
const HUB_ROW_TEXT_INSET = 4;
/** Horizontal inset of the list's selection boxes from the viewport edges. */
const HUB_LIST_X = 20;
/** Left edge of the list's selection boxes. */
const HUB_LIST_LEFT = 12;
/** Right inset of the selection boxes, clear of the scrollbar. */
const HUB_LIST_RIGHT = 20;
/** Left padding of unindented (heading and top-level) row text inside its selection box. */
const HUB_TOP_LEVEL_TEXT_X = 6;
/** The hub window's list, created on first use so openWindow stays free of paint dependencies. */
function hubMenu(window: HubWindow): Menu<HubItem> {
@@ -1208,10 +1260,12 @@ function hubMenu(window: HubWindow): Menu<HubItem> {
return window.list;
}
function drawHubRow({ image, item, x, y, width, selected }: MenuDrawArgs<HubItem>): void {
function drawHubRow({ image, item, x, y, width, height, selected }: MenuDrawArgs<HubItem>): void {
const font = chromeFont();
if (item.heading) {
image.drawText(font, x, y + HUB_ROW_TEXT_INSET, item.label, selected ? 255 : 140);
if (item.heading || item.topLevel) {
const value = selected ? 255 : item.heading ? 140 : 200;
image.drawText(font, x + HUB_TOP_LEVEL_TEXT_X, y + HUB_ROW_TEXT_INSET, item.label, value);
if (item.submenu) drawSubmenuIndicator(image, font, x, y, width, height, value);
return;
}
// Activity gutter, open-window number column, then the label, truncated
@@ -1278,20 +1332,41 @@ function hubSessionItems(window: HubWindow): HubItem[] {
const items: HubItem[] = [
{
label: "Manage Connections",
topLevel: true,
onSelect: () => setHubMode(window, "connections"),
},
{
label: "Settings",
topLevel: true,
submenu: true,
onSelect: () => windowMenu(window).open(settingsMenuItems(), "Terminal settings"),
},
];
const connected = connectedControls();
const multiHost = connected.length > 1;
const multiHost = controls.size > 1;
const canLaunch = launchPresetNames().length > 0;
for (const control of connected) {
if (multiHost) {
items.push({
label: connectionDisplayName(control.config),
heading: true,
onSelect: canLaunch ? () => launchOnHost(window, control) : undefined,
});
// Every configured host is listed under its own heading, in stored order,
// whatever its connection state: a connected host lists its sessions, any
// other shows its state on the heading plus a Connect row (unless it is
// already connecting). Full management (remove, add) lives in Manage
// Connections.
for (const control of controls.values()) {
const name = connectionDisplayName(control.config);
if (!isControlConnected(control)) {
items.push({ label: `${name} (${controlStatusWord(control)})`, heading: true });
const connecting = control.config.enabled && control.state?.phase === "connecting";
if (!connecting && clientOptionsFor(control.config)) {
items.push({
label: control.reconnectTimer ? "Connect now" : "Connect",
onSelect: () => connectControl(control),
});
}
continue;
}
items.push({
label: name,
heading: true,
onSelect: canLaunch ? () => launchOnHost(window, control) : undefined,
});
const sessions = orderedSessions(window, control);
for (const session of sessions) {
items.push({
@@ -1315,16 +1390,6 @@ function hubSessionItems(window: HubWindow): HubItem[] {
});
}
}
// 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;
}
@@ -1564,7 +1629,7 @@ function openViewWindow(control: ControlConnection, socket: string, label: strin
});
}
/** Preset names the user listed in Settings > Terminal (the wire protocol has no way to enumerate the server's). */
/** Preset names the user listed in the app's settings (the wire protocol has no way to enumerate the server's). */
function launchPresetNames(): string[] {
const names: string[] = [];
for (const piece of terminalLaunchPresetsSetting.get().split(",")) {
@@ -1631,14 +1696,14 @@ function paintHub(window: HubWindow): GrayImage {
// No border box: the shell chrome (top bar + sidebar) already frames the app.
// Long statuses get a wrapped block and move the list below it.
const title = window.mode === "connections" ? "Terminal - Connections" : "Terminal";
image.drawText(font, 18, 10, title, 220);
image.drawText(font, HUB_TITLE_X, 10, title, 220);
const header = layoutHubHeader(font, title, hubStatusLine(window), window.viewportWidth, step);
header.lines.forEach((line, index) => image.drawText(font, header.x, header.y + index * step, line, 170));
let listTop = header.listTop;
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);
image.drawText(font, HUB_TITLE_X + 6, listTop, "Add a g2mirror:// connection to get started, see:", 150);
image.drawText(font, HUB_TITLE_X + 6, listTop + step, "https://github.com/jimrandomh/g2mirror", 190);
listTop += 2 * step + 6;
}
@@ -1650,7 +1715,7 @@ function paintHub(window: HubWindow): GrayImage {
const listHeight = window.viewportHeight - 6 - listTop;
list.paint(
image,
{ x: HUB_LIST_X, y: listTop - 2, width: window.viewportWidth - 2 * HUB_LIST_X, height: listHeight },
{ x: HUB_LIST_LEFT, y: listTop - 2, width: window.viewportWidth - HUB_LIST_LEFT - HUB_LIST_RIGHT, height: listHeight },
window.focused,
);
list.drawScrollbar(image, window.viewportWidth - 10, listTop, listHeight - 4);
@@ -1750,11 +1815,14 @@ function paintView(window: ViewWindow): GrayImage {
}
// 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.
// line over the bottom row(s) whenever the session isn't actually attached.
// It's in the UI font, which can be taller than a terminal cell, so it's
// sized to that font's ink. Text is deferred glyphs (always on top of
// raster), so the covered rows must be suppressed rather than painted over.
const statusFont = chromeFont();
const statusBannerHeight = Math.max(window.cellHeight, textInkHeight(statusFont));
const statusBannerY = window.client.state().phase !== "attached"
? window.viewportHeight - window.cellHeight
? window.viewportHeight - statusBannerHeight
: null;
for (let row = 0; row < rows; row++) {
@@ -1775,8 +1843,8 @@ function paintView(window: ViewWindow): GrayImage {
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);
image.fillRect(0, statusBannerY, window.viewportWidth, statusBannerHeight, 0);
image.drawText(statusFont, 0, centeredTextY(statusFont, statusBannerY, statusBannerHeight), window.status, 170);
}
return image;
}
@@ -1873,7 +1941,7 @@ function handleTerminalTool(name: string, args: any): ToolResult | Promise<ToolR
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: "No launch presets configured (Terminal window menu > Settings > Launch presets)." };
}
return { ok: true, content: presets.map((preset) => `- ${preset}`).join("\n") };
}
+1
View File
@@ -368,6 +368,7 @@ export class TimersLayer implements Layer {
else timerEngine.restartTimer(timer.id);
return;
}
return;
}
case "new-timer":
this.openDial("");
+1 -1
View File
@@ -32,7 +32,7 @@ const MIC_ICON = imageFromAsciiArt(
" # ",
" ###### ",
],
220,
{ "#": 220 },
);
export type TranscribeAppOptions = InProcessAppOptions & {
+1 -1
View File
@@ -43,7 +43,7 @@ export class TranscribeLayer implements Layer {
const { width, height } = ctx.stack.getBaseSize();
const image = new GrayImage(width, height, 0);
const text = this.transcript.text || (this.listening ? "Listening..." : this.detail);
const wrapped = wrapTranscribeText((text) => font.measureText(text), text, width - 64);
const wrapped = wrapTranscribeText((line) => font.measureText(line), text, width - 64);
image.drawText(font, 24, 20, "Transcribe", 200);
image.drawText(font, 24, 40, this.saveNotice || this.status, 110);
+2
View File
@@ -1,10 +1,12 @@
import { type AppDefinition } from "../app-definition";
import { createWeatherAppWindow, WEATHER_SURFACE_ID, WEATHER_WINDOW_ID } from "./weather-app";
import { startWeatherIndicators } from "./weather-indicators";
const weatherApp: AppDefinition = {
appId: "weather",
title: "Weather",
icon: "cloud-sun",
boot: () => startWeatherIndicators(),
launch: (ctx) => ctx.launchInProcessApp(WEATHER_WINDOW_ID, WEATHER_SURFACE_ID, createWeatherAppWindow),
};
+24 -2
View File
@@ -1,6 +1,9 @@
import { ensureLocationPermission, hasLocationPermission } from "../../native/location-permissions";
import { weatherBridge } from "../../native/weather";
import { toggleSettingMenuItem } from "../../ui/dashboard-settings";
import { openSettingsSubMenu } from "../../ui/dashboard/settings-panel";
import { WeatherLayer } from "./weather";
import { weatherShowInStatusBarSetting, weatherShowOnSystemCardSetting } from "./weather-settings";
import {
createInProcessWindow,
YieldAtRootLayer,
@@ -16,7 +19,6 @@ export function createWeatherAppWindow(options: InProcessAppOptions): InProcessW
let closed = false;
let requestingPermission = false;
let unsubscribe: (() => void) | null = null;
let app: InProcessWindow;
const requestUpdate = () => {
if (closed || requestingPermission) return;
@@ -33,7 +35,15 @@ export function createWeatherAppWindow(options: InProcessAppOptions): InProcessW
}).catch((error) => { requestingPermission = false; console.warn(`Weather permission: ${error}`); });
};
app = createInProcessWindow({
// An indicator switched on without location access asks for it here, since
// its background refreshes have no screen of their own to prompt from.
const indicatorSettingOptions = {
onChange: (_ctx: unknown, enabled: boolean) => {
if (enabled && !hasLocationPermission()) requestUpdate();
},
};
const app = createInProcessWindow({
appId: "weather",
windowId: WEATHER_WINDOW_ID,
title: "Weather",
@@ -48,6 +58,18 @@ export function createWeatherAppWindow(options: InProcessAppOptions): InProcessW
requestUpdate();
},
},
{
label: "Settings",
onSelect: (ctx) => {
// Pop the menu first so closing the settings modal lands back on
// the forecast, not this menu.
ctx.stack.pop();
openSettingsSubMenu(ctx, "Weather settings", [
toggleSettingMenuItem(weatherShowInStatusBarSetting, indicatorSettingOptions),
toggleSettingMenuItem(weatherShowOnSystemCardSetting, indicatorSettingOptions),
]);
},
},
],
actions: options.actions,
baseLayer: new YieldAtRootLayer(new WeatherLayer(() => weatherBridge.snapshot(), requestUpdate)),
+119
View File
@@ -0,0 +1,119 @@
import { renderSvgIcon } from "../../graphics/icons";
import { type GrayImage, type UiFont } from "../../graphics/image";
import { type WeatherCondition, type WeatherState } from "../../native/weather";
// Lucide weather icons (ISC licensed), kept verbatim so they can be diffed
// against upstream.
const WEATHER_SVGS = {
sun:
'<svg viewBox="0 0 24 24" fill="none"><circle cx="12" cy="12" r="4"/><path d="M12 2v2"/><path d="M12 20v2"/><path d="m4.93 4.93 1.41 1.41"/><path d="m17.66 17.66 1.41 1.41"/><path d="M2 12h2"/><path d="M20 12h2"/><path d="m6.34 17.66-1.41 1.41"/><path d="m19.07 4.93-1.41 1.41"/></svg>',
moon:
'<svg viewBox="0 0 24 24" fill="none"><path d="M20.985 12.486a9 9 0 1 1-9.473-9.472c.405-.022.617.46.402.803a6 6 0 0 0 8.268 8.268c.344-.215.825-.004.803.401"/></svg>',
cloud:
'<svg viewBox="0 0 24 24" fill="none"><path d="M17.5 19H9a7 7 0 1 1 6.71-9h1.79a4.5 4.5 0 1 1 0 9Z"/></svg>',
"cloud-sun":
'<svg viewBox="0 0 24 24" fill="none"><path d="M12 2v2"/><path d="m4.93 4.93 1.41 1.41"/><path d="M20 12h2"/><path d="m19.07 4.93-1.41 1.41"/><path d="M15.947 12.65a4 4 0 0 0-5.925-4.128"/><path d="M13 22H7a5 5 0 1 1 4.9-6H13a3 3 0 0 1 0 6Z"/></svg>',
"cloud-moon":
'<svg viewBox="0 0 24 24" fill="none"><path d="M13 16a3 3 0 0 1 0 6H7a5 5 0 1 1 4.9-6z"/><path d="M18.376 14.512a6 6 0 0 0 3.461-4.127c.148-.625-.659-.97-1.248-.714a4 4 0 0 1-5.259-5.26c.255-.589-.09-1.395-.716-1.248a6 6 0 0 0-4.594 5.36"/></svg>',
"cloud-fog":
'<svg viewBox="0 0 24 24" fill="none"><path d="M4 14.899A7 7 0 1 1 15.71 8h1.79a4.5 4.5 0 0 1 2.5 8.242"/><path d="M16 17H7"/><path d="M17 21H9"/></svg>',
wind:
'<svg viewBox="0 0 24 24" fill="none"><path d="M12.8 19.6A2 2 0 1 0 14 16H2"/><path d="M17.5 8a2.5 2.5 0 1 1 2 4H2"/><path d="M9.8 4.4A2 2 0 1 1 11 8H2"/></svg>',
"cloud-drizzle":
'<svg viewBox="0 0 24 24" fill="none"><path d="M4 14.899A7 7 0 1 1 15.71 8h1.79a4.5 4.5 0 0 1 2.5 8.242"/><path d="M8 19v1"/><path d="M8 14v1"/><path d="M16 19v1"/><path d="M16 14v1"/><path d="M12 21v1"/><path d="M12 16v1"/></svg>',
"cloud-sun-rain":
'<svg viewBox="0 0 24 24" fill="none"><path d="M12 2v2"/><path d="m4.93 4.93 1.41 1.41"/><path d="M20 12h2"/><path d="m19.07 4.93-1.41 1.41"/><path d="M15.947 12.65a4 4 0 0 0-5.925-4.128"/><path d="M3 20a5 5 0 1 1 8.9-4H13a3 3 0 0 1 2 5.24"/><path d="M11 20v2"/><path d="M7 19v2"/></svg>',
"cloud-moon-rain":
'<svg viewBox="0 0 24 24" fill="none"><path d="M11 20v2"/><path d="M18.376 14.512a6 6 0 0 0 3.461-4.127c.148-.625-.659-.97-1.248-.714a4 4 0 0 1-5.259-5.26c.255-.589-.09-1.395-.716-1.248a6 6 0 0 0-4.594 5.36"/><path d="M3 20a5 5 0 1 1 8.9-4H13a3 3 0 0 1 2 5.24"/><path d="M7 19v2"/></svg>',
"cloud-rain":
'<svg viewBox="0 0 24 24" fill="none"><path d="M4 14.899A7 7 0 1 1 15.71 8h1.79a4.5 4.5 0 0 1 2.5 8.242"/><path d="M16 14v6"/><path d="M8 14v6"/><path d="M12 16v6"/></svg>',
"cloud-lightning":
'<svg viewBox="0 0 24 24" fill="none"><path d="M6 16.326A7 7 0 1 1 15.71 8h1.79a4.5 4.5 0 0 1 .5 8.973"/><path d="m13 12-3 5h4l-3 5"/></svg>',
"cloud-snow":
'<svg viewBox="0 0 24 24" fill="none"><path d="M4 14.899A7 7 0 1 1 15.71 8h1.79a4.5 4.5 0 0 1 2.5 8.242"/><path d="M8 15h.01"/><path d="M8 19h.01"/><path d="M12 17h.01"/><path d="M12 21h.01"/><path d="M16 15h.01"/><path d="M16 19h.01"/></svg>',
"cloud-hail":
'<svg viewBox="0 0 24 24" fill="none"><path d="M4 14.899A7 7 0 1 1 15.71 8h1.79a4.5 4.5 0 0 1 2.5 8.242"/><path d="M16 14v2"/><path d="M8 14v2"/><path d="M16 20h.01"/><path d="M8 20h.01"/><path d="M12 16v2"/><path d="M12 22h.01"/></svg>',
} as const;
type WeatherIconName = keyof typeof WEATHER_SVGS;
/** Status bar and system card hide weather whose last refresh is older than this. */
const INDICATOR_STALE_MS = 3 * 60 * 60 * 1000;
/** Gap between a reading's icon and its temperature. */
const READING_ICON_GAP = 3;
function weatherIconName(condition: WeatherCondition, isDaytime: boolean): WeatherIconName {
switch (condition) {
case "clear":
return isDaytime ? "sun" : "moon";
case "partly-cloudy":
return isDaytime ? "cloud-sun" : "cloud-moon";
case "cloudy":
return "cloud";
case "fog":
return "cloud-fog";
case "wind":
return "wind";
case "drizzle":
return "cloud-drizzle";
case "showers":
return isDaytime ? "cloud-sun-rain" : "cloud-moon-rain";
case "rain":
return "cloud-rain";
case "thunderstorm":
return "cloud-lightning";
case "snow":
return "cloud-snow";
case "sleet":
return "cloud-hail";
}
}
/** A size×size icon for the condition (sun or moon variants by time of day), rendered once and cached. */
export function renderWeatherIcon(condition: WeatherCondition, isDaytime: boolean, size: number): GrayImage | null {
const name = weatherIconName(condition, isDaytime);
return renderSvgIcon(`weather:${name}`, WEATHER_SVGS[name], size);
}
/** Current conditions as the status bar and system card show them. */
export type WeatherReading = {
condition: WeatherCondition | null;
isDaytime: boolean;
/** Rounded, unit-less ("72°"), to stay compact. */
temperature: string;
};
/** The current reading, or null when there is none or it has gone stale. */
export function weatherReading(state: WeatherState, nowMs: number): WeatherReading | null {
const current = state.current;
if (!current || state.lastUpdatedMs === null || nowMs - state.lastUpdatedMs > INDICATOR_STALE_MS) return null;
return {
condition: current.condition,
isDaytime: current.isDaytime,
temperature: current.temperatureF === null ? "--°" : `${Math.round(current.temperatureF)}°`,
};
}
/** Width drawWeatherReading takes for this reading. */
export function measureWeatherReading(reading: WeatherReading, font: UiFont, iconSize: number): number {
return (reading.condition ? iconSize + READING_ICON_GAP : 0) + font.measureText(reading.temperature);
}
/** The condition icon then the temperature, each centred vertically in the `height` rows from `top`. */
export function drawWeatherReading(
image: GrayImage,
reading: WeatherReading,
font: UiFont,
iconSize: number,
x: number,
top: number,
height: number,
shade: number,
): void {
if (reading.condition) {
const icon = renderWeatherIcon(reading.condition, reading.isDaytime, iconSize);
if (icon) image.drawImage(icon, x, top + (((height - icon.height) / 2) | 0));
x += iconSize + READING_ICON_GAP;
}
image.drawText(font, x, top + Math.max(0, ((height - font.lineHeight) / 2) | 0), reading.temperature, shade);
}
+46
View File
@@ -0,0 +1,46 @@
import { GrayImage, type UiFont } from "../../graphics/image";
import { getDefaultSmallFont } from "../../graphics/ui-fonts";
import { weatherBridge } from "../../native/weather";
import { onAnySettingChanged } from "../../ui/dashboard-settings";
import { shell } from "../../ui/shell/shell";
import { drawWeatherReading, measureWeatherReading, weatherReading, type WeatherReading } from "./weather-icons";
import { weatherShowInStatusBarSetting, weatherShowOnSystemCardSetting } from "./weather-settings";
const TRAY_ICON_ID = "weather";
const TRAY_HEIGHT = 24;
const TRAY_ICON_SIZE = 20;
let started = false;
/** What the tray currently shows, to skip rebuilding an unchanged icon. */
let trayShown: { key: string; font: UiFont } | null = null;
/**
* Shell-lifetime subscription: keeps the hourly background refresh running
* while the status-bar or system-card indicator is enabled, and the tray
* icon in sync. The system card reads the bridge itself while it shows.
*/
export function startWeatherIndicators(): void {
if (started) return;
started = true;
weatherBridge.onStateChange(syncWeatherIndicators);
onAnySettingChanged(syncWeatherIndicators);
}
function syncWeatherIndicators(): void {
const inStatusBar = weatherShowInStatusBarSetting.get();
weatherBridge.setBackgroundRefresh(inStatusBar || weatherShowOnSystemCardSetting.get());
const reading = inStatusBar ? weatherReading(weatherBridge.snapshot(), Date.now()) : null;
const font = getDefaultSmallFont();
const key = reading ? `${reading.condition}|${reading.isDaytime}|${reading.temperature}` : null;
if (key === (trayShown?.key ?? null) && (!trayShown || trayShown.font === font)) return;
trayShown = key === null ? null : { key, font };
shell.setTrayIcon(TRAY_ICON_ID, reading ? buildWeatherTrayIcon(reading, font) : null);
}
/** Condition icon and temperature, baked to a raster so the tray keeps one stable image. */
function buildWeatherTrayIcon(reading: WeatherReading, font: UiFont): GrayImage {
const image = new GrayImage(Math.ceil(measureWeatherReading(reading, font, TRAY_ICON_SIZE)), TRAY_HEIGHT, 0);
drawWeatherReading(image, reading, font, TRAY_ICON_SIZE, 0, 0, TRAY_HEIGHT, 220);
return image.withDrawsBaked();
}
+24
View File
@@ -0,0 +1,24 @@
import { ConfigSettingBoolean } from "../../ui/dashboard-settings";
/**
* Where current conditions show outside the Weather app. While either is on
* and the glasses are connected, the weather bridge refreshes hourly (see
* weather-indicators.ts).
*/
export const weatherShowInStatusBarSetting = new ConfigSettingBoolean({
id: "weather-show-in-status-bar",
label: "Show in status bar",
storageKey: "weather.showInStatusBar",
defaultValue: false,
description:
"Show the current conditions and temperature in the top bar. Weather for your current location is fetched once an hour while the glasses are connected.",
});
export const weatherShowOnSystemCardSetting = new ConfigSettingBoolean({
id: "weather-show-on-system-card",
label: "Show on system card",
storageKey: "weather.showOnSystemCard",
defaultValue: false,
description:
"Show the current conditions and temperature on the Glanceboard's system card. Weather for your current location is fetched once an hour while the glasses are connected.",
});
+30 -7
View File
@@ -6,11 +6,18 @@ import { GESTURE_CLICK, type InputEvent } from "../../ui/gestures";
import { type Layer, type LayerContext } from "../../ui/layers";
import { Menu, type MenuDrawArgs } from "../../ui/menu-core";
import { centeredTextY, lineStep, tightRowHeight } from "../../ui/metrics";
import { renderWeatherIcon } from "./weather-icons";
const PAGE_X = 18;
const HEADER_Y = 8;
const CURRENT_TOP = 34;
/** Minimum x of the description column right of the temperature. */
const CURRENT_TEXT_X = 112;
/** The current-conditions icon, left of the temperature. */
const CURRENT_ICON_SIZE = 40;
const CURRENT_ICON_GAP = 10;
/** Gap between the temperature and the description column. */
const CURRENT_TEXT_GAP = 16;
const FORECAST_HEADER_Y = 105;
const FORECAST_TOP = 124;
/** Forecast rows hold a 20px icon; grow with the font past that. */
@@ -70,7 +77,8 @@ export class WeatherLayer implements Layer {
);
return image;
}
if (weather.phase === "locating" || weather.phase === "loading") {
// A refresh with earlier data in hand keeps showing that data.
if ((weather.phase === "locating" || weather.phase === "loading") && !weather.current) {
this.drawMessage(image, font, width, height, weather.status);
return image;
}
@@ -94,7 +102,7 @@ export class WeatherLayer implements Layer {
}
return;
}
if (!weather.forecast.length || weather.phase !== "ready") return;
if (!weather.forecast.length || !showsForecast(weather)) return;
if (event.type === "scroll-up" || event.type === "scroll-down") {
this.forecastMenu.setItems(weather.forecast);
void this.forecastMenu.handleInput(event);
@@ -108,16 +116,24 @@ export class WeatherLayer implements Layer {
const medium = getDefaultMediumFont();
const small = getDefaultSmallFont();
const temperature = current.temperatureF === null ? "--°" : `${Math.round(current.temperatureF)}°F`;
image.drawText(large, PAGE_X, CURRENT_TOP + 8, temperature, 245);
const temperatureY = CURRENT_TOP + 8;
let temperatureX = PAGE_X;
if (current.condition) {
const icon = renderWeatherIcon(current.condition, current.isDaytime, CURRENT_ICON_SIZE);
if (icon) image.drawImage(icon, PAGE_X, temperatureY + (((large.lineHeight - icon.height) / 2) | 0));
temperatureX += CURRENT_ICON_SIZE + CURRENT_ICON_GAP;
}
image.drawText(large, temperatureX, temperatureY, temperature, 245);
const textX = Math.max(CURRENT_TEXT_X, temperatureX + Math.ceil(large.measureText(temperature)) + CURRENT_TEXT_GAP);
// Description / details / source stack compactly from just below the
// header line, so at large font sizes they stay clear of the forecast
// table instead of running into it.
let cy = 30;
image.drawText(
medium,
CURRENT_TEXT_X,
textX,
cy,
truncateText(medium, current.description || "Current conditions", width - CURRENT_TEXT_X - PAGE_X),
truncateText(medium, current.description || "Current conditions", width - textX - PAGE_X),
220,
);
cy += medium.lineHeight + 2;
@@ -127,11 +143,12 @@ export class WeatherLayer implements Layer {
if (current.windSpeedMph !== null) {
details.push(`Wind ${current.windDirection ? `${current.windDirection} ` : ""}${Math.round(current.windSpeedMph)} mph`);
}
image.drawText(small, CURRENT_TEXT_X, cy, details.join(" ") || "Current forecast", 170);
image.drawText(small, textX, cy, details.join(" ") || "Current forecast", 170);
cy += lineStep(small);
const source = current.observed ? "Observed" : "Forecast";
const age = current.timestampMs ? formatAge(Date.now() - current.timestampMs) : "";
image.drawText(small, CURRENT_TEXT_X, cy, `${source}${age ? ` ${age}` : ""}`, 105);
const updating = weather.phase === "locating" || weather.phase === "loading" ? " · updating" : "";
image.drawText(small, textX, cy, `${source}${age ? ` ${age}` : ""}${updating}`, 105);
image.drawLine(PAGE_X, FORECAST_HEADER_Y - 7, width - PAGE_X, FORECAST_HEADER_Y - 7, 40);
}
@@ -216,6 +233,12 @@ export class WeatherLayer implements Layer {
}
}
/** Whether paint shows the forecast table (ready, or refreshing over earlier data). */
function showsForecast(weather: WeatherState): boolean {
if (weather.phase === "ready") return true;
return (weather.phase === "locating" || weather.phase === "loading") && weather.current !== null;
}
/** Forecast row pitch, including the gap between selection boxes. */
function forecastRowHeight(font: UiFont): number {
return Math.max(FORECAST_MIN_ROW_HEIGHT, tightRowHeight(font) + 3);