Refactor dashboard cards

This commit is contained in:
James Babcock committed 2026-06-01 12:45:27 -07:00
1 parent 7afb98f7f7
commit 8d99ea97b5
5 files changed
+179 -153

No files matched your search

+15
View File
@@ -1,4 +1,5 @@
import { BdfFont, Glyph } from "./bdffont";
import { wrapText } from "./textwrap";
export const G2_LENS_WIDTH = 576;
export const G2_LENS_HEIGHT = 288;
@@ -130,6 +131,20 @@ export class GrayImage {
}
}
drawTextWrapped({font, x, y, width, text, value }: {
font: BdfFont;
x: number;
y: number;
width: number;
text: string;
value: number;
}): void {
const lines = wrapText(font, text, this.width);
for (let i = 0; i < lines.length; i++) {
this.drawText(font, x, y + i * font.lineHeight, lines[i]!, value);
}
}
bitBlt(
source: GrayImage,
dx: number,
+16
View File
@@ -0,0 +1,16 @@
import { GrayImage } from "./image";
import { loadPngAsGrayImage } from "./imagefile";
let cachedDashboardLogo: GrayImage | null | undefined;
export function getDashboardLogo(): GrayImage | null {
if (cachedDashboardLogo !== undefined) {
return cachedDashboardLogo;
}
try {
cachedDashboardLogo = loadPngAsGrayImage("images/faceclaw-logo-dashboard.png");
} catch {
cachedDashboardLogo = null;
}
return cachedDashboardLogo;
}
+12 -146
View File
@@ -1,14 +1,9 @@
import { G2_LENS_HEIGHT, G2_LENS_WIDTH, GrayImage } from "../graphics/image";
import { getDefaultMediumFont, getDefaultSmallFont } from "../graphics/bdffont";
import { BATTERY_ICON_WIDTH, drawBattery } from "../graphics/battery";
import { loadPngAsGrayImage } from "../graphics/imagefile";
import { type RawInputEvent } from "../native/faceclaw-communicator";
import { mediaControllerBridge } from "../native/media-controller";
import { nightscoutBridge } from "../native/nightscout-bridge";
import { readActiveNotificationIcons } from "../native/notification-icons";
import { readPhoneBatteryState } from "../native/phone-battery";
import { readSystemStatusIcons } from "../native/system-status-icons";
import { voiceControlBridge, type VoiceTranscriptEvent } from "../native/voice-control";
import { EventSourceType, OsEventTypeList } from "../g2/events";
import {
getDashboardPlugin,
@@ -18,7 +13,6 @@ import {
type DashboardPluginState,
} from "./dashboard-plugins";
import {
DEFAULT_SYSTEM_CARD_NAME,
getDashboardSlotIds,
isNightscoutSettingsConfigured,
loadDashboardSlotConfig,
@@ -56,6 +50,8 @@ import { NotificationsListLayer, SingleNotificationLayer } from "./notifications
import { TranscribeLayer } from "./apps/transcribe";
import { TelepromptLayer } from "./apps/teleprompt";
import { ScreenTestLayer } from "./apps/screen-test";
import { getDashboardLogo } from "~/graphics/logo";
import { drawSystemCard, getDisplayedSystemCardName } from "./dashboard/system-card";
type DashboardCardId = "system" | DashboardSlotId;
export type DashboardBatteryLevels = {
@@ -64,7 +60,7 @@ export type DashboardBatteryLevels = {
ring: number | null;
};
let dashboardState = {
export let dashboardState = {
logLines: [] as string[],
screenOn: true,
lastInputAtMs: Date.now(),
@@ -85,7 +81,6 @@ let dashboardState = {
} as DashboardBatteryLevels,
};
let cachedDashboardLogo: GrayImage | null | undefined;
const dashboardActions: LayerActions = {
disconnect: () => {},
startSystemNameEdit: () => {},
@@ -100,11 +95,6 @@ const dashboardActions: LayerActions = {
stopDedicatedVoiceInput: () => {},
};
const dashboardFont = getDefaultSmallFont();
const dashboardSystemFont = getDefaultMediumFont();
const NOTIFICATION_ICON_SIZE = 24;
const SYSTEM_CARD_ITEM_HEIGHT = 38;
const SYSTEM_CARD_ITEM_GAP = 2;
const BATTERY_ITEM_Y_OFFSET = 4;
const TOP_LEFT_MENU_LAYOUT = { x: 8, y: 8, width: 272, height: 128 };
function rawInputEventToInputEvent(event: RawInputEvent): DashboardInputEvent {
@@ -305,119 +295,6 @@ export function setDashboardBatteryLevels(levels: Partial<DashboardBatteryLevels
};
}
function getDisplayedSystemCardName(): string {
return dashboardState.systemCardName || DEFAULT_SYSTEM_CARD_NAME;
}
type SystemCardFlowItem =
| { type: "battery"; label: string; percentCharge: number; isCharging: boolean }
| { type: "notification"; icon: GrayImage };
function collectBatteryItems(): SystemCardFlowItem[] {
if (!dashboardState.systemCardSettings.showBatteryIndicators) return [];
const phone = readPhoneBatteryState();
const items: SystemCardFlowItem[] = [];
addBatteryItem(items, "Phone", phone.battery, phone.charging);
addBatteryItem(items, "G2", dashboardState.battery.headset, dashboardState.battery.headsetCharging);
addBatteryItem(items, "R1", dashboardState.battery.ring, null);
return items;
}
function addBatteryItem(
items: SystemCardFlowItem[],
label: string,
percentCharge: number | null,
isCharging: boolean | null,
): void {
if (percentCharge === null || !Number.isFinite(percentCharge)) return;
items.push({
type: "battery",
label,
percentCharge,
isCharging: Boolean(isCharging),
});
}
function drawBatteryFlowItem(image: GrayImage, x: number, y: number, item: Extract<SystemCardFlowItem, { type: "battery" }>): void {
const itemWidth = systemCardFlowItemWidth(item);
const labelX = x + Math.max(0, ((itemWidth - dashboardFont.measureText(item.label)) / 2) | 0);
image.drawText(dashboardFont, labelX, y + BATTERY_ITEM_Y_OFFSET, item.label, 150);
const battery = drawBattery(item.percentCharge, item.isCharging);
image.bitBlt(battery, x + ((itemWidth - battery.width) / 2) | 0, y + 16 + BATTERY_ITEM_Y_OFFSET);
}
const MONTHS = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
const WEEKDAYS = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
function formatDashboardDate(now: Date): string {
return `${WEEKDAYS[now.getDay()]} ${MONTHS[now.getMonth()]} ${now.getDate()} ${pad2(now.getHours())}:${pad2(now.getMinutes())}`;
}
function pad2(value: number): string {
return String(value).padStart(2, "0");
}
function getDashboardLogo(): GrayImage | null {
if (cachedDashboardLogo !== undefined) {
return cachedDashboardLogo;
}
try {
cachedDashboardLogo = loadPngAsGrayImage("images/faceclaw-logo-dashboard.png");
} catch {
cachedDashboardLogo = null;
}
return cachedDashboardLogo;
}
function drawSystemCardFlowItems(image: GrayImage, bounds: DashboardPluginCardBounds): void {
const left = bounds.x + 10;
const top = bounds.y + 84;
const right = bounds.x + bounds.width - 10;
const bottom = bounds.y + bounds.height - 6;
const items: SystemCardFlowItem[] = [];
if (dashboardState.systemCardSettings.showAndroidNotifications) {
const maxNotificationIcons = Math.max(0, ((right - left) / Math.max(1, NOTIFICATION_ICON_SIZE + SYSTEM_CARD_ITEM_GAP)) | 0) * 2;
for (const icon of readActiveNotificationIcons(maxNotificationIcons)) {
items.push({ type: "notification", icon });
}
}
items.push(...collectBatteryItems());
if (dashboardState.systemCardSettings.showSignalStrength) {
for (const icon of readSystemStatusIcons()) {
items.push({ type: "notification", icon });
}
}
let itemX = left;
let itemY = top;
for (const item of items) {
const itemWidth = systemCardFlowItemWidth(item);
if (itemX > left && itemX + itemWidth > right) {
itemX = left;
itemY += SYSTEM_CARD_ITEM_HEIGHT + SYSTEM_CARD_ITEM_GAP;
}
if (itemY + SYSTEM_CARD_ITEM_HEIGHT > bottom) {
break;
}
if (item.type === "battery") {
drawBatteryFlowItem(image, itemX, itemY, item);
} else {
image.bitBlt(
item.icon,
itemX,
itemY + ((SYSTEM_CARD_ITEM_HEIGHT - NOTIFICATION_ICON_SIZE) / 2) | 0,
);
}
itemX += itemWidth + SYSTEM_CARD_ITEM_GAP;
}
}
function systemCardFlowItemWidth(item: SystemCardFlowItem): number {
if (item.type === "notification") {
return NOTIFICATION_ICON_SIZE;
}
return Math.max(BATTERY_ICON_WIDTH, dashboardFont.measureText(item.label));
}
class DashboardLayer implements Layer {
paint(): GrayImage {
@@ -426,18 +303,8 @@ class DashboardLayer implements Layer {
}
const image = new GrayImage(G2_LENS_WIDTH, G2_LENS_HEIGHT, 0);
const now = new Date();
const logo = getDashboardLogo();
const pluginState = getPluginState();
const systemBounds = getCardBounds("system");
if (logo && dashboardState.systemCardSettings.showFaceclawLogo) {
image.bitBlt(logo, systemBounds.x + 10, systemBounds.y + 10);
}
const infoX = logo && dashboardState.systemCardSettings.showFaceclawLogo ? systemBounds.x + 92 : systemBounds.x + 10;
image.drawText(dashboardSystemFont, infoX, systemBounds.y + 10, getDisplayedSystemCardName(), 200);
image.drawText(dashboardSystemFont, infoX, systemBounds.y + 32, formatDashboardDate(now), 200);
drawSystemCardFlowItems(image, systemBounds);
drawSystemCard(image, getCardBounds("system"));
for (const slot of getDashboardSlotIds()) {
const bounds = getCardBounds(slot);
@@ -818,7 +685,7 @@ class EditSystemNameLayer implements Layer {
image.drawText(dashboardFont, 22, 16, "Edit dashboard name", 220);
image.drawText(dashboardFont, 22, 52, "Look at the phone app", 200);
image.drawText(dashboardFont, 22, 70, "to type a new name.", 200);
image.drawText(dashboardSystemFont, 22, 110, getDisplayedSystemCardName(), 220);
image.drawText(getDefaultMediumFont(), 22, 110, getDisplayedSystemCardName(), 220);
image.drawText(dashboardFont, 22, 252, "Double-click to go back", 110);
return image;
}
@@ -887,14 +754,13 @@ class AboutLayer implements Layer {
image.drawText(dashboardFont, 108, 48, "Faceclaw", 220);
image.drawText(dashboardFont, 108, 64, "Dashboard prototype", 180);
const aboutLines = [
"By James Babcock. Distributed under the GNU General Public License, version 3.",
"Version 0.1.0. Too much of an early janky development prototype to have",
"proper numbered releases."
];
for (let index = 0; index < aboutLines.length; index++) {
image.drawText(dashboardFont, 22, 128 + index * 14, aboutLines[index]!, 180);
}
image.drawTextWrapped({
font: dashboardFont,
x: 22, y: 128,
width: G2_LENS_WIDTH - 44,
text: "By James Babcock. Distributed under the GNU General Public License, version 3. Version 0.1.0. Too much of an early janky development prototype to have proper numbered releases.",
value: 180
});
return image;
}
+134
View File
@@ -0,0 +1,134 @@
import { GrayImage } from "../../graphics/image";
import { BATTERY_ICON_WIDTH, drawBattery } from "../../graphics/battery";
import { readActiveNotificationIcons } from "../../native/notification-icons";
import { readPhoneBatteryState } from "../../native/phone-battery";
import { readSystemStatusIcons } from "../../native/system-status-icons";
import { type DashboardPluginCardBounds } from "../dashboard-plugins";
import { dashboardState } from "../dashboard";
import { getDefaultMediumFont, getDefaultSmallFont } from "~/graphics/bdffont";
import { getDashboardLogo } from "~/graphics/logo";
import { DEFAULT_SYSTEM_CARD_NAME } from "../dashboard-settings";
const NOTIFICATION_ICON_SIZE = 24;
const SYSTEM_CARD_ITEM_HEIGHT = 38;
const SYSTEM_CARD_ITEM_GAP = 2;
const BATTERY_ITEM_Y_OFFSET = 4;
const MONTHS = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
const WEEKDAYS = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
type SystemCardFlowItem =
| { type: "battery"; label: string; percentCharge: number; isCharging: boolean }
| { type: "notification"; icon: GrayImage };
const smallFont = getDefaultSmallFont();
const mediumFont = getDefaultMediumFont();
function drawSystemCardFlowItems(image: GrayImage, bounds: DashboardPluginCardBounds): void {
const left = bounds.x + 10;
const top = bounds.y + 84;
const right = bounds.x + bounds.width - 10;
const bottom = bounds.y + bounds.height - 6;
const items: SystemCardFlowItem[] = [];
if (dashboardState.systemCardSettings.showAndroidNotifications) {
const maxNotificationIcons = Math.max(0, ((right - left) / Math.max(1, NOTIFICATION_ICON_SIZE + SYSTEM_CARD_ITEM_GAP)) | 0) * 2;
for (const icon of readActiveNotificationIcons(maxNotificationIcons)) {
items.push({ type: "notification", icon });
}
}
items.push(...collectBatteryItems());
if (dashboardState.systemCardSettings.showSignalStrength) {
for (const icon of readSystemStatusIcons()) {
items.push({ type: "notification", icon });
}
}
let itemX = left;
let itemY = top;
for (const item of items) {
const itemWidth = systemCardFlowItemWidth(item);
if (itemX > left && itemX + itemWidth > right) {
itemX = left;
itemY += SYSTEM_CARD_ITEM_HEIGHT + SYSTEM_CARD_ITEM_GAP;
}
if (itemY + SYSTEM_CARD_ITEM_HEIGHT > bottom) {
break;
}
if (item.type === "battery") {
drawBatteryFlowItem(image, itemX, itemY, item);
} else {
image.bitBlt(
item.icon,
itemX,
itemY + ((SYSTEM_CARD_ITEM_HEIGHT - NOTIFICATION_ICON_SIZE) / 2) | 0,
);
}
itemX += itemWidth + SYSTEM_CARD_ITEM_GAP;
}
}
function systemCardFlowItemWidth(item: SystemCardFlowItem): number {
if (item.type === "notification") {
return NOTIFICATION_ICON_SIZE;
}
return Math.max(BATTERY_ICON_WIDTH, smallFont.measureText(item.label));
}
function collectBatteryItems(): SystemCardFlowItem[] {
if (!dashboardState.systemCardSettings.showBatteryIndicators) return [];
const phone = readPhoneBatteryState();
const items: SystemCardFlowItem[] = [];
addBatteryItem(items, "Phone", phone.battery, phone.charging);
addBatteryItem(items, "G2", dashboardState.battery.headset, dashboardState.battery.headsetCharging);
addBatteryItem(items, "R1", dashboardState.battery.ring, null);
return items;
}
function addBatteryItem(
items: SystemCardFlowItem[],
label: string,
percentCharge: number | null,
isCharging: boolean | null,
): void {
if (percentCharge === null || !Number.isFinite(percentCharge)) return;
items.push({
type: "battery",
label,
percentCharge,
isCharging: Boolean(isCharging),
});
}
function drawBatteryFlowItem(image: GrayImage, x: number, y: number, item: Extract<SystemCardFlowItem, { type: "battery" }>): void {
const itemWidth = systemCardFlowItemWidth(item);
const labelX = x + Math.max(0, ((itemWidth - smallFont.measureText(item.label)) / 2) | 0);
image.drawText(smallFont, labelX, y + BATTERY_ITEM_Y_OFFSET, item.label, 150);
const battery = drawBattery(item.percentCharge, item.isCharging);
image.bitBlt(battery, x + ((itemWidth - battery.width) / 2) | 0, y + 16 + BATTERY_ITEM_Y_OFFSET);
}
function formatDashboardDate(now: Date): string {
return `${WEEKDAYS[now.getDay()]} ${MONTHS[now.getMonth()]} ${now.getDate()} ${pad2(now.getHours())}:${pad2(now.getMinutes())}`;
}
function pad2(value: number): string {
return String(value).padStart(2, "0");
}
export function getDisplayedSystemCardName(): string {
return dashboardState.systemCardName || DEFAULT_SYSTEM_CARD_NAME;
}
export function drawSystemCard(image: GrayImage, bounds: DashboardPluginCardBounds): void {
const now = new Date();
const logo = getDashboardLogo();
if (logo && dashboardState.systemCardSettings.showFaceclawLogo) {
image.bitBlt(logo, bounds.x + 10, bounds.y + 10);
}
const infoX = logo && dashboardState.systemCardSettings.showFaceclawLogo ? bounds.x + 92 : bounds.x + 10;
image.drawText(mediumFont, infoX, bounds.y + 10, getDisplayedSystemCardName(), 200);
image.drawText(mediumFont, infoX, bounds.y + 32, formatDashboardDate(now), 200);
drawSystemCardFlowItems(image, bounds);
}
+2 -7
View File
@@ -12,10 +12,8 @@ import { type DashboardInputEvent, type Layer, type LayerContext, type PaintBelo
const PAGE_X = 12;
const PAGE_Y = 12;
const PAGE_WIDTH = G2_LENS_WIDTH - 24;
const PAGE_HEIGHT = G2_LENS_HEIGHT - 24;
const LIST_TOP = 38;
const LIST_BOTTOM = 248;
const LIST_BOTTOM = G2_LENS_HEIGHT;
const CARD_X = 20;
const CARD_WIDTH = G2_LENS_WIDTH - 40;
const CARD_TEXT_WIDTH = CARD_WIDTH - 24;
@@ -48,9 +46,8 @@ export class NotificationsListLayer implements Layer {
buildNotificationCardLayout(font, notification, index === selectedIndex),
);
image.drawRect(PAGE_X, PAGE_Y, PAGE_WIDTH, PAGE_HEIGHT, 52);
image.drawText(font, PAGE_X + 12, PAGE_Y + 9, "Notifications", 220);
image.drawText(font, G2_LENS_WIDTH - 96, PAGE_Y + 9, `${notifications.length} active`, 150);
image.drawText(font, G2_LENS_WIDTH - 96, PAGE_Y + 9, `${selectedIndex+1}/${notifications.length}`, 150);
if (!notifications.length) {
image.drawText(font, 24, 72, "No current Android notifications.", 190);
@@ -69,8 +66,6 @@ export class NotificationsListLayer implements Layer {
if (cursorY > LIST_BOTTOM + 80) break;
}
const footer = `${selectedIndex + 1}/${notifications.length} tap: open double: dashboard`;
image.drawText(font, PAGE_X + 12, 258, footer, 110);
return image;
}