forked from Blendman974/faceclaw
UI changes, system status icons
This commit is contained in:
1 parent
6e442e7faa
commit
b5273715ae
14 files changed
+830
-236
No files matched your search
@@ -1,15 +1,16 @@
|
||||
import { G2_LENS_HEIGHT, G2_LENS_WIDTH, GrayImage } from "../graphics/image";
|
||||
import { EventSourceType, EventSourceTypeName, OsEventTypeList, OsEventTypeName, } from "g2-kit/lite";
|
||||
import { ImageSource } from "@nativescript/core";
|
||||
import { loadDeviceAddresses } from "./device-addresses";
|
||||
import { ensureBlePermissions } from "./android-permissions";
|
||||
import { FaceclawCommunicatorBridge, type RawInputEvent } from "../native/faceclaw-communicator";
|
||||
import { FaceclawCommunicatorBridge, type FrameMetrics, type RawInputEvent } from "../native/faceclaw-communicator";
|
||||
import { startForegroundNotification, stopForegroundNotification, updateForegroundNotification } from "../native/foreground-service";
|
||||
import { mediaControllerBridge } from "../native/media-controller";
|
||||
import { nightscoutBridge } from "../native/nightscout";
|
||||
import { openEvenAppSettings, readEvenAppNotificationState } from "../native/even-app-conflict";
|
||||
import { grayImageToPreviewSource } from "../native/gray-image-preview";
|
||||
import {
|
||||
applyDashboardScreenTimeout,
|
||||
consumeDashboardTiledWakePaint,
|
||||
drawDashboard,
|
||||
getDashboardNightscoutSettings,
|
||||
getDashboardSystemCardName,
|
||||
@@ -43,6 +44,7 @@ type LogLevel = "debug"|"info"|"warn"|"error";
|
||||
|
||||
const CONTAINER_NAME = "dashboard";
|
||||
const DASHBOARD_INTERVAL_MS = 60_000;
|
||||
const SCREEN_TIMEOUT_CHECK_MS = 1_000;
|
||||
const EVEN_APP_DETECTED_MESSAGE =
|
||||
"The Even Realities app appears to be running. If Faceclaw has trouble connecting, open its app settings and force stop it.";
|
||||
|
||||
@@ -101,18 +103,18 @@ class DashboardController {
|
||||
private activeTextSettingEditorKind: TextSettingEditorKind | null = null;
|
||||
private evenNotificationActive = false;
|
||||
private evenAppConflictMessage = "";
|
||||
private displayPreview: ImageSource | null = grayImageToPreviewSource(
|
||||
new GrayImage(G2_LENS_WIDTH, G2_LENS_HEIGHT, 0),
|
||||
);
|
||||
private displayPreview: ImageSource | null = null;
|
||||
private readonly listeners = new Set<DashboardListener>();
|
||||
|
||||
private communicator: FaceclawCommunicatorBridge | null = null;
|
||||
private dashboardTimer: ReturnType<typeof setInterval> | null = null;
|
||||
private screenTimeoutTimer: ReturnType<typeof setInterval> | null = null;
|
||||
private offState: (() => void) | null = null;
|
||||
private offLog: (() => void) | null = null;
|
||||
private offRing: (() => void) | null = null;
|
||||
private offBattery: (() => void) | null = null;
|
||||
private offEvenAppConflict: (() => void) | null = null;
|
||||
private offFrameMetrics: (() => void) | null = null;
|
||||
private offMedia: (() => void) | null = null;
|
||||
private offNightscout: (() => void) | null = null;
|
||||
private lastInput = "waiting...";
|
||||
@@ -297,6 +299,11 @@ class DashboardController {
|
||||
this.appendLog(message);
|
||||
this.emit();
|
||||
});
|
||||
this.offFrameMetrics = communicator.onFrameMetrics((metrics) => {
|
||||
if (this.phase === "connected") {
|
||||
this.setStatus(`Connected. Last frame: ${this.formatFrameMetrics(metrics)}.`);
|
||||
}
|
||||
});
|
||||
this.offMedia = mediaControllerBridge.onStateChange(() => {
|
||||
if (this.phase === "connected" && this.communicator) {
|
||||
void this.requestRender("interval").catch((error) => {
|
||||
@@ -325,6 +332,14 @@ class DashboardController {
|
||||
this.appendLog(`dashboard update failed: ${message}`);
|
||||
});
|
||||
}, DASHBOARD_INTERVAL_MS);
|
||||
this.screenTimeoutTimer = setInterval(() => {
|
||||
if (this.phase !== "connected" || !this.communicator) return;
|
||||
if (!applyDashboardScreenTimeout()) return;
|
||||
void this.requestRender("interval").catch((error) => {
|
||||
const message = this.formatError(error);
|
||||
this.appendLog(`screen timeout render failed: ${message}`);
|
||||
});
|
||||
}, SCREEN_TIMEOUT_CHECK_MS);
|
||||
} catch (error) {
|
||||
const message = this.formatError(error);
|
||||
this.offState?.();
|
||||
@@ -337,6 +352,8 @@ class DashboardController {
|
||||
this.offBattery = null;
|
||||
this.offEvenAppConflict?.();
|
||||
this.offEvenAppConflict = null;
|
||||
this.offFrameMetrics?.();
|
||||
this.offFrameMetrics = null;
|
||||
this.offMedia?.();
|
||||
this.offMedia = null;
|
||||
this.offNightscout?.();
|
||||
@@ -372,6 +389,8 @@ class DashboardController {
|
||||
this.offBattery = null;
|
||||
this.offEvenAppConflict?.();
|
||||
this.offEvenAppConflict = null;
|
||||
this.offFrameMetrics?.();
|
||||
this.offFrameMetrics = null;
|
||||
this.offMedia?.();
|
||||
this.offMedia = null;
|
||||
this.offNightscout?.();
|
||||
@@ -507,17 +526,18 @@ class DashboardController {
|
||||
}
|
||||
|
||||
private async renderDashboard(reason: "initial" | "interval"): Promise<void> {
|
||||
const now = new Date();
|
||||
const paintStartedAtMs = Date.now();
|
||||
const image = drawDashboard();
|
||||
this.setDisplayPreview(grayImageToPreviewSource(image));
|
||||
if (!this.communicator) return;
|
||||
const paintMs = Date.now() - paintStartedAtMs;
|
||||
const forceTiledCommit = consumeDashboardTiledWakePaint();
|
||||
const fingerprint = image.fingerprint();
|
||||
const tiles = image.toEvenHubTiles().map((tile) => tile.bmp);
|
||||
await this.communicator.submitDashboardImage(tiles, fingerprint);
|
||||
if (this.communicator) {
|
||||
await this.communicator.submitDashboardImage(tiles, fingerprint, forceTiledCommit, paintMs);
|
||||
}
|
||||
this.setDisplayPreview(grayImageToPreviewSource(image));
|
||||
|
||||
const renderedAt = formatForStatus(now);
|
||||
if (this.phase === "connected") {
|
||||
this.setStatus(`Connected. Dashboard queued at ${renderedAt}.`);
|
||||
updateForegroundNotification(`Connected`);
|
||||
}
|
||||
this.appendLog(`${reason === "initial" ? "initial" : "scheduled"} dashboard image queued`);
|
||||
@@ -559,9 +579,14 @@ class DashboardController {
|
||||
}
|
||||
|
||||
private clearDashboardTimer(): void {
|
||||
if (!this.dashboardTimer) return;
|
||||
clearInterval(this.dashboardTimer);
|
||||
this.dashboardTimer = null;
|
||||
if (this.dashboardTimer) {
|
||||
clearInterval(this.dashboardTimer);
|
||||
this.dashboardTimer = null;
|
||||
}
|
||||
if (this.screenTimeoutTimer) {
|
||||
clearInterval(this.screenTimeoutTimer);
|
||||
this.screenTimeoutTimer = null;
|
||||
}
|
||||
}
|
||||
|
||||
private setPhase(phase: ConnectionPhase): void {
|
||||
@@ -576,6 +601,10 @@ class DashboardController {
|
||||
this.emit();
|
||||
}
|
||||
|
||||
private formatFrameMetrics(metrics: FrameMetrics): string {
|
||||
return `paint=${Math.round(metrics.paintMs)}ms, transmit=${Math.round(metrics.transmitMs)}ms, tiles=${Math.round(metrics.tileCount)}`;
|
||||
}
|
||||
|
||||
private appendLog(line: string): void {
|
||||
const stamped = `[${formatTimestamp(new Date())}] ${line}`;
|
||||
this.log = this.log ? `${this.log}\n${stamped}` : stamped;
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
import { GrayImage, imageFromAsciiArt } from "./image";
|
||||
|
||||
const EMPTY_BATTERY_ICON = imageFromAsciiArt(
|
||||
[
|
||||
" ################## ",
|
||||
" #................# ",
|
||||
" #................###",
|
||||
" #................###",
|
||||
" #................###",
|
||||
" #................###",
|
||||
" #................###",
|
||||
" #................###",
|
||||
" #................# ",
|
||||
" ################## ",
|
||||
],
|
||||
120,
|
||||
);
|
||||
|
||||
const BATTERY_BOLT_ICON = imageFromAsciiArt(
|
||||
[
|
||||
"....#..",
|
||||
"...##..",
|
||||
"..##...",
|
||||
".######",
|
||||
"...##..",
|
||||
"..##...",
|
||||
"..#....",
|
||||
],
|
||||
255,
|
||||
);
|
||||
|
||||
export const BATTERY_ICON_WIDTH = EMPTY_BATTERY_ICON.width;
|
||||
|
||||
const BATTERY_FILL_X = 3;
|
||||
const BATTERY_FILL_Y = 2;
|
||||
const BATTERY_FILL_WIDTH = 14;
|
||||
const BATTERY_FILL_HEIGHT = 6;
|
||||
|
||||
export function drawBattery(percentCharge: number, isCharging: boolean): GrayImage {
|
||||
const icon = new GrayImage(EMPTY_BATTERY_ICON.width, EMPTY_BATTERY_ICON.height, 0);
|
||||
icon.bitBlt(EMPTY_BATTERY_ICON, 0, 0);
|
||||
const clamped = Math.max(0, Math.min(100, percentCharge));
|
||||
const fillWidth = Math.round((BATTERY_FILL_WIDTH * clamped) / 100);
|
||||
icon.fillRect(BATTERY_FILL_X, BATTERY_FILL_Y, fillWidth, BATTERY_FILL_HEIGHT, 190);
|
||||
if (isCharging) {
|
||||
overlayImage(icon, BATTERY_BOLT_ICON, 7, 1);
|
||||
}
|
||||
return icon;
|
||||
}
|
||||
|
||||
function overlayImage(target: GrayImage, source: GrayImage, dx: number, dy: number): void {
|
||||
for (let y = 0; y < source.height; y++) {
|
||||
for (let x = 0; x < source.width; x++) {
|
||||
const value = source.pixels[y * source.width + x] ?? 0;
|
||||
if (value > 0) {
|
||||
target.setPixel(dx + x, dy + y, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -19,6 +19,12 @@ export type HeadsetBatteryState = {
|
||||
chargingStatus: number;
|
||||
};
|
||||
|
||||
export type FrameMetrics = {
|
||||
paintMs: number;
|
||||
transmitMs: number;
|
||||
tileCount: number;
|
||||
};
|
||||
|
||||
export type RawInputEvent =
|
||||
| {
|
||||
kind: "list-click";
|
||||
@@ -51,6 +57,11 @@ function toJavaByteArray(bytes: Uint8Array): number[] {
|
||||
return out;
|
||||
}
|
||||
|
||||
function nonNegativeNumber(value: number): number {
|
||||
const numeric = Number(value);
|
||||
return Number.isFinite(numeric) ? Math.max(0, numeric) : 0;
|
||||
}
|
||||
|
||||
export class FaceclawCommunicatorBridge {
|
||||
private readonly communicator: any;
|
||||
private readonly listenerProxy: any;
|
||||
@@ -59,6 +70,7 @@ export class FaceclawCommunicatorBridge {
|
||||
private readonly ringListeners = new Set<(event: RawInputEvent) => void>();
|
||||
private readonly batteryListeners = new Set<(state: HeadsetBatteryState) => void>();
|
||||
private readonly evenAppConflictListeners = new Set<(message: string) => void>();
|
||||
private readonly frameMetricsListeners = new Set<(metrics: FrameMetrics) => void>();
|
||||
|
||||
constructor(addresses: { right: string; left: string; ring?: string }) {
|
||||
const context = Utils.android.getApplicationContext();
|
||||
@@ -117,6 +129,16 @@ export class FaceclawCommunicatorBridge {
|
||||
listener(String(message));
|
||||
}
|
||||
},
|
||||
onFrameMetrics: (paintMs: number, transmitMs: number, tileCount: number) => {
|
||||
const metrics = {
|
||||
paintMs: nonNegativeNumber(paintMs),
|
||||
transmitMs: nonNegativeNumber(transmitMs),
|
||||
tileCount: nonNegativeNumber(tileCount),
|
||||
};
|
||||
for (const listener of this.frameMetricsListeners) {
|
||||
listener(metrics);
|
||||
}
|
||||
},
|
||||
});
|
||||
this.communicator.setListener(this.listenerProxy);
|
||||
}
|
||||
@@ -146,11 +168,16 @@ export class FaceclawCommunicatorBridge {
|
||||
return () => this.evenAppConflictListeners.delete(listener);
|
||||
}
|
||||
|
||||
onFrameMetrics(listener: (metrics: FrameMetrics) => void): () => void {
|
||||
this.frameMetricsListeners.add(listener);
|
||||
return () => this.frameMetricsListeners.delete(listener);
|
||||
}
|
||||
|
||||
async start(): Promise<void> {
|
||||
this.communicator.start();
|
||||
}
|
||||
|
||||
async submitDashboardImage(tileBmps: Uint8Array[], fingerprint: string): Promise<void> {
|
||||
async submitDashboardImage(tileBmps: Uint8Array[], fingerprint: string, forceTiledCommit = false, paintMs = -1): Promise<void> {
|
||||
if (tileBmps.length !== 4) {
|
||||
throw new Error(`expected 4 dashboard tiles, got ${tileBmps.length}`);
|
||||
}
|
||||
@@ -160,6 +187,8 @@ export class FaceclawCommunicatorBridge {
|
||||
toJavaByteArray(tileBmps[2]!),
|
||||
toJavaByteArray(tileBmps[3]!),
|
||||
fingerprint,
|
||||
forceTiledCommit,
|
||||
Math.round(nonNegativeNumber(paintMs)),
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
import { ImageSource, Screen, Utils } from "@nativescript/core";
|
||||
import { ImageSource } from "@nativescript/core";
|
||||
|
||||
import { type GrayImage } from "../graphics/image";
|
||||
|
||||
const PREVIEW_BRIGHTEN_GAMMA = 0.7;
|
||||
const PREVIEW_COLOR_LUT = buildPreviewColorLookup();
|
||||
|
||||
export function grayImageToPreviewSource(image: GrayImage): ImageSource | null {
|
||||
if (!global.isAndroid) {
|
||||
@@ -11,8 +12,7 @@ export function grayImageToPreviewSource(image: GrayImage): ImageSource | null {
|
||||
|
||||
const colors = Array.create("int", image.width * image.height) as number[];
|
||||
for (let i = 0; i < image.pixels.length; i++) {
|
||||
const gray = brightenForPreview(image.pixels[i] ?? 0);
|
||||
colors[i] = (255 << 24) | (gray << 16) | (gray << 8) | gray;
|
||||
colors[i] = PREVIEW_COLOR_LUT[image.pixels[i] ?? 0]!;
|
||||
}
|
||||
|
||||
const bitmap = android.graphics.Bitmap.createBitmap(
|
||||
@@ -21,16 +21,15 @@ export function grayImageToPreviewSource(image: GrayImage): ImageSource | null {
|
||||
image.height,
|
||||
android.graphics.Bitmap.Config.ARGB_8888,
|
||||
);
|
||||
const targetWidth = Math.max(1, Math.round(Utils.layout.toDevicePixels(Screen.mainScreen.widthDIPs)));
|
||||
const targetHeight = Math.max(1, Math.round((targetWidth * image.height) / image.width));
|
||||
const scaledBitmap =
|
||||
bitmap.getWidth() === targetWidth && bitmap.getHeight() === targetHeight
|
||||
? bitmap
|
||||
: android.graphics.Bitmap.createScaledBitmap(bitmap, targetWidth, targetHeight, false);
|
||||
return new ImageSource(scaledBitmap);
|
||||
return new ImageSource(bitmap);
|
||||
}
|
||||
|
||||
function brightenForPreview(gray: number): number {
|
||||
const normalized = Math.max(0, Math.min(255, gray)) / 255;
|
||||
return Math.max(0, Math.min(255, Math.round(255 * Math.pow(normalized, PREVIEW_BRIGHTEN_GAMMA))));
|
||||
function buildPreviewColorLookup(): number[] {
|
||||
const colors: number[] = [];
|
||||
for (let gray = 0; gray <= 255; gray++) {
|
||||
const normalized = gray / 255;
|
||||
const preview = Math.max(0, Math.min(255, Math.round(255 * Math.pow(normalized, PREVIEW_BRIGHTEN_GAMMA))));
|
||||
colors[gray] = (255 << 24) | (preview << 16) | (preview << 8) | preview;
|
||||
}
|
||||
return colors;
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
import { Utils } from "@nativescript/core";
|
||||
|
||||
import { GrayImage } from "../graphics/image";
|
||||
|
||||
declare const com: any;
|
||||
|
||||
const ICON_SIZE = 24;
|
||||
const ICON_CACHE_MS = 5_000;
|
||||
|
||||
let cachedIcons: GrayImage[] = [];
|
||||
let cachedAtMs = 0;
|
||||
|
||||
export function readSystemStatusIcons(): GrayImage[] {
|
||||
if (!global.isAndroid) return [];
|
||||
|
||||
const now = Date.now();
|
||||
if (now - cachedAtMs < ICON_CACHE_MS) {
|
||||
return cachedIcons.map(cloneGrayImage);
|
||||
}
|
||||
|
||||
const context = Utils.android.getApplicationContext();
|
||||
if (!context) return [];
|
||||
|
||||
const bytes = toUint8Array(
|
||||
com.faceclaw.app.FaceclawSystemStatusIconProvider.getSystemStatusIconGrays(context, ICON_SIZE),
|
||||
);
|
||||
const iconByteLength = ICON_SIZE * ICON_SIZE;
|
||||
const iconCount = Math.floor(bytes.length / iconByteLength);
|
||||
const icons: GrayImage[] = [];
|
||||
for (let index = 0; index < iconCount; index++) {
|
||||
const icon = new GrayImage(ICON_SIZE, ICON_SIZE, 0);
|
||||
icon.pixels.set(bytes.subarray(index * iconByteLength, (index + 1) * iconByteLength));
|
||||
icons.push(icon);
|
||||
}
|
||||
|
||||
cachedIcons = icons;
|
||||
cachedAtMs = now;
|
||||
return icons.map(cloneGrayImage);
|
||||
}
|
||||
|
||||
function toUint8Array(bytes: ArrayLike<number> | null | undefined): Uint8Array {
|
||||
if (!bytes) return new Uint8Array(0);
|
||||
const out = new Uint8Array(bytes.length);
|
||||
for (let i = 0; i < bytes.length; i++) {
|
||||
out[i] = bytes[i]! & 0xff;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
function cloneGrayImage(image: GrayImage): GrayImage {
|
||||
const clone = new GrayImage(image.width, image.height, 0);
|
||||
clone.pixels.set(image.pixels);
|
||||
return clone;
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
import { ApplicationSettings } from "@nativescript/core";
|
||||
|
||||
export type DashboardSlotId = "top-right" | "bottom-left" | "bottom-right";
|
||||
export type DashboardSlotId = "bottom-left" | "bottom-right";
|
||||
export type DashboardPluginId = "blank" | "input-debug-log" | "music-controller" | "nightscout";
|
||||
|
||||
export type DashboardSlotConfig = Record<DashboardSlotId, DashboardPluginId>;
|
||||
@@ -12,20 +12,27 @@ export type SystemCardSettings = {
|
||||
showFaceclawLogo: boolean;
|
||||
showBatteryIndicators: boolean;
|
||||
showAndroidNotifications: boolean;
|
||||
showSignalStrength: boolean;
|
||||
};
|
||||
export type SystemCardSettingKey = keyof SystemCardSettings;
|
||||
export type ScreenTimeoutSetting = "15s" | "30s" | "1m" | "never";
|
||||
export type WakeModeSetting = "tiled" | "synchronized";
|
||||
|
||||
export const DEFAULT_SYSTEM_CARD_NAME = "Faceclaw";
|
||||
export const DEFAULT_SCREEN_TIMEOUT: ScreenTimeoutSetting = "30s";
|
||||
export const DEFAULT_WAKE_MODE: WakeModeSetting = "tiled";
|
||||
const SYSTEM_CARD_NAME_KEY = "dashboard.systemCardName";
|
||||
const SCREEN_TIMEOUT_KEY = "display.screenTimeout";
|
||||
const WAKE_MODE_KEY = "display.wakeMode";
|
||||
const SYSTEM_CARD_SETTING_KEYS: Record<SystemCardSettingKey, string> = {
|
||||
showFaceclawLogo: "dashboard.systemCard.showFaceclawLogo",
|
||||
showBatteryIndicators: "dashboard.systemCard.showBatteryIndicators",
|
||||
showAndroidNotifications: "dashboard.systemCard.showAndroidNotifications",
|
||||
showSignalStrength: "dashboard.systemCard.showSignalStrength",
|
||||
};
|
||||
const NIGHTSCOUT_SITE_URL_KEY = "integrations.nightscout.siteUrl";
|
||||
const NIGHTSCOUT_API_TOKEN_KEY = "integrations.nightscout.apiToken";
|
||||
const SLOT_KEYS: Record<DashboardSlotId, string> = {
|
||||
"top-right": "dashboard.slot.topRight",
|
||||
"bottom-left": "dashboard.slot.bottomLeft",
|
||||
"bottom-right": "dashboard.slot.bottomRight",
|
||||
};
|
||||
@@ -38,8 +45,7 @@ const KNOWN_PLUGIN_IDS = new Set<DashboardPluginId>([
|
||||
]);
|
||||
|
||||
const DEFAULT_SLOT_CONFIG: DashboardSlotConfig = {
|
||||
"top-right": "input-debug-log",
|
||||
"bottom-left": "blank",
|
||||
"bottom-left": "input-debug-log",
|
||||
"bottom-right": "blank",
|
||||
};
|
||||
|
||||
@@ -47,10 +53,11 @@ const DEFAULT_SYSTEM_CARD_SETTINGS: SystemCardSettings = {
|
||||
showFaceclawLogo: true,
|
||||
showBatteryIndicators: true,
|
||||
showAndroidNotifications: true,
|
||||
showSignalStrength: true,
|
||||
};
|
||||
|
||||
export function getDashboardSlotIds(): DashboardSlotId[] {
|
||||
return ["top-right", "bottom-left", "bottom-right"];
|
||||
return ["bottom-left", "bottom-right"];
|
||||
}
|
||||
|
||||
export function getDefaultDashboardSlotConfig(): DashboardSlotConfig {
|
||||
@@ -100,6 +107,10 @@ export function loadSystemCardSettings(): SystemCardSettings {
|
||||
SYSTEM_CARD_SETTING_KEYS.showAndroidNotifications,
|
||||
DEFAULT_SYSTEM_CARD_SETTINGS.showAndroidNotifications,
|
||||
),
|
||||
showSignalStrength: ApplicationSettings.getBoolean(
|
||||
SYSTEM_CARD_SETTING_KEYS.showSignalStrength,
|
||||
DEFAULT_SYSTEM_CARD_SETTINGS.showSignalStrength,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -107,6 +118,65 @@ export function saveSystemCardSetting(key: SystemCardSettingKey, value: boolean)
|
||||
ApplicationSettings.setBoolean(SYSTEM_CARD_SETTING_KEYS[key], value);
|
||||
}
|
||||
|
||||
export function loadScreenTimeoutSetting(): ScreenTimeoutSetting {
|
||||
return normalizeScreenTimeoutSetting(ApplicationSettings.getString(SCREEN_TIMEOUT_KEY, DEFAULT_SCREEN_TIMEOUT));
|
||||
}
|
||||
|
||||
export function saveScreenTimeoutSetting(value: ScreenTimeoutSetting): ScreenTimeoutSetting {
|
||||
const normalized = normalizeScreenTimeoutSetting(value);
|
||||
ApplicationSettings.setString(SCREEN_TIMEOUT_KEY, normalized);
|
||||
return normalized;
|
||||
}
|
||||
|
||||
export function loadWakeModeSetting(): WakeModeSetting {
|
||||
return normalizeWakeModeSetting(ApplicationSettings.getString(WAKE_MODE_KEY, DEFAULT_WAKE_MODE));
|
||||
}
|
||||
|
||||
export function saveWakeModeSetting(value: WakeModeSetting): WakeModeSetting {
|
||||
const normalized = normalizeWakeModeSetting(value);
|
||||
ApplicationSettings.setString(WAKE_MODE_KEY, normalized);
|
||||
return normalized;
|
||||
}
|
||||
|
||||
export function nextScreenTimeoutSetting(value: ScreenTimeoutSetting): ScreenTimeoutSetting {
|
||||
switch (value) {
|
||||
case "15s":
|
||||
return "30s";
|
||||
case "30s":
|
||||
return "1m";
|
||||
case "1m":
|
||||
return "never";
|
||||
case "never":
|
||||
default:
|
||||
return "15s";
|
||||
}
|
||||
}
|
||||
|
||||
export function screenTimeoutMs(value: ScreenTimeoutSetting): number | null {
|
||||
switch (value) {
|
||||
case "15s":
|
||||
return 15_000;
|
||||
case "30s":
|
||||
return 30_000;
|
||||
case "1m":
|
||||
return 60_000;
|
||||
case "never":
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export function screenTimeoutLabel(value: ScreenTimeoutSetting): string {
|
||||
return value === "never" ? "Never" : value;
|
||||
}
|
||||
|
||||
export function nextWakeModeSetting(value: WakeModeSetting): WakeModeSetting {
|
||||
return value === "tiled" ? "synchronized" : "tiled";
|
||||
}
|
||||
|
||||
export function wakeModeLabel(value: WakeModeSetting): string {
|
||||
return value === "tiled" ? "Tiled" : "Synchronized";
|
||||
}
|
||||
|
||||
export function loadNightscoutSettings(): NightscoutSettings {
|
||||
return {
|
||||
siteUrl: normalizeNightscoutSiteUrl(ApplicationSettings.getString(NIGHTSCOUT_SITE_URL_KEY, "")),
|
||||
@@ -152,3 +222,25 @@ function normalizeNightscoutSiteUrl(siteUrl: string | null | undefined): string
|
||||
function normalizeNightscoutApiToken(apiToken: string | null | undefined): string {
|
||||
return (apiToken ?? "").replace(/[\x00-\x1f]+/g, "").trim();
|
||||
}
|
||||
|
||||
function normalizeScreenTimeoutSetting(value: string | null | undefined): ScreenTimeoutSetting {
|
||||
switch (value) {
|
||||
case "15s":
|
||||
case "30s":
|
||||
case "1m":
|
||||
case "never":
|
||||
return value;
|
||||
default:
|
||||
return DEFAULT_SCREEN_TIMEOUT;
|
||||
}
|
||||
}
|
||||
|
||||
function normalizeWakeModeSetting(value: string | null | undefined): WakeModeSetting {
|
||||
switch (value) {
|
||||
case "tiled":
|
||||
case "synchronized":
|
||||
return value;
|
||||
default:
|
||||
return DEFAULT_WAKE_MODE;
|
||||
}
|
||||
}
|
||||
+171
-181
@@ -1,11 +1,13 @@
|
||||
import { G2_LENS_HEIGHT, G2_LENS_WIDTH, GrayImage, imageFromAsciiArt } from "../graphics/image";
|
||||
import { G2_LENS_HEIGHT, G2_LENS_WIDTH, GrayImage } from "../graphics/image";
|
||||
import { loadEmbeddedTerminus12, loadEmbeddedTerminus16 } 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";
|
||||
import { readActiveNotificationIcons } from "../native/notification-icons";
|
||||
import { readPhoneBatteryState } from "../native/phone-battery";
|
||||
import { readSystemStatusIcons } from "../native/system-status-icons";
|
||||
import { OsEventTypeList, EventSourceType } from "g2-kit/lite";
|
||||
import {
|
||||
getDashboardPlugin,
|
||||
@@ -20,19 +22,30 @@ import {
|
||||
isNightscoutSettingsConfigured,
|
||||
loadDashboardSlotConfig,
|
||||
loadNightscoutSettings,
|
||||
loadScreenTimeoutSetting,
|
||||
loadSystemCardSettings,
|
||||
loadSystemCardName,
|
||||
loadWakeModeSetting,
|
||||
nextScreenTimeoutSetting,
|
||||
nextWakeModeSetting,
|
||||
saveDashboardSlotPlugin,
|
||||
saveNightscoutApiToken,
|
||||
saveNightscoutSiteUrl,
|
||||
saveScreenTimeoutSetting,
|
||||
saveWakeModeSetting,
|
||||
screenTimeoutLabel,
|
||||
screenTimeoutMs,
|
||||
saveSystemCardSetting,
|
||||
saveSystemCardName,
|
||||
wakeModeLabel,
|
||||
type DashboardSlotId,
|
||||
type NightscoutSettings,
|
||||
type ScreenTimeoutSetting,
|
||||
type SystemCardSettingKey,
|
||||
type WakeModeSetting,
|
||||
} from "./dashboard-settings";
|
||||
import { Layer, LayerActions, LayerStack, type DashboardInputEvent, type LayerContext } from "./layers";
|
||||
import { MenuLayer } from "./menu";
|
||||
import { drawRightValueMenuItem, drawToggleMenuItem, MenuLayer } from "./menu";
|
||||
|
||||
type DashboardCardId = "system" | DashboardSlotId;
|
||||
export type DashboardBatteryLevels = {
|
||||
@@ -44,10 +57,13 @@ export type DashboardBatteryLevels = {
|
||||
let dashboardState = {
|
||||
logLines: [] as string[],
|
||||
screenOn: true,
|
||||
selectedCard: "system" as DashboardCardId,
|
||||
lastInputAtMs: Date.now(),
|
||||
slotConfig: loadDashboardSlotConfig(),
|
||||
systemCardName: loadSystemCardName(),
|
||||
systemCardSettings: loadSystemCardSettings(),
|
||||
screenTimeout: loadScreenTimeoutSetting(),
|
||||
wakeMode: loadWakeModeSetting(),
|
||||
tiledWakePaintPending: false,
|
||||
nightscoutSettings: loadNightscoutSettings(),
|
||||
battery: {
|
||||
headset: null,
|
||||
@@ -71,37 +87,8 @@ const NOTIFICATION_ICON_SIZE = 24;
|
||||
const SYSTEM_CARD_ITEM_HEIGHT = 38;
|
||||
const SYSTEM_CARD_ITEM_GAP = 2;
|
||||
const BATTERY_ITEM_Y_OFFSET = 4;
|
||||
const EMPTY_BATTERY_ICON = imageFromAsciiArt(
|
||||
[
|
||||
" ################## ",
|
||||
" #................# ",
|
||||
" #................###",
|
||||
" #................###",
|
||||
" #................###",
|
||||
" #................###",
|
||||
" #................###",
|
||||
" #................###",
|
||||
" #................# ",
|
||||
" ################## ",
|
||||
],
|
||||
120,
|
||||
);
|
||||
const BATTERY_BOLT_ICON = imageFromAsciiArt(
|
||||
[
|
||||
"....#..",
|
||||
"...##..",
|
||||
"..##...",
|
||||
".######",
|
||||
"...##..",
|
||||
"..##...",
|
||||
"..#....",
|
||||
],
|
||||
255,
|
||||
);
|
||||
const BATTERY_FILL_X = 3;
|
||||
const BATTERY_FILL_Y = 2;
|
||||
const BATTERY_FILL_WIDTH = 14;
|
||||
const BATTERY_FILL_HEIGHT = 6;
|
||||
const TOP_LEFT_MENU_LAYOUT = { x: 8, y: 8, width: 272, height: 128 };
|
||||
const TOP_RIGHT_MENU_LAYOUT = { x: 296, y: 8, width: 272, height: 128 };
|
||||
|
||||
function rawInputEventToInputEvent(event: RawInputEvent): DashboardInputEvent {
|
||||
if (event.kind === "sys-event") {
|
||||
@@ -144,6 +131,7 @@ function eventSourceToString(eventSource: number): "ring" | "left-arm" | "right-
|
||||
|
||||
export async function receiveInput(event: RawInputEvent): Promise<void> {
|
||||
const inputEvent = rawInputEventToInputEvent(event);
|
||||
dashboardState.lastInputAtMs = Date.now();
|
||||
dashboardState.logLines.push(eventToString(inputEvent));
|
||||
await dashboardLayers.handleInput(inputEvent);
|
||||
}
|
||||
@@ -176,6 +164,12 @@ export function drawDashboard(): GrayImage {
|
||||
return dashboardLayers.paint();
|
||||
}
|
||||
|
||||
export function consumeDashboardTiledWakePaint(): boolean {
|
||||
const value = dashboardState.tiledWakePaintPending;
|
||||
dashboardState.tiledWakePaintPending = false;
|
||||
return value;
|
||||
}
|
||||
|
||||
export function setDashboardSystemCardName(name: string): string {
|
||||
const savedName = saveSystemCardName(name);
|
||||
dashboardState.systemCardName = savedName;
|
||||
@@ -216,6 +210,15 @@ export function setDashboardSystemCardSetting(key: SystemCardSettingKey, value:
|
||||
saveSystemCardSetting(key, value);
|
||||
}
|
||||
|
||||
export function applyDashboardScreenTimeout(nowMs = Date.now()): boolean {
|
||||
const timeoutMs = screenTimeoutMs(dashboardState.screenTimeout);
|
||||
if (timeoutMs === null || !dashboardState.screenOn) return false;
|
||||
if (nowMs - dashboardState.lastInputAtMs < timeoutMs) return false;
|
||||
dashboardState.screenOn = false;
|
||||
dashboardLayers.clearToBase();
|
||||
return true;
|
||||
}
|
||||
|
||||
export function setDashboardBatteryLevels(levels: Partial<DashboardBatteryLevels>): void {
|
||||
dashboardState.battery = {
|
||||
...dashboardState.battery,
|
||||
@@ -264,29 +267,6 @@ function drawBatteryFlowItem(image: GrayImage, x: number, y: number, item: Extra
|
||||
image.bitBlt(battery, x + ((itemWidth - battery.width) / 2) | 0, y + 16 + BATTERY_ITEM_Y_OFFSET);
|
||||
}
|
||||
|
||||
function drawBattery(percentCharge: number, isCharging: boolean): GrayImage {
|
||||
const icon = new GrayImage(EMPTY_BATTERY_ICON.width, EMPTY_BATTERY_ICON.height, 0);
|
||||
icon.bitBlt(EMPTY_BATTERY_ICON, 0, 0);
|
||||
const clamped = Math.max(0, Math.min(100, percentCharge));
|
||||
const fillWidth = Math.round((BATTERY_FILL_WIDTH * clamped) / 100);
|
||||
icon.fillRect(BATTERY_FILL_X, BATTERY_FILL_Y, fillWidth, BATTERY_FILL_HEIGHT, 190);
|
||||
if (isCharging) {
|
||||
overlayImage(icon, BATTERY_BOLT_ICON, 7, 1);
|
||||
}
|
||||
return icon;
|
||||
}
|
||||
|
||||
function overlayImage(target: GrayImage, source: GrayImage, dx: number, dy: number): void {
|
||||
for (let y = 0; y < source.height; y++) {
|
||||
for (let x = 0; x < source.width; x++) {
|
||||
const value = source.pixels[y * source.width + x] ?? 0;
|
||||
if (value > 0) {
|
||||
target.setPixel(dx + x, dy + y, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const MONTHS = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
|
||||
const WEEKDAYS = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
|
||||
|
||||
@@ -315,13 +295,19 @@ function drawSystemCardFlowItems(image: GrayImage, bounds: DashboardPluginCardBo
|
||||
const top = bounds.y + 84;
|
||||
const right = bounds.x + bounds.width - 10;
|
||||
const bottom = bounds.y + bounds.height - 6;
|
||||
const items = collectBatteryItems();
|
||||
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;
|
||||
@@ -351,7 +337,7 @@ function systemCardFlowItemWidth(item: SystemCardFlowItem): number {
|
||||
if (item.type === "notification") {
|
||||
return NOTIFICATION_ICON_SIZE;
|
||||
}
|
||||
return Math.max(EMPTY_BATTERY_ICON.width, dashboardFont.measureText(item.label));
|
||||
return Math.max(BATTERY_ICON_WIDTH, dashboardFont.measureText(item.label));
|
||||
}
|
||||
|
||||
class DashboardLayer implements Layer {
|
||||
@@ -366,7 +352,6 @@ class DashboardLayer implements Layer {
|
||||
const pluginState = getPluginState();
|
||||
|
||||
const systemBounds = getCardBounds("system");
|
||||
this.drawCardBorder(image, systemBounds, dashboardState.selectedCard === "system");
|
||||
if (logo && dashboardState.systemCardSettings.showFaceclawLogo) {
|
||||
image.bitBlt(logo, systemBounds.x + 10, systemBounds.y + 10);
|
||||
}
|
||||
@@ -378,13 +363,10 @@ class DashboardLayer implements Layer {
|
||||
for (const slot of getDashboardSlotIds()) {
|
||||
const bounds = getCardBounds(slot);
|
||||
const pluginId = dashboardState.slotConfig[slot];
|
||||
if (!isBlankDashboardPlugin(pluginId)) {
|
||||
this.drawCardBorder(image, bounds, dashboardState.selectedCard === slot);
|
||||
}
|
||||
getDashboardPlugin(pluginId).renderCard({
|
||||
image,
|
||||
bounds,
|
||||
selected: dashboardState.selectedCard === slot,
|
||||
selected: false,
|
||||
font: dashboardFont,
|
||||
state: pluginState,
|
||||
});
|
||||
@@ -396,62 +378,43 @@ class DashboardLayer implements Layer {
|
||||
handleInput(event: DashboardInputEvent, ctx: { stack: LayerStack }): void {
|
||||
if (!dashboardState.screenOn) {
|
||||
if (event.type === "double-click") {
|
||||
dashboardState.selectedCard = "system";
|
||||
dashboardState.screenOn = true;
|
||||
dashboardState.tiledWakePaintPending = dashboardState.wakeMode === "tiled";
|
||||
ctx.stack.push(createRootMenuLayer());
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (event.type === "scroll-up") {
|
||||
cycleSelection(-1);
|
||||
return;
|
||||
}
|
||||
|
||||
if (event.type === "scroll-down") {
|
||||
cycleSelection(1);
|
||||
return;
|
||||
}
|
||||
|
||||
if (event.type === "click") {
|
||||
if (dashboardState.selectedCard === "system") {
|
||||
ctx.stack.push(createRootMenuLayer());
|
||||
} else {
|
||||
const pluginId = dashboardState.slotConfig[dashboardState.selectedCard];
|
||||
if (pluginId === "nightscout" && !isNightscoutSettingsConfigured(dashboardState.nightscoutSettings)) {
|
||||
ctx.stack.push(createNightscoutSettingsMenuLayer());
|
||||
return;
|
||||
}
|
||||
const layer = getDashboardPlugin(pluginId).createFullscreenLayer?.(
|
||||
getPluginState,
|
||||
);
|
||||
if (layer) {
|
||||
ctx.stack.push(layer);
|
||||
}
|
||||
}
|
||||
ctx.stack.push(createRootMenuLayer());
|
||||
return;
|
||||
}
|
||||
|
||||
if (event.type === "double-click") {
|
||||
dashboardState.selectedCard = "system";
|
||||
dashboardState.screenOn = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private drawCardBorder(image: GrayImage, bounds: DashboardPluginCardBounds, selected: boolean): void {
|
||||
if (selected) {
|
||||
image.drawRect(bounds.x, bounds.y, bounds.width, bounds.height, 60);
|
||||
class RootMenuLayer extends MenuLayer {
|
||||
async handleInput(event: DashboardInputEvent, ctx: LayerContext): Promise<void> {
|
||||
if (event.type === "double-click") {
|
||||
dashboardState.screenOn = false;
|
||||
ctx.stack.clearToBase();
|
||||
return;
|
||||
}
|
||||
await super.handleInput(event, ctx);
|
||||
}
|
||||
}
|
||||
|
||||
function createRootMenuLayer(): MenuLayer {
|
||||
return new MenuLayer(
|
||||
return new RootMenuLayer(
|
||||
"Menu",
|
||||
[
|
||||
{
|
||||
label: "About",
|
||||
label: "Apps",
|
||||
onSelect: (ctx) => {
|
||||
ctx.stack.push(new AboutLayer());
|
||||
ctx.stack.push(createAppsMenuLayer());
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -460,6 +423,40 @@ function createRootMenuLayer(): MenuLayer {
|
||||
ctx.stack.push(createSettingsMenuLayer());
|
||||
},
|
||||
},
|
||||
...getDashboardSlotIds().map((slot) => ({
|
||||
label: pluginLabelForSlot(slot),
|
||||
onSelect: (ctx: LayerContext) => {
|
||||
openDashboardSlot(slot, ctx);
|
||||
},
|
||||
render: ({ image, x, y }: { image: GrayImage; x: number; y: number }) => {
|
||||
image.drawText(dashboardFont, x, y + 3, pluginLabelForSlot(slot), 200);
|
||||
},
|
||||
})),
|
||||
{
|
||||
label: "System",
|
||||
onSelect: (ctx) => {
|
||||
ctx.stack.push(createSystemMenuLayer());
|
||||
},
|
||||
},
|
||||
],
|
||||
TOP_LEFT_MENU_LAYOUT,
|
||||
);
|
||||
}
|
||||
|
||||
function createAppsMenuLayer(): MenuLayer {
|
||||
return new MenuLayer("Apps", [], TOP_LEFT_MENU_LAYOUT);
|
||||
}
|
||||
|
||||
function createSystemMenuLayer(): MenuLayer {
|
||||
return new MenuLayer(
|
||||
"System",
|
||||
[
|
||||
{
|
||||
label: "About",
|
||||
onSelect: (ctx) => {
|
||||
ctx.stack.push(new AboutLayer());
|
||||
},
|
||||
},
|
||||
{
|
||||
label: "Quit / Disconnect",
|
||||
onSelect: async (ctx) => {
|
||||
@@ -468,19 +465,35 @@ function createRootMenuLayer(): MenuLayer {
|
||||
},
|
||||
},
|
||||
],
|
||||
{
|
||||
x: 16,
|
||||
y: 24,
|
||||
width: 252,
|
||||
height: 180,
|
||||
},
|
||||
TOP_LEFT_MENU_LAYOUT,
|
||||
);
|
||||
}
|
||||
|
||||
function openDashboardSlot(slot: DashboardSlotId, ctx: LayerContext): void {
|
||||
const pluginId = dashboardState.slotConfig[slot];
|
||||
if (isBlankDashboardPlugin(pluginId)) {
|
||||
return;
|
||||
}
|
||||
if (pluginId === "nightscout" && !isNightscoutSettingsConfigured(dashboardState.nightscoutSettings)) {
|
||||
ctx.stack.push(createNightscoutSettingsMenuLayer());
|
||||
return;
|
||||
}
|
||||
const layer = getDashboardPlugin(pluginId).createFullscreenLayer?.(getPluginState);
|
||||
if (layer) {
|
||||
ctx.stack.push(layer);
|
||||
}
|
||||
}
|
||||
|
||||
function createSettingsMenuLayer(): MenuLayer {
|
||||
return new MenuLayer(
|
||||
"Settings",
|
||||
[
|
||||
{
|
||||
label: "Display",
|
||||
onSelect: (ctx) => {
|
||||
ctx.stack.push(createDisplaySettingsMenuLayer());
|
||||
},
|
||||
},
|
||||
{
|
||||
label: "Dashboard",
|
||||
onSelect: (ctx) => {
|
||||
@@ -494,15 +507,46 @@ function createSettingsMenuLayer(): MenuLayer {
|
||||
},
|
||||
},
|
||||
],
|
||||
{
|
||||
x: 16,
|
||||
y: 24,
|
||||
width: 252,
|
||||
height: 180,
|
||||
},
|
||||
TOP_LEFT_MENU_LAYOUT,
|
||||
);
|
||||
}
|
||||
|
||||
function createDisplaySettingsMenuLayer(): MenuLayer {
|
||||
return new MenuLayer(
|
||||
"Display",
|
||||
[
|
||||
{
|
||||
label: "Screen timeout",
|
||||
onSelect: () => {
|
||||
setScreenTimeout(nextScreenTimeoutSetting(dashboardState.screenTimeout));
|
||||
},
|
||||
render: ({ image, x, y, width }) => {
|
||||
drawRightValueMenuItem(image, dashboardFont, x, y, width, "Screen timeout", screenTimeoutLabel(dashboardState.screenTimeout));
|
||||
},
|
||||
},
|
||||
{
|
||||
label: "Wake mode",
|
||||
onSelect: () => {
|
||||
setWakeMode(nextWakeModeSetting(dashboardState.wakeMode));
|
||||
},
|
||||
render: ({ image, x, y, width }) => {
|
||||
drawRightValueMenuItem(image, dashboardFont, x, y, width, "Wake mode", wakeModeLabel(dashboardState.wakeMode));
|
||||
},
|
||||
},
|
||||
],
|
||||
TOP_LEFT_MENU_LAYOUT,
|
||||
);
|
||||
}
|
||||
|
||||
function setScreenTimeout(value: ScreenTimeoutSetting): void {
|
||||
dashboardState.screenTimeout = saveScreenTimeoutSetting(value);
|
||||
dashboardState.lastInputAtMs = Date.now();
|
||||
}
|
||||
|
||||
function setWakeMode(value: WakeModeSetting): void {
|
||||
dashboardState.wakeMode = saveWakeModeSetting(value);
|
||||
}
|
||||
|
||||
function createDashboardSettingsMenuLayer(): MenuLayer {
|
||||
return new MenuLayer(
|
||||
"Dashboard",
|
||||
@@ -519,16 +563,11 @@ function createDashboardSettingsMenuLayer(): MenuLayer {
|
||||
ctx.stack.push(createSlotPickerMenu(slot));
|
||||
},
|
||||
render: ({ image, x, y }) => {
|
||||
image.drawText(dashboardFont, x, y + 4, `${slotMenuLabel(slot)}: ${pluginLabelForSlot(slot)}`, 200);
|
||||
image.drawText(dashboardFont, x, y + 3, `${slotMenuLabel(slot)}: ${pluginLabelForSlot(slot)}`, 200);
|
||||
},
|
||||
})),
|
||||
],
|
||||
{
|
||||
x: 16,
|
||||
y: 24,
|
||||
width: 252,
|
||||
height: 180,
|
||||
},
|
||||
TOP_LEFT_MENU_LAYOUT,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -543,19 +582,15 @@ function createSystemCardSettingsMenuLayer(): MenuLayer {
|
||||
ctx.stack.push(new EditSystemNameLayer());
|
||||
},
|
||||
render: ({ image, x, y }) => {
|
||||
image.drawText(dashboardFont, x, y + 4, `Name: ${getDisplayedSystemCardName()}`, 200);
|
||||
image.drawText(dashboardFont, x, y + 3, `Name: ${getDisplayedSystemCardName()}`, 200);
|
||||
},
|
||||
},
|
||||
createSystemCardToggleItem("Show Faceclaw Logo", "showFaceclawLogo"),
|
||||
createSystemCardToggleItem("Show Battery Indicators", "showBatteryIndicators"),
|
||||
createSystemCardToggleItem("Show Android Notifications", "showAndroidNotifications"),
|
||||
createSystemCardToggleItem("Show Signal Strength", "showSignalStrength"),
|
||||
],
|
||||
{
|
||||
x: 308,
|
||||
y: 24,
|
||||
width: 252,
|
||||
height: 180,
|
||||
},
|
||||
TOP_RIGHT_MENU_LAYOUT,
|
||||
true,
|
||||
);
|
||||
}
|
||||
@@ -567,25 +602,11 @@ function createSystemCardToggleItem(label: string, key: SystemCardSettingKey) {
|
||||
setDashboardSystemCardSetting(key, !dashboardState.systemCardSettings[key]);
|
||||
},
|
||||
render: ({ image, x, y, width, selected }: { image: GrayImage; x: number; y: number; width: number; selected: boolean }) => {
|
||||
drawToggleMenuItem(image, x, y, width, label, dashboardState.systemCardSettings[key], selected);
|
||||
drawToggleMenuItem(image, dashboardFont, x, y, width, label, dashboardState.systemCardSettings[key], selected);
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function drawToggleMenuItem(image: GrayImage, x: number, y: number, width: number, label: string, enabled: boolean, selected: boolean): void {
|
||||
const switchWidth = 34;
|
||||
const switchHeight = 16;
|
||||
const switchX = x + width - switchWidth - 2;
|
||||
const switchY = y + 2;
|
||||
image.drawText(dashboardFont, x, y + 4, label, 200);
|
||||
const offFill = selected ? 0 : 18;
|
||||
image.fillRoundedRect(switchX, switchY, switchWidth, switchHeight, enabled ? 70 : offFill, 8);
|
||||
image.drawRoundedRect(switchX, switchY, switchWidth, switchHeight, enabled ? 130 : 55, 8);
|
||||
const knobSize = 12;
|
||||
const knobX = enabled ? switchX + switchWidth - knobSize - 2 : switchX + 2;
|
||||
image.fillRoundedRect(knobX, switchY + 2, knobSize, knobSize, enabled ? 230 : selected ? 170 : 90, 6);
|
||||
}
|
||||
|
||||
function createIntegrationsMenuLayer(): MenuLayer {
|
||||
return new MenuLayer(
|
||||
"Integrations",
|
||||
@@ -597,12 +618,7 @@ function createIntegrationsMenuLayer(): MenuLayer {
|
||||
},
|
||||
},
|
||||
],
|
||||
{
|
||||
x: 16,
|
||||
y: 24,
|
||||
width: 252,
|
||||
height: 180,
|
||||
},
|
||||
TOP_LEFT_MENU_LAYOUT,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -617,7 +633,7 @@ function createNightscoutSettingsMenuLayer(): MenuLayer {
|
||||
ctx.stack.push(new EditTextSettingLayer("Edit Nightscout URL", () => dashboardState.nightscoutSettings.siteUrl || "(empty)"));
|
||||
},
|
||||
render: ({ image, x, y }) => {
|
||||
image.drawText(dashboardFont, x, y + 4, `URL: ${truncateSetting(dashboardState.nightscoutSettings.siteUrl)}`, 200);
|
||||
image.drawText(dashboardFont, x, y + 3, `URL: ${truncateSetting(dashboardState.nightscoutSettings.siteUrl)}`, 200);
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -627,16 +643,11 @@ function createNightscoutSettingsMenuLayer(): MenuLayer {
|
||||
ctx.stack.push(new EditTextSettingLayer("Edit API token", () => maskToken(dashboardState.nightscoutSettings.apiToken)));
|
||||
},
|
||||
render: ({ image, x, y }) => {
|
||||
image.drawText(dashboardFont, x, y + 4, `Token: ${maskToken(dashboardState.nightscoutSettings.apiToken)}`, 200);
|
||||
image.drawText(dashboardFont, x, y + 3, `Token: ${maskToken(dashboardState.nightscoutSettings.apiToken)}`, 200);
|
||||
},
|
||||
},
|
||||
],
|
||||
{
|
||||
x: 16,
|
||||
y: 24,
|
||||
width: 252,
|
||||
height: 180,
|
||||
},
|
||||
TOP_LEFT_MENU_LAYOUT,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -694,22 +705,14 @@ function createSlotPickerMenu(slot: DashboardSlotId): MenuLayer {
|
||||
onSelect: (ctx) => {
|
||||
dashboardState.slotConfig[slot] = plugin.id;
|
||||
saveDashboardSlotPlugin(slot, plugin.id);
|
||||
if (dashboardState.selectedCard === slot && isBlankDashboardPlugin(plugin.id)) {
|
||||
dashboardState.selectedCard = "system";
|
||||
}
|
||||
ctx.stack.pop();
|
||||
},
|
||||
render: ({ image, x, y }) => {
|
||||
const selected = dashboardState.slotConfig[slot] === plugin.id ? " *" : "";
|
||||
image.drawText(dashboardFont, x, y + 4, `${plugin.label}${selected}`, 200);
|
||||
image.drawText(dashboardFont, x, y + 3, `${plugin.label}${selected}`, 200);
|
||||
},
|
||||
})),
|
||||
{
|
||||
x: 16,
|
||||
y: 24,
|
||||
width: 252,
|
||||
height: 180,
|
||||
},
|
||||
TOP_LEFT_MENU_LAYOUT,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -729,9 +732,9 @@ class AboutLayer implements Layer {
|
||||
"A lobster on top of a smiley face.",
|
||||
"Perching on the faces of giants.",
|
||||
"",
|
||||
"System card opens menus.",
|
||||
"Swipe cycles dashboard cards.",
|
||||
"Plugin cards open fullscreen views.",
|
||||
"The menu opens dashboard cards.",
|
||||
"Settings configure card slots.",
|
||||
"Double-click backs out of menus.",
|
||||
];
|
||||
for (let index = 0; index < aboutLines.length; index++) {
|
||||
image.drawText(dashboardFont, 22, 128 + index * 14, aboutLines[index]!, 180);
|
||||
@@ -759,8 +762,6 @@ function getPluginState(): DashboardPluginState {
|
||||
function getCardBounds(card: DashboardCardId): DashboardPluginCardBounds {
|
||||
switch (card) {
|
||||
case "system":
|
||||
return { x: 0, y: 0, width: G2_LENS_WIDTH / 2 - 2, height: G2_LENS_HEIGHT / 2 - 2 };
|
||||
case "top-right":
|
||||
return {
|
||||
x: G2_LENS_WIDTH / 2 + 2,
|
||||
y: 0,
|
||||
@@ -784,24 +785,12 @@ function getCardBounds(card: DashboardCardId): DashboardPluginCardBounds {
|
||||
}
|
||||
}
|
||||
|
||||
function cycleSelection(direction: -1 | 1): void {
|
||||
const order: DashboardCardId[] = [
|
||||
"system",
|
||||
...getDashboardSlotIds().filter((slot) => !isBlankDashboardPlugin(dashboardState.slotConfig[slot])),
|
||||
];
|
||||
const index = order.indexOf(dashboardState.selectedCard);
|
||||
const currentIndex = index >= 0 ? index : 0;
|
||||
dashboardState.selectedCard = order[(currentIndex + direction + order.length) % order.length]!;
|
||||
}
|
||||
|
||||
function slotMenuLabel(slot: DashboardSlotId): string {
|
||||
switch (slot) {
|
||||
case "top-right":
|
||||
return "Top-right slot";
|
||||
case "bottom-left":
|
||||
return "Bottom-left slot";
|
||||
return "Bottom-left";
|
||||
case "bottom-right":
|
||||
return "Bottom-right slot";
|
||||
return "Bottom-right";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -820,4 +809,5 @@ function maskToken(token: string): string {
|
||||
}
|
||||
|
||||
const dashboardLayers = new LayerStack(new DashboardLayer(), dashboardFont, dashboardActions);
|
||||
dashboardLayers.push(createRootMenuLayer());
|
||||
|
||||
+49
-11
@@ -1,13 +1,14 @@
|
||||
import { GrayImage } from "../graphics/image";
|
||||
import { wrapText } from "../graphics/textwrap";
|
||||
import { type BdfFont } from "../graphics/bdffont";
|
||||
import { DashboardInputEvent, Layer, LayerContext, PaintBelow } from "./layers";
|
||||
|
||||
const DEFAULT_MENU_X = 48;
|
||||
const DEFAULT_MENU_Y = 24;
|
||||
const DEFAULT_MENU_WIDTH = 480;
|
||||
const DEFAULT_MENU_HEIGHT = 240;
|
||||
const MENU_TITLE_HEIGHT = 24;
|
||||
const MENU_ROW_HEIGHT = 28;
|
||||
const DEFAULT_MENU_X = 8;
|
||||
const DEFAULT_MENU_Y = 8;
|
||||
const DEFAULT_MENU_WIDTH = 272;
|
||||
const DEFAULT_MENU_HEIGHT = 128;
|
||||
const MENU_TITLE_HEIGHT = 20;
|
||||
const MENU_ROW_HEIGHT = 20;
|
||||
|
||||
export type MenuLayout = {
|
||||
x: number;
|
||||
@@ -33,6 +34,43 @@ export type MenuItem = {
|
||||
render?: (args: MenuItemRenderArgs) => void;
|
||||
};
|
||||
|
||||
export function drawToggleMenuItem(
|
||||
image: GrayImage,
|
||||
font: BdfFont,
|
||||
x: number,
|
||||
y: number,
|
||||
width: number,
|
||||
label: string,
|
||||
enabled: boolean,
|
||||
selected: boolean,
|
||||
): void {
|
||||
const switchWidth = 34;
|
||||
const switchHeight = 16;
|
||||
const switchX = x + width - switchWidth - 2;
|
||||
const switchY = y + 2;
|
||||
image.drawText(font, x, y + 3, label, 200);
|
||||
const offFill = selected ? 0 : 18;
|
||||
image.fillRoundedRect(switchX, switchY, switchWidth, switchHeight, enabled ? 70 : offFill, 8);
|
||||
image.drawRoundedRect(switchX, switchY, switchWidth, switchHeight, enabled ? 130 : 55, 8);
|
||||
const knobSize = 12;
|
||||
const knobX = enabled ? switchX + switchWidth - knobSize - 2 : switchX + 2;
|
||||
image.fillRoundedRect(knobX, switchY + 2, knobSize, knobSize, enabled ? 230 : selected ? 170 : 90, 6);
|
||||
}
|
||||
|
||||
export function drawRightValueMenuItem(
|
||||
image: GrayImage,
|
||||
font: BdfFont,
|
||||
x: number,
|
||||
y: number,
|
||||
width: number,
|
||||
label: string,
|
||||
value: string,
|
||||
): void {
|
||||
image.drawText(font, x, y + 3, label, 200);
|
||||
const valueX = x + width - font.measureText(value) - 2;
|
||||
image.drawText(font, valueX, y + 3, value, 220);
|
||||
}
|
||||
|
||||
export class MenuLayer implements Layer {
|
||||
private selectedIndex = 0;
|
||||
|
||||
@@ -55,14 +93,14 @@ export class MenuLayer implements Layer {
|
||||
image.drawRoundedRect(x, y, width, height, 72);
|
||||
image.drawText(ctx.font, x + 18, y + 10, this.title, 220);
|
||||
|
||||
const bodyY = y + MENU_TITLE_HEIGHT + 12;
|
||||
const bodyY = y + MENU_TITLE_HEIGHT + 8;
|
||||
for (let index = 0; index < this.items.length; index++) {
|
||||
const item = this.items[index]!;
|
||||
const y = bodyY + index * MENU_ROW_HEIGHT;
|
||||
const selected = index === this.selectedIndex;
|
||||
if (selected) {
|
||||
image.fillRoundedRect(x + 12, y - 2, width - 24, MENU_ROW_HEIGHT - 4, 18);
|
||||
image.drawRoundedRect(x + 12, y - 2, width - 24, MENU_ROW_HEIGHT - 4, 45);
|
||||
image.fillRoundedRect(x + 12, y - 1, width - 24, MENU_ROW_HEIGHT - 3, 18);
|
||||
image.drawRoundedRect(x + 12, y - 1, width - 24, MENU_ROW_HEIGHT - 3, 45);
|
||||
}
|
||||
if (item.render) {
|
||||
item.render({
|
||||
@@ -70,13 +108,13 @@ export class MenuLayer implements Layer {
|
||||
x: x + 22,
|
||||
y,
|
||||
width: width - 44,
|
||||
height: MENU_ROW_HEIGHT - 4,
|
||||
height: MENU_ROW_HEIGHT - 3,
|
||||
selected,
|
||||
text: item.label,
|
||||
ctx,
|
||||
});
|
||||
} else {
|
||||
image.drawText(ctx.font, x + 22, y + 4, item.label, selected ? 255 : 200);
|
||||
image.drawText(ctx.font, x + 22, y + 3, item.label, selected ? 255 : 200);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user