From 32fa5c0e6e514ffbd5f656f8ee921b6259c0355b Mon Sep 17 00:00:00 2001 From: James Babcock Date: Fri, 6 Nov 2026 23:03:09 -0800 Subject: [PATCH] Fix issue that caused broken Nightscout widget on the status bar on iOS --- app/apps/nightscout/nightscout-app.ts | 3 ++- app/graphics/image.ts | 15 ++++++++++++--- tests/gray-image.test.cjs | 23 +++++++++++++++++++++++ tests/nightscout.test.cjs | 22 +++++++++++++++++++--- 4 files changed, 56 insertions(+), 7 deletions(-) create mode 100644 tests/gray-image.test.cjs diff --git a/app/apps/nightscout/nightscout-app.ts b/app/apps/nightscout/nightscout-app.ts index 6458541a..01032319 100644 --- a/app/apps/nightscout/nightscout-app.ts +++ b/app/apps/nightscout/nightscout-app.ts @@ -84,7 +84,8 @@ function buildNightscoutTrayIcon(state: NightscoutState): GrayImage { const font = getDefaultSmallFont(); const nowMs = Date.now(); const label = state.latest ? `${state.latest.sgv}` : "--"; - const labelWidth = font.measureText(label); + // A TTF face measures fractionally; the icon's size and layout need whole pixels. + const labelWidth = Math.ceil(font.measureText(label)); const warning = evaluateNightscoutAlerts(state, loadNightscoutThresholds(), nowMs).any; const graphEnd = labelWidth + 4 + TRAY_GRAPH_WIDTH; const image = new GrayImage(graphEnd + (warning ? 24 : 0), TRAY_HEIGHT, 0); diff --git a/app/graphics/image.ts b/app/graphics/image.ts index f36a0dd7..00445b64 100644 --- a/app/graphics/image.ts +++ b/app/graphics/image.ts @@ -4,6 +4,9 @@ import type { MenuHighlightAnimation } from "../ui/menu-highlight-motion"; import { Glyph } from "./bdffont"; import { wrapText } from "./textwrap"; +// This file is also built by tests/tsconfig.json, which has no DOM or Node types. +declare const console: { warn(message: string): void }; + export const G2_LENS_WIDTH = 640; export const G2_LENS_HEIGHT = 480; const DEFAULT_CORNER_RADIUS = 8; @@ -156,9 +159,15 @@ export class GrayImage { private memoizedSourceHash: number | null = null; constructor(width: number, height: number, fill = 0) { - this.width = width; - this.height = height; - this.pixels = new Uint8Array(width * height); + // A fractional width would put most rows' `y * width + x` between pixels, + // silently losing them. TTF text measures fractionally, so this is easy to + // hit; the stack is there because the size alone doesn't identify the caller. + if (!Number.isInteger(width) || !Number.isInteger(height)) { + console.warn(`GrayImage: rounding up non-integer size ${width}x${height}\n${new Error().stack}`); + } + this.width = Math.ceil(width); + this.height = Math.ceil(height); + this.pixels = new Uint8Array(this.width * this.height); if (fill !== 0) { this.clear(fill); } diff --git a/tests/gray-image.test.cjs b/tests/gray-image.test.cjs new file mode 100644 index 00000000..bce7b1b9 --- /dev/null +++ b/tests/gray-image.test.cjs @@ -0,0 +1,23 @@ +const test = require('node:test'); +const assert = require('node:assert/strict'); +const { GrayImage } = require('../.test-build/app/graphics/image.js'); + +test('a non-integer size rounds up, warns, and leaves every pixel addressable', t => { + const warn = t.mock.method(console, 'warn', () => {}); + const image = new GrayImage(78.625, 23.5, 7); + assert.deepEqual([image.width, image.height, image.pixels.length], [79, 24, 79 * 24]); + assert.ok(image.pixels.every(value => value === 7)); + for (let y = 0; y < image.height; y++) { + image.setPixel(78, y, 200); + assert.equal(image.getPixel(78, y), 200, `row ${y}`); + } + assert.equal(warn.mock.callCount(), 1); + assert.match(warn.mock.calls[0].arguments[0], /78\.625x23\.5/); +}); + +test('a whole-number size is kept as given, without a warning', t => { + const warn = t.mock.method(console, 'warn', () => {}); + const image = new GrayImage(79, 24); + assert.deepEqual([image.width, image.height], [79, 24]); + assert.equal(warn.mock.callCount(), 0); +}); diff --git a/tests/nightscout.test.cjs b/tests/nightscout.test.cjs index 8dcbb916..34340701 100644 --- a/tests/nightscout.test.cjs +++ b/tests/nightscout.test.cjs @@ -125,14 +125,14 @@ test('overflow warning backgrounds cover graph pixels and deferred graph labels' assert.equal(baked.getPixel(200, 8), 56, 'uncovered graph remains visible'); }); -function fixture(always = false) { +function fixture(always = false, GrayImage = Image, uiFont = font) { 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 }; const bridge = { snapshot: () => state, onStateChange(fn) { notify = fn; fn(state); } }; const settings = { loadNightscoutThresholds: () => limits, nightscoutAlwaysShowInTopBarSetting: { get: () => always }, onAnySettingChanged(fn) { changed = fn; }, isNightscoutSettingsConfigured: () => true }; const deps = { - '../../graphics/image': { GrayImage: Image }, - '../../graphics/ui-fonts': { getDefaultSmallFont: () => font, getDefaultLargeFont: () => font }, + '../../graphics/image': { GrayImage }, + '../../graphics/ui-fonts': { getDefaultSmallFont: () => uiFont, getDefaultLargeFont: () => uiFont }, '../../native/nightscout-bridge': { nightscoutBridge: bridge }, '../../ui/dashboard-settings': settings, './nightscout-alerts': alerts, @@ -181,6 +181,22 @@ test('tray is hidden by default and clears recovered measurement warnings immedi second.close(); assert.equal(f.icon(), null); }); +test('tray keeps every pixel row when a TTF font measures the reading fractionally', () => { + const { GrayImage } = require('../.test-build/app/graphics/image.js'); + // 8.875 is Roboto-Light 16's digit advance under iOS Core Text. + const f = fixture(true, GrayImage, { ...font, measureText: text => text.length * 8.875, drawText() {} }); + f.state.latest = { sgv: 142, timestampMs: now }; + // Spans the graph's full 60..200 range, so its line crosses every row. + f.state.history = [{ timestampMs: now - 10 * 60_000, sgv: 200 }, { timestampMs: now, sgv: 60 }]; + f.app.startNightscoutTrayIcon(); + const icon = f.icon(); + assert.ok(Number.isInteger(icon.width), `width ${icon.width}`); + for (let y = 0; y < icon.height; y++) { + const row = Array.from({ length: icon.width }, (_, x) => icon.getPixel(x, y)); + assert.ok(row.some(value => value !== 0), `row ${y} of the graph is drawn`); + } +}); + test('main page highlights only the breached field and removes highlights on recovery', () => { const f = fixture(); const page = load('app/apps/nightscout/nightscout.ts', {