From 00c36602d673610beae5b24ec351fb040f53edf4 Mon Sep 17 00:00:00 2001 From: James Babcock Date: Mon, 12 Oct 2026 17:06:38 -0700 Subject: [PATCH 1/2] Add a prototype nightscout "missed bolus" detector --- app/apps/nightscout/index.ts | 6 +- app/apps/nightscout/missed-bolus-detector.ts | 336 ++++++++++++++++++ app/apps/nightscout/missed-bolus-monitor.ts | 73 ++++ .../nightscout/missed-bolus-notification.ts | 111 ++++++ app/apps/nightscout/nightscout.ts | 19 +- app/native/nightscout-bridge.ts | 22 +- app/ui/dashboard-settings.ts | 17 + app/ui/notifications.ts | 100 ++++-- app/ui/shell/shell.ts | 42 ++- scripts/nightscout-missed-bolus-replay.cjs | 157 ++++++++ tests/missed-bolus-detector.test.cjs | 137 +++++++ tests/nightscout.test.cjs | 37 +- tests/tsconfig.json | 1 + 13 files changed, 997 insertions(+), 61 deletions(-) create mode 100644 app/apps/nightscout/missed-bolus-detector.ts create mode 100644 app/apps/nightscout/missed-bolus-monitor.ts create mode 100644 app/apps/nightscout/missed-bolus-notification.ts create mode 100644 scripts/nightscout-missed-bolus-replay.cjs create mode 100644 tests/missed-bolus-detector.test.cjs diff --git a/app/apps/nightscout/index.ts b/app/apps/nightscout/index.ts index 6f5f59ad..07b04c56 100644 --- a/app/apps/nightscout/index.ts +++ b/app/apps/nightscout/index.ts @@ -1,11 +1,15 @@ import { type AppDefinition } from "../app-definition"; import { createNightscoutAppWindow, startNightscoutTrayIcon, NIGHTSCOUT_SURFACE_ID, NIGHTSCOUT_WINDOW_ID } from "./nightscout-app"; +import { startMissedBolusMonitor } from "./missed-bolus-monitor"; const nightscoutApp: AppDefinition = { appId: "nightscout", title: "Nightscout", icon: "nightscout", - boot: () => startNightscoutTrayIcon(), + boot: (ctx) => { + startNightscoutTrayIcon(); + startMissedBolusMonitor(ctx); + }, launch: (ctx) => ctx.launchInProcessApp(NIGHTSCOUT_WINDOW_ID, NIGHTSCOUT_SURFACE_ID, createNightscoutAppWindow), }; diff --git a/app/apps/nightscout/missed-bolus-detector.ts b/app/apps/nightscout/missed-bolus-detector.ts new file mode 100644 index 00000000..37a8a985 --- /dev/null +++ b/app/apps/nightscout/missed-bolus-detector.ts @@ -0,0 +1,336 @@ +/** + * Missed-bolus detector: given a recent window of CGM readings and + * treatments, decide whether glucose is rising the way an unannounced meal + * does, with no meal bolus, manual bolus or carb entry to account for it. + * + * Deliberately self-contained (no imports; plain data in, plain data out) so + * it can move to the server side of Nightscout unchanged, and so it can be + * iterated on against recorded history with + * scripts/nightscout-missed-bolus-replay.cjs. The Faceclaw side (when to run + * it, cooldown, the glasses notification) is in missed-bolus-monitor.ts. + * + * Glucose is mg/dL throughout: Nightscout stores sgv in mg/dL whatever the + * site's display units. + */ + +export type GlucoseReading = { timeMs: number; mgdl: number }; + +/** + * - meal: a meal/snack/wizard bolus, or any bolus entered together with carbs. + * - manual: a bolus explicitly marked user-initiated (e.g. Loop's `automatic: false`). + * - automatic: a bolus the loop issued (SMB, automatic bolus). + * - ambiguous: not marked either way. OpenAPS on a Medtronic pump uploads its + * SMBs as plain "Correction Bolus" records, indistinguishable from a manual + * correction except by size; `ambiguousBolusMinUnits` decides. + */ +export type BolusKind = "meal" | "manual" | "automatic" | "ambiguous"; +export type BolusEvent = { timeMs: number; units: number; kind: BolusKind }; +export type CarbEntry = { timeMs: number; grams: number }; + +export type MissedBolusInput = { + /** The time to classify as of. Readings and treatments after it are ignored. */ + nowMs: number; + glucose: readonly GlucoseReading[]; + boluses: readonly BolusEvent[]; + carbs: readonly CarbEntry[]; +}; + +export type MissedBolusOptions = { + /** The latest reading must be at most this old, or there is nothing current to judge. */ + maxReadingAgeMs: number; + /** How far back from the latest reading to look for where the rise started. */ + lookbackMs: number; + /** A gap between readings longer than this ends the window (the CGM dropped out). */ + maxGapMs: number; + /** Recent span the rate of rise is fitted over (least squares). */ + slopeWindowMs: number; + /** Fewest readings the rate fit needs. */ + minSlopeReadings: number; + /** Rise from the window's lowest reading to the latest. */ + minRiseMgdl: number; + /** The latest reading must be at least this high. */ + minGlucoseMgdl: number; + /** Rises from below this are taken as recovering from a low (rescue carbs go unbolused). */ + hypoNadirMgdl: number; + /** The rise must have lasted at least this long, to rule out sensor noise. */ + minRiseDurationMs: number; + /** Rate of rise over the slope window, mg/dL per minute. */ + minRateMgdlPerMin: number; + /** A bolus or carb entry this long before the rise began still accounts for it (pre-bolusing). */ + preBolusWindowMs: number; + /** Smallest meal or manual bolus that accounts for a rise. */ + minManualBolusUnits: number; + /** Smallest unmarked bolus taken as manual rather than an SMB. */ + ambiguousBolusMinUnits: number; + /** Whether a carb entry with no bolus accounts for a rise (the user evidently knows they ate). */ + carbsCountAsAnnouncement: boolean; +}; + +export const DEFAULT_MISSED_BOLUS_OPTIONS: Readonly = { + maxReadingAgeMs: 12 * 60_000, + lookbackMs: 90 * 60_000, + maxGapMs: 16 * 60_000, + slopeWindowMs: 20 * 60_000, + minSlopeReadings: 3, + minRiseMgdl: 40, + minGlucoseMgdl: 140, + hypoNadirMgdl: 70, + minRiseDurationMs: 20 * 60_000, + minRateMgdlPerMin: 1.5, + preBolusWindowMs: 60 * 60_000, + minManualBolusUnits: 0.5, + ambiguousBolusMinUnits: 1.5, + carbsCountAsAnnouncement: true, +}; + +export type GlucoseRise = { + /** When the rise started: the lowest reading in the window (the latest, if tied). */ + onsetMs: number; + nadirMgdl: number; + latestMs: number; + latestMgdl: number; + riseMgdl: number; + /** Least-squares rate over the slope window, mg/dL per minute. */ + rateMgdlPerMin: number; +}; + +export type MissedBolusClassification = "missed-bolus" | "no-alert" | "insufficient-data"; + +export type MissedBolusResult = { + classification: MissedBolusClassification; + /** One-line explanation, for logs and the replay tool. */ + reason: string; + /** The rise that was judged, when there was enough data to measure one. */ + rise: GlucoseRise | null; +}; + +export function detectMissedBolus( + input: MissedBolusInput, + overrides: Partial = {}, +): MissedBolusResult { + const options: MissedBolusOptions = { ...DEFAULT_MISSED_BOLUS_OPTIONS, ...overrides }; + const { nowMs } = input; + const readings = usableReadings(input.glucose, nowMs); + const latest = readings[readings.length - 1]; + if (!latest) return insufficient("no glucose readings"); + if (nowMs - latest.timeMs > options.maxReadingAgeMs) { + return insufficient(`latest reading is ${formatMinutes(nowMs - latest.timeMs)} old`); + } + + // The contiguous run of readings ending at the latest, within the lookback. + const windowStartMs = latest.timeMs - options.lookbackMs; + let first = readings.length - 1; + while (first > 0) { + const previous = readings[first - 1]!; + if (previous.timeMs < windowStartMs || readings[first]!.timeMs - previous.timeMs > options.maxGapMs) break; + first--; + } + const window = readings.slice(first); + let nadir = window[0]!; + for (const reading of window) { + if (reading.mgdl <= nadir.mgdl) nadir = reading; + } + + const slopeStartMs = Math.max(nadir.timeMs, latest.timeMs - options.slopeWindowMs); + const slopeReadings = window.filter((reading) => reading.timeMs >= slopeStartMs); + const rise: GlucoseRise = { + onsetMs: nadir.timeMs, + nadirMgdl: nadir.mgdl, + latestMs: latest.timeMs, + latestMgdl: latest.mgdl, + riseMgdl: latest.mgdl - nadir.mgdl, + rateMgdlPerMin: slopeReadings.length >= 2 ? slopePerMinute(slopeReadings) : 0, + }; + const noAlert = (reason: string): MissedBolusResult => ({ classification: "no-alert", reason, rise }); + + if (rise.riseMgdl < options.minRiseMgdl) { + return noAlert(`rise of ${rise.riseMgdl} mg/dL is under ${options.minRiseMgdl}`); + } + if (latest.mgdl < options.minGlucoseMgdl) { + return noAlert(`glucose ${latest.mgdl} mg/dL is under ${options.minGlucoseMgdl}`); + } + if (nadir.mgdl < options.hypoNadirMgdl) { + return noAlert(`rising from a low of ${nadir.mgdl} mg/dL`); + } + if (latest.timeMs - nadir.timeMs < options.minRiseDurationMs) { + return noAlert(`rise has lasted only ${formatMinutes(latest.timeMs - nadir.timeMs)}`); + } + if (slopeReadings.length < options.minSlopeReadings) { + return { classification: "insufficient-data", reason: `only ${slopeReadings.length} readings to fit a rate`, rise }; + } + if (rise.rateMgdlPerMin < options.minRateMgdlPerMin) { + return noAlert(`rising ${formatRate(rise.rateMgdlPerMin)}, under ${formatRate(options.minRateMgdlPerMin)}`); + } + + const coverStartMs = nadir.timeMs - options.preBolusWindowMs; + const inCoverWindow = (timeMs: number) => timeMs >= coverStartMs && timeMs <= nowMs; + const bolus = latestMatching(input.boluses, (event) => inCoverWindow(event.timeMs) && accountsForRise(event, options)); + if (bolus) { + return noAlert(`${formatUnits(bolus.units)} U ${bolus.kind} bolus ${formatMinutes(nowMs - bolus.timeMs)} ago`); + } + if (options.carbsCountAsAnnouncement) { + const carbs = latestMatching(input.carbs, (entry) => inCoverWindow(entry.timeMs) && entry.grams > 0); + if (carbs) return noAlert(`${carbs.grams} g carbs entered ${formatMinutes(nowMs - carbs.timeMs)} ago`); + } + return { + classification: "missed-bolus", + reason: `up ${rise.riseMgdl} mg/dL in ${formatMinutes(latest.timeMs - nadir.timeMs)}, rising ${formatRate(rise.rateMgdlPerMin)}, no bolus`, + rise, + }; +} + +function insufficient(reason: string): MissedBolusResult { + return { classification: "insufficient-data", reason, rise: null }; +} + +function accountsForRise(bolus: BolusEvent, options: MissedBolusOptions): boolean { + switch (bolus.kind) { + case "meal": + case "manual": + return bolus.units >= options.minManualBolusUnits; + case "ambiguous": + return bolus.units >= Math.max(options.minManualBolusUnits, options.ambiguousBolusMinUnits); + case "automatic": + return false; + } +} + +/** + * Finite, in-range readings at or before now, oldest first, one per + * timestamp. Dexcom reports 39 for "LOW" and smaller values as error codes. + */ +function usableReadings(glucose: readonly GlucoseReading[], nowMs: number): GlucoseReading[] { + const sorted = glucose + .filter((reading) => Number.isFinite(reading.timeMs) && Number.isFinite(reading.mgdl) + && reading.mgdl >= 39 && reading.mgdl <= 600 && reading.timeMs <= nowMs) + .slice() + .sort((a, b) => a.timeMs - b.timeMs); + return sorted.filter((reading, index) => index === 0 || reading.timeMs !== sorted[index - 1]!.timeMs); +} + +/** Least-squares slope through the readings, in mg/dL per minute. */ +function slopePerMinute(readings: readonly GlucoseReading[]): number { + const t0 = readings[0]!.timeMs; + const n = readings.length; + let sumX = 0, sumY = 0, sumXY = 0, sumXX = 0; + for (const reading of readings) { + const x = (reading.timeMs - t0) / 60_000; + sumX += x; + sumY += reading.mgdl; + sumXY += x * reading.mgdl; + sumXX += x * x; + } + const denominator = n * sumXX - sumX * sumX; + return denominator > 0 ? (n * sumXY - sumX * sumY) / denominator : 0; +} + +function latestMatching(events: readonly T[], predicate: (event: T) => boolean): T | null { + let found: T | null = null; + for (const event of events) { + if (predicate(event) && (!found || event.timeMs > found.timeMs)) found = event; + } + return found; +} + +function formatMinutes(ms: number): string { + return `${Math.round(ms / 60_000)} min`; +} + +function formatRate(mgdlPerMin: number): string { + return `${mgdlPerMin.toFixed(1)} mg/dL/min`; +} + +function formatUnits(units: number): string { + return `${Math.round(units * 100) / 100}`; +} + +// --------------------------------------------------------------------------- +// Nightscout record normalization, shared by the Faceclaw bridge and a +// server-side port. + +/** The fields of a Nightscout treatment record the detector reads. */ +export type NightscoutTreatmentRecord = { + eventType?: unknown; + created_at?: unknown; + timestamp?: unknown; + insulin?: unknown; + carbs?: unknown; + automatic?: unknown; + isSMB?: unknown; + type?: unknown; + wizard?: unknown; +}; + +/** The fields of a Nightscout entry record the detector reads. */ +export type NightscoutEntryRecord = { date?: unknown; sgv?: unknown }; + +const MEAL_BOLUS_EVENT_TYPES = new Set(["Meal Bolus", "Snack Bolus", "Bolus Wizard", "Combo Bolus"]); + +/** + * How a treatment's insulin was given. Uploaders mark automatic boluses + * differently: Loop sets `automatic`, AndroidAPS `isSMB`, Trio/OpenAPS + * `type: "SMB"` or eventType "SMB". Meal boluses carry carbs or a wizard + * record, or a meal-type eventType. + */ +export function bolusKindFromTreatment(treatment: NightscoutTreatmentRecord): BolusKind { + const eventType = typeof treatment.eventType === "string" ? treatment.eventType : ""; + if (treatment.automatic === true || treatment.isSMB === true || treatment.type === "SMB" || eventType === "SMB") { + return "automatic"; + } + if (MEAL_BOLUS_EVENT_TYPES.has(eventType) || positiveNumber(treatment.carbs) !== null + || (typeof treatment.wizard === "object" && treatment.wizard !== null)) { + return "meal"; + } + if (treatment.automatic === false || treatment.isSMB === false) return "manual"; + return "ambiguous"; +} + +/** Boluses and carb entries from raw Nightscout treatment records, oldest first. */ +export function normalizeNightscoutTreatments(treatments: readonly NightscoutTreatmentRecord[]): { + boluses: BolusEvent[]; + carbs: CarbEntry[]; +} { + const boluses: BolusEvent[] = []; + const carbs: CarbEntry[] = []; + for (const treatment of treatments) { + const timeMs = treatmentTimeMs(treatment); + if (timeMs === null) continue; + const units = positiveNumber(treatment.insulin); + if (units !== null) boluses.push({ timeMs, units, kind: bolusKindFromTreatment(treatment) }); + const grams = positiveNumber(treatment.carbs); + if (grams !== null) carbs.push({ timeMs, grams }); + } + boluses.sort((a, b) => a.timeMs - b.timeMs); + carbs.sort((a, b) => a.timeMs - b.timeMs); + return { boluses, carbs }; +} + +/** Glucose readings from raw Nightscout sgv entries, oldest first. */ +export function normalizeNightscoutEntries(entries: readonly NightscoutEntryRecord[]): GlucoseReading[] { + const readings: GlucoseReading[] = []; + for (const entry of entries) { + const timeMs = Number(entry.date); + const mgdl = Number(entry.sgv); + if (Number.isFinite(timeMs) && timeMs > 0 && Number.isFinite(mgdl)) readings.push({ timeMs, mgdl }); + } + return readings.sort((a, b) => a.timeMs - b.timeMs); +} + +function treatmentTimeMs(treatment: NightscoutTreatmentRecord): number | null { + for (const value of [treatment.created_at, treatment.timestamp]) { + if (typeof value === "number" && Number.isFinite(value) && value > 0) return value; + if (typeof value === "string" && value.trim()) { + const numeric = Number(value); + const parsed = Number.isFinite(numeric) ? numeric : Date.parse(value); + if (Number.isFinite(parsed) && parsed > 0) return parsed; + } + } + return null; +} + +function positiveNumber(value: unknown): number | null { + if (typeof value !== "number" && typeof value !== "string") return null; + if (typeof value === "string" && !value.trim()) return null; + const numeric = Number(value); + return Number.isFinite(numeric) && numeric > 0 ? numeric : null; +} diff --git a/app/apps/nightscout/missed-bolus-monitor.ts b/app/apps/nightscout/missed-bolus-monitor.ts new file mode 100644 index 00000000..1a64641b --- /dev/null +++ b/app/apps/nightscout/missed-bolus-monitor.ts @@ -0,0 +1,73 @@ +import { type AppContext } from "../app-definition"; +import { nightscoutBridge, type NightscoutState } from "../../native/nightscout-bridge"; +import { getStringSetting, setStringSetting } from "../../native/settings-store"; +import { nightscoutMissedBolusAlertsSetting, nightscoutMissedBolusCooldownSetting } from "../../ui/dashboard-settings"; +import { shell } from "../../ui/shell/shell"; +import { detectMissedBolus, type MissedBolusInput } from "./missed-bolus-detector"; +import { MissedBolusNotificationLayer } from "./missed-bolus-notification"; + +/** When the last alert was shown, persisted so a restart doesn't reset the cooldown. */ +const LAST_ALERT_STORAGE_KEY = "integrations.nightscout.missedBolusLastAlertMs"; + +let started = false; +let lastInputKey = ""; +let lastSummary = ""; + +/** + * Runs the missed-bolus detector each time a Nightscout sync brings new + * readings or treatments, and shows a glasses notification when it fires + * outside the cooldown. Shell-lifetime, started from the app's boot hook. + */ +export function startMissedBolusMonitor(ctx: AppContext): void { + if (started) return; + started = true; + nightscoutBridge.onStateChange((state) => checkForMissedBolus(state, ctx)); +} + +function checkForMissedBolus(state: NightscoutState, ctx: AppContext): void { + if (!nightscoutMissedBolusAlertsSetting.get() || !state.available) return; + const key = inputKey(state); + if (key === lastInputKey) return; + lastInputKey = key; + + const nowMs = Date.now(); + const result = detectMissedBolus(detectorInput(state, nowMs)); + const summary = `${result.classification}: ${result.reason}`; + if (summary !== lastSummary) { + lastSummary = summary; + console.log(`missed-bolus detector: ${summary}`); + } + if (result.classification !== "missed-bolus" || !result.rise) return; + + const lastAlertMs = Number(getStringSetting(LAST_ALERT_STORAGE_KEY, "0")) || 0; + const cooldownMs = Number(nightscoutMissedBolusCooldownSetting.get()) * 60_000; + // A last-alert time in the future means the clock moved back; don't let it block alerts. + if (lastAlertMs <= nowMs && nowMs - lastAlertMs < cooldownMs) return; + setStringSetting(LAST_ALERT_STORAGE_KEY, String(nowMs)); + ctx.appendLog(`missed-bolus alert: ${result.reason}`); + const rise = result.rise; + shell.postAppNotification({ + key: `nightscout-missed-bolus:${nowMs}`, + createLayer: (close) => new MissedBolusNotificationLayer(rise, nowMs, close, () => { + void ctx.launchApp("nightscout"); + }), + }); +} + +/** Changes whenever a sync brings a new reading or a treatment the detector reads. */ +function inputKey(state: NightscoutState): string { + return [ + state.latest?.timestampMs ?? 0, + state.boluses.map((bolus) => bolus.timestampMs).join(","), + state.carbs.map((entry) => entry.timestampMs).join(","), + ].join("|"); +} + +function detectorInput(state: NightscoutState, nowMs: number): MissedBolusInput { + return { + nowMs, + glucose: state.history.map((point) => ({ timeMs: point.timestampMs, mgdl: point.sgv })), + boluses: state.boluses.map((bolus) => ({ timeMs: bolus.timestampMs, units: bolus.insulin, kind: bolus.kind })), + carbs: state.carbs.map((entry) => ({ timeMs: entry.timestampMs, grams: entry.carbs })), + }; +} diff --git a/app/apps/nightscout/missed-bolus-notification.ts b/app/apps/nightscout/missed-bolus-notification.ts new file mode 100644 index 00000000..3b09ecf0 --- /dev/null +++ b/app/apps/nightscout/missed-bolus-notification.ts @@ -0,0 +1,111 @@ +import { getDefaultSmallFont } from "../../graphics/ui-fonts"; +import { GrayImage } from "../../graphics/image"; +import { renderIcon } from "../../graphics/icons"; +import { wrapText } from "../../graphics/textwrap"; +import { formatRelativeTime } from "../../util/date-util"; +import { nightscoutBridge } from "../../native/nightscout-bridge"; +import { type InputEvent } from "../../ui/gestures"; +import { type Layer, type LayerContext } from "../../ui/layers"; +import { lineStep } from "../../ui/metrics"; +import { + NOTIFICATION_CONTENT_X, + createNotificationActionMenu, + drawNotificationHeader, + notificationActionMenuBox, + notificationContentWidth, +} from "../../ui/notifications"; +import { type GlucoseRise } from "./missed-bolus-detector"; +import { + drawDirectionIndicator, + drawNightscoutGraph, + drawNightscoutValueStrikeThrough, + formatDelta, + isNightscoutPointStale, +} from "./nightscout"; + +const ICON_SIZE = 24; +const GRAPH_GAP = 6; +const GRAPH_BOTTOM_MARGIN = 12; +const MIN_GRAPH_HEIGHT = 32; + +type ActionItem = { label: string; run: () => void }; + +/** + * The missed-bolus alert, laid out like an Android notification's detail view + * (header, text, right-hand action column), with the live glucose readout and + * 2-hour graph below the text. + */ +export class MissedBolusNotificationLayer implements Layer { + private readonly menu = createNotificationActionMenu(); + + constructor( + private readonly rise: GlucoseRise, + private readonly postedAtMs: number, + private readonly close: () => void, + openNightscout: () => void, + ) { + this.menu.setItems([ + { label: "Dismiss", run: close }, + { label: "Open Nightscout", run: () => { + close(); + openNightscout(); + } }, + ]); + } + + paint(ctx: LayerContext): GrayImage { + const font = getDefaultSmallFont(); + const step = lineStep(font); + const { width, height } = ctx.stack.getBaseSize(); + const image = new GrayImage(width, height, 0); + const contentX = NOTIFICATION_CONTENT_X; + const contentWidth = notificationContentWidth(width); + const nowMs = Date.now(); + + let y = drawNotificationHeader(image, font, renderIcon("nightscout", ICON_SIZE), `Nightscout ${formatRelativeTime(this.postedAtMs)}`); + image.drawText(font, contentX, y, "Missed bolus?", 230); + y += step; + const minutes = Math.round((this.rise.latestMs - this.rise.onsetMs) / 60_000); + const body = `Glucose rose ${this.rise.riseMgdl} mg/dL in ${minutes} min with no bolus or carbs entered.`; + for (const line of wrapText(font, body, contentWidth)) { + image.drawText(font, contentX, y, line, 190); + y += step; + } + + // Live readout and graph: the modal repaints with each shell render, so + // these follow new readings (and a bolus, once entered) while it is up. + const nightscout = nightscoutBridge.snapshot(); + const latest = nightscout.latest; + if (latest) { + const value = `${latest.sgv}`; + image.drawText(font, contentX, y, value, 230); + if (isNightscoutPointStale(latest, nowMs)) { + drawNightscoutValueStrikeThrough(image, contentX, y + (font.lineHeight >> 1), font.measureText(value)); + } + let x = contentX + font.measureText(`${value} `); + x += drawDirectionIndicator(image, font, x, y, nightscout.direction, 200); + x += font.measureText(" "); + const iob = nightscout.iob === null ? "" : ` IOB ${nightscout.iob.toFixed(2)}`; + image.drawText(font, x, y, `${formatDelta(nightscout.delta)}${iob}`, 180); + y += step; + } + const graphTop = y + GRAPH_GAP; + const graphHeight = height - GRAPH_BOTTOM_MARGIN - graphTop; + if (graphHeight >= MIN_GRAPH_HEIGHT) { + drawNightscoutGraph(image, { x: contentX, y: graphTop, width: contentWidth, height: graphHeight }, nightscout, nowMs, font); + } + + this.menu.paint(image, notificationActionMenuBox(width, height), ctx.stack.isFocused()); + return image; + } + + async handleInput(event: InputEvent): Promise { + if (event.type === "double-click") { + this.close(); + } else if (event.type === "click") { + this.menu.selectedItem?.run(); + } else if (event.type === "scroll-up" || event.type === "scroll-down") { + await this.menu.handleInput(event); + } + } +} diff --git a/app/apps/nightscout/nightscout.ts b/app/apps/nightscout/nightscout.ts index 675a07f8..8266a31d 100644 --- a/app/apps/nightscout/nightscout.ts +++ b/app/apps/nightscout/nightscout.ts @@ -13,6 +13,8 @@ import { nightscoutBatteryLowSetting, nightscoutMaxLoopAgeSetting, nightscoutAlwaysShowInTopBarSetting, + nightscoutMissedBolusAlertsSetting, + nightscoutMissedBolusCooldownSetting, toggleSettingMenuItem, nightscoutApiTokenSetting, nightscoutSiteUrlSetting, @@ -28,6 +30,7 @@ export const NIGHTSCOUT_STALE_MS = 15 * 60 * 1000; const NIGHTSCOUT_GRAPH_WINDOW_MS = 2 * 60 * 60 * 1000; const NIGHTSCOUT_GRAPH_TIME_QUANTUM_MS = 60 * 1000; +/** Draws the trend indicator; returns its width. */ export function drawDirectionIndicator( image: GrayImage, font: UiFont, @@ -35,16 +38,10 @@ export function drawDirectionIndicator( y: number, direction: string, shade: number, -): void { - const label = directionGlyphLabel(font, direction); - if (label) { - image.drawText(font, x, y, label, shade); - return; - } - - if (direction) { - image.drawText(font, x, y, truncateLine(direction, 10), shade); - } +): number { + const label = directionGlyphLabel(font, direction) || (direction ? truncateLine(direction, 10) : ""); + if (label) image.drawText(font, x, y, label, shade); + return font.measureText(label); } /** @@ -425,6 +422,8 @@ export function nightscoutMenuItems(): MenuItem[] { textSettingMenuItem(nightscoutCartridgeLowSetting), textSettingMenuItem(nightscoutBatteryLowSetting), textSettingMenuItem(nightscoutMaxLoopAgeSetting), + toggleSettingMenuItem(nightscoutMissedBolusAlertsSetting), + textSettingMenuItem(nightscoutMissedBolusCooldownSetting), ]); }, }, diff --git a/app/native/nightscout-bridge.ts b/app/native/nightscout-bridge.ts index 9271d7c2..51be4f57 100644 --- a/app/native/nightscout-bridge.ts +++ b/app/native/nightscout-bridge.ts @@ -1,5 +1,6 @@ import { nightscoutApiTokenSetting, nightscoutSiteUrlSetting } from "../ui/dashboard-settings"; import { fetchTextWithUserAgent } from "../util/http"; +import { bolusKindFromTreatment, type BolusKind } from "../apps/nightscout/missed-bolus-detector"; export type NightscoutPoint = { timestampMs: number; @@ -14,6 +15,8 @@ export type NightscoutCarbEvent = { export type NightscoutBolusEvent = { timestampMs: number; insulin: number; + /** Meal, manual, loop-automatic, or unmarked (see BolusKind). */ + kind: BolusKind; }; export type NightscoutBasalEvent = { @@ -102,10 +105,17 @@ type NightscoutTreatmentResponse = { absolute?: unknown; rate?: unknown; duration?: unknown; + automatic?: unknown; + isSMB?: unknown; + type?: unknown; + wizard?: unknown; }; const NIGHTSCOUT_REFRESH_MS = 60_000; -const NIGHTSCOUT_HISTORY_COUNT = 30; +// Glucose history by time rather than count, so a 1-minute CGM still covers +// the graphs' 2 hours and the missed-bolus detector's 90-minute lookback. +const NIGHTSCOUT_HISTORY_WINDOW_MS = 3 * 60 * 60 * 1000; +const NIGHTSCOUT_HISTORY_MAX_COUNT = 200; const NIGHTSCOUT_TREATMENT_LOOKBACK_MS = 3 * 60 * 60 * 1000; // The v1 treatments API silently adds `created_at >= now - 4 days` to any // query without an explicit date filter (lib/server/query.js enforceDateFilter), @@ -208,13 +218,17 @@ export class NightscoutBridge { this.emit(); return; } + const historyStartMs = nowMs - NIGHTSCOUT_HISTORY_WINDOW_MS; const treatmentStartIso = new Date(nowMs - NIGHTSCOUT_TREATMENT_LOOKBACK_MS).toISOString(); const siteChangeStartIso = new Date(nowMs - NIGHTSCOUT_SITE_CHANGE_LOOKBACK_MS).toISOString(); const tokenQuery = `token=${encodeURIComponent(apiToken)}`; const [entries, deviceStatus, status, treatments, siteChanges] = await Promise.all([ fetchJson( - `${siteUrl}/api/v1/entries.json?count=${NIGHTSCOUT_HISTORY_COUNT}&${tokenQuery}`, - ), + `${siteUrl}/api/v1/entries.json?find%5Bdate%5D%5B%24gte%5D=${historyStartMs}&count=${NIGHTSCOUT_HISTORY_MAX_COUNT}&${tokenQuery}`, + ).then(async (recent) => recent?.length + ? recent + // Nothing in the window: still show the last reading, as stale. + : await fetchJson(`${siteUrl}/api/v1/entries.json?count=1&${tokenQuery}`)), fetchJson( `${siteUrl}/api/v1/devicestatus.json?count=1&${tokenQuery}`, ), @@ -334,7 +348,7 @@ function parseTreatments(treatments: NightscoutTreatmentResponse[], nowMs: numbe const insulinValue = firstFiniteNumber(treatment.insulin); if (insulinValue !== null && insulinValue > 0) { - boluses.push({ timestampMs, insulin: insulinValue }); + boluses.push({ timestampMs, insulin: insulinValue, kind: bolusKindFromTreatment(treatment) }); } const eventType = typeof treatment.eventType === "string" ? treatment.eventType : ""; diff --git a/app/ui/dashboard-settings.ts b/app/ui/dashboard-settings.ts index b0652865..19c7d675 100644 --- a/app/ui/dashboard-settings.ts +++ b/app/ui/dashboard-settings.ts @@ -1163,6 +1163,23 @@ export const nightscoutAlwaysShowInTopBarSetting = new ConfigSettingBoolean({ defaultValue: false, description: "Keep the Nightscout glucose graph and warnings in the top bar even when all Nightscout windows are closed.", }); +export const nightscoutMissedBolusAlertsSetting = new ConfigSettingBoolean({ + id: "nightscout-missed-bolus-alerts", + label: "Missed-bolus alerts", + storageKey: "integrations.nightscout.missedBolusAlerts", + defaultValue: false, + description: "Show a notification on the glasses when glucose rises like an unannounced meal with no bolus or carbs entered. Experimental.", +}); +export const nightscoutMissedBolusCooldownSetting = new ConfigSettingString({ + id: "nightscout-missed-bolus-cooldown-minutes", + label: "Missed-bolus alert cooldown", + storageKey: "integrations.nightscout.missedBolusCooldownMinutes", + defaultValue: "120", + editorTitle: "Missed-bolus alert cooldown (min)", + normalize: normalizeNightscoutThreshold, + formatValue: (value) => `${value} min`, + description: "After a missed-bolus alert, wait at least this many minutes before showing another.", +}); export function loadNightscoutThresholds(): NightscoutThresholds { return { diff --git a/app/ui/notifications.ts b/app/ui/notifications.ts index 3a3da2ba..90a5f0df 100644 --- a/app/ui/notifications.ts +++ b/app/ui/notifications.ts @@ -190,22 +190,7 @@ export class NotificationsListLayer implements Layer { */ export class SingleNotificationLayer implements Layer { /** The action column; its items are rebuilt from the live notification on every paint and input. */ - private readonly detailMenu = new Menu({ - wrap: false, - rowGap: DETAIL_MENU_ROW_GAP, - highlight: { radius: 6 }, - getHeight: (item, width) => { - const font = getDefaultSmallFont(); - return detailMenuRowLines(font, item, width).length * lineStep(font) + 8; - }, - draw: ({ image, item, x, y, width, selected }) => { - const font = getDefaultSmallFont(); - const lines = detailMenuRowLines(font, item, width); - for (let line = 0; line < lines.length; line++) { - image.drawText(font, x + DETAIL_MENU_TEXT_X, y + DETAIL_MENU_TEXT_Y + line * lineStep(font), lines[line]!, selected ? 255 : 185); - } - }, - }); + private readonly detailMenu = createNotificationActionMenu((item) => item.kind === "disable-source"); private confirmation: { source: AndroidNotification; menu: Menu } | null = null; // The last paint, which a tray-hosted layer repeats once its notification // is gone, until the tray swaps in a neighbour's (see closeUnavailableNotification). @@ -229,7 +214,7 @@ export class SingleNotificationLayer implements Layer { this.detailMenu.setItems(buildDetailMenu(notification, this.options.origin)); drawDetailContent(image, font, notification, iconForNotification(notification.key), width, height); - this.detailMenu.paint(image, detailMenuBox(width, height), ctx.stack.isFocused()); + this.detailMenu.paint(image, notificationActionMenuBox(width, height), ctx.stack.isFocused()); this.lastImage = image; return image; } @@ -427,20 +412,9 @@ function drawDetailContent( width: number, height: number, ): void { - const contentX = DETAIL_CONTENT_X; - const menuX = width - DETAIL_MENU_WIDTH - 24; - const contentWidth = menuX - contentX - 20; - image.drawText(font, contentX, DETAIL_TITLE_Y, "Notification", 220); - // The header row (icon + app name) and the body stack below the title's - // line box, so a large UI font pushes them down instead of overlapping. - const headerTop = DETAIL_TITLE_Y + font.lineHeight + DETAIL_HEADER_GAP; - const headerHeight = Math.max(ICON_SIZE, font.lineHeight); - let appLineX = contentX; - if (icon) { - image.bitBlt(icon, contentX, headerTop + ((headerHeight - ICON_SIZE) >> 1), { transparentZero: true }); - appLineX = contentX + ICON_SIZE + ICON_TEXT_GAP; - } - image.drawText(font, appLineX, centeredTextY(font, headerTop, headerHeight), `${notification.appName || notification.packageName} ${formatRelativeTime(notification.postTime)}`, 150); + const contentX = NOTIFICATION_CONTENT_X; + const contentWidth = notificationContentWidth(width); + const bodyTop = drawNotificationHeader(image, font, icon, `${notification.appName || notification.packageName} ${formatRelativeTime(notification.postTime)}`); const lines: string[] = []; lines.push(...wrapText(font, notification.title || "(untitled)", contentWidth)); @@ -456,7 +430,6 @@ function drawDetailContent( } const step = lineStep(font); - const bodyTop = headerTop + headerHeight + DETAIL_BODY_GAP; const maxLines = Math.max(1, ((height - bodyTop - step) / step) | 0); for (let index = 0; index < Math.min(lines.length, maxLines); index++) { const line = lines[index]!; @@ -467,8 +440,63 @@ function drawDetailContent( } } +/** Left edge of a notification detail view's content column. */ +export const NOTIFICATION_CONTENT_X = DETAIL_CONTENT_X; + +/** Width of a notification detail view's content column, left of the action column. */ +export function notificationContentWidth(width: number): number { + return width - DETAIL_MENU_WIDTH - 24 - NOTIFICATION_CONTENT_X - 20; +} + +/** + * The top of a notification detail view: the "Notification" title, then the + * source's icon and label. Returns the y where the body starts. + */ +export function drawNotificationHeader(image: GrayImage, font: UiFont, icon: GrayImage | null, sourceLabel: string): number { + const contentX = NOTIFICATION_CONTENT_X; + image.drawText(font, contentX, DETAIL_TITLE_Y, "Notification", 220); + // The header row (icon + app name) and the body stack below the title's + // line box, so a large UI font pushes them down instead of overlapping. + const headerTop = DETAIL_TITLE_Y + font.lineHeight + DETAIL_HEADER_GAP; + const headerHeight = Math.max(ICON_SIZE, font.lineHeight); + let sourceX = contentX; + if (icon) { + image.bitBlt(icon, contentX, headerTop + ((headerHeight - ICON_SIZE) >> 1), { transparentZero: true }); + sourceX = contentX + ICON_SIZE + ICON_TEXT_GAP; + } + image.drawText(font, sourceX, centeredTextY(font, headerTop, headerHeight), sourceLabel, 150); + return headerTop + headerHeight + DETAIL_BODY_GAP; +} + +/** + * A notification detail view's right-hand action column. Rows truncate their + * label to one line, except those `wraps` picks, which wrap it. + */ +export function createNotificationActionMenu(wraps: (item: T) => boolean = () => false): Menu { + const rowLines = (font: UiFont, item: T, rowWidth: number): string[] => { + const textWidth = rowWidth - 2 * DETAIL_MENU_TEXT_X; + return wraps(item) ? wrapText(font, item.label, textWidth) : [truncateText(font, item.label, textWidth)]; + }; + return new Menu({ + wrap: false, + rowGap: DETAIL_MENU_ROW_GAP, + highlight: { radius: 6 }, + getHeight: (item, width) => { + const font = getDefaultSmallFont(); + return rowLines(font, item, width).length * lineStep(font) + 8; + }, + draw: ({ image, item, x, y, width, selected }) => { + const font = getDefaultSmallFont(); + const lines = rowLines(font, item, width); + for (let line = 0; line < lines.length; line++) { + image.drawText(font, x + DETAIL_MENU_TEXT_X, y + DETAIL_MENU_TEXT_Y + line * lineStep(font), lines[line]!, selected ? 255 : 185); + } + }, + }); +} + /** The action column's row boxes: right of the content, from DETAIL_MENU_TOP to the bottom margin. */ -function detailMenuBox(width: number, height: number): MenuBox { +export function notificationActionMenuBox(width: number, height: number): MenuBox { return { x: width - DETAIL_MENU_WIDTH - 32, y: DETAIL_MENU_TOP, @@ -477,12 +505,6 @@ function detailMenuBox(width: number, height: number): MenuBox { }; } -/** An action row's text lines: the disable-source row wraps, the rest truncate to one line. */ -function detailMenuRowLines(font: UiFont, item: DetailMenuItem, rowWidth: number): string[] { - const textWidth = rowWidth - 2 * DETAIL_MENU_TEXT_X; - return item.kind === "disable-source" ? wrapText(font, item.label, textWidth) : [truncateText(font, item.label, textWidth)]; -} - function buildDetailMenu(notification: AndroidNotification, origin: SingleNotificationLayerOrigin): DetailMenuItem[] { return [ { kind: "back", label: "Back" }, diff --git a/app/ui/shell/shell.ts b/app/ui/shell/shell.ts index 19bc40a4..2ff4fb25 100644 --- a/app/ui/shell/shell.ts +++ b/app/ui/shell/shell.ts @@ -248,6 +248,18 @@ export type AssistantActivityEvent = { export type FocusKind = "sidebar" | "window"; +/** + * A notification an app posts itself, rather than one Android posted. It + * opens in the same modal (and waits in the same queue) as Android + * notifications, but is not listed in the Notifications app. + */ +export type AppNotification = { + /** Unique per notification; must not collide with Android notification keys. */ + key: string; + /** The modal's content; `close` takes the modal down. */ + createLayer: (close: () => void) => Layer; +}; + const noopActions: LayerActions = noopLayerActions; /** @@ -403,9 +415,12 @@ class Shell { private readonly trayIcons = new Map(); private activeVoiceLayer: VoiceInputLayer | null = null; private activeKeyboardLayer: KeyboardInputLayer | null = null; + // App-posted notifications that are open or queued, by key. + private readonly appNotifications = new Map(); private readonly notificationModals = new NotificationModalQueue({ open: (notificationKey) => this.pushNotificationModal(notificationKey), isAvailable: (notificationKey) => { + if (this.appNotifications.has(notificationKey)) return true; const notification = readActiveNotifications(ALL_NOTIFICATIONS).find((item) => item.key === notificationKey); return !!notification && shouldShowNotificationOnGlasses(notification.packageName); }, @@ -797,20 +812,35 @@ class Shell { this.config.requestShellRender(); } + /** + * Show an app's own notification the way new Android notifications are + * shown: in the notification modal, waking the screen for it if it was off. + */ + postAppNotification(notification: AppNotification): void { + this.appNotifications.set(notification.key, notification); + const wokeScreen = this.screenOn ? false : this.wake("sidebar"); + this.openNotificationModal(notification.key, wokeScreen); + } + private pushNotificationModal(notificationKey: string): ShellModalLayer { + const appNotification = this.appNotifications.get(notificationKey); + const close = () => this.closeNotificationModal(modal, notificationKey); const modal: ShellModalLayer = new ShellModalLayer( - new SingleNotificationLayer(notificationKey, { - origin: "new-notification-modal", - onClose: () => this.closeNotificationModal(modal), - }), + appNotification + ? appNotification.createLayer(close) + : new SingleNotificationLayer(notificationKey, { origin: "new-notification-modal", onClose: close }), this.config.actions, - () => this.notificationModals.removed(modal, this.screenOn), + () => { + this.appNotifications.delete(notificationKey); + this.notificationModals.removed(modal, this.screenOn); + }, ); this.stack.push(modal); return modal; } - private closeNotificationModal(modal: ShellModalLayer): void { + private closeNotificationModal(modal: ShellModalLayer, notificationKey: string): void { + this.appNotifications.delete(notificationKey); // The queue first: it opens the next notification or puts the screen back // to sleep, and then ignores the removal below. remove() rather than a // pop, so a menu opened over the modal doesn't leave it stuck. diff --git a/scripts/nightscout-missed-bolus-replay.cjs b/scripts/nightscout-missed-bolus-replay.cjs new file mode 100644 index 00000000..17b5c24a --- /dev/null +++ b/scripts/nightscout-missed-bolus-replay.cjs @@ -0,0 +1,157 @@ +#!/usr/bin/env node +// Replays the missed-bolus detector (app/apps/nightscout/missed-bolus-detector.ts) +// over recorded Nightscout history, as if it had run after every CGM reading +// with the app's cooldown, and lists where it would have alerted next to the +// meal boluses around them. For tuning the detector without the glasses. +// +// node scripts/nightscout-missed-bolus-replay.cjs --site https://x.herokuapp.com --token TOKEN --days 14 +// node scripts/nightscout-missed-bolus-replay.cjs --site ... --token ... --days 30 --save /tmp/ns +// node scripts/nightscout-missed-bolus-replay.cjs --load /tmp/ns --set minRiseMgdl=50 --set minRateMgdlPerMin=2 +// +// --site/--token default to $NIGHTSCOUT_URL/$NIGHTSCOUT_TOKEN. --save writes +// the fetched entries.json/treatments.json to a directory; --load replays a +// saved directory instead of fetching. --set overrides one detector option +// (see MissedBolusOptions); --cooldown-min sets the alert cooldown (default +// 120, the app's default); --verbose also prints every classification change. + +const fs = require('node:fs'); +const path = require('node:path'); +const vm = require('node:vm'); +const ts = require('typescript'); + +const DETECTOR = path.join(__dirname, '../app/apps/nightscout/missed-bolus-detector.ts'); +// Matches the bridge's history window: the detector looks back 90 min, plus slack. +const HISTORY_WINDOW_MS = 3 * 60 * 60_000; +// The app sees a reading at the next minutely sync after its upload. +const SYNC_LAG_MS = 60_000; +// A meal bolus this soon after an alert counts as the alert being answered. +const ANSWER_WINDOW_MS = 3 * 60 * 60_000; + +function loadDetector() { + const source = ts.transpileModule(fs.readFileSync(DETECTOR, 'utf8'), { + compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2020 }, + }).outputText; + const exports = {}; + vm.runInNewContext(source, { exports }, { filename: DETECTOR }); + return exports; +} + +function parseArgs(argv) { + const args = { site: process.env.NIGHTSCOUT_URL ?? '', token: process.env.NIGHTSCOUT_TOKEN ?? '', days: 14, cooldownMin: 120, set: {}, verbose: false }; + for (let i = 0; i < argv.length; i++) { + const flag = argv[i]; + const value = () => { + if (i + 1 >= argv.length) throw new Error(`${flag} needs a value`); + return argv[++i]; + }; + if (flag === '--site') args.site = value().replace(/\/+$/, ''); + else if (flag === '--token') args.token = value(); + else if (flag === '--days') args.days = Number(value()); + else if (flag === '--save') args.save = value(); + else if (flag === '--load') args.load = value(); + else if (flag === '--cooldown-min') args.cooldownMin = Number(value()); + else if (flag === '--verbose') args.verbose = true; + else if (flag === '--set') { + const [key, raw] = value().split('='); + args.set[key] = raw === 'true' ? true : raw === 'false' ? false : Number(raw); + } else throw new Error(`unknown argument ${flag}`); + } + return args; +} + +async function fetchJson(url) { + const response = await fetch(url); + if (!response.ok) throw new Error(`HTTP ${response.status} for ${url.replace(/token=[^&]*/, 'token=...')}`); + return response.json(); +} + +async function loadHistory(args) { + if (args.load) { + return { + entries: JSON.parse(fs.readFileSync(path.join(args.load, 'entries.json'), 'utf8')), + treatments: JSON.parse(fs.readFileSync(path.join(args.load, 'treatments.json'), 'utf8')), + }; + } + if (!args.site || !args.token) throw new Error('need --site and --token (or $NIGHTSCOUT_URL/$NIGHTSCOUT_TOKEN), or --load'); + const startMs = Date.now() - args.days * 86_400_000; + const token = `token=${encodeURIComponent(args.token)}`; + // Room for 1-minute CGMs; Nightscout otherwise caps a query at 10 results. + const count = Math.ceil(args.days * 1440 * 1.1); + const [entries, treatments] = await Promise.all([ + fetchJson(`${args.site}/api/v1/entries/sgv.json?find%5Bdate%5D%5B%24gte%5D=${startMs}&count=${count}&${token}`), + fetchJson(`${args.site}/api/v1/treatments.json?find%5Bcreated_at%5D%5B%24gte%5D=${encodeURIComponent(new Date(startMs).toISOString())}&count=${count}&${token}`), + ]); + if (args.save) { + fs.mkdirSync(args.save, { recursive: true }); + fs.writeFileSync(path.join(args.save, 'entries.json'), JSON.stringify(entries)); + fs.writeFileSync(path.join(args.save, 'treatments.json'), JSON.stringify(treatments)); + } + return { entries, treatments }; +} + +function formatTime(ms) { + const date = new Date(ms); + const pad = (n) => String(n).padStart(2, '0'); + return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())} ${pad(date.getHours())}:${pad(date.getMinutes())}`; +} + +function minutes(ms) { + return `${Math.round(ms / 60_000)} min`; +} + +async function main() { + const args = parseArgs(process.argv.slice(2)); + const detector = loadDetector(); + for (const key of Object.keys(args.set)) { + if (!(key in detector.DEFAULT_MISSED_BOLUS_OPTIONS)) throw new Error(`unknown detector option ${key}`); + } + const { entries, treatments } = await loadHistory(args); + const glucose = detector.normalizeNightscoutEntries(entries); + const { boluses, carbs } = detector.normalizeNightscoutTreatments(treatments); + const mealBoluses = boluses.filter((bolus) => bolus.kind === 'meal'); + if (!glucose.length) throw new Error('no glucose entries'); + + const cooldownMs = args.cooldownMin * 60_000; + const alerts = []; + let lastAlertMs = -Infinity; + let lastSummary = ''; + let first = 0; + for (const reading of glucose) { + const nowMs = reading.timeMs + SYNC_LAG_MS; + while (glucose[first].timeMs < nowMs - HISTORY_WINDOW_MS) first++; + const inWindow = (event) => event.timeMs >= nowMs - HISTORY_WINDOW_MS && event.timeMs <= nowMs; + const result = detector.detectMissedBolus({ + nowMs, + glucose: glucose.slice(first).filter((r) => r.timeMs <= nowMs), + boluses: boluses.filter(inWindow), + carbs: carbs.filter(inWindow), + }, args.set); + const summary = `${result.classification}: ${result.reason}`; + if (args.verbose && summary !== lastSummary) console.log(` ${formatTime(nowMs)} ${reading.mgdl} ${summary}`); + lastSummary = summary; + if (result.classification !== 'missed-bolus' || nowMs - lastAlertMs < cooldownMs) continue; + lastAlertMs = nowMs; + alerts.push({ nowMs, result }); + } + + const spanDays = (glucose[glucose.length - 1].timeMs - glucose[0].timeMs) / 86_400_000; + console.log(`${glucose.length} readings over ${spanDays.toFixed(1)} days; ${mealBoluses.length} meal boluses; ${boluses.filter((b) => b.kind === 'ambiguous').length} unmarked boluses (max ${Math.max(0, ...boluses.filter((b) => b.kind === 'ambiguous').map((b) => b.units))} U)`); + console.log(`options: ${JSON.stringify({ ...detector.DEFAULT_MISSED_BOLUS_OPTIONS, ...args.set })}`); + console.log(`\n${alerts.length} alerts (${(alerts.length / Math.max(spanDays, 1 / 24)).toFixed(2)}/day, cooldown ${args.cooldownMin} min):`); + for (const { nowMs, result } of alerts) { + const answer = mealBoluses.find((bolus) => bolus.timeMs > nowMs && bolus.timeMs - nowMs <= ANSWER_WINDOW_MS); + const note = answer ? `meal bolus ${answer.units} U ${minutes(answer.timeMs - nowMs)} later` : 'no meal bolus within 3 h'; + console.log(` ${formatTime(nowMs)} ${result.rise.latestMgdl} mg/dL ${result.reason} [${note}]`); + } + console.log('\nmeal boluses:'); + for (const bolus of mealBoluses) { + const alert = alerts.find(({ nowMs }) => nowMs < bolus.timeMs && bolus.timeMs - nowMs <= ANSWER_WINDOW_MS); + const meal = carbs.find((entry) => entry.timeMs === bolus.timeMs); + console.log(` ${formatTime(bolus.timeMs)} ${bolus.units} U${meal ? ` ${meal.grams} g` : ''}${alert ? ` [alerted ${minutes(bolus.timeMs - alert.nowMs)} before]` : ''}`); + } +} + +main().catch((error) => { + console.error(error.message ?? error); + process.exit(1); +}); diff --git a/tests/missed-bolus-detector.test.cjs b/tests/missed-bolus-detector.test.cjs new file mode 100644 index 00000000..046a2951 --- /dev/null +++ b/tests/missed-bolus-detector.test.cjs @@ -0,0 +1,137 @@ +const test = require('node:test'); +const assert = require('node:assert/strict'); +const detector = require('../.test-build/app/apps/nightscout/missed-bolus-detector.js'); + +const MIN = 60_000; +const T0 = 1_800_000_000_000; + +/** + * 5-minute readings: flat at `base` for `flatMin`, then rising `rate` mg/dL + * per minute for `riseMin`. The rise starts at T0 + flatMin. + */ +function series({ base = 100, flatMin = 60, rate = 2.5, riseMin = 40 } = {}) { + const readings = []; + for (let m = 0; m <= flatMin + riseMin; m += 5) { + readings.push({ timeMs: T0 + m * MIN, mgdl: Math.round(base + Math.max(0, m - flatMin) * rate) }); + } + return readings; +} + +/** A detector input one minute after the last reading. */ +function input(glucose, { boluses = [], carbs = [] } = {}) { + return { nowMs: glucose[glucose.length - 1].timeMs + MIN, glucose, boluses, carbs }; +} + +const onset = T0 + 60 * MIN; + +test('an unannounced meal-shaped rise is a missed bolus', () => { + const result = detector.detectMissedBolus(input(series())); + assert.equal(result.classification, 'missed-bolus', result.reason); + assert.equal(result.rise.onsetMs, onset); + assert.equal(result.rise.nadirMgdl, 100); + assert.equal(result.rise.latestMgdl, 200); + assert.equal(result.rise.riseMgdl, 100); + assert.ok(Math.abs(result.rise.rateMgdlPerMin - 2.5) < 0.01); +}); + +test('a meal or manual bolus from the pre-bolus window onward accounts for the rise', () => { + for (const [kind, offsetMin] of [['meal', -50], ['meal', 15], ['manual', 30]]) { + const result = detector.detectMissedBolus(input(series(), { + boluses: [{ timeMs: onset + offsetMin * MIN, units: 5, kind }], + })); + assert.equal(result.classification, 'no-alert', `${kind} at ${offsetMin}: ${result.reason}`); + } + // Too long before the rise began, or too small, doesn't count. + for (const bolus of [{ timeMs: onset - 70 * MIN, units: 5, kind: 'meal' }, { timeMs: onset, units: 0.3, kind: 'meal' }]) { + assert.equal(detector.detectMissedBolus(input(series(), { boluses: [bolus] })).classification, 'missed-bolus'); + } +}); + +test('loop boluses do not count; unmarked ones count only above the SMB size threshold', () => { + const smbs = [0, 10, 20, 30].map((m) => ({ timeMs: onset + m * MIN, units: 1.4, kind: 'ambiguous' })); + smbs.push({ timeMs: onset + 5 * MIN, units: 3, kind: 'automatic' }); + assert.equal(detector.detectMissedBolus(input(series(), { boluses: smbs })).classification, 'missed-bolus'); + const manual = [{ timeMs: onset + 20 * MIN, units: 2, kind: 'ambiguous' }]; + assert.equal(detector.detectMissedBolus(input(series(), { boluses: manual })).classification, 'no-alert'); + assert.equal(detector.detectMissedBolus(input(series(), { boluses: manual }), { ambiguousBolusMinUnits: 2.5 }).classification, 'missed-bolus'); +}); + +test('a carb entry accounts for the rise unless that option is off', () => { + const carbs = [{ timeMs: onset + 10 * MIN, grams: 40 }]; + assert.equal(detector.detectMissedBolus(input(series(), { carbs })).classification, 'no-alert'); + assert.equal(detector.detectMissedBolus(input(series(), { carbs }), { carbsCountAsAnnouncement: false }).classification, 'missed-bolus'); +}); + +test('rises that are small, slow, brief, low, or from a hypo do not alert', () => { + const cases = [ + series({ rate: 0.9, riseMin: 60 }), // slow: under 1.5 mg/dL/min + series({ rate: 3, riseMin: 15 }), // brief: 15 min + series({ base: 80, rate: 1.6, riseMin: 30 }), // ends at 128, under 140 + series({ base: 60, rate: 3, riseMin: 40 }), // recovering from a low + series({ base: 150, rate: 1.5, riseMin: 20 }), // only 30 mg/dL + ]; + for (const glucose of cases) { + const result = detector.detectMissedBolus(input(glucose)); + assert.equal(result.classification, 'no-alert', result.reason); + assert.ok(result.rise); + } +}); + +test('a rise that has peaked and turned down does not alert', () => { + const glucose = series(); + const last = glucose[glucose.length - 1]; + for (let m = 5; m <= 20; m += 5) glucose.push({ timeMs: last.timeMs + m * MIN, mgdl: last.mgdl - m }); + assert.equal(detector.detectMissedBolus(input(glucose)).classification, 'no-alert'); +}); + +test('stale, missing, or gapped data is insufficient', () => { + const glucose = series(); + const stale = { ...input(glucose), nowMs: glucose[glucose.length - 1].timeMs + 20 * MIN }; + assert.equal(detector.detectMissedBolus(stale).classification, 'insufficient-data'); + assert.equal(detector.detectMissedBolus({ nowMs: T0, glucose: [], boluses: [], carbs: [] }).classification, 'insufficient-data'); + // Readings after "now" are ignored rather than used. + const early = { ...input(glucose), nowMs: onset + 11 * MIN }; + assert.notEqual(detector.detectMissedBolus(early).classification, 'missed-bolus'); + // A CGM dropout splits the window: the rise before it doesn't count. + const gapped = series().filter((r) => r.timeMs < onset + 10 * MIN || r.timeMs > onset + 30 * MIN); + assert.notEqual(detector.detectMissedBolus(input(gapped)).classification, 'missed-bolus'); + // Dexcom error codes are dropped; 39 ("LOW") is a real reading. + const withErrors = series().map((r, i) => (i === 3 ? { ...r, mgdl: 5 } : r)); + assert.equal(detector.detectMissedBolus(input(withErrors)).classification, 'missed-bolus'); +}); + +test('bolus kinds from each uploader', () => { + const kind = detector.bolusKindFromTreatment; + assert.equal(kind({ eventType: 'Correction Bolus', insulin: 0.5, automatic: true }), 'automatic'); // Loop + assert.equal(kind({ eventType: 'Correction Bolus', insulin: 0.5, automatic: false }), 'manual'); // Loop + assert.equal(kind({ eventType: 'Correction Bolus', insulin: 0.4, isSMB: true }), 'automatic'); // AndroidAPS + assert.equal(kind({ eventType: 'Correction Bolus', insulin: 0.4, type: 'SMB' }), 'automatic'); // Trio + assert.equal(kind({ eventType: 'SMB', insulin: 0.4 }), 'automatic'); + assert.equal(kind({ eventType: 'Meal Bolus', insulin: 4 }), 'meal'); + assert.equal(kind({ eventType: 'Bolus', insulin: 4, carbs: 30 }), 'meal'); + assert.equal(kind({ eventType: 'Correction Bolus', insulin: 4, wizard: { carb_input: 0 } }), 'meal'); + // OpenAPS on a Medtronic pump: SMBs and manual corrections look the same. + assert.equal(kind({ eventType: 'Correction Bolus', insulin: 0.3, carbs: null, enteredBy: 'openaps://medtronic/723' }), 'ambiguous'); +}); + +test('normalizing raw Nightscout records', () => { + const { boluses, carbs } = detector.normalizeNightscoutTreatments([ + { eventType: 'Meal Bolus', created_at: '2026-10-11T23:05:32.000Z', insulin: 10.7, carbs: 75, wizard: {} }, + { eventType: 'Correction Bolus', created_at: '2026-10-11T22:05:00.000Z', insulin: 0.3, carbs: null }, + { eventType: 'Temp Basal', created_at: '2026-10-11T22:00:00.000Z', insulin: null, carbs: null, rate: 1.2 }, + { eventType: 'Carb Correction', timestamp: '2026-10-11T21:00:00.000Z', carbs: '15' }, + { eventType: 'Correction Bolus', insulin: 1 }, // no time + ]); + assert.deepEqual(boluses, [ + { timeMs: Date.parse('2026-10-11T22:05:00.000Z'), units: 0.3, kind: 'ambiguous' }, + { timeMs: Date.parse('2026-10-11T23:05:32.000Z'), units: 10.7, kind: 'meal' }, + ]); + assert.deepEqual(carbs, [ + { timeMs: Date.parse('2026-10-11T21:00:00.000Z'), grams: 15 }, + { timeMs: Date.parse('2026-10-11T23:05:32.000Z'), grams: 75 }, + ]); + assert.deepEqual( + detector.normalizeNightscoutEntries([{ date: 2000, sgv: 120 }, { date: 1000, sgv: 110 }, { date: 3000 }]), + [{ timeMs: 1000, mgdl: 110 }, { timeMs: 2000, mgdl: 120 }], + ); +}); diff --git a/tests/nightscout.test.cjs b/tests/nightscout.test.cjs index e1deee44..8dcbb916 100644 --- a/tests/nightscout.test.cjs +++ b/tests/nightscout.test.cjs @@ -5,6 +5,7 @@ const path = require('node:path'); const vm = require('node:vm'); const ts = require('typescript'); const alerts = require('../.test-build/app/apps/nightscout/nightscout-alerts.js'); +const detector = require('../.test-build/app/apps/nightscout/missed-bolus-detector.js'); const now = 1_800_000_000_000; const limits = { maxCannulaAgeHours: 72, cartridgeLowUnits: 20, batteryLowVoltage: 1.2, maxLoopAgeMinutes: 15 }; const healthy = { cageTimestampMs: now, reservoirUnits: 100, batteryVoltage: 1.5, loopTimestampMs: now }; @@ -206,6 +207,7 @@ test('bridge exposes numeric measurements and treats absent values as unknown', let pump = { reservoir: '10.5', battery: { voltage: '1.15' } }; const module = load('app/native/nightscout-bridge.ts', { '../ui/dashboard-settings': { nightscoutSiteUrlSetting: { get: () => 'https://example.test' }, nightscoutApiTokenSetting: { get: () => 'test' } }, + '../apps/nightscout/missed-bolus-detector': detector, '../util/http': { async fetchTextWithUserAgent(url) { const body = url.includes('/entries.') ? [{ date: now, sgv: 100 }] : url.includes('/devicestatus.') ? [{ pump, openaps: { suggested: { mills: now - 60_000 } } }] : url.includes('/status.') ? {} : []; return { ok: true, json: async () => body }; @@ -225,12 +227,44 @@ test('bridge exposes numeric measurements and treats absent values as unknown', assert.equal(bridge.snapshot().reservoirUnits, 0); }); +test('bridge tags bolus kinds and falls back to the last reading when the window is empty', async () => { + const urls = []; + let recent = []; + const module = load('app/native/nightscout-bridge.ts', { + '../ui/dashboard-settings': { nightscoutSiteUrlSetting: { get: () => 'https://example.test' }, nightscoutApiTokenSetting: { get: () => 'test' } }, + '../apps/nightscout/missed-bolus-detector': detector, + '../util/http': { async fetchTextWithUserAgent(url) { + urls.push(url); + const iso = new Date(now - 30 * 60_000).toISOString(); + const body = url.includes('/entries.') ? (url.includes('%24gte') ? recent : [{ date: now - 5 * 3_600_000, sgv: 140 }]) + : url.includes('/treatments.') && !url.includes('Site%20Change') ? [ + { eventType: 'Meal Bolus', created_at: iso, insulin: 6, carbs: 50 }, + { eventType: 'Correction Bolus', created_at: iso, insulin: 0.3 }, + { eventType: 'Correction Bolus', created_at: iso, insulin: 0.2, automatic: true }, + ] + : url.includes('/status.') ? {} : []; + return { ok: true, json: async () => body }; + } }, + }, { Date: class extends Date { static now() { return now; } }, console }); + const bridge = new module.NightscoutBridge(); + await bridge.refreshNow(); + assert.deepEqual(Array.from(bridge.snapshot().boluses, b => b.kind).sort(), ['ambiguous', 'automatic', 'meal']); + assert.equal(bridge.snapshot().latest.sgv, 140, 'a stale last reading still shows'); + assert.ok(urls.some(url => url.includes('/entries.json?count=1&'))); + recent = [{ date: now - 60_000, sgv: 120 }]; + urls.length = 0; + await bridge.refreshNow(); + assert.equal(bridge.snapshot().latest.sgv, 120); + assert.ok(!urls.some(url => url.includes('/entries.json?count=1&')), 'no fallback when the window has readings'); +}); + test('polling starts once and stops even while the initial HTTP request is pending', async () => { let resolveHttp, calls = 0, nextTimer = 0; const pending = new Promise(resolve => { resolveHttp = resolve; }); const timers = new Map(); const module = load('app/native/nightscout-bridge.ts', { '../ui/dashboard-settings': { nightscoutSiteUrlSetting: { get: () => 'https://example.test' }, nightscoutApiTokenSetting: { get: () => 'test' } }, + '../apps/nightscout/missed-bolus-detector': detector, '../util/http': { async fetchTextWithUserAgent() { calls++; await pending; @@ -248,6 +282,7 @@ test('polling starts once and stops even while the initial HTTP request is pendi assert.equal(timers.size, 0, 'finishing the first refresh must not restart polling'); await bridge.start(); assert.equal(timers.size, 1); - assert.equal(calls, 10); + // Five requests per refresh, plus the last-reading fallback for the empty glucose window. + assert.equal(calls, 12); await bridge.stop(); }); diff --git a/tests/tsconfig.json b/tests/tsconfig.json index a1c2e944..83a3bdb8 100644 --- a/tests/tsconfig.json +++ b/tests/tsconfig.json @@ -53,6 +53,7 @@ "../app/apps/timer/timer-model.ts", "../app/apps/teleprompter/script-tracker.ts", "../app/apps/nightscout/nightscout-alerts.ts", + "../app/apps/nightscout/missed-bolus-detector.ts", "../app/g2/brightness-curve.ts", "../app/g2/events.ts", "../app/apps/t3code/t3-rpc.ts", From 2c325fa1d433b213ab421686ab5932ad12e12123 Mon Sep 17 00:00:00 2001 From: James Babcock Date: Mon, 12 Oct 2026 17:08:41 -0700 Subject: [PATCH 2/2] Center the phone screen preview --- .../faceclaw/app/FaceclawBleCommunicator.kt | 9 ++ .../faceclaw/app/FaceclawPreviewCompositor.kt | 9 ++ .../com/faceclaw/app/PreviewBitmapUtil.kt | 28 +++++++ app/g2/dashboard-controller.ts | 19 +++-- app/g2/glance-host.ts | 10 ++- app/g2/ios-preview-controller.ts | 12 ++- app/g2/phone-preview-crop.ts | 33 ++++++++ app/native/faceclaw-communicator.ios.ts | 6 +- app/native/faceclaw-communicator.ts | 16 +++- app/native/ios-graphics.ts | 20 ++++- app/native/preview-display.ios.ts | 4 +- app/native/preview-display.ts | 13 ++- app/ui/dashboard-settings.ts | 13 +++ app/ui/dashboard/settings-menus.ts | 2 + app/ui/shell/geometry.ts | 52 ++++++++++++ tests/app-layout.test.cjs | 84 +++++++++++++++++++ 16 files changed, 307 insertions(+), 23 deletions(-) create mode 100644 app/g2/phone-preview-crop.ts diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawBleCommunicator.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawBleCommunicator.kt index 24ce8c53..cdff794d 100644 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawBleCommunicator.kt +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawBleCommunicator.kt @@ -242,6 +242,15 @@ class FaceclawBleCommunicator(context: Context, rightAddress: String?, leftAddre java.nio.ByteBuffer.wrap(composite.gray), composite.width, composite.height, brightenGamma, green) } + /** As above, cropped to a screen area (Settings > Phone display > Preview scaling); black past the screen's edges. */ + fun getCompositePreviewBitmap(brightenGamma: Double, green: Boolean, + cropX: Int, cropY: Int, cropWidth: Int, cropHeight: Int): android.graphics.Bitmap? { + val composite = core.previewComposite() ?: return null + return PreviewBitmapUtil.fromGray( + java.nio.ByteBuffer.wrap(composite.gray), composite.width, composite.height, brightenGamma, green, + cropX, cropY, cropWidth, cropHeight) + } + /** * Posted to the main looper after each step of an animation the preview is replaying (menu * highlight slides, scrolls), so the mirror can pull the new frame; frame metrics only cover diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawPreviewCompositor.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawPreviewCompositor.kt index 6ae3b8ab..6c6d4a60 100644 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawPreviewCompositor.kt +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawPreviewCompositor.kt @@ -139,6 +139,15 @@ class FaceclawPreviewCompositor(context: Context) { java.nio.ByteBuffer.wrap(composite.gray), composite.width, composite.height, brightenGamma, green) } + /** As above, cropped to a screen area (Settings > Phone display > Preview scaling); black past the screen's edges. */ + fun getCompositePreviewBitmap(brightenGamma: Double, green: Boolean, + cropX: Int, cropY: Int, cropWidth: Int, cropHeight: Int): android.graphics.Bitmap? { + val composite = compositor.previewComposite() ?: return null + return PreviewBitmapUtil.fromGray( + java.nio.ByteBuffer.wrap(composite.gray), composite.width, composite.height, brightenGamma, green, + cropX, cropY, cropWidth, cropHeight) + } + /** Save the current composite as a 4-bit grayscale PNG; returns the path or "". */ @Throws(java.io.IOException::class) fun saveCompositePngScreenshot(): String { diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/PreviewBitmapUtil.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/PreviewBitmapUtil.kt index 6c98b9b0..75135480 100644 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/PreviewBitmapUtil.kt +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/PreviewBitmapUtil.kt @@ -31,6 +31,34 @@ class PreviewBitmapUtil private constructor() { return Bitmap.createBitmap(colors, width, height, Bitmap.Config.ARGB_8888) } + /** + * As above, showing only the cropWidth x cropHeight area at (cropX, cropY) (the + * mirror's Scaled preview). The area may extend past the frame's edges; it is black + * there. + */ + @JvmStatic + fun fromGray(gray: ByteBuffer?, width: Int, height: Int, brightenGamma: Double, green: Boolean, + cropX: Int, cropY: Int, cropWidth: Int, cropHeight: Int): Bitmap { + if (gray == null || width <= 0 || height <= 0 || gray.remaining() < width * height) { + throw IllegalArgumentException("invalid gray preview buffer") + } + if (cropWidth <= 0 || cropHeight <= 0) { + throw IllegalArgumentException("invalid preview crop") + } + val bytes = ByteArray(width * height) + gray.get(bytes) + val cropped = ByteArray(cropWidth * cropHeight) + val left = maxOf(cropX, 0) + val right = minOf(cropX + cropWidth, width) + if (right > left) { + for (y in maxOf(cropY, 0) until minOf(cropY + cropHeight, height)) { + System.arraycopy(bytes, y * width + left, cropped, (y - cropY) * cropWidth + (left - cropX), right - left) + } + } + val colors = PreviewPalette.expand(cropped, 0, cropped.size, brightenGamma, green) + return Bitmap.createBitmap(colors, cropWidth, cropHeight, Bitmap.Config.ARGB_8888) + } + /** * A white-on-transparent bitmap of an 8bpp glasses icon (gray becomes * alpha; black, which the glasses show as unlit, is transparent), for diff --git a/app/g2/dashboard-controller.ts b/app/g2/dashboard-controller.ts index 53ce6cb8..d172681a 100644 --- a/app/g2/dashboard-controller.ts +++ b/app/g2/dashboard-controller.ts @@ -46,6 +46,7 @@ const MIRROR_TOUCH_GESTURES: Record, WearRemoteI "swipe-right": "swipe-right", }; import { GlanceHost } from "./glance-host"; +import { mirrorToPanelFraction, phonePreviewCrop } from "./phone-preview-crop"; import { type GlanceEvent } from "./glance-state"; import { isPreviewOnlyMode } from "../phone-ui/onboarding-state"; import { claimGlassesOnboardingAutoLaunch, ONBOARDING_APP_ID } from "../apps/onboarding/onboarding-progress"; @@ -69,7 +70,7 @@ import { ALL_APPS } from "../apps/all-apps"; import { type AppContext, type AppDefinition, type AppLaunchParams, type TextEditorHost } from "../apps/app-definition"; import { type InProcessAppOptions, type InProcessWindow } from "../ui/shell/in-process-window"; import { loadPersistedOpenApps, savePersistedOpenApps } from "../ui/shell/open-apps-persistence"; -import { appViewportRect, isOnSwitcherEdge, sidebarStripVisible, type WindowHeightMode } from "../ui/shell/geometry"; +import { appViewportRect, isOnSwitcherEdge, sidebarStripVisible, type ScreenRect, type WindowHeightMode } from "../ui/shell/geometry"; import { type LayerActions, type TextSettingsEditToggle } from "../ui/layers"; import { type KeyboardInputSession } from "../ui/shell/keyboard-input"; import { appSwitcherPositionSetting, statusBarPositionSetting, statusBarVisibilitySetting, windowBorderSetting, assistantAllowProactiveSetting, assistantBackendSetting, assistantBridgeHostSetting, assistantBridgePortSetting, assistantBridgeTokenSetting, getBrightnessPreferences, displayLayoutSetting, navigateDisplayLayoutSetting, terminalDisplayLayoutSetting, elevenLabsApiKeySetting, getStringSettingById, openAiApiKeySetting, nightscoutApiTokenSetting, firmwareDebugFlagsSetting, lockScreenEnabledSetting, nightscoutSiteUrlSetting, onAnySettingChanged, previewColorSetting, ringConnectionModeSetting, saveVoiceRecordingsSetting, sonioxApiKeySetting, screenTimeoutSetting, screenTimeoutSettingToMs, suspendEvenHubWhenScreenOffSetting, voiceProviderSetting, wakeWordActionSetting, type ConfigSettingString } from "../ui/dashboard-settings"; @@ -273,6 +274,8 @@ class DashboardController { private extractedFontsConfirmed = false; private screenRecordingActive = false; private displayPreview: ImageSource | null = createInitialDisplayPreview(); + /** The screen area displayPreview shows (Preview scaling), or null for the whole panel. */ + private displayPreviewCrop: ScreenRect | null = null; private silentMode = false; // Brightness value last pushed to the glasses; see pushBrightness. private lastPushedBrightness: string | null = null; @@ -458,8 +461,9 @@ class DashboardController { this.applyAppLayoutsIfChanged(); this.syncAssistantBridgeIfChanged(); this.syncLockScreenSettingIfChanged(); - // A Preview color change should show on the mirror at once, not at the - // next composited frame (cheap: floor-limited, dedup'd on the Java side). + // A Preview color or scaling change should show on the mirror at once, + // not at the next composited frame (cheap: floor-limited, dedup'd on + // the Java side). this.schedulePreviewUpdate(); }); // Connect to the external agent bridge at boot if configured; the @@ -1931,8 +1935,9 @@ class DashboardController { await this.injectSyntheticRingInput(MIRROR_TOUCH_GESTURES[kind], "watch"); return; } - const x = Math.round(Math.min(1, Math.max(0, nx)) * G2_LENS_WIDTH); - const y = Math.round(Math.min(1, Math.max(0, ny)) * G2_LENS_HEIGHT); + const at = mirrorToPanelFraction(this.displayPreviewCrop, nx, ny); + const x = Math.round(Math.min(1, Math.max(0, at.nx)) * G2_LENS_WIDTH); + const y = Math.round(Math.min(1, Math.max(0, at.ny)) * G2_LENS_HEIGHT); const foreground = shell.foregroundWindow(); const stripShown = sidebarStripVisible(shell.getFocus(), foreground?.appId); this.appendLog(`mirror tap at ${x},${y}`); @@ -2883,8 +2888,10 @@ class DashboardController { const power = activity.getSystemService(android.content.Context.POWER_SERVICE) as android.os.PowerManager; if (!power?.isInteractive()) return; } - const preview = display.getCompositePreview(previewColorSetting.get() === "green"); + const crop = phonePreviewCrop(this.glassesLocked, this.glance.screenRect()); + const preview = display.getCompositePreview(previewColorSetting.get() === "green", crop); if (preview) { + this.displayPreviewCrop = crop; this.setDisplayPreview(preview); } } diff --git a/app/g2/glance-host.ts b/app/g2/glance-host.ts index e92613ad..cf5d05ff 100644 --- a/app/g2/glance-host.ts +++ b/app/g2/glance-host.ts @@ -12,7 +12,7 @@ import { prepareFrameDraws } from "../graphics/glyph-wire"; import { flattenPlanesWithDraws, planesFingerprint, type Plane } from "../graphics/plane"; import * as frameTimings from "../native/frame-timings"; import { type DisplayTarget } from "../native/preview-display"; -import { minWindowTop } from "../ui/shell/geometry"; +import { minWindowTop, type ScreenRect } from "../ui/shell/geometry"; import { beginRenderPass, endRenderPass } from "../util/render-freshness"; import { type GlanceBoardInstance, type GlanceboardProvider } from "../apps/app-definition"; import { GLANCE_HIDDEN, glanceEventForGesture, reduceGlance, type GlanceEvent, type GlanceState } from "./glance-state"; @@ -59,6 +59,8 @@ export class GlanceHost { private rendering = false; private renderPending = false; private nextRenderWantsFreshData = false; + /** Where the last render put the board on screen. */ + private boardRect: ScreenRect | null = null; constructor(private readonly options: GlanceHostOptions) {} @@ -66,6 +68,11 @@ export class GlanceHost { return this.state.visible; } + /** The board's area of the screen while it shows (for the phone mirror's Scaled preview), else null. */ + screenRect(): ScreenRect | null { + return this.state.visible ? this.boardRect : null; + } + /** Whether sleep-time gestures should reach the board at all. */ isEnabled(): boolean { return this.options.getProvider()?.isEnabled() ?? false; @@ -249,6 +256,7 @@ export class GlanceHost { const x = Math.max(0, ((G2_LENS_WIDTH - boardImage.width) / 2) | 0); const y = Math.max(0, Math.min(G2_LENS_HEIGHT - boardImage.height, minWindowTop())); boardImage.composeInto(full, x, y); + this.boardRect = { x, y, width: boardImage.width, height: boardImage.height }; return full; }), ); diff --git a/app/g2/ios-preview-controller.ts b/app/g2/ios-preview-controller.ts index f59de83b..3405dd66 100644 --- a/app/g2/ios-preview-controller.ts +++ b/app/g2/ios-preview-controller.ts @@ -43,7 +43,8 @@ import { makeInputEvent, type InputEvent, type InputEventPayload } from '../ui/g import { noopLayerActions, type LayerActions } from '../ui/layers' import { TextViewerLayer } from '../apps/files/text-viewer' import { shell, rawInputEventToInputEvent, type ShellWindow } from '../ui/shell/shell' -import { appViewportRect, isOnSwitcherEdge, sidebarStripVisible } from '../ui/shell/geometry' +import { appViewportRect, isOnSwitcherEdge, sidebarStripVisible, type ScreenRect } from '../ui/shell/geometry' +import { mirrorToPanelFraction, phonePreviewCrop } from './phone-preview-crop' import { onAnySettingChanged, previewColorSetting, lockScreenEnabledSetting, getBrightnessPreferences, getStringSettingById, nightscoutSiteUrlSetting, nightscoutApiTokenSetting, voiceProviderSetting, elevenLabsApiKeySetting, @@ -116,6 +117,8 @@ export class IosPreviewController { appendLog: message => this.logBluetooth(message), }) private previewTimer: ReturnType | null = null + /** The screen area the mirror's image shows (Preview scaling), or null for the whole panel. */ + private previewCrop: ScreenRect | null = null private clockTimer: ReturnType | null = null private offSettings: (() => void) | null = null private lastBrightness: string | null = null @@ -506,8 +509,10 @@ export class IosPreviewController { this.previewTimer = null if (!this.active) return try { - const image = this.display?.getCompositePreview(previewColorSetting.get() === 'green') + const crop = phonePreviewCrop(this.glassesLocked, this.glance.screenRect()) + const image = this.display?.getCompositePreview(previewColorSetting.get() === 'green', crop) if (!image) return + this.previewCrop = crop this.onFrame(image, this.glassesLocked ? 'Glasses locked' : this.glance.isVisible() ? 'Glanceboard' : `${shell.foregroundWindow()?.title ?? 'Apps'} ยท ${shell.getFocus() === 'sidebar' ? 'App switcher' : 'App'}`) } catch (error) { this.fail(error) } @@ -561,7 +566,8 @@ export class IosPreviewController { await shell.receiveInput(event) this.requestShellRender() } - private async mirrorTap(nx: number, ny: number): Promise { + private async mirrorTap(mirrorNx: number, mirrorNy: number): Promise { + const { nx, ny } = mirrorToPanelFraction(this.previewCrop, mirrorNx, mirrorNy) const x = Math.max(0, Math.min(639, Math.floor(nx * 640))) const y = Math.max(0, Math.min(479, Math.floor(ny * 480))) const window = shell.foregroundWindow() diff --git a/app/g2/phone-preview-crop.ts b/app/g2/phone-preview-crop.ts new file mode 100644 index 00000000..2eafaa51 --- /dev/null +++ b/app/g2/phone-preview-crop.ts @@ -0,0 +1,33 @@ +import { G2_LENS_HEIGHT, G2_LENS_WIDTH } from "../graphics/image"; +import { previewScalingSetting } from "../ui/dashboard-settings"; +import { phonePreviewRect, usedScreenRect, type ScreenRect } from "../ui/shell/geometry"; +import { shell } from "../ui/shell/shell"; + +/** + * The screen area the phone's mirror shows (Settings > Phone display > + * Preview scaling), or null for the whole panel. Scaled zooms to the + * Glanceboard while it shows, else to the area the shell uses around the + * foreground window, centred at the panel's aspect. A locked display gets + * the whole panel: its message box is centred in the panel, not the band. + */ +export function phonePreviewCrop(locked: boolean, glanceRect: ScreenRect | null): ScreenRect | null { + if (locked || previewScalingSetting.get() === "raw") return null; + const window = shell.foregroundWindow(); + const crop = phonePreviewRect(glanceRect ?? usedScreenRect(window?.heightMode ?? "min", window?.appId)); + const wholePanel = crop.x === 0 && crop.y === 0 && crop.width === G2_LENS_WIDTH && crop.height === G2_LENS_HEIGHT; + return wholePanel ? null : crop; +} + +/** + * A point on the mirror, as fractions of the mirror's width and height, + * as fractions of the panel's, given the crop the mirror is showing. + * Points in the black margins past the panel's edges come out below 0 or + * above 1. + */ +export function mirrorToPanelFraction(crop: ScreenRect | null, nx: number, ny: number): { nx: number; ny: number } { + if (!crop) return { nx, ny }; + return { + nx: (crop.x + nx * crop.width) / G2_LENS_WIDTH, + ny: (crop.y + ny * crop.height) / G2_LENS_HEIGHT, + }; +} diff --git a/app/native/faceclaw-communicator.ios.ts b/app/native/faceclaw-communicator.ios.ts index 9bb7ed63..ed8937b4 100644 --- a/app/native/faceclaw-communicator.ios.ts +++ b/app/native/faceclaw-communicator.ios.ts @@ -489,13 +489,13 @@ export class FaceclawCommunicatorBridge { }); } - /** Phone-UI preview of the current composited screen, or null if none yet. */ - getCompositePreview(green = false): ImageSource | null { + /** Phone-UI preview of the current composited screen (or of `crop`'s area of it), or null if none yet. */ + getCompositePreview(green = false, crop: { x: number; y: number; width: number; height: number } | null = null): ImageSource | null { const frame = this.communicator.previewComposite(); if (!frame) return null; const width = Number(this.communicator.compositeWidth()), height = Number(this.communicator.compositeHeight()); if (width <= 0 || height <= 0) return null; - return previewPixels(fromData(frame.pixels), width, height, green); + return previewPixels(fromData(frame.pixels), width, height, green, crop); } /** Save the current composite as a 4-bit grayscale PNG under Documents; returns the path (empty if none). Cropping is not supported on iOS. */ diff --git a/app/native/faceclaw-communicator.ts b/app/native/faceclaw-communicator.ts index c469d2c2..481ae62b 100644 --- a/app/native/faceclaw-communicator.ts +++ b/app/native/faceclaw-communicator.ts @@ -507,11 +507,21 @@ export class FaceclawCommunicatorBridge { /** * Phone-UI preview of the current composited screen, or null if none yet. * `green` tints it green-on-black (the Preview color setting) instead of - * grayscale. + * grayscale; `crop` limits it to a screen area (the Preview scaling + * setting), black where that runs past the screen's edges. */ - getCompositePreview(green = false): ImageSource | null { + getCompositePreview(green = false, crop: { x: number; y: number; width: number; height: number } | null = null): ImageSource | null { if (!global.isAndroid) return null; - const bitmap = this.communicator.getCompositePreviewBitmap(PREVIEW_BRIGHTEN_GAMMA, green); + const bitmap = crop + ? this.communicator.getCompositePreviewBitmap( + PREVIEW_BRIGHTEN_GAMMA, + green, + Math.round(crop.x), + Math.round(crop.y), + Math.round(crop.width), + Math.round(crop.height), + ) + : this.communicator.getCompositePreviewBitmap(PREVIEW_BRIGHTEN_GAMMA, green); return bitmap ? new ImageSource(bitmap) : null; } diff --git a/app/native/ios-graphics.ts b/app/native/ios-graphics.ts index 86e8e72d..1a57d828 100644 --- a/app/native/ios-graphics.ts +++ b/app/native/ios-graphics.ts @@ -3,11 +3,25 @@ import { GrayImage } from '../graphics/image' declare const FaceclawGraphics: any -export function previewPixels(pixels: Uint8Array, width: number, height: number, green = false): ImageSource { +type CropRect = { x: number; y: number; width: number; height: number } + +/** The gray pixels of a screen area; black where it runs past the frame's edges. */ +function cropGray(pixels: Uint8Array, width: number, height: number, crop: CropRect): Uint8Array { + const cropped = new Uint8Array(crop.width * crop.height) + const left = Math.max(crop.x, 0), right = Math.min(crop.x + crop.width, width) + if (right <= left) return cropped + for (let y = Math.max(crop.y, 0); y < Math.min(crop.y + crop.height, height); y++) { + cropped.set(pixels.subarray(y * width + left, y * width + right), (y - crop.y) * crop.width + (left - crop.x)) + } + return cropped +} + +/** Gray pixels as the phone mirror's image, optionally cropped to a screen area (Preview scaling). */ +export function previewPixels(pixels: Uint8Array, width: number, height: number, green = false, crop: CropRect | null = null): ImageSource { // NSData copies the buffer; the UIImage owns its pixels after this call. - const copy = new Uint8Array(pixels) + const copy = crop ? cropGray(pixels, width, height, crop) : new Uint8Array(pixels) const data = NSData.dataWithBytesLength(interop.handleof(copy.buffer), copy.byteLength) - const native = FaceclawGraphics.previewWidthHeightGreen(data, width, height, green) + const native = FaceclawGraphics.previewWidthHeightGreen(data, crop?.width ?? width, crop?.height ?? height, green) if (!native) throw new Error('Could not create preview image') return new ImageSource(native) } diff --git a/app/native/preview-display.ios.ts b/app/native/preview-display.ios.ts index c25628a6..ce85084f 100644 --- a/app/native/preview-display.ios.ts +++ b/app/native/preview-display.ios.ts @@ -138,8 +138,8 @@ export class PreviewDisplayTarget implements DisplayTarget { return this.compositor.composite(); } - getCompositePreview(green = false): ImageSource | null { - return previewPixels(this.compositor.composite(), this.compositor.width, this.compositor.height, green); + getCompositePreview(green = false, crop: { x: number; y: number; width: number; height: number } | null = null): ImageSource | null { + return previewPixels(this.compositor.composite(), this.compositor.width, this.compositor.height, green, crop); } /** Save the current composite as a 4-bit grayscale PNG under Documents; cropping is not supported on iOS. */ diff --git a/app/native/preview-display.ts b/app/native/preview-display.ts index 4f6d7531..8df3f5af 100644 --- a/app/native/preview-display.ts +++ b/app/native/preview-display.ts @@ -160,9 +160,18 @@ export class PreviewDisplayTarget implements DisplayTarget { return Promise.resolve("composited"); } - getCompositePreview(green = false): ImageSource | null { + getCompositePreview(green = false, crop: { x: number; y: number; width: number; height: number } | null = null): ImageSource | null { if (!global.isAndroid) return null; - const bitmap = this.compositor.getCompositePreviewBitmap(PREVIEW_BRIGHTEN_GAMMA, green); + const bitmap = crop + ? this.compositor.getCompositePreviewBitmap( + PREVIEW_BRIGHTEN_GAMMA, + green, + Math.round(crop.x), + Math.round(crop.y), + Math.round(crop.width), + Math.round(crop.height), + ) + : this.compositor.getCompositePreviewBitmap(PREVIEW_BRIGHTEN_GAMMA, green); return bitmap ? new ImageSource(bitmap) : null; } diff --git a/app/ui/dashboard-settings.ts b/app/ui/dashboard-settings.ts index b0652865..51880c80 100644 --- a/app/ui/dashboard-settings.ts +++ b/app/ui/dashboard-settings.ts @@ -616,6 +616,19 @@ export const previewColorSetting = new ConfigSettingEnum({ "How the phone's mirror of the glasses display renders: white/grayscale (clearest), or green to match the physical glasses.", }); +export type PreviewScaling = "scaled" | "raw"; + +export const previewScalingSetting = new ConfigSettingEnum({ + id: "preview-scaling", + label: "Preview scaling", + storageKey: "phone.previewScaling", + defaultValue: "scaled", + values: ["scaled", "raw"], + formatValue: (value) => (value === "raw" ? "Raw" : "Scaled"), + description: + "How much of the glasses display the phone's mirror shows: Scaled centers and enlarges the part the current display mode uses; Raw shows the whole 640x480 panel.", +}); + export const mirrorTouchSetting = new ConfigSettingBoolean({ id: "mirror-touch", label: "Touch mirror", diff --git a/app/ui/dashboard/settings-menus.ts b/app/ui/dashboard/settings-menus.ts index e8d8a7f7..92833593 100644 --- a/app/ui/dashboard/settings-menus.ts +++ b/app/ui/dashboard/settings-menus.ts @@ -57,6 +57,7 @@ import { mirrorTouchSetting, openAiApiKeySetting, previewColorSetting, + previewScalingSetting, phoneRotationSetting, ringConnectionModeSetting, sonioxApiKeySetting, @@ -162,6 +163,7 @@ function settingsSections(): SettingsSection[] { items: [ enumSettingMenuItem(phoneRotationSetting), enumSettingMenuItem(previewColorSetting), + enumSettingMenuItem(previewScalingSetting), toggleSettingMenuItem(mirrorTouchSetting), ], }, diff --git a/app/ui/shell/geometry.ts b/app/ui/shell/geometry.ts index 5be3bfbb..9e6e738d 100644 --- a/app/ui/shell/geometry.ts +++ b/app/ui/shell/geometry.ts @@ -422,3 +422,55 @@ export function appViewportRect(mode: WindowHeightMode, appId?: string): { ...appViewportSize(mode, appId), }; } + +export type ScreenRect = { x: number; y: number; width: number; height: number }; + +function unionRect(a: ScreenRect, b: ScreenRect): ScreenRect { + const x = Math.min(a.x, b.x); + const y = Math.min(a.y, b.y); + return { + x, + y, + width: Math.max(a.x + a.width, b.x + b.width) - x, + height: Math.max(a.y + a.height, b.y + b.height) - y, + }; +} + +/** + * The part of the screen the shell draws in while the given (foreground) + * window is up: the window's band, plus the frame's side pixels; the + * min-height band the shell overlays align to; and a side strip or bottom + * row (a popup shows within the window's content). An immersive window has + * the whole panel. The phone mirror's Scaled preview zooms to this. + */ +export function usedScreenRect(heightMode: WindowHeightMode, appId?: string): ScreenRect { + if (isImmersive(appId)) return { x: 0, y: 0, width: G2_LENS_WIDTH, height: G2_LENS_HEIGHT }; + const frame = windowFramed(appId) ? 1 : 0; + const x = appViewportLeft(appId) - frame; + const width = appViewportWidth(appId) + 2 * frame; + let used = unionRect( + { x, y: windowTop(heightMode, appId), width, height: windowBandHeight(heightMode, appId) }, + { x, y: minWindowTop(appId), width, height: MIN_WINDOW_HEIGHT }, + ); + if (switcherPosition() !== "popup") used = unionRect(used, switcherRect(heightMode, appId)); + return used; +} + +/** + * The screen area the phone mirror shows to present `used` as large as it + * fits, centred, at the panel's own 4:3 aspect (so the mirror keeps its + * shape whatever the display mode): `used` grown along one axis, past the + * panel's edges if need be (the mirror shows black there). + */ +export function phonePreviewRect(used: ScreenRect): ScreenRect { + // Whole 4:3 units, so the aspect is exact. + const units = Math.ceil(Math.max(used.width / 4, used.height / 3)); + const width = units * 4; + const height = units * 3; + return { + x: Math.round(used.x + (used.width - width) / 2), + y: Math.round(used.y + (used.height - height) / 2), + width, + height, + }; +} diff --git a/tests/app-layout.test.cjs b/tests/app-layout.test.cjs index cc31f474..2128a2ad 100644 --- a/tests/app-layout.test.cjs +++ b/tests/app-layout.test.cjs @@ -263,6 +263,90 @@ test('an immersive app fills the panel without chrome rows, frame or depth, in a } }); +test('the phone mirror zooms to the area the shell uses, centred at the panel\'s 4:3', () => { + const g = geometry(); + const preview = (mode, appId) => { + const used = { ...g.usedScreenRect(mode, appId) }; + return { used, shown: { ...g.phonePreviewRect(used) } }; + }; + const panel = { x: 0, y: 0, width: 640, height: 480 }; + // A side strip and band: the panel's width, so only centring vertically. + assert.deepEqual(preview('min'), { used: { x: 0, y: 96, width: 640, height: 288 }, shown: panel }); + g.settings.displayLayoutSetting.value = 'band-top'; + assert.deepEqual(preview('min').shown, { x: 0, y: -96, width: 640, height: 480 }); + g.settings.appSwitcherPositionSetting.value = 'right'; + g.settings.displayLayoutSetting.value = 'band-high'; + assert.deepEqual(preview('min'), { used: { x: 0, y: 48, width: 640, height: 288 }, shown: { x: 0, y: -48, width: 640, height: 480 } }); + // A bottom row: the band with the frame's sides, and the row under it. + g.settings.appSwitcherPositionSetting.value = 'bottom'; + g.settings.displayLayoutSetting.value = 'band-middle'; + assert.deepEqual(preview('min'), { used: { x: 31, y: 78, width: 578, height: 324 }, shown: { x: 30, y: 23, width: 580, height: 435 } }); + // A tall window (the terminal's default) leaves just the frame's margins. + assert.deepEqual(preview('max', 'terminal'), { used: { x: 31, y: 0, width: 578, height: 480 }, shown: panel }); + // A popup shows within the window: just the band. + g.settings.appSwitcherPositionSetting.value = 'popup'; + assert.deepEqual(preview('min'), { used: { x: 31, y: 96, width: 578, height: 288 }, shown: { x: 30, y: 23, width: 580, height: 435 } }); + // Full Screen and immersive windows use the whole panel. + g.settings.displayLayoutSetting.value = 'fullscreen'; + assert.deepEqual(preview('min'), { used: panel, shown: panel }); + g.settings.displayLayoutSetting.value = 'band-middle'; + g.setAppImmersive('onboarding', true); + assert.deepEqual(preview('min', 'onboarding'), { used: panel, shown: panel }); + // Every layout's shown area is exactly 4:3 and contains the used area. + for (const position of ['left', 'right', 'bottom', 'popup']) { + g.settings.appSwitcherPositionSetting.value = position; + for (const layout of LAYOUTS) { + g.settings.displayLayoutSetting.value = layout; + for (const mode of ['min', 'medium', 'max']) { + const { used, shown } = preview(mode); + assert.equal(shown.width * 3, shown.height * 4); + assert.ok(shown.width <= 640 && shown.height <= 480); + assert.ok(shown.x <= used.x && shown.y <= used.y); + assert.ok(shown.x + shown.width >= used.x + used.width && shown.y + shown.height >= used.y + used.height); + } + } + } +}); + +test('the mirror\'s crop follows Preview scaling, the Glanceboard and the lock, and maps taps back to the panel', () => { + const g = geometry(); + const scaling = { value: 'scaled', get() { return this.value; } }; + let foreground = { heightMode: 'min', appId: 'launcher' }; + const modules = { + '../graphics/image': { G2_LENS_WIDTH: 640, G2_LENS_HEIGHT: 480 }, + '../ui/dashboard-settings': { previewScalingSetting: scaling }, + '../ui/shell/geometry': g, + '../ui/shell/shell': { shell: { foregroundWindow: () => foreground } }, + }; + const context = { exports: {}, require: (name) => modules[name] }; + vm.runInNewContext(js(read('app/g2/phone-preview-crop.ts')), context); + const { phonePreviewCrop, mirrorToPanelFraction } = context.exports; + const crop = (locked = false, glance = null) => { + const rect = phonePreviewCrop(locked, glance); + return rect && { ...rect }; + }; + g.settings.appSwitcherPositionSetting.value = 'bottom'; + const band = { x: 30, y: 23, width: 580, height: 435 }; + assert.deepEqual(crop(), band); + // The whole panel is no crop at all. + foreground = { heightMode: 'max', appId: 'terminal' }; + assert.equal(crop(), null); + foreground = null; + assert.deepEqual(crop(), band); + // The Glanceboard while it shows; a locked display's centred box needs the panel. + assert.deepEqual(crop(false, { x: 32, y: 48, width: 576, height: 432 }), { x: 32, y: 48, width: 576, height: 432 }); + assert.deepEqual(crop(false, { x: 32, y: 96, width: 576, height: 288 }), { x: 32, y: 24, width: 576, height: 432 }); + assert.equal(crop(true), null); + scaling.value = 'raw'; + assert.equal(crop(), null); + // Taps: the mirror's corners are the crop's, its margins past the panel's edges. + assert.deepEqual({ ...mirrorToPanelFraction(null, 0.25, 0.75) }, { nx: 0.25, ny: 0.75 }); + assert.deepEqual({ ...mirrorToPanelFraction(band, 0, 0) }, { nx: 30 / 640, ny: 23 / 480 }); + assert.deepEqual({ ...mirrorToPanelFraction(band, 1, 1) }, { nx: 610 / 640, ny: 458 / 480 }); + const above = mirrorToPanelFraction({ x: 0, y: -96, width: 640, height: 480 }, 0.5, 0.1); + assert.ok(above.ny < 0); +}); + // The real chrome layer over the given geometry, with platform pieces stubbed: // a 4px-per-character font that paints each character as a 2x6 block, up to // `notifications` 24px notification icons (keyed n0, n1, ...), and a phone