mirror of
https://git.undefined-dev.fr/Blendman974/faceclaw.git
synced 2026-11-19 21:03:09 +00:00
Refactor
This commit is contained in:
1 parent
8d99ea97b5
commit
d67afcbfdb
7 files changed
+128
-94
No files matched your search
@@ -1,13 +1,18 @@
|
||||
import takeRight from "lodash/takeRight";
|
||||
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 { GrayImage } from "~/graphics/image";
|
||||
|
||||
export const debugLogDashboardPlugin: DashboardPlugin = {
|
||||
id: "input-debug-log",
|
||||
label: "Input debug log",
|
||||
renderCard: ({ image, bounds, state }) => {
|
||||
export class DebugLogDashboardPlugin extends DashboardPlugin {
|
||||
constructor() {
|
||||
super({
|
||||
id: "input-debug-log",
|
||||
label: "Input debug log",
|
||||
});
|
||||
}
|
||||
|
||||
override renderCard({ image, bounds, state }: DashboardPluginCardRenderArgs): void {
|
||||
const font = getDefaultSmallFont();
|
||||
image.drawText(font, bounds.x + 10, bounds.y + 14, "Input log", 180);
|
||||
const lineHeight = 14;
|
||||
@@ -16,9 +21,12 @@ export const debugLogDashboardPlugin: DashboardPlugin = {
|
||||
for (let i = 0; i < visibleLines.length; i++) {
|
||||
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 {
|
||||
private scrollOffset = 0;
|
||||
|
||||
@@ -1,14 +1,20 @@
|
||||
import { DashboardPlugin, DashboardPluginState } from "../dashboard-plugins";
|
||||
import { DashboardPlugin, DashboardPluginCardRenderArgs, DashboardPluginState } from "../dashboard-plugins";
|
||||
import { getDefaultSmallFont } from "../../graphics/bdffont";
|
||||
import { GrayImage } from "../../graphics/image";
|
||||
import { wrapText } from "../../graphics/textwrap";
|
||||
import { mediaControllerBridge, type MediaControllerState } from "../../native/media-controller";
|
||||
import { Layer, type DashboardInputEvent, type LayerContext } from "../layers";
|
||||
|
||||
export const musicControllerDashboardPlugin: DashboardPlugin = {
|
||||
id: "music-controller",
|
||||
label: "Music controller",
|
||||
renderCard: ({ image, bounds, state }) => {
|
||||
|
||||
export class MusicControllerDashboardPlugin extends DashboardPlugin {
|
||||
constructor() {
|
||||
super({
|
||||
id: "music-controller",
|
||||
label: "Music controller",
|
||||
});
|
||||
}
|
||||
|
||||
override renderCard({ image, bounds, state }: DashboardPluginCardRenderArgs): void {
|
||||
const font = getDefaultSmallFont();
|
||||
const media = state.media;
|
||||
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 + bounds.height - 18, playbackLabel(media), 130);
|
||||
},
|
||||
createFullscreenLayer: (getState) => new MusicControllerLayer(getState),
|
||||
};
|
||||
}
|
||||
|
||||
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";
|
||||
override createFullscreenLayer(getState: () => DashboardPluginState): Layer {
|
||||
return new MusicControllerLayer(getState);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -120,4 +112,22 @@ class MusicControllerLayer implements Layer {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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";
|
||||
}
|
||||
}
|
||||
|
||||
+18
-28
@@ -1,8 +1,9 @@
|
||||
import { BdfFont, getDefaultLargeFont, getDefaultSmallFont } from "../../graphics/bdffont";
|
||||
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 { nightscoutBridge, type NightscoutState } from "../../native/nightscout-bridge";
|
||||
import { formatAgeShortFromTimestamp, formatTimestamp } from "~/util/date-util";
|
||||
|
||||
const nightscoutLargeFont = getDefaultLargeFont();
|
||||
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;
|
||||
|
||||
|
||||
export const nightscoutDashboardPlugin: DashboardPlugin = {
|
||||
id: "nightscout",
|
||||
label: "Nightscout",
|
||||
renderCard: ({ image, bounds, state }) => {
|
||||
export class NightscoutDashboardPlugin extends DashboardPlugin {
|
||||
constructor() {
|
||||
super({
|
||||
id: "nightscout",
|
||||
label: "Nightscout",
|
||||
});
|
||||
}
|
||||
|
||||
override renderCard({ image, bounds, state }: DashboardPluginCardRenderArgs): void {
|
||||
const font = getDefaultSmallFont();
|
||||
const nightscout = state.nightscout;
|
||||
const nowMs = Date.now();
|
||||
@@ -64,8 +70,11 @@ export const nightscoutDashboardPlugin: DashboardPlugin = {
|
||||
nowMs,
|
||||
font,
|
||||
);
|
||||
},
|
||||
createFullscreenLayer: (getState) => new NightscoutLayer(getState),
|
||||
}
|
||||
|
||||
override createFullscreenLayer(getState: () => DashboardPluginState): Layer {
|
||||
return new NightscoutLayer(getState);
|
||||
}
|
||||
}
|
||||
|
||||
function drawNightscoutDelta(
|
||||
@@ -157,21 +166,6 @@ function formatBolusLabel(value: number): string {
|
||||
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(
|
||||
image: GrayImage,
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
class NightscoutLayer implements Layer {
|
||||
constructor(private readonly getState: () => DashboardPluginState) {}
|
||||
|
||||
@@ -443,15 +438,10 @@ 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 {
|
||||
return point !== null && nowMs - point.timestampMs > NIGHTSCOUT_STALE_MS;
|
||||
}
|
||||
|
||||
function drawNightscoutValueStrikeThrough(image: GrayImage, x: number, y: number, width: number): void {
|
||||
image.drawLine(x, y, x + width, y, 180);
|
||||
}
|
||||
}
|
||||
@@ -2,10 +2,10 @@ import { GrayImage } from "../graphics/image";
|
||||
import { type MediaControllerState } from "../native/media-controller";
|
||||
import { type DashboardPluginId } from "./dashboard-settings";
|
||||
import { Layer } from "./layers";
|
||||
import { nightscoutDashboardPlugin } from "./apps/nightscout";
|
||||
import { NightscoutDashboardPlugin } from "./apps/nightscout";
|
||||
import { NightscoutState } from "~/native/nightscout-bridge";
|
||||
import { musicControllerDashboardPlugin } from "./apps/music-controller";
|
||||
import { debugLogDashboardPlugin } from "./apps/debug-log";
|
||||
import { MusicControllerDashboardPlugin } from "./apps/music-controller";
|
||||
import { DebugLogDashboardPlugin } from "./apps/debug-log";
|
||||
|
||||
export type DashboardPluginState = {
|
||||
logLines: string[];
|
||||
@@ -28,11 +28,23 @@ export type DashboardPluginCardRenderArgs = {
|
||||
state: DashboardPluginState;
|
||||
};
|
||||
|
||||
export interface DashboardPlugin {
|
||||
export abstract class DashboardPlugin {
|
||||
readonly id: DashboardPluginId;
|
||||
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[] = [
|
||||
@@ -42,9 +54,9 @@ const DASHBOARD_PLUGINS: DashboardPlugin[] = [
|
||||
renderCard: () => {},
|
||||
createFullscreenLayer: () => null,
|
||||
},
|
||||
debugLogDashboardPlugin,
|
||||
musicControllerDashboardPlugin,
|
||||
nightscoutDashboardPlugin,
|
||||
new DebugLogDashboardPlugin(),
|
||||
new MusicControllerDashboardPlugin(),
|
||||
new NightscoutDashboardPlugin(),
|
||||
];
|
||||
|
||||
const PLUGIN_MAP = new Map<DashboardPluginId, DashboardPlugin>(
|
||||
|
||||
+6
-26
@@ -1,3 +1,5 @@
|
||||
import { clamp } from "~/util/numeric-util";
|
||||
import { formatRelativeTime } from "~/util/date-util";
|
||||
import { BdfFont, getDefaultSmallFont } from "../graphics/bdffont";
|
||||
import { G2_LENS_HEIGHT, G2_LENS_WIDTH, GrayImage } from "../graphics/image";
|
||||
import { wrapText } from "../graphics/textwrap";
|
||||
@@ -163,33 +165,30 @@ export class SingleNotificationLayer implements Layer {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function buildNotificationCardLayout(font: BdfFont, notification: AndroidNotification, selected: boolean): CardLayout {
|
||||
const lines: string[] = [];
|
||||
const time = formatRelativeTime(notification.postTime);
|
||||
const title = notificationTitle(notification);
|
||||
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 body = primaryNotificationBody(notification);
|
||||
|
||||
if (selected) {
|
||||
if (appName && appName !== title) {
|
||||
lines.push(fitLine(font, appName, CARD_TEXT_WIDTH));
|
||||
lines.push(truncateToWidth(font, appName, CARD_TEXT_WIDTH));
|
||||
}
|
||||
if (body) {
|
||||
lines.push(...wrapText(font, body, CARD_TEXT_WIDTH).slice(0, 4));
|
||||
}
|
||||
} else if (body) {
|
||||
console.log(`buildNotificationCardLayout: CARD_TEXT_WIDTH=${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));
|
||||
lines.push(truncateToWidth(font, wrapText(font, body, CARD_TEXT_WIDTH)[0]!, CARD_TEXT_WIDTH));
|
||||
}
|
||||
if (notification.actions.length) {
|
||||
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 {
|
||||
notification,
|
||||
lines,
|
||||
@@ -296,17 +295,6 @@ function detailNotificationBody(notification: AndroidNotification): string {
|
||||
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 {
|
||||
if (font.measureText(text) <= width) return text;
|
||||
let out = text;
|
||||
@@ -319,11 +307,3 @@ function truncateToWidth(font: BdfFont, text: string, width: number): string {
|
||||
function notificationTitle(notification: AndroidNotification): string {
|
||||
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));
|
||||
}
|
||||
@@ -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`;
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
export function clamp(value: number, min: number, max: number): number {
|
||||
return Math.max(min, Math.min(max, value));
|
||||
}
|
||||
Reference in new issue
Block a user