Continued UI redesign

This commit is contained in:
James Babcock committed 2026-07-07 12:06:11 -07:00
1 parent 68ab599856
commit 5f82981b92
20 files changed
+872 -161

No files matched your search

+7 -5
View File
@@ -1,5 +1,5 @@
import { getDefaultSmallFont } from "../../graphics/bdffont";
import { G2_LENS_HEIGHT, G2_LENS_WIDTH, GrayImage } from "../../graphics/image";
import { GrayImage } from "../../graphics/image";
import { Layer, type DashboardInputEvent, type LayerContext } from "../layers";
const NOTE_NAMES = ["C", "D", "E", "F", "G", "A", "B"] as const;
@@ -9,19 +9,21 @@ export class BuzzerDemoLayer implements Layer {
private note = 1;
private oct = 2;
paint(_ctx: LayerContext, _paintBelow: () => GrayImage): GrayImage {
const image = new GrayImage(G2_LENS_WIDTH, G2_LENS_HEIGHT, 0);
paint(ctx: LayerContext, _paintBelow: () => GrayImage): GrayImage {
// Sized to the hosting stack (the Debug tests app viewport).
const { width, height } = ctx.stack.getBaseSize();
const image = new GrayImage(width, height, 0);
const font = getDefaultSmallFont();
image.drawText(font, 24, 20, "Buzzer demo", 200);
image.drawText(font, 24, 44, `Note: ${NOTE_NAMES[this.note - 1]} Octave: ${this.oct}`, 180);
image.drawText(font, 24, 68, "Scroll: change note", 150);
image.drawText(font, 24, 84, "Click: play note", 150);
image.drawText(font, 24, 252, "Double-click: back", 130);
image.drawText(font, 24, height - 24, "Double-click: back", 130);
const keyWidth = 64;
const keyY = 130;
const keyX = Math.floor((G2_LENS_WIDTH - keyWidth * NOTE_NAMES.length) / 2);
const keyX = Math.floor((width - keyWidth * NOTE_NAMES.length) / 2);
for (let i = 0; i < NOTE_NAMES.length; i++) {
const selected = i + 1 === this.note;
const x = keyX + i * keyWidth;
+181
View File
@@ -0,0 +1,181 @@
import { getDefaultSmallFont, type BdfFont } from "../../graphics/bdffont";
import { GrayImage } from "../../graphics/image";
import { clamp } from "../../util/numeric-util";
import {
hasAllFilesAccess,
listDirectory,
requestAllFilesAccess,
type DirectoryEntry,
} from "../../native/file-access";
import { Layer, type DashboardInputEvent, type LayerContext } from "../layers";
const HEADER_HEIGHT = 34;
const ROW_HEIGHT = 16;
const LIST_X = 20;
const FOOTER_HEIGHT = 22;
export type FileBrowserOptions = {
startPath: string;
/** Files failing this are listed dimmed and cannot be picked. */
isSupportedFile: (name: string) => boolean;
onFilePicked: (entry: DirectoryEntry, ctx: LayerContext) => void;
/** Double-click while already at startPath (leave the browser). */
onLeave: () => void;
};
type BrowserRow =
| { kind: "grant"; label: string }
| { kind: "info"; label: string }
| { kind: "entry"; label: string; entry: DirectoryEntry; supported: boolean };
/**
* Minimal filesystem browser: scroll to select, click to descend into a
* directory or pick a supported file, double-click to go up (leaving the
* browser when already at the top). Sized to its hosting stack.
*/
export class FileBrowserLayer implements Layer {
private currentPath: string;
private entries: DirectoryEntry[] | null = null;
private listingFailed = false;
private selectedIndex = 0;
private scrollRow = 0;
constructor(private readonly options: FileBrowserOptions) {
this.currentPath = options.startPath;
}
paint(ctx: LayerContext): GrayImage {
const font = getDefaultSmallFont();
const { width, height } = ctx.stack.getBaseSize();
const image = new GrayImage(width, height, 0);
this.loadEntries();
const rows = this.buildRows();
this.selectedIndex = clamp(this.selectedIndex, 0, Math.max(0, rows.length - 1));
image.drawText(font, 20, 8, "Teleprompt files", 220);
image.drawText(font, 20, 22, truncateLeft(font, this.currentPath, width - 40), 130);
const listHeight = height - HEADER_HEIGHT - FOOTER_HEIGHT;
const visibleRows = Math.max(1, (listHeight / ROW_HEIGHT) | 0);
if (this.selectedIndex < this.scrollRow) {
this.scrollRow = this.selectedIndex;
} else if (this.selectedIndex >= this.scrollRow + visibleRows) {
this.scrollRow = this.selectedIndex - visibleRows + 1;
}
this.scrollRow = clamp(this.scrollRow, 0, Math.max(0, rows.length - visibleRows));
const lastVisible = Math.min(rows.length, this.scrollRow + visibleRows);
for (let index = this.scrollRow; index < lastVisible; index++) {
const row = rows[index]!;
const y = HEADER_HEIGHT + (index - this.scrollRow) * ROW_HEIGHT;
const selected = index === this.selectedIndex;
if (selected) {
image.fillRoundedRect(LIST_X - 6, y - 1, width - 2 * LIST_X + 12, ROW_HEIGHT - 1, 15, 4);
image.drawRoundedRect(LIST_X - 6, y - 1, width - 2 * LIST_X + 12, ROW_HEIGHT - 1, 45, 4);
}
const value = rowValue(row, selected);
image.drawText(font, LIST_X, y + 1, truncateRight(font, row.label, width - 2 * LIST_X), value);
}
image.drawText(font, 20, height - 16, "Click: open Double-click: up / back", 110);
return image;
}
async handleInput(event: DashboardInputEvent, ctx: LayerContext): Promise<void> {
const rows = this.buildRows();
switch (event.type) {
case "scroll-up":
this.selectedIndex = Math.max(0, this.selectedIndex - 1);
return;
case "scroll-down":
this.selectedIndex = Math.min(Math.max(0, rows.length - 1), this.selectedIndex + 1);
return;
case "click": {
const row = rows[clamp(this.selectedIndex, 0, Math.max(0, rows.length - 1))];
if (!row) return;
if (row.kind === "grant") {
requestAllFilesAccess();
this.entries = null; // re-list after the user returns from Settings
return;
}
if (row.kind !== "entry") return;
if (row.entry.isDirectory) {
this.navigateTo(row.entry.path);
} else if (row.supported) {
this.options.onFilePicked(row.entry, ctx);
}
return;
}
case "double-click": {
if (this.currentPath === this.options.startPath) {
this.options.onLeave();
return;
}
const parent = this.currentPath.slice(0, this.currentPath.lastIndexOf("/"));
this.navigateTo(parent.length >= this.options.startPath.length ? parent : this.options.startPath);
return;
}
default:
return;
}
}
private navigateTo(path: string): void {
this.currentPath = path;
this.entries = null;
this.selectedIndex = 0;
this.scrollRow = 0;
}
private loadEntries(): void {
if (this.entries !== null) return;
const listing = listDirectory(this.currentPath);
this.listingFailed = listing === null;
this.entries = listing ?? [];
}
private buildRows(): BrowserRow[] {
this.loadEntries();
const rows: BrowserRow[] = [];
if (!hasAllFilesAccess()) {
rows.push({ kind: "grant", label: "Grant file access (opens phone Settings)" });
}
for (const entry of this.entries ?? []) {
rows.push({
kind: "entry",
entry,
supported: entry.isDirectory || this.options.isSupportedFile(entry.name),
label: entry.isDirectory ? `${entry.name}/` : entry.name,
});
}
if (!rows.length || (this.listingFailed && rows.every((row) => row.kind !== "entry"))) {
rows.push({ kind: "info", label: this.listingFailed ? "(unreadable directory)" : "(empty directory)" });
}
return rows;
}
}
function rowValue(row: BrowserRow, selected: boolean): number {
if (row.kind === "info") return 120;
if (row.kind === "grant") return selected ? 255 : 210;
if (!row.supported) return selected ? 140 : 100;
return selected ? 255 : 200;
}
function truncateRight(font: BdfFont, text: string, maxWidth: number): string {
if (font.measureText(text) <= maxWidth) return text;
let out = text;
while (out.length > 1 && font.measureText(`${out}...`) > maxWidth) {
out = out.slice(0, -1);
}
return `${out}...`;
}
function truncateLeft(font: BdfFont, text: string, maxWidth: number): string {
if (font.measureText(text) <= maxWidth) return text;
let out = text;
while (out.length > 1 && font.measureText(`...${out}`) > maxWidth) {
out = out.slice(1);
}
return `...${out}`;
}
+12 -12
View File
@@ -72,9 +72,6 @@ export class NightscoutDashboardPlugin extends DashboardPlugin {
);
}
override createFullscreenLayer(getState: () => DashboardPluginState): Layer {
return new NightscoutLayer(getState);
}
}
function drawNightscoutDelta(
@@ -366,28 +363,30 @@ function drawNightscoutReferenceLine(
}
class NightscoutLayer implements Layer {
export class NightscoutLayer implements Layer {
constructor(private readonly getState: () => DashboardPluginState) {}
paint(ctx: LayerContext): GrayImage {
const font = getDefaultSmallFont();
const image = new GrayImage(576, 288, 0);
// Sized to the hosting stack (the Nightscout app viewport).
const { width, height } = ctx.stack.getBaseSize();
const image = new GrayImage(width, height, 0);
const nightscout = this.getState().nightscout;
const nowMs = Date.now();
image.drawRect(12, 12, 552, 264, 52);
const footerY = height - 26;
image.drawRect(12, 12, width - 24, height - 24, 52);
image.drawText(font, 22, 16, "Nightscout", 220);
if (!this.getState().nightscoutConfigured || nightscout.configurationMissing) {
image.drawText(font, 22, 44, "Nightscout needs configuration.", 180);
image.drawText(font, 22, 62, "Double-click and use Settings", 140);
image.drawText(font, 22, 78, "Integrations > Nightscout.", 140);
image.drawText(font, 22, 62, "Use Settings > Integrations > Nightscout.", 140);
return image;
}
if (!nightscout.available || !nightscout.latest) {
image.drawText(font, 22, 44, "No Nightscout data available.", 180);
image.drawText(font, 22, 58, truncateLine(nightscout.status, 60), 140);
image.drawText(font, 22, 252, "Click: refresh Double-click: back", 110);
image.drawText(font, 22, footerY, "Click: refresh Double-click: back", 110);
return image;
}
@@ -418,9 +417,10 @@ class NightscoutLayer implements Layer {
160,
);
image.drawText(font, 22, 110, `Pump ${truncateLine(nightscout.pumpStatus || "--", 56)}`, 150);
drawNightscoutGraph(image, { x: 22, y: 128, width: 532, height: 96 }, nightscout, nowMs, font);
image.drawText(font, 22, 234, "2-hour glucose history with basal / carbs / boluses", 130);
image.drawText(font, 22, 252, "Click: refresh Double-click: back", 110);
const graphHeight = Math.max(40, height - 128 - 48);
drawNightscoutGraph(image, { x: 22, y: 128, width: width - 44, height: graphHeight }, nightscout, nowMs, font);
image.drawText(font, 22, height - 42, "2-hour glucose history with basal / carbs / boluses", 130);
image.drawText(font, 22, footerY, "Click: refresh Double-click: back", 110);
return image;
}
+9 -10
View File
@@ -1,16 +1,11 @@
import { getDefaultSmallFont } from "../../graphics/bdffont";
import { G2_LENS_HEIGHT, G2_LENS_WIDTH, GrayImage } from "../../graphics/image";
import { GrayImage } from "../../graphics/image";
import { Layer, type DashboardInputEvent, type LayerContext } from "../layers";
const COLOR_VALUE_COUNT = 256;
const COLOR_VALUE_STEP = 1;
const SWATCH_COUNT = COLOR_VALUE_COUNT / COLOR_VALUE_STEP;
const GRID_COLUMNS = 16;
const GRID_ROWS = Math.ceil(SWATCH_COUNT / GRID_COLUMNS);
const CELL_WIDTH = Math.floor(G2_LENS_WIDTH / GRID_COLUMNS);
//const CELL_HEIGHT = Math.floor(G2_LENS_HEIGHT / GRID_ROWS);
const CELL_HEIGHT = 30;
const GRID_X = Math.floor((G2_LENS_WIDTH - CELL_WIDTH * GRID_COLUMNS) / 2);
const LABEL_Y_OFFSET = 0;
const SWATCH_Y_OFFSET = 12;
const SWATCH_WIDTH = 24;
@@ -18,18 +13,22 @@ const SWATCH_HEIGHT = 16;
export class ScreenTestLayer implements Layer {
paint(ctx: LayerContext): GrayImage {
const image = new GrayImage(G2_LENS_WIDTH, G2_LENS_HEIGHT, 0);
// Sized to the hosting stack (the Debug tests app viewport).
const { width, height } = ctx.stack.getBaseSize();
const image = new GrayImage(width, height, 0);
const font = getDefaultSmallFont();
const cellWidth = Math.floor(width / GRID_COLUMNS);
const gridX = Math.floor((width - cellWidth * GRID_COLUMNS) / 2);
for (let value = 0; value < COLOR_VALUE_COUNT; value += COLOR_VALUE_STEP) {
const slot = value / COLOR_VALUE_STEP;
const col = slot % GRID_COLUMNS;
const row = Math.floor(slot / GRID_COLUMNS);
const cellX = GRID_X + col * CELL_WIDTH;
const cellX = gridX + col * cellWidth;
const cellY = row * CELL_HEIGHT;
const label = String(value);
const labelX = cellX + Math.max(0, Math.floor((CELL_WIDTH - font.measureText(label)) / 2));
const swatchX = cellX + Math.floor((CELL_WIDTH - SWATCH_WIDTH) / 2);
const labelX = cellX + Math.max(0, Math.floor((cellWidth - font.measureText(label)) / 2));
const swatchX = cellX + Math.floor((cellWidth - SWATCH_WIDTH) / 2);
image.drawText(font, labelX, cellY + LABEL_Y_OFFSET, label, 185);
fillSwatch(image, swatchX, cellY + SWATCH_Y_OFFSET, SWATCH_WIDTH, SWATCH_HEIGHT, value);
+51 -43
View File
@@ -1,59 +1,57 @@
import { wrapText } from "../../graphics/textwrap";
import { getDefaultSmallFont, type BdfFont } from "../../graphics/bdffont";
import { G2_LENS_HEIGHT, G2_LENS_WIDTH, GrayImage } from "../../graphics/image";
import { GrayImage } from "../../graphics/image";
import { Layer, type DashboardInputEvent, type LayerContext } from "../layers";
const MARGIN_X = 18;
const TITLE_Y = 16;
const BODY_X = 18;
const BODY_Y = 44;
const BODY_WIDTH = G2_LENS_WIDTH - BODY_X - 12;
const LINE_STEP = 14;
const FOOTER_Y = 252;
const BODY_LINE_COUNT = Math.floor((FOOTER_Y - BODY_Y) / LINE_STEP);
const PAGE_STEP = Math.max(1, BODY_LINE_COUNT - 1);
const FOOTER_MARGIN = 36;
/**
* Paged text viewer for a document. Sized to its hosting stack (a teleprompt
* document window, or pushed over the file browser for view-in-place).
*/
export class TelepromptLayer implements Layer {
private lines: string[] | null = null;
private wrappedForWidth = 0;
private firstLine = 0;
private bodyLineCount = 14;
constructor(private readonly documentText: string | null) {}
constructor(
private readonly documentText: string,
private readonly title = "Teleprompt",
) {}
paint(ctx: LayerContext): GrayImage {
const font = getDefaultSmallFont();
const image = new GrayImage(G2_LENS_WIDTH, G2_LENS_HEIGHT, 0);
image.drawRect(12, 12, G2_LENS_WIDTH - 24, G2_LENS_HEIGHT - 24, 52);
image.drawText(font, MARGIN_X + 4, TITLE_Y, "Teleprompt", 220);
const { width, height } = ctx.stack.getBaseSize();
const image = new GrayImage(width, height, 0);
const footerY = height - FOOTER_MARGIN;
this.bodyLineCount = Math.max(1, Math.floor((footerY - BODY_Y) / LINE_STEP));
image.drawRect(12, 12, width - 24, height - 24, 52);
image.drawText(font, MARGIN_X + 4, TITLE_Y, truncateToWidth(font, this.title, width - 2 * MARGIN_X - 8), 220);
if (this.documentText === null) {
const message = [
"Open a plain text document on your phone and Share it to Faceclaw.",
];
for (let index = 0; index < message.length; index++) {
image.drawText(font, BODY_X, BODY_Y + index * LINE_STEP, message[index]!, 200);
}
return image;
}
const lines = this.getLines(font);
const visibleLines = lines.slice(this.firstLine, this.firstLine + BODY_LINE_COUNT);
const lines = this.getLines(font, width);
const visibleLines = lines.slice(this.firstLine, this.firstLine + this.bodyLineCount);
for (let index = 0; index < visibleLines.length; index++) {
image.drawText(font, BODY_X, BODY_Y + index * LINE_STEP, visibleLines[index]!, 230);
}
const currentPage = Math.floor(this.firstLine / PAGE_STEP) + 1;
const totalPages = totalPageCount(lines.length);
image.drawText(font, BODY_X, FOOTER_Y, `Page ${currentPage}/${totalPages}`, 110);
const currentPage = Math.floor(this.firstLine / this.pageStep()) + 1;
image.drawText(font, BODY_X, footerY, `Page ${currentPage}/${this.totalPageCount(lines.length)}`, 110);
return image;
}
handleInput(event: DashboardInputEvent, ctx: LayerContext): void {
switch (event.type) {
case "scroll-down":
this.scrollBy(PAGE_STEP);
this.scrollBy(this.pageStep());
return;
case "scroll-up":
this.scrollBy(-PAGE_STEP);
this.scrollBy(-this.pageStep());
return;
case "double-click":
ctx.stack.pop();
@@ -63,9 +61,13 @@ export class TelepromptLayer implements Layer {
}
}
private pageStep(): number {
return Math.max(1, this.bodyLineCount - 1);
}
private scrollBy(delta: number): void {
const lines = this.lines ?? [];
if (lines.length <= BODY_LINE_COUNT) {
if (lines.length <= this.bodyLineCount) {
this.firstLine = 0;
return;
}
@@ -73,25 +75,31 @@ export class TelepromptLayer implements Layer {
this.firstLine = Math.max(0, Math.min(maxFirstLine, this.firstLine + delta));
}
private getLines(font: BdfFont): string[] {
if (this.lines === null) {
this.lines = wrapTextForTeleprompt(font, this.documentText ?? "");
private getLines(font: BdfFont, width: number): string[] {
if (this.lines === null || this.wrappedForWidth !== width) {
const normalized = this.documentText.replace(/\t/g, " ").replace(/\r/g, "");
this.lines = wrapText(font, normalized, width - BODY_X - 12, {
preserveLeadingWhitespace: true,
breakLongWords: true,
});
this.wrappedForWidth = width;
}
return this.lines;
}
}
function wrapTextForTeleprompt(font: BdfFont, text: string): string[] {
const normalized = text.replace(/\t/g, " ").replace(/\r/g, "");
return wrapText(font, normalized, BODY_WIDTH, {
preserveLeadingWhitespace: true,
breakLongWords: true,
});
}
function totalPageCount(lineCount: number): number {
if (lineCount <= BODY_LINE_COUNT) {
return 1;
private totalPageCount(lineCount: number): number {
if (lineCount <= this.bodyLineCount) {
return 1;
}
return Math.ceil((lineCount - this.bodyLineCount) / this.pageStep()) + 1;
}
return Math.ceil((lineCount - BODY_LINE_COUNT) / PAGE_STEP) + 1;
}
function truncateToWidth(font: BdfFont, text: string, maxWidth: number): string {
if (font.measureText(text) <= maxWidth) return text;
let out = text;
while (out.length > 1 && font.measureText(`${out}...`) > maxWidth) {
out = out.slice(0, -1);
}
return `${out}...`;
}