This commit is contained in:
James Babcock committed 2026-06-01 13:35:58 -07:00
1 parent 8d99ea97b5
commit d67afcbfdb
7 files changed
+126 -92

No files matched your search

+16 -8
View File
@@ -1,13 +1,18 @@
import takeRight from "lodash/takeRight"; import takeRight from "lodash/takeRight";
import { getDefaultSmallFont } from "~/graphics/bdffont"; import { getDefaultSmallFont } from "~/graphics/bdffont";
import { DashboardPlugin, DashboardPluginState } from "../dashboard-plugins"; import { DashboardPlugin, DashboardPluginCardRenderArgs, DashboardPluginState } from "../dashboard-plugins";
import { Layer, LayerContext, type DashboardInputEvent } from "../layers"; import { Layer, LayerContext, type DashboardInputEvent } from "../layers";
import { GrayImage } from "~/graphics/image"; import { GrayImage } from "~/graphics/image";
export const debugLogDashboardPlugin: DashboardPlugin = { export class DebugLogDashboardPlugin extends DashboardPlugin {
id: "input-debug-log", constructor() {
label: "Input debug log", super({
renderCard: ({ image, bounds, state }) => { id: "input-debug-log",
label: "Input debug log",
});
}
override renderCard({ image, bounds, state }: DashboardPluginCardRenderArgs): void {
const font = getDefaultSmallFont(); const font = getDefaultSmallFont();
image.drawText(font, bounds.x + 10, bounds.y + 14, "Input log", 180); image.drawText(font, bounds.x + 10, bounds.y + 14, "Input log", 180);
const lineHeight = 14; const lineHeight = 14;
@@ -16,9 +21,12 @@ export const debugLogDashboardPlugin: DashboardPlugin = {
for (let i = 0; i < visibleLines.length; i++) { for (let i = 0; i < visibleLines.length; i++) {
image.drawText(font, bounds.x + 8, bounds.y + 26 + i * lineHeight, visibleLines[i]!, 190); image.drawText(font, bounds.x + 8, bounds.y + 26 + i * lineHeight, visibleLines[i]!, 190);
} }
}, }
createFullscreenLayer: (getState) => new DebugLogLayer(getState),
}; override createFullscreenLayer(getState: () => DashboardPluginState): Layer {
return new DebugLogLayer(getState);
}
}
class DebugLogLayer implements Layer { class DebugLogLayer implements Layer {
private scrollOffset = 0; private scrollOffset = 0;
+32 -22
View File
@@ -1,14 +1,20 @@
import { DashboardPlugin, DashboardPluginState } from "../dashboard-plugins"; import { DashboardPlugin, DashboardPluginCardRenderArgs, DashboardPluginState } from "../dashboard-plugins";
import { getDefaultSmallFont } from "../../graphics/bdffont"; import { getDefaultSmallFont } from "../../graphics/bdffont";
import { GrayImage } from "../../graphics/image"; import { GrayImage } from "../../graphics/image";
import { wrapText } from "../../graphics/textwrap"; import { wrapText } from "../../graphics/textwrap";
import { mediaControllerBridge, type MediaControllerState } from "../../native/media-controller"; import { mediaControllerBridge, type MediaControllerState } from "../../native/media-controller";
import { Layer, type DashboardInputEvent, type LayerContext } from "../layers"; import { Layer, type DashboardInputEvent, type LayerContext } from "../layers";
export const musicControllerDashboardPlugin: DashboardPlugin = {
id: "music-controller", export class MusicControllerDashboardPlugin extends DashboardPlugin {
label: "Music controller", constructor() {
renderCard: ({ image, bounds, state }) => { super({
id: "music-controller",
label: "Music controller",
});
}
override renderCard({ image, bounds, state }: DashboardPluginCardRenderArgs): void {
const font = getDefaultSmallFont(); const font = getDefaultSmallFont();
const media = state.media; const media = state.media;
image.drawText(font, bounds.x + 10, bounds.y + 14, "Now playing", 180); image.drawText(font, bounds.x + 10, bounds.y + 14, "Now playing", 180);
@@ -30,24 +36,10 @@ export const musicControllerDashboardPlugin: DashboardPlugin = {
} }
image.drawText(font, bounds.x + 10, bounds.y + 62, artistLine, 160); image.drawText(font, bounds.x + 10, bounds.y + 62, artistLine, 160);
image.drawText(font, bounds.x + 10, bounds.y + bounds.height - 18, playbackLabel(media), 130); image.drawText(font, bounds.x + 10, bounds.y + bounds.height - 18, playbackLabel(media), 130);
}, }
createFullscreenLayer: (getState) => new MusicControllerLayer(getState),
};
function playbackLabel(media: MediaControllerState): string { override createFullscreenLayer(getState: () => DashboardPluginState): Layer {
switch (media.playbackState) { return new MusicControllerLayer(getState);
case "playing":
return "Playing";
case "paused":
return "Paused";
case "buffering":
return "Buffering";
case "stopped":
return "Stopped";
case "notification-access-required":
return "Access required";
default:
return media.status || "Idle";
} }
} }
@@ -121,3 +113,21 @@ class MusicControllerLayer implements Layer {
} }
} }
} }
function playbackLabel(media: MediaControllerState): string {
switch (media.playbackState) {
case "playing":
return "Playing";
case "paused":
return "Paused";
case "buffering":
return "Buffering";
case "stopped":
return "Stopped";
case "notification-access-required":
return "Access required";
default:
return media.status || "Idle";
}
}
+17 -27
View File
@@ -1,8 +1,9 @@
import { BdfFont, getDefaultLargeFont, getDefaultSmallFont } from "../../graphics/bdffont"; import { BdfFont, getDefaultLargeFont, getDefaultSmallFont } from "../../graphics/bdffont";
import { GrayImage, imageFromAsciiArt } from "../../graphics/image"; import { GrayImage, imageFromAsciiArt } from "../../graphics/image";
import { DashboardPlugin, DashboardPluginState } from "../dashboard-plugins"; import { DashboardPlugin, DashboardPluginCardRenderArgs, DashboardPluginState } from "../dashboard-plugins";
import { DashboardInputEvent, Layer, LayerContext } from "../layers"; import { DashboardInputEvent, Layer, LayerContext } from "../layers";
import { nightscoutBridge, type NightscoutState } from "../../native/nightscout-bridge"; import { nightscoutBridge, type NightscoutState } from "../../native/nightscout-bridge";
import { formatAgeShortFromTimestamp, formatTimestamp } from "~/util/date-util";
const nightscoutLargeFont = getDefaultLargeFont(); const nightscoutLargeFont = getDefaultLargeFont();
const NIGHTSCOUT_STALE_MS = 15 * 60 * 1000; const NIGHTSCOUT_STALE_MS = 15 * 60 * 1000;
@@ -10,10 +11,15 @@ const NIGHTSCOUT_GRAPH_WINDOW_MS = 2 * 60 * 60 * 1000;
const NIGHTSCOUT_GRAPH_TIME_QUANTUM_MS = 60 * 1000; const NIGHTSCOUT_GRAPH_TIME_QUANTUM_MS = 60 * 1000;
export const nightscoutDashboardPlugin: DashboardPlugin = { export class NightscoutDashboardPlugin extends DashboardPlugin {
id: "nightscout", constructor() {
label: "Nightscout", super({
renderCard: ({ image, bounds, state }) => { id: "nightscout",
label: "Nightscout",
});
}
override renderCard({ image, bounds, state }: DashboardPluginCardRenderArgs): void {
const font = getDefaultSmallFont(); const font = getDefaultSmallFont();
const nightscout = state.nightscout; const nightscout = state.nightscout;
const nowMs = Date.now(); const nowMs = Date.now();
@@ -64,8 +70,11 @@ export const nightscoutDashboardPlugin: DashboardPlugin = {
nowMs, nowMs,
font, font,
); );
}, }
createFullscreenLayer: (getState) => new NightscoutLayer(getState),
override createFullscreenLayer(getState: () => DashboardPluginState): Layer {
return new NightscoutLayer(getState);
}
} }
function drawNightscoutDelta( function drawNightscoutDelta(
@@ -157,21 +166,6 @@ function formatBolusLabel(value: number): string {
return Number.isInteger(value) ? `${value}` : value.toFixed(1); return Number.isInteger(value) ? `${value}` : value.toFixed(1);
} }
function formatAgeShortFromTimestamp(timestampMs: number | null, nowMs: number): string {
if (!timestampMs) {
return "--";
}
const elapsedMinutes = Math.max(0, Math.round((nowMs - timestampMs) / 60_000));
if (elapsedMinutes < 60) {
return `${elapsedMinutes}m`;
}
const elapsedHours = Math.round(elapsedMinutes / 60);
if (elapsedHours < 48) {
return `${elapsedHours}h`;
}
return `${Math.round(elapsedHours / 24)}d`;
}
function drawNightscoutGraph( function drawNightscoutGraph(
image: GrayImage, image: GrayImage,
bounds: { x: number; y: number; width: number; height: number }, bounds: { x: number; y: number; width: number; height: number },
@@ -371,6 +365,7 @@ function drawNightscoutReferenceLine(
image.drawLine(bounds.x + 1, y, bounds.x + bounds.width - 2, y, shade); image.drawLine(bounds.x + 1, y, bounds.x + bounds.width - 2, y, shade);
} }
class NightscoutLayer implements Layer { class NightscoutLayer implements Layer {
constructor(private readonly getState: () => DashboardPluginState) {} constructor(private readonly getState: () => DashboardPluginState) {}
@@ -443,11 +438,6 @@ class NightscoutLayer implements Layer {
} }
} }
function formatTimestamp(timestampMs: number): string {
const date = new Date(timestampMs);
return `${String(date.getHours()).padStart(2, "0")}:${String(date.getMinutes()).padStart(2, "0")}`;
}
function isNightscoutPointStale(point: NightscoutState["latest"], nowMs: number): boolean { function isNightscoutPointStale(point: NightscoutState["latest"], nowMs: number): boolean {
return point !== null && nowMs - point.timestampMs > NIGHTSCOUT_STALE_MS; return point !== null && nowMs - point.timestampMs > NIGHTSCOUT_STALE_MS;
} }
+21 -9
View File
@@ -2,10 +2,10 @@ import { GrayImage } from "../graphics/image";
import { type MediaControllerState } from "../native/media-controller"; import { type MediaControllerState } from "../native/media-controller";
import { type DashboardPluginId } from "./dashboard-settings"; import { type DashboardPluginId } from "./dashboard-settings";
import { Layer } from "./layers"; import { Layer } from "./layers";
import { nightscoutDashboardPlugin } from "./apps/nightscout"; import { NightscoutDashboardPlugin } from "./apps/nightscout";
import { NightscoutState } from "~/native/nightscout-bridge"; import { NightscoutState } from "~/native/nightscout-bridge";
import { musicControllerDashboardPlugin } from "./apps/music-controller"; import { MusicControllerDashboardPlugin } from "./apps/music-controller";
import { debugLogDashboardPlugin } from "./apps/debug-log"; import { DebugLogDashboardPlugin } from "./apps/debug-log";
export type DashboardPluginState = { export type DashboardPluginState = {
logLines: string[]; logLines: string[];
@@ -28,11 +28,23 @@ export type DashboardPluginCardRenderArgs = {
state: DashboardPluginState; state: DashboardPluginState;
}; };
export interface DashboardPlugin { export abstract class DashboardPlugin {
readonly id: DashboardPluginId; readonly id: DashboardPluginId;
readonly label: string; readonly label: string;
renderCard(args: DashboardPluginCardRenderArgs): void;
createFullscreenLayer?(getState: () => DashboardPluginState): Layer | null; constructor(config: {
id: DashboardPluginId;
label: string;
}) {
this.id = config.id;
this.label = config.label;
}
abstract renderCard(args: DashboardPluginCardRenderArgs): void;
createFullscreenLayer(getState: () => DashboardPluginState): Layer | null {
return null;
}
} }
const DASHBOARD_PLUGINS: DashboardPlugin[] = [ const DASHBOARD_PLUGINS: DashboardPlugin[] = [
@@ -42,9 +54,9 @@ const DASHBOARD_PLUGINS: DashboardPlugin[] = [
renderCard: () => {}, renderCard: () => {},
createFullscreenLayer: () => null, createFullscreenLayer: () => null,
}, },
debugLogDashboardPlugin, new DebugLogDashboardPlugin(),
musicControllerDashboardPlugin, new MusicControllerDashboardPlugin(),
nightscoutDashboardPlugin, new NightscoutDashboardPlugin(),
]; ];
const PLUGIN_MAP = new Map<DashboardPluginId, DashboardPlugin>( const PLUGIN_MAP = new Map<DashboardPluginId, DashboardPlugin>(
+6 -26
View File
@@ -1,3 +1,5 @@
import { clamp } from "~/util/numeric-util";
import { formatRelativeTime } from "~/util/date-util";
import { BdfFont, getDefaultSmallFont } from "../graphics/bdffont"; import { BdfFont, getDefaultSmallFont } from "../graphics/bdffont";
import { G2_LENS_HEIGHT, G2_LENS_WIDTH, GrayImage } from "../graphics/image"; import { G2_LENS_HEIGHT, G2_LENS_WIDTH, GrayImage } from "../graphics/image";
import { wrapText } from "../graphics/textwrap"; import { wrapText } from "../graphics/textwrap";
@@ -163,33 +165,30 @@ export class SingleNotificationLayer implements Layer {
} }
} }
function buildNotificationCardLayout(font: BdfFont, notification: AndroidNotification, selected: boolean): CardLayout { function buildNotificationCardLayout(font: BdfFont, notification: AndroidNotification, selected: boolean): CardLayout {
const lines: string[] = []; const lines: string[] = [];
const time = formatRelativeTime(notification.postTime); const time = formatRelativeTime(notification.postTime);
const title = notificationTitle(notification); const title = notificationTitle(notification);
const timeSuffix = time ? ` ${time}` : ""; const timeSuffix = time ? ` ${time}` : "";
lines.push(fitLine(font, `${title}${timeSuffix}`, CARD_TEXT_WIDTH)); lines.push(truncateToWidth(font, `${title}${timeSuffix}`, CARD_TEXT_WIDTH));
const appName = notification.appName || notification.packageName; const appName = notification.appName || notification.packageName;
const body = primaryNotificationBody(notification); const body = primaryNotificationBody(notification);
if (selected) { if (selected) {
if (appName && appName !== title) { if (appName && appName !== title) {
lines.push(fitLine(font, appName, CARD_TEXT_WIDTH)); lines.push(truncateToWidth(font, appName, CARD_TEXT_WIDTH));
} }
if (body) { if (body) {
lines.push(...wrapText(font, body, CARD_TEXT_WIDTH).slice(0, 4)); lines.push(...wrapText(font, body, CARD_TEXT_WIDTH).slice(0, 4));
} }
} else if (body) { } else if (body) {
console.log(`buildNotificationCardLayout: CARD_TEXT_WIDTH=${CARD_TEXT_WIDTH}`); lines.push(truncateToWidth(font, wrapText(font, body, CARD_TEXT_WIDTH)[0]!, CARD_TEXT_WIDTH));
console.log(`buildNotificationCardLayout: wrapText(...) = ${JSON.stringify(wrapText(font, body, CARD_TEXT_WIDTH))}`);
console.log(`buildNotificationCardLayout: fitLine(...) = ${fitLine(font, wrapText(font, body, CARD_TEXT_WIDTH)[0]!, CARD_TEXT_WIDTH)}`);
lines.push(fitLine(font, wrapText(font, body, CARD_TEXT_WIDTH)[0]!, CARD_TEXT_WIDTH));
} }
if (notification.actions.length) { if (notification.actions.length) {
lines.push(`${notification.actions.length} quick action${notification.actions.length === 1 ? "" : "s"}`); lines.push(`${notification.actions.length} quick action${notification.actions.length === 1 ? "" : "s"}`);
} }
console.log(`buildNotificationCardLayout: title=${title}, appName=${appName}, body=${body}, lines=${JSON.stringify(lines)}`);
return { return {
notification, notification,
lines, lines,
@@ -296,17 +295,6 @@ function detailNotificationBody(notification: AndroidNotification): string {
return [notification.bigText || notification.text, lines].filter(Boolean).join("\n"); return [notification.bigText || notification.text, lines].filter(Boolean).join("\n");
} }
function formatRelativeTime(timestamp: number): string {
if (!timestamp) return "";
const ageMs = Math.max(0, Date.now() - timestamp);
const minutes = Math.floor(ageMs / 60_000);
if (minutes < 1) return "now";
if (minutes < 60) return `${minutes}m`;
const hours = Math.floor(minutes / 60);
if (hours < 24) return `${hours}h`;
return `${Math.floor(hours / 24)}d`;
}
function truncateToWidth(font: BdfFont, text: string, width: number): string { function truncateToWidth(font: BdfFont, text: string, width: number): string {
if (font.measureText(text) <= width) return text; if (font.measureText(text) <= width) return text;
let out = text; let out = text;
@@ -319,11 +307,3 @@ function truncateToWidth(font: BdfFont, text: string, width: number): string {
function notificationTitle(notification: AndroidNotification): string { function notificationTitle(notification: AndroidNotification): string {
return notification.title || notification.text || notification.summaryText || notification.appName || notification.packageName || "(untitled)"; return notification.title || notification.text || notification.summaryText || notification.appName || notification.packageName || "(untitled)";
} }
function fitLine(font: BdfFont, text: string, width: number): string {
return truncateToWidth(font, text, width);
}
function clamp(value: number, min: number, max: number): number {
return Math.max(min, Math.min(max, value));
}
+31
View File
@@ -0,0 +1,31 @@
export function formatTimestamp(timestampMs: number): string {
const date = new Date(timestampMs);
return `${String(date.getHours()).padStart(2, "0")}:${String(date.getMinutes()).padStart(2, "0")}`;
}
export function formatAgeShortFromTimestamp(timestampMs: number | null, nowMs: number): string {
if (!timestampMs) {
return "--";
}
const elapsedMinutes = Math.max(0, Math.round((nowMs - timestampMs) / 60_000));
if (elapsedMinutes < 60) {
return `${elapsedMinutes}m`;
}
const elapsedHours = Math.round(elapsedMinutes / 60);
if (elapsedHours < 48) {
return `${elapsedHours}h`;
}
return `${Math.round(elapsedHours / 24)}d`;
}
export function formatRelativeTime(timestamp: number): string {
if (!timestamp) return "";
const ageMs = Math.max(0, Date.now() - timestamp);
const minutes = Math.floor(ageMs / 60_000);
if (minutes < 1) return "now";
if (minutes < 60) return `${minutes}m`;
const hours = Math.floor(minutes / 60);
if (hours < 24) return `${hours}h`;
return `${Math.floor(hours / 24)}d`;
}
+3
View File
@@ -0,0 +1,3 @@
export function clamp(value: number, min: number, max: number): number {
return Math.max(min, Math.min(max, value));
}