UI changes, system status icons

This commit is contained in:
James Babcock committed 2026-04-27 03:05:53 -07:00
1 parent 6e442e7faa
commit b5273715ae
14 files changed
+830 -236

No files matched your search

+97 -5
View File
@@ -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
View File
@@ -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
View File
@@ -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);
}
}