Fix issue that caused broken Nightscout widget on the status bar on iOS

This commit is contained in:
James Babcock committed 2026-11-06 23:03:09 -08:00
1 parent 6df52021c8
commit 32fa5c0e6e
4 files changed
+56 -7

No files matched your search

+2 -1
View File
@@ -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);
+12 -3
View File
@@ -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);
}
+23
View File
@@ -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);
});
+19 -3
View File
@@ -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', {