Update EvenHub compatibility layer to support new functionality (context menus and some other things) from 0.0.14

This commit is contained in:
James Babcock committed 2026-08-31 23:42:22 -07:00
1 parent 216dc59255
commit 052398d401
6 files changed
+279 -12

No files matched your search

+79 -1
View File
@@ -22,6 +22,8 @@ export type EvenHubTextContainer = {
/** Extended-layout: inherit content from a same-named container on replace. */
preserve: boolean;
content: string;
/** SDK 0.0.14 textColor: brightness level 0..4; undefined means the device default (4). */
textColor: number | undefined;
};
export type EvenHubImageContainer = {
@@ -63,11 +65,31 @@ export type EvenHubListContainer = {
export type EvenHubContainer = EvenHubTextContainer | EvenHubImageContainer | EvenHubListContainer;
/** One entry of the OS contextual menu an app registers via `menuObject`. */
export type EvenHubMenuItem = {
itemName: string;
/** Non-zero uint32, unique within the menu; echoed back in menuItemClickEvent. */
itemID: number;
};
export type EvenHubPage = {
/** In declaration order (lists, images, texts); z-sorted at paint time. */
containers: EvenHubContainer[];
/**
* The page's contextual-menu entries (SDK 0.0.14 `menuObject`). Empty when
* the app declared none, which on rebuild is how a menu is cleared.
*/
menuItems: EvenHubMenuItem[];
};
/** Firmware brightness levels for text; 4 is the device default. */
const MIN_TEXT_BRIGHTNESS = 0;
const MAX_TEXT_BRIGHTNESS = 4;
/** Firmware limits on a contextual menu, enforced app-side by the SDK too. */
const MENU_MAX_ITEMS = 10;
const MENU_NAME_MAX_BYTES = 32;
function normalizeKey(key: string): string {
return key.replace(/_/g, "").toLowerCase();
}
@@ -123,9 +145,62 @@ function parseTextContainer(json: Record<string, unknown>): EvenHubTextContainer
zOrderIndex: readOptionalNumber(json, "zOrderIndex"),
preserve: readNumber(json, "preserve", 0) !== 0,
content: readString(json, "content", ""),
textColor: readTextBrightness(json),
};
}
/**
* A text container's brightness level, or undefined for the device default.
* Out-of-range values are dropped rather than clamped: the SDK rejects the
* whole call for them, so an app that sends one is already misbehaving and
* the default level is the least surprising thing to draw.
*/
export function readTextBrightness(json: Record<string, unknown>): number | undefined {
const value = readOptionalNumber(json, "textColor");
if (value === undefined || !Number.isInteger(value)) return undefined;
if (value < MIN_TEXT_BRIGHTNESS || value > MAX_TEXT_BRIGHTNESS) return undefined;
return value;
}
/** The raw `menuObject.menuItems` array as sent, before any rule is applied. */
export function declaredMenuItems(data: Record<string, unknown>): unknown[] {
const raw = pickLoose(asRecord(pickLoose(data, "menuObject")), "menuItems");
return Array.isArray(raw) ? raw : [];
}
function utf8ByteLength(value: string): number {
let bytes = 0;
for (const char of value) {
const code = char.codePointAt(0) ?? 0;
bytes += code < 0x80 ? 1 : code < 0x800 ? 2 : code < 0x10000 ? 3 : 4;
}
return bytes;
}
/**
* Parse `menuObject.menuItems`. The SDK validates these app-side and refuses
* to call the host at all on a violation, so anything invalid arriving here
* came from an app bypassing the SDK; drop the offending entries (and any
* past the tenth) rather than rejecting the whole page, which would leave the
* app with no UI at all.
*/
export function parseMenuItems(data: Record<string, unknown>): EvenHubMenuItem[] {
const items: EvenHubMenuItem[] = [];
const seen = new Set<number>();
for (const entry of declaredMenuItems(data)) {
if (items.length >= MENU_MAX_ITEMS) break;
const json = asRecord(entry);
const itemID = readNumber(json, "itemID", 0);
if (!Number.isInteger(itemID) || itemID <= 0 || itemID > 0xffffffff) continue;
if (seen.has(itemID)) continue;
const itemName = readString(json, "itemName", "");
if (utf8ByteLength(itemName) > MENU_NAME_MAX_BYTES) continue;
seen.add(itemID);
items.push({ itemName, itemID });
}
return items;
}
function parseImageContainer(json: Record<string, unknown>): EvenHubImageContainer {
return {
kind: "image",
@@ -173,6 +248,9 @@ function parseListContainer(json: Record<string, unknown>): EvenHubListContainer
* page. Containers keep declaration order within and across the three arrays
* (lists, then images, then texts — texts last so info panels and capture
* containers sit above image tiles when no zOrderIndex is given).
*
* The whole page is replaced on rebuild, which is also what gives an omitted
* menuObject its documented meaning: the previous contextual menu goes away.
*/
export function parsePage(data: Record<string, unknown>): EvenHubPage {
const containers: EvenHubContainer[] = [];
@@ -188,7 +266,7 @@ export function parsePage(data: Record<string, unknown>): EvenHubPage {
if (Array.isArray(texts)) {
for (const item of texts) containers.push(parseTextContainer(asRecord(item)));
}
return { containers };
return { containers, menuItems: parseMenuItems(data) };
}
/** The container that owns gesture events (exactly one per valid page). */