From a7af84e2316196d6e64c88e3fb6e6586bf25ddb2 Mon Sep 17 00:00:00 2001 From: James Babcock Date: Mon, 21 Sep 2026 18:07:37 -0700 Subject: [PATCH] Nightscout glanceboard widget shows out-of-bounds values when out of space --- CHANGELOG | 1 + .../glanceboard/widgets/nightscout-widget.ts | 45 +++++++----- tests/nightscout.test.cjs | 70 +++++++++++++++++++ 3 files changed, 99 insertions(+), 17 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 202fd417..4b57d7fb 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -6,6 +6,7 @@ * Add tokens and faceclaw-input script, which allows using a CLI utility as an input device for the glasses * Navigate app: Add map-without-destination mode +* Nightscout app: Glanceboard widget still shows out-of-bounds values when out of space ### iOS port: diff --git a/app/apps/glanceboard/widgets/nightscout-widget.ts b/app/apps/glanceboard/widgets/nightscout-widget.ts index d9425d84..14139486 100644 --- a/app/apps/glanceboard/widgets/nightscout-widget.ts +++ b/app/apps/glanceboard/widgets/nightscout-widget.ts @@ -1,4 +1,4 @@ -import { type GrayImage } from "../../../graphics/image"; +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"; @@ -91,22 +91,6 @@ export class NightscoutWidget implements GlanceWidget { image.drawText(small, PAD, y, `IOB ${state.iob === null ? "--" : state.iob.toFixed(2)}`, 160); y += step; - // Breached limits only, one per line while they fit, inverted like the - // app's warning fields. Nothing here means nothing is out of range. - const alerts = evaluateNightscoutAlerts(state, loadNightscoutThresholds(), nowMs); - const warnings: string[] = []; - if (alerts.cannula) warnings.push(`CAGE ${formatAgeShortFromTimestamp(state.cageTimestampMs, nowMs)}`); - if (alerts.cartridge) warnings.push(`Reservoir ${state.reservoirUnits === null ? "--" : Math.round(state.reservoirUnits)}U`); - if (alerts.battery) warnings.push(`Battery ${state.batteryVoltage === null ? "--" : state.batteryVoltage.toFixed(2)}V`); - if (alerts.loop) warnings.push(`Loop ${formatAgeShortFromTimestamp(state.loopTimestampMs, nowMs)}`); - for (const warning of warnings) { - if (y + small.lineHeight > image.height - 2) break; - const text = truncateText(small, warning, READOUT_WIDTH - 4); - image.fillRect(PAD - 2, y - 1, small.measureText(text) + 4, small.lineHeight + 2, 230); - image.drawText(small, PAD, y, text, 1); - y += step; - } - const graphX = PAD + READOUT_WIDTH + PAD; drawNightscoutGraph( image, @@ -115,5 +99,32 @@ export class NightscoutWidget implements GlanceWidget { nowMs, small, ); + + // Breached limits only, inverted like the app's warning fields. Continue + // at the top of a second column over the graph when the readout is full. + const alerts = evaluateNightscoutAlerts(state, loadNightscoutThresholds(), nowMs); + const warnings: string[] = []; + if (alerts.cannula) warnings.push(`CAGE ${formatAgeShortFromTimestamp(state.cageTimestampMs, nowMs)}`); + if (alerts.cartridge) warnings.push(`Reservoir ${state.reservoirUnits === null ? "--" : Math.round(state.reservoirUnits)}U`); + if (alerts.battery) warnings.push(`Battery ${state.batteryVoltage === null ? "--" : state.batteryVoltage.toFixed(2)}V`); + if (alerts.loop) warnings.push(`Loop ${formatAgeShortFromTimestamp(state.loopTimestampMs, nowMs)}`); + let warningX = PAD; + for (const warning of warnings) { + if (y + small.lineHeight > image.height - 2) { + warningX = graphX; + y = PAD; + } + const text = truncateText(small, warning, READOUT_WIDTH - 4); + const width = Math.ceil(small.measureText(text)) + 4; + if (warningX === PAD) { + image.fillRect(warningX - 2, y - 1, width, small.lineHeight + 2, 230); + } else { + // Graph labels are deferred glyphs: a deferred opaque background + // covers those as well as the graph's raster lines. + image.drawImage(new GrayImage(width, small.lineHeight + 2, 230), warningX - 2, y - 1); + } + image.drawText(small, warningX, y, text, 1); + y += step; + } } } diff --git a/tests/nightscout.test.cjs b/tests/nightscout.test.cjs index 03e69c2b..9251c491 100644 --- a/tests/nightscout.test.cjs +++ b/tests/nightscout.test.cjs @@ -54,6 +54,76 @@ class Image { fillRect(...args) { this.draws.push(['rect', ...args]); } } const font = { measureText: text => text.length * 6, lineHeight: 12, hasGlyph: () => true }; + +function paintWidget(smallHeight, largeHeight, height = 144, measurements = {}) { + const { GrayImage } = require('../.test-build/app/graphics/image.js'); + const textDraws = []; + const small = { + ...font, lineHeight: smallHeight, ascent: 1, + drawText(image, x, y, text, value) { + textDraws.push({ x, y, text, value }); + // A tiny deferred glyph leaves room to inspect the opaque background. + image.drawGlyph(this, { bbxWidth: 1, bbxHeight: 1, bbxX: 2, bbxY: 0, bitmapRows: [128] }, x, y, value); + }, + }; + const state = { + available: true, latest: { sgv: 100, timestampMs: now }, units: 'mg/dL', delta: 0, direction: '', iob: 1, + cageTimestampMs: now - 100 * 3_600_000, reservoirUnits: 1, batteryVoltage: 1, loopTimestampMs: now - 60 * 60_000, + ...measurements, + }; + const { NightscoutWidget } = load('app/apps/glanceboard/widgets/nightscout-widget.ts', { + '../../../graphics/image': { GrayImage }, + '../../../graphics/ui-fonts': { getDefaultSmallFont: () => small, getDefaultLargeFont: () => ({ ...small, lineHeight: largeHeight }) }, + '../../../graphics/textwrap': require('../.test-build/app/graphics/textwrap.js'), + '../../../native/nightscout-bridge': { nightscoutBridge: { snapshot: () => state } }, + '../../../ui/dashboard-settings': { isNightscoutSettingsConfigured: () => true, loadNightscoutThresholds: () => limits }, + '../../../ui/metrics': { lineStep: font => font.lineHeight + 2 }, + '~/util/date-util': { formatAgeShortFromTimestamp: () => '1h' }, + '../../nightscout/nightscout-alerts': alerts, + '../../nightscout/nightscout': { + formatDelta: () => '+0', isNightscoutPointStale: () => false, drawDirectionIndicator() {}, + drawNightscoutGraph(image, bounds) { + image.fillRect(bounds.x, bounds.y, bounds.width, bounds.height, 56); + image.drawText(small, bounds.x - 2, bounds.y, 'graph label', 150); + }, + }, + }, { Date: { now: () => now } }); + const image = new GrayImage(288, height); + new NightscoutWidget().paint(image); + return { image, warnings: textDraws.filter(draw => draw.value === 1) }; +} + +test('glanceboard keeps fitting warnings in the readout and flows the rest over the graph', () => { + for (const [small, large] of [[8, 24], [12, 24], [16, 26], [21, 30]]) { + const { warnings } = paintWidget(small, large); + assert.deepEqual(warnings.map(draw => draw.text.split(' ')[0]), ['CAGE', 'Reservoir', 'Battery', 'Loop']); + assert.ok(warnings.every(draw => draw.y >= 8 && draw.y + small <= 142)); + const firstY = 8 + 4 * (small + 2) + large + 2; + const fitting = Math.max(0, Math.min(4, Math.floor((142 - firstY - small) / (small + 2)) + 1)); + assert.equal(warnings.filter(draw => draw.x === 8).length, fitting); + if (fitting < 4) { + assert.equal(warnings[fitting].y, 8); + assert.ok(warnings.slice(fitting).every(draw => draw.x === 112)); + } + } +}); + +test('glanceboard wraps only when a warning exceeds the bottom margin', () => { + assert.equal(paintWidget(12, 24, 104).warnings[0].x, 8); + assert.equal(paintWidget(12, 24, 103).warnings[0].x, 112); + assert.ok(paintWidget(21, 30, 240).warnings.every(draw => draw.x === 8)); + assert.equal(paintWidget(21, 30, 144, healthy).warnings.length, 0); +}); + +test('overflow warning backgrounds cover graph pixels and deferred graph labels', () => { + const { image } = paintWidget(21, 30); + const baked = image.withDrawsBaked(); + assert.equal(baked.getPixel(114, 7), 230, 'background covers the graph'); + assert.equal(baked.getPixel(112, 8), 230, 'background covers the deferred graph label'); + assert.equal(baked.getPixel(114, 8), 1, 'warning glyph remains visible'); + assert.equal(baked.getPixel(200, 8), 56, 'uncovered graph remains visible'); +}); + function fixture(always = false) { let time = now, icon, tick, changed, notify, renders = 0; const state = { ...healthy, available: true, latest: { sgv: 100, timestampMs: now }, history: [], units: 'mg/dL', delta: 0, direction: '', iob: 1, cob: 0, pumpStatus: '0m ago', configurationMissing: false };