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

@@ -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;