EvenHub extensions: Fonts, window icons, touch-start events

This commit is contained in:
James Babcock committed 2026-10-07 01:22:42 -07:00
1 parent 4ee0af86cc
commit 84baee9c76
25 files changed
+1884 -127

No files matched your search

+9 -11
View File
@@ -4,7 +4,8 @@
*
* Text uses Even's extracted 20px firmware font (via EvenHubFont), measured
* and wrapped with @evenrealities/pretext, so glyph widths and line breaks
* match what apps expect.
* match what apps expect. Extended layouts may pick other fonts per container
* (fonts.ts), which wrap with Faceclaw's own text wrapping.
*
* Known deviations from stock, acceptable for now:
* - List rendering is a plain vertical list. A non-zero itemWidth sets the
@@ -13,7 +14,7 @@
* - Lists scroll with our menus' animation and bounce (see list-menu.ts).
*/
import { GrayImage } from "../../graphics/image";
import { EvenHubFont } from "../../graphics/evenhub-font";
import { containerFace } from "./fonts";
import {
type EvenHubContainer,
type EvenHubImageContainer,
@@ -47,14 +48,11 @@ function paintTextContainer(image: GrayImage, container: EvenHubTextContainer):
// the container's border at full brightness regardless.
paintBorder(image, container);
const inset = container.borderWidth + container.paddingLength;
EvenHubFont.get().drawTextWrapped(
image,
container.x + inset,
container.y + inset,
Math.max(1, container.width - 2 * inset),
container.content,
textGrey(container.textColor),
);
const face = containerFace(container.font);
const lines = face.wrap(container.content, Math.max(1, container.width - 2 * inset));
for (let i = 0; i < lines.length; i++) {
face.drawText(image, container.x + inset, container.y + inset + i * face.lineHeight, lines[i]!, textGrey(container.textColor));
}
}
function paintBorder(
@@ -94,7 +92,7 @@ function paintImageContainer(image: GrayImage, container: EvenHubImageContainer)
function paintListContainer(image: GrayImage, container: EvenHubListContainer, focused: boolean): void {
paintBorder(image, container);
paintListItems(image, container, EvenHubFont.get(), focused);
paintListItems(image, container, containerFace(container.font), focused);
}
/** Bottom-to-top paint order for a page's containers. */
+18 -4
View File
@@ -5,6 +5,7 @@
* (Container_ID), and numbers sometimes arrive as strings, so all field
* reads go through loose key matching (the SDK's pickLoose behavior).
*/
import { mergeFontSpecs, parseFontSpec, type EvenHubFontSpec } from "./font-spec";
export type EvenHubTextContainer = {
kind: "text";
@@ -24,6 +25,8 @@ export type EvenHubTextContainer = {
content: string;
/** SDK 0.0.14 textColor: brightness level 0..4; undefined means the device default (4). */
textColor: number | undefined;
/** Extended-layout font (merged with the layout's default); undefined is the stock font. */
font: EvenHubFontSpec | undefined;
};
export type EvenHubImageContainer = {
@@ -63,6 +66,8 @@ export type EvenHubListContainer = {
selectBorder: boolean;
/** Selection is host-local: scroll moves it with no app round-trip. */
selectedIndex: number;
/** Extended-layout font (merged with the layout's default); undefined is the stock font. */
font: EvenHubFontSpec | undefined;
};
export type EvenHubContainer = EvenHubTextContainer | EvenHubImageContainer | EvenHubListContainer;
@@ -149,7 +154,12 @@ function readBorderWidth(json: Record<string, unknown>): number {
return Math.max(0, Math.min(MAX_BORDER_WIDTH, readNumber(json, "borderWidth", 0)));
}
function parseTextContainer(json: Record<string, unknown>): EvenHubTextContainer {
/** A container's font: its own `font` over the layout-wide default. */
function readFont(json: Record<string, unknown>, layoutFont: EvenHubFontSpec | undefined): EvenHubFontSpec | undefined {
return mergeFontSpecs(layoutFont, parseFontSpec(pickLoose(json, "font")));
}
function parseTextContainer(json: Record<string, unknown>, layoutFont: EvenHubFontSpec | undefined): EvenHubTextContainer {
return {
kind: "text",
id: readNumber(json, "containerID", 0),
@@ -166,6 +176,7 @@ function parseTextContainer(json: Record<string, unknown>): EvenHubTextContainer
preserve: readFlag(json, "preserve"),
content: readString(json, "content", ""),
textColor: readTextBrightness(json),
font: readFont(json, layoutFont),
};
}
@@ -238,7 +249,7 @@ function parseImageContainer(json: Record<string, unknown>): EvenHubImageContain
};
}
function parseListContainer(json: Record<string, unknown>): EvenHubListContainer {
function parseListContainer(json: Record<string, unknown>, layoutFont: EvenHubFontSpec | undefined): EvenHubListContainer {
const itemContainer = asRecord(pickLoose(json, "itemContainer"));
const itemNamesRaw = pickLoose(itemContainer, "itemName");
const itemNames = Array.isArray(itemNamesRaw) ? itemNamesRaw.map(String) : [];
@@ -260,6 +271,7 @@ function parseListContainer(json: Record<string, unknown>): EvenHubListContainer
itemWidth: readNumber(itemContainer, "itemWidth", 0),
selectBorder: readFlag(itemContainer, "isItemSelectBorderEn"),
selectedIndex: 0,
font: readFont(json, layoutFont),
};
}
@@ -274,9 +286,11 @@ function parseListContainer(json: Record<string, unknown>): EvenHubListContainer
*/
export function parsePage(data: Record<string, unknown>): EvenHubPage {
const containers: EvenHubContainer[] = [];
// Extended layouts may set a default font for every text and list container.
const layoutFont = parseFontSpec(pickLoose(data, "font"));
const lists = pickLoose(data, "listObject");
if (Array.isArray(lists)) {
for (const item of lists) containers.push(parseListContainer(asRecord(item)));
for (const item of lists) containers.push(parseListContainer(asRecord(item), layoutFont));
}
const images = pickLoose(data, "imageObject");
if (Array.isArray(images)) {
@@ -284,7 +298,7 @@ export function parsePage(data: Record<string, unknown>): EvenHubPage {
}
const texts = pickLoose(data, "textObject");
if (Array.isArray(texts)) {
for (const item of texts) containers.push(parseTextContainer(asRecord(item)));
for (const item of texts) containers.push(parseTextContainer(asRecord(item), layoutFont));
}
return { containers, menuItems: parseMenuItems(data) };
}
+28
View File
@@ -0,0 +1,28 @@
/**
* The live data behind an EvenHub app's device status: glasses presence (fed
* by the connection controller), the headset battery (from the shell's top
* bar state), and the paired glasses' serial.
*/
import { shell } from "../../ui/shell/shell";
import { getGlassesPresence, onGlassesPresenceChanged } from "../../g2/glasses-presence";
import { loadPairedGlassesIdentity } from "../../g2/device-addresses";
import { type EvenHubDeviceStatusSource } from "./device-status";
export const liveDeviceStatusSource: EvenHubDeviceStatusSource = {
read: () => {
const presence = getGlassesPresence();
return {
serial: loadPairedGlassesIdentity()?.serial ?? null,
connected: presence.connected,
worn: presence.worn,
battery: shell.getBatteryLevels().headset,
charging: presence.charging,
};
},
subscribe: (listener) => {
const offs = [onGlassesPresenceChanged(listener), shell.onBatteryLevelsChanged(listener)];
return () => {
for (const off of offs) off();
};
},
};
+63
View File
@@ -0,0 +1,63 @@
/**
* The stock SDK's DeviceStatus (getGlassesInfo().status and the
* deviceStatusChanged push), built from what Faceclaw actually knows about
* the glasses. Pure, so it can be tested without NativeScript.
*/
/** Used when no glasses have been paired (preview-only mode). */
export const PLACEHOLDER_SERIAL = "FACECLAW-G2";
/** Wire shape of the SDK's DeviceStatus. Unknown fields are omitted rather than guessed. */
export type EvenHubDeviceStatus = {
sn: string;
connectType: "connected" | "disconnected";
isWearing?: boolean;
batteryLevel?: number;
isCharging?: boolean;
isInCase?: boolean;
};
export type EvenHubDeviceStatusInput = {
/** The paired glasses' serial number, or null when none are paired. */
serial: string | null;
connected: boolean;
/** null until the firmware reports a wear state this session. */
worn: boolean | null;
/** Last known headset battery percentage, or null if never reported. */
battery: number | null;
charging: boolean;
};
/** Where a session reads the device status from (live in the app; fixed in tests). */
export type EvenHubDeviceStatusSource = {
read(): EvenHubDeviceStatusInput;
/** Call `listener` after anything read() reports may have changed. Returns an unsubscribe function. */
subscribe(listener: () => void): () => void;
};
/** No glasses: what a session reports when it isn't given a live source. */
export const OFFLINE_DEVICE_STATUS_SOURCE: EvenHubDeviceStatusSource = {
read: () => ({ serial: null, connected: false, worn: null, battery: null, charging: false }),
subscribe: () => () => {},
};
export function buildEvenHubDeviceStatus(input: EvenHubDeviceStatusInput): EvenHubDeviceStatus {
const status: EvenHubDeviceStatus = {
// The SDK only applies a status push whose sn matches getGlassesInfo's,
// so both must come from here.
sn: input.serial?.trim() || PLACEHOLDER_SERIAL,
connectType: input.connected ? "connected" : "disconnected",
isCharging: input.charging,
// The G2 only charges in its case, and Faceclaw has no separate
// in-case report.
isInCase: input.charging,
};
if (input.worn !== null) status.isWearing = input.worn;
if (input.battery !== null) status.batteryLevel = input.battery;
return status;
}
export function sameDeviceStatus(a: EvenHubDeviceStatus, b: EvenHubDeviceStatus): boolean {
return a.sn === b.sn && a.connectType === b.connectType && a.isWearing === b.isWearing &&
a.batteryLevel === b.batteryLevel && a.isCharging === b.isCharging && a.isInCase === b.isInCase;
}
+22 -3
View File
@@ -5,7 +5,7 @@
*/
import { GrayImage } from "../../graphics/image";
import { EVENHUB_SCREEN_WIDTH } from "./compositor";
import { type InputEvent } from "../../ui/gestures";
import { directionalFallback, isDirectionalInput, type InputEvent } from "../../ui/gestures";
import { type Layer, type LayerContext } from "../../ui/layers";
import {
createInProcessWindow,
@@ -18,6 +18,13 @@ import { EvenHubSession } from "./session";
import { renderInstalledEvenHubIcon } from "./installed-apps";
class EvenHubAppLayer implements Layer {
/**
* Watch swipes become the usual stock gestures here rather than in the
* stack's fallback, which labels them as ring input: an app reading the
* extension's source field should see that they came from the watch.
*/
readonly acceptsDirectional = true;
constructor(private readonly session: EvenHubSession) {}
paint(ctx: LayerContext): GrayImage {
@@ -40,7 +47,7 @@ class EvenHubAppLayer implements Layer {
// menu intercepts tap-then-hold), which is the guaranteed way out since
// EvenHub apps own double-click. Tap-then-hold is still reported to the
// app as LONG_PRESS_EVENT — see the handleInput wrapper below.
this.session.handleGesture(event);
this.session.handleGesture(isDirectionalInput(event) ? ({ ...directionalFallback(event), source: "watch" } as InputEvent) : event);
}
}
@@ -50,6 +57,7 @@ export function createEvenHubWindow(
session: EvenHubSession,
options: InProcessAppOptions,
onShowPhone: () => void,
requestShellRender: () => void,
): InProcessWindow {
const fallbackIcon = windowIcon("package", session.manifest.name.charAt(0).toUpperCase() || "E");
const created = createInProcessWindow({
@@ -58,11 +66,14 @@ export function createEvenHubWindow(
title: session.manifest.name,
iconLetter: session.manifest.name.charAt(0).toUpperCase() || "E",
closeable: true,
// An icon the app set at runtime (extension setWindowIcon) wins over the
// package's.
drawIcon: makeImageWindowIcon(
(size) => renderInstalledEvenHubIcon(session.manifest.packageId, size),
(size) => session.renderWindowIcon(size) ?? renderInstalledEvenHubIcon(session.manifest.packageId, size),
fallbackIcon,
),
heightMode: "medium",
keepsScreenOn: () => session.keepsScreenOn(),
// The window's context menu doubles as the OS contextual menu EvenHub
// apps register with `menuObject` (SDK 0.0.14): the app's own entries come
// first, then the host entries. Evaluated at open time, so a rebuild that
@@ -99,6 +110,14 @@ export function createEvenHubWindow(
// The extended layout uses the full 576x452 app area ("max"); stock apps
// stay in the 576x288 band ("medium").
setTallCanvas: (tall) => created.setHeightMode(tall ? "max" : "medium"),
setAttention: (attention) => shell.setWindowAttention(windowId, attention),
// Only while the app listens for text: otherwise the shell's Type Into App
// destinations (voice dialog, phone keyboard, watch) would send into a void.
setAcceptsTextInput: (accept) => {
created.window.receiveTextInput = accept ? (text, textOptions) => session.deliverTextInput(text, textOptions) : undefined;
},
startVoiceInput: () => shell.foregroundWindow()?.windowId === windowId && shell.startVoiceInput(),
iconChanged: requestShellRender,
windowId,
});
+171
View File
@@ -0,0 +1,171 @@
/**
* Font options for EvenHub text and list containers (the extension layout's
* `font: { family, size, weight }`): parsing, inheritance from the layout's
* default, and choosing a face from what is installed. Pure, so it can be
* tested without NativeScript; app/apps/evenhub/fonts.ts loads the result.
*
* A container with no font anywhere keeps the stock firmware font, exactly as
* on stock. Naming any font option switches to Faceclaw's fonts, with the
* defaults below filling the options left out.
*/
/** A container's font request, after parsing. Absent fields take the defaults. */
export type EvenHubFontSpec = {
family?: string;
size?: number;
weight?: number;
};
export const DEFAULT_FONT_FAMILY = "Roboto";
/** The stock firmware font's size, so switching family alone keeps text about the same size. */
export const DEFAULT_FONT_SIZE = 20;
export const DEFAULT_FONT_WEIGHT = 400;
export const MIN_FONT_SIZE = 6;
/** Larger glyphs leave the glyph-cache path (255px limit), so text stops being cheap to send. */
export const MAX_FONT_SIZE = 128;
/**
* Parse a `font` object from a layout or container. Undefined when there is
* none; unusable fields are dropped, so `{}` means "Faceclaw's default font".
*/
export function parseFontSpec(raw: unknown): EvenHubFontSpec | undefined {
if (!raw || typeof raw !== "object" || Array.isArray(raw)) return undefined;
const record = raw as Record<string, unknown>;
const spec: EvenHubFontSpec = {};
if (typeof record.family === "string" && record.family.trim()) spec.family = record.family.trim();
const size = typeof record.size === "string" ? Number(record.size) : record.size;
if (typeof size === "number" && Number.isFinite(size)) {
spec.size = Math.min(MAX_FONT_SIZE, Math.max(MIN_FONT_SIZE, Math.round(size)));
}
const weight = parseWeight(record.weight);
if (weight !== undefined) spec.weight = weight;
return spec;
}
function parseWeight(raw: unknown): number | undefined {
if (typeof raw === "string") {
const keyword = raw.trim().toLowerCase();
if (keyword === "normal") return 400;
if (keyword === "bold") return 700;
raw = Number(keyword);
}
if (typeof raw !== "number" || !Number.isFinite(raw)) return undefined;
return Math.min(1000, Math.max(1, Math.round(raw)));
}
/** A container's font: its own options over the layout default's, field by field. */
export function mergeFontSpecs(
layoutDefault: EvenHubFontSpec | undefined,
own: EvenHubFontSpec | undefined,
): EvenHubFontSpec | undefined {
if (!layoutDefault) return own;
if (!own) return layoutDefault;
return { ...layoutDefault, ...own };
}
/** One loadable face: a TTF file, a Terminus weight, or the stock font. */
export type FontFaceCandidate = {
/** Family as the font names itself (e.g. "Inter 18pt"). */
family: string;
/** 100..900. */
weight: number;
/** The sizes a bitmap face comes in; null for a scalable face. */
sizes: readonly number[] | null;
monospace: boolean;
/** Identifies the face to the loader. */
key: string;
};
/**
* The family name apps use: an optical-size suffix ("Inter 18pt") is dropped,
* since the bundled Inter is its 18pt cut and apps would ask for "Inter".
*/
export function displayFamily(family: string): string {
return family.replace(/\s+\d+\s*pt$/i, "").trim() || family;
}
function normalizeFamily(name: string): string {
return name.toLowerCase().replace(/[^a-z0-9]/g, "");
}
export function familyMatches(candidate: string, requested: string): boolean {
const wanted = normalizeFamily(requested);
return normalizeFamily(candidate) === wanted || normalizeFamily(displayFamily(candidate)) === wanted;
}
/**
* Pick the face and pixel size for a spec. An unknown family falls back to
* DEFAULT_FONT_FAMILY; null when even that is missing (the caller then uses
* the stock font). Weight follows CSS's font matching: 400–500 look up to
* 500 first, lighter requests look lighter first, heavier ones heavier
* first. A bitmap face takes its nearest size (the smaller on a tie).
*/
export function selectFontFace(
spec: EvenHubFontSpec,
candidates: readonly FontFaceCandidate[],
): { face: FontFaceCandidate; size: number } | null {
const family = spec.family ?? DEFAULT_FONT_FAMILY;
let faces = candidates.filter((candidate) => familyMatches(candidate.family, family));
if (faces.length === 0) faces = candidates.filter((candidate) => familyMatches(candidate.family, DEFAULT_FONT_FAMILY));
if (faces.length === 0) return null;
const desired = spec.weight ?? DEFAULT_FONT_WEIGHT;
const face = [...faces].sort((a, b) => weightRank(a.weight, desired) - weightRank(b.weight, desired))[0]!;
const requestedSize = spec.size ?? DEFAULT_FONT_SIZE;
return { face, size: face.sizes ? nearestSize(face.sizes, requestedSize) : requestedSize };
}
/** Lower is better: CSS font-weight matching as a sort key. */
function weightRank(weight: number, desired: number): number {
const distance = Math.abs(weight - desired);
if (weight === desired) return 0;
if (desired >= 400 && desired <= 500) {
if (weight > desired && weight <= 500) return distance;
if (weight < desired) return 1000 + distance;
return 2000 + distance;
}
const preferLighter = desired < 400;
return (weight < desired) === preferLighter ? distance : 1000 + distance;
}
function nearestSize(sizes: readonly number[], requested: number): number {
let best = sizes[0]!;
for (const size of sizes) {
const better = Math.abs(size - requested) < Math.abs(best - requested) ||
(Math.abs(size - requested) === Math.abs(best - requested) && size < best);
if (better) best = size;
}
return best;
}
/** A family as the extension's getFonts() reports it. */
export type FontFamilyInfo = {
family: string;
weights: number[];
/** Bitmap families' sizes; absent for scalable families. */
sizes?: number[];
monospace: boolean;
};
/** Group faces into families for getFonts(), sorted by name. */
export function describeFontFamilies(candidates: readonly FontFaceCandidate[]): FontFamilyInfo[] {
const families = new Map<string, FontFamilyInfo>();
for (const candidate of candidates) {
const name = displayFamily(candidate.family);
const key = normalizeFamily(name);
let info = families.get(key);
if (!info) {
info = { family: name, weights: [], monospace: candidate.monospace };
families.set(key, info);
}
if (!info.weights.includes(candidate.weight)) info.weights.push(candidate.weight);
if (candidate.sizes) info.sizes = Array.from(new Set([...(info.sizes ?? []), ...candidate.sizes]));
info.monospace = info.monospace && candidate.monospace;
}
return Array.from(families.values())
.map((info) => ({
...info,
weights: [...info.weights].sort((a, b) => a - b),
...(info.sizes ? { sizes: [...info.sizes].sort((a, b) => a - b) } : {}),
}))
.sort((a, b) => a.family.localeCompare(b.family));
}
+139
View File
@@ -0,0 +1,139 @@
/**
* Loads the fonts EvenHub text and list containers ask for (see font-spec.ts
* for the matching rules): the stock firmware font, Terminus at its bitmap
* sizes, and every installed TTF/OTF — the bundled Roboto, Inter, Montserrat
* and Roboto Mono, plus any the user installed from the Files app.
*/
import { EvenHubFont } from "../../graphics/evenhub-font";
import { getTerminusFont, TERMINUS_SIZES } from "../../graphics/bdffont";
import { TtfFont } from "../../graphics/ttf-font";
import { listInstalledFonts } from "../../graphics/installed-fonts";
import { wrapText } from "../../graphics/textwrap";
import type { GrayImage, UiFont } from "../../graphics/image";
import {
describeFontFamilies,
familyMatches,
selectFontFace,
type EvenHubFontSpec,
type FontFaceCandidate,
type FontFamilyInfo,
} from "./font-spec";
/** What containers draw text with: the stock font's interface, which UiFonts are adapted to. */
export type EvenHubTextFace = {
readonly lineHeight: number;
measureLine(text: string): number;
wrap(text: string, maxWidth: number): string[];
drawText(image: GrayImage, x: number, y: number, text: string, value?: number): void;
};
class UiFontFace implements EvenHubTextFace {
constructor(private readonly font: UiFont) {}
get lineHeight(): number {
return this.font.lineHeight;
}
measureLine(text: string): number {
return this.font.measureText(text);
}
wrap(text: string, maxWidth: number): string[] {
return wrapText(this.font, text, maxWidth);
}
drawText(image: GrayImage, x: number, y: number, text: string, value = 255): void {
this.font.drawText(image, x, y, text, value);
}
}
const STOCK_KEY = "stock";
const TERMINUS_KEY_PREFIX = "terminus:";
const TTF_KEY_PREFIX = "ttf:";
const BUILT_IN_FACES: readonly FontFaceCandidate[] = [
// The firmware font: one size, one weight.
{ family: "EvenHub", weight: 400, sizes: [20], monospace: false, key: STOCK_KEY },
{ family: "Terminus", weight: 400, sizes: TERMINUS_SIZES, monospace: true, key: `${TERMINUS_KEY_PREFIX}400` },
{ family: "Terminus", weight: 700, sizes: TERMINUS_SIZES, monospace: true, key: `${TERMINUS_KEY_PREFIX}700` },
];
/** A family the listing lacks is re-listed at most this often (the user may have just installed it). */
const RELIST_INTERVAL_MS = 30_000;
let faces: FontFaceCandidate[] | null = null;
let listedAtMs = 0;
function listFaces(): FontFaceCandidate[] {
const installed = listInstalledFonts()
// Upright faces only: there is no italic option yet.
.filter((font) => font.weightOrder < 1000)
.map((font): FontFaceCandidate => ({
family: font.family,
weight: font.weightOrder,
sizes: null,
monospace: font.monospace,
key: `${TTF_KEY_PREFIX}${font.path}`,
}));
faces = [...BUILT_IN_FACES, ...installed];
listedAtMs = Date.now();
return faces;
}
/** Whether some listed face has the family (no family asks for the default, which is bundled). */
function hasFamily(available: readonly FontFaceCandidate[], family: string | undefined): boolean {
return !family || available.some((face) => familyMatches(face.family, family));
}
/** Resolved faces by spec; null means the stock font (asked for, or nothing loadable). */
const resolved = new Map<string, EvenHubTextFace | null>();
/** The face for a spec, and whether it is in the family asked for (rather than the fallback). */
function loadFace(spec: EvenHubFontSpec): { face: EvenHubTextFace | null; exact: boolean } {
let available = faces ?? listFaces();
if (!hasFamily(available, spec.family) && Date.now() - listedAtMs > RELIST_INTERVAL_MS) available = listFaces();
const exact = hasFamily(available, spec.family);
return { face: openFace(selectFontFace(spec, available)), exact };
}
function openFace(choice: { face: FontFaceCandidate; size: number } | null): EvenHubTextFace | null {
if (!choice || choice.face.key === STOCK_KEY) return null;
const { face, size } = choice;
if (face.key.startsWith(TERMINUS_KEY_PREFIX)) {
const font = getTerminusFont(size, face.weight >= 600);
return font ? new UiFontFace(font) : null;
}
const font = TtfFont.load(face.key.slice(TTF_KEY_PREFIX.length), size);
return font ? new UiFontFace(font) : null;
}
/**
* The face a container draws with: the stock font when it names no font,
* otherwise the closest match to its spec.
*/
export function containerFace(spec: EvenHubFontSpec | undefined): EvenHubTextFace {
if (!spec) return EvenHubFont.get();
const key = `${spec.family ?? ""}|${spec.size ?? ""}|${spec.weight ?? ""}`;
let face = resolved.get(key);
if (face === undefined) {
const loaded = loadFace(spec);
face = loaded.face;
// A fallback isn't kept, so a family the user installs later gets picked up.
if (loaded.exact) resolved.set(key, face);
}
// Not cached as an object: EvenHubFont.get() changes once firmware fonts are extracted.
return face ?? EvenHubFont.get();
}
/** The font families apps can ask for (extension getFonts). Re-lists installed fonts. */
export function availableFontFamilies(): FontFamilyInfo[] {
return describeFontFamilies(listFaces());
}
/** Width of the widest line of `text` and the line height, in the font a container would use. */
export function measureContainerText(text: string, spec: EvenHubFontSpec | undefined): { width: number; lineHeight: number } {
const face = containerFace(spec);
let width = 0;
for (const line of text.split("\n")) width = Math.max(width, face.measureLine(line));
return { width: Math.ceil(width), lineHeight: face.lineHeight };
}
+3 -2
View File
@@ -22,6 +22,7 @@ import { unpackRuntime } from "./unpack-runtime";
import { type EvenHubPermission } from "./permissions";
import { EvenHubSession } from "./session";
import { createEvenHubWindow } from "./evenhub-window";
import { liveDeviceStatusSource } from "./device-status-source";
import { createEvenHubWebView, type EvenHubWebView } from "./webview";
import { shell } from "../../ui/shell/shell";
import { GrayImage } from "../../graphics/image";
@@ -126,7 +127,7 @@ async function startApp(
): Promise<void> {
const windowId = `evenhub:app:${nextSerial++}`;
const session = new EvenHubSession(manifest, distDir, ctx.appendLog, remoteUrl,
payload => ctx.actions.playBuzzerSequence(payload));
payload => ctx.actions.playBuzzerSequence(payload), liveDeviceStatusSource);
const webView = createEvenHubWebView(session);
session.attachWebView({
evaluateJs: webView.evaluateJs,
@@ -147,7 +148,7 @@ async function startApp(
try {
await ctx.launchInProcessApp(windowId, `window:${windowId}`, (options) =>
createEvenHubWindow(windowId, appId, session, options, () => showOnPhone(windowId)),
createEvenHubWindow(windowId, appId, session, options, () => showOnPhone(windowId), () => ctx.requestShellRender()),
);
} catch (error) { session.close(); throw error; }
}
+207 -48
View File
@@ -63,6 +63,16 @@ import { toolRegistry, type ToolResult, type ToolSpec } from "../../assistant/to
import { getCurrentLocation } from "../../native/location";
import { LocationTracker, type TrackedLocation } from "../../native/location-tracker";
import { ensureFineLocationPermission } from "../../native/location-permissions";
import {
buildEvenHubDeviceStatus,
OFFLINE_DEVICE_STATUS_SOURCE,
sameDeviceStatus,
type EvenHubDeviceStatus,
type EvenHubDeviceStatusSource,
} from "./device-status";
import { parseWindowIcon, WindowIconRenderer } from "./window-icon";
import { availableFontFamilies, containerFace, measureContainerText } from "./fonts";
import { parseFontSpec } from "./font-spec";
const UPNG = require("upng-js");
@@ -284,19 +294,29 @@ export function buildFaceclawExtensionsScript(versionString: string): string {
if (i >= 0) arr.splice(i, 1);
};
}
// Compass: enable on first listener, disable when the last one leaves.
var compassListeners = [];
function addCompass(cb) {
compassListeners.push(cb);
if (compassListeners.length === 1) send("setCompass", [true]);
var off = on("compass", cb);
return function () {
off();
var i = compassListeners.indexOf(cb);
if (i >= 0) compassListeners.splice(i, 1);
if (compassListeners.length === 0) send("setCompass", [false]);
// Events the host only produces on request (compass, touch-down, text
// input): it is told to start on the first listener and to stop when the
// last one leaves. Each unsubscribe function works once.
function counted(name, setMethod) {
var count = 0;
return function (cb) {
var off = on(name, cb);
if (++count === 1) send(setMethod, [true]);
var active = true;
return function () {
if (!active) return;
active = false;
off();
if (--count === 0) send(setMethod, [false]);
};
};
}
function setWindowIcon(icon) {
if (!icon) return call("setWindowIcon", [null]);
// Typed arrays (ImageData.data) don't survive JSON; send a plain array.
var data = Array.prototype.slice.call(icon.data || []);
return call("setWindowIcon", [{ width: icon.width, height: icon.height, data: data }]);
}
// Voice-assistant tools: specs go to the host; handlers stay here and run when
// the host invokes a tool via __fcExtInvokeTool.
var toolHandlers = {};
@@ -335,10 +355,18 @@ export function buildFaceclawExtensionsScript(versionString: string): string {
getConfiguredApiKeys: function () { return call("getConfiguredApiKeys"); },
requestApiKeyAccess: function (services) { return call("requestApiKeyAccess", [services || []]); },
playBuzzer: function (steps) { return call("playBuzzer", [steps || []]); },
addCompassListener: addCompass,
addCompassListener: counted("compass", "setCompass"),
createLayout: function (layout) { return call("createLayout", [layout || {}]); },
replaceLayout: function (layout) { return call("replaceLayout", [layout || {}]); },
setAssistantTools: setAssistantTools
setAssistantTools: setAssistantTools,
addTouchDownListener: counted("touchDown", "setTouchDown"),
addTextInputListener: counted("textInput", "setTextInput"),
startVoiceInput: function () { return call("startVoiceInput"); },
setWindowIcon: setWindowIcon,
setAttention: function (attention) { send("setAttention", [!!attention]); },
setKeepScreenOn: function (keepOn) { send("setKeepScreenOn", [!!keepOn]); },
getFonts: function () { return call("getFonts"); },
measureText: function (text, font) { return call("measureText", [String(text), font || null]); }
};
window.getFaceclawExtensions = function () { return extensions; };
})();
@@ -364,6 +392,14 @@ export type EvenHubWindowHooks = {
pushOverlay: (layer: Layer) => void;
/** Switch the window between the stock 576x288 band and the full 576x452 canvas. */
setTallCanvas: (tall: boolean) => void;
/** Show or clear the attention dot on the window's switcher icon. */
setAttention: (attention: boolean) => void;
/** Offer (or stop offering) the window as the shell's "Type Into App" destination. */
setAcceptsTextInput: (accept: boolean) => void;
/** Open the shell's voice dialog aimed at the window; false if it can't open now. */
startVoiceInput: () => boolean;
/** The app changed its window icon: repaint the switcher. */
iconChanged: () => void;
/** The shell window id (unique per running instance; used for tool registration). */
windowId: string;
};
@@ -414,6 +450,21 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH
private shellFocused = true;
/** API-key services the user has granted this app this session. */
private readonly grantedApiKeys = new Set<string>();
/** The app listens for touch-down (extension addTouchDownListener). */
private touchDownRequested = false;
/** The app listens for text input, so the shell offers it as a Type Into App destination. */
private textInputRequested = false;
/** The app asked for the attention dot on its switcher icon. */
private attention = false;
/** The app asked the idle screen timeout to wait while it is in the foreground. */
private keepScreenOnRequested = false;
/** The app's own switcher icon; null draws the package icon. */
private windowIcon: WindowIconRenderer | null = null;
/** The paired glasses' serial, read once so getGlassesInfo and every status push agree. */
private glassesSerial: string | null | undefined = undefined;
/** The last deviceStatusChanged sent, and the subscriptions that refresh it. */
private lastDeviceStatus: EvenHubDeviceStatus | null = null;
private readonly deviceStatusOffs: (() => void)[] = [];
private log: (message: string) => void;
constructor(
@@ -422,6 +473,7 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH
log: (message: string) => void,
remoteUrl = "",
private readonly sendBuzzerSequence: (payload: Uint8Array) => Promise<void> | void = () => {},
private readonly deviceStatusSource: EvenHubDeviceStatusSource = OFFLINE_DEVICE_STATUS_SOURCE,
) {
this.manifest = manifest;
this.distDir = distDir;
@@ -447,14 +499,8 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH
// Launched to drive the glasses (from the on-glasses file browser), so
// apps that branch on this should take their glasses path.
this.pushMessage("evenAppLaunchSource", { launchSource: "glassesMenu" });
this.pushMessage("deviceStatusChanged", {
sn: "FACECLAW-G2",
connectType: "connected",
isWearing: true,
batteryLevel: 100,
isCharging: false,
isInCase: false,
});
this.pushDeviceStatus();
this.deviceStatusOffs.push(this.deviceStatusSource.subscribe(() => this.pushDeviceStatus()));
}
// ----- glasses window wiring -----
@@ -462,6 +508,10 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH
attachWindow(hooks: EvenHubWindowHooks): void {
this.windowHooks = hooks;
this.shellWindowId = hooks.windowId;
// Requests the app made before the window existed.
if (this.textInputRequested) hooks.setAcceptsTextInput(true);
if (this.attention) hooks.setAttention(true);
if (this.windowIcon) hooks.iconChanged();
}
windowClosed(): void {
@@ -491,18 +541,29 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH
* locally with events only for clicks and boundary scrolls.
*/
handleGesture(event: InputEvent): void {
// Touch-down has no stock event (an unknown eventType would parse as a
// click), so it goes only to apps that subscribed through the extension.
if (event.type === "ring-press") {
if (this.touchDownRequested) {
this.pushExtEvent("touchDown", { source: extensionInputSource(event.source), timestampMs: event.timestampMs });
}
return;
}
if (!this.page) return;
const capture = this.page ? eventCaptureContainer(this.page) : undefined;
const capture = eventCaptureContainer(this.page);
// The stock eventSource can't tell the watch from the ring, and scrolls
// carry none; the extension field names the device when it is known.
const inputSource = "source" in event && event.source ? extensionInputSource(event.source) : undefined;
switch (event.type) {
case "click":
if (capture?.kind === "list") {
this.emitListEvent(capture, CLICK_EVENT);
this.emitListEvent(capture, CLICK_EVENT, inputSource);
} else {
this.emitSysEvent(CLICK_EVENT, gestureSource(event.source));
this.emitSysEvent(CLICK_EVENT, gestureSource(event.source), inputSource);
}
break;
case "double-click":
this.emitSysEvent(DOUBLE_CLICK_EVENT, gestureSource(event.source));
this.emitSysEvent(DOUBLE_CLICK_EVENT, gestureSource(event.source), inputSource);
break;
case "long-press":
case "short-then-long-press":
@@ -516,9 +577,9 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH
// given exclusive use of the gesture.
const eventType = event.type === "long-press-release" ? LONG_PRESS_RELEASE_EVENT : LONG_PRESS_EVENT;
if (capture?.kind === "list") {
this.emitListEvent(capture, eventType);
this.emitListEvent(capture, eventType, inputSource);
} else {
this.emitSysEvent(eventType, gestureSource(event.source));
this.emitSysEvent(eventType, gestureSource(event.source), inputSource);
}
break;
}
@@ -526,9 +587,9 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH
case "scroll-down": {
const eventType = event.type === "scroll-up" ? SCROLL_TOP_EVENT : SCROLL_BOTTOM_EVENT;
if (capture?.kind === "list") {
this.scrollList(capture, eventType);
this.scrollList(capture, eventType, inputSource);
} else {
this.emitSysEvent(eventType, 0);
this.emitSysEvent(eventType, 0, inputSource);
}
break;
}
@@ -551,11 +612,11 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH
this.pushMessage("evenHubEvent", { type: "menuItemClickEvent", jsonData: { itemID } });
}
private scrollList(list: EvenHubListContainer, eventType: number): void {
private scrollList(list: EvenHubListContainer, eventType: number, inputSource?: ExtensionInputSource): void {
// Swipe up selects the previous item, swipe down the next (stock direction).
const delta = eventType === SCROLL_TOP_EVENT ? -1 : 1;
// At a boundary the selection stays put (the list bounces) and the app hears about it.
if (!stepListSelection(list, EvenHubFont.get(), delta)) this.emitListEvent(list, eventType);
if (!stepListSelection(list, containerFace(list.font), delta)) this.emitListEvent(list, eventType, inputSource);
this.windowHooks?.requestRender();
}
@@ -820,19 +881,10 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH
return ApplicationSettings.getString(this.storageKey(readString(data, "key", "")), "");
case "getUserInfo":
return { uid: 0, name: "Faceclaw", avatar: "", country: "" };
case "getGlassesInfo":
return {
model: "g2",
sn: "FACECLAW-G2",
status: {
sn: "FACECLAW-G2",
connectType: "connected",
isWearing: true,
batteryLevel: 100,
isCharging: false,
isInCase: false,
},
};
case "getGlassesInfo": {
const status = this.deviceStatus();
return { model: "g2", sn: status.sn, status };
}
case "audioControl":
return this.audioControl(data);
case "getAppLocation":
@@ -1001,17 +1053,18 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH
this.pushMessage("evenHubEvent", { type: payloadType, jsonData: elideZeroFields(jsonData) });
}
private emitSysEvent(eventType: number, eventSource: number): void {
this.pushEvenHubEvent("sysEvent", { eventType, eventSource });
private emitSysEvent(eventType: number, eventSource: number, inputSource?: ExtensionInputSource): void {
this.pushEvenHubEvent("sysEvent", { eventType, eventSource, [INPUT_SOURCE_FIELD]: inputSource });
}
private emitListEvent(list: EvenHubListContainer, eventType: number): void {
private emitListEvent(list: EvenHubListContainer, eventType: number, inputSource?: ExtensionInputSource): void {
this.pushEvenHubEvent("listEvent", {
containerID: list.id,
containerName: list.name,
currentSelectItemIndex: list.selectedIndex,
currentSelectItemName: list.itemNames[list.selectedIndex] ?? "",
eventType,
[INPUT_SOURCE_FIELD]: inputSource,
});
}
@@ -1027,7 +1080,7 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH
result = await this.dispatchExtension(method, args.slice(1));
} catch (error) {
ok = false;
result = String(error);
result = error instanceof Error ? error.message : String(error);
this.log(`evenhub: extension call failed: ${error}`);
}
// id 0 is a fire-and-forget send; nothing is awaiting it.
@@ -1060,12 +1113,96 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH
case "setAssistantTools":
this.setAssistantTools(Array.isArray(params[0]) ? params[0] : []);
return null;
case "setTouchDown":
this.touchDownRequested = params[0] === true;
return null;
case "setTextInput":
this.setTextInputRequested(params[0] === true);
return null;
case "startVoiceInput":
// Without a text listener the dialog would have nowhere in the app to send to.
return this.textInputRequested && (this.windowHooks?.startVoiceInput() ?? false);
case "setWindowIcon":
this.setWindowIcon(params[0]);
return null;
case "setAttention":
this.attention = params[0] === true;
this.windowHooks?.setAttention(this.attention);
return null;
case "setKeepScreenOn":
this.keepScreenOnRequested = params[0] === true;
return null;
case "getFonts":
return availableFontFamilies();
case "measureText":
// No font measures in the stock font, as a container without one draws.
return measureContainerText(String(params[0] ?? ""), parseFontSpec(params[1]));
default:
this.log(`evenhub: unknown extension method ${method}`);
return null;
}
}
// ----- device status (stock getGlassesInfo / deviceStatusChanged) -----
private deviceStatus(): EvenHubDeviceStatus {
const input = this.deviceStatusSource.read();
if (this.glassesSerial === undefined) this.glassesSerial = input.serial;
return buildEvenHubDeviceStatus({ ...input, serial: this.glassesSerial });
}
/** Push deviceStatusChanged if the status differs from the last one sent. */
private pushDeviceStatus(): void {
if (this.closed) return;
const status = this.deviceStatus();
if (this.lastDeviceStatus && sameDeviceStatus(status, this.lastDeviceStatus)) return;
this.lastDeviceStatus = status;
this.pushMessage("deviceStatusChanged", status);
}
// ----- window controls and text input (extensions) -----
/** Whether the idle screen timeout should wait while this window is in the foreground. */
keepsScreenOn(): boolean {
return this.keepScreenOnRequested;
}
/** The app's own switcher icon at `size`, or null to draw the package icon. */
renderWindowIcon(size: number): GrayImage | null {
return this.windowIcon?.render(size) ?? null;
}
private setWindowIcon(raw: unknown): void {
if (raw == null) {
this.windowIcon = null;
} else {
const image = parseWindowIcon(raw);
if (!image) {
throw new Error(
"setWindowIcon: expected {width, height, data} of at most 256x256, with data holding " +
"width*height grayscale or width*height*4 RGBA bytes",
);
}
this.windowIcon = new WindowIconRenderer(image);
}
this.windowHooks?.iconChanged();
}
private setTextInputRequested(accept: boolean): void {
if (this.textInputRequested === accept) return;
this.textInputRequested = accept;
this.windowHooks?.setAcceptsTextInput(accept);
}
/**
* Text the shell routed to this window: the voice dialog's Type Into App,
* the phone keyboard, the watch, or remote input. Only arrives while the
* app listens (setAcceptsTextInput).
*/
deliverTextInput(text: string, options?: { submit?: boolean }): void {
this.pushExtEvent("textInput", options?.submit === undefined ? { text } : { text, submit: options.submit });
}
// ----- compass (EvenHubCompassClient) -----
deliverCompass(reading: EvenHubCompassReading): void {
@@ -1249,6 +1386,7 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH
this.pendingToolCalls.clear();
this.locationTracker?.stop();
this.locationTracker = null;
for (const off of this.deviceStatusOffs.splice(0)) off();
if (!this.systemExitSent) {
// Best effort: lets the app's teardown handlers run before the webview dies.
this.emitSysEvent(SYSTEM_EXIT_EVENT, 0);
@@ -1451,6 +1589,27 @@ function luminance(r: number, g: number, b: number): number {
return Math.round(0.2126 * r + 0.7152 * g + 0.0722 * b);
}
/**
* Input-device names in the extension API: the touchDown event's source, and
* the `faceclawInputSource` field added to stock gesture events.
*/
type ExtensionInputSource = "ring" | "left-touchpad" | "right-touchpad" | "watch";
const INPUT_SOURCE_FIELD = "faceclawInputSource";
function extensionInputSource(source: InputSource): ExtensionInputSource {
switch (source) {
case "ring":
return "ring";
case "left-arm":
return "left-touchpad";
case "right-arm":
return "right-touchpad";
case "watch":
return "watch";
}
}
/**
* ring/left-arm/right-arm -> PB EventSourceType. The watch is reported to
* EvenHub apps as the ring: they only know stock hardware, and the watch's
+119
View File
@@ -0,0 +1,119 @@
/**
* App-supplied window icons (the extension setWindowIcon): parse the pixel
* buffer an app sends and scale it to the sizes the app switcher draws at.
* Pure, so it can be tested without NativeScript.
*/
import { GrayImage } from "../../graphics/image";
/** Larger icons are refused: they only cost bridge traffic, since switcher icons are ~32px. */
export const MAX_WINDOW_ICON_DIMENSION = 256;
/**
* Parse `{width, height, data}` from the bridge into a grayscale image.
* `data` is width*height brightness bytes, or width*height*4 RGBA bytes (an
* ImageData), which are reduced to luminance times alpha. Arrays arrive as
* plain arrays or as JSON-ified typed arrays ({"0": n, ...}). Returns null for
* anything malformed.
*/
export function parseWindowIcon(raw: unknown): GrayImage | null {
if (!raw || typeof raw !== "object") return null;
const record = raw as Record<string, unknown>;
const width = Number(record.width);
const height = Number(record.height);
if (!Number.isInteger(width) || !Number.isInteger(height)) return null;
if (width < 1 || height < 1 || width > MAX_WINDOW_ICON_DIMENSION || height > MAX_WINDOW_ICON_DIMENSION) return null;
const data = toNumberArray(record.data);
if (!data) return null;
const area = width * height;
const image = new GrayImage(width, height, 0);
if (data.length === area) {
for (let i = 0; i < area; i++) image.pixels[i] = clampByte(data[i]!);
} else if (data.length === area * 4) {
for (let i = 0; i < area; i++) {
const o = i * 4;
const luminance = 0.2126 * data[o]! + 0.7152 * data[o + 1]! + 0.0722 * data[o + 2]!;
image.pixels[i] = clampByte((luminance * data[o + 3]!) / 255);
}
} else {
return null;
}
return image;
}
/**
* Scale an icon to fit a size x size box, keeping its aspect ratio, with an
* area-averaging filter. Results are cached per size, since the switcher
* repaints often.
*/
export class WindowIconRenderer {
private readonly scaled = new Map<number, GrayImage>();
constructor(private readonly source: GrayImage) {}
render(size: number): GrayImage {
const box = Math.max(1, Math.round(size));
let image = this.scaled.get(box);
if (!image) {
image = scaleToFit(this.source, box);
this.scaled.set(box, image);
}
return image;
}
}
function scaleToFit(source: GrayImage, box: number): GrayImage {
const scale = Math.min(box / source.width, box / source.height);
const width = Math.max(1, Math.round(source.width * scale));
const height = Math.max(1, Math.round(source.height * scale));
if (width === source.width && height === source.height) return source;
const out = new GrayImage(width, height, 0);
for (let y = 0; y < height; y++) {
const y0 = (y * source.height) / height;
const y1 = ((y + 1) * source.height) / height;
for (let x = 0; x < width; x++) {
const x0 = (x * source.width) / width;
const x1 = ((x + 1) * source.width) / width;
out.pixels[y * width + x] = clampByte(areaAverage(source, x0, y0, x1, y1));
}
}
return out;
}
/** Mean of the source over the rectangle [x0,x1) x [y0,y1), weighting partly covered pixels. */
function areaAverage(source: GrayImage, x0: number, y0: number, x1: number, y1: number): number {
let sum = 0;
let weight = 0;
for (let sy = Math.floor(y0); sy < Math.min(source.height, Math.ceil(y1)); sy++) {
const wy = Math.min(y1, sy + 1) - Math.max(y0, sy);
for (let sx = Math.floor(x0); sx < Math.min(source.width, Math.ceil(x1)); sx++) {
const w = wy * (Math.min(x1, sx + 1) - Math.max(x0, sx));
sum += source.pixels[sy * source.width + sx]! * w;
weight += w;
}
}
return weight > 0 ? sum / weight : 0;
}
function toNumberArray(raw: unknown): number[] | null {
let values: unknown[];
if (Array.isArray(raw)) {
values = raw;
} else if (raw && typeof raw === "object") {
const record = raw as Record<string, unknown>;
values = [];
for (let i = 0; String(i) in record; i++) values.push(record[String(i)]);
} else {
return null;
}
const out: number[] = [];
for (const entry of values) {
const value = Number(entry);
if (!Number.isFinite(value)) return null;
out.push(value);
}
return out;
}
function clampByte(value: number): number {
return Math.max(0, Math.min(255, Math.round(value)));
}
+7
View File
@@ -25,6 +25,7 @@ import { GlanceHost, type GlanceDisplay } from './glance-host'
import { createLockScreenImage, LOCK_SCREEN_SURFACE_ID } from './lock-screen'
import { OsEventTypeList } from './events'
import { loadDeviceAddresses } from './device-addresses'
import { updateGlassesPresence } from './glasses-presence'
import { firmwareIncompatibilityMessage, hasCompatibleFirmware } from './firmware-compat'
import { isHaltedSessionPhase, suppressAutoReconnect } from './reconnect-policy'
import { deviceAddressError } from './ios-peripheral-identity'
@@ -373,6 +374,7 @@ export class IosPreviewController {
// a session's first snapshot) wakes the screen, to the lock notice if locked.
const putOn = wearing && this.glassesWorn === false
this.glassesWorn = wearing
this.syncPresence()
this.logBluetooth(`Glasses wear state: ${wearing ? 'ON_HEAD' : 'OFF_HEAD'}`)
if (!wearing && this.phoneLocked && this.lockEnabled) this.setGlassesLocked(true)
if (putOn && this.runtimeRunning && !shell.isScreenOn()) shell.wake('sidebar')
@@ -672,6 +674,10 @@ export class IosPreviewController {
this.logBluetooth(status)
this.emitState()
}
/** Mirror the session into glasses-presence (read by EvenHub's device status and Timers). */
private syncPresence(): void {
updateGlassesPresence({ connected: this.state.phase === 'connected', worn: this.glassesWorn, charging: this.state.charging ?? false })
}
private emitState(): void {
if (this.state.phase !== 'connected') resetRingInputFilter()
this.pushBrightness()
@@ -679,6 +685,7 @@ export class IosPreviewController {
if (this.state.phase !== 'connected' && this.state.phase !== 'connecting') this.glassesWorn = null
if (this.state.phase !== 'connected') iosVoiceInput.handleSessionEnded()
shell.setBatteryLevels({ headset: this.state.battery, headsetCharging: this.state.charging })
this.syncPresence()
this.syncRuntime()
if (this.active) this.onConnectionState({ ...this.state })
this.requestShellRender()
+23
View File
@@ -248,6 +248,29 @@ export function getFont(font: EmbeddedFontName): BdfFont {
return loadEmbeddedFont(font);
}
/** Every Terminus size bundled under fonts/terminus, each in normal and bold. */
export const TERMINUS_SIZES = [12, 14, 16, 18, 20, 22, 24, 28, 32] as const;
const terminusFonts = new Map<string, BdfFont>();
/**
* Terminus at any bundled size, normal or bold (EvenHub apps pick fonts by
* size and weight). Null for a size that isn't bundled. The sizes the UI
* already uses come back as the same objects getFont returns.
*/
export function getTerminusFont(size: number, bold: boolean): BdfFont | null {
if (!(TERMINUS_SIZES as readonly number[]).includes(size)) return null;
const name = `terminus${size}${bold ? "b" : ""}`;
if (name in embeddedFonts) return loadEmbeddedFont(name as EmbeddedFontName);
let font = terminusFonts.get(name);
if (!font) {
const text = knownFolders.currentApp().getFile(`fonts/terminus/ter-u${size}${bold ? "b" : "n"}.bdf`).readTextSync();
font = BdfFont.parse(text, { atlasKey: name });
terminusFonts.set(name, font);
}
return font;
}
// NOTE: the default UI font getters (getDefaultSmallFont & co.) live in
// ./ui-fonts, which resolves the user's font settings to a BdfFont or
// TtfFont. This module stays a pure BDF loader.
+22 -5
View File
@@ -394,6 +394,7 @@ class Shell {
private battery: ShellChromeState["battery"] = {
headset: null, headsetCharging: null, ring: null, ringCharging: null, watch: null, watchCharging: null,
};
private readonly batteryListeners = new Set<() => void>();
private attention = new Map<string, boolean>();
/** Set while the switcher's selection is on a notification (see NotificationSelection). */
private selectedNotification: NotificationSelection | null = null;
@@ -584,7 +585,19 @@ class Shell {
}
setBatteryLevels(levels: Partial<ShellChromeState["battery"]>): void {
this.battery = { ...this.battery, ...levels };
const next = { ...this.battery, ...levels };
const keys = Object.keys(next) as (keyof ShellChromeState["battery"])[];
if (keys.every((key) => next[key] === this.battery[key])) return;
this.battery = next;
for (const listener of Array.from(this.batteryListeners)) listener();
}
/** Called after any battery level or charging state changes. Returns an unsubscribe function. */
onBatteryLevelsChanged(listener: () => void): () => void {
this.batteryListeners.add(listener);
return () => {
this.batteryListeners.delete(listener);
};
}
/**
@@ -1397,16 +1410,20 @@ class Shell {
* Open the voice dialog aimed at the foreground window. Called when the
* user picks Voice input from the system menu (or an app asks via a
* start-voice-input message). The menu click already ended the press, so
* the dialog finishes on click instead of long-press-release.
* the dialog finishes on click instead of long-press-release. Returns
* false when the dialog can't open now (voice input disabled, screen off,
* another dialog or overlay up); true means it is opening, though a
* microphone-permission prompt can still stop it.
*/
startVoiceInput(): void {
if (this.config.voiceInputEnabled === false) return;
if (!this.screenOn || this.activeVoiceLayer || !this.stack.isAtBase()) return;
startVoiceInput(): boolean {
if (this.config.voiceInputEnabled === false || this.voiceDialogPending) return false;
if (!this.screenOn || this.activeVoiceLayer || !this.stack.isAtBase()) return false;
// The transcript is aimed at the window whose menu requested it; the menu
// entry point defaults the highlight to Type Into App.
this.focus = "window";
this.openVoiceDialog({ finishOnClick: true, defaultTarget: "app" });
this.config.requestShellRender();
return true;
}
/**