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

+112 -14
View File
@@ -1,8 +1,11 @@
import { G2_LENS_HEIGHT, G2_LENS_WIDTH, GrayImage } from "../../graphics/image";
import { getDefaultMediumFont } from "../../graphics/bdffont";
import { drawBattery } from "../../graphics/battery";
import { getDefaultMediumFont, getDefaultSmallFont } from "../../graphics/bdffont";
import { BATTERY_ICON_WIDTH, drawBattery } from "../../graphics/battery";
import { readActiveNotificationIcons } from "../../native/notification-icons";
import { readPhoneBatteryState } from "../../native/phone-battery";
import { noteStaleDataUsed, renderPassAllowsStaleData } from "../../util/render-freshness";
import { clamp } from "../../util/numeric-util";
import { batteryDisplayModeSetting } from "../dashboard-settings";
import { Layer } from "../layers";
import { SHELL_OPAQUE_BLACK, SIDEBAR_WIDTH, TOP_BAR_HEIGHT } from "./geometry";
@@ -26,6 +29,8 @@ export type ShellChromeState = {
selectedIndex: number;
focus: "sidebar" | "window";
battery: { headset: number | null; headsetCharging: boolean | null };
/** App-provided tray images, drawn between notification icons and batteries. */
trayIcons: GrayImage[];
};
/** Reusable placeholder window icon: rounded outline with a single letter. */
@@ -45,6 +50,9 @@ export function makeLetterWindowIcon(letter: string): ShellChromeWindow["drawIco
* the color-key shell surface), so the app viewport shows through.
*/
export class ShellChromeLayer implements Layer {
// First sidebar row shown; adjusted each paint to keep the selection visible.
private scrollRow = 0;
constructor(private readonly getState: () => ShellChromeState) {}
paint(): GrayImage {
@@ -63,10 +71,25 @@ export class ShellChromeLayer implements Layer {
private drawSidebar(image: GrayImage, state: ShellChromeState): void {
image.fillRect(0, 0, SIDEBAR_WIDTH, G2_LENS_HEIGHT, SHELL_OPAQUE_BLACK);
image.drawLine(SIDEBAR_WIDTH - 1, 0, SIDEBAR_WIDTH - 1, G2_LENS_HEIGHT - 1, BORDER_VALUE);
for (let index = 0; index < state.windows.length; index++) {
// Scroll the icon list to keep the selection visible; chevrons mark
// windows off-screen above/below.
const listTop = TOP_BAR_HEIGHT + 10;
const itemStride = ICON_SIZE + ICON_SPACING;
const listHeight = G2_LENS_HEIGHT - listTop - 10;
const visibleCount = Math.max(1, ((listHeight + ICON_SPACING) / itemStride) | 0);
const count = state.windows.length;
if (state.selectedIndex < this.scrollRow) {
this.scrollRow = state.selectedIndex;
} else if (state.selectedIndex >= this.scrollRow + visibleCount) {
this.scrollRow = state.selectedIndex - visibleCount + 1;
}
this.scrollRow = clamp(this.scrollRow, 0, Math.max(0, count - visibleCount));
const lastVisible = Math.min(count, this.scrollRow + visibleCount);
for (let index = this.scrollRow; index < lastVisible; index++) {
const window = state.windows[index]!;
const y = TOP_BAR_HEIGHT + 6 + index * (ICON_SIZE + ICON_SPACING);
if (y + ICON_SIZE > G2_LENS_HEIGHT) break;
const y = listTop + (index - this.scrollRow) * itemStride;
if (index === state.selectedIndex) {
const highlight = state.focus === "sidebar" ? 200 : 70;
image.drawRoundedRect(0, y - 2, SIDEBAR_WIDTH - 3, ICON_SIZE + 4, highlight, 6);
@@ -76,6 +99,13 @@ export class ShellChromeLayer implements Layer {
image.fillRoundedRect(ICON_MARGIN_X + ICON_SIZE - 7, y - 1, 8, 8, 255, 4);
}
}
if (this.scrollRow > 0) {
drawChevron(image, SIDEBAR_WIDTH / 2, TOP_BAR_HEIGHT + 6, -1);
}
if (lastVisible < count) {
drawChevron(image, SIDEBAR_WIDTH / 2, G2_LENS_HEIGHT - 6, 1);
}
}
private drawTopBar(image: GrayImage, state: ShellChromeState): void {
@@ -90,17 +120,11 @@ export class ShellChromeLayer implements Layer {
const textY = Math.max(0, ((TOP_BAR_HEIGHT - font.lineHeight) / 2) | 0);
image.drawText(font, clockX, textY, clock, 210);
let batteryLeft = G2_LENS_WIDTH;
if (state.battery.headset !== null) {
const battery = drawBattery(state.battery.headset, Boolean(state.battery.headsetCharging));
batteryLeft = G2_LENS_WIDTH - battery.width - 8;
image.bitBlt(battery, batteryLeft, Math.max(0, ((TOP_BAR_HEIGHT - battery.height) / 2) | 0), {
transparentZero: true,
});
}
const batteryLeft = this.drawTopBarBatteries(image, state);
const trayLeft = drawTrayIcons(image, state.trayIcons, batteryLeft);
const iconsX = clockX + font.measureText(clock) + 16;
const maxIcons = Math.max(0, ((batteryLeft - 8 - iconsX) / (NOTIFICATION_ICON_SIZE + 4)) | 0);
const maxIcons = Math.max(0, ((trayLeft - 8 - iconsX) / (NOTIFICATION_ICON_SIZE + 4)) | 0);
if (maxIcons > 0) {
const { icons, stale } = readActiveNotificationIcons(maxIcons, renderPassAllowsStaleData());
if (stale) {
@@ -114,4 +138,78 @@ export class ShellChromeLayer implements Layer {
}
}
}
/**
* Labelled battery indicators for the phone and the G2, right-aligned in
* the top bar, following the dashboard card's icon/percentage setting.
* Returns the left edge of the battery block.
*/
private drawTopBarBatteries(image: GrayImage, state: ShellChromeState): number {
const font = getDefaultSmallFont();
const percentageMode = batteryDisplayModeSetting.get() === "percentage";
type BatteryItem = { label: string; percent: number; charging: boolean };
const items: BatteryItem[] = [];
const phone = readPhoneBatteryState();
if (phone.battery !== null && Number.isFinite(phone.battery)) {
items.push({ label: "Phone", percent: phone.battery, charging: Boolean(phone.charging) });
}
if (state.battery.headset !== null && Number.isFinite(state.battery.headset)) {
items.push({ label: "G2", percent: state.battery.headset, charging: Boolean(state.battery.headsetCharging) });
}
if (!items.length) return G2_LENS_WIDTH;
const labelGap = 5;
const itemGap = 12;
const textY = Math.max(0, ((TOP_BAR_HEIGHT - font.lineHeight) / 2) | 0);
let x = G2_LENS_WIDTH - 8;
for (let index = items.length - 1; index >= 0; index--) {
const item = items[index]!;
const percentText = `${Math.max(0, Math.min(100, Math.round(item.percent)))}%`;
const valueWidth = percentageMode ? font.measureText(percentText) : BATTERY_ICON_WIDTH;
const labelWidth = font.measureText(item.label);
x -= labelWidth + labelGap + valueWidth;
image.drawText(font, x, textY, item.label, 150);
const valueX = x + labelWidth + labelGap;
if (percentageMode) {
if (item.charging) {
// Inverted text marks charging, matching the dashboard card.
image.fillRect(valueX - 2, textY - 1, valueWidth + 4, font.lineHeight + 2, 255);
image.drawText(font, valueX, textY, percentText, 1);
} else {
image.drawText(font, valueX, textY, percentText, 200);
}
} else {
const icon = drawBattery(item.percent, item.charging);
image.bitBlt(icon, valueX, Math.max(0, ((TOP_BAR_HEIGHT - icon.height) / 2) | 0), {
transparentZero: true,
});
}
x -= itemGap;
}
return x + itemGap;
}
}
/**
* Draw app tray icons right-to-left, ending just left of the battery block;
* returns the left edge of the tray region.
*/
function drawTrayIcons(image: GrayImage, trayIcons: GrayImage[], rightEdge: number): number {
let x = rightEdge;
for (let index = trayIcons.length - 1; index >= 0; index--) {
const icon = trayIcons[index]!;
x -= icon.width + 10;
image.bitBlt(icon, x, Math.max(0, ((TOP_BAR_HEIGHT - icon.height) / 2) | 0), {
transparentZero: true,
});
}
return x;
}
/** Small triangle marker for sidebar overflow; direction -1 = up, 1 = down. */
function drawChevron(image: GrayImage, centerX: number, y: number, direction: -1 | 1): void {
const half = 5;
const tipY = direction < 0 ? y - 3 : y + 3;
image.drawLine(centerX - half, y, centerX, tipY, 140);
image.drawLine(centerX, tipY, centerX + half, y, 140);
}
+54
View File
@@ -0,0 +1,54 @@
import { MenuLayer } from "../menu";
import { ScreenTestLayer } from "../apps/screen-test";
import { BuzzerDemoLayer } from "../apps/buzzer-demo";
import { APP_VIEWPORT } from "./geometry";
import {
createInProcessWindow,
YieldAtRootLayer,
type InProcessAppOptions,
type InProcessWindow,
} from "./in-process-window";
export const DEBUG_TESTS_WINDOW_ID = "debug-tests";
export const DEBUG_TESTS_SURFACE_ID = "window:debug-tests";
/** Debug/diagnostic pages (screen test, buzzer demo) as an in-process app. */
export function createDebugTestsAppWindow(options: InProcessAppOptions): InProcessWindow {
const menu = new MenuLayer(
"Debug tests",
[
{
label: "Screen test",
onSelect: (ctx) => {
ctx.stack.push(new ScreenTestLayer());
},
},
{
label: "Buzzer demo",
onSelect: (ctx) => {
ctx.stack.push(new BuzzerDemoLayer());
},
},
],
{
x: 8,
y: 8,
width: 272,
minHeight: 0,
maxHeight: APP_VIEWPORT.height - 16,
},
);
return createInProcessWindow({
appId: "debug-tests",
windowId: DEBUG_TESTS_WINDOW_ID,
title: "Debug tests",
iconLetter: "Db",
closeable: true,
actions: options.actions,
baseLayer: new YieldAtRootLayer(menu),
submitFrame: options.submitFrame,
setSurfaceVisible: options.setSurfaceVisible,
removeSurface: options.removeSurface,
onClosed: options.onClosed,
});
}
+9
View File
@@ -33,6 +33,15 @@ export type InProcessWindow = {
requestRender: () => void;
};
/** The controller-provided plumbing common to every in-process app window. */
export type InProcessAppOptions = {
actions: LayerActions;
submitFrame: (image: GrayImage, paintMs: number, frameId: number) => Promise<void>;
setSurfaceVisible: (visible: boolean) => void;
removeSurface: () => void;
onClosed: () => void;
};
export function createInProcessWindow(options: InProcessWindowOptions): InProcessWindow {
const requestRender = () => {
void render(0).catch((error) => {
+103
View File
@@ -0,0 +1,103 @@
import { getDefaultSmallFont } from "../../graphics/bdffont";
import { GrayImage } from "../../graphics/image";
import { nightscoutBridge, type NightscoutState } from "../../native/nightscout-bridge";
import { getPluginState } from "../dashboard";
import { NightscoutLayer } from "../apps/nightscout";
import {
createInProcessWindow,
YieldAtRootLayer,
type InProcessAppOptions,
type InProcessWindow,
} from "./in-process-window";
import { shell } from "./shell";
export const NIGHTSCOUT_WINDOW_ID = "nightscout";
export const NIGHTSCOUT_SURFACE_ID = "window:nightscout";
const TRAY_ICON_ID = "nightscout";
const TRAY_HEIGHT = 24;
const TRAY_GRAPH_WIDTH = 48;
const TRAY_STALE_MS = 15 * 60 * 1000;
const TRAY_GRAPH_WINDOW_MS = 2 * 60 * 60 * 1000;
/**
* The Nightscout app: the full-screen glucose view (previously reached via
* the dashboard card) in its own in-process window. While the app is running
* it also publishes a top-bar tray icon: the latest blood-glucose value
* (struck through when stale) plus a minimal 48px history graph.
*/
export function createNightscoutAppWindow(options: InProcessAppOptions): InProcessWindow {
let unsubscribe: (() => void) | null = null;
const app = createInProcessWindow({
appId: "nightscout",
windowId: NIGHTSCOUT_WINDOW_ID,
title: "Nightscout",
iconLetter: "Ns",
closeable: true,
actions: options.actions,
baseLayer: new YieldAtRootLayer(new NightscoutLayer(getPluginState)),
submitFrame: options.submitFrame,
setSurfaceVisible: options.setSurfaceVisible,
removeSurface: options.removeSurface,
onClosed: () => {
unsubscribe?.();
unsubscribe = null;
shell.setTrayIcon(TRAY_ICON_ID, null);
options.onClosed();
},
});
// onStateChange fires immediately with the current snapshot, so this also
// publishes the initial tray icon.
unsubscribe = nightscoutBridge.onStateChange((state) => {
shell.setTrayIcon(TRAY_ICON_ID, buildNightscoutTrayIcon(state));
app.requestRender();
});
return app;
}
/** BG value (struck through when stale) + minimal 2-hour line graph. */
function buildNightscoutTrayIcon(state: NightscoutState): GrayImage {
const font = getDefaultSmallFont();
const nowMs = Date.now();
const label = state.latest ? `${state.latest.sgv}` : "--";
const labelWidth = font.measureText(label);
const image = new GrayImage(labelWidth + 4 + TRAY_GRAPH_WIDTH, TRAY_HEIGHT, 0);
const textY = Math.max(0, ((TRAY_HEIGHT - font.lineHeight) / 2) | 0);
image.drawText(font, 0, textY, label, 220);
const stale = state.latest !== null && nowMs - state.latest.timestampMs > TRAY_STALE_MS;
if (stale) {
image.drawLine(0, TRAY_HEIGHT / 2, labelWidth, TRAY_HEIGHT / 2, 180);
}
drawTrayGraph(image, labelWidth + 4, state, nowMs);
return image;
}
function drawTrayGraph(image: GrayImage, x: number, state: NightscoutState, nowMs: number): void {
const windowStartMs = nowMs - TRAY_GRAPH_WINDOW_MS;
const points = state.history.filter(
(point) => point.timestampMs >= windowStartMs && point.timestampMs <= nowMs,
);
if (points.length === 0) return;
const values = points.map((point) => point.sgv);
const min = Math.min(...values, 60);
const max = Math.max(...values, 200);
const span = Math.max(20, max - min);
const plotted = points.map((point) => ({
timestampMs: point.timestampMs,
x: x + Math.round(((point.timestampMs - windowStartMs) / TRAY_GRAPH_WINDOW_MS) * (TRAY_GRAPH_WIDTH - 1)),
y: TRAY_HEIGHT - 1 - Math.round(((point.sgv - min) / span) * (TRAY_HEIGHT - 1)),
}));
for (let index = 1; index < plotted.length; index++) {
const previous = plotted[index - 1]!;
const current = plotted[index]!;
if (current.timestampMs - previous.timestampMs <= TRAY_STALE_MS) {
image.drawLine(previous.x, previous.y, current.x, current.y, 190);
} else {
image.fillRect(current.x, current.y, 1, 1, 190);
}
}
}
+7 -12
View File
@@ -1,25 +1,20 @@
import { GrayImage } from "../../graphics/image";
import { LayerActions } from "../layers";
import { NotificationsListLayer } from "../notifications";
import { createInProcessWindow, YieldAtRootLayer, type InProcessWindow } from "./in-process-window";
import {
createInProcessWindow,
YieldAtRootLayer,
type InProcessAppOptions,
type InProcessWindow,
} from "./in-process-window";
export const NOTIFICATIONS_WINDOW_ID = "notifications";
export const NOTIFICATIONS_SURFACE_ID = "window:notifications";
export type NotificationsAppOptions = {
actions: LayerActions;
submitFrame: (image: GrayImage, paintMs: number, frameId: number) => Promise<void>;
setSurfaceVisible: (visible: boolean) => void;
removeSurface: () => void;
onClosed: () => void;
};
/**
* The Notifications app: the Android-notification list (previously a
* dashboard page) in its own in-process window; selecting a notification
* opens the detail view with its quick actions.
*/
export function createNotificationsAppWindow(options: NotificationsAppOptions): InProcessWindow {
export function createNotificationsAppWindow(options: InProcessAppOptions): InProcessWindow {
return createInProcessWindow({
appId: "notifications",
windowId: NOTIFICATIONS_WINDOW_ID,
+20
View File
@@ -96,6 +96,9 @@ class Shell {
private lastInputAtMs = Date.now();
private battery: ShellChromeState["battery"] = { headset: null, headsetCharging: null };
private attention = new Map<string, boolean>();
// App-provided top-bar tray icons, keyed by owner id; drawn between the
// notification icons and the battery indicators.
private readonly trayIcons = new Map<string, GrayImage>();
private activeVoiceLayer: VoiceInputLayer | null = null;
private readonly actions: LayerActions = { ...noopActions };
private config: ShellConfig = {
@@ -173,6 +176,20 @@ class Shell {
this.battery = { ...this.battery, ...levels };
}
/**
* Set or clear an app's top-bar tray icon (a small grayscale image, drawn
* between the notification icons and the battery indicators). Small and
* infrequently updated by design; not a framebuffer.
*/
setTrayIcon(ownerId: string, icon: GrayImage | null): void {
if (icon) {
this.trayIcons.set(ownerId, icon);
} else if (!this.trayIcons.delete(ownerId)) {
return;
}
this.config.requestShellRender();
}
isScreenOn(): boolean {
return this.screenOn;
}
@@ -400,6 +417,9 @@ class Shell {
selectedIndex: this.selectedIndex,
focus: this.focus,
battery: this.battery,
trayIcons: Array.from(this.trayIcons.keys())
.sort()
.map((key) => this.trayIcons.get(key)!),
};
}
}
+109
View File
@@ -0,0 +1,109 @@
import { externalStorageRootPath, readTextFile, type DirectoryEntry } from "../../native/file-access";
import { FileBrowserLayer } from "../apps/file-browser";
import { TelepromptLayer } from "../apps/teleprompt";
import { MenuLayer } from "../menu";
import { APP_VIEWPORT } from "./geometry";
import {
createInProcessWindow,
YieldAtRootLayer,
type InProcessAppOptions,
type InProcessWindow,
} from "./in-process-window";
import { shell } from "./shell";
export const TELEPROMPT_WINDOW_ID = "teleprompt";
export const TELEPROMPT_SURFACE_ID = "window:teleprompt";
const SUPPORTED_FILE = /\.(txt|md|log)$/i;
export type TelepromptBrowserOptions = InProcessAppOptions & {
/** Open a document as its own shell window (also used by the share intent). */
openDocumentWindow: (title: string, text: string) => void;
};
/**
* The Teleprompt app's launcher-opened window: a file browser over external
* storage. Picking a supported (text) file offers viewing it in this window
* or opening it as a new window.
*/
export function createTelepromptBrowserWindow(options: TelepromptBrowserOptions): InProcessWindow {
const browser = new FileBrowserLayer({
startPath: externalStorageRootPath(),
isSupportedFile: (name) => SUPPORTED_FILE.test(name),
// The browser handles double-click itself (up a directory), so it is not
// wrapped in YieldAtRootLayer; it yields explicitly from the top level.
onLeave: () => shell.yieldFocusToSidebar(),
onFilePicked: (entry, ctx) => {
ctx.stack.push(createFilePickedMenu(entry, options.openDocumentWindow));
},
});
return createInProcessWindow({
appId: "teleprompt",
windowId: TELEPROMPT_WINDOW_ID,
title: "Teleprompt",
iconLetter: "Tp",
closeable: true,
actions: options.actions,
baseLayer: browser,
submitFrame: options.submitFrame,
setSurfaceVisible: options.setSurfaceVisible,
removeSurface: options.removeSurface,
onClosed: options.onClosed,
});
}
/** One opened document as its own window (from the browser or a share intent). */
export function createTelepromptDocumentWindow(
windowId: string,
title: string,
text: string,
options: InProcessAppOptions,
): InProcessWindow {
return createInProcessWindow({
appId: "teleprompt",
windowId,
title,
iconLetter: "Tp",
closeable: true,
actions: options.actions,
baseLayer: new YieldAtRootLayer(new TelepromptLayer(text, title)),
submitFrame: options.submitFrame,
setSurfaceVisible: options.setSurfaceVisible,
removeSurface: options.removeSurface,
onClosed: options.onClosed,
});
}
function createFilePickedMenu(
entry: DirectoryEntry,
openDocumentWindow: (title: string, text: string) => void,
): MenuLayer {
return new MenuLayer(
entry.name,
[
{
label: "View here",
onSelect: (ctx) => {
const text = readTextFile(entry.path);
ctx.stack.pop();
ctx.stack.push(new TelepromptLayer(text ?? "(could not read file)", entry.name));
},
},
{
label: "Open in new window",
onSelect: (ctx) => {
const text = readTextFile(entry.path);
ctx.stack.pop();
openDocumentWindow(entry.name, text ?? "(could not read file)");
},
},
],
{
x: 8,
y: 8,
width: 272,
minHeight: 0,
maxHeight: APP_VIEWPORT.height - 16,
},
);
}
+20 -1
View File
@@ -1,3 +1,4 @@
import { GrayImage } from "../../graphics/image";
import { makeLetterWindowIcon } from "./chrome-layer";
import { shell, type ShellWindow } from "./shell";
@@ -26,7 +27,16 @@ export type WorkerAppReply =
focus?: boolean;
}
| { type: "set-title"; windowId: string; title: string }
| { type: "set-attention"; windowId: string; attention: boolean };
| { type: "set-attention"; windowId: string; attention: boolean }
| {
/**
* Set or clear the app's top-bar tray icon. Pixels ride the JSON
* postMessage roundtrip — acceptable because tray icons are small and
* infrequently updated.
*/
type: "set-tray-icon";
icon: { width: number; height: number; pixels: number[] } | null;
};
export type WorkerWindowSpec = {
/** Unique across the shell; namespace with the appId (e.g. "terminal:view:3"). */
@@ -80,6 +90,15 @@ export class WorkerAppHost {
case "set-attention":
shell.setWindowAttention(message.windowId, message.attention);
break;
case "set-tray-icon": {
let icon: GrayImage | null = null;
if (message.icon) {
icon = new GrayImage(message.icon.width, message.icon.height, 0);
icon.pixels.set(message.icon.pixels.slice(0, icon.pixels.length));
}
shell.setTrayIcon(this.options.appId, icon);
break;
}
case "set-title":
// Titles are informational for now (sidebar shows icons only).
break;