forked from Blendman974/faceclaw
Merge remote-tracking branch 'upstream/main' into pr36-merge
# Conflicts: # app/g2/dashboard-controller.ts
This commit is contained in:
411 files changed
+37477
-23831
No files matched your search
@@ -54,6 +54,8 @@ export type GlanceboardProvider = {
|
||||
showOnLongPress: () => boolean;
|
||||
/** Whether the head-tilt wake goes to the board rather than the regular UI. */
|
||||
showOnHeadTilt: () => boolean;
|
||||
/** Stereo depth (firmware units; positive is nearer) while the board covers the screen. */
|
||||
depth: () => number;
|
||||
/** A fresh board; requestRender asks the host to repaint it. */
|
||||
createBoard: (requestRender: () => void) => GlanceBoardInstance;
|
||||
};
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
* focus, tap-then-hold opens the window menu.
|
||||
*/
|
||||
import "@nativescript/core/globals";
|
||||
import { finishWorkerShutdown } from "../../ui/shell/worker-lifecycle";
|
||||
import { GrayImage } from "../../graphics/image";
|
||||
import { flattenPlanesWithDraws, planesFingerprint, type Plane } from "../../graphics/plane";
|
||||
import { prepareFrameDraws } from "../../graphics/glyph-wire";
|
||||
@@ -187,6 +188,12 @@ post({ type: "worker-ready" });
|
||||
global.onmessage = (event: { data: WorkerAppMessage }) => {
|
||||
const message = event.data;
|
||||
switch (message.type) {
|
||||
case "check-idle":
|
||||
post({ type: "worker-idle" });
|
||||
break;
|
||||
case "shutdown":
|
||||
finishWorkerShutdown();
|
||||
break;
|
||||
case "open-window": {
|
||||
const window: BlocksWindow = {
|
||||
windowId: message.windowId,
|
||||
|
||||
@@ -2,8 +2,8 @@ import { getDefaultSmallFont } from "../../graphics/ui-fonts";
|
||||
import { GrayImage } from "../../graphics/image";
|
||||
import { Layer, type LayerContext } from "../../ui/layers";
|
||||
import { type InputEvent } from "../../ui/gestures";
|
||||
import { drawSelectionHighlight } from "../../ui/menu";
|
||||
import { tightRowHeight } from "../../ui/metrics";
|
||||
import { Menu, type MenuDrawArgs } from "../../ui/menu-core";
|
||||
import { centeredTextY, tightRowHeight } from "../../ui/metrics";
|
||||
import {
|
||||
cancelBandwidthBenchmark,
|
||||
getBandwidthBenchmarkStatus,
|
||||
@@ -29,6 +29,7 @@ const LIST_X = 20;
|
||||
const VALUE_X = 150;
|
||||
|
||||
type Phase = "config" | "arming" | "running" | "done";
|
||||
type ConfigRow = "size" | "window" | "link" | "start";
|
||||
|
||||
/**
|
||||
* BLE bandwidth benchmark: pick a message size and pipeline window, stream
|
||||
@@ -38,7 +39,14 @@ type Phase = "config" | "arming" | "running" | "done";
|
||||
*/
|
||||
export class BandwidthBenchmarkLayer implements Layer {
|
||||
private phase: Phase = "config";
|
||||
private selectedRow = 0; // size, window, link preference, start
|
||||
private readonly menu = new Menu<ConfigRow>({
|
||||
items: ["size", "window", "link", "start"],
|
||||
wrap: false,
|
||||
rowGap: 1,
|
||||
highlight: { radius: 4 },
|
||||
getHeight: () => tightRowHeight(getDefaultSmallFont()) + 4,
|
||||
draw: (args) => this.drawRow(args),
|
||||
});
|
||||
private sizeIndex = 2;
|
||||
private windowIndex = 2;
|
||||
private linkModeIndex = 0;
|
||||
@@ -134,26 +142,14 @@ export class BandwidthBenchmarkLayer implements Layer {
|
||||
}
|
||||
|
||||
const rowH = tightRowHeight(font) + 4;
|
||||
const rows: Array<[string, string]> = [
|
||||
["Message size", `${MESSAGE_SIZES[this.sizeIndex]} B`],
|
||||
["Window size", `${WINDOW_SIZES[this.windowIndex]}`],
|
||||
["Link request", LINK_MODES[this.linkModeIndex]!],
|
||||
[this.phase === "done" ? "Run again" : "Start", ""],
|
||||
];
|
||||
for (let index = 0; index < rows.length; index++) {
|
||||
const [label, value] = rows[index]!;
|
||||
const y = HEADER_HEIGHT + index * rowH;
|
||||
const selected = index === this.selectedRow;
|
||||
if (selected) {
|
||||
drawSelectionHighlight(image, LIST_X - 6, y - 1, width - 2 * LIST_X + 12, rowH - 1, ctx.stack.isFocused(), 4);
|
||||
}
|
||||
image.drawText(font, LIST_X, y + 2, label, selected ? 255 : 200);
|
||||
if (value) {
|
||||
image.drawText(font, VALUE_X, y + 2, value, selected ? 235 : 160);
|
||||
}
|
||||
}
|
||||
const rowCount = this.menu.items.length;
|
||||
this.menu.paint(
|
||||
image,
|
||||
{ x: LIST_X - 6, y: HEADER_HEIGHT - 1, width: width - 2 * LIST_X + 12, height: rowCount * rowH },
|
||||
ctx.stack.isFocused(),
|
||||
);
|
||||
|
||||
let y = HEADER_HEIGHT + rows.length * rowH + 10;
|
||||
let y = HEADER_HEIGHT + rowCount * rowH + 10;
|
||||
if (this.error) {
|
||||
image.drawText(font, LIST_X, y, this.error, 180);
|
||||
} else if (this.result) {
|
||||
@@ -176,6 +172,21 @@ export class BandwidthBenchmarkLayer implements Layer {
|
||||
return image;
|
||||
}
|
||||
|
||||
private drawRow({ image, item, x, y, height, selected }: MenuDrawArgs<ConfigRow>): void {
|
||||
const font = getDefaultSmallFont();
|
||||
const [label, value]: [string, string] =
|
||||
item === "size" ? ["Message size", `${MESSAGE_SIZES[this.sizeIndex]} B`]
|
||||
: item === "window" ? ["Window size", `${WINDOW_SIZES[this.windowIndex]}`]
|
||||
: item === "link" ? ["Link request", LINK_MODES[this.linkModeIndex]!]
|
||||
: [this.phase === "done" ? "Run again" : "Start", ""];
|
||||
const textY = centeredTextY(font, y, height);
|
||||
// Text sits 6px in from the row box (LIST_X), values at VALUE_X.
|
||||
image.drawText(font, x + 6, textY, label, selected ? 255 : 200);
|
||||
if (value) {
|
||||
image.drawText(font, x + 6 + VALUE_X - LIST_X, textY, value, selected ? 235 : 160);
|
||||
}
|
||||
}
|
||||
|
||||
async handleInput(event: InputEvent, ctx: LayerContext): Promise<void> {
|
||||
if (this.phase === "arming" || this.phase === "running") {
|
||||
// A static screen during the run: only allow bailing out.
|
||||
@@ -186,22 +197,22 @@ export class BandwidthBenchmarkLayer implements Layer {
|
||||
}
|
||||
switch (event.type) {
|
||||
case "scroll-up":
|
||||
this.selectedRow = Math.max(0, this.selectedRow - 1);
|
||||
return;
|
||||
case "scroll-down":
|
||||
this.selectedRow = Math.min(3, this.selectedRow + 1);
|
||||
await this.menu.handleInput(event);
|
||||
return;
|
||||
case "click":
|
||||
if (this.selectedRow === 0) {
|
||||
case "click": {
|
||||
const row = this.menu.selectedItem;
|
||||
if (row === "size") {
|
||||
this.sizeIndex = (this.sizeIndex + 1) % MESSAGE_SIZES.length;
|
||||
} else if (this.selectedRow === 1) {
|
||||
} else if (row === "window") {
|
||||
this.windowIndex = (this.windowIndex + 1) % WINDOW_SIZES.length;
|
||||
} else if (this.selectedRow === 2) {
|
||||
} else if (row === "link") {
|
||||
this.linkModeIndex = (this.linkModeIndex + 1) % LINK_MODES.length;
|
||||
} else {
|
||||
} else if (row === "start") {
|
||||
this.startRun();
|
||||
}
|
||||
return;
|
||||
}
|
||||
case "double-click":
|
||||
ctx.stack.pop();
|
||||
return;
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
import { getDefaultSmallFont } from "../../graphics/ui-fonts";
|
||||
import { GrayImage } from "../../graphics/image";
|
||||
import { clamp } from "../../util/numeric-util";
|
||||
import { Layer, type LayerActions, type LayerContext } from "../../ui/layers";
|
||||
import { GESTURE_CLICK, GESTURE_DOUBLE_CLICK, GESTURE_SCROLL, type InputEvent } from "../../ui/gestures";
|
||||
import { drawSelectionHighlight, scrollToKeepSelectionVisible } from "../../ui/menu";
|
||||
import { tightRowHeight } from "../../ui/metrics";
|
||||
import { Menu, type MenuDrawArgs } from "../../ui/menu-core";
|
||||
import { centeredTextY, tightRowHeight } from "../../ui/metrics";
|
||||
import {
|
||||
buildSoundSequencePayload,
|
||||
CFW_SEQ_MAX,
|
||||
@@ -28,8 +27,14 @@ function sleep(ms: number): Promise<void> {
|
||||
* sequencer message lands as the previous phrase finishes.
|
||||
*/
|
||||
export class BuzzerDemoLayer implements Layer {
|
||||
private selectedIndex = 0;
|
||||
private scrollRow = 0;
|
||||
private readonly menu = new Menu<SoundEffect>({
|
||||
items: SOUND_EFFECTS,
|
||||
wrap: false,
|
||||
rowGap: 1,
|
||||
highlight: { radius: 4 },
|
||||
getHeight: () => tightRowHeight(getDefaultSmallFont()),
|
||||
draw: (args) => drawEffectRow(args),
|
||||
});
|
||||
private playing: string | null = null;
|
||||
|
||||
paint(ctx: LayerContext): GrayImage {
|
||||
@@ -42,21 +47,11 @@ export class BuzzerDemoLayer implements Layer {
|
||||
image.drawText(font, width - 24 - font.measureText(status), 8, status, 140);
|
||||
|
||||
const listHeight = height - HEADER_HEIGHT - FOOTER_HEIGHT;
|
||||
const rowH = tightRowHeight(font);
|
||||
const visibleRows = Math.max(1, (listHeight / rowH) | 0);
|
||||
this.scrollRow = scrollToKeepSelectionVisible(this.scrollRow, this.selectedIndex, visibleRows, SOUND_EFFECTS.length);
|
||||
|
||||
const lastVisible = Math.min(SOUND_EFFECTS.length, this.scrollRow + visibleRows);
|
||||
for (let index = this.scrollRow; index < lastVisible; index++) {
|
||||
const effect = SOUND_EFFECTS[index]!;
|
||||
const y = HEADER_HEIGHT + (index - this.scrollRow) * rowH;
|
||||
const selected = index === this.selectedIndex;
|
||||
if (selected) {
|
||||
drawSelectionHighlight(image, LIST_X - 6, y - 1, width - 2 * LIST_X + 12, rowH - 1, ctx.stack.isFocused(), 4);
|
||||
}
|
||||
image.drawText(font, LIST_X, y + 1, effect.name, selected ? 255 : 200);
|
||||
image.drawText(font, LIST_X + 110, y + 1, effect.desc, selected ? 180 : 120);
|
||||
}
|
||||
this.menu.paint(
|
||||
image,
|
||||
{ x: LIST_X - 6, y: HEADER_HEIGHT - 1, width: width - 2 * LIST_X + 12, height: listHeight },
|
||||
ctx.stack.isFocused(),
|
||||
);
|
||||
|
||||
return image;
|
||||
}
|
||||
@@ -64,13 +59,11 @@ export class BuzzerDemoLayer implements Layer {
|
||||
async handleInput(event: InputEvent, ctx: LayerContext): Promise<void> {
|
||||
switch (event.type) {
|
||||
case "scroll-up":
|
||||
this.selectedIndex = Math.max(0, this.selectedIndex - 1);
|
||||
return;
|
||||
case "scroll-down":
|
||||
this.selectedIndex = Math.min(SOUND_EFFECTS.length - 1, this.selectedIndex + 1);
|
||||
await this.menu.handleInput(event);
|
||||
return;
|
||||
case "click": {
|
||||
const effect = SOUND_EFFECTS[clamp(this.selectedIndex, 0, SOUND_EFFECTS.length - 1)];
|
||||
const effect = this.menu.selectedItem;
|
||||
if (effect && !this.playing) {
|
||||
// Fire-and-forget: playback paces itself with sleeps between
|
||||
// phrases; awaiting it here would stall input for seconds.
|
||||
@@ -106,3 +99,11 @@ export class BuzzerDemoLayer implements Layer {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Effect name, then its description in a second column 110px to the right. */
|
||||
function drawEffectRow({ image, item: effect, x, y, height, selected }: MenuDrawArgs<SoundEffect>): void {
|
||||
const font = getDefaultSmallFont();
|
||||
const textY = centeredTextY(font, y, height);
|
||||
image.drawText(font, x + 6, textY, effect.name, selected ? 255 : 200);
|
||||
image.drawText(font, x + 6 + 110, textY, effect.desc, selected ? 180 : 120);
|
||||
}
|
||||
@@ -3,8 +3,8 @@ import { GrayImage } from "../../graphics/image";
|
||||
import { truncateText } from "../../graphics/textwrap";
|
||||
import { type InputEvent } from "../../ui/gestures";
|
||||
import { Layer, type LayerContext, type PaintBelow } from "../../ui/layers";
|
||||
import { drawSelectionHighlight } from "../../ui/menu";
|
||||
import { LIST_ROW_TEXT_INSET, lineStep, listRowHeight } from "../../ui/metrics";
|
||||
import { Menu, type MenuDrawArgs } from "../../ui/menu-core";
|
||||
import { lineStep, listRowHeight } from "../../ui/metrics";
|
||||
|
||||
const DIALOG_X = 8;
|
||||
const DIALOG_Y = 8;
|
||||
@@ -21,6 +21,8 @@ const SERVICE_LABELS: Record<string, string> = {
|
||||
|
||||
export type ApiKeyRequestItem = { id: string; configured: boolean };
|
||||
|
||||
type ConsentAction = "Allow" | "Deny";
|
||||
|
||||
/**
|
||||
* Consent dialog shown when an EvenHub app calls requestApiKeyAccess: lists the
|
||||
* requested API-key services (marking any the user hasn't configured) and asks
|
||||
@@ -28,8 +30,14 @@ export type ApiKeyRequestItem = { id: string; configured: boolean };
|
||||
* double-click) shares nothing.
|
||||
*/
|
||||
export class EvenHubApiKeyDialogLayer implements Layer {
|
||||
/** 0 = Allow, 1 = Deny. */
|
||||
private selectedIndex = 0;
|
||||
private readonly menu = new Menu<ConsentAction>({
|
||||
items: ["Allow", "Deny"],
|
||||
wrap: true,
|
||||
rowGap: 1,
|
||||
highlight: { radius: 8 },
|
||||
getHeight: () => listRowHeight(getDefaultSmallFont()),
|
||||
draw: (args) => drawActionRow(args),
|
||||
});
|
||||
private resolved = false;
|
||||
|
||||
constructor(
|
||||
@@ -67,16 +75,11 @@ export class EvenHubApiKeyDialogLayer implements Layer {
|
||||
}
|
||||
y += 4;
|
||||
|
||||
const focused = ctx.stack.isFocused();
|
||||
const actions = ["Allow", "Deny"];
|
||||
for (let index = 0; index < actions.length; index++) {
|
||||
const rowY = y + index * actionRowH;
|
||||
const selected = index === this.selectedIndex;
|
||||
if (selected) {
|
||||
drawSelectionHighlight(image, DIALOG_X + 12, rowY, DIALOG_WIDTH - 24, actionRowH - 1, focused, 8);
|
||||
}
|
||||
image.drawText(font, DIALOG_X + 22, rowY + 3, actions[index]!, selected ? 255 : 200);
|
||||
}
|
||||
this.menu.paint(
|
||||
image,
|
||||
{ x: DIALOG_X + 12, y, width: DIALOG_WIDTH - 24, height: Math.max(0, DIALOG_Y + height - PADDING - y) },
|
||||
ctx.stack.isFocused(),
|
||||
);
|
||||
return image;
|
||||
}
|
||||
|
||||
@@ -84,10 +87,10 @@ export class EvenHubApiKeyDialogLayer implements Layer {
|
||||
switch (event.type) {
|
||||
case "scroll-up":
|
||||
case "scroll-down":
|
||||
this.selectedIndex = (this.selectedIndex + 1) % 2;
|
||||
void this.menu.handleInput(event);
|
||||
return;
|
||||
case "click":
|
||||
this.resolve(ctx, this.selectedIndex === 0);
|
||||
this.resolve(ctx, this.menu.selectedItem === "Allow");
|
||||
return;
|
||||
case "double-click":
|
||||
this.resolve(ctx, false);
|
||||
@@ -113,3 +116,7 @@ export class EvenHubApiKeyDialogLayer implements Layer {
|
||||
else this.onCancel();
|
||||
}
|
||||
}
|
||||
|
||||
function drawActionRow({ image, item, x, y, selected }: MenuDrawArgs<ConsentAction>): void {
|
||||
image.drawText(getDefaultSmallFont(), x + 10, y + 3, item, selected ? 255 : 200);
|
||||
}
|
||||
@@ -181,12 +181,18 @@ export class EvenHubApiClient {
|
||||
path: string,
|
||||
options: { query?: Record<string, string | number>; body?: Record<string, unknown> } = {},
|
||||
): Promise<unknown> {
|
||||
let token = await this.ensureLogin();
|
||||
const token = await this.ensureLogin();
|
||||
const response = await this.request(method, path, { ...options, token, commonVersion: 3 });
|
||||
if (response.httpStatus === 401) {
|
||||
invalidateEvenHubToken();
|
||||
try {
|
||||
return unwrap(response.envelope, response.httpStatus, path);
|
||||
} catch (error) {
|
||||
// A rejected session fails every later request too, so drop the token
|
||||
// (unless a fresh sign-in replaced it while this request was in flight).
|
||||
if (error instanceof EvenHubAuthenticationError && getEvenHubToken() === token) {
|
||||
invalidateEvenHubToken();
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
return unwrap(response.envelope, response.httpStatus, path);
|
||||
}
|
||||
|
||||
private ensureLogin(): Promise<string> {
|
||||
@@ -350,15 +356,17 @@ async function fetchWithTimeout(url: string, init: RequestInit, timeoutMs = REQU
|
||||
}
|
||||
|
||||
function unwrap(envelope: ApiEnvelope, httpStatus: number, path: string): unknown {
|
||||
if (httpStatus === 401) {
|
||||
throw new EvenHubAuthenticationError("Even rejected the login session.");
|
||||
const message = typeof envelope.msg === "string" ? envelope.msg.trim() : "";
|
||||
// An expired session can come back as HTTP 401, or as HTTP 200 with code
|
||||
// 401 in the envelope ("Your login is expired").
|
||||
if (httpStatus === 401 || Number(envelope.code) === 401) {
|
||||
throw new EvenHubAuthenticationError(message || "Even rejected the login session.");
|
||||
}
|
||||
if (httpStatus < 200 || httpStatus >= 300) {
|
||||
throw new Error(`EvenHub request failed (HTTP ${httpStatus}, ${path}).`);
|
||||
}
|
||||
if (envelope.code !== 0) {
|
||||
const detail = typeof envelope.msg === "string" && envelope.msg ? `: ${envelope.msg}` : "";
|
||||
throw new Error(`EvenHub API error ${envelope.code ?? "unknown"}${detail}`);
|
||||
throw new Error(`EvenHub API error ${envelope.code ?? "unknown"}${message ? `: ${message}` : ""}`);
|
||||
}
|
||||
return envelope.data;
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ import { GrayImage } from "../../graphics/image";
|
||||
import { truncateText, wrapText } from "../../graphics/textwrap";
|
||||
import { type InputEvent } from "../../ui/gestures";
|
||||
import { type Layer, type LayerContext } from "../../ui/layers";
|
||||
import { drawSelectionHighlight } from "../../ui/menu";
|
||||
import { Menu } from "../../ui/menu-core";
|
||||
import { TextViewerLayer } from "../files/text-viewer";
|
||||
import { evenHubApi, type EvenHubStoreApp } from "./even-api";
|
||||
import {
|
||||
@@ -18,17 +18,36 @@ import { lineStep, listRowHeight } from "../../ui/metrics";
|
||||
|
||||
const X = 18;
|
||||
|
||||
type DetailAction = "about" | "whats-new" | "primary";
|
||||
const ACTIONS: readonly DetailAction[] = ["about", "whats-new", "primary"];
|
||||
|
||||
export type EvenHubStoreDetailOptions = {
|
||||
launchApp: (appId: string) => Promise<void> | void;
|
||||
appendLog: (message: string) => void;
|
||||
/** A package was installed, updated, or reinstalled from this page. */
|
||||
onInstalled?: (app: InstalledEvenHubApp) => void;
|
||||
/** Offered every failure first; returns true if it was a rejected session (and was dealt with). */
|
||||
onAuthError?: (ctx: LayerContext, error: unknown) => boolean;
|
||||
};
|
||||
|
||||
/** Storefront metadata and the Install/Launch action for one public app. */
|
||||
export class EvenHubStoreDetailLayer implements Layer {
|
||||
/** About, What's New, Install/Launch. */
|
||||
private selectedIndex = 2;
|
||||
/** About, What's New, Install/Launch; starts on the primary action. */
|
||||
private readonly actions = new Menu<DetailAction>({
|
||||
items: ACTIONS,
|
||||
selectedIndex: 2,
|
||||
rowGap: 1,
|
||||
highlight: { radius: 8 },
|
||||
getHeight: () => listRowHeight(getDefaultSmallFont()) + 4,
|
||||
draw: ({ image, item, x, y, width, selected }) => {
|
||||
const font = getDefaultSmallFont();
|
||||
const label = item === "about" ? "About" : item === "whats-new" ? "What's New" : this.primaryLabel;
|
||||
const value = this.working && item === "primary" ? 145 : selected ? 245 : 190;
|
||||
image.drawText(font, x + 11, y + 5, truncateText(font, label, width - 18), value);
|
||||
},
|
||||
});
|
||||
/** The primary action's label for the paint in progress. */
|
||||
private primaryLabel = "";
|
||||
private working = false;
|
||||
private detailPromise: Promise<void> | null = null;
|
||||
private status = "";
|
||||
@@ -47,7 +66,7 @@ export class EvenHubStoreDetailLayer implements Layer {
|
||||
const actionRowH = listRowHeight(font) + 4;
|
||||
|
||||
const installed = getInstalledEvenHubApp(this.app.packageId);
|
||||
const actions = ["About", "What's New", this.working ? "Installing..." : this.primaryActionLabel(installed)];
|
||||
this.primaryLabel = this.working ? "Installing..." : this.primaryActionLabel(installed);
|
||||
|
||||
// Detail lines as (text, shade, extra gap below); built per candidate
|
||||
// width so the layout choice below can measure before drawing.
|
||||
@@ -90,7 +109,7 @@ export class EvenHubStoreDetailLayer implements Layer {
|
||||
// Stacked layout (details full-width, actions at the bottom) when it
|
||||
// fits; otherwise the actions become a narrow top-right menu and the
|
||||
// details flow down a left column beside it.
|
||||
const stackedActionTop = height - 6 - actions.length * actionRowH;
|
||||
const stackedActionTop = height - 6 - ACTIONS.length * actionRowH;
|
||||
const stacked = 8 + linesHeight(buildLines(width - X * 2)) + 6 <= stackedActionTop;
|
||||
const menuW = 150;
|
||||
const menuX = width - menuW - 12;
|
||||
@@ -105,15 +124,11 @@ export class EvenHubStoreDetailLayer implements Layer {
|
||||
const actionX = stacked ? X : menuX;
|
||||
const actionW = stacked ? textWidth : menuW;
|
||||
const actionTop = stacked ? stackedActionTop : 8;
|
||||
for (let index = 0; index < actions.length; index++) {
|
||||
const actionY = actionTop + index * actionRowH;
|
||||
const selected = index === this.selectedIndex;
|
||||
if (selected) {
|
||||
drawSelectionHighlight(image, actionX - 5, actionY, actionW + 10, actionRowH - 1, ctx.stack.isFocused(), 8);
|
||||
}
|
||||
const value = this.working && index === 2 ? 145 : selected ? 245 : 190;
|
||||
image.drawText(font, actionX + 6, actionY + 5, truncateText(font, actions[index]!, actionW - 8), value);
|
||||
}
|
||||
this.actions.paint(
|
||||
image,
|
||||
{ x: actionX - 5, y: actionTop, width: actionW + 10, height: ACTIONS.length * actionRowH - 1 },
|
||||
ctx.stack.isFocused(),
|
||||
);
|
||||
return image;
|
||||
}
|
||||
|
||||
@@ -123,12 +138,8 @@ export class EvenHubStoreDetailLayer implements Layer {
|
||||
return;
|
||||
}
|
||||
if (this.working) return;
|
||||
if (event.type === "scroll-up") {
|
||||
this.selectedIndex = Math.max(0, this.selectedIndex - 1);
|
||||
return;
|
||||
}
|
||||
if (event.type === "scroll-down") {
|
||||
this.selectedIndex = Math.min(2, this.selectedIndex + 1);
|
||||
if (event.type === "scroll-up" || event.type === "scroll-down") {
|
||||
await this.actions.handleInput(event);
|
||||
return;
|
||||
}
|
||||
if (event.type !== "click") return;
|
||||
@@ -143,11 +154,12 @@ export class EvenHubStoreDetailLayer implements Layer {
|
||||
try {
|
||||
await this.ensureDetail(ctx);
|
||||
|
||||
if (this.selectedIndex === 0) {
|
||||
const action = this.actions.selectedItem;
|
||||
if (action === "about") {
|
||||
ctx.stack.push(new TextViewerLayer(this.app.description || "No description supplied.", "About"));
|
||||
return;
|
||||
}
|
||||
if (this.selectedIndex === 1) {
|
||||
if (action === "whats-new") {
|
||||
ctx.stack.push(new TextViewerLayer(this.app.changelog || "No release notes supplied.", "What's New"));
|
||||
return;
|
||||
}
|
||||
@@ -160,6 +172,7 @@ export class EvenHubStoreDetailLayer implements Layer {
|
||||
|
||||
await this.install(ctx);
|
||||
} catch (error) {
|
||||
if (this.options.onAuthError?.(ctx, error)) return;
|
||||
this.status = cleanError(error);
|
||||
this.options.appendLog(`evenhub store: ${this.status}`);
|
||||
} finally { this.working = false; ctx.actions.requestRender(); }
|
||||
@@ -193,6 +206,7 @@ export class EvenHubStoreDetailLayer implements Layer {
|
||||
ctx.actions.requestRender();
|
||||
await this.options.launchApp(installedEvenHubAppId(result.packageId));
|
||||
} catch (error) {
|
||||
if (this.options.onAuthError?.(ctx, error)) return;
|
||||
this.status = cleanError(error);
|
||||
this.options.appendLog(`evenhub store: ${this.status}`);
|
||||
} finally {
|
||||
@@ -216,6 +230,7 @@ export class EvenHubStoreDetailLayer implements Layer {
|
||||
return undefined;
|
||||
})
|
||||
.catch((error) => {
|
||||
if (this.options.onAuthError?.(ctx, error)) return;
|
||||
this.options.appendLog(`evenhub store: app details unavailable: ${cleanError(error)}`);
|
||||
})
|
||||
.finally(() => ctx.actions.requestRender());
|
||||
|
||||
@@ -53,8 +53,6 @@ export async function installStoreApp(
|
||||
const needsPrompt = (manifest.permissions.length > 0 || privacyPolicyUrl)
|
||||
&& !alreadyGranted(app.packageId, manifest);
|
||||
if (needsPrompt) {
|
||||
options.onStatus("");
|
||||
ctx.actions.requestRender();
|
||||
const accepted = await new Promise<boolean>((resolve) => {
|
||||
ctx.stack.push(
|
||||
new EvenHubPermissionDialogLayer(
|
||||
@@ -65,6 +63,10 @@ export async function installStoreApp(
|
||||
() => resolve(false),
|
||||
),
|
||||
);
|
||||
// Only once the dialog is stacked: an in-process window's requestRender
|
||||
// paints synchronously, and nothing else repaints after a download.
|
||||
options.onStatus("");
|
||||
ctx.actions.requestRender();
|
||||
});
|
||||
if (!accepted) return null;
|
||||
}
|
||||
|
||||
+120
-99
@@ -1,16 +1,16 @@
|
||||
import { getDefaultSmallFont } from "../../graphics/ui-fonts";
|
||||
import { GrayImage, type UiFont } from "../../graphics/image";
|
||||
import { truncateText, wrapText } from "../../graphics/textwrap";
|
||||
import { clamp } from "../../util/numeric-util";
|
||||
import { GESTURE_CLICK, type InputEvent } from "../../ui/gestures";
|
||||
import { type Layer, type LayerActions, type LayerContext, type LayerStack } from "../../ui/layers";
|
||||
import { drawListScrollbar, drawSelectionHighlight, scrollToKeepSelectionVisible, type MenuItem } from "../../ui/menu";
|
||||
import { drawSelectionHighlight, type MenuItem } from "../../ui/menu";
|
||||
import { Menu } from "../../ui/menu-core";
|
||||
import { shell } from "../../ui/shell/shell";
|
||||
import { ConfigSettingString } from "../../ui/dashboard-settings";
|
||||
import { evenHubApi, EvenHubAuthenticationError, isEvenHubStoreConfigured, type EvenHubStoreApp, type EvenHubStorePage } from "./even-api";
|
||||
import { clearEvenHubLoginForm, clearEvenHubSession, clearTransientEvenHubSession, evenHubLoginEmailSetting, evenHubLoginPasswordSetting, evenHubRememberMeSetting, resetEvenHubLoginForm } from "./credentials";
|
||||
import { getInstalledEvenHubApps, uninstallEvenHubPackage, type InstalledEvenHubApp } from "./installed-apps";
|
||||
import { EvenHubStoreDetailLayer } from "./store-detail-layer";
|
||||
import { EvenHubStoreDetailLayer, type EvenHubStoreDetailOptions } from "./store-detail-layer";
|
||||
import { cleanError, installStoreApp } from "./store-install";
|
||||
import { checkForEvenHubUpdates, describeUpdate, type EvenHubAppUpdate } from "./updates";
|
||||
import { lineStep } from "../../ui/metrics";
|
||||
@@ -24,6 +24,11 @@ function headerHeight(font: UiFont): number {
|
||||
return 8 + lineStep(font) + font.lineHeight + 6;
|
||||
}
|
||||
|
||||
/** Row pitch of the two-line app list (title + detail), including the 2px gap between highlights. */
|
||||
function storeRowHeight(font: UiFont): number {
|
||||
return Math.max(30, 2 * lineStep(font) + 4);
|
||||
}
|
||||
|
||||
/** Last search typed on the phone; kept so reopening the store shows it again. */
|
||||
export const evenHubSearchQuerySetting = new ConfigSettingString({
|
||||
id: "evenhub-search-query",
|
||||
@@ -64,27 +69,46 @@ type TabState = {
|
||||
loading: boolean;
|
||||
/** Progress or error text for the subtitle line. */
|
||||
status: string;
|
||||
selectedIndex: number;
|
||||
scrollRow: number;
|
||||
/** Selection and scroll over `rows` (resynced before each paint and input). */
|
||||
menu: Menu<StoreRow>;
|
||||
/** Search only: the query the rows belong to. */
|
||||
query: string;
|
||||
};
|
||||
|
||||
function emptyTab(): TabState {
|
||||
const rows: StoreRow[] = [];
|
||||
return {
|
||||
rows: [],
|
||||
rows,
|
||||
total: 0,
|
||||
nextPage: 1,
|
||||
exhausted: false,
|
||||
loaded: false,
|
||||
loading: false,
|
||||
status: "",
|
||||
selectedIndex: 0,
|
||||
scrollRow: 0,
|
||||
menu: new Menu<StoreRow>({
|
||||
items: rows,
|
||||
rowGap: 2,
|
||||
highlight: { radius: 5 },
|
||||
getHeight: () => storeRowHeight(getDefaultSmallFont()),
|
||||
draw: ({ image, item, x, y, width }) => {
|
||||
const font = getDefaultSmallFont();
|
||||
const { title, detail, titleValue } = rowText(item);
|
||||
const textX = x + 6;
|
||||
const textW = width - 12;
|
||||
image.drawText(font, textX, y + 2, truncateText(font, title, textW), titleValue);
|
||||
image.drawText(font, textX, y + 2 + lineStep(font), truncateText(font, detail, textW), 115);
|
||||
},
|
||||
}),
|
||||
query: "",
|
||||
};
|
||||
}
|
||||
|
||||
/** The tab's menu, with its items brought up to date with `rows` (which loads replace or extend). */
|
||||
function tabMenu(tab: TabState): Menu<StoreRow> {
|
||||
if (tab.menu.items !== tab.rows) tab.menu.setItems(tab.rows);
|
||||
return tab.menu;
|
||||
}
|
||||
|
||||
export type EvenHubStoreLayerOptions = {
|
||||
launchApp: (appId: string) => Promise<void> | void;
|
||||
appendLog: (message: string) => void;
|
||||
@@ -126,13 +150,11 @@ export class EvenHubStoreLayer implements Layer {
|
||||
this.started = true;
|
||||
// Do NOT open the phone credential editor here. This runs on the first
|
||||
// paint, which includes the restore of a still-open EvenHub at app
|
||||
// startup -- so an unfinished sign-in re-opened the form on every launch,
|
||||
// and the form has no way out: it has no Cancel control, its onCancel is
|
||||
// dropped before it reaches the controller, and Back leaves the app
|
||||
// rather than closing it. The login pane already says "Enter your Even
|
||||
// account email and password in the phone app" and already opens the
|
||||
// editor on click (handleInput), so the editor is still one tap away for
|
||||
// someone who actually wants it.
|
||||
// startup -- so an unfinished sign-in re-opened the form on every
|
||||
// launch, unasked. The login pane already says "Enter your Even account
|
||||
// email and password in the phone app" and already opens the editor on
|
||||
// click (handleInput), so the editor is still one tap away for someone
|
||||
// who actually wants it.
|
||||
if (!this.showingLogin) {
|
||||
void this.ensureTabLoaded(ctx, this.activeTab);
|
||||
}
|
||||
@@ -170,25 +192,16 @@ export class EvenHubStoreLayer implements Layer {
|
||||
return image;
|
||||
}
|
||||
|
||||
tab.selectedIndex = clamp(tab.selectedIndex, 0, tab.rows.length - 1);
|
||||
const rowH = Math.max(30, 2 * lineStep(font) + 4);
|
||||
const rowH = storeRowHeight(font);
|
||||
const visibleRows = Math.max(1, Math.floor((height - headerH - 4) / rowH));
|
||||
tab.scrollRow = scrollToKeepSelectionVisible(tab.scrollRow, tab.selectedIndex, visibleRows, tab.rows.length);
|
||||
const last = Math.min(tab.rows.length, tab.scrollRow + visibleRows);
|
||||
const listFocused = ctx.stack.isFocused() && this.focus === "list";
|
||||
for (let index = tab.scrollRow; index < last; index++) {
|
||||
const row = tab.rows[index]!;
|
||||
const y = headerH + (index - tab.scrollRow) * rowH;
|
||||
if (index === tab.selectedIndex) {
|
||||
drawSelectionHighlight(image, LIST_X - 6, y, width - LIST_X * 2 + 12, rowH - 2, listFocused, 5);
|
||||
}
|
||||
const { title, detail, titleValue } = this.rowText(row);
|
||||
image.drawText(font, LIST_X, y + 2, truncateText(font, title, width - LIST_X * 2), titleValue);
|
||||
image.drawText(font, LIST_X, y + 2 + lineStep(font), truncateText(font, detail, width - LIST_X * 2), 115);
|
||||
}
|
||||
if (tab.rows.length > visibleRows) {
|
||||
drawListScrollbar(image, width - 5, headerH, visibleRows * rowH - 3, tab.scrollRow, visibleRows, tab.rows.length);
|
||||
}
|
||||
const menu = tabMenu(tab);
|
||||
menu.paint(
|
||||
image,
|
||||
{ x: LIST_X - 6, y: headerH, width: width - LIST_X * 2 + 12, height: visibleRows * rowH - 2 },
|
||||
listFocused,
|
||||
);
|
||||
menu.drawScrollbar(image, width - 5, headerH, visibleRows * rowH - 3);
|
||||
return image;
|
||||
}
|
||||
|
||||
@@ -250,41 +263,6 @@ export class EvenHubStoreLayer implements Layer {
|
||||
}
|
||||
}
|
||||
|
||||
private rowText(row: StoreRow): { title: string; detail: string; titleValue: number } {
|
||||
switch (row.kind) {
|
||||
case "search-query": {
|
||||
const query = evenHubSearchQuerySetting.get();
|
||||
return {
|
||||
title: query ? `Search: ${query}` : "Search: (enter a query)",
|
||||
detail: "Click to type on the phone, or use voice input.",
|
||||
titleValue: 200,
|
||||
};
|
||||
}
|
||||
case "orphan":
|
||||
return {
|
||||
title: row.update.installed.name,
|
||||
detail: row.update.error
|
||||
? `Package missing · store lookup failed: ${row.update.error}`
|
||||
: "Package missing · not in the store · click to forget",
|
||||
titleValue: 170,
|
||||
};
|
||||
case "app": {
|
||||
const { app, update } = row;
|
||||
if (!update) {
|
||||
return {
|
||||
title: app.name,
|
||||
detail: app.tagline || `${app.creatorName} · ${formatCount(app.installCount)} installs`,
|
||||
titleValue: 215,
|
||||
};
|
||||
}
|
||||
const detail = update.packageMissing
|
||||
? `Package missing · reinstall ${app.version ? `version ${app.version}` : ""}`.trim()
|
||||
: `Version ${update.installed.version} installed · ${app.version} available`;
|
||||
return { title: app.name, detail, titleValue: 215 };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async handleInput(event: InputEvent, ctx: LayerContext): Promise<void> {
|
||||
if (this.showingLogin) {
|
||||
if (event.type === "click" && !this.loginBusy) {
|
||||
@@ -299,16 +277,17 @@ export class EvenHubStoreLayer implements Layer {
|
||||
return;
|
||||
}
|
||||
const tab = this.tabs[this.activeTab];
|
||||
const menu = tabMenu(tab);
|
||||
switch (event.type) {
|
||||
case "scroll-up":
|
||||
tab.selectedIndex = Math.max(0, tab.selectedIndex - 1);
|
||||
await menu.handleInput(event);
|
||||
return;
|
||||
case "scroll-down":
|
||||
tab.selectedIndex = Math.min(Math.max(0, tab.rows.length - 1), tab.selectedIndex + 1);
|
||||
if (tab.selectedIndex >= tab.rows.length - 4) void this.loadNextPage(ctx, this.activeTab);
|
||||
await menu.handleInput(event);
|
||||
if ((menu.selectedIndex ?? 0) >= tab.rows.length - 4) void this.loadNextPage(ctx, this.activeTab);
|
||||
return;
|
||||
case "click":
|
||||
await this.activateRow(ctx, tab.rows[tab.selectedIndex]);
|
||||
await this.activateRow(ctx, menu.selectedItem ?? undefined);
|
||||
return;
|
||||
case "double-click":
|
||||
this.focus = "tabs";
|
||||
@@ -367,13 +346,7 @@ export class EvenHubStoreLayer implements Layer {
|
||||
}
|
||||
case "app":
|
||||
if (this.updatingAll) return;
|
||||
ctx.stack.push(
|
||||
new EvenHubStoreDetailLayer(row.app, {
|
||||
launchApp: this.options.launchApp,
|
||||
appendLog: this.options.appendLog,
|
||||
onInstalled: (installed) => this.onInstalled(installed),
|
||||
}),
|
||||
);
|
||||
ctx.stack.push(new EvenHubStoreDetailLayer(row.app, this.detailOptions()));
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -391,13 +364,7 @@ export class EvenHubStoreLayer implements Layer {
|
||||
}
|
||||
this.pendingPackage = null;
|
||||
stack.clearToBase();
|
||||
stack.push(
|
||||
new EvenHubStoreDetailLayer(placeholderStoreApp(installed), {
|
||||
launchApp: this.options.launchApp,
|
||||
appendLog: this.options.appendLog,
|
||||
onInstalled: (result) => this.onInstalled(result),
|
||||
}),
|
||||
);
|
||||
stack.push(new EvenHubStoreDetailLayer(placeholderStoreApp(installed), this.detailOptions()));
|
||||
}
|
||||
|
||||
/** Dictated text becomes a search (the shell's "Type Into App" / voice input). */
|
||||
@@ -540,7 +507,7 @@ export class EvenHubStoreLayer implements Layer {
|
||||
state.rows = [{ kind: "search-query" }];
|
||||
state.loaded = !query;
|
||||
// Keep the cursor on the first result once there are some.
|
||||
state.selectedIndex = query ? 1 : 0;
|
||||
state.menu.setItems(state.rows, query ? 1 : 0);
|
||||
this.tabs.search = state;
|
||||
ctx.actions.requestRender();
|
||||
if (query) await this.loadNextPage(ctx, "search");
|
||||
@@ -566,8 +533,8 @@ export class EvenHubStoreLayer implements Layer {
|
||||
update.latest ? { kind: "app", app: update.latest, update } : { kind: "orphan", update },
|
||||
);
|
||||
state.rows = rows;
|
||||
state.selectedIndex = 0;
|
||||
state.scrollRow = 0;
|
||||
state.menu.setItems(rows, 0);
|
||||
state.menu.scrollTop = 0;
|
||||
state.status = "";
|
||||
state.exhausted = true;
|
||||
} catch (error) {
|
||||
@@ -614,6 +581,8 @@ export class EvenHubStoreLayer implements Layer {
|
||||
failures.push(`${row.app.name} (declined)`);
|
||||
}
|
||||
} catch (error) {
|
||||
// An expired session fails every remaining update the same way.
|
||||
if (this.handleAuthError(ctx, error)) return;
|
||||
const message = cleanError(error);
|
||||
this.options.appendLog(`evenhub store: update of ${row.app.packageId} failed: ${message}`);
|
||||
failures.push(row.app.name);
|
||||
@@ -643,20 +612,38 @@ export class EvenHubStoreLayer implements Layer {
|
||||
state.rows = state.rows.filter((row) =>
|
||||
row.kind === "search-query" ? true : row.kind === "app" ? row.app.packageId !== packageId : row.update.installed.packageId !== packageId,
|
||||
);
|
||||
state.selectedIndex = clamp(state.selectedIndex, 0, Math.max(0, state.rows.length - 1));
|
||||
state.menu.setItems(state.rows);
|
||||
}
|
||||
|
||||
// ---- login and phone editors --------------------------------------------
|
||||
|
||||
/** Route a rejected session back to the login form; true if it was one. */
|
||||
/**
|
||||
* Drop back to the logged-out pane when Even rejects the session (an
|
||||
* expired login); true if the error was one. Anything open above the list
|
||||
* (an app page, a permission dialog) needed the session too, so it goes.
|
||||
* The phone form is not opened: this can happen on the first load of a
|
||||
* store window restored at startup (see paint), and the pane's click
|
||||
* opens it.
|
||||
*/
|
||||
private handleAuthError(ctx: LayerContext, error: unknown): boolean {
|
||||
if (!(error instanceof EvenHubAuthenticationError)) return false;
|
||||
const message = cleanError(error);
|
||||
this.options.appendLog(`evenhub store login failed: ${message}`);
|
||||
this.enterLogin(ctx, `Login failed: ${message}`);
|
||||
this.options.appendLog(`evenhub store: session rejected: ${message}`);
|
||||
if (this.closed) return true;
|
||||
ctx.stack.clearToBase();
|
||||
this.enterLogin(ctx, `Signed out: ${message}`, false);
|
||||
return true;
|
||||
}
|
||||
|
||||
private detailOptions(): EvenHubStoreDetailOptions {
|
||||
return {
|
||||
launchApp: this.options.launchApp,
|
||||
appendLog: this.options.appendLog,
|
||||
onInstalled: (installed) => this.onInstalled(installed),
|
||||
onAuthError: (ctx, error) => this.handleAuthError(ctx, error),
|
||||
};
|
||||
}
|
||||
|
||||
private enterLogin(
|
||||
ctx: LayerContext,
|
||||
status = "Sign in to browse public apps.",
|
||||
@@ -710,13 +697,11 @@ export class EvenHubStoreLayer implements Layer {
|
||||
const email = evenHubLoginEmailSetting.get();
|
||||
const password = evenHubLoginPasswordSetting.get();
|
||||
if (!email.trim() || !password) {
|
||||
// The phone editor has no Cancel control, so submitting an untouched form
|
||||
// with the keyboard's Done key is the only way someone can back out of
|
||||
// it. Reopening the editor there makes that a loop with no exit, so a
|
||||
// wholly empty submission is treated as a cancel and leaves the editor
|
||||
// closed -- the login pane still says how to get back in, and a click
|
||||
// reopens the editor. A half-filled form is a real mistake, so that
|
||||
// still reopens with the message.
|
||||
// A wholly empty submission (the keyboard's Done key on an untouched
|
||||
// form) is treated as a cancel and leaves the editor closed -- the login
|
||||
// pane still says how to get back in, and a click reopens the editor.
|
||||
// A half-filled form is a real mistake, so that still reopens with the
|
||||
// message.
|
||||
const cancelled = !email.trim() && !password;
|
||||
this.enterLogin(
|
||||
ctx,
|
||||
@@ -755,6 +740,42 @@ export class EvenHubStoreLayer implements Layer {
|
||||
}
|
||||
}
|
||||
|
||||
/** A list row's title (with its shade) and dim detail line. */
|
||||
function rowText(row: StoreRow): { title: string; detail: string; titleValue: number } {
|
||||
switch (row.kind) {
|
||||
case "search-query": {
|
||||
const query = evenHubSearchQuerySetting.get();
|
||||
return {
|
||||
title: query ? `Search: ${query}` : "Search: (enter a query)",
|
||||
detail: "Click to type on the phone, or use voice input.",
|
||||
titleValue: 200,
|
||||
};
|
||||
}
|
||||
case "orphan":
|
||||
return {
|
||||
title: row.update.installed.name,
|
||||
detail: row.update.error
|
||||
? `Package missing · store lookup failed: ${row.update.error}`
|
||||
: "Package missing · not in the store · click to forget",
|
||||
titleValue: 170,
|
||||
};
|
||||
case "app": {
|
||||
const { app, update } = row;
|
||||
if (!update) {
|
||||
return {
|
||||
title: app.name,
|
||||
detail: app.tagline || `${app.creatorName} · ${formatCount(app.installCount)} installs`,
|
||||
titleValue: 215,
|
||||
};
|
||||
}
|
||||
const detail = update.packageMissing
|
||||
? `Package missing · reinstall ${app.version ? `version ${app.version}` : ""}`.trim()
|
||||
: `Version ${update.installed.version} installed · ${app.version} available`;
|
||||
return { title: app.name, detail, titleValue: 215 };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** A registry entry dressed as a store record, for pages opened before the store record arrives. */
|
||||
function placeholderStoreApp(installed: InstalledEvenHubApp): EvenHubStoreApp {
|
||||
return {
|
||||
|
||||
@@ -4,7 +4,8 @@ import { GrayImage, type UiFont } from "../../graphics/image";
|
||||
import { renderIcon, type IconName } from "../../graphics/icons";
|
||||
import { clamp } from "../../util/numeric-util";
|
||||
import { drawListScrollbar, drawSelectionHighlight, scrollToKeepSelectionVisible, type MenuItem } from "../../ui/menu";
|
||||
import { iconGridMinRowHeight, tightRowHeight } from "../../ui/metrics";
|
||||
import { Menu, type MenuDrawArgs } from "../../ui/menu-core";
|
||||
import { centeredTextY, iconGridMinRowHeight, tightRowHeight } from "../../ui/metrics";
|
||||
import { ConfigSettingEnum } from "../../ui/dashboard-settings";
|
||||
import { getStringSetting, setStringSetting } from "../../native/settings-store";
|
||||
import {
|
||||
@@ -20,6 +21,8 @@ import { Layer, type LayerContext } from "../../ui/layers";
|
||||
import { shell } from "../../ui/shell/shell";
|
||||
|
||||
const LIST_X = 20;
|
||||
/** List view: the selection box extends this far left and right of the row text. */
|
||||
const LIST_HIGHLIGHT_PAD = 6;
|
||||
|
||||
/** Header: one line holding both the title and the current path. */
|
||||
function headerHeight(font: UiFont): number {
|
||||
@@ -133,7 +136,14 @@ export class FileBrowserLayer implements Layer {
|
||||
private entries: DirectoryEntry[] | null = null;
|
||||
private listingFailed = false;
|
||||
|
||||
private listIndex = 0;
|
||||
/** List view. Its selected index is the flat-row index both views translate through (setViewMode). */
|
||||
private readonly listMenu = new Menu<BrowserItem>({
|
||||
wrap: false,
|
||||
rowGap: 1,
|
||||
highlight: { radius: 4 },
|
||||
getHeight: () => tightRowHeight(getDefaultSmallFont()),
|
||||
draw: (args) => this.drawListRow(args),
|
||||
});
|
||||
private selectedRow = 0;
|
||||
private selectedCol = 0;
|
||||
private iconMode: IconMode = "row";
|
||||
@@ -162,25 +172,20 @@ export class FileBrowserLayer implements Layer {
|
||||
|
||||
private paintList(image: GrayImage, font: UiFont, rows: BrowserItem[], ctx: LayerContext): void {
|
||||
const { width, height } = ctx.stack.getBaseSize();
|
||||
this.listIndex = clamp(this.listIndex, 0, Math.max(0, rows.length - 1));
|
||||
|
||||
const rowH = tightRowHeight(font);
|
||||
const headerH = headerHeight(font);
|
||||
const listHeight = height - headerH - 4;
|
||||
const visibleRows = Math.max(1, (listHeight / rowH) | 0);
|
||||
this.scrollRow = scrollToKeepSelectionVisible(this.scrollRow, this.listIndex, visibleRows, rows.length);
|
||||
this.listMenu.setItems(rows);
|
||||
// Row boxes start one pixel above the header's bottom edge.
|
||||
this.listMenu.paint(
|
||||
image,
|
||||
{ x: LIST_X - LIST_HIGHLIGHT_PAD, y: headerH - 1, width: width - 2 * LIST_X + 2 * LIST_HIGHLIGHT_PAD, height: height - headerH - 4 },
|
||||
ctx.stack.isFocused(),
|
||||
);
|
||||
}
|
||||
|
||||
const lastVisible = Math.min(rows.length, this.scrollRow + visibleRows);
|
||||
for (let index = this.scrollRow; index < lastVisible; index++) {
|
||||
const row = rows[index]!;
|
||||
const y = headerH + (index - this.scrollRow) * rowH;
|
||||
const selected = index === this.listIndex;
|
||||
if (selected) {
|
||||
drawSelectionHighlight(image, LIST_X - 6, y - 1, width - 2 * LIST_X + 12, rowH - 1, ctx.stack.isFocused(), 4);
|
||||
}
|
||||
const value = itemValue(row, selected);
|
||||
image.drawText(font, LIST_X, y + 1, truncateText(font, row.label, width - 2 * LIST_X), value);
|
||||
}
|
||||
private drawListRow({ image, item, x, y, width, height, selected }: MenuDrawArgs<BrowserItem>): void {
|
||||
const font = getDefaultSmallFont();
|
||||
const label = truncateText(font, item.label, width - 2 * LIST_HIGHLIGHT_PAD);
|
||||
image.drawText(font, x + LIST_HIGHLIGHT_PAD, centeredTextY(font, y, height), label, itemValue(item, selected));
|
||||
}
|
||||
|
||||
private paintIcons(image: GrayImage, font: UiFont, rows: BrowserItem[], ctx: LayerContext): void {
|
||||
@@ -276,16 +281,14 @@ export class FileBrowserLayer implements Layer {
|
||||
}
|
||||
|
||||
private async handleListInput(event: InputEvent, ctx: LayerContext): Promise<void> {
|
||||
const rows = this.flatRows();
|
||||
this.listMenu.setItems(this.flatRows());
|
||||
switch (event.type) {
|
||||
case "scroll-up":
|
||||
this.listIndex = Math.max(0, this.listIndex - 1);
|
||||
return;
|
||||
case "scroll-down":
|
||||
this.listIndex = Math.min(Math.max(0, rows.length - 1), this.listIndex + 1);
|
||||
await this.listMenu.handleInput(event);
|
||||
return;
|
||||
case "click": {
|
||||
const row = rows[clamp(this.listIndex, 0, Math.max(0, rows.length - 1))];
|
||||
const row = this.listMenu.selectedItem;
|
||||
if (row) await this.activateItem(row, ctx);
|
||||
return;
|
||||
}
|
||||
@@ -461,8 +464,7 @@ export class FileBrowserLayer implements Layer {
|
||||
label: isBookmarked(path) ? `Remove bookmark: ${name}` : `Bookmark: ${name}`,
|
||||
onSelect: (ctx) => {
|
||||
toggleBookmark(path);
|
||||
const rows = this.flatRows();
|
||||
this.listIndex = clamp(this.listIndex, 0, Math.max(0, rows.length - 1));
|
||||
this.listMenu.setItems(this.flatRows());
|
||||
ctx.stack.pop();
|
||||
},
|
||||
});
|
||||
@@ -475,7 +477,8 @@ export class FileBrowserLayer implements Layer {
|
||||
const rows = this.flatRows();
|
||||
if (!rows.length) return null;
|
||||
if (filesViewModeSetting.get() === "list") {
|
||||
return rows[clamp(this.listIndex, 0, rows.length - 1)] ?? null;
|
||||
this.listMenu.setItems(rows);
|
||||
return this.listMenu.selectedItem;
|
||||
}
|
||||
const iconRows = buildIconRows(rows);
|
||||
const row = iconRows[clamp(this.selectedRow, 0, iconRows.length - 1)];
|
||||
@@ -489,20 +492,23 @@ export class FileBrowserLayer implements Layer {
|
||||
if (filesViewModeSetting.get() === mode) return;
|
||||
const rows = this.flatRows();
|
||||
const iconRows = buildIconRows(rows);
|
||||
this.listMenu.setItems(rows);
|
||||
if (mode === "list") {
|
||||
// Carry the icon-grid selection into the flat list (clamped column when
|
||||
// only a row was selected).
|
||||
const row = iconRows[clamp(this.selectedRow, 0, Math.max(0, iconRows.length - 1))];
|
||||
if (row) {
|
||||
this.listIndex =
|
||||
row.kind === "special" ? row.flatIndex : row.firstIndex + clamp(this.selectedCol, 0, row.items.length - 1);
|
||||
this.listMenu.select(
|
||||
row.kind === "special" ? row.flatIndex : row.firstIndex + clamp(this.selectedCol, 0, row.items.length - 1),
|
||||
);
|
||||
}
|
||||
} else {
|
||||
this.setGridSelectionFromFlatIndex(rows, clamp(this.listIndex, 0, Math.max(0, rows.length - 1)));
|
||||
this.setGridSelectionFromFlatIndex(rows, this.listMenu.selectedIndex ?? 0);
|
||||
this.iconMode = "row";
|
||||
}
|
||||
filesViewModeSetting.set(mode);
|
||||
this.scrollRow = 0;
|
||||
this.listMenu.scrollTop = 0;
|
||||
}
|
||||
|
||||
private async activateItem(item: BrowserItem, ctx: LayerContext): Promise<void> {
|
||||
@@ -548,7 +554,8 @@ export class FileBrowserLayer implements Layer {
|
||||
}
|
||||
|
||||
private resetSelection(): void {
|
||||
this.listIndex = 0;
|
||||
this.listMenu.select(0);
|
||||
this.listMenu.scrollTop = 0;
|
||||
this.selectedRow = 0;
|
||||
this.selectedCol = 0;
|
||||
this.iconMode = "row";
|
||||
@@ -560,7 +567,9 @@ export class FileBrowserLayer implements Layer {
|
||||
const rows = this.flatRows();
|
||||
const index = rows.findIndex((row) => row.kind === "entry" && row.entry.path === path);
|
||||
if (index < 0) return;
|
||||
this.listIndex = index;
|
||||
// Items first, then the selection, so the viewport scrolls minimally from the top.
|
||||
this.listMenu.setItems(rows);
|
||||
this.listMenu.select(index);
|
||||
this.setGridSelectionFromFlatIndex(rows, index);
|
||||
}
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@ import { wrapText } from "../../graphics/textwrap";
|
||||
import type { DirectoryEntry } from "../../native/file-access";
|
||||
import { GESTURE_DOUBLE_CLICK, type InputEvent } from "../../ui/gestures";
|
||||
import { Layer, type LayerContext, type PaintBelow } from "../../ui/layers";
|
||||
import { drawSelectionHighlight } from "../../ui/menu";
|
||||
import { Menu, type MenuDrawArgs } from "../../ui/menu-core";
|
||||
import { LIST_ROW_TEXT_INSET, lineStep, listRowHeight } from "../../ui/metrics";
|
||||
|
||||
const DIALOG_X = 8;
|
||||
@@ -25,12 +25,21 @@ export type FileInfoAction = {
|
||||
* double-click closes it.
|
||||
*/
|
||||
export class FileInfoDialogLayer implements Layer {
|
||||
private selectedIndex = 0;
|
||||
private readonly menu: Menu<FileInfoAction>;
|
||||
|
||||
constructor(
|
||||
private readonly entry: DirectoryEntry,
|
||||
private readonly actions: FileInfoAction[],
|
||||
) {}
|
||||
) {
|
||||
this.menu = new Menu<FileInfoAction>({
|
||||
items: actions,
|
||||
wrap: true,
|
||||
rowGap: 1,
|
||||
highlight: { radius: 8 },
|
||||
getHeight: () => listRowHeight(getDefaultSmallFont()),
|
||||
draw: (args) => drawActionRow(args),
|
||||
});
|
||||
}
|
||||
|
||||
paint(ctx: LayerContext, paintBelow: PaintBelow): GrayImage {
|
||||
const font = getDefaultSmallFont();
|
||||
@@ -69,32 +78,23 @@ export class FileInfoDialogLayer implements Layer {
|
||||
image.drawText(font, DIALOG_X + PADDING + 2, y + 3, `${GESTURE_DOUBLE_CLICK} close`, 110);
|
||||
return image;
|
||||
}
|
||||
const focused = ctx.stack.isFocused();
|
||||
for (let index = 0; index < this.actions.length; index++) {
|
||||
const rowY = y + index * rowH;
|
||||
const selected = index === this.selectedIndex;
|
||||
if (selected) {
|
||||
drawSelectionHighlight(image, DIALOG_X + 12, rowY, DIALOG_WIDTH - 24, rowH - 1, focused, 8);
|
||||
}
|
||||
image.drawText(font, DIALOG_X + 22, rowY + LIST_ROW_TEXT_INSET, this.actions[index]!.label, selected ? 255 : 200);
|
||||
}
|
||||
// The rows fill the rest of the dialog (and scroll if a clamped dialog is too short).
|
||||
this.menu.paint(
|
||||
image,
|
||||
{ x: DIALOG_X + 12, y, width: DIALOG_WIDTH - 24, height: Math.max(0, DIALOG_Y + height - PADDING - y) },
|
||||
ctx.stack.isFocused(),
|
||||
);
|
||||
return image;
|
||||
}
|
||||
|
||||
async handleInput(event: InputEvent, ctx: LayerContext): Promise<void> {
|
||||
switch (event.type) {
|
||||
case "scroll-up":
|
||||
if (this.actions.length) {
|
||||
this.selectedIndex = (this.selectedIndex + this.actions.length - 1) % this.actions.length;
|
||||
}
|
||||
return;
|
||||
case "scroll-down":
|
||||
if (this.actions.length) {
|
||||
this.selectedIndex = (this.selectedIndex + 1) % this.actions.length;
|
||||
}
|
||||
await this.menu.handleInput(event);
|
||||
return;
|
||||
case "click":
|
||||
await this.actions[this.selectedIndex]?.onSelect(ctx);
|
||||
await this.menu.selectedItem?.onSelect(ctx);
|
||||
return;
|
||||
case "double-click":
|
||||
ctx.stack.pop();
|
||||
@@ -105,6 +105,10 @@ export class FileInfoDialogLayer implements Layer {
|
||||
}
|
||||
}
|
||||
|
||||
function drawActionRow({ image, item, x, y, selected }: MenuDrawArgs<FileInfoAction>): void {
|
||||
image.drawText(getDefaultSmallFont(), x + 10, y + LIST_ROW_TEXT_INSET, item.label, selected ? 255 : 200);
|
||||
}
|
||||
|
||||
function formatSize(bytes: number): string {
|
||||
if (bytes < 1024) return `${bytes} B`;
|
||||
if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`;
|
||||
|
||||
@@ -25,6 +25,7 @@
|
||||
* focus to the sidebar) pauses, as does backgrounding or screen-off.
|
||||
*/
|
||||
import "@nativescript/core/globals";
|
||||
import { finishWorkerShutdown } from "../../ui/shell/worker-lifecycle";
|
||||
import { GrayImage } from "../../graphics/image";
|
||||
import { flattenPlanesWithDraws, planesFingerprint, type Plane } from "../../graphics/plane";
|
||||
import { prepareFrameDraws } from "../../graphics/glyph-wire";
|
||||
@@ -180,6 +181,12 @@ post({ type: "worker-ready" });
|
||||
global.onmessage = (event: { data: WorkerAppMessage }) => {
|
||||
const message = event.data;
|
||||
switch (message.type) {
|
||||
case "check-idle":
|
||||
post({ type: "worker-idle" });
|
||||
break;
|
||||
case "shutdown":
|
||||
finishWorkerShutdown();
|
||||
break;
|
||||
case "open-window": {
|
||||
const window: FlappyWindow = {
|
||||
windowId: message.windowId,
|
||||
@@ -761,17 +768,19 @@ function renderAndSubmit(window: FlappyWindow, inputFrameId: number): void {
|
||||
}
|
||||
const { image, draws } = frameTimings.span(frameId, "flatten", () => flattenPlanesWithDraws(planes));
|
||||
const buffer = frameTimings.span(frameId, "to8bpp", () => image.to8bppBuffer());
|
||||
communicator.submitSurfaceFrame(
|
||||
buffer.buffer,
|
||||
window.surfaceId,
|
||||
0,
|
||||
0,
|
||||
image.width,
|
||||
image.height,
|
||||
fingerprint,
|
||||
paintMs,
|
||||
frameId,
|
||||
frameTimings.span(frameId, "prepareFrameDraws", () => prepareFrameDraws(draws)),
|
||||
frameTimings.span(frameId, "submitSurfaceFrame", () =>
|
||||
communicator.submitSurfaceFrame(
|
||||
buffer.buffer,
|
||||
window.surfaceId,
|
||||
0,
|
||||
0,
|
||||
image.width,
|
||||
image.height,
|
||||
fingerprint,
|
||||
paintMs,
|
||||
frameId,
|
||||
frameTimings.span(frameId, "prepareFrameDraws", () => prepareFrameDraws(draws)),
|
||||
)
|
||||
);
|
||||
window.lastSubmittedFingerprint = fingerprint;
|
||||
} catch (error) {
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
* Safe cards auto-play to the foundations after every move.
|
||||
*/
|
||||
import "@nativescript/core/globals";
|
||||
import { finishWorkerShutdown } from "../../ui/shell/worker-lifecycle";
|
||||
import { GrayImage } from "../../graphics/image";
|
||||
import { flattenPlanesWithDraws, planesFingerprint, singlePlane, type Plane } from "../../graphics/plane";
|
||||
import { prepareFrameDraws } from "../../graphics/glyph-wire";
|
||||
@@ -144,6 +145,12 @@ post({ type: "worker-ready" });
|
||||
global.onmessage = (event: { data: WorkerAppMessage }) => {
|
||||
const message = event.data;
|
||||
switch (message.type) {
|
||||
case "check-idle":
|
||||
post({ type: "worker-idle" });
|
||||
break;
|
||||
case "shutdown":
|
||||
finishWorkerShutdown();
|
||||
break;
|
||||
case "open-window": {
|
||||
const window: FreecellWindow = {
|
||||
windowId: message.windowId,
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
import { getStringSetting } from "../../native/settings-store";
|
||||
import { QUADRANT_LAYOUT, SIX_SLOT_LAYOUT } from "./layout";
|
||||
|
||||
/** Configuration-only query usable by workers without loading widget/UI modules. */
|
||||
export function isTerminalWidgetConfigured(): boolean {
|
||||
const layout = getStringSetting("glanceboard.layout", "2x2") === "2x3" ? SIX_SLOT_LAYOUT : QUADRANT_LAYOUT;
|
||||
// Both layouts store their slots under the original quadrants key. None
|
||||
// of the default slots needs a worker, so absent keys cannot retain one.
|
||||
return layout.slots.some((_, index) =>
|
||||
getStringSetting(`glanceboard.${QUADRANT_LAYOUT.id}.slot.${index}`, "none") === "terminal");
|
||||
}
|
||||
@@ -3,15 +3,17 @@ import { truncateText, wrapText } from "../../graphics/textwrap";
|
||||
import { getDefaultSmallFont } from "../../graphics/ui-fonts";
|
||||
import { enumSettingMenuItem, onAnySettingChanged, toggleSettingMenuItem } from "../../ui/dashboard-settings";
|
||||
import { type InputEvent } from "../../ui/gestures";
|
||||
import { type Layer, type LayerContext, type PaintBelow } from "../../ui/layers";
|
||||
import { drawSelectionHighlight, MenuLayer, openModalMenu, type MenuItem } from "../../ui/menu";
|
||||
import { LIST_ROW_TEXT_INSET, lineStep } from "../../ui/metrics";
|
||||
import { type Layer, type LayerContext } from "../../ui/layers";
|
||||
import { drawSelectionHighlight, isMenuItemDisabled, MenuLayer, openModalMenu, type MenuItem } from "../../ui/menu";
|
||||
import { Menu } from "../../ui/menu-core";
|
||||
import { LIST_ROW_TEXT_INSET, lineStep, listRowHeight } from "../../ui/metrics";
|
||||
import { createInProcessWindow, type InProcessAppOptions, type InProcessWindow } from "../../ui/shell/in-process-window";
|
||||
import { shell } from "../../ui/shell/shell";
|
||||
import { GlanceBoard } from "./board";
|
||||
import {
|
||||
clearConflictingSlots,
|
||||
glanceboardEnabledSetting,
|
||||
glanceDepthSetting,
|
||||
glanceLayout,
|
||||
glanceLayoutSetting,
|
||||
glanceShowLinesSetting,
|
||||
@@ -35,6 +37,15 @@ const PARAGRAPH_X = 24;
|
||||
const PREVIEW_MARGIN = 8;
|
||||
const PREVIEW_MIN_LINE_VALUE = 20;
|
||||
const PREVIEW_LINE_VALUE = 100;
|
||||
/** The entry menu's row boxes: left inset, gap below the paragraph, and inset from the page's bottom. */
|
||||
const HOME_MENU_X = 20;
|
||||
const HOME_MENU_TOP_GAP = 12;
|
||||
const HOME_MENU_BOTTOM_MARGIN = 8;
|
||||
/** Horizontal inset of row text (and custom row content) from its selection box. */
|
||||
const HOME_ROW_TEXT_X = 10;
|
||||
// A throwaway menu to satisfy MenuItem.onSelect's second parameter; the
|
||||
// entry page's items never use it (they act via ctx only).
|
||||
const NO_MENU = new MenuLayer(null, []);
|
||||
|
||||
const INTRO =
|
||||
"Your Glanceboard is a display for things you want to look at quickly. Starting " +
|
||||
@@ -55,25 +66,21 @@ function pageMenuLayout(width: number) {
|
||||
* a double-tap returns focus to the menu.
|
||||
*/
|
||||
class GlanceboardHomeLayer implements Layer {
|
||||
private menu: MenuLayer | null = null;
|
||||
private menuTop = 0;
|
||||
private menuHeight = 0;
|
||||
private readonly menu: Menu<MenuItem>;
|
||||
private focus: "menu" | "preview" = "menu";
|
||||
private selectedSlot = 0;
|
||||
/** The context of the paint in progress, handed to item render callbacks. */
|
||||
private paintCtx: LayerContext | null = null;
|
||||
|
||||
constructor(private readonly setPreviewVisible: (visible: boolean) => void) {}
|
||||
|
||||
private currentMenu(ctx: LayerContext, menuTop: number): MenuLayer {
|
||||
const { width, height } = ctx.stack.getBaseSize();
|
||||
if (!this.menu || this.menuTop !== menuTop || this.menuHeight !== height) {
|
||||
const half = (width / 2) | 0;
|
||||
const items: MenuItem[] = [
|
||||
constructor(private readonly setPreviewVisible: (visible: boolean) => void) {
|
||||
this.menu = new Menu<MenuItem>({
|
||||
items: [
|
||||
toggleSettingMenuItem(glanceboardEnabledSetting),
|
||||
{
|
||||
label: "Preview",
|
||||
onSelect: (menuCtx) => {
|
||||
menuCtx.stack.push(new GlancePreviewLayer(
|
||||
() => menuCtx.actions.requestRender(),
|
||||
onSelect: (ctx) => {
|
||||
ctx.stack.push(new GlancePreviewLayer(
|
||||
() => ctx.actions.requestRender(),
|
||||
() => this.setPreviewVisible(false),
|
||||
));
|
||||
this.setPreviewVisible(true);
|
||||
@@ -87,17 +94,32 @@ class GlanceboardHomeLayer implements Layer {
|
||||
},
|
||||
{
|
||||
label: "Settings",
|
||||
onSelect: (menuCtx) => menuCtx.stack.push(new GlanceSettingsLayer()),
|
||||
onSelect: openGlanceSettings,
|
||||
},
|
||||
];
|
||||
this.menuTop = menuTop;
|
||||
this.menuHeight = height;
|
||||
this.menu = new MenuLayer(null, items, {
|
||||
x: 8, y: menuTop, width: half - 16,
|
||||
showBorder: false, minHeight: 0, maxHeight: height - menuTop,
|
||||
});
|
||||
}
|
||||
return this.menu;
|
||||
],
|
||||
wrap: true,
|
||||
rowGap: 1,
|
||||
getHeight: () => listRowHeight(getDefaultSmallFont()),
|
||||
draw: ({ image, item, x, y, width, height, selected }) => {
|
||||
const disabled = isMenuItemDisabled(item);
|
||||
if (item.render) {
|
||||
item.render({
|
||||
image,
|
||||
x: x + HOME_ROW_TEXT_X,
|
||||
y,
|
||||
width: width - 2 * HOME_ROW_TEXT_X,
|
||||
height: height - 2,
|
||||
selected,
|
||||
disabled,
|
||||
text: item.label,
|
||||
ctx: this.paintCtx!,
|
||||
});
|
||||
} else {
|
||||
image.drawText(getDefaultSmallFont(), x + HOME_ROW_TEXT_X, y + LIST_ROW_TEXT_INSET, item.label,
|
||||
disabled ? 70 : selected ? 255 : 200);
|
||||
}
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
/** Where the scaled board sits: the right half, board aspect, vertically centred. */
|
||||
@@ -128,13 +150,19 @@ class GlanceboardHomeLayer implements Layer {
|
||||
image.drawText(font, PARAGRAPH_X, y, line, 160);
|
||||
y += step;
|
||||
}
|
||||
// The menu paints onto this page below the paragraph (its fill is raster,
|
||||
// so the deferred paragraph glyphs above stay untouched either way).
|
||||
// While the preview has focus the menu keeps its outline-only selection.
|
||||
const menu = this.currentMenu(ctx, y + 4);
|
||||
const menuFocused = ctx.stack.isFocused() && this.focus === "menu";
|
||||
const menuCtx: LayerContext = { ...ctx, stack: Object.assign(Object.create(ctx.stack), { isFocused: () => menuFocused }) };
|
||||
menu.paint(menuCtx, () => image);
|
||||
// The menu sits below the paragraph in the left half. While the preview
|
||||
// has focus the menu keeps its outline-only selection.
|
||||
const menuTop = y + HOME_MENU_TOP_GAP;
|
||||
const menuWidth = half - 2 * HOME_MENU_X;
|
||||
const menuHeight = Math.max(0, height - menuTop - HOME_MENU_BOTTOM_MARGIN);
|
||||
this.paintCtx = ctx;
|
||||
try {
|
||||
this.menu.paint(image, { x: HOME_MENU_X, y: menuTop, width: menuWidth, height: menuHeight },
|
||||
ctx.stack.isFocused() && this.focus === "menu");
|
||||
} finally {
|
||||
this.paintCtx = null;
|
||||
}
|
||||
this.menu.drawScrollbar(image, HOME_MENU_X + menuWidth + 5, menuTop, menuHeight - 4);
|
||||
|
||||
this.selectedSlot = Math.min(this.selectedSlot, glanceLayout().slots.length - 1);
|
||||
const preview = this.previewRect(width, height);
|
||||
@@ -173,7 +201,12 @@ class GlanceboardHomeLayer implements Layer {
|
||||
shell.yieldFocusToSidebar();
|
||||
return;
|
||||
}
|
||||
await this.currentMenu(ctx, this.menuTop).handleInput(event, ctx);
|
||||
if (event.type === "click") {
|
||||
const item = this.menu.selectedItem;
|
||||
if (item && !isMenuItemDisabled(item)) await item.onSelect(ctx, NO_MENU);
|
||||
return;
|
||||
}
|
||||
await this.menu.handleInput(event);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -265,32 +298,17 @@ function openSlotPicker(ctx: LayerContext, slotIndex: number): void {
|
||||
openModalMenu(ctx, setting.label, items, Math.max(0, setting.values.indexOf(current)));
|
||||
}
|
||||
|
||||
/** Settings: layout, sleep gestures, duration, and slot lines. */
|
||||
class GlanceSettingsLayer implements Layer {
|
||||
private menu: MenuLayer | null = null;
|
||||
|
||||
private currentMenu(ctx: LayerContext): MenuLayer {
|
||||
if (!this.menu) {
|
||||
const { width } = ctx.stack.getBaseSize();
|
||||
const items: MenuItem[] = [
|
||||
enumSettingMenuItem(glanceLayoutSetting),
|
||||
enumSettingMenuItem(glanceTapDurationSetting),
|
||||
toggleSettingMenuItem(glanceShowOnLongPressSetting),
|
||||
toggleSettingMenuItem(glanceShowOnHeadTiltSetting),
|
||||
toggleSettingMenuItem(glanceShowLinesSetting),
|
||||
];
|
||||
this.menu = new MenuLayer("Glanceboard settings", items, pageMenuLayout(width));
|
||||
}
|
||||
return this.menu;
|
||||
}
|
||||
|
||||
paint(ctx: LayerContext, paintBelow: PaintBelow): GrayImage {
|
||||
return this.currentMenu(ctx).paint(ctx, paintBelow);
|
||||
}
|
||||
|
||||
async handleInput(event: InputEvent, ctx: LayerContext): Promise<void> {
|
||||
await this.currentMenu(ctx).handleInput(event, ctx);
|
||||
}
|
||||
/** Settings: layout, sleep gestures, duration, slot lines, and stereo depth. */
|
||||
function openGlanceSettings(ctx: LayerContext): void {
|
||||
const items: MenuItem[] = [
|
||||
enumSettingMenuItem(glanceLayoutSetting),
|
||||
enumSettingMenuItem(glanceTapDurationSetting),
|
||||
toggleSettingMenuItem(glanceShowOnLongPressSetting),
|
||||
toggleSettingMenuItem(glanceShowOnHeadTiltSetting),
|
||||
toggleSettingMenuItem(glanceShowLinesSetting),
|
||||
enumSettingMenuItem(glanceDepthSetting),
|
||||
];
|
||||
ctx.stack.push(new MenuLayer("Glanceboard settings", items, pageMenuLayout(ctx.stack.getBaseSize().width)));
|
||||
}
|
||||
|
||||
/** The live board in the window; any click or double-click returns to the list. */
|
||||
|
||||
@@ -103,6 +103,31 @@ export const glanceShowOnHeadTiltSetting = new ConfigSettingBoolean({
|
||||
description: "Tilting your head up while asleep shows the Glanceboard instead of waking the regular UI.",
|
||||
});
|
||||
|
||||
const DEPTH_VALUES = ["-64", "-48", "-32", "-16", "0", "16", "32", "48", "64"] as const;
|
||||
export type GlanceDepth = (typeof DEPTH_VALUES)[number];
|
||||
|
||||
/**
|
||||
* Stereo depth of the board, in the firmware's depth units: the lenses shift
|
||||
* it by half this many pixels each, in opposite directions.
|
||||
*/
|
||||
export const glanceDepthSetting = new ConfigSettingEnum<GlanceDepth>({
|
||||
id: "glanceboard-depth",
|
||||
label: "Depth",
|
||||
storageKey: "glanceboard.depth",
|
||||
defaultValue: "0",
|
||||
values: DEPTH_VALUES,
|
||||
formatValue: (value) => {
|
||||
const depth = Number(value);
|
||||
return depth === 0 ? "0" : depth > 0 ? `+${depth} (nearer)` : `${depth} (farther)`;
|
||||
},
|
||||
description:
|
||||
"Moves the Glanceboard nearer (positive) or farther away (negative) by shifting it in opposite directions on the two lenses.",
|
||||
});
|
||||
|
||||
export function glanceDepth(): number {
|
||||
return Number(glanceDepthSetting.get());
|
||||
}
|
||||
|
||||
/** Hairlines between the board's slots. */
|
||||
export const glanceShowLinesSetting = new ConfigSettingBoolean({
|
||||
id: "glanceboard-show-lines",
|
||||
|
||||
@@ -3,6 +3,7 @@ import { GlanceBoard } from "./board";
|
||||
import { createGlanceboardAppWindow, GLANCEBOARD_SURFACE_ID, GLANCEBOARD_WINDOW_ID } from "./glanceboard-app";
|
||||
import {
|
||||
glanceboardEnabledSetting,
|
||||
glanceDepth,
|
||||
glanceLayout,
|
||||
glanceShowOnHeadTiltSetting,
|
||||
glanceShowOnLongPressSetting,
|
||||
@@ -25,6 +26,7 @@ const glanceboardApp: AppDefinition = {
|
||||
tapTimeoutMs: glanceTapTimeoutMs,
|
||||
showOnLongPress: () => glanceShowOnLongPressSetting.get(),
|
||||
showOnHeadTilt: () => glanceShowOnHeadTiltSetting.get(),
|
||||
depth: glanceDepth,
|
||||
createBoard: (requestRender) => new GlanceBoard(requestRender),
|
||||
},
|
||||
};
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { type GrayImage } from "../../../graphics/image";
|
||||
import { GrayImage } from "../../../graphics/image";
|
||||
import { getDefaultLargeFont, getDefaultSmallFont } from "../../../graphics/ui-fonts";
|
||||
import { truncateText } from "../../../graphics/textwrap";
|
||||
import { nightscoutBridge, type NightscoutState } from "../../../native/nightscout-bridge";
|
||||
@@ -91,22 +91,6 @@ export class NightscoutWidget implements GlanceWidget {
|
||||
image.drawText(small, PAD, y, `IOB ${state.iob === null ? "--" : state.iob.toFixed(2)}`, 160);
|
||||
y += step;
|
||||
|
||||
// Breached limits only, one per line while they fit, inverted like the
|
||||
// app's warning fields. Nothing here means nothing is out of range.
|
||||
const alerts = evaluateNightscoutAlerts(state, loadNightscoutThresholds(), nowMs);
|
||||
const warnings: string[] = [];
|
||||
if (alerts.cannula) warnings.push(`CAGE ${formatAgeShortFromTimestamp(state.cageTimestampMs, nowMs)}`);
|
||||
if (alerts.cartridge) warnings.push(`Reservoir ${state.reservoirUnits === null ? "--" : Math.round(state.reservoirUnits)}U`);
|
||||
if (alerts.battery) warnings.push(`Battery ${state.batteryVoltage === null ? "--" : state.batteryVoltage.toFixed(2)}V`);
|
||||
if (alerts.loop) warnings.push(`Loop ${formatAgeShortFromTimestamp(state.loopTimestampMs, nowMs)}`);
|
||||
for (const warning of warnings) {
|
||||
if (y + small.lineHeight > image.height - 2) break;
|
||||
const text = truncateText(small, warning, READOUT_WIDTH - 4);
|
||||
image.fillRect(PAD - 2, y - 1, small.measureText(text) + 4, small.lineHeight + 2, 230);
|
||||
image.drawText(small, PAD, y, text, 1);
|
||||
y += step;
|
||||
}
|
||||
|
||||
const graphX = PAD + READOUT_WIDTH + PAD;
|
||||
drawNightscoutGraph(
|
||||
image,
|
||||
@@ -115,5 +99,32 @@ export class NightscoutWidget implements GlanceWidget {
|
||||
nowMs,
|
||||
small,
|
||||
);
|
||||
|
||||
// Breached limits only, inverted like the app's warning fields. Continue
|
||||
// at the top of a second column over the graph when the readout is full.
|
||||
const alerts = evaluateNightscoutAlerts(state, loadNightscoutThresholds(), nowMs);
|
||||
const warnings: string[] = [];
|
||||
if (alerts.cannula) warnings.push(`CAGE ${formatAgeShortFromTimestamp(state.cageTimestampMs, nowMs)}`);
|
||||
if (alerts.cartridge) warnings.push(`Reservoir ${state.reservoirUnits === null ? "--" : Math.round(state.reservoirUnits)}U`);
|
||||
if (alerts.battery) warnings.push(`Battery ${state.batteryVoltage === null ? "--" : state.batteryVoltage.toFixed(2)}V`);
|
||||
if (alerts.loop) warnings.push(`Loop ${formatAgeShortFromTimestamp(state.loopTimestampMs, nowMs)}`);
|
||||
let warningX = PAD;
|
||||
for (const warning of warnings) {
|
||||
if (y + small.lineHeight > image.height - 2) {
|
||||
warningX = graphX;
|
||||
y = PAD;
|
||||
}
|
||||
const text = truncateText(small, warning, READOUT_WIDTH - 4);
|
||||
const width = Math.ceil(small.measureText(text)) + 4;
|
||||
if (warningX === PAD) {
|
||||
image.fillRect(warningX - 2, y - 1, width, small.lineHeight + 2, 230);
|
||||
} else {
|
||||
// Graph labels are deferred glyphs: a deferred opaque background
|
||||
// covers those as well as the graph's raster lines.
|
||||
image.drawImage(new GrayImage(width, small.lineHeight + 2, 230), warningX - 2, y - 1);
|
||||
}
|
||||
image.drawText(small, warningX, y, text, 1);
|
||||
y += step;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -18,6 +18,7 @@
|
||||
* launches.
|
||||
*/
|
||||
import "@nativescript/core/globals";
|
||||
import { finishWorkerShutdown } from "../../ui/shell/worker-lifecycle";
|
||||
import { GrayImage } from "../../graphics/image";
|
||||
import { flattenPlanesWithDraws, planesFingerprint, type Plane } from "../../graphics/plane";
|
||||
import { prepareFrameDraws } from "../../graphics/glyph-wire";
|
||||
@@ -152,6 +153,12 @@ post({ type: "worker-ready" });
|
||||
global.onmessage = (event: { data: WorkerAppMessage }) => {
|
||||
const message = event.data;
|
||||
switch (message.type) {
|
||||
case "check-idle":
|
||||
post({ type: "worker-idle" });
|
||||
break;
|
||||
case "shutdown":
|
||||
finishWorkerShutdown();
|
||||
break;
|
||||
case "open-window": {
|
||||
const window: MinesweeperWindow = {
|
||||
windowId: message.windowId,
|
||||
|
||||
@@ -1,15 +1,9 @@
|
||||
import { getDefaultSmallFont } from "../../graphics/ui-fonts";
|
||||
import { GrayImage, type UiFont } from "../../graphics/image";
|
||||
import { clamp } from "../../util/numeric-util";
|
||||
import { GESTURE_CLICK, GESTURE_DOUBLE_CLICK, GESTURE_SCROLL, type InputEvent } from "../../ui/gestures";
|
||||
import {
|
||||
MenuLayer,
|
||||
drawListScrollbar,
|
||||
drawSelectionHighlight,
|
||||
drawSubmenuIndicator,
|
||||
scrollToKeepSelectionVisible,
|
||||
} from "../../ui/menu";
|
||||
import { lineStep, tightRowHeight } from "../../ui/metrics";
|
||||
import { MenuLayer, drawSubmenuIndicator } from "../../ui/menu";
|
||||
import { Menu, type MenuDrawArgs } from "../../ui/menu-core";
|
||||
import { centeredTextY, lineStep, tightRowHeight } from "../../ui/metrics";
|
||||
import {
|
||||
mediaBrowserBridge,
|
||||
type MediaBrowseItem,
|
||||
@@ -22,6 +16,8 @@ function headerHeight(font: UiFont): number {
|
||||
return 8 + lineStep(font) + font.lineHeight;
|
||||
}
|
||||
const LIST_X = 20;
|
||||
/** The selection box extends this far left and right of the row text. */
|
||||
const LIST_HIGHLIGHT_PAD = 6;
|
||||
|
||||
export type MediaBrowseOptions = {
|
||||
app: MediaBrowserApp;
|
||||
@@ -37,8 +33,8 @@ type BrowseLevel = {
|
||||
/** null while the listing is loading. */
|
||||
items: MediaBrowseItem[] | null;
|
||||
error: string;
|
||||
selectedIndex: number;
|
||||
scrollRow: number;
|
||||
/** Holds the loaded items; keeps this level's selection and scroll while a child level is open. */
|
||||
menu: Menu<MediaBrowseItem>;
|
||||
};
|
||||
|
||||
type BrowsePhase = "idle" | "connecting" | "ready" | "failed";
|
||||
@@ -97,50 +93,16 @@ export class MediaBrowseLayer implements Layer {
|
||||
return image;
|
||||
}
|
||||
|
||||
level.selectedIndex = clamp(level.selectedIndex, 0, items.length - 1);
|
||||
const listHeight = height - headerHeight(font) - font.lineHeight - 10;
|
||||
const rowH = tightRowHeight(font);
|
||||
const visibleRows = Math.max(1, (listHeight / rowH) | 0);
|
||||
level.scrollRow = scrollToKeepSelectionVisible(level.scrollRow, level.selectedIndex, visibleRows, items.length);
|
||||
|
||||
const rowWidth = width - 2 * LIST_X;
|
||||
const lastVisible = Math.min(items.length, level.scrollRow + visibleRows);
|
||||
for (let index = level.scrollRow; index < lastVisible; index++) {
|
||||
const item = items[index]!;
|
||||
const y = headerHeight(font) + (index - level.scrollRow) * rowH;
|
||||
const selected = index === level.selectedIndex;
|
||||
const highlightX = LIST_X - 6;
|
||||
const highlightY = y - 1;
|
||||
const highlightWidth = rowWidth + 12;
|
||||
const highlightHeight = rowH - 1;
|
||||
if (selected) {
|
||||
drawSelectionHighlight(image, highlightX, highlightY, highlightWidth, highlightHeight, ctx.stack.isFocused(), 4);
|
||||
}
|
||||
const value = selected ? 255 : 200;
|
||||
const title = truncateRight(font, item.title || "(untitled)", rowWidth - 16);
|
||||
image.drawText(font, LIST_X, y + 1, title, value);
|
||||
if (item.subtitle) {
|
||||
const titleWidth = font.measureText(title);
|
||||
const subtitleWidth = font.measureText(item.subtitle);
|
||||
if (titleWidth + subtitleWidth + 24 <= rowWidth - 16) {
|
||||
image.drawText(font, LIST_X + rowWidth - 16 - subtitleWidth, y + 1, item.subtitle, 110);
|
||||
}
|
||||
}
|
||||
if (item.browsable) {
|
||||
drawSubmenuIndicator(image, font, highlightX, highlightY, highlightWidth, highlightHeight, selected ? value : 110);
|
||||
}
|
||||
}
|
||||
if (items.length > visibleRows) {
|
||||
drawListScrollbar(
|
||||
image,
|
||||
width - 12,
|
||||
headerHeight(font),
|
||||
visibleRows * rowH - 4,
|
||||
level.scrollRow,
|
||||
visibleRows,
|
||||
items.length,
|
||||
);
|
||||
}
|
||||
const listTop = headerHeight(font);
|
||||
const listHeight = height - listTop - font.lineHeight - 10;
|
||||
const visibleRows = Math.max(1, (listHeight / tightRowHeight(font)) | 0);
|
||||
// Row boxes start one pixel above the header's bottom edge.
|
||||
level.menu.paint(
|
||||
image,
|
||||
{ x: LIST_X - LIST_HIGHLIGHT_PAD, y: listTop - 1, width: width - 2 * LIST_X + 2 * LIST_HIGHLIGHT_PAD, height: listHeight },
|
||||
ctx.stack.isFocused(),
|
||||
);
|
||||
level.menu.drawScrollbar(image, width - 12, listTop, visibleRows * tightRowHeight(font) - 4);
|
||||
|
||||
image.drawText(
|
||||
font,
|
||||
@@ -175,13 +137,11 @@ export class MediaBrowseLayer implements Layer {
|
||||
if (!level || !items || !items.length) return;
|
||||
switch (event.type) {
|
||||
case "scroll-up":
|
||||
level.selectedIndex = Math.max(0, level.selectedIndex - 1);
|
||||
return;
|
||||
case "scroll-down":
|
||||
level.selectedIndex = Math.min(items.length - 1, level.selectedIndex + 1);
|
||||
await level.menu.handleInput(event);
|
||||
return;
|
||||
case "click": {
|
||||
const item = items[clamp(level.selectedIndex, 0, items.length - 1)];
|
||||
const item = level.menu.selectedItem;
|
||||
if (!item) return;
|
||||
if (item.browsable && item.playable) {
|
||||
ctx.stack.push(
|
||||
@@ -233,18 +193,46 @@ export class MediaBrowseLayer implements Layer {
|
||||
}
|
||||
|
||||
private descendInto(ctx: LayerContext, item: MediaBrowseItem): void {
|
||||
const level: BrowseLevel = {
|
||||
parentId: item.mediaId,
|
||||
title: item.title || "(untitled)",
|
||||
items: null,
|
||||
error: "",
|
||||
selectedIndex: 0,
|
||||
scrollRow: 0,
|
||||
};
|
||||
const level = this.createLevel(item.mediaId, item.title || "(untitled)");
|
||||
this.levels.push(level);
|
||||
this.loadLevel(ctx, level);
|
||||
}
|
||||
|
||||
private createLevel(parentId: string, title: string): BrowseLevel {
|
||||
return {
|
||||
parentId,
|
||||
title,
|
||||
items: null,
|
||||
error: "",
|
||||
menu: new Menu<MediaBrowseItem>({
|
||||
wrap: false,
|
||||
rowGap: 1,
|
||||
highlight: { radius: 4 },
|
||||
getHeight: () => tightRowHeight(getDefaultSmallFont()),
|
||||
draw: (args) => this.drawItem(args),
|
||||
}),
|
||||
};
|
||||
}
|
||||
|
||||
private drawItem({ image, item, x, y, width, height, selected }: MenuDrawArgs<MediaBrowseItem>): void {
|
||||
const font = getDefaultSmallFont();
|
||||
const value = selected ? 255 : 200;
|
||||
const textWidth = width - 2 * LIST_HIGHLIGHT_PAD - 16;
|
||||
const textY = centeredTextY(font, y, height);
|
||||
const title = truncateRight(font, item.title || "(untitled)", textWidth);
|
||||
image.drawText(font, x + LIST_HIGHLIGHT_PAD, textY, title, value);
|
||||
if (item.subtitle) {
|
||||
const titleWidth = font.measureText(title);
|
||||
const subtitleWidth = font.measureText(item.subtitle);
|
||||
if (titleWidth + subtitleWidth + 24 <= textWidth) {
|
||||
image.drawText(font, x + LIST_HIGHLIGHT_PAD + textWidth - subtitleWidth, textY, item.subtitle, 110);
|
||||
}
|
||||
}
|
||||
if (item.browsable) {
|
||||
drawSubmenuIndicator(image, font, x, y, width, height, selected ? value : 110);
|
||||
}
|
||||
}
|
||||
|
||||
private startConnect(ctx: LayerContext): void {
|
||||
this.phase = "connecting";
|
||||
mediaBrowserBridge
|
||||
@@ -252,14 +240,7 @@ export class MediaBrowseLayer implements Layer {
|
||||
.then((rootId) => {
|
||||
if (this.removed) return;
|
||||
this.phase = "ready";
|
||||
const root: BrowseLevel = {
|
||||
parentId: rootId,
|
||||
title: "Library",
|
||||
items: null,
|
||||
error: "",
|
||||
selectedIndex: 0,
|
||||
scrollRow: 0,
|
||||
};
|
||||
const root = this.createLevel(rootId, "Library");
|
||||
this.levels = [root];
|
||||
this.loadLevel(ctx, root);
|
||||
ctx.actions.requestRender();
|
||||
@@ -278,6 +259,7 @@ export class MediaBrowseLayer implements Layer {
|
||||
.then((items) => {
|
||||
if (this.removed) return;
|
||||
level.items = items;
|
||||
level.menu.setItems(items, 0);
|
||||
ctx.actions.requestRender();
|
||||
})
|
||||
.catch((error) => {
|
||||
|
||||
+57
-87
@@ -3,8 +3,9 @@ import { GrayImage } from "../../graphics/image";
|
||||
import { wrapText, truncateText } from "../../graphics/textwrap";
|
||||
import { clamp } from "../../util/numeric-util";
|
||||
import { directionalFallback, GESTURE_CLICK, GESTURE_DOUBLE_CLICK, GESTURE_SCROLL_DOWN, GESTURE_SCROLL_UP, type InputEvent, isDirectionalInput } from "../../ui/gestures";
|
||||
import { MenuLayer, drawSelectionHighlight, drawSubmenuIndicator } from "../../ui/menu";
|
||||
import { lineStep, tightRowHeight } from "../../ui/metrics";
|
||||
import { MenuLayer, drawSubmenuIndicator } from "../../ui/menu";
|
||||
import { Menu, type MenuDrawArgs } from "../../ui/menu-core";
|
||||
import { centeredTextY, lineStep, tightRowHeight } from "../../ui/metrics";
|
||||
import { mediaControllerBridge, type MediaControllerState, type MediaQueueItem } from "../../native/media-controller";
|
||||
import { mediaBrowserBridge, type MediaBrowserApp } from "../../native/media-browser";
|
||||
import { MediaBrowseLayer } from "./media-browse";
|
||||
@@ -28,6 +29,8 @@ const ACTION_X = 22;
|
||||
const ACTION_WIDTH = 154;
|
||||
const COLUMN_DIVIDER_X = 190;
|
||||
const QUEUE_X = 204;
|
||||
/** Horizontal inset of row text from its selection box. */
|
||||
const LIST_TEXT_INSET = 6;
|
||||
const PLAYLIST_ACTION_INDEX = 1;
|
||||
|
||||
type MusicAction =
|
||||
@@ -49,9 +52,20 @@ class MusicAppLayer implements Layer {
|
||||
// Watch swipes skip tracks: right = next, left = previous.
|
||||
readonly acceptsDirectional = true;
|
||||
private focusColumn: FocusColumn = "actions";
|
||||
private selectedActionIndex = 0;
|
||||
private selectedQueueIndex = 0;
|
||||
private queueScrollRow = 0;
|
||||
private readonly actionsMenu = new Menu<MusicAction>({
|
||||
wrap: true,
|
||||
rowGap: 1,
|
||||
highlight: { radius: 4 },
|
||||
getHeight: () => tightRowHeight(getDefaultSmallFont()),
|
||||
draw: (args) => this.drawAction(args),
|
||||
});
|
||||
private readonly queueMenu = new Menu<MediaQueueItem>({
|
||||
wrap: false,
|
||||
rowGap: 1,
|
||||
highlight: { radius: 4 },
|
||||
getHeight: () => tightRowHeight(getDefaultSmallFont()),
|
||||
draw: (args) => this.drawQueueItem(args),
|
||||
});
|
||||
private art: GrayImage | null = null;
|
||||
private artKey = "";
|
||||
|
||||
@@ -124,83 +138,49 @@ class MusicAppLayer implements Layer {
|
||||
const actions = this.buildActions(media, queue);
|
||||
this.reconcileSelection(actions, queue);
|
||||
const listHeight = height - LIST_TOP - LIST_BOTTOM_MARGIN;
|
||||
const rowH = tightRowHeight(font);
|
||||
const visibleRows = Math.max(1, (listHeight / rowH) | 0);
|
||||
if (this.selectedQueueIndex < this.queueScrollRow) {
|
||||
this.queueScrollRow = this.selectedQueueIndex;
|
||||
} else if (this.selectedQueueIndex >= this.queueScrollRow + visibleRows) {
|
||||
this.queueScrollRow = this.selectedQueueIndex - visibleRows + 1;
|
||||
}
|
||||
this.queueScrollRow = clamp(this.queueScrollRow, 0, Math.max(0, queue.length - visibleRows));
|
||||
|
||||
for (let index = 0; index < actions.length; index++) {
|
||||
const action = actions[index]!;
|
||||
const y = LIST_TOP + index * rowH;
|
||||
const selected = index === this.selectedActionIndex;
|
||||
const highlightX = ACTION_X - 6;
|
||||
const highlightY = y - 1;
|
||||
const highlightWidth = ACTION_WIDTH + 8;
|
||||
const highlightHeight = rowH - 1;
|
||||
if (selected) {
|
||||
drawSelectionHighlight(
|
||||
image,
|
||||
highlightX,
|
||||
highlightY,
|
||||
highlightWidth,
|
||||
highlightHeight,
|
||||
ctx.stack.isFocused() && this.focusColumn === "actions",
|
||||
4,
|
||||
);
|
||||
}
|
||||
const value = !action.enabled ? (selected ? 130 : 90) : selected ? 255 : 200;
|
||||
image.drawText(font, ACTION_X, y + 1, truncateText(font, action.label, ACTION_WIDTH - 16), value);
|
||||
if ((action.kind === "playlist" || action.kind === "browse") && action.enabled) {
|
||||
drawSubmenuIndicator(image, font, highlightX, highlightY, highlightWidth, highlightHeight, value);
|
||||
}
|
||||
}
|
||||
const focused = ctx.stack.isFocused();
|
||||
// Row boxes start one pixel above LIST_TOP, level with the column divider.
|
||||
this.actionsMenu.paint(
|
||||
image,
|
||||
{ x: ACTION_X - LIST_TEXT_INSET, y: LIST_TOP - 1, width: ACTION_WIDTH + 2 * LIST_TEXT_INSET - 4, height: listHeight },
|
||||
focused && this.focusColumn === "actions",
|
||||
);
|
||||
|
||||
image.drawLine(COLUMN_DIVIDER_X, LIST_TOP - 3, COLUMN_DIVIDER_X, height - LIST_BOTTOM_MARGIN - 3, 45);
|
||||
if (!queue.length) {
|
||||
image.drawText(font, QUEUE_X, LIST_TOP + 1, "Playlist unavailable", 90);
|
||||
} else {
|
||||
const queueWidth = width - QUEUE_X - 20;
|
||||
const lastVisible = Math.min(queue.length, this.queueScrollRow + visibleRows);
|
||||
for (let index = this.queueScrollRow; index < lastVisible; index++) {
|
||||
const item = queue[index]!;
|
||||
const y = LIST_TOP + (index - this.queueScrollRow) * rowH;
|
||||
const selected = index === this.selectedQueueIndex;
|
||||
if (selected) {
|
||||
drawSelectionHighlight(
|
||||
image,
|
||||
QUEUE_X - 6,
|
||||
y - 1,
|
||||
queueWidth + 8,
|
||||
rowH - 1,
|
||||
ctx.stack.isFocused() && this.focusColumn === "playlist",
|
||||
4,
|
||||
);
|
||||
}
|
||||
const label = `${item.active ? "> " : " "}${item.title || "(untitled)"}`;
|
||||
image.drawText(font, QUEUE_X, y + 1, truncateText(font, label, queueWidth - 4), selected ? 255 : 200);
|
||||
}
|
||||
if (queue.length > visibleRows) {
|
||||
const trackHeight = visibleRows * rowH - 4;
|
||||
const trackX = width - 12;
|
||||
image.fillRect(trackX, LIST_TOP, 3, trackHeight, 30);
|
||||
const thumbHeight = Math.max(8, (trackHeight * visibleRows / queue.length) | 0);
|
||||
const maxScrollRow = queue.length - visibleRows;
|
||||
const thumbY = LIST_TOP + (((trackHeight - thumbHeight) * this.queueScrollRow / maxScrollRow) | 0);
|
||||
image.fillRect(trackX, thumbY, 3, thumbHeight, 120);
|
||||
}
|
||||
this.queueMenu.paint(
|
||||
image,
|
||||
{ x: QUEUE_X - LIST_TEXT_INSET, y: LIST_TOP - 1, width: queueWidth + 2 * LIST_TEXT_INSET - 4, height: listHeight },
|
||||
focused && this.focusColumn === "playlist",
|
||||
);
|
||||
this.queueMenu.drawScrollbar(image, width - 12, LIST_TOP, listHeight - 4);
|
||||
}
|
||||
|
||||
return image;
|
||||
}
|
||||
|
||||
private drawAction({ image, item: action, x, y, width, height, selected }: MenuDrawArgs<MusicAction>): void {
|
||||
const font = getDefaultSmallFont();
|
||||
const value = !action.enabled ? (selected ? 130 : 90) : selected ? 255 : 200;
|
||||
image.drawText(font, x + LIST_TEXT_INSET, centeredTextY(font, y, height), truncateText(font, action.label, ACTION_WIDTH - 16), value);
|
||||
if ((action.kind === "playlist" || action.kind === "browse") && action.enabled) {
|
||||
drawSubmenuIndicator(image, font, x, y, width, height, value);
|
||||
}
|
||||
}
|
||||
|
||||
private drawQueueItem({ image, item, x, y, width, height, selected }: MenuDrawArgs<MediaQueueItem>): void {
|
||||
const font = getDefaultSmallFont();
|
||||
const label = `${item.active ? "> " : " "}${item.title || "(untitled)"}`;
|
||||
image.drawText(font, x + LIST_TEXT_INSET, centeredTextY(font, y, height), truncateText(font, label, width - 2 * LIST_TEXT_INSET), selected ? 255 : 200);
|
||||
}
|
||||
|
||||
async handleInput(event: InputEvent, ctx: LayerContext): Promise<void> {
|
||||
if (event.type === "double-click" && this.focusColumn === "playlist") {
|
||||
this.focusColumn = "actions";
|
||||
this.selectedActionIndex = PLAYLIST_ACTION_INDEX;
|
||||
this.actionsMenu.select(PLAYLIST_ACTION_INDEX);
|
||||
return;
|
||||
}
|
||||
const media = mediaControllerBridge.snapshot();
|
||||
@@ -237,31 +217,20 @@ class MusicAppLayer implements Layer {
|
||||
case "swipe-down":
|
||||
return this.handleInput({ type: "scroll-down", timestampMs: event.timestampMs }, ctx);
|
||||
case "scroll-up":
|
||||
if (this.focusColumn === "playlist") {
|
||||
this.selectedQueueIndex = Math.max(0, this.selectedQueueIndex - 1);
|
||||
} else {
|
||||
this.selectedActionIndex = (this.selectedActionIndex + actions.length - 1) % actions.length;
|
||||
}
|
||||
return;
|
||||
case "scroll-down":
|
||||
if (this.focusColumn === "playlist") {
|
||||
this.selectedQueueIndex = Math.min(Math.max(0, queue.length - 1), this.selectedQueueIndex + 1);
|
||||
} else {
|
||||
this.selectedActionIndex = (this.selectedActionIndex + 1) % actions.length;
|
||||
}
|
||||
await (this.focusColumn === "playlist" ? this.queueMenu : this.actionsMenu).handleInput(event);
|
||||
return;
|
||||
case "click": {
|
||||
if (this.focusColumn === "playlist") {
|
||||
const item = queue[this.selectedQueueIndex];
|
||||
const item = this.queueMenu.selectedItem;
|
||||
if (item) {
|
||||
await mediaControllerBridge.skipToQueueItem(item.id);
|
||||
// The player rebuilds the queue with the chosen track at index 0.
|
||||
this.selectedQueueIndex = 0;
|
||||
this.queueScrollRow = 0;
|
||||
this.queueMenu.select(0);
|
||||
}
|
||||
return;
|
||||
}
|
||||
const action = actions[this.selectedActionIndex];
|
||||
const action = this.actionsMenu.selectedItem;
|
||||
if (!action || !action.enabled) return;
|
||||
if (action.kind === "play-pause") await mediaControllerBridge.playPause();
|
||||
else if (action.kind === "previous") await mediaControllerBridge.skipPrevious();
|
||||
@@ -271,7 +240,7 @@ class MusicAppLayer implements Layer {
|
||||
ctx.stack.push(new VolumeModalLayer(mediaControllerBridge.getMediaVolumePercent()));
|
||||
} else if (action.kind === "playlist") {
|
||||
const activeIndex = queue.findIndex((item) => item.active);
|
||||
if (activeIndex >= 0) this.selectedQueueIndex = activeIndex;
|
||||
if (activeIndex >= 0) this.queueMenu.select(activeIndex);
|
||||
this.focusColumn = "playlist";
|
||||
}
|
||||
return;
|
||||
@@ -332,12 +301,13 @@ class MusicAppLayer implements Layer {
|
||||
];
|
||||
}
|
||||
|
||||
/** Hand the menus the freshly built rows; each keeps its selection index, clamped. */
|
||||
private reconcileSelection(actions: MusicAction[], queue: MediaQueueItem[]): void {
|
||||
this.selectedActionIndex = clamp(this.selectedActionIndex, 0, actions.length - 1);
|
||||
this.selectedQueueIndex = clamp(this.selectedQueueIndex, 0, Math.max(0, queue.length - 1));
|
||||
this.actionsMenu.setItems(actions);
|
||||
this.queueMenu.setItems(queue);
|
||||
if (!queue.length && this.focusColumn === "playlist") {
|
||||
this.focusColumn = "actions";
|
||||
this.selectedActionIndex = PLAYLIST_ACTION_INDEX;
|
||||
this.actionsMenu.select(PLAYLIST_ACTION_INDEX);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -10,39 +10,45 @@ import { drawToggleMenuItem, MenuLayer, type MenuItem } from "../../ui/menu";
|
||||
/** Discovered apps stay visible even when ignored or no longer publishing a session. */
|
||||
export class MusicSettingsLayer implements Layer {
|
||||
private menu: MenuLayer | null = null;
|
||||
private signature = "";
|
||||
private selectedPackage = "";
|
||||
private menuWidth = 0;
|
||||
/** The package behind each current row, parallel to the menu's items. */
|
||||
private packages: string[] = [];
|
||||
|
||||
constructor() {
|
||||
// Refresh installed browser services, remembering ignored services too.
|
||||
mediaBrowserBridge.listBrowsableApps(true);
|
||||
}
|
||||
|
||||
/** Refresh the rows from the remembered apps, keeping the selection on the same package. */
|
||||
private currentMenu(ctx: LayerContext): MenuLayer {
|
||||
const apps = readMediaApps().sort((a, b) =>
|
||||
a.appName.localeCompare(b.appName) || a.packageName.localeCompare(b.packageName));
|
||||
const items: MenuItem[] = apps.map((app) => ({
|
||||
label: app.appName,
|
||||
onSelect: () => setMediaAppEnabled(app, !isMediaAppEnabled(app.packageName)),
|
||||
render: ({ image, x, y, width, selected }) => {
|
||||
const font = getDefaultSmallFont();
|
||||
const duplicateName = apps.some((other) => other !== app && other.appName === app.appName);
|
||||
const label = duplicateName ? `${app.appName} (${app.packageName})` : app.appName;
|
||||
drawToggleMenuItem(image, font, x, y, width, truncateText(font, label, width - 48),
|
||||
isMediaAppEnabled(app.packageName), selected);
|
||||
},
|
||||
}));
|
||||
if (!items.length) items.push({ label: "No media apps discovered yet.", disabled: true, onSelect: () => {} });
|
||||
const selectedPackage = this.menu?.selectedIndex != null ? this.packages[this.menu.selectedIndex] : undefined;
|
||||
const index = apps.findIndex((app) => app.packageName === selectedPackage);
|
||||
this.packages = apps.map((app) => app.packageName);
|
||||
|
||||
const width = ctx.stack.getBaseSize().width;
|
||||
const signature = JSON.stringify([width, apps.map((app) => [app.packageName, app.appName])]);
|
||||
if (!this.menu || signature !== this.signature) {
|
||||
this.signature = signature;
|
||||
const items: MenuItem[] = apps.map((app) => ({
|
||||
label: app.appName,
|
||||
onSelect: () => setMediaAppEnabled(app, !isMediaAppEnabled(app.packageName)),
|
||||
render: ({ image, x, y, width, selected }) => {
|
||||
if (selected) this.selectedPackage = app.packageName;
|
||||
const font = getDefaultSmallFont();
|
||||
const duplicateName = apps.some((other) => other !== app && other.appName === app.appName);
|
||||
const label = duplicateName ? `${app.appName} (${app.packageName})` : app.appName;
|
||||
drawToggleMenuItem(image, font, x, y, width, truncateText(font, label, width - 48),
|
||||
isMediaAppEnabled(app.packageName), selected);
|
||||
},
|
||||
}));
|
||||
if (!items.length) items.push({ label: "No media apps discovered yet.", disabled: true, onSelect: () => {} });
|
||||
if (!this.menu || width !== this.menuWidth) {
|
||||
this.menuWidth = width;
|
||||
this.menu = new MenuLayer("Music settings", items, {
|
||||
x: 8, y: 8, width: width - 16,
|
||||
opaque: true, showBorder: false,
|
||||
footer: "On = enabled Off = ignored",
|
||||
}).selectItem(Math.max(0, apps.findIndex((app) => app.packageName === this.selectedPackage)));
|
||||
}).selectItem(Math.max(0, index));
|
||||
} else {
|
||||
this.menu.setItems(items, index >= 0 ? index : undefined);
|
||||
}
|
||||
return this.menu;
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
* guidance text (right pane) updates per GPS fix and ships as small deltas.
|
||||
*/
|
||||
import "@nativescript/core/globals";
|
||||
import { finishWorkerShutdown } from "../../ui/shell/worker-lifecycle";
|
||||
import { GrayImage } from "../../graphics/image";
|
||||
import { flattenPlanesWithDraws, planesFingerprint, type Plane } from "../../graphics/plane";
|
||||
import { prepareFrameDraws } from "../../graphics/glyph-wire";
|
||||
@@ -15,14 +16,12 @@ import { getDefaultSmallFont } from "../../graphics/ui-fonts";
|
||||
import * as frameTimings from "../../native/frame-timings";
|
||||
import { getActiveDisplay } from "../../native/active-display";
|
||||
import {
|
||||
drawListScrollbar,
|
||||
drawRightValueMenuItem,
|
||||
drawSelectionHighlight,
|
||||
drawSubmenuIndicator,
|
||||
drawToggleMenuItem,
|
||||
scrollToKeepSelectionVisible,
|
||||
type MenuItem,
|
||||
} from "../../ui/menu";
|
||||
import { Menu, type MenuDrawArgs } from "../../ui/menu-core";
|
||||
import { LIST_ROW_TEXT_INSET, listRowHeight } from "../../ui/metrics";
|
||||
import { WindowMenu, WindowMenuLayer } from "../../ui/window-menu";
|
||||
import type { LayerContext } from "../../ui/layers";
|
||||
@@ -204,9 +203,8 @@ type IdleEntry =
|
||||
| { kind: "recent"; destination: RecentDestination }
|
||||
| { kind: "action"; label: string; detail: string; run: () => void };
|
||||
|
||||
/** Idle-page list selection (index into idleEntries()). */
|
||||
let idleSelection = 0;
|
||||
let idleScrollRow = 0;
|
||||
/** The idle page's list; created on first use (idleList) so module load stays free of paint dependencies. */
|
||||
let idleMenu: Menu<IdleEntry> | null = null;
|
||||
|
||||
/**
|
||||
* In-progress destination edit: the phone text editor is open on `setting`
|
||||
@@ -275,6 +273,12 @@ post({ type: "worker-ready" });
|
||||
global.onmessage = (event: { data: WorkerAppMessage }) => {
|
||||
const message = event.data;
|
||||
switch (message.type) {
|
||||
case "check-idle":
|
||||
post({ type: "worker-idle" });
|
||||
break;
|
||||
case "shutdown":
|
||||
finishWorkerShutdown();
|
||||
break;
|
||||
case "navigation-sensors":
|
||||
handleNavigationSensorEvent(message.event);
|
||||
break;
|
||||
@@ -941,7 +945,7 @@ function windowMenuItems(win: NavWindow): MenuItem[] {
|
||||
onSelect: () => {
|
||||
const enabled = navigateRememberRecentSetting.toggle();
|
||||
if (!enabled) clearRecentDestinations();
|
||||
clampIdleSelection();
|
||||
syncIdleList();
|
||||
},
|
||||
render: ({ image, x, y, width, selected }) => {
|
||||
drawToggleMenuItem(
|
||||
@@ -956,7 +960,7 @@ function windowMenuItems(win: NavWindow): MenuItem[] {
|
||||
);
|
||||
},
|
||||
});
|
||||
const selectedEntry = phase === "idle" ? idleEntries()[idleSelection] : undefined;
|
||||
const selectedEntry = phase === "idle" ? syncIdleList().selectedItem : null;
|
||||
if (selectedEntry?.kind === "recent") {
|
||||
const recent = selectedEntry.destination;
|
||||
items.push({
|
||||
@@ -964,7 +968,7 @@ function windowMenuItems(win: NavWindow): MenuItem[] {
|
||||
onSelect: (ctx) => {
|
||||
ctx.stack.pop();
|
||||
removeRecentDestination(recent);
|
||||
clampIdleSelection();
|
||||
syncIdleList();
|
||||
},
|
||||
});
|
||||
}
|
||||
@@ -974,7 +978,7 @@ function windowMenuItems(win: NavWindow): MenuItem[] {
|
||||
onSelect: (ctx) => {
|
||||
ctx.stack.pop();
|
||||
clearRecentDestinations();
|
||||
clampIdleSelection();
|
||||
syncIdleList();
|
||||
},
|
||||
});
|
||||
}
|
||||
@@ -1059,7 +1063,7 @@ function customDestinationMenuItems(destination: SavedDestination): MenuItem[] {
|
||||
onSelect: (ctx) => {
|
||||
closeMenusAnd(ctx, () => {
|
||||
removeCustomDestination(destination.id);
|
||||
clampIdleSelection();
|
||||
syncIdleList();
|
||||
});
|
||||
},
|
||||
},
|
||||
@@ -1114,7 +1118,7 @@ function finishEdit(confirmed: boolean): void {
|
||||
if (!editing) {
|
||||
navigateDestinationNameDraftSetting.set("");
|
||||
navigateDestinationAddressDraftSetting.set("");
|
||||
clampIdleSelection();
|
||||
syncIdleList();
|
||||
render();
|
||||
}
|
||||
}
|
||||
@@ -1199,9 +1203,27 @@ function idleEntries(): IdleEntry[] {
|
||||
return entries;
|
||||
}
|
||||
|
||||
function clampIdleSelection(): void {
|
||||
const count = idleEntries().length;
|
||||
idleSelection = Math.max(0, Math.min(idleSelection, count - 1));
|
||||
/** Horizontal inset of the idle list's selection boxes. */
|
||||
const IDLE_LIST_X = 20;
|
||||
/** Pixels between consecutive idle rows' selection boxes. */
|
||||
const IDLE_ROW_GAP = 2;
|
||||
|
||||
function idleList(): Menu<IdleEntry> {
|
||||
idleMenu ??= new Menu<IdleEntry>({
|
||||
wrap: false,
|
||||
rowGap: IDLE_ROW_GAP,
|
||||
highlight: { radius: 6 },
|
||||
getHeight: () => listRowHeight(smallFont),
|
||||
draw: drawIdleRow,
|
||||
});
|
||||
return idleMenu;
|
||||
}
|
||||
|
||||
/** Refresh the idle list from idleEntries(); the selection keeps its index, clamped into range. */
|
||||
function syncIdleList(): Menu<IdleEntry> {
|
||||
const list = idleList();
|
||||
list.setItems(idleEntries());
|
||||
return list;
|
||||
}
|
||||
|
||||
function navigateToIdleEntry(entry: IdleEntry): void {
|
||||
@@ -1275,7 +1297,7 @@ function handleInput(win: NavWindow, event: InputEvent, frameId: number): void {
|
||||
} else if (phase === "arrived" || phase === "map") {
|
||||
stopNavigation("");
|
||||
} else if (phase === "idle") {
|
||||
const entry = idleEntries()[idleSelection];
|
||||
const entry = syncIdleList().selectedItem;
|
||||
if (!entry) {
|
||||
frameTimings.finishFrame(frameId, "discarded: navigate ignored click");
|
||||
return;
|
||||
@@ -1294,8 +1316,7 @@ function handleInput(win: NavWindow, event: InputEvent, frameId: number): void {
|
||||
maybeRefreshMap();
|
||||
renderAndSubmit(win, frameId);
|
||||
} else if (phase === "idle" && idleEntries().length > 1) {
|
||||
const count = idleEntries().length;
|
||||
idleSelection = Math.max(0, Math.min(count - 1, idleSelection + (event.type === "scroll-down" ? 1 : -1)));
|
||||
syncIdleList().moveSelection(event.type === "scroll-down" ? 1 : -1);
|
||||
renderAndSubmit(win, frameId);
|
||||
} else {
|
||||
frameTimings.finishFrame(frameId, "discarded: navigate ignored scroll");
|
||||
@@ -1385,59 +1406,33 @@ function paintIdleList(image: GrayImage, win: NavWindow, entries: IdleEntry[], t
|
||||
const rowHeight = listRowHeight(smallFont);
|
||||
const listBottom = image.height - 30;
|
||||
const visibleRows = Math.max(1, Math.floor((listBottom - top) / rowHeight));
|
||||
idleSelection = Math.max(0, Math.min(idleSelection, entries.length - 1));
|
||||
idleScrollRow = scrollToKeepSelectionVisible(idleScrollRow, idleSelection, visibleRows, entries.length);
|
||||
const rowX = 20;
|
||||
const rowWidth = image.width - 40 - (entries.length > visibleRows ? 10 : 0);
|
||||
const labelWidth = Math.floor(rowWidth * 0.4);
|
||||
for (let row = 0; row < visibleRows; row++) {
|
||||
const index = idleScrollRow + row;
|
||||
const entry = entries[index];
|
||||
if (!entry) break;
|
||||
const y = top + row * rowHeight;
|
||||
const selected = index === idleSelection;
|
||||
if (selected) drawSelectionHighlight(image, rowX, y, rowWidth, rowHeight - 2, win.focused);
|
||||
const textX = rowX + 6;
|
||||
const textY = y + LIST_ROW_TEXT_INSET;
|
||||
if (entry.kind === "action") {
|
||||
const label = truncateText(smallFont, entry.label, labelWidth);
|
||||
image.drawText(smallFont, textX, textY, label, selected ? 245 : 210);
|
||||
const detailX = textX + labelWidth + 8;
|
||||
image.drawText(
|
||||
smallFont,
|
||||
detailX,
|
||||
textY,
|
||||
truncateText(smallFont, entry.detail, rowX + rowWidth - 6 - detailX),
|
||||
selected ? 170 : 130,
|
||||
);
|
||||
} else if (entry.kind === "saved") {
|
||||
const label = truncateText(smallFont, entry.destination.name, labelWidth);
|
||||
image.drawText(smallFont, textX, textY, label, selected ? 245 : 210);
|
||||
const detailX = textX + labelWidth + 8;
|
||||
image.drawText(
|
||||
smallFont,
|
||||
detailX,
|
||||
textY,
|
||||
truncateText(smallFont, entry.destination.address, rowX + rowWidth - 6 - detailX),
|
||||
selected ? 170 : 130,
|
||||
);
|
||||
} else {
|
||||
const label = truncateText(smallFont, entry.destination.name, labelWidth);
|
||||
image.drawText(smallFont, textX, textY, label, selected ? 245 : 210);
|
||||
const detailX = textX + labelWidth + 8;
|
||||
const detail = entry.destination.place ? `${entry.destination.place} (recent)` : "(recent)";
|
||||
image.drawText(
|
||||
smallFont,
|
||||
detailX,
|
||||
textY,
|
||||
truncateText(smallFont, detail, rowX + rowWidth - 6 - detailX),
|
||||
selected ? 170 : 130,
|
||||
);
|
||||
}
|
||||
}
|
||||
if (entries.length > visibleRows) {
|
||||
drawListScrollbar(image, rowX + rowWidth + 4, top, visibleRows * rowHeight - 2, idleScrollRow, visibleRows, entries.length);
|
||||
const list = idleList();
|
||||
list.setItems(entries);
|
||||
list.paint(image, { x: IDLE_LIST_X, y: top, width: rowWidth, height: visibleRows * rowHeight - IDLE_ROW_GAP }, win.focused);
|
||||
list.drawScrollbar(image, IDLE_LIST_X + rowWidth + 4, top, visibleRows * rowHeight - 2);
|
||||
}
|
||||
|
||||
/** Two columns: the entry's name, then its detail (action hint, address or place). */
|
||||
function drawIdleRow({ image, item: entry, x, y, width, selected }: MenuDrawArgs<IdleEntry>): void {
|
||||
const labelWidth = Math.floor(width * 0.4);
|
||||
const textX = x + 6;
|
||||
const textY = y + LIST_ROW_TEXT_INSET;
|
||||
let label: string;
|
||||
let detail: string;
|
||||
if (entry.kind === "action") {
|
||||
label = entry.label;
|
||||
detail = entry.detail;
|
||||
} else if (entry.kind === "saved") {
|
||||
label = entry.destination.name;
|
||||
detail = entry.destination.address;
|
||||
} else {
|
||||
label = entry.destination.name;
|
||||
detail = entry.destination.place ? `${entry.destination.place} (recent)` : "(recent)";
|
||||
}
|
||||
image.drawText(smallFont, textX, textY, truncateText(smallFont, label, labelWidth), selected ? 245 : 210);
|
||||
const detailX = textX + labelWidth + 8;
|
||||
image.drawText(smallFont, detailX, textY, truncateText(smallFont, detail, x + width - 6 - detailX), selected ? 170 : 130);
|
||||
}
|
||||
|
||||
function paintNavigating(image: GrayImage, win: NavWindow): void {
|
||||
|
||||
@@ -420,11 +420,11 @@ export function nightscoutMenuItems(): MenuItem[] {
|
||||
openSettingsSubMenu(ctx, "Nightscout settings", [
|
||||
textSettingMenuItem(nightscoutSiteUrlSetting),
|
||||
textSettingMenuItem(nightscoutApiTokenSetting),
|
||||
toggleSettingMenuItem(nightscoutAlwaysShowInTopBarSetting),
|
||||
textSettingMenuItem(nightscoutMaxCannulaAgeSetting),
|
||||
textSettingMenuItem(nightscoutCartridgeLowSetting),
|
||||
textSettingMenuItem(nightscoutBatteryLowSetting),
|
||||
textSettingMenuItem(nightscoutMaxLoopAgeSetting),
|
||||
toggleSettingMenuItem(nightscoutAlwaysShowInTopBarSetting),
|
||||
]);
|
||||
},
|
||||
},
|
||||
|
||||
@@ -21,6 +21,7 @@
|
||||
* sidebar) pauses, as does backgrounding or screen-off.
|
||||
*/
|
||||
import "@nativescript/core/globals";
|
||||
import { finishWorkerShutdown } from "../../ui/shell/worker-lifecycle";
|
||||
import { GrayImage } from "../../graphics/image";
|
||||
import { flattenPlanesWithDraws, planesFingerprint, type Plane } from "../../graphics/plane";
|
||||
import { prepareFrameDraws } from "../../graphics/glyph-wire";
|
||||
@@ -290,6 +291,12 @@ post({ type: "worker-ready" });
|
||||
global.onmessage = (event: { data: WorkerAppMessage }) => {
|
||||
const message = event.data;
|
||||
switch (message.type) {
|
||||
case "check-idle":
|
||||
post({ type: "worker-idle" });
|
||||
break;
|
||||
case "shutdown":
|
||||
finishWorkerShutdown();
|
||||
break;
|
||||
case "open-window": {
|
||||
const window: PinballWindow = {
|
||||
windowId: message.windowId,
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
* menu, via the phone app's text editor.
|
||||
*/
|
||||
import "@nativescript/core/globals";
|
||||
import { finishWorkerShutdown } from "../../ui/shell/worker-lifecycle";
|
||||
import { GrayImage } from "../../graphics/image";
|
||||
import { flattenPlanesWithDraws, planesFingerprint, singlePlane, type Plane } from "../../graphics/plane";
|
||||
import { prepareFrameDraws } from "../../graphics/glyph-wire";
|
||||
@@ -165,6 +166,12 @@ post({ type: "worker-ready" });
|
||||
global.onmessage = (event: { data: WorkerAppMessage }) => {
|
||||
const message = event.data;
|
||||
switch (message.type) {
|
||||
case "check-idle":
|
||||
post({ type: "worker-idle" });
|
||||
break;
|
||||
case "shutdown":
|
||||
finishWorkerShutdown();
|
||||
break;
|
||||
case "open-window":
|
||||
window = {
|
||||
windowId: message.windowId,
|
||||
|
||||
@@ -52,10 +52,8 @@ export type TeleprompterAppOptions = InProcessAppOptions & {
|
||||
* over the home page.
|
||||
*/
|
||||
export function createTeleprompterAppWindow(options: TeleprompterAppOptions): InProcessWindow {
|
||||
// The home menu's items are rebuilt in place whenever the recents change
|
||||
// (MenuLayer reads the array each paint).
|
||||
const homeItems: MenuItem[] = [];
|
||||
const home = new MenuLayer("Teleprompter", homeItems, HOME_LAYOUT);
|
||||
// The home menu's items are replaced (refreshHome) whenever the recents change.
|
||||
const home = new MenuLayer("Teleprompter", [], HOME_LAYOUT);
|
||||
let created: InProcessWindow | null = null;
|
||||
let notice = "";
|
||||
|
||||
@@ -142,10 +140,8 @@ export function createTeleprompterAppWindow(options: TeleprompterAppOptions): In
|
||||
if (notice) {
|
||||
items.push({ label: notice, disabled: true, onSelect: () => {} });
|
||||
}
|
||||
homeItems.length = 0;
|
||||
homeItems.push(...items);
|
||||
// Land on the most recent script (one click resumes it), else on Browse.
|
||||
home.selectItem(getRecentScripts().length > 0 ? 1 : 0);
|
||||
home.setItems(items, getRecentScripts().length > 0 ? 1 : 0);
|
||||
}
|
||||
|
||||
refreshHome();
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
import { isTerminalWidgetConfigured } from "../glanceboard/configuration";
|
||||
import { loadConnections, parseConnectionString } from "./connections";
|
||||
|
||||
/** Keep session-list connections alive for a configured widget, even while it is hidden. */
|
||||
export function hasTerminalBackgroundWork(): boolean {
|
||||
return isTerminalWidgetConfigured() &&
|
||||
loadConnections().some(connection => connection.enabled && parseConnectionString(connection.url) !== null);
|
||||
}
|
||||
@@ -31,6 +31,8 @@
|
||||
* re-attach snapshot resyncs contents and scrollback only once it's needed.
|
||||
*/
|
||||
import "@nativescript/core/globals";
|
||||
import { hasTerminalBackgroundWork } from "./background";
|
||||
import { finishWorkerShutdown } from "../../ui/shell/worker-lifecycle";
|
||||
import { GrayImage, type UiFont } from "../../graphics/image";
|
||||
import { flattenPlanesWithDraws, planesFingerprint, type Plane } from "../../graphics/plane";
|
||||
import { prepareFrameDraws } from "../../graphics/glyph-wire";
|
||||
@@ -52,7 +54,8 @@ import { clamp } from "../../util/numeric-util";
|
||||
import { terminalAutoReconnectSetting, terminalLaunchPresetsSetting, terminalNewConnectionSetting, terminalWakeOnBellSetting } from "../../ui/dashboard-settings";
|
||||
import { connectionDisplayName, loadConnections, parseConnectionString, saveConnections, TERMINAL_CONNECTIONS_KEY, updateConnection, type TerminalConnection } from "./connections";
|
||||
import { TerminalEmulator } from "./terminal-emulator";
|
||||
import { drawListScrollbar, drawSelectionHighlight, scrollToKeepSelectionVisible, type MenuItem } from "../../ui/menu";
|
||||
import { type MenuItem } from "../../ui/menu";
|
||||
import { Menu, type MenuDrawArgs } from "../../ui/menu-core";
|
||||
import { lineStep, listRowHeight } from "../../ui/metrics";
|
||||
import { WindowMenu } from "../../ui/window-menu";
|
||||
import { appViewportSize } from "../../ui/shell/geometry";
|
||||
@@ -124,8 +127,8 @@ type HubMode = "sessions" | "connections" | "add";
|
||||
type HubWindow = BaseWindow & {
|
||||
kind: "hub";
|
||||
mode: HubMode;
|
||||
selectedIndex: number;
|
||||
scrollRow: number;
|
||||
/** The section's item list; created on first paint or input (hubMenu). */
|
||||
list: Menu<HubItem> | null;
|
||||
/**
|
||||
* Session recency keys in display order, captured when the window last
|
||||
* became visible so the list doesn't reshuffle under the user while it's
|
||||
@@ -311,6 +314,14 @@ post({ type: "worker-ready" });
|
||||
global.onmessage = (event: { data: WorkerAppMessage }) => {
|
||||
const message = event.data;
|
||||
switch (message.type) {
|
||||
case "check-idle":
|
||||
reportIdle();
|
||||
break;
|
||||
case "shutdown":
|
||||
for (const control of controls.values()) stopControl(control);
|
||||
controls.clear();
|
||||
finishWorkerShutdown();
|
||||
break;
|
||||
case "open-window":
|
||||
openWindow(message.windowId, message.surfaceId, message.title, message.viewport);
|
||||
break;
|
||||
@@ -424,12 +435,17 @@ global.onmessage = (event: { data: WorkerAppMessage }) => {
|
||||
// toggles edited in the Settings app, the Add-connection draft typed on the
|
||||
// phone).
|
||||
onSettingsStoreChanged((key) => {
|
||||
if (key.startsWith("glanceboard.")) {
|
||||
reportIdle();
|
||||
return;
|
||||
}
|
||||
if (!key.startsWith("terminal.")) return;
|
||||
switch (key) {
|
||||
case TERMINAL_CONNECTIONS_KEY:
|
||||
// Only once the app has actually been opened.
|
||||
if (controlsInitialized) syncControlsFromSettings();
|
||||
renderHubWindows();
|
||||
reportIdle();
|
||||
return;
|
||||
case NEW_CONNECTION_DRAFT_KEY:
|
||||
// Live keystrokes from the phone editor; repaint the add screen.
|
||||
@@ -438,7 +454,7 @@ onSettingsStoreChanged((key) => {
|
||||
case "terminal.autoReconnect":
|
||||
if (!terminalAutoReconnectSetting.get()) {
|
||||
cancelPendingReconnects();
|
||||
} else if (windows.size > 0) {
|
||||
} else if (windows.size > 0 || hasTerminalBackgroundWork()) {
|
||||
for (const control of controls.values()) {
|
||||
if ((control.state?.phase ?? "idle") === "failed") scheduleControlReconnect(control);
|
||||
}
|
||||
@@ -450,6 +466,10 @@ onSettingsStoreChanged((key) => {
|
||||
}
|
||||
});
|
||||
|
||||
function reportIdle(): void {
|
||||
if (windows.size === 0 && !hasTerminalBackgroundWork()) post({ type: "worker-idle" });
|
||||
}
|
||||
|
||||
/** Cancel scheduled retries (auto-reconnect turned off); stale views stay marked. */
|
||||
function cancelPendingReconnects(): void {
|
||||
for (const control of controls.values()) {
|
||||
@@ -488,8 +508,7 @@ function openWindow(windowId: string, surfaceId: string, title: string, viewport
|
||||
lastSubmittedFingerprint: "",
|
||||
menu: null,
|
||||
mode: "sessions",
|
||||
selectedIndex: 0,
|
||||
scrollRow: 0,
|
||||
list: null,
|
||||
sessionOrder: [],
|
||||
addError: "",
|
||||
});
|
||||
@@ -521,8 +540,8 @@ function closeWindow(windowId: string): void {
|
||||
windows.delete(windowId);
|
||||
windowIconActivity.delete(windowId);
|
||||
updateHubAnimation();
|
||||
// Auto-reconnect only runs while at least one terminal window is open.
|
||||
if (windows.size === 0) {
|
||||
// A configured widget owns the control connections after the last window closes.
|
||||
if (windows.size === 0 && !hasTerminalBackgroundWork()) {
|
||||
for (const control of controls.values()) {
|
||||
cancelControlReconnect(control);
|
||||
}
|
||||
@@ -676,19 +695,18 @@ function noteSessionListRecency(control: ControlConnection, state: G2MirrorState
|
||||
/**
|
||||
* Retry a control connection after a backoff delay. The delay doubles per
|
||||
* scheduled attempt and resets when a connection reaches "connected" (or on
|
||||
* a manual Connect). No-op when auto-reconnect is off, no terminal window is
|
||||
* open, or the connection was disabled/removed meanwhile.
|
||||
* a manual Connect). Windows and a configured widget can both own the client.
|
||||
*/
|
||||
function scheduleControlReconnect(control: ControlConnection): void {
|
||||
if (control.reconnectTimer) return;
|
||||
if (!terminalAutoReconnectSetting.get()) return;
|
||||
if (windows.size === 0) return;
|
||||
if (windows.size === 0 && !hasTerminalBackgroundWork()) return;
|
||||
if (!control.config.enabled) return;
|
||||
const delayMs = control.reconnectDelayMs;
|
||||
control.reconnectDelayMs = Math.min(control.reconnectDelayMs * 2, RECONNECT_MAX_DELAY_MS);
|
||||
control.reconnectTimer = setTimeout(() => {
|
||||
control.reconnectTimer = null;
|
||||
if (!terminalAutoReconnectSetting.get() || windows.size === 0) return;
|
||||
if (!terminalAutoReconnectSetting.get() || (windows.size === 0 && !hasTerminalBackgroundWork())) return;
|
||||
if (controls.get(control.config.id) !== control || !control.config.enabled) return;
|
||||
if ((control.state?.phase ?? "idle") === "failed") startControl(control);
|
||||
}, delayMs);
|
||||
@@ -1171,11 +1189,48 @@ type HubItem = {
|
||||
onSelect?: () => void;
|
||||
};
|
||||
|
||||
/** Vertical offset of a row's text line from the top of its selection box. */
|
||||
const HUB_ROW_TEXT_INSET = 4;
|
||||
/** Horizontal inset of the list's selection boxes from the viewport edges. */
|
||||
const HUB_LIST_X = 20;
|
||||
|
||||
/** The hub window's list, created on first use so openWindow stays free of paint dependencies. */
|
||||
function hubMenu(window: HubWindow): Menu<HubItem> {
|
||||
window.list ??= new Menu<HubItem>({
|
||||
wrap: false,
|
||||
rowGap: 1,
|
||||
getHeight: () => listRowHeight(chromeFont()),
|
||||
isSelectable: (item) => Boolean(item.onSelect),
|
||||
onSelect: (item) => item.onSelect?.(),
|
||||
draw: drawHubRow,
|
||||
});
|
||||
return window.list;
|
||||
}
|
||||
|
||||
function drawHubRow({ image, item, x, y, width, selected }: MenuDrawArgs<HubItem>): void {
|
||||
const font = chromeFont();
|
||||
if (item.heading) {
|
||||
image.drawText(font, x, y + HUB_ROW_TEXT_INSET, item.label, 140);
|
||||
return;
|
||||
}
|
||||
// Activity gutter, open-window number column, then the label, truncated
|
||||
// to the row (shared with the Glanceboard's Terminal widget).
|
||||
drawSessionRow(image, font, {
|
||||
x: x + 2,
|
||||
y: y + HUB_ROW_TEXT_INSET,
|
||||
width: width - 8,
|
||||
label: item.label,
|
||||
openGlyph: item.openGlyph ?? null,
|
||||
active: Boolean(item.active),
|
||||
phase: hubAnimationPhase,
|
||||
value: selected ? 255 : 200,
|
||||
});
|
||||
}
|
||||
|
||||
/** Switch the hub between its sections, resetting selection state. */
|
||||
function setHubMode(window: HubWindow, mode: HubMode): void {
|
||||
window.mode = mode;
|
||||
window.selectedIndex = 0;
|
||||
window.scrollRow = 0;
|
||||
window.list?.select(0);
|
||||
window.addError = "";
|
||||
}
|
||||
|
||||
@@ -1405,34 +1460,6 @@ function endAddConnection(window: HubWindow): void {
|
||||
post({ type: "end-text-setting-edit" });
|
||||
}
|
||||
|
||||
/** Move the hub selection to the next selectable item in `direction`. */
|
||||
function moveHubSelection(window: HubWindow, items: HubItem[], direction: -1 | 1): void {
|
||||
let index = window.selectedIndex + direction;
|
||||
while (index >= 0 && index < items.length && !items[index]!.onSelect) {
|
||||
index += direction;
|
||||
}
|
||||
if (index >= 0 && index < items.length) {
|
||||
window.selectedIndex = index;
|
||||
}
|
||||
}
|
||||
|
||||
/** Clamp the selection into range and off heading rows (prefer moving down). */
|
||||
function clampHubSelection(window: HubWindow, items: HubItem[]): void {
|
||||
if (!items.length) {
|
||||
window.selectedIndex = 0;
|
||||
return;
|
||||
}
|
||||
let index = Math.max(0, Math.min(window.selectedIndex, items.length - 1));
|
||||
if (!items[index]!.onSelect) {
|
||||
let forward = index;
|
||||
while (forward < items.length && !items[forward]!.onSelect) forward++;
|
||||
let backward = index;
|
||||
while (backward >= 0 && !items[backward]!.onSelect) backward--;
|
||||
index = forward < items.length ? forward : Math.max(0, backward);
|
||||
}
|
||||
window.selectedIndex = index;
|
||||
}
|
||||
|
||||
function handleHubInput(window: HubWindow, event: InputEvent, frameId: number): void {
|
||||
if (window.mode === "add") {
|
||||
if (event.type === "click") {
|
||||
@@ -1443,23 +1470,18 @@ function handleHubInput(window: HubWindow, event: InputEvent, frameId: number):
|
||||
frameTimings.finishFrame(frameId, "discarded: terminal add-connection ignored input");
|
||||
return;
|
||||
}
|
||||
const items = hubItems(window);
|
||||
clampHubSelection(window, items);
|
||||
const list = hubMenu(window);
|
||||
list.setItems(hubItems(window));
|
||||
switch (event.type) {
|
||||
case "scroll-up":
|
||||
moveHubSelection(window, items, -1);
|
||||
renderAndSubmit(window, frameId);
|
||||
return;
|
||||
case "scroll-down":
|
||||
moveHubSelection(window, items, 1);
|
||||
case "click":
|
||||
// The menu's input handler is async only to await onSelect; hub item
|
||||
// callbacks are synchronous, so the selection move or action has
|
||||
// already applied when it returns and the frame can paint right away.
|
||||
list.handleInput(event).catch((error) => console.error("terminal hub action failed", error));
|
||||
renderAndSubmit(window, frameId);
|
||||
return;
|
||||
case "click": {
|
||||
const item = items[window.selectedIndex];
|
||||
item?.onSelect?.();
|
||||
renderAndSubmit(window, frameId);
|
||||
return;
|
||||
}
|
||||
default:
|
||||
frameTimings.finishFrame(frameId, "discarded: terminal hub ignored input");
|
||||
return;
|
||||
@@ -1583,49 +1605,18 @@ function paintHub(window: HubWindow): GrayImage {
|
||||
listTop += 2 * step + 6;
|
||||
}
|
||||
|
||||
const items = hubItems(window);
|
||||
clampHubSelection(window, items);
|
||||
const hubRowH = listRowHeight(chromeFont());
|
||||
const visibleRowCount = Math.max(1, ((window.viewportHeight - 6 - listTop) / hubRowH) | 0);
|
||||
window.scrollRow = scrollToKeepSelectionVisible(window.scrollRow, window.selectedIndex, visibleRowCount, items.length);
|
||||
const lastVisibleRow = Math.min(items.length, window.scrollRow + visibleRowCount);
|
||||
for (let index = window.scrollRow; index < lastVisibleRow; index++) {
|
||||
const y = listTop + (index - window.scrollRow) * hubRowH;
|
||||
const item = items[index]!;
|
||||
if (item.heading) {
|
||||
image.drawText(chromeFont(), 20, y + 2, item.label, 140);
|
||||
continue;
|
||||
}
|
||||
const selected = index === window.selectedIndex;
|
||||
if (selected) {
|
||||
// Match the shell convention: fill only when this window has focus, so
|
||||
// an outline-only selection signals the sidebar owns input.
|
||||
drawSelectionHighlight(image, 20, y - 2, window.viewportWidth - 40, hubRowH - 1, window.focused, 8);
|
||||
}
|
||||
// Activity gutter, open-window number column, then the label, truncated
|
||||
// to the row (shared with the Glanceboard's Terminal widget).
|
||||
drawSessionRow(image, chromeFont(), {
|
||||
x: 22,
|
||||
y: y + 2,
|
||||
width: window.viewportWidth - 22 - 26,
|
||||
label: item.label,
|
||||
openGlyph: item.openGlyph ?? null,
|
||||
active: Boolean(item.active),
|
||||
phase: hubAnimationPhase,
|
||||
value: selected ? 255 : 200,
|
||||
});
|
||||
}
|
||||
if (items.length > visibleRowCount) {
|
||||
drawListScrollbar(
|
||||
image,
|
||||
window.viewportWidth - 10,
|
||||
listTop,
|
||||
visibleRowCount * hubRowH - 4,
|
||||
window.scrollRow,
|
||||
visibleRowCount,
|
||||
items.length,
|
||||
);
|
||||
}
|
||||
const list = hubMenu(window);
|
||||
list.setItems(hubItems(window));
|
||||
// Selection boxes start above the text line; the menu fills the selected
|
||||
// box only while this window has focus (an outline alone means the sidebar
|
||||
// owns input), matching the shell convention.
|
||||
const listHeight = window.viewportHeight - 6 - listTop;
|
||||
list.paint(
|
||||
image,
|
||||
{ x: HUB_LIST_X, y: listTop - 2, width: window.viewportWidth - 2 * HUB_LIST_X, height: listHeight },
|
||||
window.focused,
|
||||
);
|
||||
list.drawScrollbar(image, window.viewportWidth - 10, listTop, listHeight - 4);
|
||||
|
||||
return image;
|
||||
}
|
||||
|
||||
+104
-89
@@ -31,14 +31,12 @@ import {
|
||||
} from "../../ui/gestures";
|
||||
import { type Layer, type LayerContext } from "../../ui/layers";
|
||||
import {
|
||||
drawListScrollbar,
|
||||
drawRightValueMenuItem,
|
||||
drawSelectionHighlight,
|
||||
drawToggleMenuItem,
|
||||
scrollToKeepSelectionVisible,
|
||||
TextPageLayer,
|
||||
type MenuItem,
|
||||
} from "../../ui/menu";
|
||||
import { Menu, type MenuDrawArgs } from "../../ui/menu-core";
|
||||
import { LIST_ROW_TEXT_INSET, lineStep, listRowHeight } from "../../ui/metrics";
|
||||
import { createInProcessWindow, type InProcessAppOptions, type InProcessWindow } from "../../ui/shell/in-process-window";
|
||||
import { shell } from "../../ui/shell/shell";
|
||||
@@ -91,6 +89,8 @@ const LIST_SHARE = 0.4;
|
||||
const LIST_MIN_WIDTH = 190;
|
||||
const LIST_MAX_WIDTH = 240;
|
||||
const LIST_TOP = 6;
|
||||
/** Pixels between consecutive list rows' selection boxes. */
|
||||
const LIST_ROW_GAP = 1;
|
||||
const STAGE_X = 10;
|
||||
/** Gap between the stage's right edge and the list's divider. */
|
||||
const STAGE_LIST_GAP = 14;
|
||||
@@ -183,8 +183,22 @@ function fittingDigitFont(text: string, maxWidth: number, sizes: readonly number
|
||||
export class TimersLayer implements Layer {
|
||||
readonly acceptsDirectional = true;
|
||||
|
||||
/**
|
||||
* The selected row's key: the source of truth for the selection, which
|
||||
* survives row rebuilds and is pinned to newly ringing items. The list
|
||||
* menu is re-synced from it before each paint, hit test and move.
|
||||
*/
|
||||
private selectionKey = "";
|
||||
private scrollRow = 0;
|
||||
private readonly list = new Menu<Row>({
|
||||
wrap: true,
|
||||
rowGap: LIST_ROW_GAP,
|
||||
highlight: { radius: 6 },
|
||||
getHeight: () => listRowHeight(smallFont()),
|
||||
isSelectable: (row) => row.kind !== "header",
|
||||
draw: (args) => this.drawListRow(args),
|
||||
});
|
||||
/** Per-paint inputs to drawListRow. */
|
||||
private listPaint = { dim: 1, now: 0 };
|
||||
private mode: Mode = { kind: "list" };
|
||||
/** Which of the ringing stage's two buttons is highlighted. */
|
||||
private ringButton = 0;
|
||||
@@ -232,7 +246,8 @@ export class TimersLayer implements Layer {
|
||||
let index = rows.findIndex((row) => row.key === this.selectionKey);
|
||||
if (index < 0) {
|
||||
// The row went away (a dismissed timer, a deleted alarm): stay in place.
|
||||
index = clamp(this.scrollRow, 0, rows.length - 1);
|
||||
const firstVisibleRow = Math.floor(this.list.scrollTop / listRowHeight(smallFont()));
|
||||
index = clamp(firstVisibleRow, 0, rows.length - 1);
|
||||
while (index < rows.length && rows[index]!.kind === "header") index++;
|
||||
if (index >= rows.length) index = rows.findIndex((row) => row.kind !== "header");
|
||||
this.selectionKey = rows[index]!.key;
|
||||
@@ -244,14 +259,16 @@ export class TimersLayer implements Layer {
|
||||
return rows[this.reconcileSelection(rows)]!;
|
||||
}
|
||||
|
||||
private moveSelection(rows: Row[], delta: number): void {
|
||||
const count = rows.length;
|
||||
let index = this.reconcileSelection(rows);
|
||||
for (let step = 0; step < count; step++) {
|
||||
index = (index + delta + count) % count;
|
||||
if (rows[index]!.kind !== "header") break;
|
||||
}
|
||||
this.selectionKey = rows[index]!.key;
|
||||
/** Point the list menu at these rows and the current selection. */
|
||||
private syncList(rows: Row[]): void {
|
||||
this.list.setItems(rows, this.reconcileSelection(rows));
|
||||
}
|
||||
|
||||
private moveSelection(rows: Row[], delta: -1 | 1): void {
|
||||
this.syncList(rows);
|
||||
this.list.moveSelection(delta);
|
||||
const index = this.list.selectedIndex;
|
||||
if (index !== null) this.selectionKey = rows[index]!.key;
|
||||
}
|
||||
|
||||
/** Called on every engine change: jump to a newly ringing item. */
|
||||
@@ -547,15 +564,14 @@ export class TimersLayer implements Layer {
|
||||
/** A mirror touch: on the list, select the row under it. */
|
||||
hitTest(x: number, y: number, ctx: LayerContext): boolean {
|
||||
if (this.mode.kind !== "list") return false;
|
||||
const { width, height } = ctx.stack.getBaseSize();
|
||||
const { width } = ctx.stack.getBaseSize();
|
||||
const listX = width - listWidthFor(width);
|
||||
if (x < listX) return false;
|
||||
const rows = this.buildRows(Date.now());
|
||||
this.reconcileSelection(rows);
|
||||
const rowHeight = listRowHeight(smallFont());
|
||||
const index = this.scrollRow + Math.floor((y - LIST_TOP) / rowHeight);
|
||||
const row = rows[index];
|
||||
if (!row || row.kind === "header" || y < LIST_TOP || y > height) return true;
|
||||
this.syncList(rows);
|
||||
const index = this.list.indexAt(y);
|
||||
if (index === null) return true;
|
||||
const row = rows[index]!;
|
||||
if (this.selectionKey === row.key) {
|
||||
this.activateRow(row);
|
||||
} else {
|
||||
@@ -882,79 +898,78 @@ export class TimersLayer implements Layer {
|
||||
dim: number,
|
||||
now: number,
|
||||
): void {
|
||||
const font = smallFont();
|
||||
const rowHeight = listRowHeight(font);
|
||||
const rowHeight = listRowHeight(smallFont());
|
||||
const visible = Math.max(1, Math.floor((height - 2 * LIST_TOP) / rowHeight));
|
||||
this.scrollRow = scrollToKeepSelectionVisible(this.scrollRow, selectedIndex, visible, rows.length);
|
||||
this.listPaint = { dim, now };
|
||||
// While a picker owns the stage the list has no live selection, so no
|
||||
// highlight; drawListRow still brightens the selected row by key.
|
||||
this.list.setItems(rows, dim === 1 ? selectedIndex : null);
|
||||
this.list.paint(image, { x: listX, y: LIST_TOP, width: listWidth - 12, height: visible * rowHeight - LIST_ROW_GAP }, focused);
|
||||
this.list.drawScrollbar(image, listX + listWidth - 7, LIST_TOP, visible * rowHeight - 4);
|
||||
}
|
||||
|
||||
private drawListRow({ image, item: row, x, y, width, height }: MenuDrawArgs<Row>): void {
|
||||
const font = smallFont();
|
||||
const { dim, now } = this.listPaint;
|
||||
const rowHeight = height + LIST_ROW_GAP;
|
||||
const shade = (value: number) => Math.round(value * dim);
|
||||
const textX = listX + 8;
|
||||
const textWidth = listWidth - 24;
|
||||
const last = Math.min(rows.length, this.scrollRow + visible);
|
||||
for (let index = this.scrollRow; index < last; index++) {
|
||||
const row = rows[index]!;
|
||||
const y = LIST_TOP + (index - this.scrollRow) * rowHeight;
|
||||
const textY = y + LIST_ROW_TEXT_INSET;
|
||||
if (row.kind === "header") {
|
||||
image.drawText(font, textX, textY, row.title!, shade(90));
|
||||
continue;
|
||||
}
|
||||
const selected = index === selectedIndex;
|
||||
if (selected && dim === 1) {
|
||||
drawSelectionHighlight(image, listX, y, listWidth - 12, rowHeight - 1, focused, 6);
|
||||
}
|
||||
const bright = shade(selected ? 250 : 210);
|
||||
const soft = shade(selected ? 170 : 130);
|
||||
switch (row.kind) {
|
||||
case "timer": {
|
||||
const timer = row.timer!;
|
||||
const phase = timerPhase(timer);
|
||||
const digits = phase === "rung" ? "0:00" : formatCountdown(timerRemainingMs(timer, now));
|
||||
const digitsValue = phase === "rung" ? (Math.floor(now / 1000) % 2 ? bright : soft) : phase === "paused" ? soft : bright;
|
||||
image.drawText(font, textX, textY, digits, digitsValue);
|
||||
const digitsWidth = font.measureText(digits) + 8;
|
||||
const nameWidth = textWidth - digitsWidth - (phase === "paused" ? 12 : 0);
|
||||
if (nameWidth > 12) {
|
||||
image.drawText(font, textX + digitsWidth, textY, truncateText(font, timerDisplayName(timer), nameWidth), soft);
|
||||
}
|
||||
if (phase === "paused") drawPauseMark(image, listX + listWidth - 22, y + Math.floor(rowHeight / 2) - 5, soft);
|
||||
break;
|
||||
}
|
||||
case "new-timer":
|
||||
image.drawText(font, textX, textY, "+ New timer", shade(selected ? 220 : 150));
|
||||
break;
|
||||
case "stopwatch": {
|
||||
const stopwatch = timerEngine.state.stopwatch;
|
||||
const running = stopwatchIsRunning(stopwatch);
|
||||
const elapsed = formatElapsed(stopwatchElapsedMs(stopwatch, now), false);
|
||||
image.drawText(font, textX, textY, "Stopwatch", running ? bright : soft);
|
||||
image.drawText(font, listX + listWidth - 16 - font.measureText(elapsed), textY, elapsed, running ? bright : soft);
|
||||
break;
|
||||
}
|
||||
case "alarm": {
|
||||
const alarm = row.alarm!;
|
||||
const on = alarm.enabled;
|
||||
const time = formatTimeOfDay(alarm.hour, alarm.minute, twelveHour());
|
||||
image.drawText(font, textX, textY, time, on ? bright : soft);
|
||||
const timeWidth = font.measureText(time) + 8;
|
||||
const daysWidth = textWidth - timeWidth - 14;
|
||||
if (daysWidth > 12) {
|
||||
image.drawText(font, textX + timeWidth, textY, truncateText(font, alarm.label || formatDays(alarm.days), daysWidth), on ? soft : shade(80));
|
||||
}
|
||||
const dotX = listX + listWidth - 22;
|
||||
const dotY = y + Math.floor(rowHeight / 2) - 4;
|
||||
if (alarm.ringingSinceMs !== null || on) image.fillRoundedRect(dotX, dotY, 8, 8, on ? shade(220) : shade(90), 4);
|
||||
else image.drawRoundedRect(dotX, dotY, 8, 8, shade(90), 4);
|
||||
break;
|
||||
}
|
||||
case "new-alarm":
|
||||
image.drawText(font, textX, textY, "+ New alarm", shade(selected ? 220 : 150));
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
const textX = x + 8;
|
||||
const textWidth = width - 12;
|
||||
const textY = y + LIST_ROW_TEXT_INSET;
|
||||
if (row.kind === "header") {
|
||||
image.drawText(font, textX, textY, row.title!, shade(90));
|
||||
return;
|
||||
}
|
||||
if (rows.length > visible) {
|
||||
drawListScrollbar(image, listX + listWidth - 7, LIST_TOP, visible * rowHeight - 4, this.scrollRow, visible, rows.length);
|
||||
const selected = row.key === this.selectionKey;
|
||||
const bright = shade(selected ? 250 : 210);
|
||||
const soft = shade(selected ? 170 : 130);
|
||||
switch (row.kind) {
|
||||
case "timer": {
|
||||
const timer = row.timer!;
|
||||
const phase = timerPhase(timer);
|
||||
const digits = phase === "rung" ? "0:00" : formatCountdown(timerRemainingMs(timer, now));
|
||||
const digitsValue = phase === "rung" ? (Math.floor(now / 1000) % 2 ? bright : soft) : phase === "paused" ? soft : bright;
|
||||
image.drawText(font, textX, textY, digits, digitsValue);
|
||||
const digitsWidth = font.measureText(digits) + 8;
|
||||
const nameWidth = textWidth - digitsWidth - (phase === "paused" ? 12 : 0);
|
||||
if (nameWidth > 12) {
|
||||
image.drawText(font, textX + digitsWidth, textY, truncateText(font, timerDisplayName(timer), nameWidth), soft);
|
||||
}
|
||||
if (phase === "paused") drawPauseMark(image, x + width - 10, y + Math.floor(rowHeight / 2) - 5, soft);
|
||||
break;
|
||||
}
|
||||
case "new-timer":
|
||||
image.drawText(font, textX, textY, "+ New timer", shade(selected ? 220 : 150));
|
||||
break;
|
||||
case "stopwatch": {
|
||||
const stopwatch = timerEngine.state.stopwatch;
|
||||
const running = stopwatchIsRunning(stopwatch);
|
||||
const elapsed = formatElapsed(stopwatchElapsedMs(stopwatch, now), false);
|
||||
image.drawText(font, textX, textY, "Stopwatch", running ? bright : soft);
|
||||
image.drawText(font, x + width - 4 - font.measureText(elapsed), textY, elapsed, running ? bright : soft);
|
||||
break;
|
||||
}
|
||||
case "alarm": {
|
||||
const alarm = row.alarm!;
|
||||
const on = alarm.enabled;
|
||||
const time = formatTimeOfDay(alarm.hour, alarm.minute, twelveHour());
|
||||
image.drawText(font, textX, textY, time, on ? bright : soft);
|
||||
const timeWidth = font.measureText(time) + 8;
|
||||
const daysWidth = textWidth - timeWidth - 14;
|
||||
if (daysWidth > 12) {
|
||||
image.drawText(font, textX + timeWidth, textY, truncateText(font, alarm.label || formatDays(alarm.days), daysWidth), on ? soft : shade(80));
|
||||
}
|
||||
const dotX = x + width - 10;
|
||||
const dotY = y + Math.floor(rowHeight / 2) - 4;
|
||||
if (alarm.ringingSinceMs !== null || on) image.fillRoundedRect(dotX, dotY, 8, 8, on ? shade(220) : shade(90), 4);
|
||||
else image.drawRoundedRect(dotX, dotY, 8, 8, shade(90), 4);
|
||||
break;
|
||||
}
|
||||
case "new-alarm":
|
||||
image.drawText(font, textX, textY, "+ New alarm", shade(selected ? 220 : 150));
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
*
|
||||
* Two expiry paths run side by side: a JS deadline for prompt on-glasses
|
||||
* ringing while the process is awake, and an AlarmManager alarm-clock alarm
|
||||
* (FaceclawAlarms.java) that rings on the phone at the right moment even if
|
||||
* (FaceclawAlarms.kt) that rings on the phone at the right moment even if
|
||||
* the process is asleep or dead. The phone shows the item silently first
|
||||
* and adds its own sound when the glasses cannot carry it (not connected,
|
||||
* not worn, charging, not shown within seconds, or not acknowledged within
|
||||
|
||||
+57
-26
@@ -1,12 +1,11 @@
|
||||
import { getDefaultLargeFont, getDefaultMediumFont, getDefaultSmallFont } from "../../graphics/ui-fonts";
|
||||
import { GrayImage, type UiFont } from "../../graphics/image";
|
||||
import { wrapText, truncateText } from "../../graphics/textwrap";
|
||||
import { clamp } from "../../util/numeric-util";
|
||||
import { type ForecastPeriod, type WeatherState } from "../../native/weather";
|
||||
import { GESTURE_CLICK, type InputEvent } from "../../ui/gestures";
|
||||
import { type Layer, type LayerContext } from "../../ui/layers";
|
||||
import { drawSelectionHighlight, scrollToKeepSelectionVisible } from "../../ui/menu";
|
||||
import { lineStep, tightRowHeight } from "../../ui/metrics";
|
||||
import { Menu, type MenuDrawArgs } from "../../ui/menu-core";
|
||||
import { centeredTextY, lineStep, tightRowHeight } from "../../ui/metrics";
|
||||
|
||||
const PAGE_X = 18;
|
||||
const HEADER_Y = 8;
|
||||
@@ -18,11 +17,26 @@ const FORECAST_TOP = 124;
|
||||
const FORECAST_MIN_ROW_HEIGHT = 23;
|
||||
/** Gap between forecast columns. */
|
||||
const FORECAST_COL_GAP = 14;
|
||||
/** Left edge of the forecast rows' selection boxes. */
|
||||
const FORECAST_BOX_X = PAGE_X - 7;
|
||||
/** Pixels between consecutive forecast rows' selection boxes. */
|
||||
const FORECAST_ROW_GAP = 2;
|
||||
|
||||
/** Forecast column x offsets from the row box's left edge. */
|
||||
type ForecastColumns = { name: number; temp: number; precip: number; summary: number };
|
||||
|
||||
/** Weather's current-conditions summary and scrollable 12-hour forecast. */
|
||||
export class WeatherLayer implements Layer {
|
||||
private selectedIndex = 0;
|
||||
private scrollRow = 0;
|
||||
/** Forecast table rows: selection only, a click does nothing there. */
|
||||
private readonly forecastMenu = new Menu<ForecastPeriod>({
|
||||
wrap: false,
|
||||
rowGap: FORECAST_ROW_GAP,
|
||||
highlight: { radius: 5 },
|
||||
getHeight: () => forecastRowHeight(getDefaultSmallFont()),
|
||||
draw: (args) => this.drawForecastRow(args),
|
||||
});
|
||||
/** Column layout for drawForecastRow, set by each drawForecast. */
|
||||
private columns: ForecastColumns = { name: 0, temp: 0, precip: 0, summary: 0 };
|
||||
|
||||
constructor(
|
||||
private readonly state: () => WeatherState,
|
||||
@@ -81,10 +95,9 @@ export class WeatherLayer implements Layer {
|
||||
return;
|
||||
}
|
||||
if (!weather.forecast.length || weather.phase !== "ready") return;
|
||||
if (event.type === "scroll-up") {
|
||||
this.selectedIndex = Math.max(0, this.selectedIndex - 1);
|
||||
} else if (event.type === "scroll-down") {
|
||||
this.selectedIndex = Math.min(weather.forecast.length - 1, this.selectedIndex + 1);
|
||||
if (event.type === "scroll-up" || event.type === "scroll-down") {
|
||||
this.forecastMenu.setItems(weather.forecast);
|
||||
void this.forecastMenu.handleInput(event);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -134,12 +147,14 @@ export class WeatherLayer implements Layer {
|
||||
image.drawText(font, PAGE_X, FORECAST_TOP, "No upcoming forecast periods.", 160);
|
||||
return;
|
||||
}
|
||||
this.selectedIndex = clamp(this.selectedIndex, 0, forecast.length - 1);
|
||||
const rowH = Math.max(FORECAST_MIN_ROW_HEIGHT, tightRowHeight(font) + 3);
|
||||
const rowH = forecastRowHeight(font);
|
||||
// The last row only needs its text line (not a full row pitch of
|
||||
// clearance below), which usually fits one more row before the bottom.
|
||||
// The menu only draws rows whose whole selection box fits, so the box
|
||||
// holds that many rows, cut at the image's bottom edge.
|
||||
const visibleRows = Math.max(1, 1 + Math.floor((height - FORECAST_TOP - (font.lineHeight + 4)) / rowH));
|
||||
this.scrollRow = scrollToKeepSelectionVisible(this.scrollRow, this.selectedIndex, visibleRows, forecast.length);
|
||||
const boxTop = FORECAST_TOP - 2;
|
||||
const boxHeight = Math.min(visibleRows * rowH - FORECAST_ROW_GAP, height - boxTop);
|
||||
|
||||
// Columns are sized to the widest period name so names ("Wednesday
|
||||
// Night") never truncate; the summary column absorbs what's left.
|
||||
@@ -155,20 +170,31 @@ export class WeatherLayer implements Layer {
|
||||
image.drawText(font, tempX, FORECAST_HEADER_Y, "Temp", 105);
|
||||
image.drawText(font, precipX, FORECAST_HEADER_Y, "Rain", 105);
|
||||
|
||||
const last = Math.min(forecast.length, this.scrollRow + visibleRows);
|
||||
for (let index = this.scrollRow; index < last; index++) {
|
||||
const period = forecast[index]!;
|
||||
const y = FORECAST_TOP + (index - this.scrollRow) * rowH;
|
||||
const selected = index === this.selectedIndex;
|
||||
if (selected) {
|
||||
drawSelectionHighlight(image, PAGE_X - 7, y - 2, width - 2 * (PAGE_X - 7), rowH - 2, ctx.stack.isFocused(), 5);
|
||||
}
|
||||
const shade = selected ? 245 : 190;
|
||||
image.drawText(font, PAGE_X, y + 2, period.name, shade);
|
||||
image.drawText(font, tempX, y + 2, period.temperatureF === null ? "--" : `${Math.round(period.temperatureF)}°F`, shade);
|
||||
image.drawText(font, precipX, y + 2, period.precipitationPercent === null ? "--" : `${Math.round(period.precipitationPercent)}%`, selected ? 220 : 155);
|
||||
image.drawText(font, summaryX, y + 2, truncateText(font, period.shortForecast, width - summaryX - PAGE_X), selected ? 220 : 165);
|
||||
}
|
||||
this.columns = {
|
||||
name: PAGE_X - FORECAST_BOX_X,
|
||||
temp: tempX - FORECAST_BOX_X,
|
||||
precip: precipX - FORECAST_BOX_X,
|
||||
summary: summaryX - FORECAST_BOX_X,
|
||||
};
|
||||
this.forecastMenu.setItems(forecast);
|
||||
this.forecastMenu.paint(
|
||||
image,
|
||||
{ x: FORECAST_BOX_X, y: boxTop, width: width - 2 * FORECAST_BOX_X, height: boxHeight },
|
||||
ctx.stack.isFocused(),
|
||||
);
|
||||
}
|
||||
|
||||
private drawForecastRow({ image, item: period, x, y, width, height, selected }: MenuDrawArgs<ForecastPeriod>): void {
|
||||
const font = getDefaultSmallFont();
|
||||
const columns = this.columns;
|
||||
const shade = selected ? 245 : 190;
|
||||
const textY = centeredTextY(font, y, height);
|
||||
image.drawText(font, x + columns.name, textY, period.name, shade);
|
||||
image.drawText(font, x + columns.temp, textY, period.temperatureF === null ? "--" : `${Math.round(period.temperatureF)}°F`, shade);
|
||||
image.drawText(font, x + columns.precip, textY, period.precipitationPercent === null ? "--" : `${Math.round(period.precipitationPercent)}%`, selected ? 220 : 155);
|
||||
// The summary stops PAGE_X short of the viewport's right edge, as the name starts PAGE_X in from the left.
|
||||
const summaryWidth = width - columns.summary - columns.name;
|
||||
image.drawText(font, x + columns.summary, textY, truncateText(font, period.shortForecast, summaryWidth), selected ? 220 : 165);
|
||||
}
|
||||
|
||||
private drawMessage(
|
||||
@@ -190,6 +216,11 @@ export class WeatherLayer implements Layer {
|
||||
}
|
||||
}
|
||||
|
||||
/** Forecast row pitch, including the gap between selection boxes. */
|
||||
function forecastRowHeight(font: UiFont): number {
|
||||
return Math.max(FORECAST_MIN_ROW_HEIGHT, tightRowHeight(font) + 3);
|
||||
}
|
||||
|
||||
function formatAge(ageMs: number): string {
|
||||
const minutes = Math.max(0, Math.round(ageMs / 60_000));
|
||||
if (minutes < 1) return "just now";
|
||||
|
||||
@@ -1,15 +0,0 @@
|
||||
import { fromData, toData, nativeArray } from '../native/kotlin-data'
|
||||
declare const FaceclawKitIosProtocol: any
|
||||
const protocol = FaceclawKitIosProtocol.new()
|
||||
export function buildBoundingBoxPayload(previous: Uint8Array | null, next: Uint8Array, width: number, height: number, frameId: number): Uint8Array | null {
|
||||
if (!Number.isInteger(width) || !Number.isInteger(height) || width <= 0 || height <= 0 ||
|
||||
width % 4 || height % 2 || width / 4 > 255 || height / 2 > 255 ||
|
||||
!previous || previous.length !== width * height / 2 || next.length !== previous.length) return null
|
||||
const result = protocol.boundingBoxPreviousNextWidthHeightFrameId(toData(previous), toData(next), width, height, frameId)
|
||||
return result ? fromData(result) : null
|
||||
}
|
||||
export function buildFullFrameBands(packed: Uint8Array, width: number, height: number, firstId: number): Uint8Array[] {
|
||||
if (width <= 0 || width > 640 || width % 4 || height <= 0 || height > 480 || height % 2 || packed.length !== width * height / 2)
|
||||
throw new Error('Invalid framebuffer dimensions')
|
||||
return nativeArray(protocol.fullFrameBandsDataWidthHeightFirstId(toData(packed), width, height, firstId), fromData)
|
||||
}
|
||||
@@ -1,49 +0,0 @@
|
||||
import { concat, rle4 } from './ble-protocol'
|
||||
|
||||
/** Android BleImageOptimizer's mode-3 changed rectangle. Coordinates are in
|
||||
* units of 4 columns / 2 rows; pixels are top-down packed 4bpp, high nibble first.
|
||||
* null means identical, incomparable, or a full-screen box: use dedup/mode 6. Compression belongs to the CFW transport.
|
||||
*/
|
||||
export function buildBoundingBoxPayload(previous: Uint8Array | null, next: Uint8Array,
|
||||
width: number, height: number, frameId: number): Uint8Array | null {
|
||||
if (!Number.isInteger(width) || !Number.isInteger(height) || width <= 0 || height <= 0 ||
|
||||
width % 4 || height % 2 || width / 4 > 255 || height / 2 > 255 ||
|
||||
!previous || previous.length !== width * height / 2 || next.length !== previous.length) return null
|
||||
const stride = width / 2
|
||||
let minX = stride, maxX = -1, minY = height, maxY = -1
|
||||
for (let y = 0; y < height; y++) {
|
||||
const row = y * stride
|
||||
for (let x = 0; x < stride; x++) {
|
||||
if (previous[row + x] === next[row + x]) continue
|
||||
minX = Math.min(minX, x); maxX = Math.max(maxX, x)
|
||||
minY = Math.min(minY, y); maxY = y
|
||||
}
|
||||
}
|
||||
if (maxY < 0) return null
|
||||
const left = (minX * 2) & ~3, right = Math.min(width, ((maxX + 1) * 2 + 3) & ~3)
|
||||
const top = minY & ~1, bottom = Math.min(height, (maxY + 2) & ~1)
|
||||
const boxWidth = right - left, boxHeight = bottom - top
|
||||
if (boxWidth === width && boxHeight === height) return null
|
||||
const regionStride = boxWidth / 2, region = new Uint8Array(regionStride * boxHeight)
|
||||
for (let y = 0; y < boxHeight; y++) {
|
||||
const start = (top + y) * stride + left / 2
|
||||
region.set(next.subarray(start, start + regionStride), y * regionStride)
|
||||
}
|
||||
return concat(new Uint8Array([3, left / 4, top / 2, boxWidth / 4, boxHeight / 2,
|
||||
frameId & 255, (frameId >>> 8) & 255]), rle4(region))
|
||||
}
|
||||
|
||||
/** Full repaint as independent 64-row mode-3 bands when RLE exceeds uint16. */
|
||||
export function buildFullFrameBands(packed: Uint8Array, width: number, height: number, firstId: number): Uint8Array[] {
|
||||
if (width <= 0 || width > 640 || width % 4 || height <= 0 || height > 480 || height % 2 || packed.length !== width * height / 2)
|
||||
throw new Error('Invalid framebuffer dimensions')
|
||||
const bands: Uint8Array[] = []
|
||||
let id = firstId
|
||||
for (let top = 0; top < height; top += 64) {
|
||||
const rows = Math.min(64, height - top)
|
||||
bands.push(concat(new Uint8Array([3, 0, top / 2, width / 4, rows / 2, id & 255, id >>> 8]),
|
||||
rle4(packed.subarray(top * width / 2, (top + rows) * width / 2))))
|
||||
id = id >= 0xfffe ? 1 : id + 1
|
||||
}
|
||||
return bands
|
||||
}
|
||||
@@ -1,203 +0,0 @@
|
||||
import { type RingInput, decodeRingMetadata } from "./ring-input";
|
||||
import { type CompassEvent } from '../native/compass-types'
|
||||
/** G2 wire protocol, ported from g2protocol/BleProtocol.java. No platform APIs. */
|
||||
import { OsEventTypeList } from './events'
|
||||
export const G2_WRITE = '00002760-08c2-11e1-9073-0e8ac72e5401'
|
||||
export const G2_NOTIFY = '00002760-08c2-11e1-9073-0e8ac72e5402'
|
||||
export const G2_RENDER_NOTIFY = '00002760-08c2-11e1-9073-0e8ac72e6402'
|
||||
export const RING_NOTIFY = ['bae80011-4f05-4503-8e65-3af1f7329d1f', 'bae80013-4f05-4503-8e65-3af1f7329d1f']
|
||||
export const SID = { auth: 0x80, launch: 1, hub: 0xe0, settings: 9, cfw: 0xf0 } as const
|
||||
export function concat(...parts: Uint8Array[]): Uint8Array {
|
||||
const output = new Uint8Array(parts.reduce((n, p) => n + p.length, 0))
|
||||
let offset = 0; for (const part of parts) { output.set(part, offset); offset += part.length }; return output
|
||||
}
|
||||
function varint(value: number): Uint8Array {
|
||||
const bytes: number[] = []
|
||||
do { const next = value & 127; value >>>= 7; bytes.push(next | (value ? 128 : 0)) } while (value)
|
||||
return new Uint8Array(bytes)
|
||||
}
|
||||
export const integer = (field: number, value: number): Uint8Array => concat(varint(field * 8), varint(value))
|
||||
export const bytes = (field: number, value: Uint8Array): Uint8Array => concat(varint(field * 8 + 2), varint(value.length), value)
|
||||
// Outbound names/text here are fixed ASCII protocol identifiers.
|
||||
const string = (field: number, value: string): Uint8Array => bytes(field, new Uint8Array(Array.from(value, c => c.charCodeAt(0))))
|
||||
const wrap = (command: number, magic: number, field: number, body: Uint8Array) => concat(integer(1, command), integer(2, magic), bytes(field, body))
|
||||
export const authentication = (magic: number) => wrap(4, magic, 3, concat(integer(1, 1), integer(2, 4)))
|
||||
export const prelude = () => wrap(2, 156, 4, bytes(3, bytes(2, bytes(2, concat(integer(1, 0), integer(2, 0))))))
|
||||
export const heartbeat = (magic: number) => wrap(12, magic, 14, integer(1, 0))
|
||||
export const audioControl = (magic: number, enabled: boolean) => wrap(15, magic, 18, integer(1, enabled ? 1 : 0))
|
||||
export const settingsQuery = (magic: number) => wrap(2, magic, 4, integer(1, 1))
|
||||
export const shutdown = (magic: number) => wrap(9, magic, 11, integer(1, 1))
|
||||
export const framebufferLease = (acquire: boolean) => concat(integer(1, 1), integer(2, 0), bytes(101, new Uint8Array([70, 67, 1, acquire ? 5 : 6, 0, 0])))
|
||||
export function createLayout(magic: number): Uint8Array {
|
||||
const geometry = concat(integer(1, 0), integer(2, 0), integer(3, 576), integer(4, 288))
|
||||
const text = concat(geometry, integer(9, 1), string(10, 'dashboard'), integer(11, 1), string(12, ' '))
|
||||
return wrap(0, magic, 3, concat(integer(1, 1), bytes(3, text), integer(5, 10000)))
|
||||
}
|
||||
export function crc16(data: Uint8Array): number {
|
||||
let crc = 0xffff
|
||||
for (const byte of data) {
|
||||
crc ^= byte << 8
|
||||
for (let i = 0; i < 8; i++) crc = ((crc << 1) ^ ((crc & 0x8000) ? 0x1021 : 0)) & 0xffff
|
||||
}
|
||||
return crc
|
||||
}
|
||||
export function frameMessage(payload: Uint8Array, sid: number, flag: number, sequence: number, maxWrite = 240): Uint8Array[] {
|
||||
const chunkSize = Math.min(232, Math.floor(maxWrite) - 8)
|
||||
if (chunkSize < 12) throw new Error('Negotiated BLE write size is too small')
|
||||
const crc = crc16(payload), withCrc = concat(payload, new Uint8Array([crc & 255, crc >>> 8]))
|
||||
const count = Math.ceil(withCrc.length / chunkSize)
|
||||
if (count > 255) throw new Error('Protocol message exceeds the fragment limit')
|
||||
return Array.from({ length: count }, (_, i) => {
|
||||
const chunk = withCrc.subarray(i * chunkSize, (i + 1) * chunkSize)
|
||||
return concat(new Uint8Array([0xaa, 0x21, sequence & 255, chunk.length, count, i + 1, sid, flag]), chunk)
|
||||
})
|
||||
}
|
||||
export type ProtocolMessage = { sid: number; flag: number; payload: Uint8Array; command: number; magic: number; packet?: Uint8Array }
|
||||
/** Handles split/coalesced envelopes and reassembles each link/sequence separately. */
|
||||
export class MessageReceiver {
|
||||
private streams = new Map<string, Uint8Array>()
|
||||
private messages = new Map<string, { chunks: Uint8Array[]; total: number; seen: number; size: number }>()
|
||||
clear(): void { this.streams.clear(); this.messages.clear() }
|
||||
receive(link: string, data: Uint8Array, now = Date.now()): ProtocolMessage[] {
|
||||
for (const [key, message] of this.messages) if (now - message.seen > 5000) this.messages.delete(key)
|
||||
let stream = concat(this.streams.get(link) ?? new Uint8Array(), data)
|
||||
const output: ProtocolMessage[] = []
|
||||
while (stream.length >= 8) {
|
||||
if (stream[0] !== 0xaa || ![0x21, 0x12].includes(stream[1])) { stream = stream.subarray(1); continue }
|
||||
const length = stream[3], count = stream[4], index = stream[5]
|
||||
if (stream.length < length + 8) break
|
||||
const sid = stream[6], flag = stream[7], key = `${link}:${stream[2]}:${sid}:${flag}`
|
||||
const packet = stream.slice(0, length + 8)
|
||||
const chunk = stream.slice(8, 8 + length); stream = stream.subarray(length + 8)
|
||||
if (sid === SID.cfw) {
|
||||
output.push({ sid, flag, payload: chunk, command: -1, magic: -1, packet })
|
||||
continue
|
||||
}
|
||||
if (!count || !index || index > count) { this.messages.delete(key); continue }
|
||||
if (index === 1) this.messages.set(key, { chunks: [], total: count, seen: now, size: 0 })
|
||||
const message = this.messages.get(key)
|
||||
if (!message || message.total !== count || message.chunks.length + 1 !== index) { this.messages.delete(key); continue }
|
||||
message.chunks.push(chunk); message.size += chunk.length; message.seen = now
|
||||
if (message.size > 65536) { this.messages.delete(key); continue }
|
||||
if (index !== count) continue
|
||||
this.messages.delete(key)
|
||||
const complete = concat(...message.chunks)
|
||||
if (complete.length < 2) continue
|
||||
const payload = complete.subarray(0, -2), crc = complete[complete.length - 2] | (complete[complete.length - 1] << 8)
|
||||
if (crc16(payload) !== crc) continue
|
||||
try { output.push({ sid, flag, payload, command: readInteger(payload, 1, -1), magic: readInteger(payload, 2, -1) }) }
|
||||
catch { /* Malformed notifications cannot satisfy an acknowledgement. */ }
|
||||
}
|
||||
this.streams.set(link, stream.slice())
|
||||
return output
|
||||
}
|
||||
}
|
||||
function readVarint(data: Uint8Array, offset: number): [number, number] {
|
||||
let value = 0
|
||||
for (let i = 0; i < 5 && offset < data.length; i++) {
|
||||
const byte = data[offset++]; value += (byte & 127) * 2 ** (i * 7)
|
||||
if (!(byte & 128)) return [value, offset]
|
||||
}
|
||||
throw new Error('Truncated or oversized protobuf varint')
|
||||
}
|
||||
function field(data: Uint8Array, number: number): number | Uint8Array | undefined {
|
||||
let offset = 0
|
||||
while (offset < data.length) {
|
||||
let key: number; [key, offset] = readVarint(data, offset)
|
||||
const tag = key >>> 3, wire = key & 7
|
||||
if (!tag) throw new Error('Invalid protobuf field')
|
||||
if (wire === 0) {
|
||||
let value: number; [value, offset] = readVarint(data, offset)
|
||||
if (tag === number) return value
|
||||
} else {
|
||||
let length: number
|
||||
if (wire === 2) [length, offset] = readVarint(data, offset)
|
||||
else if (wire === 1) length = 8
|
||||
else if (wire === 5) length = 4
|
||||
else throw new Error('Unsupported protobuf wire type')
|
||||
if (offset + length > data.length) throw new Error('Truncated protobuf field')
|
||||
if (tag === number && wire === 2) return data.subarray(offset, offset + length)
|
||||
offset += length
|
||||
}
|
||||
}
|
||||
return undefined
|
||||
}
|
||||
export function readInteger(data: Uint8Array, number: number, fallback = 0): number { const value = field(data, number); return typeof value === 'number' ? value : fallback }
|
||||
export function readBytes(data: Uint8Array, number: number): Uint8Array | undefined { const value = field(data, number); return value instanceof Uint8Array ? value : undefined }
|
||||
export function readString(data: Uint8Array, number: number): string { return Array.from(readBytes(data, number) ?? [], value => String.fromCharCode(value)).join('') }
|
||||
export function authenticationSucceeded(message: ProtocolMessage, magic: number): boolean {
|
||||
return message.sid === SID.auth && message.command === 4 && message.magic === magic && readBytes(message.payload, 3)?.length === 0
|
||||
}
|
||||
export type GlassesInput = { ringInput?: RingInput; kind: 'sys-event' | 'list-click' | 'text-click' | 'display-wake' | 'even-ai'; eventType: number; eventSource: number; systemExitReasonCode: number; containerName: string; frameId: number }
|
||||
export function decodeGlassesInput(message: ProtocolMessage): GlassesInput | null {
|
||||
if (message.sid !== SID.hub || ![1, 6].includes(message.flag)) return null
|
||||
const events = readBytes(message.payload, 13); if (!events) return null
|
||||
for (const [field, kind, typeField] of [[1, 'list-click', 5], [2, 'text-click', 3], [3, 'sys-event', 1]] as const) {
|
||||
const event = readBytes(events, field)
|
||||
if (event) return { kind: kind === 'sys-event' && readInteger(event, typeField) === OsEventTypeList.HEAD_UP_EVENT ? 'display-wake' : kind,
|
||||
eventType: readInteger(event, typeField), eventSource: field === 3 ? readInteger(event, 2) : 0,
|
||||
ringInput: field === 3 && readInteger(event, 2) === 2 ? decodeRingMetadata(readBytes(event, 100)) : undefined,
|
||||
systemExitReasonCode: field === 3 ? readInteger(event, 4) : 0, containerName: field === 3 ? '' : readString(event, 2), frameId: 0 }
|
||||
}
|
||||
return null
|
||||
}
|
||||
export function decodeRingInput(data: Uint8Array): GlassesInput | null {
|
||||
let eventType: number | undefined
|
||||
if (data.length === 11 && data[0] === 0 && data[1] === 9 && data[2] === 0x61 && data[3] === 0)
|
||||
eventType = ({ 0: 9, 1: 0, 2: 3, 4: 1, 5: 2, 8: 10 } as Record<number, number>)[data[4]]
|
||||
if (data.length === 3 && data[0] === 255) {
|
||||
if (data[1] === 3 && data[2] === 32) eventType = 9
|
||||
if (data[1] === 4 && data[2] === 1) eventType = 0
|
||||
if (data[1] === 4 && data[2] === 2) eventType = 3
|
||||
if (data[1] === 5) eventType = data[2] <= 1 ? 2 : 1
|
||||
}
|
||||
return eventType === undefined ? null : { kind: 'sys-event', eventType, eventSource: 2, systemExitReasonCode: 0, containerName: '', frameId: 0 }
|
||||
}
|
||||
export function packGray4(gray: Uint8Array, width: number, height: number): Uint8Array {
|
||||
if (!Number.isInteger(width) || !Number.isInteger(height) || width < 1 || height < 1 || gray.length !== width * height) throw new Error('Invalid display frame')
|
||||
const stride = Math.ceil(width / 2), packed = new Uint8Array(stride * height)
|
||||
for (let y = 0; y < height; y++) for (let x = 0; x < width; x++) {
|
||||
const nibble = Math.min(15, (gray[y * width + x] + 8) >> 4)
|
||||
packed[y * stride + (x >> 1)] |= nibble << (x % 2 ? 0 : 4)
|
||||
}
|
||||
return packed
|
||||
}
|
||||
export function rle4(packed: Uint8Array): Uint8Array {
|
||||
const output = new Uint8Array(packed.length * 2)
|
||||
const nibble = (i: number) => (packed[i >> 1] >> (i % 2 ? 0 : 4)) & 15
|
||||
let used = 0, i = 0
|
||||
while (i < packed.length * 2) {
|
||||
const color = nibble(i); let end = i + 1
|
||||
while (end < packed.length * 2 && nibble(end) === color) end++
|
||||
let remaining = end - i
|
||||
while (remaining) {
|
||||
const count = Math.min(65535, remaining)
|
||||
if (count <= 15) output[used++] = (count << 4) | color
|
||||
else { output[used++] = color; if (count <= 255) output[used++] = count
|
||||
else { output[used++] = 0; output[used++] = count & 255; output[used++] = count >>> 8 } }
|
||||
remaining -= count
|
||||
}
|
||||
i = end
|
||||
}
|
||||
return output.slice(0, used)
|
||||
}
|
||||
|
||||
/** Reference decoder for host tests; iOS uses the shared Kotlin implementation. */
|
||||
export function decodeCompassInput(message: ProtocolMessage): CompassEvent | null {
|
||||
if (message.sid !== 8 || ![1, 6].includes(message.flag)) return null
|
||||
const command = readInteger(message.payload, 1, -1)
|
||||
if (command === 16 || command === 17) return { command, headingDegrees: -1 }
|
||||
if (command !== 15) return null
|
||||
const compass = readBytes(message.payload, 10)
|
||||
const headingDegrees = compass ? readInteger(compass, 1, -1) : -1
|
||||
if (headingDegrees < 0 || headingDegrees >= 360) return null
|
||||
const event: CompassEvent = { command, headingDegrees }
|
||||
let d: Uint8Array | undefined
|
||||
try { d = readBytes(message.payload, 100) } catch { return event }
|
||||
if (d?.length === 12 && d[0] === 67 && d[1] === 77 && d[2] === 1 && d[7] === 0 &&
|
||||
(d[3] <= 3 || d[3] === 255) && (d[4] <= 2 || d[4] === 255) && d[5] <= 3) {
|
||||
event.diagnostics = { magneticAccuracy: d[3] === 255 ? -1 : d[3], magneticAnomalies: d[4] === 255 ? -1 : d[4],
|
||||
orientationSource: d[5], flags: d[6], sampleTimeMs: new DataView(d.buffer, d.byteOffset + 8, 4).getUint32(0, true) }
|
||||
}
|
||||
return event
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
/** Same units as Android FaceclawBleManager: protocol messages, framed write
|
||||
* bytes accepted by the transport, and fully acknowledged display updates.
|
||||
* Totals survive reconnects and the setting being switched off.
|
||||
*/
|
||||
export class BleTrafficCounters {
|
||||
private messages = 0
|
||||
private bytes = 0
|
||||
private frames = 0
|
||||
recordWrite(bytes: number): void { this.bytes += bytes }
|
||||
recordMessage(): void { this.messages++ }
|
||||
recordDisplayFrame(): void { this.frames++ }
|
||||
sample(): { messages: number; bytes: number; frames: number } {
|
||||
return { messages: this.messages, bytes: this.bytes, frames: this.frames }
|
||||
}
|
||||
}
|
||||
// The iOS BLE session and phone UI both run in the main isolate.
|
||||
export const iosBleTraffic = new BleTrafficCounters()
|
||||
@@ -0,0 +1,26 @@
|
||||
export const DEFAULT_BRIGHTNESS_CURVE = "0:0,0.2:7,1.5:33,5:100";
|
||||
const PREVIOUS_DEFAULT_CURVE = "0:0,1:10,10:30,100:65,1000:100";
|
||||
|
||||
/** Percent of the configured range; defaults (20–100) give 20/26/46/100. */
|
||||
export function brightnessCurveError(value: string): string | null {
|
||||
const pairs = value.split(",").map(part => part.trim().split(":"));
|
||||
if (pairs.length < 2 || pairs.length > 16 || pairs.some(p => p.length !== 2 || p.some(s => !s.trim())))
|
||||
return "Use 2–16 lux:percent pairs separated by commas.";
|
||||
const points = pairs.map(pair => pair.map(Number));
|
||||
if (points.some(([lux, percent]) => !Number.isFinite(lux) || !Number.isFinite(percent)
|
||||
|| lux < 0 || lux > 1_000_000 || percent < 0 || percent > 100))
|
||||
return "Lux must be 0–1000000 and percent must be 0–100.";
|
||||
if (points[0][0] !== 0 || points[0][1] !== 0 || points[points.length - 1][1] !== 100)
|
||||
return "Start at 0:0 and end at 100%. Set dark-room brightness with Auto minimum.";
|
||||
if (points.some(([lux, percent], i) => i > 0 && (lux <= points[i-1][0] || percent < points[i-1][1])))
|
||||
return "Lux must increase and brightness must not decrease.";
|
||||
return null;
|
||||
}
|
||||
|
||||
/** Keep incomplete drafts intact. Validation must never replace typed input. */
|
||||
export function normalizeBrightnessCurve(value: string | null | undefined): string {
|
||||
if (value == null) return DEFAULT_BRIGHTNESS_CURVE;
|
||||
if (brightnessCurveError(value)) return value;
|
||||
const canonical = value.split(",").map(pair => pair.split(":").map(Number).join(":")).join(",");
|
||||
return canonical === PREVIOUS_DEFAULT_CURVE ? DEFAULT_BRIGHTNESS_CURVE : canonical;
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
import { bytes, concat, integer } from './ble-protocol'
|
||||
|
||||
/** Same G2SettingPackage as Kotlin BleProtocol.buildSetBrightness. */
|
||||
export function setBrightness(magic: number, level: number | null): Uint8Array {
|
||||
const fields = level === null ? integer(1, 1)
|
||||
: concat(integer(1, 0), integer(2, Math.max(0, Math.min(100, Math.round(level)))))
|
||||
return concat(integer(1, 1), integer(2, magic), bytes(3, bytes(1, fields)))
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
/** Message IDs and header flags mirror g2flash/patches/zlib_glue.c. */
|
||||
/** Retired wire ID; used internally by the phone optimizer. */
|
||||
export const CFW_MSG_BOUNDING_BOX = 3
|
||||
export const CFW_MSG_BUZZER = 5
|
||||
/** Retired wire ID; used internally by the phone optimizer. */
|
||||
export const CFW_MSG_FULL_FRAME = 6
|
||||
export const CFW_MSG_DIAGNOSTICS = 7
|
||||
/** Retired wire ID; used internally by the phone optimizer. */
|
||||
export const CFW_MSG_MULTI_SEGMENT = 8
|
||||
/** Retired wire ID; used internally by the phone optimizer. */
|
||||
export const CFW_MSG_RECT_COPY = 9
|
||||
export const CFW_MSG_COMPASS = 10
|
||||
export const CFW_MSG_CLEANUP = 11
|
||||
export const CFW_MSG_RETIRED_12 = 12
|
||||
export const CFW_MSG_RETIRED_13 = 13
|
||||
export const CFW_MSG_RETIRED_14 = 14
|
||||
/** Retired wire ID; used internally by the phone optimizer. */
|
||||
export const CFW_MSG_STOCK_FONT_STRING = 15
|
||||
export const CFW_MSG_AMBIENT_LIGHT = 16
|
||||
export const CFW_MSG_RING_BATTERY = 17
|
||||
export const CFW_MSG_RETIRED_CACHE_WRITE = 18
|
||||
/** Retired wire ID; used internally by the phone optimizer. */
|
||||
export const CFW_MSG_CACHED_IMAGE = 19
|
||||
/** Retired wire ID; used internally by the phone optimizer. */
|
||||
export const CFW_MSG_CACHED_TEXT = 20
|
||||
export const CFW_MSG_UPLOAD_RESOURCE = 21
|
||||
export const CFW_MSG_EVICT_RESOURCE = 22
|
||||
export const CFW_MSG_PANEL_READ = 23
|
||||
export const CFW_MSG_PANEL_WRITE = 24
|
||||
export const CFW_MSG_PANEL_PATTERN = 25
|
||||
export const CFW_MSG_DRAW_CALLS = 26
|
||||
export const CFW_MSG_SET_ROOT_DISPLAY_LIST = 27
|
||||
export const CFW_MSG_PRESENT = 28
|
||||
export const CFW_MSG_CREATE_SURFACE = 29
|
||||
export const CFW_MSG_BRIGHTNESS = 30
|
||||
export const CFW_MSG_TYPE_MASK = 127
|
||||
export const CFW_MSG_FLAG_LENSES_DIFFER = 128
|
||||
@@ -1,21 +0,0 @@
|
||||
import { fromData, toData, nativeArray } from '../native/kotlin-data'
|
||||
declare const FaceclawKitIosCfwTransport: any, FaceclawKitIosProtocol: any
|
||||
export const CFW_MAX_MESSAGE = 65535
|
||||
export type CfwAck = { nack: boolean; streamId: number; messageId: number; lens: number; size: number; checksum: number }
|
||||
/** maxWrite is the ATT value limit; Kotlin owns the persistent zlib stream. */
|
||||
export class CfwTransport {
|
||||
private native = FaceclawKitIosCfwTransport.new()
|
||||
reset(): void { this.native.reset() }
|
||||
encode(message: Uint8Array, streamId: number, lenses: number, maxWrite: number): Uint8Array[] {
|
||||
if (message.length > CFW_MAX_MESSAGE || !Number.isInteger(streamId) || streamId < 0 || streamId > 255 ||
|
||||
![1, 2, 3].includes(lenses) || !Number.isInteger(maxWrite) || maxWrite < 20 || maxWrite > 514)
|
||||
throw new Error('Invalid CFW stream parameters')
|
||||
return nativeArray(this.native.encodeDataStreamIdLensesMaxWrite(toData(message), streamId, lenses, maxWrite), fromData)
|
||||
}
|
||||
}
|
||||
const protocol = FaceclawKitIosProtocol.new()
|
||||
export function parseCfwAcks(packet: Uint8Array): CfwAck[] | null {
|
||||
const acks = protocol.acksData(toData(packet))
|
||||
return acks ? nativeArray(acks, (ack: any) => ({ nack: ack.nack, streamId: ack.streamId, messageId: ack.messageId,
|
||||
lens: ack.lens, size: ack.size, checksum: ack.checksum })) : null
|
||||
}
|
||||
@@ -1,56 +0,0 @@
|
||||
import { concat, crc16, SID } from './ble-protocol'
|
||||
|
||||
declare const require: (name: string) => any
|
||||
export const CFW_MAX_MESSAGE = 65535
|
||||
export type CfwAck = { nack: boolean; streamId: number; messageId: number; lens: number; size: number; checksum: number }
|
||||
const u16 = (data: Uint8Array, offset: number) => data[offset] | (data[offset + 1] << 8)
|
||||
|
||||
/** Matches Android CfwTransport. maxWrite is the ATT value limit (MTU - 3). */
|
||||
export class CfwTransport {
|
||||
private compressor: any
|
||||
private resetPending = true
|
||||
private lenses = 0
|
||||
reset(): void { this.compressor = null; this.resetPending = true; this.lenses = 0 }
|
||||
encode(message: Uint8Array, streamId: number, lenses: number, maxWrite: number): Uint8Array[] {
|
||||
if (message.length > CFW_MAX_MESSAGE || !Number.isInteger(streamId) || streamId < 0 || streamId > 255 ||
|
||||
![1, 2, 3].includes(lenses) || !Number.isInteger(maxWrite) || maxWrite < 20 || maxWrite > 514)
|
||||
throw new Error('Invalid CFW stream parameters')
|
||||
if (this.lenses !== lenses) this.reset()
|
||||
if (!this.compressor) this.compressor = new (require('pako').Deflate)()
|
||||
const chunks: Uint8Array[] = []
|
||||
// SYNC_FLUSH emits this record while retaining history for the next one.
|
||||
this.compressor.onData = (chunk: Uint8Array) => chunks.push(chunk.slice())
|
||||
if (!this.compressor.push(message, 2)) throw new Error(this.compressor.msg || 'CFW compression failed')
|
||||
let body = concat(...chunks), flags = lenses | 4 | (this.resetPending ? 8 : 0)
|
||||
if (!body.length) flags &= ~4
|
||||
if (body.length > CFW_MAX_MESSAGE) {
|
||||
this.reset(); body = message; flags = lenses | 8
|
||||
} else { this.resetPending = false; this.lenses = lenses }
|
||||
const checksum = crc16(message)
|
||||
const record = concat(new Uint8Array([flags, body.length & 255, body.length >>> 8, checksum & 255, checksum >>> 8]), body)
|
||||
const capacity = Math.min(252, maxWrite - 11), packets: Uint8Array[] = []
|
||||
for (let offset = 0, index = 0; offset < record.length; offset += capacity, index++) {
|
||||
const body = concat(new Uint8Array([lenses | (offset === 0 ? 0x80 : 0) |
|
||||
(offset + capacity >= record.length ? 0x40 : 0)]), record.subarray(offset, offset + capacity))
|
||||
const crc = crc16(body)
|
||||
packets.push(concat(new Uint8Array([0xaa, 0x21, (streamId + index) & 255, body.length + 2, 1, 1, SID.cfw, 0]),
|
||||
body, new Uint8Array([crc & 255, crc >>> 8])))
|
||||
}
|
||||
return packets
|
||||
}
|
||||
}
|
||||
|
||||
/** Validate the entire primary + redundant-ACK packet before applying any ACK. */
|
||||
export function parseCfwAcks(packet: Uint8Array): CfwAck[] | null {
|
||||
const n = packet.length
|
||||
if (n < 19 || n > 40 || (n - 19) % 7 || packet[0] !== 0xaa || packet[1] !== 0x12 ||
|
||||
packet[3] !== n - 8 || packet[4] !== 1 || packet[5] !== 1 || packet[6] !== SID.cfw || packet[7] !== 0 ||
|
||||
![1, 3].includes(packet[8]) || ![1, 2].includes(packet[12]) || (packet[8] === 3 && n !== 19) ||
|
||||
crc16(packet.subarray(8, -2)) !== u16(packet, n - 2)) return null
|
||||
const acks = [{ nack: packet[8] === 3, streamId: packet[9], messageId: u16(packet, 10),
|
||||
lens: packet[12], size: u16(packet, 13), checksum: u16(packet, 15) }]
|
||||
for (let offset = 17; offset < n - 2; offset += 7)
|
||||
acks.push({ nack: false, streamId: packet[offset], messageId: u16(packet, offset + 1), lens: packet[12],
|
||||
size: u16(packet, offset + 3), checksum: u16(packet, offset + 5) })
|
||||
return acks
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
import { startRemoteInput } from "../remote/service";
|
||||
import { acceptInput, resetRingInputFilter } from "../ui/input-monitor";
|
||||
import { Application, ImageSource } from "@nativescript/core";
|
||||
import { Application, Dialogs, ImageSource } from "@nativescript/core";
|
||||
import { EvenAIStatus, EvenAIStatusName, EventSourceType, EventSourceTypeName, OsEventTypeList, OsEventTypeName, WatchGestureType, WatchGestureTypeName } from "./events";
|
||||
import { isValidMacAddress, loadDeviceAddresses } from "./device-addresses";
|
||||
import {
|
||||
@@ -71,7 +71,7 @@ import { loadPersistedOpenApps, savePersistedOpenApps } from "../ui/shell/open-a
|
||||
import { appViewportRect, SIDEBAR_WIDTH, sidebarStripVisible, type WindowHeightMode } from "../ui/shell/geometry";
|
||||
import { type LayerActions, type TextSettingsEditToggle } from "../ui/layers";
|
||||
import { type KeyboardInputSession } from "../ui/shell/keyboard-input";
|
||||
import { assistantAllowProactiveSetting, assistantBackendSetting, assistantBridgeHostSetting, assistantBridgePortSetting, assistantBridgeTokenSetting, brightnessSetting, brightnessSettingToLevel, displayModeSetting, navigateDisplayModeSetting, navigateVerticalPositionSetting, terminalDisplayModeSetting, terminalVerticalPositionSetting, elevenLabsApiKeySetting, getStringSettingById, openAiApiKeySetting, nightscoutApiTokenSetting, firmwareDebugFlagsSetting, lockScreenEnabledSetting, nightscoutSiteUrlSetting, onAnySettingChanged, previewColorSetting, ringConnectionModeSetting, saveVoiceRecordingsSetting, sonioxApiKeySetting, screenTimeoutSetting, screenTimeoutSettingToMs, suspendEvenHubWhenScreenOffSetting, verticalPositionSetting, voiceProviderSetting, wakeWordActionSetting, type ConfigSettingString } from "../ui/dashboard-settings";
|
||||
import { assistantAllowProactiveSetting, assistantBackendSetting, assistantBridgeHostSetting, assistantBridgePortSetting, assistantBridgeTokenSetting, getBrightnessPreferences, displayModeSetting, navigateDisplayModeSetting, navigateVerticalPositionSetting, terminalDisplayModeSetting, terminalVerticalPositionSetting, elevenLabsApiKeySetting, getStringSettingById, openAiApiKeySetting, nightscoutApiTokenSetting, firmwareDebugFlagsSetting, lockScreenEnabledSetting, nightscoutSiteUrlSetting, onAnySettingChanged, previewColorSetting, ringConnectionModeSetting, saveVoiceRecordingsSetting, sonioxApiKeySetting, screenTimeoutSetting, screenTimeoutSettingToMs, suspendEvenHubWhenScreenOffSetting, verticalPositionSetting, voiceProviderSetting, wakeWordActionSetting, type ConfigSettingString } from "../ui/dashboard-settings";
|
||||
import { isIgnoringBatteryOptimizations, requestIgnoreBatteryOptimizations } from "../native/battery-optimization";
|
||||
import {
|
||||
getInstalledEvenHubAppById,
|
||||
@@ -213,6 +213,13 @@ class DashboardController {
|
||||
*/
|
||||
private activeTextEditorOnCancel: (() => void) | null = null;
|
||||
private activeTextEditorToggle: TextSettingsEditToggle | null = null;
|
||||
/**
|
||||
* The active settings' values (and the toggle's) when the edit began. The
|
||||
* phone fields write through on every keystroke, so this is what the
|
||||
* editor's Cancel button puts back.
|
||||
*/
|
||||
private activeTextSettingOriginalValues: string[] = [];
|
||||
private activeTextEditorToggleOriginalValue = false;
|
||||
private evenNotificationActive = false;
|
||||
private evenAppConflictMessage = "";
|
||||
private firmwareWarningMessage = "";
|
||||
@@ -276,7 +283,6 @@ class DashboardController {
|
||||
private lockSurfaceConfigured = false;
|
||||
private lastLockScreenEnabled = lockScreenEnabledSetting.get();
|
||||
private offState: (() => void) | null = null;
|
||||
private offLog: (() => void) | null = null;
|
||||
private offRing: (() => void) | null = null;
|
||||
private offBattery: (() => void) | null = null;
|
||||
private offSilentMode: (() => void) | null = null;
|
||||
@@ -909,11 +915,14 @@ class DashboardController {
|
||||
*/
|
||||
private pushBrightness(force = false): void {
|
||||
if (!this.communicator) return;
|
||||
const value = brightnessSetting.get();
|
||||
const preferences = getBrightnessPreferences();
|
||||
const value = JSON.stringify(preferences);
|
||||
if (!force && value === this.lastPushedBrightness) return;
|
||||
this.lastPushedBrightness = value;
|
||||
const level = brightnessSettingToLevel(value);
|
||||
void this.communicator.setBrightness(level === null, level ?? 0).catch(() => {});
|
||||
void this.communicator.configureBrightness(preferences).catch((error) => {
|
||||
this.lastPushedBrightness = null;
|
||||
this.appendLog(`brightness configuration failed: ${this.formatError(error)}`);
|
||||
});
|
||||
}
|
||||
|
||||
/** Save the occupied part of the composited screen as a 4-bit grayscale PNG. */
|
||||
@@ -1336,9 +1345,13 @@ class DashboardController {
|
||||
ring: ringAddress,
|
||||
});
|
||||
this.communicator = communicator;
|
||||
this.offLog = communicator.onLog((line) => {
|
||||
this.appendLog(line);
|
||||
});
|
||||
// Size the compositor before anything else is awaited: windows opened
|
||||
// during the bridge start-ups below (the terminal hub, restored apps)
|
||||
// configure their surfaces through this.communicator, and the Java call
|
||||
// queue is FIFO, so enqueuing the screen size first guarantees it lands
|
||||
// ahead of them. Mirrors ensurePreviewDisplay.
|
||||
await communicator.configureCompositorScreen(G2_LENS_WIDTH, G2_LENS_HEIGHT);
|
||||
await communicator.configureBrightness(getBrightnessPreferences());
|
||||
this.offState = communicator.onStateChange((state) => {
|
||||
if (state.phase !== "connected") resetRingInputFilter();
|
||||
if (state.phase === "unpaired") {
|
||||
@@ -1506,7 +1519,6 @@ class DashboardController {
|
||||
await nightscoutBridge.start();
|
||||
// Register the compositor surfaces: the shell chrome above all windows,
|
||||
// and a surface per live window (only the foreground one is composited).
|
||||
await communicator.configureCompositorScreen(G2_LENS_WIDTH, G2_LENS_HEIGHT);
|
||||
await communicator.configureSurface(SHELL_SURFACE_ID, {
|
||||
x: 0,
|
||||
y: 0,
|
||||
@@ -1551,8 +1563,6 @@ class DashboardController {
|
||||
const message = this.formatError(error);
|
||||
this.offState?.();
|
||||
this.offState = null;
|
||||
this.offLog?.();
|
||||
this.offLog = null;
|
||||
this.offRing?.();
|
||||
this.offRing = null;
|
||||
this.offBattery?.();
|
||||
@@ -1691,8 +1701,6 @@ class DashboardController {
|
||||
this.clearDashboardTimer();
|
||||
this.offState?.();
|
||||
this.offState = null;
|
||||
this.offLog?.();
|
||||
this.offLog = null;
|
||||
this.offRing?.();
|
||||
this.offRing = null;
|
||||
this.offBattery?.();
|
||||
@@ -1896,9 +1904,11 @@ class DashboardController {
|
||||
onCancel?: () => void,
|
||||
): void {
|
||||
this.activeTextSettings = Array.from(settings.slice(0, 2));
|
||||
this.activeTextSettingOriginalValues = this.activeTextSettings.map((setting) => setting.get());
|
||||
this.activeTextEditorTitle = title;
|
||||
this.activeTextEditorOnFinish = onFinish ?? null;
|
||||
this.activeTextEditorToggle = toggle ?? null;
|
||||
this.activeTextEditorToggleOriginalValue = toggle?.setting.get() ?? false;
|
||||
this.activeTextEditorOnCancel = onCancel ?? null;
|
||||
this.emit();
|
||||
}
|
||||
@@ -2020,6 +2030,7 @@ class DashboardController {
|
||||
// finishTextSettingEdit clears this first, so a completed edit never fires it.
|
||||
const onCancel = this.activeTextEditorOnCancel;
|
||||
this.activeTextSettings = [];
|
||||
this.activeTextSettingOriginalValues = [];
|
||||
this.activeTextEditorTitle = "";
|
||||
this.activeTextEditorOnFinish = null;
|
||||
this.activeTextEditorToggle = null;
|
||||
@@ -2038,6 +2049,11 @@ class DashboardController {
|
||||
*/
|
||||
finishActiveTextSettingEdit(): void {
|
||||
if (!this.activeTextSettings.length) return;
|
||||
const invalid = this.activeTextSettings.find(setting => setting.validationError());
|
||||
if (invalid) {
|
||||
void Dialogs.alert({ title: invalid.editorTitle, message: invalid.validationError()!, okButtonText: "OK" });
|
||||
return;
|
||||
}
|
||||
const onFinish = this.activeTextEditorOnFinish;
|
||||
// Completing is not cancelling: drop the cancel callback before
|
||||
// endTextSettingEdit, which fires whatever is still set.
|
||||
@@ -2050,6 +2066,28 @@ class DashboardController {
|
||||
onFinish?.();
|
||||
}
|
||||
|
||||
/**
|
||||
* The phone editor's Cancel button: put back the values the edit started
|
||||
* with, end the edit as a cancel (the caller's onCancel fires), and
|
||||
* navigate the Settings app's glasses editor out of the edit page.
|
||||
*/
|
||||
cancelActiveTextSettingEdit(): void {
|
||||
if (!this.activeTextSettings.length) return;
|
||||
this.activeTextSettings.forEach((setting, index) => {
|
||||
const original = this.activeTextSettingOriginalValues[index] ?? "";
|
||||
if (setting.get() !== original) setting.set(original);
|
||||
});
|
||||
const toggle = this.activeTextEditorToggle;
|
||||
if (toggle && toggle.setting.get() !== this.activeTextEditorToggleOriginalValue) {
|
||||
toggle.setting.set(this.activeTextEditorToggleOriginalValue);
|
||||
}
|
||||
const closesGlassesEditor = this.activeTextSettings.length === 1;
|
||||
this.endTextSettingEdit();
|
||||
if (closesGlassesEditor && this.textEditorHost?.closeTextEditor()) {
|
||||
this.textEditorHost.requestRender();
|
||||
}
|
||||
}
|
||||
|
||||
private updateTextSetting(setting: ConfigSettingString, value: string): void {
|
||||
if (setting.get() !== value) {
|
||||
setting.set(value);
|
||||
@@ -2255,6 +2293,7 @@ class DashboardController {
|
||||
const host = new WorkerAppHost({
|
||||
appId,
|
||||
worker: createWorker(),
|
||||
onStopping: () => { if (this.appHosts.get(appId) === host) this.appHosts.delete(appId); },
|
||||
configureSurface: (surfaceId, visible, heightMode) =>
|
||||
this.configureWindowSurface(surfaceId, visible, heightMode),
|
||||
setSurfaceVisible: (surfaceId, visible) => this.setWindowSurfaceVisible(surfaceId, visible),
|
||||
@@ -2499,7 +2538,7 @@ class DashboardController {
|
||||
beginRenderPass(!wantFreshData);
|
||||
const paintStartedAtMs = Date.now();
|
||||
const planes = frameTimings.span(frameId, "paint", () =>
|
||||
frameTimings.runWithFrame(frameId, () => shell.paintSurface()),
|
||||
frameTimings.runWithFrame(frameId, () => shell.paintScene()),
|
||||
);
|
||||
const paintMs = Date.now() - paintStartedAtMs;
|
||||
const paintUsedStaleData = endRenderPass();
|
||||
@@ -2513,36 +2552,11 @@ class DashboardController {
|
||||
}
|
||||
// The display-target check that used to live here now runs before the
|
||||
// paint, at the top of this function. Nothing between the two can change
|
||||
// it -- paintSurface is synchronous and there is no await in between -- so
|
||||
// it -- paintScene is synchronous and there is no await in between -- so
|
||||
// repeating it here is dead code, and the compiler agrees: the early
|
||||
// return narrows this.phase, making the "charging" comparison impossible.
|
||||
const display = this.display;
|
||||
// A shell overlay that dims what it covers (a context menu) must dim the
|
||||
// window surfaces too, which live below the shell surface in the
|
||||
// compositor: forward the factor before this frame composites.
|
||||
const underlayDim = shell.underlayDim();
|
||||
if (underlayDim !== this.appliedUnderlayDim) {
|
||||
this.appliedUnderlayDim = underlayDim;
|
||||
await display.setUnderlayDim(SHELL_SURFACE_Z_ORDER, underlayDim);
|
||||
}
|
||||
const fingerprint = frameTimings.span(frameId, "fingerprint", () => planesFingerprint(planes));
|
||||
const { image, draws } = frameTimings.span(frameId, "flatten", () => flattenPlanesWithDraws(planes));
|
||||
const buffer = frameTimings.span(frameId, "to8bpp", () => image.to8bppBuffer());
|
||||
const preparedDraws = frameTimings.span(frameId, "prepareFrameDraws", () => prepareFrameDraws(draws));
|
||||
// Spanned because the bridge serializes Java calls: a frame can sit here
|
||||
// behind another surface's submission, which is otherwise an unexplained
|
||||
// jump between the paint spans and the composite.
|
||||
await frameTimings.spanAsync(frameId, "submit", () =>
|
||||
display.submitSurfaceFrame(
|
||||
SHELL_SURFACE_ID,
|
||||
buffer,
|
||||
{ x: 0, y: 0, width: image.width, height: image.height },
|
||||
fingerprint,
|
||||
paintMs,
|
||||
frameId,
|
||||
preparedDraws,
|
||||
),
|
||||
);
|
||||
await frameTimings.spanAsync(frameId, "submit", () => display.submitShellScene(planes, paintMs, frameId));
|
||||
// Backpressure: the next shell render waits for this one to reach the
|
||||
// glasses. Timing out here means the loop was blocked for the full timeout
|
||||
// and any input arriving meanwhile had its chrome repaint delayed, so say
|
||||
@@ -2651,7 +2665,7 @@ class DashboardController {
|
||||
}
|
||||
|
||||
private appendLog(line: string): void {
|
||||
console.log(`[${formatTimestamp(new Date())}] ${line}`);
|
||||
//console.log(`[${formatTimestamp(new Date())}] ${line}`);
|
||||
}
|
||||
|
||||
private setDisplayPreview(preview: ImageSource | null): void {
|
||||
@@ -2668,6 +2682,22 @@ class DashboardController {
|
||||
*/
|
||||
private previewTrailingTimer: ReturnType<typeof setTimeout> | null = null;
|
||||
private lastRecordCaptureAtMs = 0;
|
||||
private phonePreviewVisible: (() => boolean) | null = null;
|
||||
|
||||
/** The main page owns the mirror; other phone pages must not keep it rendering. */
|
||||
attachPhonePreview(isVisible: () => boolean): () => void {
|
||||
this.phonePreviewVisible = isVisible;
|
||||
this.lastConnectedPreviewUpdateAtMs = 0;
|
||||
this.updateCompositePreview();
|
||||
return () => {
|
||||
if (this.phonePreviewVisible !== isVisible) return;
|
||||
this.phonePreviewVisible = null;
|
||||
if (this.previewTrailingTimer) {
|
||||
clearTimeout(this.previewTrailingTimer);
|
||||
this.previewTrailingTimer = null;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Refresh the preview now if the floor allows, otherwise once the floor
|
||||
@@ -2675,6 +2705,7 @@ class DashboardController {
|
||||
* of waiting for the safety-net poll.
|
||||
*/
|
||||
private schedulePreviewUpdate(): void {
|
||||
if (!this.phonePreviewVisible && !this.screenRecordingActive) return;
|
||||
if (this.previewTrailingTimer) return;
|
||||
const wait = this.lastConnectedPreviewUpdateAtMs + CONNECTED_PREVIEW_MIN_UPDATE_MS - Date.now();
|
||||
if (wait <= 0) {
|
||||
@@ -2702,6 +2733,13 @@ class DashboardController {
|
||||
return;
|
||||
}
|
||||
this.lastConnectedPreviewUpdateAtMs = now;
|
||||
// A user-requested GIF is an independent consumer, including while the
|
||||
// phone is asleep or another page/app is foregrounded.
|
||||
if (this.screenRecordingActive && now - this.lastRecordCaptureAtMs >= RECORDING_MIN_CAPTURE_MS) {
|
||||
this.lastRecordCaptureAtMs = now;
|
||||
display.recordScreenFrame();
|
||||
}
|
||||
if (!this.phonePreviewVisible?.()) return;
|
||||
// The connected foreground service intentionally keeps this controller
|
||||
// alive after the phone UI is backgrounded. Do not keep constructing
|
||||
// 640x480 Android Bitmaps for a window that cannot display them: besides
|
||||
@@ -2710,10 +2748,8 @@ class DashboardController {
|
||||
if (global.isAndroid) {
|
||||
const activity = Application.android.foregroundActivity;
|
||||
if (!activity || !activity.hasWindowFocus()) return;
|
||||
}
|
||||
if (this.screenRecordingActive && now - this.lastRecordCaptureAtMs >= RECORDING_MIN_CAPTURE_MS) {
|
||||
this.lastRecordCaptureAtMs = now;
|
||||
display.recordScreenFrame();
|
||||
const power = activity.getSystemService(android.content.Context.POWER_SERVICE) as android.os.PowerManager;
|
||||
if (!power?.isInteractive()) return;
|
||||
}
|
||||
const preview = display.getCompositePreview(previewColorSetting.get() === "green");
|
||||
if (preview) {
|
||||
|
||||
@@ -26,7 +26,7 @@ export type FirmwareInfo = {
|
||||
};
|
||||
|
||||
/** The Faceclaw firmware revision this build of the app needs. */
|
||||
export const REQUIRED_FACECLAW_FIRMWARE_VERSION = 22;
|
||||
export const REQUIRED_FACECLAW_FIRMWARE_VERSION = 34;
|
||||
|
||||
const FACECLAW_PREFIX = "Faceclaw/";
|
||||
const LEGACY_PREFIX = "EVENCFW";
|
||||
|
||||
@@ -1,91 +0,0 @@
|
||||
/** Stock EVENOTA validation and transfer, matching Android's flasher/g2flash.py. */
|
||||
import { StockConnection, OTA_WRITE, OTA_NOTIFY } from './stock-connection'
|
||||
import { concat } from './ble-protocol'
|
||||
import type { FlashProgress } from './firmware-types'
|
||||
|
||||
export type FirmwareSegment = { offset: number; size: number; name: string }
|
||||
const CRC_TABLE = Array.from({ length: 256 }, (_, b) => {
|
||||
let c = b << 24
|
||||
for (let i = 0; i < 8; i++) c = c & 0x80000000 ? (c << 1) ^ 0x1edc6f41 : c << 1
|
||||
return c >>> 0
|
||||
})
|
||||
export function firmwareCrc(data: Uint8Array): number {
|
||||
let crc = 0
|
||||
for (const byte of data) crc = (crc << 8) ^ CRC_TABLE[((crc >>> 24) ^ byte) & 255]
|
||||
return crc >>> 0
|
||||
}
|
||||
export function validateFirmware(image: Uint8Array): FirmwareSegment[] {
|
||||
const u32 = (offset: number) => {
|
||||
if (offset < 0 || offset + 4 > image.length) throw new Error('Firmware header is truncated.')
|
||||
return new DataView(image.buffer, image.byteOffset, image.byteLength).getUint32(offset, true)
|
||||
}
|
||||
if (image.length < 0x40) throw new Error('File is too small to be a firmware image.')
|
||||
const count = u32(8)
|
||||
if (count < 5 || count > 6 || 0x40 + count * 16 > image.length) throw new Error('Expected 5–6 firmware components.')
|
||||
const segments: FirmwareSegment[] = []
|
||||
for (let i = 0; i < count; i++) {
|
||||
const toc = 0x40 + i * 16, offset = u32(toc + 4)
|
||||
if (offset < 0x40 + count * 16 || offset + 128 > image.length) throw new Error('Firmware component header is out of bounds.')
|
||||
const size = u32(offset + 8), end = offset + 128 + size
|
||||
if (!size || end > image.length) throw new Error('Firmware component payload is truncated.')
|
||||
if (segments.some(s => offset < s.offset + 128 + s.size && s.offset < end)) throw new Error('Firmware components overlap.')
|
||||
const nameBytes = image.subarray(offset + 48, offset + 128)
|
||||
const name = String.fromCharCode(...nameBytes.subarray(0, nameBytes.indexOf(0) < 0 ? 80 : nameBytes.indexOf(0)))
|
||||
const crc = firmwareCrc(image.subarray(offset + 128, end))
|
||||
if (crc !== u32(toc + 12) || crc !== u32(offset + 12)) throw new Error(`Component ${name} CRC32C is stale (image not checksum-fixed).`)
|
||||
segments.push({ offset, size, name })
|
||||
}
|
||||
const mains = segments.filter(s => s.name === 'ota/s200_firmware_ota.bin')
|
||||
if (mains.length !== 1) throw new Error('Required main firmware component is missing or duplicated.')
|
||||
const main = mains[0], payload = main.offset + 128
|
||||
if (main.size < 32 || u32(payload + 0x14) !== 0x00438000 || (u32(payload) & 0xffffff) !== main.size)
|
||||
throw new Error('Main-app preamble load address or length is invalid.')
|
||||
if (0x00438000 + main.size - 32 > 0x007f0000) throw new Error('Main-app exceeds the safe MRAM ceiling — refusing to flash.')
|
||||
return segments
|
||||
}
|
||||
|
||||
export async function flashLensImage(link: StockConnection, role: string, image: Uint8Array, segments: FirmwareSegment[],
|
||||
progress: (value: FlashProgress) => void, log: (line: string) => void,
|
||||
delay: (ms: number) => Promise<void>): Promise<void> {
|
||||
let sequence = 0
|
||||
const ctrl = async (op: number, payload = new Uint8Array(), data?: Uint8Array): Promise<number> => {
|
||||
sequence = (sequence + 1) & 255
|
||||
const message = await link.exchange(role, 0xc0, concat(new Uint8Array([op]), payload),
|
||||
message => message.command === op, data ? 4000 : 8000, OTA_WRITE, OTA_NOTIFY, 0, sequence, data)
|
||||
return message.payload[1]
|
||||
}
|
||||
const accepted = (status: number) => [0, 8, 9].includes(status)
|
||||
const begin = await ctrl(0)
|
||||
if (!accepted(begin)) throw new Error(`OTA BEGIN rejected (status ${begin}).`)
|
||||
const total = segments.reduce((sum, segment) => sum + segment.size, 0)
|
||||
let before = 0
|
||||
for (const [index, segment] of segments.entries()) {
|
||||
let verified = false, failure: unknown
|
||||
for (let attempt = 0; attempt < 3; attempt++) {
|
||||
link.check()
|
||||
if (attempt) { log(`${segment.name}: re-flash attempt ${attempt + 1}/3`); await delay(1500) }
|
||||
try {
|
||||
const status = await ctrl(1, image.slice(segment.offset, segment.offset + 128))
|
||||
if (status !== 0) throw new Error(`FILE_CHECK rejected (status ${status}).`)
|
||||
const blocks = Math.ceil(segment.size / 4096)
|
||||
for (let block = 0; block < blocks; block++) {
|
||||
const start = segment.offset + 128 + block * 4096
|
||||
const data = image.slice(start, Math.min(start + 4096, segment.offset + 128 + segment.size))
|
||||
let ok = false
|
||||
for (let retry = 0; retry < 3; retry++) {
|
||||
if (await ctrl(2, new Uint8Array(), data) === 0) { ok = true; break }
|
||||
}
|
||||
if (!ok) throw new Error(`Block ${block + 1} rejected three times.`)
|
||||
if (block % 20 === 0 || block === blocks - 1)
|
||||
progress({ lens: role, componentIndex: index + 1, componentCount: segments.length, blockIndex: block + 1,
|
||||
blockCount: blocks, bytesSent: before + Math.min((block + 1) * 4096, segment.size), bytesTotal: total })
|
||||
}
|
||||
const end = await ctrl(3)
|
||||
if (!accepted(end)) throw new Error(`END verification failed (status ${end}).`)
|
||||
log(`${role} ${segment.name}: END verify OK (status ${end})`); verified = true; break
|
||||
} catch (error) { link.check(); failure = error }
|
||||
}
|
||||
if (!verified) throw new Error(`Component ${segment.name} failed after 3 attempts: ${failure}`)
|
||||
before += segment.size
|
||||
}
|
||||
}
|
||||
@@ -19,12 +19,12 @@ export type FirmwarePatchSet = {
|
||||
export const CFW_PATCH_SET: FirmwarePatchSet = {
|
||||
"base": "g2_2.3.0.24.bin",
|
||||
"baseSha256": "187ccf2bcc5c17a212106e8a376745511e8289c4232b634a7ea94b9bf25a0979",
|
||||
"outputSha256": "059ff37a5455b9b272b715ab62c2910ee4dc4966fbfb2e117159bda746bd7315",
|
||||
"outputSha256": "7d8764f8b720252354dcd1695d23578b11f9b7cf5df9895d08b9f6668e9d0ee7",
|
||||
"patches": [
|
||||
{
|
||||
"offset": 1045797,
|
||||
"old": "38b584b0",
|
||||
"new": "5af317bf",
|
||||
"new": "5df3a9b8",
|
||||
"desc": "ring receiver entry: timestamped unfiltered reports under framebuffer lease"
|
||||
},
|
||||
{
|
||||
@@ -36,49 +36,49 @@ export const CFW_PATCH_SET: FirmwarePatchSet = {
|
||||
{
|
||||
"offset": 1437923,
|
||||
"old": "fef788ff",
|
||||
"new": "fbf226fc",
|
||||
"new": "fdf2b8fd",
|
||||
"desc": "cfw_ancs_event"
|
||||
},
|
||||
{
|
||||
"offset": 1437951,
|
||||
"old": "fef7e8fb",
|
||||
"new": "fbf260f9",
|
||||
"new": "fdf2f2fa",
|
||||
"desc": "cfw_ancs_open"
|
||||
},
|
||||
{
|
||||
"offset": 1437969,
|
||||
"old": "fef7e7fb",
|
||||
"new": "fbf277f9",
|
||||
"new": "fdf209fb",
|
||||
"desc": "cfw_ancs_close"
|
||||
},
|
||||
{
|
||||
"offset": 1388387,
|
||||
"old": "39694d00",
|
||||
"new": "35407d00",
|
||||
"new": "59637d00",
|
||||
"desc": "connection-bound ANCS EUS write callback"
|
||||
},
|
||||
{
|
||||
"offset": 971497,
|
||||
"old": "fff717f8",
|
||||
"new": "6df347f8",
|
||||
"new": "6ff3d9f9",
|
||||
"desc": "bl faceclaw_input_bridge_received (ring relay and ordered private transport before worker pool)"
|
||||
},
|
||||
{
|
||||
"offset": 971845,
|
||||
"old": "fef769ff",
|
||||
"new": "6cf399ff",
|
||||
"new": "6ff32bf9",
|
||||
"desc": "bl faceclaw_input_bridge_received (ring relay and ordered private transport before worker pool)"
|
||||
},
|
||||
{
|
||||
"offset": 979579,
|
||||
"old": "fdf74ef8",
|
||||
"new": "6bf37ef8",
|
||||
"new": "6df310fa",
|
||||
"desc": "bl faceclaw_input_bridge_received (ring relay and ordered private transport before worker pool)"
|
||||
},
|
||||
{
|
||||
"offset": 1428895,
|
||||
"old": "f8f76cfd",
|
||||
"new": "faf224f9",
|
||||
"new": "fcf228fa",
|
||||
"desc": "bl cfw_receive_packet (private SID-f0 probe before TPL reassembly)"
|
||||
},
|
||||
{
|
||||
@@ -108,145 +108,145 @@ export const CFW_PATCH_SET: FirmwarePatchSet = {
|
||||
{
|
||||
"offset": 1248487,
|
||||
"old": "d4f712fb",
|
||||
"new": "28f378f9",
|
||||
"new": "2af3f2fa",
|
||||
"desc": "bl settings_send_wrapper (append caps field 100)"
|
||||
},
|
||||
{
|
||||
"offset": 1246183,
|
||||
"old": "f5f7e2f8",
|
||||
"new": "28f310fb",
|
||||
"new": "2af376fc",
|
||||
"desc": "bl settings_decode_wrapper (Faceclaw lease field 101)"
|
||||
},
|
||||
{
|
||||
"offset": 975219,
|
||||
"old": "02f083fc",
|
||||
"new": "6af372fb",
|
||||
"new": "6cf3e2fc",
|
||||
"desc": "bl faceclaw_display_start @ 0x467d68 (fail-open wake takeover)"
|
||||
},
|
||||
{
|
||||
"offset": 975411,
|
||||
"old": "02f023fc",
|
||||
"new": "6af3e0fb",
|
||||
"new": "6cf346fd",
|
||||
"desc": "bl faceclaw_display_start_headup @ 0x467e28 (fail-open wake takeover)"
|
||||
},
|
||||
{
|
||||
"offset": 975103,
|
||||
"old": "07f085fb",
|
||||
"new": "6cf350f8",
|
||||
"new": "6ef3e2f9",
|
||||
"desc": "bl headup_gate (head-up sensor event -> EvenHub sys event 12 under lease)"
|
||||
},
|
||||
{
|
||||
"offset": 830549,
|
||||
"old": "19f03efb",
|
||||
"new": "8ff3fff9",
|
||||
"new": "91f391fb",
|
||||
"desc": "bl gesture_ring_press_mode (observe ring touch-down, preserve stock dispatch)"
|
||||
},
|
||||
{
|
||||
"offset": 830795,
|
||||
"old": "1ff071ff",
|
||||
"new": "8ff3b8f9",
|
||||
"new": "91f34afb",
|
||||
"desc": "bl gesture_press (owned event 9 before stock menu gate)"
|
||||
},
|
||||
{
|
||||
"offset": 830887,
|
||||
"old": "1ff043ff",
|
||||
"new": "8ff3b2f9",
|
||||
"new": "91f344fb",
|
||||
"desc": "bl gesture_short_long (Faceclaw lease gates event 11 vs stock Menu path)"
|
||||
},
|
||||
{
|
||||
"offset": 831763,
|
||||
"old": "1ff08dfd",
|
||||
"new": "8ef3e8ff",
|
||||
"new": "91f37af9",
|
||||
"desc": "bl gesture_release (owned event 10 before stock menu gate)"
|
||||
},
|
||||
{
|
||||
"offset": 1570209,
|
||||
"old": "7fb50600",
|
||||
"new": "d9f209bd",
|
||||
"new": "dbf283be",
|
||||
"desc": "even_ai_display_ctrl entry -> conditional Faceclaw lease trampoline"
|
||||
},
|
||||
{
|
||||
"offset": 1051545,
|
||||
"old": "f6f787f9",
|
||||
"new": "53f3d7fe",
|
||||
"new": "55f311f9",
|
||||
"desc": "bl display_copy_hook @ 0x47a78e (640x480 direct framebuffer)"
|
||||
},
|
||||
{
|
||||
"offset": 1051861,
|
||||
"old": "f6f7e9f8",
|
||||
"new": "53f339fe",
|
||||
"new": "55f373f8",
|
||||
"desc": "bl display_copy_hook @ 0x47a8ca (640x480 direct framebuffer)"
|
||||
},
|
||||
{
|
||||
"offset": 1262237,
|
||||
"old": "d9f7d0fd",
|
||||
"new": "24f3cff9",
|
||||
"new": "26f333fb",
|
||||
"desc": "bl faceclaw_send_wear_event @ 0x4ade92 (outside onboarding)"
|
||||
},
|
||||
{
|
||||
"offset": 1262337,
|
||||
"old": "d9f79efd",
|
||||
"new": "24f39df9",
|
||||
"new": "26f301fb",
|
||||
"desc": "bl faceclaw_send_wear_event @ 0x4adef6 (outside onboarding)"
|
||||
},
|
||||
{
|
||||
"offset": 1309405,
|
||||
"old": "67f02dff",
|
||||
"new": "18f3b7fa",
|
||||
"new": "1af30bfc",
|
||||
"desc": "bl compass_decode_capture (sample-matched GAF diagnostics)"
|
||||
},
|
||||
{
|
||||
"offset": 1307865,
|
||||
"old": "fff7c9fb",
|
||||
"new": "18f335fe",
|
||||
"new": "1af389ff",
|
||||
"desc": "bl compass_report_event (stock UI + heading with diagnostics over BLE)"
|
||||
},
|
||||
{
|
||||
"offset": 1300937,
|
||||
"old": "00210170",
|
||||
"new": "1af31bfb",
|
||||
"new": "1cf36ffc",
|
||||
"desc": "bl compass_preserve_accuracy (retain magnetic calibration under Faceclaw framebuffer lease)"
|
||||
},
|
||||
{
|
||||
"offset": 4537963,
|
||||
"old": "",
|
||||
"new": "b0b581ea00039b0701d0034637e0c2b110f0030315d000f1010ca2f1010e0023cc5c9e45c45403f1010404d0634413f003032346f4d121440319121b102a14d302e00346102a10d3d1f800c0d1f804e0cc688d68103adc609d60c3f804e043f810cb0f2a01f11001eed8042a07d300bf51f8045b043a032a43f8045bf8d8002a08bfb0bd0139013b11f8015f013a03f8015ff9d1b0bd00bf884218bf002a00d1704710b50b1a38bf431a934207d288420ad281ea00039b0726d0034669e0fff79fffbde81040704781ea000300eb020c9b0701eb020e42d15fea8c732bd0a0f1010c00bf8b1813f801ec8418531e04f801ec04d0624412f003021a46f2d101eb030e00eb030c17e0830736d0a2f1010c00f1010e002300bfcc5c9c45c45403f1010404d0734413f003032346f4d1121b2144031922e01346042b0ed3aef10402acf1040152f80449043b032b41f80449f8d802f1040e01f1040c1a4653b1aef10101acf1010300bf11f80149013a03f80149f9d1bde8104070470346042a06d351f8044b043a032a43f8044bf8d8002af0d00139013b00bf11f8014f013a03f8014ff9d1e6e700bf2de9f04fa5b04ef66c6b4ff29c5416460d46ccf2ff0bc2f229044ff6ff7900274ff00108019006e048f2a351c0f24501bff32f8f8847d4f800a0bff35f8f20680321cff680710140b1f1005fbff35f8f03d10168594500f0c3804ff4407005f073f9002800f0b7824ff60051cff6ff714218403182f8007382f8017382f8027382f8037382f8047382f8057382f8067382f8077382f8087382f8097382f80a7382f80b7382f80c7382f80d7382f80e7382f80f7382f8107382f8117382f8127382f8137382f8147382f8157382f8167382f8177382f8187382f8197382f81a7382f81b7382f81c7382f81d7382f81e7382f81f7382f8207382f8217382f8227382f8237382f8247382f8257382f8267382f8277382f8287382f8297382f82a7382f82b7382f82c7382f82d7382f82e7382f82f7382f8307382f8317382f8327382f8337382f8347382f8357382f8367382f8377382f8387382f8397382f83a7382f83b7382f83c7382f83d7382f83e7382f83f737ff47cafc0f800b080f80a80a0f81c90a0f81e90a0f82090a0f82290a0f82490a0f82690a0f82890a0f82a90a0f82c90a0f82e90a0f83090a0f83290a0f83490a0f83690a0f83890a0f83a9054e8001f51457ff42fafbff35f8f00bf44e8000121b154e8001f5145f8d023e7aa4645ea064100260127c0f8701206e048f2a351c0f24501bff32f8f88472568bff35f8f20680321cff680710140b1f1005fbff35f8f03d10168594500f0c2804ff4407005f090f8002800f0d4814ff60051cff6ff7100bf4218403182f8006382f8016382f8026382f8036382f8046382f8056382f8066382f8076382f8086382f8096382f80a6382f80b6382f80c6382f80d6382f80e6382f80f6382f8106382f8116382f8126382f8136382f8146382f8156382f8166382f8176382f8186382f8196382f81a6382f81b6382f81c6382f81d6382f81e6382f81f6382f8206382f8216382f8226382f8236382f8246382f8256382f8266382f8276382f8286382f8296382f82a6382f82b6382f82c6382f82d6382f82e6382f82f6382f8306382f8316382f8326382f8336382f8346382f8356382f8366382f8376382f8386382f8396382f83a6382f83b6382f83c6382f83d6382f83e6382f83f637ff47cafc0f800b08772a0f81c90a0f81e90a0f82090a0f82290a0f82490a0f82690a0f82890a0f82a90a0f82c90a0f82e90a0f83090a0f83290a0f83490a0f83690a0f83890a0f83a9054e8001fa9427ff430afbff35f8f44e8000121b154e8001fa942f8d025e7d0f8746242f6f767c0f24407002ebff35f8f00f0f38007f19a0230464ff0ff314ff0ff379047002840f0498147f6df40c0f24f00804700274ff001080397adf8107006e048f2a351c0f24501bff32f8f88472568bff35f8f20680321cff680710140b1f1005fbff35f8f03d10168594500f0e9804ff4407004f094ff002800f0e1804ff60051cff6ff7100bf4218403182f8007382f8017382f8027382f8037382f8047382f8057382f8067382f8077382f8087382f8097382f80a7382f80b7382f80c7382f80d7382f80e7382f80f7382f8107382f8117382f8127382f8137382f8147382f8157382f8167382f8177382f8187382f8197382f81a7382f81b7382f81c7382f81d7382f81e7382f81f7382f8207382f8217382f8227382f8237382f8247382f8257382f8267382f8277382f8287382f8297382f82a7382f82b7382f82c7382f82d7382f82e7382f82f7382f8307382f8317382f8327382f8337382f8347382f8357382f8367382f8377382f8387382f8397382f83a7382f83b7382f83c7382f83d7382f83e7382f83f737ff47cafc0f800b080f80a80a0f81c90a0f81e90a0f82090a0f82290a0f82490a0f82690a0f82890a0f82a90a0f82c90a0f82e90a0f83090a0f83290a0f83490a0f83690a0f83890a0f83a9054e8001fa9427ff42fafbff35f8f00bf44e8000149b354e8001fa942f8d023e705460020b847b0b105f51d7151e8006f4eb9bff35f8f00bf41e8000292b151e8006f002ef8d00021bff32f8fbff35f8f21b10646f1e64ff0ff3742e007f5a9718847eae60121f1e70020019d42f6f764002d4ff0010bc0f2440418bfbaf1000f33d10df108098046484602f061fa03ab28465146012206f093fa9df8111007464846002918bf4ff0ff3702f08dfbbbf1000f044608d19df8100028b94af21f30c0f247004c308047b8f1000f18bfc8f80c4042f6f764c0f2440404f5807130468847384625b0bde8f08f2a7802f07f01a1f1150313f1030f0bd802f07b030b2b07d002f07e02082a03d0032901d00629b7d140b14af21f31c0f247010746884797f8641111b14ff0ff37d6e738464ff0000ba6e72de9f04f81b04ef66c6b4ff29c554ff6005848f2a35accf2ff0bc2f229054ff6ff76cff6ff7800274ff00109c0f2450a03e000bfbff32f8fd0472c68bff35f8f2868bff35f8f01466ff39601b1f1005f03d10168594500f0b2804ff4407004f019fe002800f0aa80414600bf4218403182f8007382f8017382f8027382f8037382f8047382f8057382f8067382f8077382f8087382f8097382f80a7382f80b7382f80c7382f80d7382f80e7382f80f7382f8107382f8117382f8127382f8137382f8147382f8157382f8167382f8177382f8187382f8197382f81a7382f81b7382f81c7382f81d7382f81e7382f81f7382f8207382f8217382f8227382f8237382f8247382f8257382f8267382f8277382f8287382f8297382f82a7382f82b7382f82c7382f82d7382f82e7382f82f7382f8307382f8317382f8327382f8337382f8347382f8357382f8367382f8377382f8387382f8397382f83a7382f83b738Line truncated
|
||||
"new": "b0b581ea00039b0701d0034637e0c2b110f0030315d000f1010ca2f1010e0023cc5c9e45c45403f1010404d0634413f003032346f4d121440319121b102a14d302e00346102a10d3d1f800c0d1f804e0cc688d68103adc609d60c3f804e043f810cb0f2a01f11001eed8042a07d300bf51f8045b043a032a43f8045bf8d8002a08bfb0bd0139013b11f8015f013a03f8015ff9d1b0bd00bf884218bf002a00d1704710b50b1a38bf431a934207d288420ad281ea00039b0726d0034669e0fff79fffbde81040704781ea000300eb020c9b0701eb020e42d15fea8c732bd0a0f1010c00bf8b1813f801ec8418531e04f801ec04d0624412f003021a46f2d101eb030e00eb030c17e0830736d0a2f1010c00f1010e002300bfcc5c9c45c45403f1010404d0734413f003032346f4d1121b2144031922e01346042b0ed3aef10402acf1040152f80449043b032b41f80449f8d802f1040e01f1040c1a4653b1aef10101acf1010300bf11f80149013a03f80149f9d1bde8104070470346042a06d351f8044b043a032a43f8044bf8d8002af0d00139013b00bf11f8014f013a03f8014ff9d1e6e700bf80b5002a18bf10f0030301d186460ce000f1010c864600bf013a0ef8011b04d01cf003030cf1010cf6d1102a11d35ffa81fc4ff001330cfb03fc00bf103acef80cc0cef808c0cef804c04ef810cb0f2af4d8042a0bd35ffa81fc4ff001330cfb03fc00bf043a032a4ef804cbfad8002a08bf80bdaef10103013a03f8011ffbd180bd00bf2de9f04fc1b04ff62c484ef66c694ff29c5b1446cff6ff78ccf2ff09c2f2290b4ff6ff75002701260993cde90a0106e048f2a351c0f24501bff32f8f8847dbf800a0bff35f8fdbf800000321cff680710140b1f1005fbff35f8f03d10168494500f0ba804ff4757007f0a6fa002800f0078241464218463182f8d47382f8d57382f8d67382f8d77382f8d87382f8d97382f8da7382f8db7382f8dc7382f8dd7382f8de7382f8df7382f8e07382f8e17382f8e27382f8e37382f8e47382f8e57382f8e67382f8e77382f8e87382f8e97382f8ea7382f8eb7382f8ec7382f8ed7382f8ee7382f8ef7382f8f07382f8f17382f8f27382f8f37382f8f47382f8f57382f8f67382f8f77382f8f87382f8f97382f8fa7382f8fb7382f8fc7382f8fd7382f8fe7382f8ff7382f8007482f8017482f8027482f8037482f8047482f8057482f8067482f8077482f8087482f8097482f80a7482f80b7482f80c7482f80d7482f80e7482f80f7482f8107482f8117482f8127482f8137482f8147482f8157482f8167482f8177482f8187482f819747ff470afc0f8009086728583c583058445848584c584058545858585c585058645868586c586058745875be8001f51457ff436afbff35f8f4be8000121b15be8001f5145f8d02be70b99012641ea04410024c0f8701206e048f2a351c0f24501bff32f8f8847dbf80070bff35f8fdbf800000321cff680710140b1f1005fbff35f8f03d10168494500f0ba804ff4757007f0caf9002800f02b8141464218463182f8d44382f8d54382f8d64382f8d74382f8d84382f8d94382f8da4382f8db4382f8dc4382f8dd4382f8de4382f8df4382f8e04382f8e14382f8e24382f8e34382f8e44382f8e54382f8e64382f8e74382f8e84382f8e94382f8ea4382f8eb4382f8ec4382f8ed4382f8ee4382f8ef4382f8f04382f8f14382f8f24382f8f34382f8f44382f8f54382f8f64382f8f74382f8f84382f8f94382f8fa4382f8fb4382f8fc4382f8fd4382f8fe4382f8ff4382f8004482f8014482f8024482f8034482f8044482f8054482f8064482f8074482f8084482f8094482f80a4482f80b4482f80c4482f80d4482f80e4482f80f4482f8104482f8114482f8124482f8134482f8144482f8154482f8164482f8174482f8184482f819447ff470afc0f8009086728583c583058445848584c584058545858585c585058645868586c586058745875be8001fb9427ff436afbff35f8f4be8000121b15be8001fb942f8d02be7d0f874a242f6f766c0f24406baf1000fbff35f8f4ad006f19a0250464ff0ff314ff0ff379047002840f09d810b98002861d00a980778a7f1170002285bd80b980c2840f086810a9e7078b178084300f08081f0780138022800f27b81b279f379747a42ea0322337a307942ea0342717942ea0463b27af67a192f42ea062200f01082172f40f02682002a40f06281fe2800f06d86812800f06086002840f0738651ea030040f0558100f0bdbe04460020b047e8b104f51d7151e800afbaf1000f0cd1bff35f8f00bf41e80002002a00f0f58051e800afbaf1000ff5d00021bff32f8f0029bff35f8f00f0e480824692e74ff0ff3737e147f6df40c0f24f008047002701240d97adf8387006e048f2a351c0f24501bff32f8f8847dbf80060bff35f8fdbf800000321cff680710140b1f1005fbff35f8f03d10168494500f0c1804ff4757007f070f8002800f0b98041464218463182f8d47382f8d57382f8d67382f8d77382f8d87382f8d97382f8da7382f8db7382f8dc7382f8dd7382f8de7382f8df7382f8e07382f8e17382f8e27382f8e37382f8e47382f8e57382f8e67382f8e77382f8e87382f8e97382f8ea7382f8eb7382f8ec7382f8ed7382f8ee7382f8ef7382f8f07382f8f17382f8f27382f8f37382f8f47382f8f57382f8f67382f8f77382f8f87382f8f97382f8fa7382f8fb7382f8fc7382f8fd7382f8fe7382f8ff7382f8007482f8017482f8027482f8037482f8047482f8057482f8067482f8077482f8087482f8097482f80a7482f80b7482f80c7482f80d7482f80e7482f80f7482f8107482f8117482f8127482f8137482f8147482f8157482f8167482f8177482f8187482f819747ff470afc0f8009084728583c583058445848584c584058545858585c585058645868586c586058745875be8001fb1427ff436afbff35f8f4be8000159b15be8001fb142f8d02be706f5a9718847ace6012110e700200a9999b10b9a8ab1097801f07f02b2f10b0618bf0126b2f11c0718bf01278a2924dc0b2926d01c2924d03fe009900ca804f097fa4ff0ff3701249df8390000280ca818bf4ff0ff3704f0d3fb054644b99df8380028b94af21f30c0f247004c3080470998002818bfc56013e09c2901d08b291bd160b14af21f31c0f247010446884794f8641119b994f80413204669b14ff0ff3742f6f760c0f2440000f5807150468847384641b0bde8f08f09900ca806ea070404f055fa0a98017801f07f061e2e04d10b9908f076fb0746b5e71d2e01f07e0018bf1a2811d1dbf800100322cff680720a40b2f1005fbff35f8f0ad10a684a4504bf002281f8812303e0a6f115010129e9d90f2e0adc092e00f37381052e00f0ef82072e40f01c8300279ae0142e00f32182102e00f0eb82112e40f011830b98022841f0c6800a9908781Line truncated
|
||||
"desc": "append injected blobs to main-app payload"
|
||||
},
|
||||
{
|
||||
"offset": 779123,
|
||||
"old": "805a3900",
|
||||
"new": "f0173a00",
|
||||
"new": "e06a3a00",
|
||||
"desc": "main-app subheader payload size (ps)"
|
||||
},
|
||||
{
|
||||
"offset": 152,
|
||||
"old": "005b3900",
|
||||
"new": "70183a00",
|
||||
"new": "606b3a00",
|
||||
"desc": "main-app TOC entry size (ps + 128)"
|
||||
},
|
||||
{
|
||||
"offset": 779243,
|
||||
"old": "805a3904",
|
||||
"new": "f0173a04",
|
||||
"new": "e06a3a04",
|
||||
"desc": "main-app preamble length (low 24 bits)"
|
||||
},
|
||||
{
|
||||
"offset": 779247,
|
||||
"old": "effdbcea",
|
||||
"new": "ed6be905",
|
||||
"new": "38f29188",
|
||||
"desc": "[5] ota/s200_firmware_ota.bin preamble crc32"
|
||||
},
|
||||
{
|
||||
"offset": 156,
|
||||
"old": "c0c6322a",
|
||||
"new": "18540264",
|
||||
"new": "f8d63d83",
|
||||
"desc": "[5] ota/s200_firmware_ota.bin component crc32c (TOC)"
|
||||
},
|
||||
{
|
||||
"offset": 779127,
|
||||
"old": "c0c6322a",
|
||||
"new": "18540264",
|
||||
"new": "f8d63d83",
|
||||
"desc": "[5] ota/s200_firmware_ota.bin component crc32c (subheader)"
|
||||
}
|
||||
]
|
||||
|
||||
@@ -1,18 +0,0 @@
|
||||
import { bytes, concat, integer, readBytes, readInteger, readString, type ProtocolMessage } from './ble-protocol'
|
||||
const text = (field: number, value: string) => bytes(field, new Uint8Array(Array.from(value, ch => ch.charCodeAt(0))))
|
||||
export function createFlashPrompt(magic: number, warning: string): Uint8Array {
|
||||
const geometry = (y: number, height: number, id: number, name: string) => concat(
|
||||
integer(1, 0), integer(2, y), integer(3, 280), integer(4, height), integer(9, id), text(10, name))
|
||||
const list = concat(geometry(150, 120, 2, 'flashmenu'), bytes(11, concat(integer(1, 2), integer(3, 1), text(4, 'No, cancel'), text(4, 'Yes, flash'))), integer(12, 1))
|
||||
const label = concat(geometry(0, 130, 1, 'flashwarn'), text(12, warning))
|
||||
return concat(integer(1, 0), integer(2, magic), bytes(3, concat(integer(1, 2), bytes(2, list), bytes(3, label), integer(5, 10000))))
|
||||
}
|
||||
export function flashPromptSelection(message: ProtocolMessage): boolean | null {
|
||||
if (message.sid !== 0xe0 || ![1, 6].includes(message.flag)) return null
|
||||
const event = readBytes(message.payload, 13), list = event && readBytes(event, 1)
|
||||
if (!list || readString(list, 2) !== 'flashmenu' || readInteger(list, 5, 0) !== 0) return null
|
||||
const index = readInteger(list, 4, -1), name = readString(list, 3).toLowerCase()
|
||||
if (index === 1 || name.startsWith('yes')) return true
|
||||
if (index === 0 || name.startsWith('no')) return false
|
||||
return null
|
||||
}
|
||||
+10
-1
@@ -23,7 +23,7 @@ export const GLANCE_SURFACE_Z_ORDER = 900;
|
||||
|
||||
/** Surface operations needed by the board, shared by Android and iOS. */
|
||||
export type GlanceDisplay = Pick<DisplayTarget,
|
||||
"configureSurface" | "setSurfaceVisible" | "setScreenBlanked" | "submitSurfaceFrame">;
|
||||
"configureSurface" | "setSurfaceVisible" | "setSurfaceDepth" | "setScreenBlanked" | "submitSurfaceFrame">;
|
||||
|
||||
export type GlanceHostOptions = {
|
||||
getDisplay: () => GlanceDisplay | null;
|
||||
@@ -52,6 +52,8 @@ export class GlanceHost {
|
||||
private shown = false;
|
||||
private hideTimer: ReturnType<typeof setTimeout> | null = null;
|
||||
private configuredFor: GlanceDisplay | null = null;
|
||||
/** Last depth given to the surface; null forces the next render to set it. */
|
||||
private configuredDepth: number | null = null;
|
||||
/** Serializes show/hide against each other and against in-flight renders. */
|
||||
private queue: Promise<void> = Promise.resolve();
|
||||
private rendering = false;
|
||||
@@ -158,6 +160,7 @@ export class GlanceHost {
|
||||
transparency: "opaque",
|
||||
});
|
||||
await display.setSurfaceVisible(GLANCE_SURFACE_ID, false);
|
||||
this.configuredDepth = null;
|
||||
this.configuredFor = display;
|
||||
}
|
||||
|
||||
@@ -256,6 +259,12 @@ export class GlanceHost {
|
||||
const { image, draws } = frameTimings.span(frameId, "flatten", () => flattenPlanesWithDraws(planes));
|
||||
const buffer = frameTimings.span(frameId, "to8bpp", () => image.to8bppBuffer());
|
||||
const preparedDraws = frameTimings.span(frameId, "prepareFrameDraws", () => prepareFrameDraws(draws));
|
||||
// The glasses copy the screen shifted per lens while the board covers it.
|
||||
const depth = this.options.getProvider()?.depth() ?? 0;
|
||||
if (depth !== this.configuredDepth) {
|
||||
await display.setSurfaceDepth(GLANCE_SURFACE_ID, depth);
|
||||
this.configuredDepth = depth;
|
||||
}
|
||||
await frameTimings.spanAsync(frameId, "submit", () =>
|
||||
display.submitSurfaceFrame(
|
||||
GLANCE_SURFACE_ID,
|
||||
|
||||
@@ -1,638 +0,0 @@
|
||||
import { setBrightness } from './brightness-protocol'
|
||||
import { AncsClient, ANCS_FIRMWARE_VERSION } from './ancs-client'
|
||||
import { type CompassEvent } from '../native/compass-types'
|
||||
import * as protocol from './ble-protocol'
|
||||
import { iosBleTraffic } from './ble-traffic-counters'
|
||||
import { buildBoundingBoxPayload, buildFullFrameBands } from './ble-image-optimizer'
|
||||
import { CfwTransport, CFW_MAX_MESSAGE, parseCfwAcks } from './cfw-transport'
|
||||
import { hasCompatibleFirmware, firmwareIncompatibilityMessage, REQUIRED_FACECLAW_FIRMWARE_VERSION } from './firmware-compat'
|
||||
import { hexToBytes } from '../util/hex-util'
|
||||
import { deviceAddressError } from './ios-peripheral-identity'
|
||||
import { decodeWearState, enableWearDetection, queryWearState } from './wear-protocol'
|
||||
import type { SessionTexturePlanner, TextureFrame } from './texture-planner'
|
||||
|
||||
declare function setTimeout(callback: () => void, ms: number): number
|
||||
declare function clearTimeout(id: number): void
|
||||
export type SessionAddresses = { left: string; right: string; ring: string }
|
||||
export type TransportEvent = { kind: string; identifier?: string; characteristic?: string; data?: string; message?: string; state?: number; authorized?: boolean }
|
||||
export interface SessionTransport {
|
||||
onEvent(listener: (event: TransportEvent) => void): () => void
|
||||
resolveDevices(addresses: SessionAddresses): Promise<Record<string, string>>
|
||||
connect(identifier: string, requiresANCS?: boolean): Promise<{ characteristics: string[]; maxWrite: number }>
|
||||
subscribe(identifier: string, characteristic: string): Promise<void>
|
||||
write(identifier: string, characteristic: string, bytes: Uint8Array): Promise<void>
|
||||
disconnect(identifier: string): void
|
||||
stopScan(): void
|
||||
}
|
||||
export type SessionState = { phase: 'disconnected' | 'connecting' | 'connected' | 'retrying' | 'disconnecting' | 'error';
|
||||
status: string; battery: number | null; charging: boolean | null; ring: boolean; frames: number; capabilities: string; leftVersion: string; rightVersion: string }
|
||||
type PendingAck = { resolve: (message: protocol.ProtocolMessage) => void; reject: (error: Error) => void;
|
||||
timer: number | null; command: number; label: string }
|
||||
class AckTimeout extends Error {}
|
||||
// Match Android ConnectionOptions.WINDOW_SIZE. This bounds complete image
|
||||
// messages queued for writing or awaiting ACK, not individual BLE packets.
|
||||
const DISPLAY_WINDOW_SIZE = 3
|
||||
type CfwPending = { payload: Uint8Array; checksum: number; role: string; magic: number; ackLenses: number; retries: number;
|
||||
retryPending: boolean; deadline: number; resolve: () => void; reject: (error: Error) => void }
|
||||
type DisplayFrame = { packed: Uint8Array; commands: Uint8Array[]; offset: number; pending: number }
|
||||
|
||||
/** G2 session independent of the phone OS. Images go to L, control to R, as on Android. */
|
||||
export class GlassesSession {
|
||||
state: SessionState = { phase: 'disconnected', status: 'Preview only', battery: null, charging: null, ring: false,
|
||||
frames: 0, capabilities: '', leftVersion: '', rightVersion: '' }
|
||||
readonly notifications: AncsClient
|
||||
private generation = 0
|
||||
private ids: Record<string, string> = {}
|
||||
private limits: Record<string, number> = {}
|
||||
private cfwPending: CfwPending[] = []
|
||||
private cfwTransports = new Map<string, CfwTransport>()
|
||||
private cfwTimer: number | null = null
|
||||
private pending = new Map<string, PendingAck>()
|
||||
private receiver = new protocol.MessageReceiver()
|
||||
private writes = new Map<string, Promise<void>>()
|
||||
private magic = 100
|
||||
private sequence = 0x40
|
||||
private timer: number | null = null
|
||||
private retryTimer: number | null = null
|
||||
private ringRetryTimer: number | null = null
|
||||
private ringRetryCount = 0
|
||||
private ringConnecting = false
|
||||
private retryCount = 0
|
||||
private wanted = false
|
||||
private addresses: SessionAddresses | null = null
|
||||
private latest: Uint8Array | null = null
|
||||
private latestTextures: TextureFrame | null = null
|
||||
private displayed: Uint8Array | null = null
|
||||
private lastEnqueued: Uint8Array | null = null
|
||||
// Match Android: advance only for emitted deltas, skipping 0 / 0xffff.
|
||||
private nextImageFrameId = 1
|
||||
private displayFrames: DisplayFrame[] = []
|
||||
private displaySending: DisplayFrame | null = null
|
||||
private displayInFlight = 0
|
||||
private pumping = false
|
||||
private lastHeartbeat = 0
|
||||
private lastLease = 0
|
||||
private lastSettings = 0
|
||||
private charging = false
|
||||
private layoutCreated = false
|
||||
private off: () => void
|
||||
private stopping: Promise<void> | null = null
|
||||
private audioListener: ((packet: Uint8Array) => void) | null = null
|
||||
private microphoneToken = 0
|
||||
private microphoneWork: Promise<void> = Promise.resolve()
|
||||
private microphoneEnabled = false
|
||||
private compassWanted = false
|
||||
private compassSent: boolean | null = null
|
||||
private compassStopping = false
|
||||
private compassWork: Promise<void> = Promise.resolve()
|
||||
constructor(private readonly transport: SessionTransport,
|
||||
private readonly onState: (state: SessionState) => void,
|
||||
private readonly onInput: (input: protocol.GlassesInput) => void,
|
||||
private readonly log: (message: string) => void = () => {},
|
||||
private readonly onActivity: () => void = () => {},
|
||||
private readonly onCompass: (event: CompassEvent) => void = () => {},
|
||||
private readonly onWearState: (wearing: boolean) => void = () => {},
|
||||
onNotificationsChanged: (key?: string, popup?: boolean) => void = () => {},
|
||||
private readonly textures?: SessionTexturePlanner) {
|
||||
this.notifications = new AncsClient(packet => this.writePackets('right', () => [packet]), onNotificationsChanged, message => this.log('ANCS: ' + message))
|
||||
this.off = transport.onEvent(event => this.receive(event))
|
||||
}
|
||||
private update(phase: SessionState['phase'], status: string): void {
|
||||
this.state = { ...this.state, phase, status }; this.log(status); this.onState({ ...this.state })
|
||||
}
|
||||
async start(addresses: SessionAddresses, retry = false): Promise<void> {
|
||||
if (['connecting', 'connected', 'disconnecting'].includes(this.state.phase)) return
|
||||
const invalid = deviceAddressError(addresses)
|
||||
if (invalid) { this.update('error', invalid); return }
|
||||
this.addresses = { ...addresses }; this.wanted = true; this.compassStopping = false
|
||||
if (!retry) this.retryCount = 0
|
||||
const generation = ++this.generation
|
||||
this.state = { ...this.state, capabilities: '', leftVersion: '', rightVersion: '', battery: null, charging: null, ring: false }
|
||||
this.charging = false
|
||||
this.update('connecting', 'Finding configured devices…')
|
||||
try {
|
||||
const ids = await this.transport.resolveDevices(addresses); this.check(generation)
|
||||
this.ids = ids
|
||||
for (const role of ['right', 'left']) {
|
||||
this.update('connecting', `Connecting ${role} arm…`)
|
||||
const details = await this.transport.connect(ids[role], role === 'right'); this.check(generation)
|
||||
this.limits[role] = details.maxWrite
|
||||
if (!details.characteristics.includes(protocol.G2_WRITE) || !details.characteristics.includes(protocol.G2_NOTIFY)) throw new Error(`${role} arm is missing G2 communication characteristics.`)
|
||||
await this.transport.subscribe(ids[role], protocol.G2_NOTIFY); this.check(generation)
|
||||
}
|
||||
this.update('connecting', 'Authenticating… Accept pairing if iOS asks.')
|
||||
await Promise.all(['right', 'left'].map(async role => {
|
||||
try {
|
||||
const answer = await this.request(role, protocol.SID.auth, protocol.authentication, 'Security authentication', 30_000, 0)
|
||||
if (!protocol.authenticationSucceeded(answer, answer.magic)) throw new Error(`${role} arm rejected authentication.`)
|
||||
} catch (error) {
|
||||
if (!(error instanceof AckTimeout)) throw error
|
||||
// Older CFW may not implement the reply. A settings response below
|
||||
// still has to prove we have a working, compatible session.
|
||||
this.log(`${role} authentication unconfirmed; checking firmware response`)
|
||||
}
|
||||
}))
|
||||
this.check(generation)
|
||||
this.update('connecting', 'Checking glasses firmware…')
|
||||
const settings = await this.request('right', protocol.SID.settings, protocol.settingsQuery, 'Settings query')
|
||||
this.applySettings(settings)
|
||||
const firmware = { leftVersion: this.state.leftVersion, rightVersion: this.state.rightVersion, extension: this.state.capabilities }
|
||||
if (!hasCompatibleFirmware(firmware))
|
||||
throw new Error((firmwareIncompatibilityMessage(firmware) || `The glasses must report Faceclaw firmware revision ${REQUIRED_FACECLAW_FIRMWARE_VERSION} or newer.`) + ' Use Devices → Check firmware to install it.')
|
||||
this.update('connecting', 'Starting glasses display…')
|
||||
await this.request('right', protocol.SID.launch, () => protocol.prelude(), 'App launch', 3500, 0x20, 156)
|
||||
await this.lease(true)
|
||||
await this.request('right', protocol.SID.hub, protocol.createLayout, 'Create display layout')
|
||||
this.check(generation); this.layoutCreated = true
|
||||
await this.enableWearDetectionAndRequestState(); this.check(generation)
|
||||
this.lastHeartbeat = this.lastLease = this.lastSettings = Date.now()
|
||||
this.displayed = null; this.retryCount = 0
|
||||
this.update('connected', 'Glasses connected')
|
||||
if (/^Faceclaw\/(\d+)/.test(this.state.capabilities) && Number(this.state.capabilities.split('/')[1]) >= ANCS_FIRMWARE_VERSION)
|
||||
this.notifications.start((Date.now() ^ Math.floor(Math.random()*0xffffffff)) >>> 0, this.limits.right)
|
||||
else this.notifications.stop(`Update glasses to Faceclaw firmware ${ANCS_FIRMWARE_VERSION} or newer for iPhone notifications.`)
|
||||
await this.syncCompass(); this.check(generation)
|
||||
this.schedule()
|
||||
if (addresses.ring && ids.ring) await this.connectRing(generation)
|
||||
else if (addresses.ring) this.update('connected', 'Glasses connected; configured ring was not found')
|
||||
} catch (error) {
|
||||
if (generation !== this.generation) return
|
||||
this.fail(error, retry || error instanceof AckTimeout)
|
||||
}
|
||||
}
|
||||
private async connectRing(generation: number): Promise<void> {
|
||||
if (this.ringConnecting || generation !== this.generation || this.state.phase !== 'connected' || !this.ids.ring) return
|
||||
this.ringConnecting = true
|
||||
const identifier = this.ids.ring
|
||||
try {
|
||||
const ring = await this.transport.connect(identifier); this.check(generation)
|
||||
this.log(`R1 services discovered: ${ring.characteristics.join(', ')}`)
|
||||
const notifications = protocol.RING_NOTIFY.filter(uuid => ring.characteristics.includes(uuid))
|
||||
if (!notifications.length) throw new Error('No R1 gesture notifications found')
|
||||
let subscribed = 0
|
||||
for (const uuid of notifications) {
|
||||
this.log(`Subscribing R1 ${uuid}`)
|
||||
try { await this.transport.subscribe(identifier, uuid); this.check(generation); subscribed++; this.log(`R1 subscribed ${uuid}`) }
|
||||
catch (error) { this.check(generation); this.log(`R1 channel unavailable ${uuid}: ${this.message(error)}`) }
|
||||
}
|
||||
if (!subscribed) throw new Error('Neither R1 notification channel could be subscribed')
|
||||
this.check(generation); this.state.ring = true; this.ringRetryCount = 0
|
||||
this.update('connected', 'Glasses and ring connected')
|
||||
} catch (error) {
|
||||
if (generation !== this.generation) return
|
||||
this.state.ring = false
|
||||
this.transport.disconnect(identifier)
|
||||
this.update('connected', `Glasses connected; ring unavailable: ${this.message(error)}`)
|
||||
} finally {
|
||||
if (generation === this.generation) {
|
||||
this.ringConnecting = false
|
||||
if (!this.state.ring) this.scheduleRingRetry()
|
||||
}
|
||||
}
|
||||
}
|
||||
private scheduleRingRetry(): void {
|
||||
if (!this.wanted || this.state.phase !== 'connected' || !this.ids.ring || this.ringConnecting || this.ringRetryTimer !== null || this.ringRetryCount >= 5) return
|
||||
const generation = this.generation, delay = Math.min(30_000, 2000 * 2 ** this.ringRetryCount++)
|
||||
this.log(`Retrying ring in ${delay / 1000}s`)
|
||||
this.ringRetryTimer = setTimeout(() => {
|
||||
this.ringRetryTimer = null
|
||||
if (generation === this.generation) void this.connectRing(generation)
|
||||
}, delay)
|
||||
}
|
||||
private check(generation: number): void { if (generation !== this.generation) throw new Error('Connection cancelled') }
|
||||
private nextMagic(): number {
|
||||
for (let i = 0; i < 156; i++) {
|
||||
const magic = this.magic; this.magic = this.magic === 255 ? 100 : this.magic + 1
|
||||
if (magic !== 156 && !this.cfwPending.some(item => item.magic === magic) && ![...this.pending.keys()].some(key => key.endsWith(`:${magic}`))) return magic
|
||||
}
|
||||
throw new Error('All message identifiers are in use')
|
||||
}
|
||||
private request(role: string, sid: number, build: (magic: number) => Uint8Array, label: string,
|
||||
timeout = 3500, flag = 0x20, fixedMagic?: number): Promise<protocol.ProtocolMessage> {
|
||||
const magic = fixedMagic ?? this.nextMagic(), payload = build(magic), key = `${sid}:${magic}`
|
||||
this.log(`TX ${label} ${role} sid=${sid.toString(16)} magic=${magic}`)
|
||||
const generation = this.generation
|
||||
return new Promise((resolve, reject) => {
|
||||
const pending: PendingAck = { resolve, reject, timer: null, command: protocol.readInteger(payload, 1, -1), label }
|
||||
this.pending.set(key, pending)
|
||||
this.send(role, sid, flag, payload).then(() => {
|
||||
if (this.pending.get(key) !== pending || generation !== this.generation) return
|
||||
pending.timer = setTimeout(() => {
|
||||
this.pending.delete(key); reject(new AckTimeout(`${label} was not acknowledged by the glasses.`))
|
||||
}, timeout)
|
||||
}).catch(error => {
|
||||
if (this.pending.get(key) !== pending) return
|
||||
this.pending.delete(key); if (pending.timer) clearTimeout(pending.timer); reject(error)
|
||||
})
|
||||
})
|
||||
}
|
||||
private send(role: string, sid: number, flag: number, payload: Uint8Array): Promise<void> {
|
||||
return this.writePackets(role, () => protocol.frameMessage(payload, sid, flag, this.sequence++, this.limits[role]))
|
||||
}
|
||||
private writePackets(role: string, build: () => Uint8Array[], current = () => true): Promise<void> {
|
||||
const identifier = this.ids[role], generation = this.generation
|
||||
if (!identifier) return Promise.reject(new Error(`No ${role} device connected`))
|
||||
const previous = this.writes.get(identifier) ?? Promise.resolve()
|
||||
// A failed write poisons this link's queue until reset: do not transmit
|
||||
// later fragments after a hole in the image stream.
|
||||
const work = previous.then(async () => {
|
||||
this.check(generation)
|
||||
if (!current()) return
|
||||
for (const frame of build()) {
|
||||
this.check(generation)
|
||||
if (!current()) return
|
||||
await this.transport.write(identifier, protocol.G2_WRITE, frame)
|
||||
iosBleTraffic.recordWrite(frame.length)
|
||||
}
|
||||
iosBleTraffic.recordMessage()
|
||||
})
|
||||
// A whole message owns the write queue; heartbeat packets cannot split an image message.
|
||||
this.writes.set(identifier, work)
|
||||
return work
|
||||
}
|
||||
private lease(acquire: boolean): Promise<void[]> {
|
||||
return Promise.all(['right', 'left'].map(role => this.send(role, protocol.SID.settings, 0x20, protocol.framebufferLease(acquire))))
|
||||
}
|
||||
private requestCfw(role: string, payload: Uint8Array): Promise<void> {
|
||||
if (payload.length > CFW_MAX_MESSAGE) return Promise.reject(new Error('CFW message too large'))
|
||||
return new Promise((resolve, reject) => {
|
||||
const item: CfwPending = { role, payload, checksum: protocol.crc16(payload), magic: this.nextMagic(), ackLenses: 0, retries: 0,
|
||||
retryPending: false, deadline: 0, resolve, reject }
|
||||
this.cfwPending.push(item)
|
||||
this.sendCfw(item)
|
||||
})
|
||||
}
|
||||
private sendCfw(item: CfwPending): void {
|
||||
const generation = this.generation, magic = item.magic
|
||||
const current = () => generation === this.generation && item.magic === magic && this.cfwPending.includes(item)
|
||||
void this.writePackets(item.role, () => {
|
||||
let transport = this.cfwTransports.get(item.role)
|
||||
if (!transport) { transport = new CfwTransport(); this.cfwTransports.set(item.role, transport) }
|
||||
if (item.retries) transport.reset()
|
||||
return transport.encode(item.payload, magic, 3, this.limits[item.role])
|
||||
}, current).then(() => {
|
||||
if (!current()) return
|
||||
item.deadline = Date.now() + 500
|
||||
this.scheduleCfwRecovery()
|
||||
}).catch(error => { if (current()) this.fail(error, true) })
|
||||
}
|
||||
private receiveCfw(identifier: string, packet: Uint8Array): void {
|
||||
const acks = parseCfwAcks(packet)
|
||||
if (!acks) return
|
||||
for (const ack of acks) {
|
||||
// The ingress temple relays the other lens's result on the SAME link.
|
||||
const item = this.cfwPending.find(p => this.ids[p.role] === identifier && p.magic === ack.streamId)
|
||||
if (!item || ack.messageId !== 0) continue
|
||||
if (ack.nack) item.retryPending = true
|
||||
else if (ack.size === item.payload.length && ack.checksum === item.checksum) item.ackLenses |= ack.lens
|
||||
this.log(`CFW ${ack.nack ? 'NACK' : 'ACK'} stream=${ack.streamId} lens=${ack.lens} size=${ack.size} crc=${ack.checksum} confirmed=${item.ackLenses}`)
|
||||
}
|
||||
while (this.cfwPending.length) {
|
||||
const head = this.cfwPending[0]
|
||||
if (head.retryPending || head.ackLenses !== 3) break
|
||||
this.cfwPending.shift(); head.resolve()
|
||||
}
|
||||
this.scheduleCfwRecovery()
|
||||
}
|
||||
private scheduleCfwRecovery(): void {
|
||||
if (this.cfwTimer !== null) clearTimeout(this.cfwTimer)
|
||||
this.cfwTimer = null
|
||||
let deadline = Infinity
|
||||
for (const item of this.cfwPending) {
|
||||
if (item.retryPending) deadline = 0
|
||||
else if (item.ackLenses !== 3 && item.deadline) deadline = Math.min(deadline, item.deadline)
|
||||
}
|
||||
if (deadline === Infinity) return
|
||||
this.cfwTimer = setTimeout(() => {
|
||||
this.cfwTimer = null
|
||||
this.recoverCfw()
|
||||
}, Math.max(0, deadline - Date.now()))
|
||||
}
|
||||
private recoverCfw(): void {
|
||||
if (!this.cfwPending.some(p => p.retryPending || (p.ackLenses !== 3 && p.deadline > 0 && p.deadline <= Date.now()))) {
|
||||
this.scheduleCfwRecovery(); return
|
||||
}
|
||||
if (this.cfwPending.some(p => p.retries >= 3)) { this.fail(new Error('CFW recovery retry limit'), true); return }
|
||||
this.log(`CFW recovery: replay ${this.cfwPending.length} unresolved messages`)
|
||||
// Go back to the oldest unresolved command, including any ACKed tail.
|
||||
// Fresh stream IDs reject stale ACKs; each replay resets compression history.
|
||||
for (const item of this.cfwPending) {
|
||||
item.magic = this.nextMagic(); item.retries++; item.ackLenses = 0
|
||||
item.deadline = 0; item.retryPending = false
|
||||
}
|
||||
for (const item of this.cfwPending) this.sendCfw(item)
|
||||
}
|
||||
private receive(event: TransportEvent): void {
|
||||
if (!Object.values(this.ids).includes(event.identifier ?? '')) return
|
||||
if (event.kind === 'ancs-authorization' && event.identifier === this.ids.right) {
|
||||
if (event.authorized === false) {
|
||||
const active = this.notifications.state !== 'disconnected'
|
||||
const command = this.notifications.stopCommand()
|
||||
this.notifications.stop('Enable Share System Notifications in iPhone Settings → Bluetooth → right lens.')
|
||||
if (active && this.state.phase === 'connected') void this.writePackets('right', () => [command]).catch(() => {})
|
||||
} else if (this.state.phase === 'connected' && /^Faceclaw\/(\d+)/.test(this.state.capabilities) && Number(this.state.capabilities.split('/')[1]) >= ANCS_FIRMWARE_VERSION) {
|
||||
this.notifications.start((Date.now() ^ Math.floor(Math.random()*0xffffffff)) >>> 0, this.limits.right)
|
||||
}
|
||||
return
|
||||
}
|
||||
if (event.kind === 'disconnected') {
|
||||
if (event.identifier === this.ids.ring) {
|
||||
this.state.ring = false
|
||||
if (this.state.phase === 'connected') {
|
||||
this.update('connected', `Glasses connected; ring disconnected${event.message ? ': ' + event.message : ''}`)
|
||||
this.scheduleRingRetry()
|
||||
}
|
||||
} else if (['connected', 'connecting'].includes(this.state.phase)) this.fail(new Error(event.message || 'Glasses disconnected'), true)
|
||||
return
|
||||
}
|
||||
if (event.kind !== 'notification' || !event.data) return
|
||||
if (event.characteristic === protocol.G2_RENDER_NOTIFY && event.identifier !== this.ids.ring) {
|
||||
if (this.audioListener && this.state.phase === 'connected') {
|
||||
this.audioListener(hexToBytes(event.data))
|
||||
this.onActivity(); this.wake()
|
||||
}
|
||||
return
|
||||
}
|
||||
const ring = event.identifier === this.ids.ring
|
||||
if (ring ? !protocol.RING_NOTIFY.includes(event.characteristic ?? '') : event.characteristic !== protocol.G2_NOTIFY) return
|
||||
try {
|
||||
const data = hexToBytes(event.data)
|
||||
if (ring) {
|
||||
const input = protocol.decodeRingInput(data)
|
||||
if (input) { this.log(`Direct R1 gesture ${input.eventType}`); this.onInput(input) }
|
||||
return
|
||||
}
|
||||
if (event.identifier === this.ids.right && this.notifications.receive(data)) return
|
||||
for (const message of this.receiver.receive(event.identifier!, data)) {
|
||||
this.log(`RX ${event.identifier === this.ids.left ? 'L' : 'R'} sid=${message.sid.toString(16)} flag=${message.flag.toString(16)} cmd=${message.command} magic=${message.magic}`)
|
||||
if (message.sid === protocol.SID.cfw) {
|
||||
this.receiveCfw(event.identifier!, message.packet!)
|
||||
continue
|
||||
}
|
||||
if (![1, 6].includes(message.flag)) {
|
||||
// A master arm can relay the other arm's ACK; magic is allocated
|
||||
// across BOTH links, matching Android's communicator.
|
||||
const key = `${message.sid}:${message.magic}`, pending = this.pending.get(key)
|
||||
if (pending) {
|
||||
this.pending.delete(key); if (pending.timer) clearTimeout(pending.timer)
|
||||
pending.resolve(message)
|
||||
}
|
||||
}
|
||||
if (message.sid === protocol.SID.settings) this.applySettings(message)
|
||||
const wearing = decodeWearState(message)
|
||||
if (wearing !== null) this.onWearState(wearing)
|
||||
if (event.identifier === this.ids.right) {
|
||||
if (this.state.phase === 'connected') {
|
||||
const compass = protocol.decodeCompassInput(message)
|
||||
if (compass) this.onCompass(compass)
|
||||
}
|
||||
const input = protocol.decodeGlassesInput(message)
|
||||
if (input) {
|
||||
if (input.kind === 'sys-event' && [5, 6, 7].includes(input.eventType) && this.state.phase === 'connected') {
|
||||
// Respect an explicit glasses-side exit; the user can reconnect.
|
||||
void this.stop(); continue
|
||||
}
|
||||
this.onInput(input)
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (error) { this.log(`Ignored malformed BLE notification: ${this.message(error)}`) }
|
||||
finally {
|
||||
// CoreBluetooth can wake a suspended iOS process for a notification.
|
||||
// Service wall-clock deadlines now, without waiting for a suspended timer.
|
||||
if (this.state.phase === 'connected') { this.onActivity(); this.wake() }
|
||||
}
|
||||
}
|
||||
private applySettings(message: protocol.ProtocolMessage): void {
|
||||
const values = protocol.readBytes(message.payload, 4)
|
||||
const capabilities = protocol.readString(message.payload, 100)
|
||||
if (capabilities) this.state.capabilities = capabilities
|
||||
if (values) {
|
||||
const left = protocol.readString(values, 5), right = protocol.readString(values, 6)
|
||||
if (left) this.state.leftVersion = left
|
||||
if (right) this.state.rightVersion = right
|
||||
const battery = protocol.readInteger(values, 12, -1), charging = protocol.readInteger(values, 13, -1)
|
||||
if (battery >= 0 && battery <= 100) this.state.battery = battery
|
||||
if (charging >= 0) this.state.charging = this.charging = charging > 0
|
||||
}
|
||||
this.onState({ ...this.state })
|
||||
}
|
||||
async enableWearDetectionAndRequestState(): Promise<void> {
|
||||
const generation = this.generation
|
||||
// Like Android, a missing enable ACK must not suppress the current-state query.
|
||||
try { await this.request('right', protocol.SID.settings, enableWearDetection, 'Enable wear detection') }
|
||||
catch (error) { if (!(error instanceof AckTimeout)) throw error; this.log(this.message(error)) }
|
||||
this.check(generation)
|
||||
await this.send('right', protocol.SID.settings, 0x20, queryWearState())
|
||||
this.check(generation)
|
||||
await this.send('left', protocol.SID.settings, 0x20, queryWearState())
|
||||
}
|
||||
async setBrightness(level: number | null): Promise<void> {
|
||||
if (this.state.phase !== 'connected') return
|
||||
await this.request('right', protocol.SID.settings, magic => setBrightness(magic, level), 'Brightness')
|
||||
}
|
||||
setFrame(gray: Uint8Array, textures?: TextureFrame): void {
|
||||
if (this.state.phase !== 'connected') return
|
||||
this.latest = protocol.packGray4(gray, 640, 480)
|
||||
this.latestTextures = textures ?? null
|
||||
this.schedule(0)
|
||||
}
|
||||
/** Same mode-5 kind-4 sequencer payload and acknowledged image path as Android. */
|
||||
async playBuzzerSequence(payload: Uint8Array): Promise<void> {
|
||||
if (this.state.phase !== 'connected' || !this.layoutCreated || payload.length < 3) return
|
||||
const generation = this.generation
|
||||
try { await this.requestCfw('left', new Uint8Array(payload)) }
|
||||
catch (error) { if (generation === this.generation) this.fail(error, true) }
|
||||
}
|
||||
/** Serialize mic enable/disable, including a release while enable awaits ACK. */
|
||||
setMicrophone(enabled: boolean, listener?: (packet: Uint8Array) => void): Promise<void> {
|
||||
const token = ++this.microphoneToken, generation = this.generation
|
||||
this.audioListener = enabled ? listener ?? null : null
|
||||
const current = () => generation === this.generation && token === this.microphoneToken
|
||||
const work = this.microphoneWork.catch(() => {}).then(async () => {
|
||||
if (!current()) return
|
||||
if (this.state.phase !== 'connected') {
|
||||
if (enabled) throw new Error('Connect the glasses before starting voice input.')
|
||||
return
|
||||
}
|
||||
if (enabled) {
|
||||
await Promise.all(['left', 'right'].map(role => this.transport.subscribe(this.ids[role], protocol.G2_RENDER_NOTIFY)))
|
||||
if (!current()) return
|
||||
}
|
||||
await this.request('right', protocol.SID.hub, magic => protocol.audioControl(magic, enabled), enabled ? 'Microphone enable' : 'Microphone disable')
|
||||
if (current()) { this.microphoneEnabled = enabled; this.log(`Glasses microphone ${enabled ? 'enabled' : 'disabled'}`) }
|
||||
}).catch(error => {
|
||||
if (current()) { this.audioListener = null; this.microphoneEnabled = false }
|
||||
throw error
|
||||
})
|
||||
this.microphoneWork = work
|
||||
return work
|
||||
}
|
||||
setCompassEnabled(enabled: boolean): void {
|
||||
this.compassWanted = enabled
|
||||
void this.syncCompass()
|
||||
}
|
||||
/** Serialize changes so a release cannot overtake an unacknowledged enable. */
|
||||
private syncCompass(): Promise<void> {
|
||||
const generation = this.generation
|
||||
const work = this.compassWork.then(async () => {
|
||||
if (generation !== this.generation || this.state.phase !== 'connected') return
|
||||
const enabled = !this.compassStopping && this.compassWanted
|
||||
if (this.compassSent === enabled) return
|
||||
// Same CFW mode 10, 100 ms report interval and zero minimum change as Android.
|
||||
await this.requestCfw('left', new Uint8Array(enabled ? [10, 2, 100, 0, 0, 0] : [10, 0]))
|
||||
if (generation === this.generation) this.compassSent = enabled
|
||||
}).catch(error => { if (generation === this.generation) this.fail(error, true) })
|
||||
this.compassWork = work
|
||||
return work
|
||||
}
|
||||
wake(): void {
|
||||
if (this.timer !== null) { clearTimeout(this.timer); this.timer = null }
|
||||
void this.pump()
|
||||
}
|
||||
private schedule(delay = 1000): void {
|
||||
if (delay === 0 && this.timer !== null) { clearTimeout(this.timer); this.timer = null }
|
||||
if (this.timer !== null || this.pumping || this.state.phase !== 'connected') return
|
||||
this.timer = setTimeout(() => { this.timer = null; void this.pump() }, delay)
|
||||
}
|
||||
private canSendDisplay(): boolean {
|
||||
// Also bound completed frames retained behind a missing/out-of-order ACK.
|
||||
return this.displayInFlight < DISPLAY_WINDOW_SIZE && !!(this.displaySending ||
|
||||
(this.latest && !this.charging && this.displayFrames.length < DISPLAY_WINDOW_SIZE))
|
||||
}
|
||||
private fillDisplayWindow(generation: number): void {
|
||||
while (this.canSendDisplay()) {
|
||||
if (!this.displaySending) {
|
||||
const packed = this.latest!; this.latest = null
|
||||
const textureFrame = this.latestTextures; this.latestTextures = null
|
||||
// Compare with the last enqueued image, not the last ACKed one. In an
|
||||
// A -> B -> A sequence, B may still be in flight when A is requested.
|
||||
if (this.lastEnqueued && packed.every((value, i) => value === this.lastEnqueued![i])) continue
|
||||
const texturePlan = textureFrame ? this.textures?.plan(this.lastEnqueued, packed, textureFrame, this.nextImageFrameId) : null
|
||||
let commands: Uint8Array[]
|
||||
if (texturePlan) {
|
||||
// Uploads participate in the same ordered, acknowledged command window as
|
||||
// the image. Coalescing only happens before planning mutates residency.
|
||||
commands = [...texturePlan.uploads, texturePlan.payload]
|
||||
this.nextImageFrameId = texturePlan.nextFid
|
||||
this.log(`Display textures (${texturePlan.uploads.length} uploads, cache=${texturePlan.usedBytes}B)`)
|
||||
} else {
|
||||
const delta = buildBoundingBoxPayload(this.lastEnqueued, packed, 640, 480, this.nextImageFrameId)
|
||||
const payload = delta ?? protocol.concat(new Uint8Array([6]), protocol.rle4(packed))
|
||||
if (delta) this.nextImageFrameId = this.nextImageFrameId >= 0xfffe ? 1 : this.nextImageFrameId + 1
|
||||
commands = payload.length <= CFW_MAX_MESSAGE ? [payload] : buildFullFrameBands(packed, 640, 480, this.nextImageFrameId)
|
||||
if (payload.length > CFW_MAX_MESSAGE) for (const _ of commands)
|
||||
this.nextImageFrameId = this.nextImageFrameId >= 0xfffe ? 1 : this.nextImageFrameId + 1
|
||||
this.log(`Display ${delta ? 'bbox' : 'full'} (${commands.length} CFW commands)`)
|
||||
}
|
||||
this.displaySending = { packed, commands, offset: 0, pending: 0 }
|
||||
this.displayFrames.push(this.displaySending); this.lastEnqueued = packed
|
||||
}
|
||||
const frame = this.displaySending, payload = frame.commands[frame.offset++]
|
||||
frame.pending++; this.displayInFlight++
|
||||
if (frame.offset >= frame.commands.length) this.displaySending = null
|
||||
// Writes serialize on L while up to three complete commands await their
|
||||
// two-lens ACKs. Retire commands in submission order.
|
||||
void this.requestCfw('left', payload).then(() => {
|
||||
if (generation !== this.generation) return
|
||||
frame.pending--; this.displayInFlight--; this.lastHeartbeat = Date.now()
|
||||
// Retire in submission order even if relayed ACKs arrive out of order.
|
||||
// A frame counts only after every command has been acknowledged.
|
||||
while (this.displayFrames.length) {
|
||||
const completed = this.displayFrames[0]
|
||||
if (completed.offset < completed.commands.length || completed.pending) break
|
||||
this.displayFrames.shift(); this.displayed = completed.packed
|
||||
this.state.frames++; iosBleTraffic.recordDisplayFrame(); this.onState({ ...this.state })
|
||||
this.log(`Display frame ${this.state.frames} acknowledged (${completed.commands.length} commands)`)
|
||||
}
|
||||
this.wake()
|
||||
}).catch(error => { if (generation === this.generation) this.fail(error, true) })
|
||||
}
|
||||
}
|
||||
private async pump(): Promise<void> {
|
||||
if (this.pumping || this.state.phase !== 'connected') return
|
||||
this.pumping = true; const generation = this.generation
|
||||
try {
|
||||
const now = Date.now()
|
||||
// Firmware frees textures when its 90-second framebuffer lease expires.
|
||||
// After a long iOS suspension, reconnect rather than replaying pending
|
||||
// draws against unknown memory. Leave a margin for the two arm writes.
|
||||
if (now - this.lastLease >= 80_000) throw new Error('Display lease lapsed; restarting session')
|
||||
if (now - this.lastHeartbeat >= 4000) {
|
||||
const gap = now - this.lastHeartbeat
|
||||
await this.request('right', protocol.SID.hub, protocol.heartbeat, 'Heartbeat', 1500); this.lastHeartbeat = Date.now()
|
||||
this.log(`Heartbeat acknowledged (gap ${gap}ms)`)
|
||||
}
|
||||
if (now - this.lastLease >= 45_000) { await this.lease(true); this.lastLease = Date.now() }
|
||||
if (now - this.lastSettings >= (this.charging ? 30_000 : 300_000)) {
|
||||
const settings = await this.request('right', protocol.SID.settings, protocol.settingsQuery, 'Battery query')
|
||||
this.applySettings(settings); this.lastSettings = Date.now()
|
||||
}
|
||||
this.check(generation)
|
||||
this.fillDisplayWindow(generation)
|
||||
} catch (error) { if (generation === this.generation) this.fail(error, true) }
|
||||
finally { if (generation === this.generation) { this.pumping = false; this.schedule(this.canSendDisplay() ? 0 : 1000) } }
|
||||
}
|
||||
private reset(): void {
|
||||
this.notifications.stop()
|
||||
++this.generation
|
||||
this.compassSent = null; this.compassWork = Promise.resolve()
|
||||
++this.microphoneToken; this.audioListener = null; this.microphoneEnabled = false
|
||||
if (this.timer !== null) clearTimeout(this.timer)
|
||||
if (this.retryTimer !== null) clearTimeout(this.retryTimer)
|
||||
if (this.ringRetryTimer !== null) clearTimeout(this.ringRetryTimer)
|
||||
this.ringRetryTimer = null; this.ringConnecting = false; this.ringRetryCount = 0
|
||||
this.timer = this.retryTimer = null; this.pumping = false
|
||||
const pending = [...this.pending.values()]; this.pending.clear()
|
||||
for (const request of pending) { if (request.timer) clearTimeout(request.timer); request.reject(new Error('Connection ended')) }
|
||||
if (this.cfwTimer !== null) clearTimeout(this.cfwTimer)
|
||||
this.cfwTimer = null
|
||||
const custom = this.cfwPending; this.cfwPending = []
|
||||
for (const item of custom) item.reject(new Error('Connection ended'))
|
||||
this.cfwTransports.clear()
|
||||
this.receiver.clear(); this.writes.clear(); this.latest = this.displayed = null
|
||||
this.latestTextures = null; this.textures?.reset()
|
||||
this.lastEnqueued = null; this.displayFrames = []; this.displaySending = null; this.displayInFlight = 0
|
||||
}
|
||||
private closeLinks(): void {
|
||||
const ids = this.ids; this.ids = {}; this.limits = {}
|
||||
for (const identifier of Object.values(ids)) this.transport.disconnect(identifier)
|
||||
this.transport.stopScan(); this.layoutCreated = false; this.state.ring = false
|
||||
}
|
||||
private fail(error: unknown, retry: boolean): void {
|
||||
const message = this.message(error); this.reset(); this.closeLinks()
|
||||
if (retry && this.wanted && this.addresses && this.retryCount < 5) {
|
||||
const delay = Math.min(30_000, 2000 * 2 ** this.retryCount++)
|
||||
this.update('retrying', `${message} Retrying in ${delay / 1000}s…`)
|
||||
this.retryTimer = setTimeout(() => { this.retryTimer = null; if (this.wanted && this.addresses) void this.start(this.addresses, true) }, delay)
|
||||
} else { this.wanted = false; this.update('error', message) }
|
||||
}
|
||||
stop(): Promise<void> {
|
||||
if (this.stopping) return this.stopping
|
||||
this.stopping = this.stopInternal().finally(() => { this.stopping = null })
|
||||
return this.stopping
|
||||
}
|
||||
private async stopInternal(): Promise<void> {
|
||||
this.wanted = false
|
||||
this.compassStopping = true
|
||||
await this.syncCompass()
|
||||
// Stop mic capture before ending the display session. Late enable ACKs
|
||||
// cannot leave a microphone streaming after an explicit Disconnect.
|
||||
if (this.audioListener || this.microphoneEnabled) {
|
||||
try { await this.setMicrophone(false) } catch (error) { this.log(`Microphone cleanup: ${this.message(error)}`) }
|
||||
}
|
||||
if (this.notifications.state !== 'disconnected') {
|
||||
try { await this.writePackets('right', () => [this.notifications.stopCommand()]) } catch {}
|
||||
}
|
||||
const cleanup = this.layoutCreated && this.state.phase === 'connected'
|
||||
this.update('disconnecting', 'Disconnecting…'); this.reset()
|
||||
try {
|
||||
if (cleanup) {
|
||||
await this.requestCfw('left', new Uint8Array([11]))
|
||||
}
|
||||
} catch (error) { this.log(`Disconnect cleanup: ${this.message(error)}`) }
|
||||
finally { this.reset(); this.closeLinks(); this.update('disconnected', 'Preview only') }
|
||||
}
|
||||
dispose(): void { void this.stop().finally(() => this.off()) }
|
||||
private message(error: unknown): string { return error instanceof Error ? error.message : String(error) }
|
||||
}
|
||||
+408
-132
@@ -17,10 +17,11 @@ import { IosNavigationSensors } from '../native/ios-navigation-sensors'
|
||||
import { Utils } from '@nativescript/core'
|
||||
import { bindCompassSession, receiveCompassEvent } from '../native/compass.ios'
|
||||
import { Dialogs, File, knownFolders, path, type ImageSource } from '@nativescript/core'
|
||||
import { iosBluetooth } from '../native/ios-bluetooth'
|
||||
import { iosVoiceInput } from '../native/ios-voice-input'
|
||||
import { nightscoutBridge } from '../native/nightscout-bridge'
|
||||
import { GlassesSession, type SessionState } from './glasses-session'
|
||||
import { FaceclawCommunicatorBridge, resolveIosPeripherals, type CommunicatorState, type RawInputEvent } from '../native/faceclaw-communicator.ios'
|
||||
import { PreviewDisplayTarget, type DisplayTarget } from '../native/preview-display.ios'
|
||||
import { AncsClient, ANCS_FIRMWARE_VERSION } from './ancs-client'
|
||||
import { GlanceHost, type GlanceDisplay } from './glance-host'
|
||||
import { createLockScreenImage, LOCK_SCREEN_SURFACE_ID } from './lock-screen'
|
||||
import { OsEventTypeList } from './events'
|
||||
@@ -34,64 +35,90 @@ import { createInProcessWindow, YieldAtRootLayer, type InProcessAppOptions, type
|
||||
import { getStringSettingById, nightscoutSiteUrlSetting, nightscoutApiTokenSetting } from '../ui/dashboard-settings'
|
||||
import { readPhoneBatteryState } from '../native/phone-battery'
|
||||
import { iosAppUnavailableReason } from '../apps/ios-availability'
|
||||
import { SurfaceCompositor } from '../graphics/surface-compositor'
|
||||
import { flattenPlanesWithDraws, type Plane } from '../graphics/plane'
|
||||
import { flattenPlanesWithDraws, planesFingerprint, type Plane } from '../graphics/plane'
|
||||
import { prepareFrameDraws } from '../graphics/glyph-wire'
|
||||
import { IosTexturePlanner } from '../native/texture-planner.ios'
|
||||
import { G2_LENS_WIDTH, G2_LENS_HEIGHT } from '../graphics/image'
|
||||
import { previewPixels } from '../native/ios-graphics'
|
||||
import { makeInputEvent, type InputEvent, type InputEventPayload } from '../ui/gestures'
|
||||
import { noopLayerActions, type LayerActions } from '../ui/layers'
|
||||
import { TextViewerLayer } from '../apps/files/text-viewer'
|
||||
import { shell, rawInputEventToInputEvent, type ShellWindow } from '../ui/shell/shell'
|
||||
import { appViewportRect, SIDEBAR_WIDTH, sidebarStripVisible } from '../ui/shell/geometry'
|
||||
import { DISPLAY_MODE_VALUES, displayModeLabel, displayModeSetting, onAnySettingChanged,
|
||||
previewColorSetting, lockScreenEnabledSetting, brightnessSetting, brightnessSettingToLevel } from '../ui/dashboard-settings'
|
||||
previewColorSetting, lockScreenEnabledSetting, getBrightnessPreferences } from '../ui/dashboard-settings'
|
||||
import type { PhoneGesture } from '../phone-ui/phone-gestures'
|
||||
import { isWelcomeSoundPending, setWelcomeSoundPending } from '../phone-ui/onboarding-state'
|
||||
import { findSoundEffect, playSoundEffect } from '../ui/sound-effects'
|
||||
|
||||
/** iOS host for the shared app registry, shell, compositor and BLE session. */
|
||||
const SHELL_SURFACE_ID = 'shell'
|
||||
const SHELL_SURFACE_Z_ORDER = 1
|
||||
const LOCK_SURFACE_Z_ORDER = 1000
|
||||
/** The shell render loop waits this long for the glasses to take a frame before moving on. */
|
||||
const FRAME_TRANSMIT_BACKPRESSURE_TIMEOUT_MS = 2000
|
||||
|
||||
export type SessionPhase = 'disconnected' | 'connecting' | 'connected' | 'retrying' | 'disconnecting' | 'error'
|
||||
export type SessionState = { phase: SessionPhase; status: string; battery: number | null; charging: boolean | null;
|
||||
ring: boolean; frames: number; capabilities: string; leftVersion: string; rightVersion: string }
|
||||
|
||||
function ancsCapable(capabilities: string): boolean {
|
||||
return /^Faceclaw\/(\d+)/.test(capabilities) && Number(capabilities.split('/')[1]) >= ANCS_FIRMWARE_VERSION
|
||||
}
|
||||
|
||||
/**
|
||||
* iOS host for the shared app registry, shell and BLE session. The session,
|
||||
* compositor and transport live in shared Kotlin (FaceclawCommunicatorBridge
|
||||
* over FaceclawKitIosGlassesSession); with no glasses connected the same
|
||||
* compositor runs headless (PreviewDisplayTarget) so the phone mirror works.
|
||||
*/
|
||||
export class IosPreviewController {
|
||||
private readonly compositor = new SurfaceCompositor(G2_LENS_WIDTH, G2_LENS_HEIGHT)
|
||||
private previewTarget: PreviewDisplayTarget | null = null
|
||||
private communicator: FaceclawCommunicatorBridge | null = null
|
||||
private readonly startedCommunicators = new WeakSet<FaceclawCommunicatorBridge>()
|
||||
state: SessionState = { phase: 'disconnected', status: 'Preview only', battery: null, charging: null, ring: false,
|
||||
frames: 0, capabilities: '', leftVersion: '', rightVersion: '' }
|
||||
private notifications: AncsClient | null = null
|
||||
private readonly sessionOffs: (() => void)[] = []
|
||||
private stopping: Promise<void> | null = null
|
||||
/** Resolves once the current display target has every surface configured. */
|
||||
private displayReady: Promise<void> = Promise.resolve()
|
||||
private connecting = false
|
||||
private sessionCreated = false
|
||||
private wearDetectionRequested = false
|
||||
private readonly glanceDisplay: GlanceDisplay = {
|
||||
configureSurface: async (id, options) => {
|
||||
this.compositor.configureSurface(id, options)
|
||||
},
|
||||
setSurfaceVisible: async (id, visible) => {
|
||||
this.compositor.setSurfaceVisible(id, visible); this.scheduleFrame()
|
||||
},
|
||||
setScreenBlanked: async blanked => {
|
||||
this.compositor.setScreenBlanked(blanked); this.scheduleFrame()
|
||||
},
|
||||
submitSurfaceFrame: async (id, pixels, rect, _fingerprint, _paintMs, _frameId, draws) => {
|
||||
this.compositor.submitSurfaceFrame(id, pixels, rect, draws); this.scheduleFrame()
|
||||
configureSurface: async (id, options) => { await this.display?.configureSurface(id, options) },
|
||||
setSurfaceVisible: async (id, visible) => { await this.display?.setSurfaceVisible(id, visible); this.schedulePreviewUpdate() },
|
||||
setSurfaceDepth: async (id, depth) => { await this.display?.setSurfaceDepth(id, depth) },
|
||||
setScreenBlanked: async blanked => { await this.display?.setScreenBlanked(blanked); this.schedulePreviewUpdate() },
|
||||
submitSurfaceFrame: async (id, pixels, rect, fingerprint, paintMs, frameId, draws) => {
|
||||
await this.display?.submitSurfaceFrame(id, pixels, rect, fingerprint, paintMs, frameId, draws); this.schedulePreviewUpdate()
|
||||
},
|
||||
}
|
||||
private readonly glance = new GlanceHost({
|
||||
getDisplay: () => this.glanceDisplay,
|
||||
getDisplay: () => this.display ? this.glanceDisplay : null,
|
||||
getProvider: () => ALL_APPS.find(app => app.glanceboard)?.glanceboard ?? null,
|
||||
canShow: () => this.runtimeNeeded && !this.glassesLocked,
|
||||
// iOS retains the EvenHub session while the shell sleeps. The board's
|
||||
// opaque first frame is ready before we unblank the compositor.
|
||||
ensureSessionActive: async () => {
|
||||
this.compositor.setScreenBlanked(false); this.scheduleFrame(); return true
|
||||
await this.display?.setScreenBlanked(false); this.schedulePreviewUpdate(); return true
|
||||
},
|
||||
onHiddenWhileAsleep: () => {
|
||||
if (!shell.isScreenOn()) this.compositor.setScreenBlanked(true)
|
||||
this.scheduleFrame()
|
||||
if (!shell.isScreenOn()) void this.display?.setScreenBlanked(true).then(() => this.schedulePreviewUpdate())
|
||||
else this.schedulePreviewUpdate()
|
||||
},
|
||||
onVisibilityChanged: () => this.scheduleFrame(),
|
||||
onVisibilityChanged: () => this.schedulePreviewUpdate(),
|
||||
appendLog: message => this.logBluetooth(message),
|
||||
})
|
||||
private renderTimer: ReturnType<typeof setTimeout> | null = null
|
||||
private previewTimer: ReturnType<typeof setTimeout> | null = null
|
||||
private clockTimer: ReturnType<typeof setInterval> | null = null
|
||||
private offSettings: (() => void) | null = null
|
||||
private lastBrightness: string | null = null
|
||||
private active = false
|
||||
private runtimeRunning = false
|
||||
private clockMinute = -1
|
||||
private shellDirty = true
|
||||
private shellRenderInProgress = false
|
||||
private shellRenderQueued = false
|
||||
private nextFrameId = 1
|
||||
private nextWorkerFrame = 0
|
||||
private inputQueue: Promise<void> = Promise.resolve()
|
||||
private lastLayout = ''
|
||||
private readonly appHosts = new Map<string, WorkerAppHost>()
|
||||
@@ -102,13 +129,12 @@ export class IosPreviewController {
|
||||
private glassesWorn: boolean | null = null
|
||||
private glassesLocked = false
|
||||
private lockEnabled = lockScreenEnabledSetting.get()
|
||||
private session: GlassesSession | null = null
|
||||
private acknowledgedFrames = 0
|
||||
private logLines: string[] = []
|
||||
private logTimer: ReturnType<typeof setTimeout> | null = null
|
||||
private readonly actions: LayerActions = {
|
||||
...noopLayerActions,
|
||||
playBuzzerSequence: payload => this.session?.playBuzzerSequence(payload),
|
||||
playBuzzerSequence: payload => this.communicator?.playBuzzerSequence(payload),
|
||||
requestRender: () => this.requestShellRender(),
|
||||
disconnect: () => this.disconnect(),
|
||||
startVoiceCapture: endpointing => this.startVoiceCapture(endpointing),
|
||||
@@ -130,11 +156,7 @@ export class IosPreviewController {
|
||||
private readonly onError: (message: string) => void,
|
||||
private readonly onConnectionState: (state: SessionState) => void = () => {},
|
||||
private readonly onKeyboardInputChanged: (session: KeyboardInputSession | null) => void = () => {}) {
|
||||
this.compositor.configureSurface('shell', { x: 0, y: 0, width: 640, height: 480, zOrder: 1, transparency: 'color-key' })
|
||||
this.compositor.configureSurface(LOCK_SCREEN_SURFACE_ID, {
|
||||
x: 0, y: 0, width: G2_LENS_WIDTH, height: G2_LENS_HEIGHT, zOrder: 1000, transparency: 'opaque',
|
||||
})
|
||||
this.compositor.setSurfaceVisible(LOCK_SCREEN_SURFACE_ID, false)
|
||||
this.ensurePreviewDisplay()
|
||||
shell.configure({
|
||||
actions: this.actions,
|
||||
voiceInputEnabled: true,
|
||||
@@ -151,7 +173,8 @@ export class IosPreviewController {
|
||||
onWindowsChanged: () => this.requestShellRender(),
|
||||
onScreenStateChanged: on => {
|
||||
if (on) this.glance.dismiss()
|
||||
this.compositor.setScreenBlanked(!on); this.requestShellRender()
|
||||
void this.display?.setScreenBlanked(!on).catch(error => this.fail(error))
|
||||
this.requestShellRender()
|
||||
},
|
||||
})
|
||||
const launcher = createLauncherWindow({
|
||||
@@ -160,9 +183,9 @@ export class IosPreviewController {
|
||||
launchApp: id => this.launchApp(id),
|
||||
uninstallApp: id => this.uninstallApp(id),
|
||||
submitFrame: planes => this.submit(LAUNCHER_SURFACE_ID, planes),
|
||||
setSurfaceVisible: visible => { this.compositor.setSurfaceVisible(LAUNCHER_SURFACE_ID, visible); this.scheduleFrame() },
|
||||
setSurfaceVisible: visible => this.setWindowSurfaceVisible(LAUNCHER_SURFACE_ID, visible),
|
||||
})
|
||||
this.configureWindow(launcher)
|
||||
void this.configureWindow(launcher)
|
||||
shell.registerWindow(launcher)
|
||||
shell.wake('window')
|
||||
shell.focusWindow(launcher.windowId)
|
||||
@@ -198,14 +221,54 @@ export class IosPreviewController {
|
||||
for (const app of ALL_APPS) if (app.appId !== 'launcher' && !iosAppUnavailableReason(app.appId)) app.boot?.(this.buildAppContext(app))
|
||||
}
|
||||
|
||||
/** The compositor frames go to: the live session, else the headless preview. */
|
||||
private get display(): DisplayTarget | null { return this.communicator ?? this.previewTarget }
|
||||
|
||||
private ensurePreviewDisplay(): void {
|
||||
if (this.communicator || this.previewTarget) return
|
||||
const target = new PreviewDisplayTarget()
|
||||
this.previewTarget = target
|
||||
target.activate(() => this.schedulePreviewUpdate())
|
||||
this.displayReady = this.registerSurfaces(target).catch(error => this.fail(error))
|
||||
}
|
||||
|
||||
private teardownPreviewDisplay(): void {
|
||||
const target = this.previewTarget
|
||||
if (!target) return
|
||||
this.previewTarget = null
|
||||
this.glance.reset()
|
||||
target.release()
|
||||
}
|
||||
|
||||
/** (Re)create every compositor surface on a fresh display target. */
|
||||
private async registerSurfaces(target: DisplayTarget): Promise<void> {
|
||||
await target.configureCompositorScreen(G2_LENS_WIDTH, G2_LENS_HEIGHT)
|
||||
await target.configureSurface(SHELL_SURFACE_ID, { x: 0, y: 0, width: G2_LENS_WIDTH, height: G2_LENS_HEIGHT,
|
||||
zOrder: SHELL_SURFACE_Z_ORDER, transparency: 'color-key' })
|
||||
await target.setUnderlayDim(SHELL_SURFACE_Z_ORDER, 1)
|
||||
await target.configureSurface(LOCK_SCREEN_SURFACE_ID, {
|
||||
x: 0, y: 0, width: G2_LENS_WIDTH, height: G2_LENS_HEIGHT, zOrder: LOCK_SURFACE_Z_ORDER, transparency: 'opaque',
|
||||
})
|
||||
if (this.glassesLocked) await this.submitLockImage(target)
|
||||
await target.setSurfaceVisible(LOCK_SCREEN_SURFACE_ID, this.glassesLocked)
|
||||
await target.setScreenBlanked(!shell.isScreenOn())
|
||||
this.lastLayout = ''
|
||||
for (const window of shell.getWindows()) await this.configureWindow(window, target)
|
||||
}
|
||||
|
||||
private async submitLockImage(target: DisplayTarget): Promise<void> {
|
||||
const image = createLockScreenImage()
|
||||
await target.submitSurfaceFrame(LOCK_SCREEN_SURFACE_ID, image.to8bppBuffer(),
|
||||
{ x: 0, y: 0, width: image.width, height: image.height }, 'lock-screen', 0, 0)
|
||||
}
|
||||
|
||||
resume(): void {
|
||||
if (this.active) return
|
||||
this.active = true
|
||||
this.syncRuntime()
|
||||
this.handlePhoneLockState(!UIApplication.sharedApplication.protectedDataAvailable)
|
||||
this.logBluetooth('Phone foreground')
|
||||
if (this.session) this.onConnectionState({ ...this.session.state })
|
||||
this.session?.wake()
|
||||
if (this.communicator) this.onConnectionState({ ...this.state })
|
||||
this.relayout()
|
||||
shell.foregroundWindow()?.requestRender()
|
||||
this.requestShellRender()
|
||||
@@ -216,12 +279,11 @@ export class IosPreviewController {
|
||||
iosVoiceInput.stopPhoneCapture()
|
||||
// The phone preview is hidden; the glasses still own their screen and input.
|
||||
this.syncRuntime()
|
||||
this.logBluetooth(`Phone background; glasses ${this.session?.state.phase ?? 'disconnected'}`)
|
||||
this.logBluetooth(`Phone background; glasses ${this.state.phase}`)
|
||||
this.flushBluetoothLog()
|
||||
this.session?.wake()
|
||||
}
|
||||
private get runtimeNeeded(): boolean {
|
||||
return this.active || !!this.session && ['connected', 'connecting', 'retrying', 'disconnecting'].includes(this.session.state.phase)
|
||||
return this.active || ['connected', 'connecting', 'retrying', 'disconnecting'].includes(this.state.phase)
|
||||
}
|
||||
private syncRuntime(): void {
|
||||
const running = this.runtimeNeeded
|
||||
@@ -231,14 +293,14 @@ export class IosPreviewController {
|
||||
if (!running) {
|
||||
void nightscoutBridge.stop().catch(error => this.fail(error))
|
||||
this.glance.dismiss()
|
||||
this.compositor.setScreenBlanked(!shell.isScreenOn())
|
||||
void this.display?.setScreenBlanked(!shell.isScreenOn()).catch(error => this.fail(error))
|
||||
this.offSettings?.(); this.offSettings = null
|
||||
for (const observer of this.batteryObservers.splice(0)) NSNotificationCenter.defaultCenter.removeObserver(observer)
|
||||
for (const observer of this.lockObservers.splice(0)) NSNotificationCenter.defaultCenter.removeObserver(observer)
|
||||
UIDevice.currentDevice.batteryMonitoringEnabled = false
|
||||
if (this.clockTimer !== null) clearInterval(this.clockTimer)
|
||||
if (this.renderTimer !== null) clearTimeout(this.renderTimer)
|
||||
this.clockTimer = this.renderTimer = null
|
||||
if (this.previewTimer !== null) clearTimeout(this.previewTimer)
|
||||
this.clockTimer = this.previewTimer = null
|
||||
return
|
||||
}
|
||||
UIDevice.currentDevice.batteryMonitoringEnabled = true
|
||||
@@ -268,12 +330,13 @@ export class IosPreviewController {
|
||||
this.clockTimer = setInterval(() => this.refreshClock(), 60_000)
|
||||
}
|
||||
private pushBrightness(): void {
|
||||
const session = this.session
|
||||
if (session?.state.phase !== 'connected') { this.lastBrightness = null; return }
|
||||
const value = brightnessSetting.get()
|
||||
const communicator = this.communicator
|
||||
if (!communicator || this.state.phase !== 'connected') { this.lastBrightness = null; return }
|
||||
const preferences = getBrightnessPreferences()
|
||||
const value = JSON.stringify(preferences)
|
||||
if (value === this.lastBrightness) return
|
||||
this.lastBrightness = value
|
||||
void session.setBrightness(brightnessSettingToLevel(value)).catch(error => {
|
||||
void communicator.configureBrightness(preferences).catch(error => {
|
||||
this.lastBrightness = null
|
||||
this.logBluetooth(`Brightness: ${String(error)}`)
|
||||
})
|
||||
@@ -285,12 +348,13 @@ export class IosPreviewController {
|
||||
if (!enabled) this.setGlassesLocked(false)
|
||||
else {
|
||||
if (this.phoneLocked && this.glassesWorn === false) this.setGlassesLocked(true)
|
||||
if (this.session?.state.phase === 'connected')
|
||||
void this.session.enableWearDetectionAndRequestState().catch(error => this.fail(error))
|
||||
if (this.communicator && this.state.phase === 'connected')
|
||||
void this.communicator.enableWearDetectionAndRequestState().catch(error => this.fail(error))
|
||||
}
|
||||
}
|
||||
private handlePhoneLockState(locked: boolean): void {
|
||||
this.phoneLocked = locked
|
||||
this.communicator?.onPhoneLockSignal()
|
||||
if (!locked) this.setGlassesLocked(false)
|
||||
else if (this.lockEnabled && this.glassesWorn === false) this.setGlassesLocked(true)
|
||||
}
|
||||
@@ -304,17 +368,21 @@ export class IosPreviewController {
|
||||
}
|
||||
private setGlassesLocked(locked: boolean): void {
|
||||
if (locked === this.glassesLocked) return
|
||||
if (locked) {
|
||||
const image = createLockScreenImage()
|
||||
this.compositor.submitSurfaceFrame(LOCK_SCREEN_SURFACE_ID, image.to8bppBuffer(),
|
||||
{ x: 0, y: 0, width: image.width, height: image.height })
|
||||
}
|
||||
this.glassesLocked = locked
|
||||
if (locked) {
|
||||
this.glance.dismiss()
|
||||
iosVoiceInput.handleSessionEnded('Glasses locked. Unlock your phone to start voice input again.')
|
||||
}
|
||||
this.compositor.setSurfaceVisible(LOCK_SCREEN_SURFACE_ID, locked)
|
||||
void (async () => {
|
||||
await this.displayReady
|
||||
const display = this.display
|
||||
if (!display) return
|
||||
{
|
||||
if (locked) await this.submitLockImage(display)
|
||||
await display.setSurfaceVisible(LOCK_SCREEN_SURFACE_ID, locked)
|
||||
this.schedulePreviewUpdate()
|
||||
}
|
||||
})().catch(error => this.fail(error))
|
||||
this.logBluetooth(`Glasses ${locked ? 'locked' : 'unlocked'}`)
|
||||
this.requestShellRender()
|
||||
}
|
||||
@@ -322,47 +390,90 @@ export class IosPreviewController {
|
||||
const minute = Math.floor(Date.now() / 60_000)
|
||||
if (minute !== this.clockMinute) { this.clockMinute = minute; this.requestShellRender() }
|
||||
}
|
||||
private configureWindow(window: ShellWindow): void {
|
||||
this.compositor.configureSurface(window.surfaceId, {
|
||||
private async configureWindow(window: ShellWindow, target: DisplayTarget | null = this.display): Promise<void> {
|
||||
if (!target) return
|
||||
await target.configureSurface(window.surfaceId, {
|
||||
...appViewportRect(window.heightMode, window.appId), zOrder: 0, transparency: 'opaque',
|
||||
})
|
||||
this.compositor.setSurfaceVisible(window.surfaceId, shell.foregroundWindow()?.windowId === window.windowId)
|
||||
await target.setSurfaceVisible(window.surfaceId, shell.foregroundWindow()?.windowId === window.windowId)
|
||||
}
|
||||
private relayout(): void {
|
||||
const layout = shell.getWindows().map(window => JSON.stringify(appViewportRect(window.heightMode, window.appId))).join(";")
|
||||
if (layout === this.lastLayout) return
|
||||
this.lastLayout = layout
|
||||
for (const window of shell.getWindows()) {
|
||||
this.configureWindow(window)
|
||||
void this.configureWindow(window).catch(error => this.fail(error))
|
||||
window.relayout?.()
|
||||
}
|
||||
}
|
||||
/** Submit a painted frame for an in-process window (e.g. the launcher). */
|
||||
private async submit(id: string, planes: Plane[]): Promise<void> {
|
||||
await this.displayReady
|
||||
const display = this.display
|
||||
if (!display) return
|
||||
const fingerprint = planesFingerprint(planes)
|
||||
const { image, draws } = flattenPlanesWithDraws(planes)
|
||||
this.compositor.submitSurfaceFrame(id, image.pixels, { x: 0, y: 0, width: image.width, height: image.height }, prepareFrameDraws(draws))
|
||||
this.scheduleFrame()
|
||||
await display.submitSurfaceFrame(id, image.pixels, { x: 0, y: 0, width: image.width, height: image.height },
|
||||
fingerprint, -1, 0, prepareFrameDraws(draws))
|
||||
this.schedulePreviewUpdate()
|
||||
}
|
||||
private requestShellRender(): void { this.shellDirty = true; this.scheduleFrame() }
|
||||
private scheduleFrame(): void {
|
||||
if (!this.runtimeNeeded || this.renderTimer !== null) return
|
||||
// Coalesce window/chrome updates into one glasses frame, capped at 30 fps.
|
||||
this.renderTimer = setTimeout(() => {
|
||||
this.renderTimer = null
|
||||
if (!this.runtimeNeeded) return
|
||||
/** Worker windows ship baked pixels; every submit is new content to the compositor. */
|
||||
private async submitWorkerPixels(id: string, pixels: Uint8Array, width: number, height: number, draws: ArrayBuffer | null): Promise<void> {
|
||||
await this.displayReady
|
||||
const display = this.display
|
||||
if (!display) return
|
||||
await display.submitSurfaceFrame(id, pixels, { x: 0, y: 0, width, height }, `${id}#${++this.nextWorkerFrame}`, -1, 0, draws)
|
||||
this.schedulePreviewUpdate()
|
||||
}
|
||||
private setWindowSurfaceVisible(id: string, visible: boolean): void {
|
||||
void this.display?.setSurfaceVisible(id, visible).then(() => this.schedulePreviewUpdate()).catch(error => this.fail(error))
|
||||
}
|
||||
private removeWindowSurface(id: string): void {
|
||||
void this.display?.removeSurface(id).then(() => this.schedulePreviewUpdate()).catch(error => this.fail(error))
|
||||
}
|
||||
/** Re-render and resubmit the shell surface; one render in flight, at most one queued. */
|
||||
private requestShellRender(): void {
|
||||
if (this.shellRenderInProgress) { this.shellRenderQueued = true; return }
|
||||
this.shellRenderInProgress = true
|
||||
void (async () => {
|
||||
try {
|
||||
if (this.shellDirty) {
|
||||
this.shellDirty = false
|
||||
const { image, draws } = flattenPlanesWithDraws(shell.paintSurface(), { width: 640, height: 480 })
|
||||
this.compositor.submitSurfaceFrame('shell', image.pixels, { x: 0, y: 0, width: 640, height: 480 }, prepareFrameDraws(draws))
|
||||
this.compositor.setUnderlayDim(1, shell.underlayDim())
|
||||
}
|
||||
const { pixels, textures } = this.compositor.compositeFrame()
|
||||
this.session?.setFrame(pixels, textures)
|
||||
if (this.active) {
|
||||
const image = previewPixels(pixels, 640, 480, previewColorSetting.get() === 'green')
|
||||
this.onFrame(image, this.glassesLocked ? 'Glasses locked' : this.glance.isVisible() ? 'Glanceboard'
|
||||
: `${shell.foregroundWindow()?.title ?? 'Apps'} · ${shell.getFocus() === 'sidebar' ? 'App switcher' : 'App'}`)
|
||||
}
|
||||
do {
|
||||
this.shellRenderQueued = false
|
||||
await this.renderShell()
|
||||
} while (this.shellRenderQueued)
|
||||
} catch (error) { this.fail(error) }
|
||||
finally { this.shellRenderInProgress = false }
|
||||
})()
|
||||
}
|
||||
private async renderShell(): Promise<void> {
|
||||
await this.displayReady
|
||||
const display = this.display
|
||||
if (!display) return
|
||||
const startedAt = Date.now()
|
||||
const bytes = shell.paintScene()
|
||||
const frameId = this.allocateFrameId()
|
||||
await display.submitShellScene(bytes, Date.now() - startedAt, frameId)
|
||||
// Backpressure: the next shell render waits for this one to reach the
|
||||
// glasses (the preview target resolves immediately; nothing transmits).
|
||||
await display.waitForFrameFinished(frameId, FRAME_TRANSMIT_BACKPRESSURE_TIMEOUT_MS)
|
||||
this.schedulePreviewUpdate()
|
||||
}
|
||||
private allocateFrameId(): number {
|
||||
const id = this.nextFrameId
|
||||
this.nextFrameId = this.nextFrameId >= 0x7fffffff ? 1 : this.nextFrameId + 1
|
||||
return id
|
||||
}
|
||||
/** Refresh the phone mirror from the composited screen, coalesced at 30 fps. */
|
||||
private schedulePreviewUpdate(): void {
|
||||
if (!this.active || this.previewTimer !== null) return
|
||||
this.previewTimer = setTimeout(() => {
|
||||
this.previewTimer = null
|
||||
if (!this.active) return
|
||||
try {
|
||||
const image = this.display?.getCompositePreview(previewColorSetting.get() === 'green')
|
||||
if (!image) return
|
||||
this.onFrame(image, this.glassesLocked ? 'Glasses locked' : this.glance.isVisible() ? 'Glanceboard'
|
||||
: `${shell.foregroundWindow()?.title ?? 'Apps'} · ${shell.getFocus() === 'sidebar' ? 'App switcher' : 'App'}`)
|
||||
} catch (error) { this.fail(error) }
|
||||
}, 33)
|
||||
}
|
||||
@@ -471,7 +582,7 @@ export class IosPreviewController {
|
||||
launchInProcessApp: (id, surface, create) => this.launchInProcessApp(id, surface, create),
|
||||
ensureWorkerHost: create => this.ensureWorkerHost(app.appId, create),
|
||||
submitWindowFrame: (id, planes) => this.submit(id, planes),
|
||||
setWindowSurfaceVisible: (id, visible) => { this.compositor.setSurfaceVisible(id, visible); this.scheduleFrame() },
|
||||
setWindowSurfaceVisible: (id, visible) => this.setWindowSurfaceVisible(id, visible),
|
||||
requestShellRender: () => this.requestShellRender(), appendLog: message => console.log(`[ios-app] ${message}`),
|
||||
setTextEditorHost: () => {},
|
||||
}
|
||||
@@ -486,13 +597,17 @@ export class IosPreviewController {
|
||||
let surfaceReady = false
|
||||
const app = create({
|
||||
actions: this.actions, submitFrame: planes => surfaceReady ? this.submit(surfaceId, planes) : Promise.resolve(),
|
||||
setSurfaceVisible: visible => { this.compositor.setSurfaceVisible(surfaceId, visible); this.scheduleFrame() },
|
||||
removeSurface: () => { surfaceReady = false; this.compositor.removeSurface(surfaceId); this.scheduleFrame() },
|
||||
reconfigureSurface: () => { const window = shell.getWindows().find(w => w.windowId === windowId); if (window) this.configureWindow(window); this.requestShellRender() },
|
||||
setSurfaceVisible: visible => this.setWindowSurfaceVisible(surfaceId, visible),
|
||||
removeSurface: () => { surfaceReady = false; this.removeWindowSurface(surfaceId) },
|
||||
reconfigureSurface: () => {
|
||||
const window = shell.getWindows().find(w => w.windowId === windowId)
|
||||
if (window) void this.configureWindow(window).catch(error => this.fail(error))
|
||||
this.requestShellRender()
|
||||
},
|
||||
onClosed: () => { this.inProcessApps.delete(windowId) },
|
||||
})
|
||||
this.inProcessApps.set(windowId, app)
|
||||
this.configureWindow(app.window)
|
||||
await this.configureWindow(app.window)
|
||||
surfaceReady = true
|
||||
shell.registerWindow(app.window); shell.focusWindow(windowId)
|
||||
app.requestRender(); this.requestShellRender()
|
||||
@@ -505,16 +620,19 @@ export class IosPreviewController {
|
||||
? new IosNavigationSensors(event => worker.postMessage({ type: 'navigation-sensors', event })) : undefined
|
||||
const host = new WorkerAppHost({
|
||||
appId, worker, navigationSensors,
|
||||
onStopping: () => { if (this.appHosts.get(appId) === host) this.appHosts.delete(appId) },
|
||||
playBuzzerSequence: payload => this.actions.playBuzzerSequence(payload),
|
||||
openUrl: url => { void Utils.openUrl(url) },
|
||||
configureSurface: async (id, visible, heightMode) => {
|
||||
this.compositor.configureSurface(id, { ...appViewportRect(heightMode, appId), zOrder: 0, transparency: 'opaque' })
|
||||
this.compositor.setSurfaceVisible(id, visible)
|
||||
const display = this.display
|
||||
if (!display) return
|
||||
await display.configureSurface(id, { ...appViewportRect(heightMode, appId), zOrder: 0, transparency: 'opaque' })
|
||||
await display.setSurfaceVisible(id, visible)
|
||||
},
|
||||
setSurfaceVisible: (id, visible) => { this.compositor.setSurfaceVisible(id, visible); this.scheduleFrame() },
|
||||
removeSurface: id => { this.compositor.removeSurface(id); this.scheduleFrame() },
|
||||
setSurfaceVisible: (id, visible) => this.setWindowSurfaceVisible(id, visible),
|
||||
removeSurface: id => this.removeWindowSurface(id),
|
||||
submitPixels: (id, pixels, width, height, draws) => {
|
||||
this.compositor.submitSurfaceFrame(id, pixels, { x: 0, y: 0, width, height }, draws); this.scheduleFrame()
|
||||
void this.submitWorkerPixels(id, pixels, width, height, draws).catch(error => this.fail(error))
|
||||
},
|
||||
requestShellRender: () => this.requestShellRender(),
|
||||
openSettings: section => { void this.launchApp('settings', { section }) },
|
||||
@@ -530,45 +648,194 @@ export class IosPreviewController {
|
||||
displayModeSetting.set(values[(values.indexOf(displayModeSetting.get()) + 1) % values.length])
|
||||
}
|
||||
get displayModeLabel(): string { return displayModeLabel(displayModeSetting.get()) }
|
||||
get connectionState(): SessionState | null { return this.session?.state ?? null }
|
||||
get connectionState(): SessionState | null { return this.sessionCreated ? this.state : null }
|
||||
get connectionDetails(): string {
|
||||
const state = this.connectionState
|
||||
return state ? `${state.status}\nL: ${state.leftVersion || 'unknown'}\nR: ${state.rightVersion || 'unknown'}\nBattery: ${state.battery ?? '?'}%\nRing: ${state.ring ? 'connected' : 'disconnected'}\nFrames acknowledged: ${state.frames}\n${state.capabilities}\n\n${this.logLines.slice(-12).join('\n')}` : 'Preview only. Configure devices, then connect.'
|
||||
}
|
||||
private update(phase: SessionPhase, status: string): void {
|
||||
this.state = { ...this.state, phase, status }
|
||||
this.logBluetooth(status)
|
||||
this.emitState()
|
||||
}
|
||||
private emitState(): void {
|
||||
if (this.state.phase !== 'connected') resetRingInputFilter()
|
||||
this.pushBrightness()
|
||||
this.maybePlayWelcomeSound(this.state)
|
||||
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.syncRuntime()
|
||||
if (this.active) this.onConnectionState({ ...this.state })
|
||||
this.requestShellRender()
|
||||
}
|
||||
async connect(): Promise<void> {
|
||||
if (!this.active) return
|
||||
if (!this.active || this.connecting || this.stopping) return
|
||||
if (this.communicator && ['connecting', 'connected', 'retrying', 'disconnecting'].includes(this.state.phase)) return
|
||||
const addresses = loadDeviceAddresses(), error = deviceAddressError(addresses)
|
||||
if (error) { this.onError(error); return }
|
||||
if (!this.session) {
|
||||
this.session = new GlassesSession(iosBluetooth(), state => {
|
||||
if (state.phase !== "connected") resetRingInputFilter()
|
||||
this.pushBrightness()
|
||||
this.maybePlayWelcomeSound(state)
|
||||
if (state.phase !== 'connected' && state.phase !== 'connecting') this.glassesWorn = null
|
||||
if (state.phase !== 'connected') iosVoiceInput.handleSessionEnded()
|
||||
shell.setBatteryLevels({ headset: state.battery, headsetCharging: state.charging })
|
||||
this.syncRuntime()
|
||||
if (this.active) this.onConnectionState(state)
|
||||
this.requestShellRender()
|
||||
}, input => {
|
||||
this.inputQueue = this.inputQueue.then(async () => {
|
||||
if (this.session?.state.phase !== 'connected') return
|
||||
await this.receiveInput(rawInputEventToInputEvent(input),
|
||||
input.kind === 'display-wake' && input.eventType === OsEventTypeList.HEAD_UP_EVENT)
|
||||
this.logBluetooth(`Input ${input.eventType} source ${input.eventSource}`)
|
||||
}).catch(error => this.fail(error))
|
||||
}, message => this.logBluetooth(message), () => {
|
||||
if (!UIApplication.sharedApplication.protectedDataAvailable) this.handlePhoneLockState(true)
|
||||
this.refreshClock()
|
||||
}, receiveCompassEvent, wearing => this.handleWearState(wearing), (key, popup) => {
|
||||
iosNotificationsChanged(key, popup); this.requestShellRender()
|
||||
}, new IosTexturePlanner())
|
||||
bindIosNotifications(this.session.notifications)
|
||||
bindCompassSession(this.session)
|
||||
this.connecting = true
|
||||
this.sessionCreated = true
|
||||
try {
|
||||
this.state = { ...this.state, capabilities: '', leftVersion: '', rightVersion: '', battery: null, charging: null, ring: false }
|
||||
this.update('connecting', 'Finding configured devices…')
|
||||
let identifiers
|
||||
try { identifiers = await resolveIosPeripherals(addresses) }
|
||||
catch (failure) {
|
||||
this.update('error', failure instanceof Error ? failure.message : String(failure))
|
||||
return
|
||||
}
|
||||
if (this.communicator) await this.closeCommunicator()
|
||||
const communicator = new FaceclawCommunicatorBridge(identifiers)
|
||||
this.teardownPreviewDisplay()
|
||||
this.communicator = communicator
|
||||
this.wearDetectionRequested = false
|
||||
this.bindCommunicator(communicator)
|
||||
this.displayReady = this.registerSurfaces(communicator)
|
||||
await this.displayReady
|
||||
this.update('connecting', 'Connecting to the glasses…')
|
||||
await communicator.configureBrightness(getBrightnessPreferences())
|
||||
this.startedCommunicators.add(communicator)
|
||||
await communicator.start()
|
||||
for (const window of shell.getWindows()) window.requestRender()
|
||||
this.requestShellRender()
|
||||
} catch (failure) {
|
||||
this.fail(failure)
|
||||
await this.closeCommunicator().catch(() => {})
|
||||
this.ensurePreviewDisplay()
|
||||
this.update('error', failure instanceof Error ? failure.message : String(failure))
|
||||
} finally {
|
||||
this.connecting = false
|
||||
}
|
||||
await this.session.start(addresses)
|
||||
}
|
||||
async disconnect(): Promise<void> { await this.session?.stop() }
|
||||
private bindCommunicator(communicator: FaceclawCommunicatorBridge): void {
|
||||
const notifications = new AncsClient(packet => communicator.writeRawToRight(packet), (key, popup) => {
|
||||
iosNotificationsChanged(key, popup); this.requestShellRender()
|
||||
}, message => this.logBluetooth('ANCS: ' + message))
|
||||
this.notifications = notifications
|
||||
bindIosNotifications(notifications)
|
||||
bindCompassSession(communicator)
|
||||
communicator.setRequiresAncs(true)
|
||||
this.sessionOffs.push(
|
||||
communicator.onStateChange(state => this.handleSessionState(communicator, state)),
|
||||
communicator.onRingEvent(input => this.handleRingEvent(input)),
|
||||
communicator.onBatteryState(state => {
|
||||
if (this.communicator !== communicator) return
|
||||
this.state = { ...this.state, battery: state.battery >= 0 && state.battery <= 100 ? state.battery : this.state.battery,
|
||||
charging: state.chargingStatus >= 0 ? state.chargingStatus > 0 : this.state.charging,
|
||||
ring: (state.ringBattery ?? -1) >= 0 || this.state.ring }
|
||||
shell.setBatteryLevels({ headset: this.state.battery, headsetCharging: this.state.charging })
|
||||
if (this.active) this.onConnectionState({ ...this.state })
|
||||
this.requestShellRender()
|
||||
}),
|
||||
communicator.onFirmwareInfo(info => {
|
||||
if (this.communicator !== communicator) return
|
||||
this.state = { ...this.state, leftVersion: info.leftVersion || this.state.leftVersion,
|
||||
rightVersion: info.rightVersion || this.state.rightVersion, capabilities: info.extension || this.state.capabilities }
|
||||
if (this.active) this.onConnectionState({ ...this.state })
|
||||
this.syncNotifications(communicator)
|
||||
}),
|
||||
communicator.onFrameMetrics(() => {
|
||||
if (this.communicator !== communicator) return
|
||||
this.state = { ...this.state, frames: this.state.frames + 1 }
|
||||
this.onActivity()
|
||||
this.maybePlayWelcomeSound(this.state)
|
||||
if (this.active) this.onConnectionState({ ...this.state })
|
||||
this.schedulePreviewUpdate()
|
||||
}),
|
||||
communicator.onWearState(wearing => { if (this.communicator === communicator) this.handleWearState(wearing) }),
|
||||
communicator.addCompassListener(receiveCompassEvent),
|
||||
communicator.onAncsRelayFrame(frame => { if (this.communicator === communicator) notifications.receive(frame) }),
|
||||
communicator.onAncsAuthorization(authorized => {
|
||||
if (this.communicator !== communicator) return
|
||||
if (authorized) { this.syncNotifications(communicator); return }
|
||||
const active = notifications.state !== 'disconnected'
|
||||
const command = notifications.stopCommand()
|
||||
notifications.stop('Enable Share System Notifications in iPhone Settings → Bluetooth → right lens.')
|
||||
if (active && this.state.phase === 'connected') void communicator.writeRawToRight(command).catch(() => {})
|
||||
}),
|
||||
)
|
||||
}
|
||||
private handleSessionState(communicator: FaceclawCommunicatorBridge, state: CommunicatorState): void {
|
||||
if (this.communicator !== communicator) return
|
||||
// Registering the listener makes the Kotlin session report its current state, which is
|
||||
// "disconnected" before start(); that must not be mistaken for the session ending.
|
||||
if (!this.startedCommunicators.has(communicator)) return
|
||||
const phase: SessionPhase = state.phase === 'charging' ? 'connected' : state.phase === 'unpaired' ? 'error' : state.phase
|
||||
if (phase === 'connected' && this.state.phase !== 'connected') {
|
||||
this.state = { ...this.state, frames: this.state.frames }
|
||||
if (!this.wearDetectionRequested) {
|
||||
this.wearDetectionRequested = true
|
||||
void communicator.enableWearDetectionAndRequestState().catch(error => this.logBluetooth(`Wear detection: ${String(error)}`))
|
||||
}
|
||||
}
|
||||
this.update(phase, state.status)
|
||||
if (phase === 'connected') this.syncNotifications(communicator)
|
||||
else if ((phase === 'disconnected' || phase === 'error') && !this.stopping) {
|
||||
// The session ended on its own (retries exhausted, an arm unpaired): release it and go headless.
|
||||
void this.disconnect(state.status)
|
||||
}
|
||||
}
|
||||
private handleRingEvent(input: RawInputEvent): void {
|
||||
this.inputQueue = this.inputQueue.then(async () => {
|
||||
if (this.state.phase !== 'connected') return
|
||||
this.onActivity()
|
||||
if (input.ringInput) this.state = { ...this.state, ring: true }
|
||||
await this.receiveInput(rawInputEventToInputEvent(input),
|
||||
input.kind === 'display-wake' && input.eventType === OsEventTypeList.HEAD_UP_EVENT)
|
||||
this.logBluetooth(`Input ${input.eventType} source ${input.eventSource}`)
|
||||
}).catch(error => this.fail(error))
|
||||
}
|
||||
/** Any glasses traffic: CoreBluetooth can wake a suspended process, so refresh what timers would have. */
|
||||
private onActivity(): void {
|
||||
if (!UIApplication.sharedApplication.protectedDataAvailable) this.handlePhoneLockState(true)
|
||||
this.refreshClock()
|
||||
}
|
||||
/** Start iPhone notification relay once the firmware is known to support it and ANCS is authorized. */
|
||||
private syncNotifications(communicator: FaceclawCommunicatorBridge): void {
|
||||
const notifications = this.notifications
|
||||
if (!notifications || this.state.phase !== 'connected') return
|
||||
if (!ancsCapable(this.state.capabilities)) {
|
||||
if (this.state.capabilities) notifications.stop(`Update glasses to Faceclaw firmware ${ANCS_FIRMWARE_VERSION} or newer for iPhone notifications.`)
|
||||
return
|
||||
}
|
||||
if (notifications.state !== 'disconnected') return
|
||||
notifications.start((Date.now() ^ Math.floor(Math.random() * 0xffffffff)) >>> 0, communicator.rightWriteLimit())
|
||||
}
|
||||
async disconnect(reason?: string): Promise<void> {
|
||||
if (this.stopping) return this.stopping
|
||||
const communicator = this.communicator
|
||||
if (!communicator) return
|
||||
// Marked before any bridge call: the session may report "disconnected" from inside disconnect().
|
||||
let finished!: () => void
|
||||
this.stopping = new Promise<void>(resolve => { finished = resolve })
|
||||
try {
|
||||
const notifications = this.notifications
|
||||
if (notifications && notifications.state !== 'disconnected') {
|
||||
const command = notifications.stopCommand()
|
||||
notifications.stop()
|
||||
if (this.state.phase === 'connected') { try { await communicator.writeRawToRight(command) } catch {} }
|
||||
}
|
||||
if (this.state.phase === 'connected' || this.state.phase === 'connecting' || this.state.phase === 'retrying')
|
||||
this.update('disconnecting', 'Disconnecting…')
|
||||
await communicator.disconnect().catch(error => this.logBluetooth(`Disconnect: ${String(error)}`))
|
||||
} finally {
|
||||
await this.closeCommunicator()
|
||||
this.ensurePreviewDisplay()
|
||||
this.update(reason ? 'error' : 'disconnected', reason ?? 'Preview only')
|
||||
this.stopping = null
|
||||
finished()
|
||||
}
|
||||
}
|
||||
private async closeCommunicator(): Promise<void> {
|
||||
const communicator = this.communicator
|
||||
if (!communicator) return
|
||||
for (const off of this.sessionOffs.splice(0)) off()
|
||||
this.notifications = null
|
||||
this.communicator = null
|
||||
this.glance.reset()
|
||||
await communicator.close().catch(error => this.logBluetooth(`Close: ${String(error)}`))
|
||||
}
|
||||
private maybePlayWelcomeSound(state: SessionState): void {
|
||||
const firstNewFrame = state.frames > this.acknowledgedFrames
|
||||
this.acknowledgedFrames = state.frames
|
||||
@@ -583,17 +850,18 @@ export class IosPreviewController {
|
||||
startVoiceInput(): void { if (!this.glassesLocked) shell.startVoiceInput() }
|
||||
private async prepareVoiceCapture(): Promise<boolean> {
|
||||
if (this.glassesLocked) return false
|
||||
const usePhoneMic = this.session?.state.phase !== 'connected'
|
||||
const usePhoneMic = this.state.phase !== 'connected'
|
||||
if (usePhoneMic && !this.active) return false
|
||||
const ready = await iosVoiceInput.prepare(this.active, usePhoneMic)
|
||||
if (!ready && this.active) this.onError(iosVoiceInput.statusText)
|
||||
return ready && !this.glassesLocked && (this.session?.state.phase === 'connected' || this.active)
|
||||
return ready && !this.glassesLocked && (this.state.phase === 'connected' || this.active)
|
||||
}
|
||||
private async startVoiceCapture(endpointing = false): Promise<void> {
|
||||
if (this.glassesLocked) return
|
||||
const log = (message: string) => this.logBluetooth(message)
|
||||
if (this.session?.state.phase === 'connected') {
|
||||
await iosVoiceInput.startGlassesCapture(this.session, log, endpointing)
|
||||
const communicator = this.communicator
|
||||
if (communicator && this.state.phase === 'connected') {
|
||||
await iosVoiceInput.startGlassesCapture(communicator, log, endpointing)
|
||||
} else if (this.active) {
|
||||
// Recheck microphone permission if the glasses disconnected after prepare.
|
||||
if (!await iosVoiceInput.prepare(this.active, true) || !this.active || this.glassesLocked) return
|
||||
@@ -620,13 +888,21 @@ export class IosPreviewController {
|
||||
if (this.glassesLocked || !this.active) return
|
||||
shell.startKeyboardInput()
|
||||
}
|
||||
private async editSetting(setting: { editorTitle: string; inputKind?: string; get(): string; set(value: string): void }): Promise<boolean> {
|
||||
private async editSetting(setting: { editorTitle: string; inputKind?: string; get(): string; set(value: string): void; validationError?(value?: string): string | null }): Promise<boolean> {
|
||||
if (!this.active) { this.logBluetooth('Editing text settings requires opening Faceclaw on the phone'); return false }
|
||||
const result = await Dialogs.prompt({ title: setting.editorTitle, defaultText: setting.get(), inputType: setting.inputKind, okButtonText: 'Save', cancelButtonText: 'Cancel' })
|
||||
if (result.result) {
|
||||
setting.set(result.text)
|
||||
let draft = setting.get()
|
||||
while (true) {
|
||||
const result = await Dialogs.prompt({ title: setting.editorTitle, defaultText: draft, inputType: setting.inputKind, okButtonText: 'Save', cancelButtonText: 'Cancel' })
|
||||
if (!result.result) return false
|
||||
draft = result.text
|
||||
const error = setting.validationError?.(draft)
|
||||
if (error) {
|
||||
await Dialogs.alert({ title: setting.editorTitle, message: error, okButtonText: 'OK' })
|
||||
continue
|
||||
}
|
||||
setting.set(draft)
|
||||
if (setting === nightscoutSiteUrlSetting || setting === nightscoutApiTokenSetting) await nightscoutBridge.refreshNow()
|
||||
return true
|
||||
}
|
||||
return result.result
|
||||
}
|
||||
}
|
||||
@@ -1,152 +0,0 @@
|
||||
import * as p from './ble-protocol'
|
||||
import type { SessionTransport, SessionAddresses, TransportEvent } from './glasses-session'
|
||||
import { hexToBytes } from '../util/hex-util'
|
||||
|
||||
declare function setTimeout(callback: () => void, ms: number): number
|
||||
declare function clearTimeout(id: number): void
|
||||
|
||||
export const OTA_WRITE = '00002760-08c2-11e1-9073-0e8ac72e0001'
|
||||
export const OTA_NOTIFY = '00002760-08c2-11e1-9073-0e8ac72e0002'
|
||||
export class StockTimeout extends Error {}
|
||||
type Waiter = { role: string; characteristic: string; match: (message: p.ProtocolMessage) => boolean;
|
||||
resolve: (message: p.ProtocolMessage) => void; reject: (error: Error) => void; timer: ReturnType<typeof setTimeout> | null }
|
||||
|
||||
/** Exclusive, short-lived stock-protocol connection for onboarding/OTA. No display pump. */
|
||||
export class StockConnection {
|
||||
ids: Record<string, string> = {}
|
||||
private limits: Record<string, number> = {}
|
||||
private receiver = new p.MessageReceiver()
|
||||
private waiters = new Set<Waiter>()
|
||||
private listeners = new Set<(role: string, message: p.ProtocolMessage) => void>()
|
||||
private lost = new Set<string>()
|
||||
private writes = new Map<string, Promise<void>>()
|
||||
private closed = false
|
||||
private magic = 0x60
|
||||
private sequence = 0
|
||||
private off: () => void
|
||||
constructor(private transport: SessionTransport, private log: (line: string) => void = () => {}) {
|
||||
this.off = transport.onEvent(event => this.receive(event))
|
||||
}
|
||||
check(): void { if (this.closed) throw new Error('Cancelled.') }
|
||||
isConnected(role: string): boolean { return !this.closed && !!this.limits[role] && !this.lost.has(role) }
|
||||
async resolve(addresses: SessionAddresses): Promise<void> {
|
||||
this.ids = await this.transport.resolveDevices(addresses); this.check()
|
||||
}
|
||||
async connect(role: string, ota = false): Promise<void> {
|
||||
this.check(); this.lost.delete(role)
|
||||
const id = this.ids[role]
|
||||
if (!id) throw new Error(`No ${role} lens configured.`)
|
||||
const details = await this.transport.connect(id)
|
||||
if (this.closed) { this.transport.disconnect(id); this.check() }
|
||||
this.limits[role] = details.maxWrite
|
||||
for (const characteristic of [p.G2_WRITE, p.G2_NOTIFY, ...(ota ? [OTA_WRITE, OTA_NOTIFY] : [])])
|
||||
if (!details.characteristics.includes(characteristic)) throw new Error(`${role} lens is missing ${ota ? 'firmware update' : 'G2'} characteristics.`)
|
||||
await this.transport.subscribe(id, p.G2_NOTIFY); this.check()
|
||||
if (ota) { await this.transport.subscribe(id, OTA_NOTIFY); this.check() }
|
||||
}
|
||||
nextMagic(): number { this.magic = this.magic >= 0x7f ? 0x60 : this.magic + 1; return this.magic }
|
||||
async authenticate(role: string): Promise<void> {
|
||||
for (let attempt = 0; attempt < 2; attempt++) {
|
||||
const magic = this.nextMagic()
|
||||
try {
|
||||
await this.exchange(role, p.SID.auth, p.authentication(magic),
|
||||
message => p.authenticationSucceeded(message, magic), 30_000, p.G2_WRITE, p.G2_NOTIFY, 0)
|
||||
this.log(`Security authentication complete: ${role} lens`); return
|
||||
} catch (error) { this.check(); if (!(error instanceof StockTimeout) || attempt === 1) throw error }
|
||||
}
|
||||
}
|
||||
request(role: string, sid: number, build: (magic: number) => Uint8Array, timeout = 3500, fixedMagic?: number): Promise<p.ProtocolMessage> {
|
||||
const magic = fixedMagic ?? this.nextMagic()
|
||||
return this.exchange(role, sid, build(magic), message => message.sid === sid && message.magic === magic && ![1, 6].includes(message.flag), timeout)
|
||||
}
|
||||
write(role: string, sid: number, payload: Uint8Array, characteristic = p.G2_WRITE, flag = 0x20, sequence = ++this.sequence): Promise<void> {
|
||||
// Keep a fragmented control message together when the prompt heartbeat
|
||||
// overlaps a settings request or shutdown.
|
||||
const pending = (this.writes.get(role) ?? Promise.resolve()).catch(() => {}).then(async () => {
|
||||
this.check()
|
||||
for (const frame of p.frameMessage(payload, sid, flag, sequence, this.limits[role])) {
|
||||
this.check()
|
||||
if (this.lost.has(role)) throw new Error(`Lost connection to the ${role} lens.`)
|
||||
await this.transport.write(this.ids[role], characteristic, frame)
|
||||
}
|
||||
this.check()
|
||||
if (this.lost.has(role)) throw new Error(`Lost connection to the ${role} lens.`)
|
||||
})
|
||||
this.writes.set(role, pending)
|
||||
return pending
|
||||
}
|
||||
exchange(role: string, sid: number, payload: Uint8Array, match: Waiter['match'], timeout: number,
|
||||
write = p.G2_WRITE, notify = p.G2_NOTIFY, flag = 0x20, sequence?: number, data?: Uint8Array): Promise<p.ProtocolMessage> {
|
||||
this.check()
|
||||
return new Promise((resolve, reject) => {
|
||||
let written = false, reply: p.ProtocolMessage | null = null
|
||||
const waiter: Waiter = { role, characteristic: notify, match,
|
||||
resolve: message => { reply = message; if (written) resolve(message) }, reject,
|
||||
timer: null }
|
||||
this.waiters.add(waiter)
|
||||
void (async () => {
|
||||
await this.write(role, sid, payload, write, flag, sequence)
|
||||
if (data) await this.write(role, 0xc1, data, write, flag, sequence)
|
||||
this.check()
|
||||
if (!reply && !this.waiters.has(waiter)) return
|
||||
written = true
|
||||
if (reply) resolve(reply)
|
||||
else waiter.timer = setTimeout(() => {
|
||||
this.waiters.delete(waiter)
|
||||
reject(new StockTimeout(`No response from the ${role} lens (0x${sid.toString(16)}).`))
|
||||
}, timeout)
|
||||
})().catch(error => { this.waiters.delete(waiter); if (waiter.timer !== null) clearTimeout(waiter.timer); reject(error) })
|
||||
})
|
||||
}
|
||||
|
||||
onMessage(listener: (role: string, message: p.ProtocolMessage) => void): () => void {
|
||||
this.listeners.add(listener); return () => this.listeners.delete(listener)
|
||||
}
|
||||
private receive(event: TransportEvent): void {
|
||||
if (this.closed) return
|
||||
const role = Object.keys(this.ids).find(role => this.ids[role] === event.identifier)
|
||||
if (!role) return
|
||||
if (event.kind === 'disconnected') {
|
||||
this.lost.add(role)
|
||||
for (const waiter of [...this.waiters]) if (waiter.role === role) {
|
||||
this.waiters.delete(waiter); if (waiter.timer !== null) clearTimeout(waiter.timer); waiter.reject(new Error(`Lost connection to the ${role} lens.`))
|
||||
}
|
||||
return
|
||||
}
|
||||
if (event.kind !== 'notification' || !event.data || ![p.G2_NOTIFY, OTA_NOTIFY].includes(event.characteristic!)) return
|
||||
try {
|
||||
const data = hexToBytes(event.data)
|
||||
const messages = event.characteristic === OTA_NOTIFY ? decodeOtaAcks(data) : this.receiver.receive(event.identifier!, data)
|
||||
for (const message of messages) {
|
||||
for (const waiter of [...this.waiters]) if (waiter.role === role && waiter.characteristic === event.characteristic && waiter.match(message)) {
|
||||
this.waiters.delete(waiter); if (waiter.timer !== null) clearTimeout(waiter.timer); waiter.resolve(message)
|
||||
}
|
||||
if (event.characteristic === p.G2_NOTIFY) for (const listener of this.listeners) listener(role, message)
|
||||
}
|
||||
} catch (error) { this.log(`Ignored malformed onboarding notification: ${error}`) }
|
||||
}
|
||||
disconnect(role: string): void { this.lost.add(role); if (this.ids[role]) this.transport.disconnect(this.ids[role]); this.receiver.clear() }
|
||||
close(): void {
|
||||
if (this.closed) return
|
||||
this.closed = true; this.off(); this.transport.stopScan()
|
||||
for (const waiter of this.waiters) { if (waiter.timer !== null) clearTimeout(waiter.timer); waiter.reject(new Error('Cancelled.')) }
|
||||
this.waiters.clear(); this.listeners.clear()
|
||||
for (const role of Object.keys(this.ids)) this.disconnect(role)
|
||||
}
|
||||
}
|
||||
|
||||
/** OTA ACKs are raw opcode/status bytes, not protobuf; validate the aa21 CRC envelope. */
|
||||
export function decodeOtaAcks(data: Uint8Array): p.ProtocolMessage[] {
|
||||
const messages: p.ProtocolMessage[] = []
|
||||
for (let offset = 0; offset + 8 <= data.length;) {
|
||||
const n = data[offset + 3], end = offset + 8 + n
|
||||
if (end > data.length) break
|
||||
if (data[offset] === 0xaa && [0x12, 0x21].includes(data[offset + 1]) && data[offset + 4] === 1 && data[offset + 5] === 1 && n >= 4) {
|
||||
const payload = data.slice(offset + 8, end - 2)
|
||||
if (p.crc16(payload) === (data[end - 2] | data[end - 1] << 8))
|
||||
messages.push({ sid: data[offset + 6], flag: data[offset + 7], payload, command: payload[0], magic: -1 })
|
||||
}
|
||||
offset = end
|
||||
}
|
||||
return messages
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
/** Platform-owned snapshot of the same composite as the submitted pixels. */
|
||||
export type TextureFrame = { readonly native: object }
|
||||
export type TexturePlan = { uploads: Uint8Array[]; payload: Uint8Array; nextFid: number; usedBytes: number }
|
||||
/** Sessions own residency; frame snapshots may be coalesced without changing it. */
|
||||
export interface SessionTexturePlanner {
|
||||
plan(previous: Uint8Array | null, next: Uint8Array, frame: TextureFrame, firstId: number): TexturePlan | null
|
||||
reset(): void
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
import { bytes, concat, integer, readBytes, readInteger, type ProtocolMessage } from './ble-protocol'
|
||||
|
||||
// Matches BleProtocol.buildSetWearDetection/buildFaceclawWakeControl on Android.
|
||||
export const enableWearDetection = (magic: number): Uint8Array => concat(
|
||||
integer(1, 1), integer(2, magic), bytes(3, bytes(5, integer(1, 1))))
|
||||
export const queryWearState = (): Uint8Array => concat(
|
||||
integer(1, 1), integer(2, 0), bytes(101, new Uint8Array([70, 67, 1, 7, 0, 0])))
|
||||
|
||||
export function decodeWearState(message: ProtocolMessage): boolean | null {
|
||||
if (message.sid !== 0x10 || message.command !== 3) return null
|
||||
const event = readBytes(message.payload, 5)
|
||||
if (!event || readInteger(event, 1, -1) !== 1) return null
|
||||
const state = readInteger(event, 2, -1)
|
||||
return state === 0 ? false : state === 1 ? true : null
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
# TypeScript display lists
|
||||
|
||||
`GrayImage.drawDisplayList(list, x, y, width, height, depth)` retains a list alongside
|
||||
the image's ordinary pixels. Existing frame submission (`prepareFrameDraws`) and
|
||||
shell scene submission carry it to Kotlin. Bounds describe its footprint for plane
|
||||
occlusion; draw coordinates are relative to its placement. Keep the list, its image
|
||||
resources, and their pixel buffers immutable after submission.
|
||||
|
||||
```ts
|
||||
import { DrawExpression as E } from './draw-expression';
|
||||
import { DrawOp, type DisplayList } from './display-list';
|
||||
|
||||
const list: DisplayList = {
|
||||
resources: [],
|
||||
timeline: { token: animationId, startedAt: Date.now() },
|
||||
calls: [{
|
||||
op: DrawOp.ROUNDED_RECT,
|
||||
x: E.progress(300).ease().lerp(E.f32(0), E.f32(100)).toInt(),
|
||||
y: 0,
|
||||
width: 40, height: 20, radius: 6,
|
||||
background: 4, border: 15,
|
||||
}],
|
||||
};
|
||||
frame.drawDisplayList(list, 20, 30, 140, 20, 0);
|
||||
```
|
||||
|
||||
Supported calls in this first pass:
|
||||
|
||||
- `ROUNDED_RECT`: literal or expression `x`/`y`, literal dimensions/radius, and
|
||||
4-bit colors (0–15). Border 16 means no border. Fill takes the maximum of the
|
||||
background color and existing pixels.
|
||||
- `IMAGE`: a local resource index, integer `x`/`y`, and `transparent`. Transparent
|
||||
images skip pixels quantized to zero. Resources have `width`, `height`, and
|
||||
gray8 `pixels`; a baked `GrayImage` can be used directly.
|
||||
- `RECT_COPY`: a local resource index, source rectangle and destination
|
||||
`dx`/`dy`. Source x/y and `dx`/`dy` accept expressions (revision 29); the
|
||||
source rectangle must stay inside the source at every time, or firmware
|
||||
rejects the list. `SCREEN` reads the uncomposed frame; its source coordinates
|
||||
are also relative to this list's placement. Each call may add its own `depth`
|
||||
to the list's depth. A screen restore normally uses the negative of the list depth.
|
||||
|
||||
Kotlin allocates/cache-pins all image resources, resolves the local indices, applies
|
||||
surface placement, and includes the calls in the root firmware display list. The
|
||||
phone's existing display-list renderer consumes the same compiled calls. This uses
|
||||
the current firmware protocol; no firmware update is needed for this bridge change.
|
||||
|
||||
## Expressions and time
|
||||
|
||||
`E.i32` and `E.f32` construct typed constants. Builders support arithmetic, min/max,
|
||||
integer remainder, negation, explicit conversions, lerp, and the easing functions
|
||||
in `ExprOp`. `ease()` defaults to smoothstep. Integers wrap at 32 bits; float
|
||||
operations round to f32. The firmware's existing stack/error semantics apply.
|
||||
Rounded-rectangle x/y and rect-copy source/destination coordinates accept
|
||||
expressions. `E.progress(duration, delay)` stays 0 for the first `delay` ms of
|
||||
the timeline, so one list can run motions that started at different times.
|
||||
|
||||
`maximum.time()` returns `min(milliseconds since PRESENT, maximum)`. The existing
|
||||
45 ms repaint timer remains active while this is below maximum. `E.elapsed()` is
|
||||
a bridge binding: Kotlin replaces it with the timeline's elapsed milliseconds when
|
||||
building a new root list. It is a constant within that PRESENT, not a second clock.
|
||||
`E.progress(durationMs)` combines the two and clamps to 0..1 so intervening frames
|
||||
and PRESENTs continue an animation instead of restarting it.
|
||||
|
||||
Keep `timeline.token` and `timeline.startedAt` stable for an animation; use a new
|
||||
token when restarting it. JS sends an elapsed duration, so JS and native clocks
|
||||
need not share an epoch. Fingerprints include the token and expression code but
|
||||
exclude the changing elapsed duration. Without a timeline, raw `time()` expressions
|
||||
start at each PRESENT. Native compilation strips the bridge-only elapsed opcode.
|
||||
|
||||
`menu-selection-list.ts` authors both the rounded highlight and the transparent row
|
||||
image using this API. `menu-scroll-list.ts` authors a menu scroll or bounce: a
|
||||
viewport-sized copy from a strip resource whose source y animates, then the
|
||||
highlight. The bounce curve (`app/ui/menu-scroll-motion.ts`) needs no branches:
|
||||
the easing ops clamp their input to 0..1, so `ease(t / p)` holds at 1 after the
|
||||
peak and `ease((t - p) / (1 - p))` holds at 0 before it. Navigation eligibility, duration, and easing remain in
|
||||
TypeScript. There is no animated-menu tag or menu animation policy in Kotlin.
|
||||
|
||||
## Bridge framing
|
||||
|
||||
Tag 7 is followed by a little-endian u32 byte length and a bounded body:
|
||||
placement (i16 x/y, u16 width/height, i8 depth), timeline (u32 token/elapsed),
|
||||
u16 resource count, gray8 images (u16 width/height followed by pixels), u16 call
|
||||
count, then calls. Calls start with the firmware opcode and an i16 relative depth;
|
||||
operands otherwise use their fixed-width fields, except rounded x/y and rect-copy
|
||||
x/y/dx/dy which use extended varints. Expressions use the firmware bytecode plus bridge-only
|
||||
`ELAPSED = 128`. Resource indices are local to this record, never firmware IDs.
|
||||
Rect-copy source x/y are signed here to permit relative SCREEN coordinates.
|
||||
|
||||
Limits: 256 images per record, 64 KiB per packed image, 4 MiB per bridge record,
|
||||
4096 calls, and 900 input bytes per expression. Native validates the bound program
|
||||
against the firmware's 1024-byte limit. The combined scene must also fit the
|
||||
existing resource cache, root-list size, and VM stack limits.
|
||||
@@ -0,0 +1,135 @@
|
||||
import { DrawBytes, encodeValue, evaluate, integer, readValue, type DrawValue } from './draw-expression';
|
||||
|
||||
/** Local resources are assigned firmware cache IDs by Kotlin. SCREEN reads the uncomposed frame. */
|
||||
export const SCREEN = 65535;
|
||||
export const DrawOp = { RECT_COPY: 2, IMAGE: 4, ROUNDED_RECT: 8 } as const;
|
||||
export const DISPLAY_LIST_RECORD = 7;
|
||||
export type ListImage = { readonly width: number; readonly height: number; readonly pixels: Uint8Array };
|
||||
export type ListCall = (
|
||||
| { op: typeof DrawOp.ROUNDED_RECT; x: DrawValue; y: DrawValue; width: number; height: number; radius: number; background: number; border: number }
|
||||
| { op: typeof DrawOp.IMAGE; resource: number; x: number; y: number; transparent: boolean }
|
||||
| { op: typeof DrawOp.RECT_COPY; resource: number; x: DrawValue; y: DrawValue; width: number; height: number; dx: DrawValue; dy: DrawValue }
|
||||
) & { depth?: number };
|
||||
export type DisplayList = {
|
||||
readonly resources: readonly ListImage[];
|
||||
readonly calls: readonly ListCall[];
|
||||
/** Stable identity plus JS clock origin. Native binds ELAPSED again for each PRESENT. */
|
||||
/** Set by the decoder: the local PRESENT origin for software replay. */
|
||||
readonly presentedAt?: number;
|
||||
readonly timeline?: { readonly token: number; readonly startedAt: number };
|
||||
};
|
||||
export type PlacedDisplayList = { displayList: DisplayList; x: number; y: number; width: number; height: number; depth: number };
|
||||
|
||||
class Writer {
|
||||
bytes: number[] = [];
|
||||
u8(n: number): void { this.bytes.push(integer(n, 0, 255)); }
|
||||
i8(n: number): void { this.u8(integer(n, -128, 127) & 255); }
|
||||
u16(n: number): void { integer(n, 0, 65535); this.bytes.push(n & 255, n >>> 8); }
|
||||
i16(n: number): void { this.u16(integer(n, -32768, 32767) & 65535); }
|
||||
u32(n: number): void { integer(n, 0, 4294967295); this.u16(n & 65535); this.u16(n >>> 16); }
|
||||
raw(bytes: ArrayLike<number>): void { for (let i = 0; i < bytes.length; i++) this.bytes.push(bytes[i]); }
|
||||
}
|
||||
|
||||
/** Length-delimited bridge envelope; call operands use fixed widths except extended x/y. */
|
||||
export function encodeDisplayList(placed: PlacedDisplayList, now = Date.now()): Uint8Array {
|
||||
const { displayList: list } = placed, out = new Writer();
|
||||
integer(placed.width, 1, 640); integer(placed.height, 1, 480);
|
||||
out.i16(placed.x); out.i16(placed.y); out.u16(placed.width); out.u16(placed.height); out.i8(placed.depth);
|
||||
out.u32(list.timeline?.token ?? 0);
|
||||
out.u32(list.timeline ? Math.min(2147483647, Math.max(0, Math.trunc(now - list.timeline.startedAt))) : 0);
|
||||
integer(list.resources.length, 0, 256); out.u16(list.resources.length);
|
||||
for (const image of list.resources) {
|
||||
integer(image.width, 1, 640); integer(image.height, 1, 480);
|
||||
if (image.pixels.length !== image.width * image.height || 5 + Math.ceil(image.width / 2) * image.height > 65536) throw new Error('Invalid display-list image');
|
||||
out.u16(image.width); out.u16(image.height); out.raw(image.pixels);
|
||||
}
|
||||
integer(list.calls.length, 0, 4096); out.u16(list.calls.length);
|
||||
for (const call of list.calls) {
|
||||
out.u8(call.op); out.i16(call.depth ?? 0);
|
||||
integer(placed.depth + (call.depth ?? 0), -128, 127);
|
||||
if (call.op === DrawOp.ROUNDED_RECT) {
|
||||
out.raw(encodeValue(call.x)); out.raw(encodeValue(call.y));
|
||||
out.u16(integer(call.width, 1, 640)); out.u16(integer(call.height, 1, 480)); out.u16(call.radius);
|
||||
out.u8(integer(call.background, 0, 15)); out.u8(integer(call.border, 0, 16));
|
||||
} else {
|
||||
if (call.resource !== SCREEN) integer(call.resource, 0, list.resources.length - 1);
|
||||
if (call.op === DrawOp.RECT_COPY && call.resource !== SCREEN && [call.x, call.y].some(v => typeof v === 'number' && v < 0)) throw new Error('Negative image source coordinate');
|
||||
if (call.op === DrawOp.IMAGE && call.resource === SCREEN) throw new Error('Use rectCopy to read SCREEN');
|
||||
out.u16(call.resource);
|
||||
if (call.op === DrawOp.IMAGE) { out.i16(call.x); out.i16(call.y); out.u8(call.transparent ? 31 : 15); }
|
||||
else {
|
||||
out.raw(encodeValue(call.x)); out.raw(encodeValue(call.y)); out.u16(integer(call.width, 1, 640)); out.u16(integer(call.height, 1, 480));
|
||||
out.raw(encodeValue(call.dx)); out.raw(encodeValue(call.dy));
|
||||
}
|
||||
}
|
||||
}
|
||||
if (out.bytes.length > 4 * 1024 * 1024) throw new Error('Display list exceeds bridge limit');
|
||||
const header = new Writer(); header.u8(DISPLAY_LIST_RECORD); header.u32(out.bytes.length); header.raw(out.bytes);
|
||||
return Uint8Array.from(header.bytes);
|
||||
}
|
||||
|
||||
export function readDisplayList(bytes: Uint8Array, offset: number, now = Date.now()): { placed: PlacedDisplayList; end: number } {
|
||||
const envelope = new DrawBytes(bytes.subarray(offset));
|
||||
if (envelope.u8() !== DISPLAY_LIST_RECORD) throw new Error('Invalid display-list record');
|
||||
const length = envelope.u32(); integer(length, 0, 4 * 1024 * 1024);
|
||||
const r = new DrawBytes(envelope.take(length));
|
||||
const x = r.i16(), y = r.i16(), width = r.u16(), height = r.u16(), depth = r.u8() << 24 >> 24;
|
||||
const token = r.u32(), elapsed = r.u32(), count = r.u16(); integer(count, 0, 256);
|
||||
const resources: ListImage[] = [];
|
||||
for (let i = 0; i < count; i++) { const width = r.u16(), height = r.u16(); resources.push({ width, height, pixels: r.take(width * height) }); }
|
||||
const calls: ListCall[] = [], callCount = r.u16(); integer(callCount, 0, 4096);
|
||||
for (let i = 0; i < callCount; i++) {
|
||||
const op = r.u8(), depth = r.i16();
|
||||
if (op === DrawOp.ROUNDED_RECT) calls.push({ op, depth, x: readValue(r), y: readValue(r), width: r.u16(), height: r.u16(), radius: r.u16(), background: r.u8(), border: r.u8() });
|
||||
else if (op === DrawOp.IMAGE) calls.push({ op, depth, resource: r.u16(), x: r.i16(), y: r.i16(), transparent: (r.u8() & 16) !== 0 });
|
||||
else if (op === DrawOp.RECT_COPY) calls.push({ op, depth, resource: r.u16(), x: readValue(r), y: readValue(r), width: r.u16(), height: r.u16(), dx: readValue(r), dy: readValue(r) });
|
||||
else throw new Error('Unsupported display-list call');
|
||||
}
|
||||
if (r.offset !== length) throw new Error('Trailing display-list bytes');
|
||||
const placed = { x, y, width, height, depth, displayList: { resources, calls, presentedAt: now, timeline: { token, startedAt: now - elapsed } } };
|
||||
// The writer is also the shared structural validator for software preview records.
|
||||
encodeDisplayList(placed, now);
|
||||
return { placed, end: offset + envelope.offset };
|
||||
}
|
||||
|
||||
export function dimDisplayList(list: DisplayList, factor: number): DisplayList {
|
||||
const dim = (v: number) => v === 0 ? 0 : Math.max(1, Math.round(v * factor));
|
||||
return { ...list, resources: list.resources.map(r => ({ ...r, pixels: r.pixels.map(dim) })),
|
||||
calls: list.calls.map(c => c.op === DrawOp.ROUNDED_RECT ? { ...c, background: Math.min(15, Math.round(c.background * factor)), border: c.border === 16 ? 16 : Math.min(15, Math.round(c.border * factor)) } : c) };
|
||||
}
|
||||
|
||||
/** Fallback compositor; native preview and glasses replay the compiled list with their existing timers. */
|
||||
export function paintDisplayList(output: Uint8Array, screen: Uint8Array, width: number, height: number, placed: PlacedDisplayList, right = false, now = Date.now()): void {
|
||||
const q = (n: number) => Math.min(15, (n + 8) >> 4) * 16;
|
||||
const list = placed.displayList;
|
||||
const elapsed = list.timeline ? Math.max(0, (list.presentedAt ?? now) - list.timeline.startedAt) : 0;
|
||||
const presentTime = list.presentedAt === undefined ? 0 : Math.max(0, now - list.presentedAt);
|
||||
const inside = (x: number, y: number, w: number, h: number, radius: number) => {
|
||||
if (x < 0 || y < 0 || x >= w || y >= h) return false;
|
||||
const r = Math.min(radius, Math.floor(w / 2), Math.floor(h / 2));
|
||||
const dx = Math.max(0, 2 * r - (2 * Math.min(x, w - 1 - x) + 1)), dy = Math.max(0, 2 * r - (2 * Math.min(y, h - 1 - y) + 1));
|
||||
return dx * dx + dy * dy <= 4 * r * r;
|
||||
};
|
||||
for (const c of placed.displayList.calls) {
|
||||
const depth = placed.depth + (c.depth ?? 0), shift = right ? -Math.floor((depth + 1) / 2) : Math.floor(depth / 2);
|
||||
if (c.op === DrawOp.ROUNDED_RECT) {
|
||||
const x = placed.x + evaluate(c.x, elapsed, presentTime).value + shift, y = placed.y + evaluate(c.y, elapsed, presentTime).value;
|
||||
for (let yy = 0; yy < c.height; yy++) for (let xx = 0; xx < c.width; xx++) {
|
||||
if (x + xx < 0 || y + yy < 0 || x + xx >= width || y + yy >= height || !inside(xx, yy, c.width, c.height, c.radius)) continue;
|
||||
const p = (y + yy) * width + x + xx;
|
||||
output[p] = c.border !== 16 && !inside(xx - 1, yy - 1, c.width - 2, c.height - 2, Math.max(0, c.radius - 1)) ? c.border * 16 : Math.max(output[p], c.background * 16);
|
||||
}
|
||||
} else {
|
||||
const source = c.resource === SCREEN ? { width, height, pixels: screen } : placed.displayList.resources[c.resource];
|
||||
const w = c.op === DrawOp.IMAGE ? source.width : c.width, h = c.op === DrawOp.IMAGE ? source.height : c.height;
|
||||
const at = (v: DrawValue) => evaluate(v, elapsed, presentTime).value;
|
||||
const sx = c.op === DrawOp.IMAGE ? 0 : at(c.x) + (c.resource === SCREEN ? placed.x : 0), sy = c.op === DrawOp.IMAGE ? 0 : at(c.y) + (c.resource === SCREEN ? placed.y : 0);
|
||||
const x = placed.x + (c.op === DrawOp.IMAGE ? c.x : at(c.dx)) + shift, y = placed.y + (c.op === DrawOp.IMAGE ? c.y : at(c.dy));
|
||||
for (let yy = 0; yy < h; yy++) for (let xx = 0; xx < w; xx++) {
|
||||
if (x + xx < 0 || y + yy < 0 || x + xx >= width || y + yy >= height || sx + xx < 0 || sy + yy < 0 || sx + xx >= source.width || sy + yy >= source.height) continue;
|
||||
const v = q(source.pixels[(sy + yy) * source.width + sx + xx]);
|
||||
if (c.op !== DrawOp.IMAGE || !c.transparent || v !== 0) output[(y + yy) * width + x + xx] = v;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,160 @@
|
||||
/** Revision 27 expression bytecode. ELAPSED is a bridge binding, never sent to firmware. */
|
||||
export const ExprOp = {
|
||||
I32: 1, F32: 2, DUP: 3, DROP: 4, SWAP: 5,
|
||||
IADD: 16, ISUB: 17, IMUL: 18, IDIV: 19, IMOD: 20, INEG: 21, IMIN: 22, IMAX: 23,
|
||||
FADD: 32, FSUB: 33, FMUL: 34, FDIV: 35, FNEG: 36, FMIN: 37, FMAX: 38,
|
||||
I2F: 48, F2I: 49, TIME: 50, LERP: 64, SMOOTHSTEP: 65,
|
||||
EASE_IN_QUAD: 66, EASE_OUT_QUAD: 67, EASE_IN_OUT_QUAD: 68,
|
||||
EASE_IN_CUBIC: 69, EASE_OUT_CUBIC: 70, EASE_IN_OUT_CUBIC: 71,
|
||||
ELAPSED: 128,
|
||||
} as const;
|
||||
|
||||
export function integer(value: number, min: number, max: number): number {
|
||||
if (!Number.isInteger(value) || value < min || value > max) throw new Error('Draw integer out of range');
|
||||
return value;
|
||||
}
|
||||
export function varint(value: number, signed = true): number[] {
|
||||
integer(value, signed ? -2147483648 : 0, signed ? 2147483647 : 4294967295);
|
||||
const widths = [7, 14, 21, 28, 32], prefixes = [0, 128, 192, 224, 240];
|
||||
const size = widths.findIndex(bits => bits === 32 || (signed
|
||||
? value >= -(2 ** (bits - 1)) && value < 2 ** (bits - 1) : value < 2 ** bits)) + 1;
|
||||
const bytes = [size === 5 ? 240 : prefixes[size - 1] | ((value >>> (8 * (size - 1))) & ((1 << (8 - size)) - 1))];
|
||||
for (let i = 1; i < size; i++) bytes.push((value >>> (8 * (size - 1 - i))) & 255);
|
||||
return bytes;
|
||||
}
|
||||
|
||||
export class DrawBytes {
|
||||
offset = 0;
|
||||
constructor(readonly bytes: Uint8Array) {}
|
||||
take(size: number): Uint8Array {
|
||||
integer(size, 0, this.bytes.length - this.offset);
|
||||
const result = this.bytes.slice(this.offset, this.offset + size); this.offset += size; return result;
|
||||
}
|
||||
u8(): number { return this.take(1)[0]; }
|
||||
u16(): number { return this.u8() | this.u8() << 8; }
|
||||
i16(): number { return this.u16() << 16 >> 16; }
|
||||
u32(): number { return (this.u16() | this.u16() << 16) >>> 0; }
|
||||
variable(signed = true): number {
|
||||
const first = this.u8();
|
||||
const size = first < 128 ? 1 : first < 192 ? 2 : first < 224 ? 3 : first < 240 ? 4 : first === 240 ? 5 : 0;
|
||||
if (!size) throw new Error('Reserved extended-varint prefix');
|
||||
let value = size === 5 ? 0 : first & ((1 << (8 - size)) - 1);
|
||||
for (let i = 1; i < size; i++) value = (value << 8) | this.u8();
|
||||
const bits = [7, 14, 21, 28, 32][size - 1];
|
||||
return signed ? bits < 32 ? value << (32 - bits) >> (32 - bits) : value : value >>> 0;
|
||||
}
|
||||
}
|
||||
|
||||
/** Immutable, typed expression builder; operations use firmware i32/f32 semantics. */
|
||||
export class DrawExpression<T extends 'i32' | 'f32'> {
|
||||
private constructor(readonly type: T, readonly code: readonly number[]) {
|
||||
// Leave room for native elapsed bindings and surface translation.
|
||||
if (code.length > 900) throw new Error('Draw expression too large');
|
||||
}
|
||||
static i32(value: number): DrawExpression<'i32'> { return new DrawExpression('i32', [ExprOp.I32, ...varint(value)]); }
|
||||
static f32(value: number): DrawExpression<'f32'> {
|
||||
if (!Number.isFinite(Math.fround(value))) throw new Error('Invalid draw float');
|
||||
const bytes = new Uint8Array(4); new DataView(bytes.buffer).setFloat32(0, value, true);
|
||||
return new DrawExpression('f32', [ExprOp.F32, ...bytes]);
|
||||
}
|
||||
/** Time already elapsed on this list's timeline when the next PRESENT happens. */
|
||||
static elapsed(): DrawExpression<'i32'> { return new DrawExpression('i32', [ExprOp.ELAPSED]); }
|
||||
/**
|
||||
* A 0..1 timeline fraction that survives subsequent PRESENT clock resets.
|
||||
* With a delay, it stays 0 for the first delayMs of the timeline; a
|
||||
* negative delay continues a motion that started before the timeline.
|
||||
*/
|
||||
static progress(durationMs: number, delayMs = 0): DrawExpression<'f32'> {
|
||||
integer(delayMs, -2147483647, 2147483647);
|
||||
if (delayMs + durationMs <= 0) return DrawExpression.f32(1);
|
||||
const duration = DrawExpression.i32(integer(durationMs, 1, 2147483647));
|
||||
const end = DrawExpression.i32(integer(delayMs + durationMs, 1, 2147483647));
|
||||
const elapsed = DrawExpression.elapsed().min(end);
|
||||
let total = end.sub(elapsed).max(DrawExpression.i32(0)).time().add(elapsed);
|
||||
if (delayMs) total = total.sub(DrawExpression.i32(delayMs)).max(DrawExpression.i32(0));
|
||||
return total.toFloat().div(duration.toFloat());
|
||||
}
|
||||
static decode(code: Uint8Array): DrawExpression<'i32'> { return new DrawExpression('i32', Array.from(code)); }
|
||||
private binary(other: DrawExpression<T>, i: number, f: number): DrawExpression<T> {
|
||||
if (other.type !== this.type) throw new Error('Draw expression type mismatch');
|
||||
return new DrawExpression(this.type, [...this.code, ...other.code, this.type === 'i32' ? i : f]);
|
||||
}
|
||||
add(b: DrawExpression<T>): DrawExpression<T> { return this.binary(b, ExprOp.IADD, ExprOp.FADD); }
|
||||
sub(b: DrawExpression<T>): DrawExpression<T> { return this.binary(b, ExprOp.ISUB, ExprOp.FSUB); }
|
||||
mul(b: DrawExpression<T>): DrawExpression<T> { return this.binary(b, ExprOp.IMUL, ExprOp.FMUL); }
|
||||
div(b: DrawExpression<T>): DrawExpression<T> { return this.binary(b, ExprOp.IDIV, ExprOp.FDIV); }
|
||||
min(b: DrawExpression<T>): DrawExpression<T> { return this.binary(b, ExprOp.IMIN, ExprOp.FMIN); }
|
||||
max(b: DrawExpression<T>): DrawExpression<T> { return this.binary(b, ExprOp.IMAX, ExprOp.FMAX); }
|
||||
mod(this: DrawExpression<'i32'>, b: DrawExpression<'i32'>): DrawExpression<'i32'> { return this.binary(b, ExprOp.IMOD, ExprOp.IMOD); }
|
||||
neg(): DrawExpression<T> { return new DrawExpression(this.type, [...this.code, this.type === 'i32' ? ExprOp.INEG : ExprOp.FNEG]); }
|
||||
toFloat(this: DrawExpression<'i32'>): DrawExpression<'f32'> { return new DrawExpression('f32', [...this.code, ExprOp.I2F]); }
|
||||
toInt(this: DrawExpression<'f32'>): DrawExpression<'i32'> { return new DrawExpression('i32', [...this.code, ExprOp.F2I]); }
|
||||
time(this: DrawExpression<'i32'>): DrawExpression<'i32'> { return new DrawExpression('i32', [...this.code, ExprOp.TIME]); }
|
||||
ease(this: DrawExpression<'f32'>, op: number = ExprOp.SMOOTHSTEP): DrawExpression<'f32'> {
|
||||
integer(op, ExprOp.SMOOTHSTEP, ExprOp.EASE_IN_OUT_CUBIC);
|
||||
return new DrawExpression('f32', [...this.code, op]);
|
||||
}
|
||||
lerp(this: DrawExpression<'f32'>, from: DrawExpression<'f32'>, to: DrawExpression<'f32'>): DrawExpression<'f32'> {
|
||||
return new DrawExpression('f32', [...from.code, ...to.code, ...this.code, ExprOp.LERP]);
|
||||
}
|
||||
}
|
||||
export type DrawValue = number | DrawExpression<'i32'>;
|
||||
export function encodeValue(value: DrawValue): number[] {
|
||||
return typeof value === 'number' ? varint(value) : [255, ...varint(value.code.length, false), ...value.code];
|
||||
}
|
||||
export function readValue(reader: DrawBytes): DrawValue {
|
||||
if (reader.bytes[reader.offset] !== 255) return reader.variable();
|
||||
reader.u8(); return DrawExpression.decode(reader.take(reader.variable(false)));
|
||||
}
|
||||
|
||||
/** Software preview of the same VM; invalid programs return zero and discard pending animation. */
|
||||
export function evaluate(value: DrawValue, elapsed: number, presentTime = 0): { value: number; pending: boolean } {
|
||||
if (typeof value === 'number') return { value, pending: false };
|
||||
const reader = new DrawBytes(Uint8Array.from(value.code)), stack: { value: number; float: boolean }[] = [];
|
||||
let pending = false;
|
||||
const push = (value: number, float = false) => {
|
||||
value = float ? Math.fround(value) : value | 0;
|
||||
if (!Number.isFinite(value) || stack.length === 32) throw new Error('Invalid expression stack');
|
||||
stack.push({ value, float });
|
||||
};
|
||||
const pop = (float: boolean) => { const v = stack.pop(); if (!v || v.float !== float) throw new Error('Expression type mismatch'); return v.value; };
|
||||
const toInt = (v: number) => { if (v < -2147483648 || v >= 2147483648) throw new Error('Invalid integer conversion'); return Math.trunc(v); };
|
||||
try {
|
||||
while (reader.offset < reader.bytes.length) {
|
||||
const op = reader.u8();
|
||||
if (op === ExprOp.I32) push(reader.variable());
|
||||
else if (op === ExprOp.F32) { const b = reader.take(4); push(new DataView(b.buffer).getFloat32(0, true), true); }
|
||||
else if (op === ExprOp.ELAPSED) push(Math.min(2147483647, Math.max(0, elapsed)));
|
||||
else if (op === ExprOp.I2F) push(pop(false), true);
|
||||
else if (op === ExprOp.F2I) push(toInt(pop(true)));
|
||||
else if (op === ExprOp.TIME) { const max = pop(false); if (max < 0) throw new Error('Invalid time'); const t = Math.min(Math.max(0, presentTime), max); pending ||= t !== max; push(t); }
|
||||
else if (op === ExprOp.INEG || op === ExprOp.FNEG) push(-pop(op === ExprOp.FNEG), op === ExprOp.FNEG);
|
||||
else if (op >= ExprOp.IADD && op <= ExprOp.IMAX || op >= ExprOp.FADD && op <= ExprOp.FMAX) {
|
||||
const float = op >= ExprOp.FADD, b = pop(float), a = pop(float);
|
||||
const key = float ? op - ExprOp.FADD : op - ExprOp.IADD;
|
||||
if ((key === 3 || !float && key === 4) && b === 0) throw new Error('Division by zero');
|
||||
const result = key === 0 ? a + b : key === 1 ? a - b : key === 2 ? float ? a * b : Math.imul(a, b) : key === 3 ? a / b : !float && key === 4 ? a % b : key === (float ? 5 : 6) ? Math.min(a, b) : Math.max(a, b);
|
||||
push(result, float);
|
||||
} else if (op === ExprOp.LERP) { const t = pop(true), b = pop(true), a = pop(true); push(a + Math.fround(Math.fround(b - a) * t), true); }
|
||||
else if (op >= ExprOp.SMOOTHSTEP && op <= ExprOp.EASE_IN_OUT_CUBIC) {
|
||||
const t = Math.max(0, Math.min(1, pop(true))), u = Math.fround(1 - t), f = Math.fround;
|
||||
let result: number;
|
||||
switch (op) {
|
||||
case ExprOp.SMOOTHSTEP: result = f(t * t) * f(3 - f(2 * t)); break;
|
||||
case ExprOp.EASE_IN_QUAD: result = t * t; break;
|
||||
case ExprOp.EASE_OUT_QUAD: result = 1 - f(u * u); break;
|
||||
case ExprOp.EASE_IN_OUT_QUAD: result = t < .5 ? f(2 * t) * t : 1 - f(f(2 * u) * u); break;
|
||||
case ExprOp.EASE_IN_CUBIC: result = f(t * t) * t; break;
|
||||
case ExprOp.EASE_OUT_CUBIC: result = 1 - f(f(u * u) * u); break;
|
||||
default: result = t < .5 ? f(f(4 * t) * t) * t : 1 - f(f(f(4 * u) * u) * u);
|
||||
}
|
||||
push(result, true);
|
||||
} else if (op === ExprOp.DUP) { const a = stack[stack.length - 1]; if (!a) throw new Error('Underflow'); push(a.value, a.float); }
|
||||
else if (op === ExprOp.DROP) { if (!stack.pop()) throw new Error('Underflow'); }
|
||||
else if (op === ExprOp.SWAP) { if (stack.length < 2) throw new Error('Underflow'); const a = stack.pop()!, b = stack.pop()!; stack.push(a, b); }
|
||||
else throw new Error('Unknown expression opcode');
|
||||
}
|
||||
const top = stack.pop(); if (!top) throw new Error('Empty expression');
|
||||
return { value: top.float ? toInt(top.value) : top.value, pending };
|
||||
} catch { return { value: 0, pending: false }; }
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
import { DrawRecordKind, encodePresentation } from "./presentation-wire";
|
||||
/**
|
||||
* Marshals deferred-draw identity (text glyphs and icon images) to the Kotlin
|
||||
* side for the texture-cache pipeline (CFW modes 18/19/20; see
|
||||
@@ -84,6 +85,7 @@ const fwRegistered = new Set<string>();
|
||||
* unseen ink members' rasters with the Kotlin FwGlyphAtlas as a side effect.
|
||||
*/
|
||||
function prepareFwTextRun(placed: PlacedFwText): boolean {
|
||||
if (!textureAtlasAvailable()) return false;
|
||||
if (!inRange16(placed.x) || !inRange16(placed.y)) return false;
|
||||
if (placed.glyphs.length === 0 || placed.glyphs.length > 255) return false;
|
||||
let registration: Array<{ cp: number; data: NonNullable<ReturnType<PlacedFwText["font"]["fwGlyphWireData"]>> }> | null = null;
|
||||
@@ -152,11 +154,12 @@ function inRange16(v: number): boolean {
|
||||
* in draw order. Returns null when nothing is expressible (or without a native atlas).
|
||||
*/
|
||||
export function prepareFrameDraws(draws: readonly DeferredDraw[]): ArrayBuffer | null {
|
||||
if (!textureAtlasAvailable() || draws.length === 0) return null;
|
||||
if (draws.length === 0) return null;
|
||||
|
||||
// Pass 1: resolve ids, register unseen rasters, size the buffer.
|
||||
let registration: Map<FontWireState, RegistrationGroup> | null = null;
|
||||
const fwRunOk = new Map<PlacedFwText, boolean>();
|
||||
const presentations = new Map<PlacedImage, Uint8Array>();
|
||||
let bytes = 0;
|
||||
for (const placed of draws) {
|
||||
if (placed.kind === "glyph") {
|
||||
@@ -175,7 +178,8 @@ export function prepareFrameDraws(draws: readonly DeferredDraw[]): ArrayBuffer |
|
||||
(placed.glyph.coverage ? group.aaGlyphs : group.glyphs).push(placed.glyph);
|
||||
} else if (placed.kind === "image") {
|
||||
if (!inRange16(placed.x) || !inRange16(placed.y)) continue;
|
||||
if (imageId(placed) !== null) bytes += IMAGE_RECORD_BYTES;
|
||||
if (placed.presentation) { const record = encodePresentation(placed); presentations.set(placed, record); bytes += record.length; }
|
||||
else if (imageId(placed) !== null) bytes += IMAGE_RECORD_BYTES;
|
||||
} else {
|
||||
const ok = prepareFwTextRun(placed);
|
||||
fwRunOk.set(placed, ok);
|
||||
@@ -198,7 +202,7 @@ export function prepareFrameDraws(draws: readonly DeferredDraw[]): ArrayBuffer |
|
||||
if (!inRange16(placed.x) || !inRange16(placed.y)) continue;
|
||||
const state = fontWireState(placed.font);
|
||||
if (!state || !representableGlyph(placed.font, placed.glyph)) continue;
|
||||
out.setUint8(offset, 0);
|
||||
out.setUint8(offset, DrawRecordKind.GLYPH);
|
||||
out.setUint16(offset + 1, state.fontId, true);
|
||||
out.setUint32(offset + 3, placed.glyph.encoding, true);
|
||||
out.setInt16(offset + 7, placed.x, true);
|
||||
@@ -207,16 +211,17 @@ export function prepareFrameDraws(draws: readonly DeferredDraw[]): ArrayBuffer |
|
||||
offset += GLYPH_RECORD_BYTES;
|
||||
} else if (placed.kind === "image") {
|
||||
if (!inRange16(placed.x) || !inRange16(placed.y)) continue;
|
||||
if (placed.presentation) { const bytes = presentations.get(placed)!; new Uint8Array(out.buffer).set(bytes, offset); offset += bytes.length; continue; }
|
||||
const id = imageId(placed);
|
||||
if (id === null) continue;
|
||||
out.setUint8(offset, 1);
|
||||
out.setUint8(offset, DrawRecordKind.TEXTURE_IMAGE);
|
||||
out.setUint32(offset + 1, id, true);
|
||||
out.setInt16(offset + 5, placed.x, true);
|
||||
out.setInt16(offset + 7, placed.y, true);
|
||||
offset += IMAGE_RECORD_BYTES;
|
||||
} else {
|
||||
if (!fwRunOk.get(placed)) continue;
|
||||
out.setUint8(offset, 2);
|
||||
out.setUint8(offset, DrawRecordKind.FIRMWARE_TEXT);
|
||||
out.setInt16(offset + 1, placed.x, true);
|
||||
out.setInt16(offset + 3, placed.y, true);
|
||||
out.setUint8(offset + 5, placed.value);
|
||||
|
||||
+67
-7
@@ -1,3 +1,6 @@
|
||||
import { menuSelectionList } from "./menu-selection-list";
|
||||
import { encodeDisplayList, dimDisplayList, paintDisplayList, type DisplayList } from "./display-list";
|
||||
import type { MenuHighlightAnimation } from "../ui/menu-highlight-motion";
|
||||
import { Glyph } from "./bdffont";
|
||||
import { wrapText } from "./textwrap";
|
||||
|
||||
@@ -73,6 +76,7 @@ export type PlacedGlyph = {
|
||||
* from the moment it is placed.
|
||||
*/
|
||||
export type PlacedImage = {
|
||||
presentation?: { displayList?: DisplayList; mode?: "image" | "masked-image"; radius: number; background: number; border: number; depth: number; occlusions?: readonly { x: number; y: number; width: number; height: number }[] };
|
||||
kind: "image";
|
||||
source: GrayImage;
|
||||
x: number;
|
||||
@@ -148,7 +152,9 @@ export class GrayImage {
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
this.pixels = new Uint8Array(width * height);
|
||||
this.clear(fill);
|
||||
if (fill !== 0) {
|
||||
this.clear(fill);
|
||||
}
|
||||
}
|
||||
|
||||
get draws(): readonly DeferredDraw[] {
|
||||
@@ -391,6 +397,9 @@ export class GrayImage {
|
||||
hash = mixInt(hash, placed.source.width);
|
||||
hash = mixInt(hash, placed.source.height);
|
||||
hash = mixInt(hash, placed.source.sourceContentHash32());
|
||||
if (placed.presentation) { const p = placed.presentation; for (const value of [p.radius, p.background, p.border, p.depth, p.mode === "image" ? 1 : p.mode === "masked-image" ? 2 : 0]) hash = mixInt(hash, value);
|
||||
if (p.displayList) for (const byte of encodeDisplayList({ displayList: p.displayList, x: 0, y: 0, width: placed.source.width, height: placed.source.height, depth: p.depth }, p.displayList.timeline?.startedAt ?? 0)) hash = mixInt(hash, byte);
|
||||
}
|
||||
} else {
|
||||
hash = mixInt(hash, 0xf17e);
|
||||
hash = mixInt(hash, placed.font.atlasTag);
|
||||
@@ -501,6 +510,31 @@ export class GrayImage {
|
||||
this.drawList.push({ kind: "fwtext", font, x, y, value: clampByte(value), glyphs });
|
||||
}
|
||||
|
||||
/** Retain the selected row separately from the menu surface for glasses-side composition. */
|
||||
drawMenuSelection(source: GrayImage, x: number, y: number, background: number, border: number, radius = 8, depth = 2, animation?: MenuHighlightAnimation): void {
|
||||
const baked = source.withDrawsBaked();
|
||||
this.drawList.push({ kind: "image", source: baked, x, y,
|
||||
presentation: { radius, background, border, depth, displayList: menuSelectionList(baked, background, border, radius, animation) } });
|
||||
}
|
||||
|
||||
/** Submit a retained list alongside this frame; coordinates are relative to x/y. */
|
||||
drawDisplayList(displayList: DisplayList, x: number, y: number, width: number, height: number, depth = 0): void {
|
||||
this.drawList.push({ kind: "image", source: new GrayImage(width, height), x, y,
|
||||
presentation: { radius: 0, background: 0, border: 0, depth, displayList } });
|
||||
}
|
||||
|
||||
/** Replay an image at stereo depth. Masked images preserve intentional gray8 black (1). */
|
||||
drawDepthImage(source: GrayImage, x: number, y: number, depth: number, masked = false): void {
|
||||
this.drawList.push({ kind: "image", source: source.withDrawsBaked(), x, y,
|
||||
presentation: { mode: masked ? "masked-image" : "image", radius: 0, background: 0, border: 0, depth } });
|
||||
}
|
||||
|
||||
copyPresentationsInto(target: GrayImage, dx: number, dy: number): void {
|
||||
for (const placed of this.drawList) if (placed.kind === "image" && placed.presentation) {
|
||||
target.drawList.push({ ...placed, x: placed.x + dx, y: placed.y + dy });
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Bake this image's deferred draws into its own pixels and clear the list.
|
||||
* Used where later raster must be able to cover earlier draws within the
|
||||
@@ -513,7 +547,8 @@ export class GrayImage {
|
||||
if (placed.kind === "glyph") {
|
||||
rasterizeGlyph(this, placed.font, placed.glyph, placed.x, placed.y, placed.value);
|
||||
} else if (placed.kind === "image") {
|
||||
this.bitBlt(placed.source, placed.x, placed.y, { transparentZero: true });
|
||||
if (placed.presentation) { const one = new GrayImage(this.width, this.height); one.drawList.push(placed); one.bakeDrawsInto(this, 0, 0); }
|
||||
else this.bitBlt(placed.source, placed.x, placed.y, { transparentZero: true });
|
||||
} else {
|
||||
placed.font.bakeFwTextRun(this, placed, 0, 0);
|
||||
}
|
||||
@@ -534,7 +569,7 @@ export class GrayImage {
|
||||
const copy = new GrayImage(this.width, this.height, 0);
|
||||
copy.pixels.set(this.pixels);
|
||||
for (const placed of this.drawList) {
|
||||
if (placed.kind === "image") {
|
||||
if (placed.kind === "image" && !placed.presentation) {
|
||||
copy.bitBlt(placed.source, placed.x, placed.y, { transparentZero: true });
|
||||
}
|
||||
}
|
||||
@@ -544,7 +579,10 @@ export class GrayImage {
|
||||
if (value !== 0) pixels[i] = dimValue(value, scale);
|
||||
}
|
||||
for (const placed of this.drawList) {
|
||||
if (placed.kind !== "image") {
|
||||
if (placed.kind === "image" && placed.presentation) {
|
||||
const p = placed.presentation;
|
||||
copy.drawList.push({ ...placed, source: placed.source.dimmed(factor), presentation: { ...p, background: dimValue(p.background, scale), border: dimValue(p.border, scale), displayList: p.displayList && dimDisplayList(p.displayList, factor) } });
|
||||
} else if (placed.kind !== "image") {
|
||||
copy.drawList.push({ ...placed, value: dimValue(placed.value, scale) });
|
||||
}
|
||||
}
|
||||
@@ -556,11 +594,11 @@ export class GrayImage {
|
||||
* Returns this image unchanged when there are none, otherwise a baked copy
|
||||
* with an empty draw list.
|
||||
*/
|
||||
withDrawsBaked(): GrayImage {
|
||||
withDrawsBaked(presentations = true): GrayImage {
|
||||
if (!this.drawList.length) return this;
|
||||
const baked = new GrayImage(this.width, this.height, 0);
|
||||
baked.pixels.set(this.pixels);
|
||||
this.bakeDrawsInto(baked, 0, 0);
|
||||
this.bakeDrawsInto(baked, 0, 0, presentations);
|
||||
return baked;
|
||||
}
|
||||
|
||||
@@ -579,11 +617,33 @@ export class GrayImage {
|
||||
}
|
||||
|
||||
/** Rasterize this image's deferred draws into target, translated by (dx, dy). */
|
||||
bakeDrawsInto(target: GrayImage, dx: number, dy: number): void {
|
||||
bakeDrawsInto(target: GrayImage, dx: number, dy: number, presentations = true): void {
|
||||
for (const placed of this.drawList) {
|
||||
if (placed.kind === "glyph") {
|
||||
rasterizeGlyph(target, placed.font, placed.glyph, placed.x + dx, placed.y + dy, placed.value);
|
||||
} else if (placed.kind === "image") {
|
||||
if (placed.presentation) {
|
||||
if (!presentations) continue;
|
||||
if (placed.presentation.displayList) {
|
||||
paintDisplayList(target.pixels, target.pixels.slice(), target.width, target.height,
|
||||
{ displayList: placed.presentation.displayList, x: placed.x + dx, y: placed.y + dy, width: placed.source.width, height: placed.source.height, depth: placed.presentation.depth });
|
||||
continue;
|
||||
}
|
||||
if (placed.presentation.mode) {
|
||||
target.bitBlt(placed.source, placed.x + dx, placed.y + dy, { transparentZero: true });
|
||||
continue;
|
||||
}
|
||||
const { radius, background, border } = placed.presentation;
|
||||
const fill = new GrayImage(placed.source.width, placed.source.height);
|
||||
fill.fillRoundedRect(0, 0, fill.width, fill.height, background, radius);
|
||||
for (let y = 0; y < fill.height; y++) for (let x = 0; x < fill.width; x++) {
|
||||
const tx = placed.x + dx + x, ty = placed.y + dy + y;
|
||||
if (tx >= 0 && ty >= 0 && tx < target.width && ty < target.height) {
|
||||
const i = ty * target.width + tx; target.pixels[i] = Math.max(target.pixels[i], fill.pixels[y * fill.width + x]);
|
||||
}
|
||||
}
|
||||
target.drawRoundedRect(placed.x + dx, placed.y + dy, fill.width, fill.height, border, radius);
|
||||
}
|
||||
target.bitBlt(placed.source, placed.x + dx, placed.y + dy, { transparentZero: true });
|
||||
} else {
|
||||
placed.font.bakeFwTextRun(target, placed, dx, dy);
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
import type { MenuHighlightAnimation } from '../ui/menu-highlight-motion';
|
||||
import { DrawExpression as E, type DrawValue } from './draw-expression';
|
||||
import { DrawOp, type DisplayList, type ListCall, type ListImage } from './display-list';
|
||||
|
||||
export type MenuScrollHighlight = {
|
||||
/** Row top within the viewport; clamped inside it. */
|
||||
y: DrawValue;
|
||||
width: number;
|
||||
height: number;
|
||||
radius: number;
|
||||
/** Gray8, like drawMenuSelection. */
|
||||
background: number;
|
||||
border: number;
|
||||
};
|
||||
|
||||
/**
|
||||
* A highlight sliding by (0, dy) to rest at `y`, on a list timeline that
|
||||
* started at `timelineStart` (the slide may have started before or after it).
|
||||
*/
|
||||
export function slidingHighlightY(y: number, motion: MenuHighlightAnimation | undefined, timelineStart: number): E<'i32'> {
|
||||
if (!motion?.dy) return E.i32(y);
|
||||
return E.progress(motion.durationMs, motion.startedAt - timelineStart).ease()
|
||||
.lerp(E.i32(y + motion.dy).toFloat(), E.i32(y).toFloat()).toInt();
|
||||
}
|
||||
|
||||
/**
|
||||
* A menu viewport moving over a pre-rendered strip. `strip` holds every row
|
||||
* visible at any point of the motion, and sits `stripX` pixels into the
|
||||
* viewport; `sourceY` is the strip row at the viewport's top, animated on
|
||||
* `timeline`. The highlight is max-blended over the copied rows.
|
||||
*/
|
||||
export function menuScrollList(strip: ListImage, stripX: number, viewportHeight: number,
|
||||
timeline: { token: number; startedAt: number }, sourceY: DrawValue, highlight?: MenuScrollHighlight): DisplayList {
|
||||
const calls: ListCall[] = [
|
||||
{ op: DrawOp.RECT_COPY, resource: 0, x: 0, y: sourceY, width: strip.width, height: viewportHeight, dx: stripX, dy: 0 },
|
||||
];
|
||||
if (highlight) {
|
||||
const q = (n: number) => Math.min(15, (n + 8) >> 4);
|
||||
const y = typeof highlight.y === 'number' ? highlight.y
|
||||
: highlight.y.max(E.i32(0)).min(E.i32(viewportHeight - highlight.height));
|
||||
calls.push({ op: DrawOp.ROUNDED_RECT, x: 0, y, width: highlight.width, height: highlight.height, radius: highlight.radius,
|
||||
background: q(highlight.background), border: q(highlight.border) });
|
||||
}
|
||||
return { resources: [strip], timeline: { token: timeline.token, startedAt: timeline.startedAt }, calls };
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
import type { MenuHighlightAnimation } from '../ui/menu-highlight-motion';
|
||||
import { DrawExpression as E } from './draw-expression';
|
||||
import { DrawOp, type DisplayList, type ListImage } from './display-list';
|
||||
|
||||
/** Menu policy and easing live in TypeScript; the bridge carries only generic calls/expressions. */
|
||||
export function menuSelectionList(image: ListImage, background: number, border: number, radius: number, animation?: MenuHighlightAnimation): DisplayList {
|
||||
const coordinate = (delta: number) => {
|
||||
if (!animation || !delta) return 0;
|
||||
const progress = E.progress(animation.durationMs).ease();
|
||||
return progress.lerp(E.i32(delta).toFloat(), E.f32(0)).toInt();
|
||||
};
|
||||
const q = (n: number) => Math.min(15, (n + 8) >> 4);
|
||||
return { resources: [image], timeline: animation,
|
||||
calls: [
|
||||
{ op: DrawOp.ROUNDED_RECT, x: coordinate(animation?.dx ?? 0), y: coordinate(animation?.dy ?? 0), width: image.width, height: image.height, radius, background: q(background), border: q(border) },
|
||||
{ op: DrawOp.IMAGE, resource: 0, x: 0, y: 0, transparent: true },
|
||||
] };
|
||||
}
|
||||
+58
-19
@@ -1,4 +1,4 @@
|
||||
import { GrayImage, type DeferredDraw } from "./image";
|
||||
import { GrayImage, type DeferredDraw, type PlacedImage } from "./image";
|
||||
|
||||
/**
|
||||
* The unit apps (and the shell) submit to the compositor: instead of one
|
||||
@@ -8,18 +8,17 @@ import { GrayImage, type DeferredDraw } from "./image";
|
||||
* transparent, 1 is black (the two quantize to the same 4bpp shade, so
|
||||
* reserving 0 costs nothing visible).
|
||||
*
|
||||
* This representation exists to carry structure the wire pipeline will later
|
||||
* exploit: a plane's glyph list can become on-glasses display-list references
|
||||
* (texture cache), and the offset can diverge per lens for stereo depth. For
|
||||
* now the compositor boundary just flattens (flattenPlanes) and feeds the
|
||||
* existing incremental-update and screen-preview pipeline.
|
||||
* Static planes form the incremental screen buffer. Planes with stereo depth
|
||||
* and retained presentation draws replay from resources in the root display list.
|
||||
*/
|
||||
export type Plane = {
|
||||
shellKey?: number;
|
||||
depth?: number;
|
||||
dimUnderneath?: number;
|
||||
image: GrayImage;
|
||||
/**
|
||||
* Offset of the plane's top-left within the submitted frame. Applied
|
||||
* identically to both lenses for now; a per-lens divergence (stereo depth
|
||||
* for floating overlays) is the planned extension.
|
||||
* before any per-lens horizontal offset from depth.
|
||||
*/
|
||||
x: number;
|
||||
y: number;
|
||||
@@ -70,8 +69,9 @@ export function flattenPlanes(
|
||||
/**
|
||||
* Flatten planes and also return the frame's deferred draws (glyphs and
|
||||
* images), translated into frame coordinates and in bake order (plane order,
|
||||
* then each image's draw order). The flattened image has every draw baked in,
|
||||
* exactly as flattenPlanes produces; the list preserves the draws' identity
|
||||
* then each image's draw order). Regular draws are baked into the image;
|
||||
* selected-menu presentation records remain unbaked for root-list replay.
|
||||
* The list preserves the draws' identity
|
||||
* so the texture-cache pipeline can replay them as on-glasses cached draws
|
||||
* (see graphics/glyph-wire.ts).
|
||||
*/
|
||||
@@ -79,15 +79,54 @@ export function flattenPlanesWithDraws(
|
||||
planes: readonly Plane[],
|
||||
size?: { width: number; height: number },
|
||||
): { image: GrayImage; draws: DeferredDraw[] } {
|
||||
const image = flattenPlanes(planes, size);
|
||||
const rasters = planes.map(p => p.image.withDrawsBaked(false));
|
||||
const image = flattenPlanes(planes.map((plane, i) => ({ ...plane, image: plane.depth ? new GrayImage(plane.image.width, plane.image.height) : rasters[i]! })), size);
|
||||
const draws: DeferredDraw[] = [];
|
||||
for (const plane of planes) {
|
||||
for (const placed of plane.image.draws) {
|
||||
draws.push(
|
||||
plane.x === 0 && plane.y === 0
|
||||
? placed
|
||||
: { ...placed, x: placed.x + plane.x, y: placed.y + plane.y },
|
||||
);
|
||||
for (const [index, plane] of planes.entries()) {
|
||||
const retained: DeferredDraw[] = [];
|
||||
if (plane.depth) {
|
||||
// Keep the whole menu off the screen buffer, including its black background.
|
||||
const raster = rasters[index]!;
|
||||
let left = raster.width, top = raster.height, right = -1, bottom = -1;
|
||||
for (let y = 0; y < raster.height; y++) for (let x = 0; x < raster.width; x++) if (raster.pixels[y*raster.width+x]) {
|
||||
left = Math.min(left,x); top = Math.min(top,y); right = Math.max(right,x); bottom = Math.max(bottom,y);
|
||||
}
|
||||
if (right >= left) {
|
||||
const crop = new GrayImage(right-left+1,bottom-top+1);
|
||||
for(let y=0;y<crop.height;y++) crop.pixels.set(raster.pixels.subarray((y+top)*raster.width+left,(y+top)*raster.width+right+1),y*crop.width);
|
||||
retained.push({ kind: "image", source: crop, x: left, y: top,
|
||||
presentation: { mode: "masked-image", radius: 0, background: 0, border: 0, depth: plane.depth } });
|
||||
}
|
||||
}
|
||||
const planeDraws = plane.depth ? [...retained, ...plane.image.draws.filter(d => d.kind === "image" && d.presentation).map(d => {
|
||||
const draw = d as PlacedImage;
|
||||
return { ...draw, presentation: { ...draw.presentation!, depth: draw.presentation!.depth + plane.depth! } };
|
||||
})] : plane.image.draws;
|
||||
for (const placed of planeDraws) {
|
||||
const translated = { ...placed, x: placed.x + plane.x, y: placed.y + plane.y };
|
||||
if (translated.kind === "image" && translated.presentation) {
|
||||
// Restore opaque pixels from later planes after replaying this row. The screen
|
||||
// already contains those pixels; rect copies preserve rounded/irregular occlusion.
|
||||
const occlusions: { x: number; y: number; width: number; height: number }[] = [];
|
||||
const depth = translated.presentation.depth;
|
||||
const leftShift = Math.floor(depth / 2), rightShift = -Math.floor((depth + 1) / 2);
|
||||
const left = Math.max(0, translated.x + Math.min(leftShift, rightShift));
|
||||
const right = Math.min(image.width, translated.x + translated.source.width + Math.max(leftShift, rightShift));
|
||||
for (let y = Math.max(0, translated.y); y < Math.min(image.height, translated.y + translated.source.height); y++) {
|
||||
let start = -1;
|
||||
for (let x = left; x <= right; x++) {
|
||||
const covered = x < right && planes.some((p, i) => i > index && !p.depth && x >= p.x && y >= p.y && x < p.x + p.image.width && y < p.y + p.image.height && rasters[i]!.pixels[(y-p.y)*p.image.width+x-p.x] !== 0);
|
||||
if (covered && start < 0) start = x;
|
||||
if (!covered && start >= 0) {
|
||||
const previous = occlusions.find(r => r.x === start && r.width === x - start && r.y + r.height === y);
|
||||
if (previous) previous.height++; else occlusions.push({ x: start, y, width: x-start, height: 1 });
|
||||
start = -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
translated.presentation = { ...translated.presentation, occlusions };
|
||||
}
|
||||
draws.push(translated);
|
||||
}
|
||||
}
|
||||
return { image, draws };
|
||||
@@ -100,6 +139,6 @@ export function flattenPlanesWithDraws(
|
||||
*/
|
||||
export function planesFingerprint(planes: readonly Plane[]): string {
|
||||
return planes
|
||||
.map((plane) => `${plane.x},${plane.y}+${plane.image.width}x${plane.image.height}:${plane.image.fingerprint()}`)
|
||||
.map((plane) => `${plane.depth ?? 0}:${plane.x},${plane.y}+${plane.image.width}x${plane.image.height}:${plane.image.fingerprint()}`)
|
||||
.join("|");
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
import type { PlacedImage } from './image'
|
||||
import { DISPLAY_LIST_RECORD, DrawOp, SCREEN, encodeDisplayList, readDisplayList, paintDisplayList, type DisplayList } from './display-list'
|
||||
|
||||
/** TS/native bridge tags, not firmware draw opcodes. Keep in sync with DrawRecordKind.kt. */
|
||||
export const DrawRecordKind = {
|
||||
GLYPH: 0,
|
||||
TEXTURE_IMAGE: 1,
|
||||
FIRMWARE_TEXT: 2,
|
||||
MENU_SELECTION: 3,
|
||||
TRANSPARENT_IMAGE: 4,
|
||||
MASKED_IMAGE: 5,
|
||||
DISPLAY_LIST: DISPLAY_LIST_RECORD,
|
||||
} as const
|
||||
|
||||
export function isPresentationKind(kind: number | undefined): boolean {
|
||||
return kind === DrawRecordKind.MENU_SELECTION || kind === DrawRecordKind.TRANSPARENT_IMAGE ||
|
||||
kind === DrawRecordKind.MASKED_IMAGE || kind === DrawRecordKind.DISPLAY_LIST
|
||||
}
|
||||
|
||||
/** Presentation record: tag, signed xy, u16 wh/radius, gray fill/stroke, signed depth, restore-count, gray pixels, restore rectangles. */
|
||||
export function encodePresentation(draw: PlacedImage): Uint8Array {
|
||||
const p = draw.presentation!
|
||||
const { width, height, pixels } = draw.source
|
||||
if (width < 1 || height < 1 || width > 640 || height > 480 || !p.displayList && 5 + Math.ceil(width / 2) * height > 65536) throw new Error('Presentation image exceeds the resource size limit')
|
||||
if (!Number.isInteger(p.depth) || p.depth < -128 || p.depth > 127) throw new Error('Invalid menu depth')
|
||||
if ((p.occlusions?.length ?? 0) > 2048) throw new Error('Too many menu occlusion rectangles')
|
||||
if (p.displayList) {
|
||||
const calls = [...p.displayList.calls, ...(p.occlusions ?? []).map(r => ({
|
||||
op: DrawOp.RECT_COPY, resource: SCREEN, x: r.x - draw.x, y: r.y - draw.y,
|
||||
width: r.width, height: r.height, dx: r.x - draw.x, dy: r.y - draw.y, depth: -p.depth,
|
||||
}))];
|
||||
return encodeDisplayList({ displayList: { ...p.displayList, calls }, x: draw.x, y: draw.y, width, height, depth: p.depth });
|
||||
}
|
||||
const header = 16;
|
||||
const bytes = new Uint8Array(header + width * height + (p.occlusions?.length ?? 0) * 8), view = new DataView(bytes.buffer)
|
||||
bytes[0] = p.mode === "image" ? DrawRecordKind.TRANSPARENT_IMAGE
|
||||
: p.mode === "masked-image" ? DrawRecordKind.MASKED_IMAGE : DrawRecordKind.MENU_SELECTION
|
||||
view.setInt16(1, draw.x, true); view.setInt16(3, draw.y, true)
|
||||
view.setUint16(5, width, true); view.setUint16(7, height, true); view.setUint16(9, p.radius, true)
|
||||
bytes[11] = p.background; bytes[12] = p.border; view.setInt8(13, p.depth)
|
||||
view.setUint16(14, p.occlusions?.length ?? 0, true)
|
||||
bytes.set(pixels, header)
|
||||
let offset = header + pixels.length
|
||||
for (const rect of p.occlusions ?? []) {
|
||||
view.setUint16(offset, rect.x, true); view.setUint16(offset + 2, rect.y, true)
|
||||
view.setUint16(offset + 4, rect.width, true); view.setUint16(offset + 6, rect.height, true); offset += 8
|
||||
}
|
||||
return bytes
|
||||
}
|
||||
|
||||
export type Selection = { displayList?: DisplayList; mode?: "image" | "masked-image"; x: number; y: number; width: number; height: number; radius: number; background: number; border: number; depth: number; pixels: Uint8Array; occlusions: { x: number; y: number; width: number; height: number }[] }
|
||||
export function readPresentation(bytes: Uint8Array, offset: number): { selection: Selection; end: number } {
|
||||
if (bytes[offset] === DrawRecordKind.DISPLAY_LIST) {
|
||||
const { placed, end } = readDisplayList(bytes, offset);
|
||||
return { selection: { ...placed, radius: 0, background: 0, border: 0, pixels: new Uint8Array(0), occlusions: [] }, end };
|
||||
}
|
||||
const v = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength), w = v.getUint16(offset + 5, true), h = v.getUint16(offset + 7, true)
|
||||
const kind = bytes[offset]
|
||||
const header = 16
|
||||
const selection: Selection = { mode: kind === DrawRecordKind.TRANSPARENT_IMAGE ? "image" : kind === DrawRecordKind.MASKED_IMAGE ? "masked-image" : undefined, x: v.getInt16(offset+1,true), y: v.getInt16(offset+3,true), width:w, height:h,
|
||||
radius:v.getUint16(offset+9,true), background:bytes[offset+11]!, border:bytes[offset+12]!, depth:v.getInt8(offset+13),
|
||||
pixels:bytes.slice(offset+header,offset+header+w*h), occlusions:[] }
|
||||
let end=offset+header+w*h
|
||||
for(let i=0;i<v.getUint16(offset+14,true);i++) { selection.occlusions.push({x:v.getUint16(end,true),y:v.getUint16(end+2,true),width:v.getUint16(end+4,true),height:v.getUint16(end+6,true)});end+=8 }
|
||||
return {selection,end}
|
||||
}
|
||||
export function presentationRecords(buffer: ArrayBuffer | null): Selection[] {
|
||||
if (!buffer) return []
|
||||
const bytes=new Uint8Array(buffer), result:Selection[]=[]
|
||||
for(let p=0;p<bytes.length;) {
|
||||
if(isPresentationKind(bytes[p])) { const record=readPresentation(bytes,p);result.push(record.selection);p=record.end }
|
||||
else if(bytes[p]===DrawRecordKind.GLYPH) p+=12
|
||||
else if(bytes[p]===DrawRecordKind.TEXTURE_IMAGE) p+=9
|
||||
else if(bytes[p]===DrawRecordKind.FIRMWARE_TEXT) p+=7+bytes[p+6]!*7
|
||||
else break
|
||||
}
|
||||
return result
|
||||
}
|
||||
export function paintPresentation(output: Uint8Array, screen: Uint8Array, width: number, height: number, row: Selection, right = false): void {
|
||||
if (row.displayList) { paintDisplayList(output, screen, width, height, { ...row, displayList: row.displayList }, right); return; }
|
||||
const q=(n:number)=>Math.min(15,(n+8)>>4), shift=right?-Math.floor((row.depth+1)/2):Math.floor(row.depth/2)
|
||||
const inside=(x:number,y:number,w:number,h:number,r:number)=>{
|
||||
if(x<0||y<0||x>=w||y>=h) return false
|
||||
r=Math.min(r,Math.floor(w/2),Math.floor(h/2))
|
||||
const dx=Math.max(0,2*r-(2*Math.min(x,w-1-x)+1)),dy=Math.max(0,2*r-(2*Math.min(y,h-1-y)+1))
|
||||
return dx*dx+dy*dy<=4*r*r
|
||||
}
|
||||
const highlightX = row.x, highlightY = row.y
|
||||
if (!row.mode) for(let yy=0;yy<row.height;yy++) for(let xx=0;xx<row.width;xx++) {
|
||||
const x=highlightX+xx+shift,y=highlightY+yy
|
||||
if(x<0||y<0||x>=width||y>=height || !inside(xx,yy,row.width,row.height,row.radius)) continue
|
||||
const i=y*width+x
|
||||
output[i]=!inside(xx-1,yy-1,row.width-2,row.height-2,Math.max(0,row.radius-1))?q(row.border)*16:Math.max(output[i]!,q(row.background)*16)
|
||||
}
|
||||
for(let yy=0;yy<row.height;yy++) for(let xx=0;xx<row.width;xx++) {
|
||||
const x=row.x+xx+shift,y=row.y+yy
|
||||
if(x<0||y<0||x>=width||y>=height) continue
|
||||
const value=q(row.pixels[yy*row.width+xx]!)
|
||||
if(row.mode === "masked-image" ? row.pixels[yy*row.width+xx] !== 0 : value !== 0) output[y*width+x]=value*16
|
||||
}
|
||||
for(const r of row.occlusions) for(let yy=r.y;yy<r.y+r.height;yy++) for(let xx=r.x;xx<r.x+r.width;xx++) if(xx>=0&&yy>=0&&xx<width&&yy<height) output[yy*width+xx]=q(screen[yy*width+xx]!)*16
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
import { encodePresentation } from "./presentation-wire"
|
||||
import { GrayImage, type PlacedImage } from './image'
|
||||
import type { Plane } from './plane'
|
||||
|
||||
/** Crop one independently owned, opaque shell surface; never split it into tiles. */
|
||||
export function shellCrop(image: GrayImage, x: number, y: number, width: number, height: number, key: number): Plane {
|
||||
const baked = image.withDrawsBaked(false), crop = new GrayImage(width, height, 0)
|
||||
for (let row = 0; row < height; row++) crop.pixels.set(baked.pixels.subarray((y + row) * image.width + x, (y + row) * image.width + x + width), row * width)
|
||||
image.copyPresentationsInto(crop, -x, -y)
|
||||
return { image: crop, x, y, shellKey: key }
|
||||
}
|
||||
|
||||
/** Shared Kotlin receives original 8-bit pixels and quantizes once, before composing. */
|
||||
export function encodeShellScene(planes: readonly Plane[]): Uint8Array {
|
||||
const layers: { image: GrayImage; x: number; y: number; key: number; depth: number; dim: number; selections: Uint8Array[] }[] = []
|
||||
for (const plane of planes) {
|
||||
const image = plane.image.withDrawsBaked(false)
|
||||
let left = image.width, top = image.height, right = -1, bottom = -1
|
||||
for (let y = 0; y < image.height; y++) for (let x = 0; x < image.width; x++) {
|
||||
if (image.pixels[y * image.width + x] === 0) continue
|
||||
left = Math.min(left, x); right = Math.max(right, x); top = Math.min(top, y); bottom = Math.max(bottom, y)
|
||||
}
|
||||
if (right < left) continue
|
||||
const w = right - left + 1, h = bottom - top + 1
|
||||
if (5 + Math.ceil(w / 2) * h > 65536) throw new Error(`Shell surface ${plane.shellKey} exceeds 64 KiB (${w}×${h})`)
|
||||
const cropped = shellCrop(image, left, top, w, h, plane.shellKey!)
|
||||
const selections = plane.image.draws
|
||||
.filter((d): d is PlacedImage => d.kind === "image" && !!d.presentation)
|
||||
.map(d => encodePresentation({ ...d, x: d.x + plane.x, y: d.y + plane.y,
|
||||
presentation: { ...d.presentation!, depth: d.presentation!.depth + (plane.depth ?? 0) } }))
|
||||
layers.push({ depth: plane.depth ?? 0, selections, image: cropped.image, x: plane.x + left, y: plane.y + top, key: plane.shellKey!, dim: Math.round((plane.dimUnderneath ?? 1) * 256) })
|
||||
}
|
||||
const result = new Uint8Array(2 + layers.reduce((n, l) => n + 16 + l.image.pixels.length + l.selections.reduce((n, b) => n + b.length, 0), 0))
|
||||
const view = new DataView(result.buffer); let p = 0
|
||||
const word = (n: number) => { view.setUint16(p, n, true); p += 2 }
|
||||
word(layers.length)
|
||||
for (const l of layers) { word(l.key); word(l.x); word(l.y); word(l.image.width); word(l.image.height); word(l.dim); word(l.selections.length); word(l.depth); result.set(l.image.pixels, p); p += l.image.pixels.length; for (const selection of l.selections) { result.set(selection, p); p += selection.length } }
|
||||
return result
|
||||
}
|
||||
@@ -1,5 +1,4 @@
|
||||
import { fromData, toData } from '../native/kotlin-data'
|
||||
import type { TextureFrame } from '../g2/texture-planner'
|
||||
import type { SurfaceConfiguration, SurfaceRect } from './surface-compositor'
|
||||
export type { SurfaceConfiguration, SurfaceRect } from './surface-compositor'
|
||||
declare const FaceclawKitIosSurfaceCompositor: any
|
||||
@@ -19,6 +18,7 @@ export class SurfaceCompositor {
|
||||
}
|
||||
removeSurface(id: string): void { this.native.removeId(id); this.surfaces.delete(id) }
|
||||
setSurfaceVisible(id: string, visible: boolean): void { this.native.visibleIdVisible(id, visible) }
|
||||
setSurfaceDepth(id: string, depth: number): void { this.native.depthIdDepth(id, depth) }
|
||||
setUnderlayDim(belowZOrder: number, factor: number): void {
|
||||
if (!Number.isFinite(factor)) throw new Error('Invalid dim factor')
|
||||
this.native.dimBelowFactor(belowZOrder, factor)
|
||||
@@ -33,9 +33,6 @@ export class SurfaceCompositor {
|
||||
this.native.submitDrawsIdDataXYWidthHeightDraws(id, toData(pixels), rect.x, rect.y, rect.width, rect.height,
|
||||
draws ? toData(new Uint8Array(draws)) : null)
|
||||
}
|
||||
setShellScene(bytes: Uint8Array): void { this.native.shellData(toData(bytes)) }
|
||||
composite(): Uint8Array { return fromData(this.native.composite()) }
|
||||
compositeFrame(): { pixels: Uint8Array; textures: TextureFrame } {
|
||||
const native = this.native.compositeFrame()
|
||||
return { pixels: fromData(native.pixels), textures: { native } }
|
||||
}
|
||||
}
|
||||
@@ -1,14 +1,17 @@
|
||||
import { presentationRecords, readPresentation, paintPresentation, type Selection } from "./presentation-wire"
|
||||
/** Platform-independent 8bpp surface composition for the local display. */
|
||||
import type { TextureFrame } from '../g2/texture-planner'
|
||||
export type SurfaceRect = { x: number; y: number; width: number; height: number }
|
||||
export type SurfaceConfiguration = SurfaceRect & {
|
||||
zOrder: number
|
||||
transparency: 'opaque' | 'color-key'
|
||||
}
|
||||
type Surface = SurfaceConfiguration & { pixels: Uint8Array; visible: boolean }
|
||||
type Surface = SurfaceConfiguration & { pixels: Uint8Array; visible: boolean; selections: Selection[] }
|
||||
|
||||
export class SurfaceCompositor {
|
||||
private readonly surfaces = new Map<string, Surface>()
|
||||
private shellScene: Uint8Array | null = null
|
||||
private shellReceivedAt = 0
|
||||
setShellScene(bytes: Uint8Array): void { this.shellScene = new Uint8Array(bytes); this.shellReceivedAt = Date.now() }
|
||||
private dimBelow = 0
|
||||
private dimFactor = 1
|
||||
private blanked = false
|
||||
@@ -23,13 +26,15 @@ export class SurfaceCompositor {
|
||||
const previous = this.surfaces.get(id)
|
||||
const pixels = previous?.width === options.width && previous.height === options.height
|
||||
? previous.pixels : new Uint8Array(options.width * options.height)
|
||||
this.surfaces.set(id, { ...options, pixels, visible: previous?.visible ?? true })
|
||||
this.surfaces.set(id, { ...options, pixels, visible: previous?.visible ?? true, selections: previous?.selections ?? [] })
|
||||
}
|
||||
removeSurface(id: string): void { this.surfaces.delete(id) }
|
||||
setSurfaceVisible(id: string, visible: boolean): void {
|
||||
const surface = this.surfaces.get(id)
|
||||
if (surface) surface.visible = visible
|
||||
}
|
||||
/** Stereo depth only shifts the lenses; this local composite shows the unshifted screen. */
|
||||
setSurfaceDepth(_id: string, _depth: number): void {}
|
||||
setUnderlayDim(belowZOrder: number, factor: number): void {
|
||||
if (!Number.isFinite(factor)) throw new Error('Invalid dim factor')
|
||||
this.dimBelow = belowZOrder
|
||||
@@ -43,6 +48,7 @@ export class SurfaceCompositor {
|
||||
|| rect.width <= 0 || rect.height <= 0 || pixels.length !== rect.width * rect.height) {
|
||||
throw new Error('Invalid frame buffer or rectangle')
|
||||
}
|
||||
surface.selections = presentationRecords(_draws)
|
||||
const left = Math.max(0, rect.x), right = Math.min(surface.width, rect.x + rect.width)
|
||||
const top = Math.max(0, rect.y), bottom = Math.min(surface.height, rect.y + rect.height)
|
||||
for (let y = top; y < bottom; y++) {
|
||||
@@ -51,15 +57,14 @@ export class SurfaceCompositor {
|
||||
surface.pixels.set(pixels.subarray(source, source + right - left), y * surface.width + left)
|
||||
}
|
||||
}
|
||||
compositeFrame(): { pixels: Uint8Array; textures?: TextureFrame } { return { pixels: this.composite() } }
|
||||
composite(): Uint8Array {
|
||||
const output = new Uint8Array(this.width * this.height)
|
||||
if (this.blanked) return output
|
||||
const surfaces = [...this.surfaces.values()].filter(s => s.visible).sort((a, b) => a.zOrder - b.zOrder)
|
||||
const surfaces = [...this.surfaces.values()].filter(s => s.visible && !(this.shellScene && s === this.surfaces.get('shell'))).sort((a, b) => a.zOrder - b.zOrder)
|
||||
for (const surface of surfaces) {
|
||||
const left = Math.max(0, surface.x), right = Math.min(this.width, surface.x + surface.width)
|
||||
const top = Math.max(0, surface.y), bottom = Math.min(this.height, surface.y + surface.height)
|
||||
const dim = surface.zOrder < this.dimBelow ? this.dimFactor : 1
|
||||
const dim = !this.shellScene && surface.zOrder < this.dimBelow ? this.dimFactor : 1
|
||||
for (let y = top; y < bottom; y++) {
|
||||
let source = (y - surface.y) * surface.width + left - surface.x
|
||||
let target = y * this.width + left
|
||||
@@ -72,6 +77,27 @@ export class SurfaceCompositor {
|
||||
}
|
||||
}
|
||||
}
|
||||
const screen = output.slice()
|
||||
if (!surfaces.some(s => s.zOrder > 1) && (this.shellScene || surfaces.some(s => s.selections.length))) {
|
||||
for(let i=0;i<output.length;i++) output[i]=Math.min(15,(output[i]!+8)>>4)*16
|
||||
for(const surface of surfaces) for(const row of surface.selections) paintPresentation(output,screen,this.width,this.height,{...row,x:row.x+surface.x,y:row.y+surface.y,occlusions:row.occlusions.map(r=>({...r,x:r.x+surface.x,y:r.y+surface.y}))})
|
||||
}
|
||||
if (this.shellScene && !surfaces.some(s => s.zOrder > 1)) {
|
||||
const quantize = (v: number) => Math.min(15, (v + 8) >> 4)
|
||||
for (let i=0;i<output.length;i++) output[i]=quantize(output[i])*16
|
||||
const bytes=this.shellScene, view=new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength)
|
||||
let p=2
|
||||
for(let i=0;i<view.getUint16(0,true);i++) {
|
||||
const x=view.getInt16(p+2,true), y=view.getInt16(p+4,true), w=view.getUint16(p+6,true), h=view.getUint16(p+8,true), dim=view.getUint16(p+10,true), selections=view.getUint16(p+12,true), shift=Math.floor(view.getInt16(p+14,true)/2)
|
||||
p+=16
|
||||
if(dim<256) for(let j=0;j<output.length;j++) output[j]=Math.floor((output[j]/16)*dim/256)*16
|
||||
for(let yy=0;yy<h;yy++) for(let xx=0;xx<w;xx++) {
|
||||
if(x+xx+shift>=0 && y+yy>=0 && x+xx+shift<this.width && y+yy<this.height) output[(y+yy)*this.width+x+xx+shift]=quantize(bytes[p+yy*w+xx])*16
|
||||
}
|
||||
p+=w*h
|
||||
for(let j=0;j<selections;j++) { const record=readPresentation(bytes,p);p=record.end;if(record.selection.displayList?.timeline) record.selection.displayList = { ...record.selection.displayList, presentedAt: this.shellReceivedAt, timeline: { ...record.selection.displayList.timeline, startedAt: record.selection.displayList.timeline.startedAt - (Date.now()-this.shellReceivedAt) } };paintPresentation(output,screen,this.width,this.height,record.selection) }
|
||||
}
|
||||
}
|
||||
return output
|
||||
}
|
||||
}
|
||||
@@ -18,7 +18,7 @@ declare const java: any;
|
||||
* - "whisper-base-en": sherpa-onnx's offline Whisper backend, base.en,
|
||||
* int8-quantized. Picked over tiny.en for materially better accuracy
|
||||
* (Whisper's own tiny/base WER gap is real and well documented) on the
|
||||
* reasoning that FaceclawVoiceController.java no longer re-decodes a
|
||||
* reasoning that FaceclawVoiceController.kt no longer re-decodes a
|
||||
* Whisper segment on every live-partial tick — see its ASR_WHISPER_*
|
||||
* comment — so base.en's extra compute is a one-time cost per utterance,
|
||||
* not a cost paid ~1.4x/second while the user is speaking. tiny.en is an
|
||||
|
||||
@@ -1,3 +1,15 @@
|
||||
import { iosBleTraffic } from '../g2/ble-traffic-counters'
|
||||
export type { BleTrafficSample } from './ble-traffic'
|
||||
export function sampleBleTraffic(): { messages: number; bytes: number; frames: number } { return iosBleTraffic.sample() }
|
||||
declare const FaceclawKitIosBleTraffic: any
|
||||
|
||||
// The Kotlin counters reset on every sample; accumulate here so callers see
|
||||
// running totals like FaceclawBleManager.sampleOutboundTraffic on Android.
|
||||
// CoreBluetooth has no per-message boundary; messages counts write calls.
|
||||
const totals = { messages: 0, bytes: 0, frames: 0 }
|
||||
export function sampleBleTraffic(): { messages: number; bytes: number; frames: number } {
|
||||
const sample = FaceclawKitIosBleTraffic.shared.sample()
|
||||
const written = Number(sample.getIndex(0)), frames = Number(sample.getIndex(2))
|
||||
totals.bytes += written
|
||||
totals.frames += frames
|
||||
if (written > 0) totals.messages++
|
||||
return { ...totals }
|
||||
}
|
||||
@@ -1,68 +1,89 @@
|
||||
import { StockConnection, StockTimeout } from '../g2/stock-connection'
|
||||
import * as p from '../g2/ble-protocol'
|
||||
import { iosBluetooth } from './ios-bluetooth'
|
||||
import { kotlinListener } from './kotlin-listener.ios'
|
||||
import type { DeviceInfo, DeviceInfoState } from './device-info-probe'
|
||||
export type { DeviceInfo, DeviceInfoState } from './device-info-probe'
|
||||
|
||||
declare const FaceclawKitIosDeviceInfoProbe: any, FaceclawKitFaceclawDeviceInfoProbeListener: any
|
||||
|
||||
/**
|
||||
* iOS twin of the Android DeviceInfoProbe wrapper: the shared Kotlin DeviceInfoProbeFlow
|
||||
* runs over a CoreBluetooth central (FaceclawKitIosDeviceInfoProbe); TypeScript only maps
|
||||
* the stored MAC addresses to peripheral identifiers first. Callbacks arrive on the main
|
||||
* queue and are fanned out asynchronously like the Android wrapper does.
|
||||
*/
|
||||
export class DeviceInfoProbe {
|
||||
private logs = new Set<(line: string) => void>()
|
||||
private states = new Set<(state: DeviceInfoState, detail: string) => void>()
|
||||
private link = new StockConnection(iosBluetooth(), line => { for (const fn of this.logs) fn(line) })
|
||||
constructor(private right: string, private left = '') {}
|
||||
onLog(fn: (line: string) => void): () => void { this.logs.add(fn); return () => this.logs.delete(fn) }
|
||||
onStateChange(fn: (state: DeviceInfoState, detail: string) => void): () => void { this.states.add(fn); return () => this.states.delete(fn) }
|
||||
async run(): Promise<DeviceInfo> {
|
||||
const snapshots = new Map<string, DeviceInfo>()
|
||||
const parse = (message: p.ProtocolMessage): DeviceInfo => {
|
||||
const values = p.readBytes(message.payload, 4)
|
||||
return { leftVersion: values ? p.readString(values, 5) : '', rightVersion: values ? p.readString(values, 6) : '', extension: p.readString(message.payload, 100) }
|
||||
}
|
||||
const hasVersion = (info: DeviceInfo) => !!(info.leftVersion || info.rightVersion || info.extension)
|
||||
// Some stock builds push settings using their own magic instead of the
|
||||
// query's magic. Keep a valid snapshot as the Android probe does.
|
||||
this.link.onMessage((role, message) => {
|
||||
if (message.sid === p.SID.settings) { const info = parse(message); if (hasVersion(info)) snapshots.set(role, info) }
|
||||
private probe: any = null
|
||||
private listenerProxy: object | null = null
|
||||
private readonly logListeners = new Set<(line: string) => void>()
|
||||
private readonly stateListeners = new Set<(state: DeviceInfoState, detail: string) => void>()
|
||||
private settled = false
|
||||
private cancelled = false
|
||||
private resolveFn: ((info: DeviceInfo) => void) | null = null
|
||||
private rejectFn: ((error: Error) => void) | null = null
|
||||
|
||||
constructor(private readonly rightAddress: string, private readonly leftAddress = '') {}
|
||||
|
||||
onLog(listener: (line: string) => void): () => void {
|
||||
this.logListeners.add(listener)
|
||||
return () => this.logListeners.delete(listener)
|
||||
}
|
||||
|
||||
onStateChange(listener: (state: DeviceInfoState, detail: string) => void): () => void {
|
||||
this.stateListeners.add(listener)
|
||||
return () => this.stateListeners.delete(listener)
|
||||
}
|
||||
|
||||
run(): Promise<DeviceInfo> {
|
||||
return new Promise<DeviceInfo>((resolve, reject) => {
|
||||
this.resolveFn = resolve
|
||||
this.rejectFn = reject
|
||||
void this.start()
|
||||
})
|
||||
}
|
||||
|
||||
private async start(): Promise<void> {
|
||||
let identifiers: Record<string, string>
|
||||
try {
|
||||
await this.link.resolve({ right: this.right, left: this.left, ring: '' })
|
||||
for (const role of ['right', ...(this.left ? ['left'] : [])]) {
|
||||
await this.bringUp(role)
|
||||
}
|
||||
let lastError: unknown
|
||||
for (const role of ['right', ...(this.left ? ['left'] : [])]) {
|
||||
for (const fn of this.states) fn('querying', role)
|
||||
try {
|
||||
if (!this.link.isConnected(role)) await this.bringUp(role)
|
||||
await this.link.request(role, p.SID.launch, p.prelude, 3500, 156)
|
||||
for (let attempt = 0; attempt < 2; attempt++) {
|
||||
try {
|
||||
const message = await this.link.request(role, p.SID.settings, p.settingsQuery)
|
||||
const info = parse(message)
|
||||
if (hasVersion(info)) return info
|
||||
} catch (error) { this.link.check(); lastError = error }
|
||||
if (snapshots.has(role)) return snapshots.get(role)!
|
||||
}
|
||||
} catch (error) { this.link.check(); lastError = error }
|
||||
}
|
||||
throw lastError ?? new Error("Connected, but couldn't read a firmware version.")
|
||||
} finally { this.close() }
|
||||
}
|
||||
private async bringUp(role: string): Promise<void> {
|
||||
for (let attempt = 0; ; attempt++) {
|
||||
try {
|
||||
for (const fn of this.states) fn('connecting', role)
|
||||
await this.link.connect(role)
|
||||
for (const fn of this.states) fn('authenticating', role)
|
||||
// Version reads remain available on older stock builds without an auth ACK.
|
||||
try { await this.link.authenticate(role) } catch (error) { if (!(error instanceof StockTimeout)) throw error }
|
||||
return
|
||||
} catch (error) {
|
||||
this.link.check(); this.link.disconnect(role)
|
||||
if (attempt === 2) throw error
|
||||
await new Promise(resolve => setTimeout(resolve, 1500)); this.link.check()
|
||||
}
|
||||
this.emit(this.stateListeners, 'connecting', 'right')
|
||||
identifiers = await iosBluetooth().resolveDevices({ right: this.rightAddress, left: this.leftAddress, ring: '' })
|
||||
} catch (error) {
|
||||
this.settle(error instanceof Error ? error : new Error(String(error)), null)
|
||||
return
|
||||
}
|
||||
if (this.cancelled) { this.settle(new Error('Cancelled.'), null); return }
|
||||
this.probe = FaceclawKitIosDeviceInfoProbe.alloc().initWithRightAddressLeftAddress(identifiers.right ?? '', identifiers.left ?? '')
|
||||
this.listenerProxy = kotlinListener(FaceclawKitFaceclawDeviceInfoProbeListener, {
|
||||
onLogLine: (line: string) => this.emit(this.logListeners, String(line ?? '')),
|
||||
onStateStateDetail: (state: string, detail: string) =>
|
||||
this.emit(this.stateListeners, String(state) as DeviceInfoState, String(detail ?? '')),
|
||||
onResultLeftVersionRightVersionExtension: (leftVersion: string, rightVersion: string, extension: string) =>
|
||||
this.settle(null, { leftVersion: String(leftVersion ?? ''), rightVersion: String(rightVersion ?? ''), extension: String(extension ?? '') }),
|
||||
onErrorMessage: (message: string) => this.settle(new Error(String(message ?? '')), null),
|
||||
})
|
||||
this.probe.setListenerListener(this.listenerProxy)
|
||||
this.probe.start()
|
||||
}
|
||||
|
||||
cancel(): void {
|
||||
this.cancelled = true
|
||||
try { this.probe?.cancel() } catch { /* ignore */ }
|
||||
}
|
||||
|
||||
close(): void {
|
||||
this.cancelled = true
|
||||
try { this.probe?.close() } catch { /* ignore */ }
|
||||
}
|
||||
|
||||
private settle(error: Error | null, info: DeviceInfo | null): void {
|
||||
if (this.settled) return
|
||||
this.settled = true
|
||||
this.close()
|
||||
if (error) this.rejectFn?.(error)
|
||||
else if (info) this.resolveFn?.(info)
|
||||
}
|
||||
|
||||
private emit<A extends unknown[]>(listeners: Set<(...args: A) => void>, ...args: A): void {
|
||||
const snapshot = Array.from(listeners)
|
||||
setTimeout(() => { for (const listener of snapshot) listener(...args) }, 0)
|
||||
}
|
||||
cancel(): void { this.link.close() }
|
||||
close(): void { this.link.close() }
|
||||
}
|
||||
@@ -0,0 +1,685 @@
|
||||
import type { RingInput } from "../g2/ring-input";
|
||||
import { File, knownFolders, path, type ImageSource } from "@nativescript/core";
|
||||
import { type FirmwareInfo } from "../g2/firmware-compat";
|
||||
import { type CompassEvent } from "./compass-types";
|
||||
import { fromData, toData } from "./kotlin-data";
|
||||
import { previewPixels } from "./ios-graphics";
|
||||
import { iosBluetooth } from "./ios-bluetooth";
|
||||
|
||||
export type { FirmwareInfo };
|
||||
|
||||
// Kotlin/Native facades exported by FaceclawKit (see native/kotlin/shared/src/iosMain/.../ble).
|
||||
declare const FaceclawKitIosGlassesSession: any;
|
||||
declare const FaceclawKitIosProtocolPlatform: any;
|
||||
declare const FaceclawKitFaceclawBleCommunicatorListener: any;
|
||||
declare const FaceclawKitIosAncsListener: any;
|
||||
declare const FaceclawKitIosAudioPacketListener: any;
|
||||
declare const FaceclawKitFaceclawCompassListener: any;
|
||||
|
||||
|
||||
export type CommunicatorPhase =
|
||||
| "disconnected"
|
||||
| "connecting"
|
||||
| "connected"
|
||||
| "charging"
|
||||
| "retrying"
|
||||
| "unpaired"
|
||||
| "disconnecting";
|
||||
|
||||
export type CommunicatorState = {
|
||||
phase: CommunicatorPhase;
|
||||
status: string;
|
||||
};
|
||||
|
||||
export type HeadsetBatteryState = {
|
||||
battery: number;
|
||||
chargingStatus: number;
|
||||
ringBattery?: number;
|
||||
ringChargingStatus?: number;
|
||||
};
|
||||
|
||||
export type FrameMetrics = {
|
||||
paintMs: number;
|
||||
transmitMs: number;
|
||||
tileCount: number;
|
||||
};
|
||||
|
||||
/** See the Android twin: compositor surface configuration in screen pixels. */
|
||||
export type SurfaceOptions = {
|
||||
x: number;
|
||||
y: number;
|
||||
width: number;
|
||||
height: number;
|
||||
zOrder: number;
|
||||
transparency: "opaque" | "color-key";
|
||||
};
|
||||
|
||||
export type RawInputEvent = { ringInput?: RingInput } & (
|
||||
| { kind: "list-click"; containerName: string; eventType: number; eventSource: number; systemExitReasonCode: number; frameId: number }
|
||||
| { kind: "text-click"; containerName: string; eventType: number; eventSource: number; systemExitReasonCode: number; frameId: number }
|
||||
| { kind: "sys-event"; containerName: string; eventType: number; eventSource: number; systemExitReasonCode: number; frameId: number }
|
||||
| { kind: "even-ai"; containerName: string; eventType: number; eventSource: number; systemExitReasonCode: number; frameId: number }
|
||||
| { kind: "watch-gesture"; containerName: string; eventType: number; eventSource: number; systemExitReasonCode: number; frameId: number }
|
||||
| { kind: "display-wake"; containerName: string; eventType: number; eventSource: number; systemExitReasonCode: number; frameId: number }
|
||||
);
|
||||
|
||||
function nonNegativeNumber(value: number): number {
|
||||
const numeric = Number(value);
|
||||
return Number.isFinite(numeric) ? Math.max(0, numeric) : 0;
|
||||
}
|
||||
|
||||
/** A 0..1 brightness factor as the compositor's 0..256 fixed-point form. */
|
||||
export function dimFactor256(factor: number): number {
|
||||
return Math.round(Math.max(0, Math.min(1, factor)) * 256);
|
||||
}
|
||||
|
||||
function timestamp(): string {
|
||||
const now = new Date();
|
||||
const pad = (value: number, width = 2) => String(value).padStart(width, "0");
|
||||
return `${now.getFullYear()}${pad(now.getMonth() + 1)}${pad(now.getDate())}-${pad(now.getHours())}${pad(now.getMinutes())}${pad(now.getSeconds())}-${pad(now.getMilliseconds(), 3)}`;
|
||||
}
|
||||
|
||||
export type PeripheralIdentifiers = { right: string; left: string; ring: string };
|
||||
|
||||
/**
|
||||
* Map the configured MAC addresses to CoreBluetooth identifiers through the scan
|
||||
* layer (app/native/ios-bluetooth.ts): the mapping a previous scan persisted, else a
|
||||
* fresh scan that classifies Even advertisements. The scan layer also owns Bluetooth
|
||||
* readiness (creating the central triggers the permission prompt on first use).
|
||||
*/
|
||||
export async function resolveIosPeripherals(addresses: { right: string; left: string; ring?: string }): Promise<PeripheralIdentifiers> {
|
||||
const resolved = await iosBluetooth().resolveDevices({ right: addresses.right, left: addresses.left, ring: addresses.ring ?? "" });
|
||||
return {
|
||||
right: String(resolved.right ?? "").toUpperCase(),
|
||||
left: String(resolved.left ?? "").toUpperCase(),
|
||||
ring: String(resolved.ring ?? "").toUpperCase(),
|
||||
};
|
||||
}
|
||||
|
||||
/** Bluetooth powered on and authorized (prompts for permission the first time); rejects with the user-facing reason. */
|
||||
export function ensureIosBluetoothReady(): Promise<void> {
|
||||
return iosBluetooth().ensureReady();
|
||||
}
|
||||
|
||||
@NativeClass()
|
||||
class SessionListener extends NSObject {
|
||||
static ObjCProtocols = [FaceclawKitFaceclawBleCommunicatorListener];
|
||||
bridge!: FaceclawCommunicatorBridge;
|
||||
onStateChangePhaseStatus(phase: string, status: string): void { this.bridge.handleStateChange(String(phase ?? ""), String(status ?? "")); }
|
||||
onRingEventKindContainerNameEventTypeEventSourceSystemExitReasonCodeFrameIdRingTickRingTypeRingAuxRingSpeed(
|
||||
kind: string, containerName: string, eventType: number, eventSource: number, systemExitReasonCode: number,
|
||||
frameId: number, ringTick: number, ringType: number, ringAux: number, ringSpeed: number,
|
||||
): void {
|
||||
this.bridge.handleRingEvent(String(kind ?? ""), String(containerName ?? ""), Number(eventType), Number(eventSource),
|
||||
Number(systemExitReasonCode), Number(frameId), Number(ringTick), Number(ringType), Number(ringAux), Number(ringSpeed));
|
||||
}
|
||||
onBatteryStateHeadsetBatteryHeadsetChargingRingBatteryRingCharging(headsetBattery: number, headsetCharging: number, ringBattery: number, ringCharging: number): void {
|
||||
this.bridge.handleBatteryState(Number(headsetBattery), Number(headsetCharging), Number(ringBattery), Number(ringCharging));
|
||||
}
|
||||
onSilentModeSilent(silent: boolean): void { this.bridge.handleSilentMode(Boolean(silent)); }
|
||||
onWearStateWearing(wearing: boolean): void { this.bridge.handleWearState(Boolean(wearing)); }
|
||||
onPhoneLockStateLocked(locked: boolean): void { this.bridge.handlePhoneLockState(Boolean(locked)); }
|
||||
onEvenAppConflictMessage(message: string): void { this.bridge.handleEvenAppConflict(String(message ?? "")); }
|
||||
onFrameMetricsPaintMsTransmitMsTileCount(paintMs: number, transmitMs: number, tileCount: number): void {
|
||||
this.bridge.handleFrameMetrics(Number(paintMs), Number(transmitMs), Number(tileCount));
|
||||
}
|
||||
onFrameFinishedFrameIdOutcome(frameId: number, outcome: string): void { this.bridge.handleFrameFinished(Number(frameId), String(outcome ?? "")); }
|
||||
onFirmwareInfoLeftVersionRightVersionExtension(leftVersion: string, rightVersion: string, extension: string): void {
|
||||
this.bridge.handleFirmwareInfo(String(leftVersion ?? ""), String(rightVersion ?? ""), String(extension ?? ""));
|
||||
}
|
||||
}
|
||||
|
||||
@NativeClass()
|
||||
class AncsListener extends NSObject {
|
||||
static ObjCProtocols = [FaceclawKitIosAncsListener];
|
||||
bridge!: FaceclawCommunicatorBridge;
|
||||
onAncsAuthorizationAuthorized(authorized: boolean): void { this.bridge.handleAncsAuthorization(Boolean(authorized)); }
|
||||
onRelayFrameData(data: NSData): void { this.bridge.handleAncsRelayFrame(fromData(data)); }
|
||||
}
|
||||
|
||||
@NativeClass()
|
||||
class AudioPacketListener extends NSObject {
|
||||
static ObjCProtocols = [FaceclawKitIosAudioPacketListener];
|
||||
handler!: (packet: Uint8Array, arm: string, arrivalMs: number) => void;
|
||||
onAudioPacketDataArmArrivalMs(data: NSData, arm: string, arrivalMs: number): void {
|
||||
this.handler(fromData(data), String(arm ?? ""), Number(arrivalMs));
|
||||
}
|
||||
}
|
||||
|
||||
@NativeClass()
|
||||
class CompassListener extends NSObject {
|
||||
static ObjCProtocols = [FaceclawKitFaceclawCompassListener];
|
||||
handler!: (event: CompassEvent) => void;
|
||||
onCompassEventCommandHeadingDegreesMagneticAccuracyMagneticAnomaliesOrientationSourceDiagnosticFlagsSampleTimeMs(
|
||||
command: number, headingDegrees: number, magneticAccuracy: number, magneticAnomalies: number,
|
||||
orientationSource: number, diagnosticFlags: number, sampleTimeMs: number,
|
||||
): void {
|
||||
this.handler({
|
||||
command: Number(command), headingDegrees: Number(headingDegrees),
|
||||
diagnostics: diagnosticFlags >= 0 ? {
|
||||
magneticAccuracy: Number(magneticAccuracy), magneticAnomalies: Number(magneticAnomalies),
|
||||
orientationSource: Number(orientationSource), flags: Number(diagnosticFlags), sampleTimeMs: Number(sampleTimeMs),
|
||||
} : undefined,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The iOS twin of the Android communicator bridge: the same public API over
|
||||
* the shared Kotlin session (FaceclawKitIosGlassesSession, CoreBluetooth
|
||||
* underneath). Addresses are CoreBluetooth identifiers (resolveIosPeripherals
|
||||
* maps the configured MACs). Every Kotlin callback already arrives on the
|
||||
* main queue; listener fan-out is still deferred with setTimeout like on
|
||||
* Android so callers never re-enter the bridge from a Kotlin frame.
|
||||
*/
|
||||
export class FaceclawCommunicatorBridge {
|
||||
private readonly communicator: any;
|
||||
private readonly listenerProxy: SessionListener;
|
||||
private ancsProxy: AncsListener | null = null;
|
||||
private nativeCallQueue: Promise<void> = Promise.resolve();
|
||||
private queuedNativeCalls = 0;
|
||||
private readonly frameMetricWaiters = new Set<(metrics: FrameMetrics) => void>();
|
||||
private readonly finishedFrameOutcomes = new Map<number, string>();
|
||||
private readonly frameFinishedWaiters = new Map<number, Set<(outcome: string) => void>>();
|
||||
private readonly stateListeners = new Set<(state: CommunicatorState) => void>();
|
||||
private readonly ringListeners = new Set<(event: RawInputEvent) => void>();
|
||||
private readonly batteryListeners = new Set<(state: HeadsetBatteryState) => void>();
|
||||
private readonly silentModeListeners = new Set<(silent: boolean) => void>();
|
||||
private readonly wearStateListeners = new Set<(wearing: boolean) => void>();
|
||||
private readonly phoneLockStateListeners = new Set<(locked: boolean) => void>();
|
||||
private latestWearState: boolean | null = null;
|
||||
private latestPhoneLockState: boolean | null = null;
|
||||
private readonly evenAppConflictListeners = new Set<(message: string) => void>();
|
||||
private readonly frameMetricsListeners = new Set<(metrics: FrameMetrics) => void>();
|
||||
private readonly firmwareInfoListeners = new Set<(info: FirmwareInfo) => void>();
|
||||
private readonly ancsAuthorizationListeners = new Set<(authorized: boolean) => void>();
|
||||
private readonly ancsRelayListeners = new Set<(frame: Uint8Array) => void>();
|
||||
private audioListener: AudioPacketListener | null = null;
|
||||
private microphoneWork: Promise<void> = Promise.resolve();
|
||||
private microphoneToken = 0;
|
||||
|
||||
constructor(addresses: { right: string; left: string; ring?: string }) {
|
||||
this.communicator = FaceclawKitIosGlassesSession.alloc().initWithRightAddressLeftAddressRingAddress(
|
||||
String(addresses.right ?? "").toUpperCase(),
|
||||
String(addresses.left ?? "").toUpperCase(),
|
||||
String(addresses.ring ?? "").toUpperCase(),
|
||||
);
|
||||
this.listenerProxy = SessionListener.new() as SessionListener;
|
||||
this.listenerProxy.bridge = this;
|
||||
this.communicator.setListenerListener(this.listenerProxy);
|
||||
}
|
||||
|
||||
// ----- Kotlin listener entry points (main queue) -----------------------------------
|
||||
|
||||
/** @internal */
|
||||
handleStateChange(phase: string, status: string): void {
|
||||
this.emitAsync(this.stateListeners, { phase: phase as CommunicatorPhase, status });
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
handleRingEvent(kind: string, containerName: string, eventType: number, eventSource: number, systemExitReasonCode: number,
|
||||
frameId: number, ringTick: number, ringType: number, ringAux: number, ringSpeed: number): void {
|
||||
const event = {
|
||||
kind: kind as RawInputEvent["kind"], containerName, eventType, eventSource, systemExitReasonCode, frameId,
|
||||
ringInput: ringTick >= 0 ? { tick: ringTick, type: ringType, aux: ringAux, speed: ringSpeed } : undefined,
|
||||
} as RawInputEvent;
|
||||
this.emitAsync(this.ringListeners, event);
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
handleBatteryState(battery: number, chargingStatus: number, ringBattery: number, ringChargingStatus: number): void {
|
||||
this.emitAsync(this.batteryListeners, { battery, chargingStatus, ringBattery, ringChargingStatus });
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
handleSilentMode(silent: boolean): void { this.emitAsync(this.silentModeListeners, silent); }
|
||||
|
||||
/** @internal */
|
||||
handleWearState(wearing: boolean): void {
|
||||
this.latestWearState = wearing;
|
||||
this.emitAsync(this.wearStateListeners, wearing);
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
handlePhoneLockState(locked: boolean): void {
|
||||
this.latestPhoneLockState = locked;
|
||||
this.emitAsync(this.phoneLockStateListeners, locked);
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
handleEvenAppConflict(message: string): void { this.emitAsync(this.evenAppConflictListeners, message); }
|
||||
|
||||
/** @internal */
|
||||
handleFrameMetrics(paintMs: number, transmitMs: number, tileCount: number): void {
|
||||
const metrics = { paintMs: nonNegativeNumber(paintMs), transmitMs: nonNegativeNumber(transmitMs), tileCount: nonNegativeNumber(tileCount) };
|
||||
const waiters = Array.from(this.frameMetricWaiters);
|
||||
this.frameMetricWaiters.clear();
|
||||
for (const waiter of waiters) setTimeout(() => waiter(metrics), 0);
|
||||
this.emitAsync(this.frameMetricsListeners, metrics);
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
handleFrameFinished(frameId: number, outcome: string): void { this.recordFrameFinished(frameId, outcome); }
|
||||
|
||||
/** @internal */
|
||||
handleFirmwareInfo(leftVersion: string, rightVersion: string, extension: string): void {
|
||||
this.emitAsync(this.firmwareInfoListeners, { leftVersion, rightVersion, extension });
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
handleAncsAuthorization(authorized: boolean): void { this.emitAsync(this.ancsAuthorizationListeners, authorized); }
|
||||
|
||||
/** @internal */
|
||||
handleAncsRelayFrame(frame: Uint8Array): void { this.emitAsync(this.ancsRelayListeners, frame); }
|
||||
|
||||
private emitAsync<T>(listeners: Set<(value: T) => void>, value: T): void {
|
||||
const snapshot = Array.from(listeners);
|
||||
setTimeout(() => {
|
||||
for (const listener of snapshot) listener(value);
|
||||
}, 0);
|
||||
}
|
||||
|
||||
/** Same FIFO as the Android bridge: native calls stay ordered; the fast path skips a task hop when idle. */
|
||||
private enqueueNativeCall<T>(operation: () => T, inlineWhenIdle = false): Promise<T> {
|
||||
if (inlineWhenIdle && this.queuedNativeCalls === 0) {
|
||||
try {
|
||||
return Promise.resolve(operation());
|
||||
} catch (error) {
|
||||
return Promise.reject(error);
|
||||
}
|
||||
}
|
||||
this.queuedNativeCalls++;
|
||||
const run = () =>
|
||||
new Promise<T>((resolve, reject) => {
|
||||
setTimeout(() => {
|
||||
this.queuedNativeCalls--;
|
||||
try {
|
||||
resolve(operation());
|
||||
} catch (error) {
|
||||
reject(error);
|
||||
}
|
||||
}, 0);
|
||||
});
|
||||
const result = this.nativeCallQueue.then(run, run);
|
||||
this.nativeCallQueue = result.then(() => undefined, () => undefined);
|
||||
return result;
|
||||
}
|
||||
|
||||
// ----- listeners -------------------------------------------------------------------
|
||||
|
||||
onStateChange(listener: (state: CommunicatorState) => void): () => void {
|
||||
this.stateListeners.add(listener);
|
||||
return () => this.stateListeners.delete(listener);
|
||||
}
|
||||
|
||||
onRingEvent(listener: (event: RawInputEvent) => void): () => void {
|
||||
this.ringListeners.add(listener);
|
||||
return () => this.ringListeners.delete(listener);
|
||||
}
|
||||
|
||||
onBatteryState(listener: (state: HeadsetBatteryState) => void): () => void {
|
||||
this.batteryListeners.add(listener);
|
||||
return () => this.batteryListeners.delete(listener);
|
||||
}
|
||||
|
||||
onSilentMode(listener: (silent: boolean) => void): () => void {
|
||||
this.silentModeListeners.add(listener);
|
||||
return () => this.silentModeListeners.delete(listener);
|
||||
}
|
||||
|
||||
onWearState(listener: (wearing: boolean) => void): () => void {
|
||||
this.wearStateListeners.add(listener);
|
||||
if (this.latestWearState !== null) {
|
||||
const wearing = this.latestWearState;
|
||||
setTimeout(() => { if (this.wearStateListeners.has(listener)) listener(wearing); }, 0);
|
||||
}
|
||||
return () => this.wearStateListeners.delete(listener);
|
||||
}
|
||||
|
||||
onPhoneLockState(listener: (locked: boolean) => void): () => void {
|
||||
this.phoneLockStateListeners.add(listener);
|
||||
if (this.latestPhoneLockState !== null) {
|
||||
const locked = this.latestPhoneLockState;
|
||||
setTimeout(() => { if (this.phoneLockStateListeners.has(listener)) listener(locked); }, 0);
|
||||
}
|
||||
return () => this.phoneLockStateListeners.delete(listener);
|
||||
}
|
||||
|
||||
onEvenAppConflict(listener: (message: string) => void): () => void {
|
||||
this.evenAppConflictListeners.add(listener);
|
||||
return () => this.evenAppConflictListeners.delete(listener);
|
||||
}
|
||||
|
||||
onFrameMetrics(listener: (metrics: FrameMetrics) => void): () => void {
|
||||
this.frameMetricsListeners.add(listener);
|
||||
return () => this.frameMetricsListeners.delete(listener);
|
||||
}
|
||||
|
||||
onFirmwareInfo(listener: (info: FirmwareInfo) => void): () => void {
|
||||
this.firmwareInfoListeners.add(listener);
|
||||
return () => this.firmwareInfoListeners.delete(listener);
|
||||
}
|
||||
|
||||
getNativeCommunicator(): any {
|
||||
return this.communicator;
|
||||
}
|
||||
|
||||
private recordFrameFinished(frameId: number, outcome: string): void {
|
||||
if (!Number.isFinite(frameId) || frameId <= 0) return;
|
||||
this.finishedFrameOutcomes.set(frameId, outcome);
|
||||
while (this.finishedFrameOutcomes.size > 128) {
|
||||
const oldest = this.finishedFrameOutcomes.keys().next().value;
|
||||
if (oldest === undefined) break;
|
||||
this.finishedFrameOutcomes.delete(oldest);
|
||||
}
|
||||
const waiters = this.frameFinishedWaiters.get(frameId);
|
||||
if (waiters) {
|
||||
this.frameFinishedWaiters.delete(frameId);
|
||||
for (const waiter of waiters) setTimeout(() => waiter(outcome), 0);
|
||||
}
|
||||
}
|
||||
|
||||
waitForFrameFinished(frameId: number, timeoutMs: number): Promise<string | null> {
|
||||
const known = this.finishedFrameOutcomes.get(frameId);
|
||||
if (known !== undefined) return Promise.resolve(known);
|
||||
if (frameId <= 0) return Promise.resolve(null);
|
||||
const delayMs = Math.max(1, Math.round(nonNegativeNumber(timeoutMs)));
|
||||
return new Promise((resolve) => {
|
||||
let settled = false;
|
||||
const onFinished = (outcome: string) => {
|
||||
if (settled) return;
|
||||
settled = true;
|
||||
clearTimeout(timeoutHandle);
|
||||
resolve(outcome);
|
||||
};
|
||||
let waiters = this.frameFinishedWaiters.get(frameId);
|
||||
if (!waiters) {
|
||||
waiters = new Set();
|
||||
this.frameFinishedWaiters.set(frameId, waiters);
|
||||
}
|
||||
waiters.add(onFinished);
|
||||
const timeoutHandle = setTimeout(() => {
|
||||
if (settled) return;
|
||||
settled = true;
|
||||
const pending = this.frameFinishedWaiters.get(frameId);
|
||||
if (pending) {
|
||||
pending.delete(onFinished);
|
||||
if (pending.size === 0) this.frameFinishedWaiters.delete(frameId);
|
||||
}
|
||||
resolve(null);
|
||||
}, delayMs);
|
||||
});
|
||||
}
|
||||
|
||||
waitForNextFrameMetrics(timeoutMs: number): Promise<FrameMetrics | null> {
|
||||
const delayMs = Math.max(1, Math.round(nonNegativeNumber(timeoutMs)));
|
||||
return new Promise((resolve) => {
|
||||
let settled = false;
|
||||
let timeoutHandle: ReturnType<typeof setTimeout> | null = null;
|
||||
const complete = (metrics: FrameMetrics | null) => {
|
||||
if (settled) return;
|
||||
settled = true;
|
||||
if (timeoutHandle !== null) clearTimeout(timeoutHandle);
|
||||
this.frameMetricWaiters.delete(onMetrics);
|
||||
resolve(metrics);
|
||||
};
|
||||
const onMetrics = (metrics: FrameMetrics) => complete(metrics);
|
||||
this.frameMetricWaiters.add(onMetrics);
|
||||
timeoutHandle = setTimeout(() => complete(null), delayMs);
|
||||
});
|
||||
}
|
||||
|
||||
// ----- session ---------------------------------------------------------------------
|
||||
|
||||
async start(): Promise<void> {
|
||||
await this.enqueueNativeCall(() => { this.communicator.start(); });
|
||||
}
|
||||
|
||||
async setG2ScreenOn(screenOn: boolean): Promise<void> {
|
||||
await this.enqueueNativeCall(() => this.communicator.setG2ScreenOnScreenOn(Boolean(screenOn)));
|
||||
}
|
||||
|
||||
async setFirmwareDebugFlags(enabled: boolean): Promise<void> {
|
||||
await this.enqueueNativeCall(() => this.communicator.setFirmwareDebugFlagsEnabled(Boolean(enabled)));
|
||||
}
|
||||
|
||||
/** Set lens brightness: Faceclaw auto or a fixed level (clamped to 2–100). */
|
||||
async setBrightness(autoAdjust: boolean, level: number): Promise<void> {
|
||||
await this.enqueueNativeCall(() => this.communicator.setBrightnessAutoAdjustBrightnessLevel(Boolean(autoAdjust), Math.round(level)));
|
||||
}
|
||||
|
||||
async configureBrightness(p: { auto: boolean; level: number; minimum: number; maximum: number; curve: string; fadeMs: number }): Promise<void> {
|
||||
await this.enqueueNativeCall(() => this.communicator.configureBrightnessAutoLevelMinimumMaximumCurveFadeMs(p.auto, p.level, p.minimum, p.maximum, p.curve, p.fadeMs));
|
||||
}
|
||||
|
||||
async enableWearDetectionAndRequestState(): Promise<void> {
|
||||
await this.enqueueNativeCall(() => this.communicator.enableWearDetectionAndRequestState());
|
||||
}
|
||||
|
||||
isSessionReady(): boolean {
|
||||
return Boolean(this.communicator.isSessionReady());
|
||||
}
|
||||
|
||||
/** The platform's lock/screen signal (the iOS host also observes protected data itself). */
|
||||
onPhoneLockSignal(): void {
|
||||
this.communicator.onPhoneLockSignal();
|
||||
}
|
||||
|
||||
// ----- compositor ------------------------------------------------------------------
|
||||
|
||||
async configureCompositorScreen(width: number, height: number): Promise<void> {
|
||||
await this.enqueueNativeCall(() => {
|
||||
this.communicator.configureCompositorScreenWidthHeight(Math.round(width), Math.round(height));
|
||||
});
|
||||
}
|
||||
|
||||
/** Phone-UI preview of the current composited screen, or null if none yet. */
|
||||
getCompositePreview(green = false): ImageSource | null {
|
||||
const frame = this.communicator.previewComposite();
|
||||
if (!frame) return null;
|
||||
const width = Number(this.communicator.compositeWidth()), height = Number(this.communicator.compositeHeight());
|
||||
if (width <= 0 || height <= 0) return null;
|
||||
return previewPixels(fromData(frame.pixels), width, height, green);
|
||||
}
|
||||
|
||||
/** Save the current composite as a 4-bit grayscale PNG under Documents; returns the path (empty if none). Cropping is not supported on iOS. */
|
||||
saveScreenshot(crop?: { x: number; y: number; width: number; height: number }): string {
|
||||
if (crop) return "";
|
||||
const png = this.communicator.screenshotPng();
|
||||
if (!png) return "";
|
||||
const target = path.join(knownFolders.documents().path, `screen-${timestamp()}.png`);
|
||||
File.fromPath(target).writeSync(png);
|
||||
return target;
|
||||
}
|
||||
|
||||
/** Screen recording is not available on iOS yet. */
|
||||
startScreenRecording(): void {}
|
||||
|
||||
recordScreenFrame(): void {}
|
||||
|
||||
stopScreenRecording(): string { return ""; }
|
||||
|
||||
async configureSurface(id: string, options: SurfaceOptions): Promise<void> {
|
||||
const transparency = options.transparency === "color-key" ? 1 : 0;
|
||||
await this.enqueueNativeCall(() => {
|
||||
this.communicator.configureSurfaceIdXYWidthHeightZOrderTransparency(
|
||||
id, Math.round(options.x), Math.round(options.y), Math.round(options.width), Math.round(options.height),
|
||||
Math.round(options.zOrder), transparency,
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
async removeSurface(id: string): Promise<void> {
|
||||
await this.enqueueNativeCall(() => { this.communicator.removeSurfaceId(id); });
|
||||
}
|
||||
|
||||
async submitShellScene(bytes: Uint8Array, paintMs = 0, frameId = 0): Promise<void> {
|
||||
const snapshot = new Uint8Array(bytes);
|
||||
await this.enqueueNativeCall(() => this.communicator.submitShellSceneBytesPaintMsFrameId(toData(snapshot), Math.round(nonNegativeNumber(paintMs)), Math.round(nonNegativeNumber(frameId))), true);
|
||||
}
|
||||
|
||||
async setUnderlayDim(belowZOrder: number, factor: number): Promise<void> {
|
||||
await this.enqueueNativeCall(() => {
|
||||
this.communicator.setUnderlayDimBelowZOrderFactor256(Math.round(belowZOrder), dimFactor256(factor));
|
||||
});
|
||||
}
|
||||
|
||||
async setSurfaceVisible(id: string, visible: boolean): Promise<void> {
|
||||
await this.enqueueNativeCall(() => { this.communicator.setSurfaceVisibleIdVisible(id, Boolean(visible)); });
|
||||
}
|
||||
|
||||
async setSurfaceDepth(id: string, depth: number): Promise<void> {
|
||||
await this.enqueueNativeCall(() => { this.communicator.setSurfaceDepthIdDepth(id, Math.round(depth)); });
|
||||
}
|
||||
|
||||
async setScreenBlanked(blanked: boolean): Promise<void> {
|
||||
await this.enqueueNativeCall(() => { this.communicator.setScreenBlankedBlanked(Boolean(blanked)); });
|
||||
}
|
||||
|
||||
async submitSurfaceFrame(
|
||||
surfaceId: string,
|
||||
pixels8bpp: Uint8Array,
|
||||
rect: { x: number; y: number; width: number; height: number },
|
||||
fingerprint: string,
|
||||
paintMs = -1,
|
||||
frameId = 0,
|
||||
glyphs: ArrayBuffer | null = null,
|
||||
): Promise<void> {
|
||||
const snapshot = new Uint8Array(pixels8bpp);
|
||||
const draws = glyphs ? toData(new Uint8Array(glyphs)) : null;
|
||||
await this.enqueueNativeCall(() => {
|
||||
this.communicator.submitSurfaceFramePixels8bppSurfaceIdRectXRectYRectWidthRectHeightContentFingerprintPaintMsFrameIdGlyphs(
|
||||
toData(snapshot), surfaceId, Math.round(rect.x), Math.round(rect.y), Math.round(rect.width), Math.round(rect.height),
|
||||
fingerprint, Math.round(nonNegativeNumber(paintMs)), Math.round(nonNegativeNumber(frameId)), draws,
|
||||
);
|
||||
}, true);
|
||||
}
|
||||
|
||||
// ----- controls --------------------------------------------------------------------
|
||||
|
||||
async disconnect(): Promise<void> {
|
||||
await this.enqueueNativeCall(() => this.communicator.disconnect());
|
||||
}
|
||||
|
||||
async sendShutdown(exitMode = 0): Promise<boolean> {
|
||||
return this.enqueueNativeCall(() => Boolean(this.communicator.sendShutdownExitMode(exitMode)));
|
||||
}
|
||||
|
||||
async sendCfwCleanup(): Promise<boolean> {
|
||||
return this.enqueueNativeCall(() => Boolean(this.communicator.sendCfwCleanup()));
|
||||
}
|
||||
|
||||
async suspendEvenHubSession(): Promise<boolean> {
|
||||
return this.enqueueNativeCall(() => Boolean(this.communicator.suspendEvenHubSession()));
|
||||
}
|
||||
|
||||
async resumeEvenHubSession(): Promise<boolean> {
|
||||
return this.enqueueNativeCall(() => Boolean(this.communicator.resumeEvenHubSession()));
|
||||
}
|
||||
|
||||
async setFaceclawWakeLeaseEnabled(enabled: boolean): Promise<boolean> {
|
||||
return this.enqueueNativeCall(() => Boolean(this.communicator.setFaceclawWakeLeaseEnabledEnabled(Boolean(enabled))));
|
||||
}
|
||||
|
||||
async awaitEvenHubSessionReady(timeoutMs: number): Promise<boolean> {
|
||||
return this.enqueueNativeCall(() => Boolean(this.communicator.awaitEvenHubSessionReadyTimeoutMs(Math.round(nonNegativeNumber(timeoutMs)))));
|
||||
}
|
||||
|
||||
/** Play a CFW mode-5 kind-4 tone sequence (complete wire payload). */
|
||||
async playBuzzerSequence(payload: Uint8Array): Promise<void> {
|
||||
const snapshot = new Uint8Array(payload);
|
||||
await this.enqueueNativeCall(() => { this.communicator.playBuzzerSequencePayload(toData(snapshot)); });
|
||||
}
|
||||
|
||||
async close(): Promise<void> {
|
||||
this.setAncsListeners(false);
|
||||
await this.enqueueNativeCall(() => this.communicator.close());
|
||||
}
|
||||
|
||||
// ----- iOS extras: compass, microphone, iPhone notifications (ANCS) ----------------------
|
||||
|
||||
/** CFW mode-10 compass on behalf of `owner`; the magnetometer runs while any owner wants it. */
|
||||
setCompassEnabled(enabled: boolean, owner = "compass"): void {
|
||||
this.communicator.setCompassEnabledForOwnerOwnerEnable(owner, Boolean(enabled));
|
||||
}
|
||||
|
||||
/** Compass heading/calibration events, delivered on the main queue. */
|
||||
addCompassListener(listener: (event: CompassEvent) => void): () => void {
|
||||
const proxy = CompassListener.new() as CompassListener;
|
||||
proxy.handler = listener;
|
||||
this.communicator.addCompassListenerListener(proxy);
|
||||
return () => { this.communicator.removeCompassListenerListener(proxy); };
|
||||
}
|
||||
|
||||
/**
|
||||
* Serialized glasses microphone enable/disable (the voice bridge's
|
||||
* IosMicrophoneSession contract). A release while an enable awaits its ACK
|
||||
* still wins: the token check drops the stale packets.
|
||||
*/
|
||||
setMicrophone(enabled: boolean, listener?: (packet: Uint8Array) => void): Promise<void> {
|
||||
const token = ++this.microphoneToken;
|
||||
const work = this.microphoneWork.catch(() => {}).then(async () => {
|
||||
if (token !== this.microphoneToken) return;
|
||||
if (!enabled) {
|
||||
this.audioListener = null;
|
||||
await this.enqueueNativeCall(() => this.communicator.stopG2AudioCapture());
|
||||
return;
|
||||
}
|
||||
if (!this.isSessionReady()) throw new Error("Connect the glasses before starting voice input.");
|
||||
const proxy = AudioPacketListener.new() as AudioPacketListener;
|
||||
proxy.handler = (packet) => { if (token === this.microphoneToken && listener) listener(packet); };
|
||||
this.audioListener = proxy;
|
||||
const started = await this.enqueueNativeCall(() => Boolean(this.communicator.startG2AudioCaptureNsDataListener(proxy)));
|
||||
if (!started) {
|
||||
this.audioListener = null;
|
||||
throw new Error("The glasses did not enable their microphone. Reconnect and try again.");
|
||||
}
|
||||
});
|
||||
this.microphoneWork = work;
|
||||
return work;
|
||||
}
|
||||
|
||||
/** Ask CoreBluetooth for ANCS on the right arm's next connection (call before start()). */
|
||||
setRequiresAncs(required: boolean): void {
|
||||
this.communicator.setRequiresAncsRequired(Boolean(required));
|
||||
this.setAncsListeners(required);
|
||||
}
|
||||
|
||||
isAncsAuthorized(): boolean {
|
||||
return Boolean(this.communicator.isAncsAuthorized());
|
||||
}
|
||||
|
||||
onAncsAuthorization(listener: (authorized: boolean) => void): () => void {
|
||||
this.ancsAuthorizationListeners.add(listener);
|
||||
return () => this.ancsAuthorizationListeners.delete(listener);
|
||||
}
|
||||
|
||||
/** Relay frames ('A','N',...) received on the right arm, for the ANCS client. */
|
||||
onAncsRelayFrame(listener: (frame: Uint8Array) => void): () => void {
|
||||
this.ancsRelayListeners.add(listener);
|
||||
return () => this.ancsRelayListeners.delete(listener);
|
||||
}
|
||||
|
||||
/** Queue one relay packet for the right arm; the Kotlin side writes it off the JS thread. */
|
||||
async writeRawToRight(packet: Uint8Array): Promise<void> {
|
||||
if (!this.isSessionReady()) throw new Error("Glasses are not connected.");
|
||||
const snapshot = new Uint8Array(packet);
|
||||
this.communicator.writeRawToRightPacket(toData(snapshot));
|
||||
}
|
||||
|
||||
/** The right arm's usable write payload for relay packets (20 until connected). */
|
||||
rightWriteLimit(): number {
|
||||
return Number(this.communicator.rightWriteLimit()) || 20;
|
||||
}
|
||||
|
||||
private setAncsListeners(install: boolean): void {
|
||||
if (install && !this.ancsProxy) {
|
||||
this.ancsProxy = AncsListener.new() as AncsListener;
|
||||
this.ancsProxy.bridge = this;
|
||||
this.communicator.setAncsListenerListener(this.ancsProxy);
|
||||
} else if (!install && this.ancsProxy) {
|
||||
this.communicator.setAncsListenerListener(null);
|
||||
this.ancsProxy = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -137,7 +137,6 @@ export class FaceclawCommunicatorBridge {
|
||||
// does not race against finishes that land before the wait starts.
|
||||
private readonly finishedFrameOutcomes = new Map<number, string>();
|
||||
private readonly frameFinishedWaiters = new Map<number, Set<(outcome: string) => void>>();
|
||||
private readonly logListeners = new Set<(line: string) => void>();
|
||||
private readonly stateListeners = new Set<(state: CommunicatorState) => void>();
|
||||
private readonly ringListeners = new Set<(event: RawInputEvent) => void>();
|
||||
private readonly batteryListeners = new Set<(state: HeadsetBatteryState) => void>();
|
||||
@@ -161,9 +160,6 @@ export class FaceclawCommunicatorBridge {
|
||||
addresses.ring ?? "",
|
||||
);
|
||||
this.listenerProxy = new com.faceclaw.app.FaceclawBleCommunicatorListener({
|
||||
onLog: (line: string) => {
|
||||
this.emitAsync(this.logListeners, String(line));
|
||||
},
|
||||
onStateChange: (phase: string, status: string) => {
|
||||
const state = {
|
||||
phase: String(phase) as CommunicatorPhase,
|
||||
@@ -293,11 +289,6 @@ export class FaceclawCommunicatorBridge {
|
||||
return result;
|
||||
}
|
||||
|
||||
onLog(listener: (line: string) => void): () => void {
|
||||
this.logListeners.add(listener);
|
||||
return () => this.logListeners.delete(listener);
|
||||
}
|
||||
|
||||
onStateChange(listener: (state: CommunicatorState) => void): () => void {
|
||||
this.stateListeners.add(listener);
|
||||
return () => this.stateListeners.delete(listener);
|
||||
@@ -451,11 +442,15 @@ export class FaceclawCommunicatorBridge {
|
||||
await this.enqueueJavaCall(() => this.communicator.setFirmwareDebugFlags(Boolean(enabled)));
|
||||
}
|
||||
|
||||
/** Set lens brightness: auto (ambient sensor) or an explicit 0-100 level. */
|
||||
/** Set lens brightness: Faceclaw auto or a fixed level (clamped to 2–100). */
|
||||
async setBrightness(autoAdjust: boolean, level: number): Promise<void> {
|
||||
await this.enqueueJavaCall(() => this.communicator.setBrightness(Boolean(autoAdjust), Math.round(level)));
|
||||
}
|
||||
|
||||
async configureBrightness(p: { auto: boolean; level: number; minimum: number; maximum: number; curve: string; fadeMs: number }): Promise<void> {
|
||||
await this.enqueueJavaCall(() => this.communicator.configureBrightness(p.auto, p.level, p.minimum, p.maximum, p.curve, p.fadeMs));
|
||||
}
|
||||
|
||||
async enableWearDetectionAndRequestState(): Promise<void> {
|
||||
await this.enqueueJavaCall(() => this.communicator.enableWearDetectionAndRequestState());
|
||||
}
|
||||
@@ -541,6 +536,11 @@ export class FaceclawCommunicatorBridge {
|
||||
* next composite. How a shell overlay's Layer.dimUnderneath reaches the
|
||||
* window surfaces beneath the shell surface.
|
||||
*/
|
||||
async submitShellScene(bytes: Uint8Array, paintMs = 0, frameId = 0): Promise<void> {
|
||||
const snapshot = new Uint8Array(bytes);
|
||||
await this.enqueueJavaCall(() => this.communicator.submitShellScene(snapshot.buffer, paintMs, frameId), true);
|
||||
}
|
||||
|
||||
async setUnderlayDim(belowZOrder: number, factor: number): Promise<void> {
|
||||
await this.enqueueJavaCall(() => {
|
||||
this.communicator.setUnderlayDim(Math.round(belowZOrder), dimFactor256(factor));
|
||||
@@ -554,6 +554,16 @@ export class FaceclawCommunicatorBridge {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Stereo depth for a surface, applied while it is the topmost visible
|
||||
* surface and opaquely covers the screen; takes effect at its next frame.
|
||||
*/
|
||||
async setSurfaceDepth(id: string, depth: number): Promise<void> {
|
||||
await this.enqueueJavaCall(() => {
|
||||
this.communicator.setSurfaceDepth(id, Math.round(depth));
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Blank (screen off) or unblank the composited output; retained surface
|
||||
* state survives, so unblanking restores the screen without repaints.
|
||||
|
||||
@@ -1,83 +1,124 @@
|
||||
import { Application } from '@nativescript/core'
|
||||
import { StockConnection } from '../g2/stock-connection'
|
||||
import { flashLensImage, validateFirmware } from '../g2/firmware-ota'
|
||||
import { CFW_PATCH_SET } from '../g2/firmware/cfw-patches'
|
||||
import { firmwareSha256, readFirmwareFile } from './firmware-files.ios'
|
||||
import { iosBluetooth } from './ios-bluetooth'
|
||||
import type { FlashState, FlashProgress } from './firmware-flasher'
|
||||
export type { FlashState, FlashProgress } from './firmware-flasher'
|
||||
import { kotlinListener } from './kotlin-listener.ios'
|
||||
import type { FlashState, FlashProgress } from '../g2/firmware-types'
|
||||
export type { FlashState, FlashProgress } from '../g2/firmware-types'
|
||||
|
||||
declare const FaceclawKitIosFirmwareFlasher: any, FaceclawKitFaceclawFirmwareFlasherListener: any
|
||||
|
||||
/**
|
||||
* iOS twin of the Android FirmwareFlasher wrapper. The OTA transfer itself is the shared
|
||||
* Kotlin OtaFlashFlow over a CoreBluetooth central (FaceclawKitIosFirmwareFlasher); this
|
||||
* wrapper keeps the iOS-only policy around it: the image must still hash to the prepared
|
||||
* custom (or pinned stock) firmware, the phone must stay in the foreground with the screen
|
||||
* awake, and MAC addresses are mapped to peripheral identifiers first.
|
||||
*/
|
||||
export class FirmwareFlasher {
|
||||
private logs = new Set<(line: string) => void>()
|
||||
private states = new Set<(state: FlashState, detail: string) => void>()
|
||||
private progresses = new Set<(value: FlashProgress) => void>()
|
||||
private completes = new Set<(success: boolean, detail: string) => void>()
|
||||
private link = new StockConnection(iosBluetooth(), line => this.log(line))
|
||||
private flasher: any = null
|
||||
private listenerProxy: object | null = null
|
||||
private readonly logListeners = new Set<(line: string) => void>()
|
||||
private readonly progressListeners = new Set<(progress: FlashProgress) => void>()
|
||||
private readonly stateListeners = new Set<(state: FlashState, detail: string) => void>()
|
||||
private readonly completeListeners = new Set<(success: boolean, detail: string) => void>()
|
||||
private started = false
|
||||
private finished = false
|
||||
private cancelled = false
|
||||
private sleeps = new Set<() => void>()
|
||||
private foregroundError = ''
|
||||
constructor(private addresses: { right: string; left: string }, private path: string) {}
|
||||
onLog(fn: (line: string) => void): () => void { this.logs.add(fn); return () => this.logs.delete(fn) }
|
||||
onStateChange(fn: (state: FlashState, detail: string) => void): () => void { this.states.add(fn); return () => this.states.delete(fn) }
|
||||
onProgress(fn: (value: FlashProgress) => void): () => void { this.progresses.add(fn); return () => this.progresses.delete(fn) }
|
||||
onComplete(fn: (success: boolean, detail: string) => void): () => void { this.completes.add(fn); return () => this.completes.delete(fn) }
|
||||
private log(line: string): void { for (const fn of this.logs) fn(line) }
|
||||
private state(state: FlashState, detail = ''): void { for (const fn of this.states) fn(state, detail) }
|
||||
start(): void { if (!this.started) { this.started = true; void this.run() } }
|
||||
private previousIdle = false
|
||||
|
||||
constructor(private readonly addresses: { right: string; left: string }, private readonly path: string) {}
|
||||
|
||||
onLog(listener: (line: string) => void): () => void {
|
||||
this.logListeners.add(listener)
|
||||
return () => this.logListeners.delete(listener)
|
||||
}
|
||||
|
||||
onProgress(listener: (progress: FlashProgress) => void): () => void {
|
||||
this.progressListeners.add(listener)
|
||||
return () => this.progressListeners.delete(listener)
|
||||
}
|
||||
|
||||
onStateChange(listener: (state: FlashState, detail: string) => void): () => void {
|
||||
this.stateListeners.add(listener)
|
||||
return () => this.stateListeners.delete(listener)
|
||||
}
|
||||
|
||||
onComplete(listener: (success: boolean, detail: string) => void): () => void {
|
||||
this.completeListeners.add(listener)
|
||||
return () => this.completeListeners.delete(listener)
|
||||
}
|
||||
|
||||
start(): void {
|
||||
if (this.started) return
|
||||
this.started = true
|
||||
void this.run()
|
||||
}
|
||||
|
||||
private readonly backgrounded = () => {
|
||||
this.foregroundError = 'Flashing was interrupted because Faceclaw left the foreground. Keep both lenses charged, return to Faceclaw, and retry both lenses.'
|
||||
this.cancel()
|
||||
}
|
||||
|
||||
private async run(): Promise<void> {
|
||||
const previousIdle = UIApplication.sharedApplication.idleTimerDisabled
|
||||
this.previousIdle = UIApplication.sharedApplication.idleTimerDisabled
|
||||
UIApplication.sharedApplication.idleTimerDisabled = true
|
||||
Application.on(Application.suspendEvent, this.backgrounded)
|
||||
let success = false, detail = ''
|
||||
try {
|
||||
this.link.check()
|
||||
if (UIApplication.sharedApplication.applicationState !== UIApplicationState.Active) throw new Error('Keep Faceclaw open on the iPhone while flashing.')
|
||||
this.state('validating')
|
||||
this.emit(this.stateListeners, 'validating', '')
|
||||
const image = readFirmwareFile(this.path)
|
||||
const hash = firmwareSha256(image.buffer)
|
||||
if (![CFW_PATCH_SET.outputSha256, CFW_PATCH_SET.baseSha256].includes(hash)) throw new Error('Prepared firmware failed SHA-256 verification. Prepare the firmware again before flashing.')
|
||||
const segments = validateFirmware(image)
|
||||
await this.link.resolve({ ...this.addresses, ring: '' })
|
||||
for (const role of ['left', 'right']) {
|
||||
if (role === 'right') {
|
||||
this.state('rebooting', 'Left lens done. Both lenses reboot briefly; reconnecting for the right lens.')
|
||||
await this.delay(5000)
|
||||
}
|
||||
this.state('connecting', role)
|
||||
const deadline = Date.now() + (role === 'left' ? 30_000 : 120_000)
|
||||
for (;;) {
|
||||
try { await this.link.connect(role, true); await this.link.authenticate(role); break }
|
||||
catch (error) { this.link.check(); this.link.disconnect(role); if (Date.now() >= deadline) throw error; await this.delay(2500) }
|
||||
}
|
||||
await this.delay(2500)
|
||||
this.state('flashing', role)
|
||||
await flashLensImage(this.link, role, image, segments, value => { for (const fn of this.progresses) fn(value) }, line => this.log(line), ms => this.delay(ms))
|
||||
this.link.disconnect(role)
|
||||
}
|
||||
success = true; detail = 'Both lenses flashed. Your glasses are rebooting.'
|
||||
const identifiers = await iosBluetooth().resolveDevices({ ...this.addresses, ring: '' })
|
||||
if (this.cancelled) throw new Error('Cancelled.')
|
||||
this.flasher = FaceclawKitIosFirmwareFlasher.alloc().initWithRightAddressLeftAddressFirmwarePath(identifiers.right ?? '', identifiers.left ?? '', this.path)
|
||||
this.listenerProxy = kotlinListener(FaceclawKitFaceclawFirmwareFlasherListener, {
|
||||
onLogLine: (line: string) => this.emit(this.logListeners, String(line ?? '')),
|
||||
onProgressLensComponentIndexComponentCountBlockIndexBlockCountBytesSentBytesTotal: (
|
||||
lens: string, componentIndex: number, componentCount: number, blockIndex: number, blockCount: number, bytesSent: number, bytesTotal: number,
|
||||
) => this.emit(this.progressListeners, {
|
||||
lens: String(lens ?? ''), componentIndex: Number(componentIndex), componentCount: Number(componentCount),
|
||||
blockIndex: Number(blockIndex), blockCount: Number(blockCount), bytesSent: Number(bytesSent), bytesTotal: Number(bytesTotal),
|
||||
}),
|
||||
onStateStateDetail: (state: string, detail: string) => {
|
||||
// The terminal state is reported by finish() so a foreground/cancel reason can replace the detail.
|
||||
const name = String(state) as FlashState
|
||||
if (name !== 'done' && name !== 'error') this.emit(this.stateListeners, name, String(detail ?? ''))
|
||||
},
|
||||
onCompleteSuccessDetail: (success: boolean, detail: string) => this.finish(Boolean(success), String(detail ?? '')),
|
||||
})
|
||||
this.flasher.setListenerListener(this.listenerProxy)
|
||||
this.flasher.start()
|
||||
} catch (error) {
|
||||
detail = this.foregroundError || (this.cancelled ? 'Cancelled. Retry with both lenses powered on to complete installation.' : String((error as Error).message ?? error))
|
||||
} finally {
|
||||
Application.off(Application.suspendEvent, this.backgrounded)
|
||||
UIApplication.sharedApplication.idleTimerDisabled = previousIdle
|
||||
this.link.close()
|
||||
this.finish(false, String((error as Error).message ?? error))
|
||||
}
|
||||
this.state(success ? 'done' : 'error', detail)
|
||||
for (const fn of this.completes) fn(success, detail)
|
||||
}
|
||||
private delay(ms: number): Promise<void> {
|
||||
this.link.check()
|
||||
return new Promise((resolve, reject) => {
|
||||
const cancel = () => { clearTimeout(timer); this.sleeps.delete(cancel); reject(new Error('Cancelled.')) }
|
||||
const timer = setTimeout(() => { this.sleeps.delete(cancel); resolve() }, ms)
|
||||
this.sleeps.add(cancel)
|
||||
})
|
||||
|
||||
private finish(success: boolean, detail: string): void {
|
||||
if (this.finished) return
|
||||
this.finished = true
|
||||
Application.off(Application.suspendEvent, this.backgrounded)
|
||||
UIApplication.sharedApplication.idleTimerDisabled = this.previousIdle
|
||||
try { this.flasher?.close() } catch { /* ignore */ }
|
||||
const message = success ? detail
|
||||
: this.foregroundError || (this.cancelled ? 'Cancelled. Retry with both lenses powered on to complete installation.' : detail)
|
||||
this.emit(this.stateListeners, success ? 'done' : 'error', message)
|
||||
this.emit(this.completeListeners, success, message)
|
||||
}
|
||||
cancel(): void { this.cancelled = true; this.link.close(); for (const cancel of [...this.sleeps]) cancel() }
|
||||
|
||||
cancel(): void {
|
||||
this.cancelled = true
|
||||
try { this.flasher?.cancel() } catch { /* ignore */ }
|
||||
// Before the Kotlin flow exists (validation, address resolution) nothing will report completion.
|
||||
if (!this.flasher) this.finish(false, 'Cancelled.')
|
||||
}
|
||||
|
||||
close(): void { this.cancel() }
|
||||
|
||||
private emit<A extends unknown[]>(listeners: Set<(...args: A) => void>, ...args: A): void {
|
||||
const snapshot = Array.from(listeners)
|
||||
setTimeout(() => { for (const listener of snapshot) listener(...args) }, 0)
|
||||
}
|
||||
}
|
||||
@@ -1,88 +1,98 @@
|
||||
import { StockConnection } from '../g2/stock-connection'
|
||||
import { createFlashPrompt, flashPromptSelection } from '../g2/flash-prompt-protocol'
|
||||
import * as p from '../g2/ble-protocol'
|
||||
import { iosBluetooth } from './ios-bluetooth'
|
||||
import { kotlinListener } from './kotlin-listener.ios'
|
||||
import type { FlashPromptBattery, FlashPromptState } from './flash-prompt-communicator'
|
||||
export type { FlashPromptBattery, FlashPromptState } from './flash-prompt-communicator'
|
||||
|
||||
declare const FaceclawKitIosFlashPrompt: any, FaceclawKitFaceclawFlashPromptListener: any
|
||||
|
||||
function normalizePercent(value: number): number | null {
|
||||
const n = Number(value)
|
||||
return Number.isFinite(n) && n >= 0 ? n : null
|
||||
}
|
||||
|
||||
/**
|
||||
* iOS twin of the Android FlashPromptCommunicator wrapper over the shared Kotlin
|
||||
* FlashPromptFlow (FaceclawKitIosFlashPrompt): connects and authenticates both arms, shows
|
||||
* the pre-flash Yes/No page on the glasses (unless skipPrompt) and reads each arm's battery.
|
||||
*/
|
||||
export class FlashPromptCommunicator {
|
||||
private logs = new Set<(line: string) => void>()
|
||||
private states = new Set<(state: FlashPromptState, detail: string) => void>()
|
||||
private results = new Set<(approved: boolean) => void>()
|
||||
private batteries = new Set<(battery: FlashPromptBattery) => void>()
|
||||
private link = new StockConnection(iosBluetooth(), line => { for (const fn of this.logs) fn(line) })
|
||||
private communicator: any = null
|
||||
private listenerProxy: object | null = null
|
||||
private readonly logListeners = new Set<(line: string) => void>()
|
||||
private readonly stateListeners = new Set<(state: FlashPromptState, detail: string) => void>()
|
||||
private readonly resultListeners = new Set<(approved: boolean) => void>()
|
||||
private readonly batteryListeners = new Set<(battery: FlashPromptBattery) => void>()
|
||||
private started = false
|
||||
private closed = false
|
||||
private started = false
|
||||
private heartbeat: ReturnType<typeof setInterval> | null = null
|
||||
private stopSelection: (() => void) | null = null
|
||||
constructor(private addresses: { right: string; left: string }, private warning: string, private options?: { skipPrompt?: boolean }) {}
|
||||
onLog(fn: (line: string) => void): () => void { this.logs.add(fn); return () => this.logs.delete(fn) }
|
||||
onStateChange(fn: (state: FlashPromptState, detail: string) => void): () => void { this.states.add(fn); return () => this.states.delete(fn) }
|
||||
onResult(fn: (approved: boolean) => void): () => void { this.results.add(fn); return () => this.results.delete(fn) }
|
||||
onBattery(fn: (battery: FlashPromptBattery) => void): () => void { this.batteries.add(fn); return () => this.batteries.delete(fn) }
|
||||
private state(state: FlashPromptState, detail = ''): void { for (const fn of this.states) fn(state, detail) }
|
||||
start(): void { if (!this.started) { this.started = true; void this.run() } }
|
||||
|
||||
constructor(
|
||||
private readonly addresses: { right: string; left: string },
|
||||
private readonly warningText: string,
|
||||
private readonly options?: { skipPrompt?: boolean },
|
||||
) {}
|
||||
|
||||
onLog(listener: (line: string) => void): () => void {
|
||||
this.logListeners.add(listener)
|
||||
return () => this.logListeners.delete(listener)
|
||||
}
|
||||
|
||||
onStateChange(listener: (state: FlashPromptState, detail: string) => void): () => void {
|
||||
this.stateListeners.add(listener)
|
||||
return () => this.stateListeners.delete(listener)
|
||||
}
|
||||
|
||||
onBattery(listener: (battery: FlashPromptBattery) => void): () => void {
|
||||
this.batteryListeners.add(listener)
|
||||
return () => this.batteryListeners.delete(listener)
|
||||
}
|
||||
|
||||
onResult(listener: (approved: boolean) => void): () => void {
|
||||
this.resultListeners.add(listener)
|
||||
return () => this.resultListeners.delete(listener)
|
||||
}
|
||||
|
||||
start(): void {
|
||||
if (this.started) return
|
||||
this.started = true
|
||||
void this.run()
|
||||
}
|
||||
|
||||
private async run(): Promise<void> {
|
||||
let identifiers: Record<string, string>
|
||||
try {
|
||||
this.state('connecting')
|
||||
await this.link.resolve({ ...this.addresses, ring: '' })
|
||||
// Complete pairing for one peripheral before starting the other.
|
||||
for (const role of ['right', 'left']) {
|
||||
await this.link.connect(role)
|
||||
if (role === 'right') this.state('connected')
|
||||
await this.link.authenticate(role)
|
||||
}
|
||||
await this.link.request('right', p.SID.launch, p.prelude, 3500, 156)
|
||||
if (!this.options?.skipPrompt) {
|
||||
// Listen before creating the page: an immediate tap may precede its ACK.
|
||||
const selection = this.selection()
|
||||
// Attach a rejection handler immediately, even while create-page is pending.
|
||||
void selection.catch(() => {})
|
||||
for (let attempt = 0; ; attempt++) {
|
||||
try { await this.link.request('right', p.SID.hub, magic => createFlashPrompt(magic, this.warning)); break }
|
||||
catch (error) { this.link.check(); if (attempt) throw error }
|
||||
}
|
||||
this.state('prompting')
|
||||
this.heartbeat = setInterval(() => {
|
||||
void this.link.write('right', p.SID.hub, p.heartbeat(this.link.nextMagic())).catch(error => {
|
||||
if (!this.closed) this.state('error', String(error.message ?? error))
|
||||
this.close()
|
||||
})
|
||||
}, 4000)
|
||||
const approved = await selection
|
||||
this.stopHeartbeat()
|
||||
// Stock shutdown returns the glasses to their normal UI before OTA.
|
||||
await this.link.write('right', p.SID.hub, p.concat(p.integer(1, 9), p.integer(2, this.link.nextMagic()), p.bytes(11, p.integer(1, 0))))
|
||||
if (!approved) { for (const fn of this.results) fn(false); return }
|
||||
}
|
||||
this.state('battery')
|
||||
const battery: FlashPromptBattery = { right: null, left: null }
|
||||
for (const role of ['right', 'left'] as const) {
|
||||
for (let attempt = 0; attempt < 2; attempt++) {
|
||||
try {
|
||||
const message = await this.link.request(role, p.SID.settings, p.settingsQuery)
|
||||
const values = p.readBytes(message.payload, 4), value = values ? p.readInteger(values, 12, -1) : -1
|
||||
if (value >= 0 && value <= 100) { battery[role] = value; break }
|
||||
} catch (error) { this.link.check() }
|
||||
}
|
||||
}
|
||||
for (const fn of this.batteries) fn(battery)
|
||||
for (const fn of this.results) fn(true)
|
||||
} catch (error) { if (!this.closed) this.state('error', String((error as Error).message ?? error)) }
|
||||
finally { this.close() }
|
||||
this.emit(this.stateListeners, 'connecting', '')
|
||||
identifiers = await iosBluetooth().resolveDevices({ ...this.addresses, ring: '' })
|
||||
} catch (error) {
|
||||
if (!this.closed) this.emit(this.stateListeners, 'error', String((error as Error).message ?? error))
|
||||
return
|
||||
}
|
||||
if (this.closed) return
|
||||
this.communicator = FaceclawKitIosFlashPrompt.alloc().initWithRightAddressLeftAddressWarningTextSkipPrompt(
|
||||
identifiers.right ?? '', identifiers.left ?? '', this.warningText, Boolean(this.options?.skipPrompt))
|
||||
this.listenerProxy = kotlinListener(FaceclawKitFaceclawFlashPromptListener, {
|
||||
onLogLine: (line: string) => this.emit(this.logListeners, String(line ?? '')),
|
||||
onStateStateDetail: (state: string, detail: string) =>
|
||||
this.emit(this.stateListeners, String(state) as FlashPromptState, String(detail ?? '')),
|
||||
onBatteryRightPercentLeftPercent: (rightPercent: number, leftPercent: number) =>
|
||||
this.emit(this.batteryListeners, { right: normalizePercent(rightPercent), left: normalizePercent(leftPercent) }),
|
||||
onResultApproved: (approved: boolean) => this.emit(this.resultListeners, Boolean(approved)),
|
||||
})
|
||||
this.communicator.setListenerListener(this.listenerProxy)
|
||||
this.communicator.start()
|
||||
}
|
||||
|
||||
cancel(): void {
|
||||
this.closed = true
|
||||
try { this.communicator?.cancel() } catch { /* ignore */ }
|
||||
}
|
||||
private selection(): Promise<boolean> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const cleanup = () => { clearTimeout(timer); off(); this.stopSelection = null }
|
||||
const off = this.link.onMessage((role, message) => {
|
||||
const result = role === 'right' ? flashPromptSelection(message) : null
|
||||
if (result !== null) { cleanup(); resolve(result) }
|
||||
})
|
||||
const timer = setTimeout(() => { cleanup(); reject(new Error('No response from the glasses.')) }, 120_000)
|
||||
this.stopSelection = () => { cleanup(); reject(new Error('Cancelled.')) }
|
||||
})
|
||||
|
||||
close(): void {
|
||||
this.closed = true
|
||||
try { this.communicator?.close() } catch { /* ignore */ }
|
||||
}
|
||||
|
||||
private emit<A extends unknown[]>(listeners: Set<(...args: A) => void>, ...args: A): void {
|
||||
const snapshot = Array.from(listeners)
|
||||
setTimeout(() => { for (const listener of snapshot) listener(...args) }, 0)
|
||||
}
|
||||
private stopHeartbeat(): void { if (this.heartbeat) clearInterval(this.heartbeat); this.heartbeat = null }
|
||||
cancel(): void { this.close() }
|
||||
close(): void { this.closed = true; this.stopHeartbeat(); this.stopSelection?.(); this.link.close() }
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
/**
|
||||
* TS-side wrapper for the Java FrameTimings singleton (per-frame latency
|
||||
* instrumentation; see FrameTimings.java). Frame ID 0 means "no frame" and is
|
||||
* instrumentation; see FrameTimings.kt). Frame ID 0 means "no frame" and is
|
||||
* ignored by every method, so callers never need to null-check.
|
||||
*
|
||||
* Frames form a tree: pass the causing frame's ID as `parentFrameId` when one
|
||||
|
||||
+56
-73
@@ -1,75 +1,80 @@
|
||||
import { ApplicationSettings } from '@nativescript/core'
|
||||
import { identifyIosPeripheral, type IosAdvertisement, type IosDevice } from '../g2/ios-peripheral-identity'
|
||||
import { hexToBytes } from '../util/hex-util'
|
||||
import { kotlinListener } from './kotlin-listener.ios'
|
||||
|
||||
declare const FaceclawBluetooth: any
|
||||
type NativeEvent = { kind: string; id?: number; error?: string; details?: ConnectionDetails; state?: number;
|
||||
identifier?: string; characteristic?: string; data?: string; message?: string } & Partial<IosAdvertisement>
|
||||
export type ConnectionDetails = { characteristics: string[]; maxWrite: number }
|
||||
export type BluetoothEvent = NativeEvent | { kind: 'device'; device: IosDevice }
|
||||
type Pending = { resolve: (details: ConnectionDetails) => void; reject: (error: Error) => void; timer: ReturnType<typeof setTimeout> }
|
||||
declare const FaceclawKitIosBleCentral: any, FaceclawKitIosProtocolPlatform: any, FaceclawKitIosBleScanListener: any
|
||||
|
||||
/** One central manager per app, with asynchronous main-queue GATT operations. */
|
||||
/** CBManagerState raw values reported by the Kotlin central. */
|
||||
export const BLE_STATE_UNSUPPORTED = 2, BLE_STATE_UNAUTHORIZED = 3, BLE_STATE_POWERED_OFF = 4, BLE_STATE_POWERED_ON = 5
|
||||
|
||||
export type BluetoothEvent =
|
||||
| { kind: 'state'; state: number; authorization: number }
|
||||
| { kind: 'advertisement'; identifier: string; name: string; manufacturerData: string; rssi: number; connectable: boolean }
|
||||
| { kind: 'device'; device: IosDevice }
|
||||
| { kind: 'scan-started' }
|
||||
| { kind: 'scan-stopped' }
|
||||
|
||||
/**
|
||||
* Scanning and MAC-address-to-identifier resolution over the shared Kotlin CoreBluetooth
|
||||
* central (FaceclawKitIosBleCentral). GATT sessions no longer pass through here: the
|
||||
* session and stock-firmware flows own their own Kotlin centrals. What stays in TypeScript
|
||||
* is the Even classification of advertisements and the persisted identifier map, which the
|
||||
* Android address model (MACs) needs on iOS.
|
||||
*/
|
||||
export class IosBluetooth {
|
||||
private readonly native = FaceclawBluetooth.new()
|
||||
private readonly native = FaceclawKitIosBleCentral.alloc().initWithPlatform(FaceclawKitIosProtocolPlatform.shared)
|
||||
private readonly listeners = new Set<(event: BluetoothEvent) => void>()
|
||||
private readonly pending = new Map<number, Pending>()
|
||||
private readonly advertisements = new Map<string, IosAdvertisement>()
|
||||
readonly devices = new Map<string, IosDevice>()
|
||||
private requestId = 0
|
||||
private scanTimer: ReturnType<typeof setTimeout> | null = null
|
||||
private scanGeneration = 0
|
||||
private readonly scanListener: object
|
||||
state = 0
|
||||
authorization = 0
|
||||
scanning = false
|
||||
constructor() {
|
||||
this.native.eventHandler = (json: string) => {
|
||||
try { this.receive(JSON.parse(json)) } catch (error) { console.error(`[ios-ble] ${error}`) }
|
||||
}
|
||||
this.scanListener = kotlinListener(FaceclawKitIosBleScanListener, {
|
||||
onBluetoothStateStateAuthorization: (state: number, authorization: number) => {
|
||||
this.state = Number(state); this.authorization = Number(authorization)
|
||||
this.emit({ kind: 'state', state: this.state, authorization: this.authorization })
|
||||
},
|
||||
onAdvertisementIdentifierNameManufacturerDataHexRssiConnectable: (identifier: string, name: string, manufacturerData: string, rssi: number, connectable: boolean) => {
|
||||
// The Kotlin central already merges split advertisements and bounds its cache.
|
||||
const raw: IosAdvertisement = { identifier: String(identifier), name: String(name ?? ''), manufacturerData: String(manufacturerData ?? ''), rssi: Number(rssi), connectable: connectable !== false }
|
||||
this.emit({ kind: 'advertisement', ...raw })
|
||||
const device = identifyIosPeripheral(raw)
|
||||
if (device) {
|
||||
this.devices.set(device.address, device)
|
||||
ApplicationSettings.setString(`ios.ble.peripheral.${device.address}`, JSON.stringify({ identifier: device.identifier, role: device.role }))
|
||||
this.emit({ kind: 'device', device })
|
||||
}
|
||||
},
|
||||
})
|
||||
this.native.setScanListenerListener(this.scanListener)
|
||||
}
|
||||
onEvent(listener: (event: BluetoothEvent) => void): () => void {
|
||||
this.listeners.add(listener); return () => { this.listeners.delete(listener) }
|
||||
}
|
||||
private emit(event: BluetoothEvent): void { for (const listener of [...this.listeners]) listener(event) }
|
||||
private receive(event: NativeEvent): void {
|
||||
if (event.kind === 'completion') {
|
||||
const pending = this.pending.get(event.id!)
|
||||
if (pending) {
|
||||
this.pending.delete(event.id!); clearTimeout(pending.timer)
|
||||
if (event.error) pending.reject(new Error(event.error)); else pending.resolve(event.details!)
|
||||
}
|
||||
return
|
||||
}
|
||||
if (event.kind === 'state') this.state = event.state!
|
||||
if (event.kind === 'advertisement') {
|
||||
const previous = this.advertisements.get(event.identifier!)
|
||||
const raw: IosAdvertisement = { identifier: event.identifier!, name: event.name || previous?.name || '',
|
||||
manufacturerData: event.manufacturerData || previous?.manufacturerData || '', rssi: event.rssi!, connectable: event.connectable !== false }
|
||||
// Bound the cache even in a crowded radio environment.
|
||||
if (this.advertisements.size >= 512 && !this.advertisements.has(raw.identifier)) this.advertisements.delete(this.advertisements.keys().next().value!)
|
||||
this.advertisements.set(raw.identifier, raw)
|
||||
const device = identifyIosPeripheral(raw)
|
||||
if (device) {
|
||||
this.devices.set(device.address, device)
|
||||
ApplicationSettings.setString(`ios.ble.peripheral.${device.address}`, JSON.stringify({ identifier: device.identifier, role: device.role }))
|
||||
this.emit({ kind: 'device', device })
|
||||
}
|
||||
return
|
||||
}
|
||||
this.emit(event)
|
||||
/** Creates the central (first use shows the Bluetooth permission prompt) without blocking the JS thread. */
|
||||
private ensureCentral(): void {
|
||||
// A zero timeout returns immediately after creating the central; the state arrives
|
||||
// through the scan listener on the main queue.
|
||||
this.native.waitForPoweredOnTimeoutMs(0)
|
||||
}
|
||||
ensureReady(): Promise<void> {
|
||||
if (this.state === BLE_STATE_POWERED_ON) return Promise.resolve()
|
||||
return new Promise((resolve, reject) => {
|
||||
const finish = (error?: Error) => { clearTimeout(timer); off(); error ? reject(error) : resolve() }
|
||||
const off = this.onEvent(event => {
|
||||
if (event.kind !== 'state') return
|
||||
const state = (event as NativeEvent).state
|
||||
if (state === 5) finish()
|
||||
else if (state === 2) finish(new Error('Bluetooth is unavailable on this device. Use the physical iPhone.'))
|
||||
else if (state === 3) finish(new Error('Allow Faceclaw to use Bluetooth in iPhone Settings.'))
|
||||
else if (state === 4) finish(new Error('Turn on Bluetooth, then try again.'))
|
||||
if (event.state === BLE_STATE_POWERED_ON) finish()
|
||||
else if (event.state === BLE_STATE_UNSUPPORTED) finish(new Error('Bluetooth is unavailable on this device. Use the physical iPhone.'))
|
||||
else if (event.state === BLE_STATE_UNAUTHORIZED) finish(new Error('Allow Faceclaw to use Bluetooth in iPhone Settings.'))
|
||||
else if (event.state === BLE_STATE_POWERED_OFF) finish(new Error('Turn on Bluetooth, then try again.'))
|
||||
})
|
||||
const timer = setTimeout(() => finish(new Error('Waiting for Bluetooth permission timed out. Try again after allowing Bluetooth.')), 30_000)
|
||||
this.native.initializeBluetooth()
|
||||
this.ensureCentral()
|
||||
})
|
||||
}
|
||||
async startScan(durationMs = 12_000): Promise<void> {
|
||||
@@ -77,17 +82,18 @@ export class IosBluetooth {
|
||||
const generation = this.scanGeneration
|
||||
await this.ensureReady()
|
||||
if (generation !== this.scanGeneration) throw new Error('Bluetooth scan cancelled')
|
||||
this.scanning = true; this.native.scan(true)
|
||||
this.scanning = true; this.native.startScan()
|
||||
this.scanTimer = setTimeout(() => this.stopScan(), durationMs)
|
||||
this.emit({ kind: 'scan-started' })
|
||||
}
|
||||
stopScan(): void {
|
||||
++this.scanGeneration
|
||||
if (this.scanTimer) clearTimeout(this.scanTimer)
|
||||
this.scanTimer = null; this.native.scan(false)
|
||||
this.scanTimer = null
|
||||
const wasScanning = this.scanning; this.scanning = false
|
||||
if (wasScanning) this.emit({ kind: 'scan-stopped' })
|
||||
if (wasScanning) { this.native.stopScan(); this.emit({ kind: 'scan-stopped' }) }
|
||||
}
|
||||
/** Peripheral identifiers for the stored MAC addresses, scanning for any that are not remembered. */
|
||||
async resolveDevices(addresses: { left: string; right: string; ring: string }): Promise<Record<string, string>> {
|
||||
const generation = this.scanGeneration
|
||||
await this.ensureReady()
|
||||
@@ -109,7 +115,7 @@ export class IosBluetooth {
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
const off = this.onEvent(event => {
|
||||
if (event.kind === 'device') {
|
||||
const device = (event as { device: IosDevice }).device
|
||||
const device = event.device
|
||||
for (const role of ['left', 'right', 'ring'] as const) {
|
||||
if (addresses[role] === device.address && device.role === role) result[role] = device.identifier
|
||||
}
|
||||
@@ -123,29 +129,6 @@ export class IosBluetooth {
|
||||
})
|
||||
return result
|
||||
}
|
||||
private operation(identifier: string, call: (id: number) => void, timeoutMs = 10_000, disconnectOnTimeout = true): Promise<ConnectionDetails> {
|
||||
const id = ++this.requestId
|
||||
return new Promise((resolve, reject) => {
|
||||
const timer = setTimeout(() => {
|
||||
this.pending.delete(id); if (disconnectOnTimeout) this.native.disconnect(identifier)
|
||||
reject(new Error('Bluetooth operation timed out. Wake the device and try connecting again.'))
|
||||
}, timeoutMs)
|
||||
this.pending.set(id, { resolve, reject, timer })
|
||||
try { call(id) } catch (error) { clearTimeout(timer); this.pending.delete(id); reject(error) }
|
||||
})
|
||||
}
|
||||
connect(identifier: string, requiresANCS = false): Promise<ConnectionDetails> { return this.operation(identifier, id => this.native.connectRequiresANCSRequestId(identifier, requiresANCS, id), 20_000) }
|
||||
async subscribe(identifier: string, characteristic: string): Promise<void> {
|
||||
// R1 exposes two notification channels; one may never complete its CCCD
|
||||
// request. Keep the link alive so the other channel can still be tried.
|
||||
await this.operation(identifier, id => this.native.subscribeCharacteristicRequestId(identifier, characteristic, id), 30_000, false)
|
||||
}
|
||||
async write(identifier: string, characteristic: string, bytes: Uint8Array): Promise<void> {
|
||||
const copy = new Uint8Array(bytes)
|
||||
const data = NSData.dataWithBytesLength(interop.handleof(copy.buffer), copy.byteLength)
|
||||
await this.operation(identifier, id => this.native.writeCharacteristicDataRequestId(identifier, characteristic, data, id))
|
||||
}
|
||||
disconnect(identifier: string): void { this.native.disconnect(identifier) }
|
||||
forget(address: string): void { ApplicationSettings.remove(`ios.ble.peripheral.${address}`); this.devices.delete(address) }
|
||||
}
|
||||
let instance: IosBluetooth | null = null
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
/**
|
||||
* Implements a FaceclawKit (Kotlin) listener protocol with plain JavaScript methods.
|
||||
* NativeScript maps the method names to the protocol's selectors by camel-casing the
|
||||
* selector parts (`onLog:` -> `onLogLine`, `onState:detail:` -> `onStateStateDetail`, ...).
|
||||
* Keep a reference to the returned object for as long as the Kotlin side may call it.
|
||||
*/
|
||||
export function kotlinListener<T extends object>(protocol: unknown, methods: T): T {
|
||||
const NativeListener = (NSObject as any).extend(methods, { protocols: [protocol] })
|
||||
return NativeListener.new() as T
|
||||
}
|
||||
@@ -1,11 +1,9 @@
|
||||
import { GrayImage } from "../graphics/image"
|
||||
import { encodeShellScene } from "../graphics/shell-scene"
|
||||
import { decodeImageBytes } from './image-bytes.ios'
|
||||
import * as protocol from '../g2/ble-protocol.ios'
|
||||
import { CfwTransport, parseCfwAcks } from '../g2/cfw-transport.ios'
|
||||
import { SurfaceCompositor } from '../graphics/surface-compositor.ios'
|
||||
import { buildBoundingBoxPayload } from '../g2/ble-image-optimizer.ios'
|
||||
import { lvglMetrics } from './lvgl-font.ios'
|
||||
import { textureImageId } from './texture-atlas.ios'
|
||||
import { IosTexturePlanner } from './texture-planner.ios'
|
||||
|
||||
/** Development-only checks of real NativeScript selectors and bulk data crossings. */
|
||||
export function runKotlinProtocolSmokeTest(): void {
|
||||
@@ -21,31 +19,20 @@ export function runKotlinProtocolSmokeTest(): void {
|
||||
const payload = protocol.bytes(13, protocol.bytes(3, protocol.concat(protocol.integer(1, 12), protocol.integer(2, 2))))
|
||||
const event = protocol.decodeGlassesInput({ sid: 224, flag: 1, payload, command: 0, magic: 0 })
|
||||
assert(event?.kind === 'display-wake' && event.eventSource === 2, 'event')
|
||||
const transport = new CfwTransport()
|
||||
const packets = transport.encode(new Uint8Array(128).fill(9), 255, 3, 185)
|
||||
assert(packets.length > 0 && packets.every(p => p.length <= 185), 'compression')
|
||||
transport.reset()
|
||||
assert(parseCfwAcks(new Uint8Array()) === null, 'invalid ACK')
|
||||
const c = new SurfaceCompositor(3, 2)
|
||||
c.configureSurface('test', { x: 0, y: 0, width: 3, height: 2, zOrder: 0, transparency: 'opaque' })
|
||||
c.submitSurfaceFrame('test', new Uint8Array([0, 16, 255, 32, 48, 64]), { x: 0, y: 0, width: 3, height: 2 })
|
||||
assert(hex(c.composite()) === '0010ff203040', 'composition')
|
||||
assert(hex(protocol.packGray4(c.composite(), 3, 2)) === '01f02340', 'packing')
|
||||
c.setShellScene(new Uint8Array([1,0,1,0,0,0,0,0,1,0,1,0,128,0,0,0,0,0,255]))
|
||||
assert(hex(c.composite()) === 'f00070101020', 'shell scene bridge and 4bpp composition')
|
||||
c.setScreenBlanked(true); assert(c.composite().every(p => p === 0), 'blanking')
|
||||
const textures = new IosTexturePlanner(), textured = new SurfaceCompositor(4, 2)
|
||||
textured.configureSurface('icon', { x: 0, y: 0, width: 2, height: 2, zOrder: 0, transparency: 'opaque' })
|
||||
const white = new Uint8Array(4).fill(255)
|
||||
const icon = textureImageId('protocol-smoke-white', 2, 2, white)
|
||||
const draw = new DataView(new ArrayBuffer(9)); draw.setUint8(0, 1); draw.setUint32(1, icon, true)
|
||||
textured.submitSurfaceFrame('icon', white, { x: 0, y: 0, width: 2, height: 2 }, draw.buffer)
|
||||
const snapshot = textured.compositeFrame(), packed = protocol.packGray4(snapshot.pixels, 4, 2)
|
||||
const cold = textures.plan(null, packed, snapshot.textures, 1)
|
||||
assert(!!cold && cold.uploads[0]?.[0] === 18 && cold.payload[0] === 8, 'texture upload and draw')
|
||||
assert(textures.plan(null, packed, snapshot.textures, 1)?.uploads.length === 0, 'texture reuse')
|
||||
textures.reset()
|
||||
assert((textures.plan(null, packed, snapshot.textures, 1)?.uploads.length ?? 0) > 0, 'texture reset')
|
||||
const changed = new Uint8Array(16); changed[0] = 255
|
||||
assert(buildBoundingBoxPayload(new Uint8Array(16), changed, 8, 4, 1)?.[0] === 3, 'image update')
|
||||
const selectedSurface = new GrayImage(8, 4, 1)
|
||||
selectedSurface.drawMenuSelection(new GrayImage(4, 2, 255), 1, 1, 16, 48, 0, 2)
|
||||
const selectedPreview = new SurfaceCompositor(8, 4)
|
||||
selectedPreview.setShellScene(encodeShellScene([{ image: selectedSurface, x: 0, y: 0, shellKey: 100 }]))
|
||||
const selectedPixels = selectedPreview.composite()
|
||||
assert(selectedPixels[10] === 240 && selectedPixels[9] === 0, 'menu selection depth and bridge')
|
||||
assert(lvglMetrics('/nonexistent/faceclaw-font').length === 0, 'font bridge')
|
||||
const compass = protocol.decodeCompassInput({ sid: 8, flag: 1, command: 15, magic: 0,
|
||||
payload: new Uint8Array([8, 15, 16, 0, 82, 3, 8, 231, 2, 162, 6, 12, 67, 77, 1, 3, 2, 3, 140, 0, 152, 186, 220, 254]) })
|
||||
|
||||
@@ -0,0 +1,146 @@
|
||||
import { File, knownFolders, path, type ImageSource } from "@nativescript/core";
|
||||
import { SurfaceCompositor } from "../graphics/surface-compositor";
|
||||
import { G2_LENS_WIDTH, G2_LENS_HEIGHT } from "../graphics/image";
|
||||
import { previewPixels } from "./ios-graphics";
|
||||
import { toData } from "./kotlin-data";
|
||||
import type { FaceclawCommunicatorBridge, SurfaceOptions } from "./faceclaw-communicator.ios";
|
||||
|
||||
declare const FaceclawKitIosProtocol: any;
|
||||
|
||||
/** The display subset of the communicator bridge; see the Android twin. */
|
||||
export type DisplayTarget = Pick<
|
||||
FaceclawCommunicatorBridge,
|
||||
| "configureCompositorScreen"
|
||||
| "configureSurface"
|
||||
| "removeSurface"
|
||||
| "setSurfaceVisible"
|
||||
| "setSurfaceDepth"
|
||||
| "setUnderlayDim"
|
||||
| "setScreenBlanked"
|
||||
| "submitSurfaceFrame"
|
||||
| "submitShellScene"
|
||||
| "waitForFrameFinished"
|
||||
| "getCompositePreview"
|
||||
| "saveScreenshot"
|
||||
| "startScreenRecording"
|
||||
| "recordScreenFrame"
|
||||
| "stopScreenRecording"
|
||||
>;
|
||||
|
||||
function timestamp(): string {
|
||||
const now = new Date();
|
||||
const pad = (value: number, width = 2) => String(value).padStart(width, "0");
|
||||
return `${now.getFullYear()}${pad(now.getMonth() + 1)}${pad(now.getDate())}-${pad(now.getHours())}${pad(now.getMinutes())}${pad(now.getSeconds())}-${pad(now.getMilliseconds(), 3)}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Headless display target for iOS while no glasses session exists: the same
|
||||
* shared Kotlin SurfaceCompositor a live session composes with, so the phone
|
||||
* mirror, apps and shell render identically before connecting. Frames end
|
||||
* at the compositor; there is no transmit backpressure to wait on.
|
||||
*/
|
||||
export class PreviewDisplayTarget implements DisplayTarget {
|
||||
private compositor = new SurfaceCompositor(G2_LENS_WIDTH, G2_LENS_HEIGHT);
|
||||
private onFrameComposited: (() => void) | null = null;
|
||||
private released = false;
|
||||
|
||||
/** Receive a callback per applied frame, the stand-in for the connected path's frame-finished callback. */
|
||||
activate(onFrameComposited: () => void): void {
|
||||
this.onFrameComposited = onFrameComposited;
|
||||
}
|
||||
|
||||
release(): void {
|
||||
this.released = true;
|
||||
this.onFrameComposited = null;
|
||||
}
|
||||
|
||||
private composited(): void {
|
||||
if (!this.released) this.onFrameComposited?.();
|
||||
}
|
||||
|
||||
async configureCompositorScreen(width: number, height: number): Promise<void> {
|
||||
if (Math.round(width) !== this.compositor.width || Math.round(height) !== this.compositor.height) {
|
||||
this.compositor = new SurfaceCompositor(Math.round(width), Math.round(height));
|
||||
}
|
||||
}
|
||||
|
||||
async configureSurface(id: string, options: SurfaceOptions): Promise<void> {
|
||||
this.compositor.configureSurface(id, {
|
||||
x: Math.round(options.x), y: Math.round(options.y), width: Math.round(options.width), height: Math.round(options.height),
|
||||
zOrder: Math.round(options.zOrder), transparency: options.transparency,
|
||||
});
|
||||
}
|
||||
|
||||
async removeSurface(id: string): Promise<void> {
|
||||
this.compositor.removeSurface(id);
|
||||
this.composited();
|
||||
}
|
||||
|
||||
async setSurfaceVisible(id: string, visible: boolean): Promise<void> {
|
||||
this.compositor.setSurfaceVisible(id, Boolean(visible));
|
||||
this.composited();
|
||||
}
|
||||
|
||||
async setSurfaceDepth(id: string, depth: number): Promise<void> {
|
||||
this.compositor.setSurfaceDepth(id, Math.round(depth));
|
||||
}
|
||||
|
||||
async setUnderlayDim(belowZOrder: number, factor: number): Promise<void> {
|
||||
this.compositor.setUnderlayDim(Math.round(belowZOrder), Math.max(0, Math.min(1, factor)));
|
||||
this.composited();
|
||||
}
|
||||
|
||||
async setScreenBlanked(blanked: boolean): Promise<void> {
|
||||
this.compositor.setScreenBlanked(Boolean(blanked));
|
||||
this.composited();
|
||||
}
|
||||
|
||||
async submitSurfaceFrame(
|
||||
surfaceId: string,
|
||||
pixels8bpp: Uint8Array,
|
||||
rect: { x: number; y: number; width: number; height: number },
|
||||
_fingerprint: string,
|
||||
_paintMs = -1,
|
||||
_frameId = 0,
|
||||
glyphs: ArrayBuffer | null = null,
|
||||
): Promise<void> {
|
||||
this.compositor.submitSurfaceFrame(surfaceId, pixels8bpp,
|
||||
{ x: Math.round(rect.x), y: Math.round(rect.y), width: Math.round(rect.width), height: Math.round(rect.height) }, glyphs);
|
||||
this.composited();
|
||||
}
|
||||
|
||||
async submitShellScene(bytes: Uint8Array, _paintMs = 0, _frameId = 0): Promise<void> {
|
||||
this.compositor.setShellScene(bytes);
|
||||
this.composited();
|
||||
}
|
||||
|
||||
/** Nothing transmits: every frame is finished as soon as it is composited. */
|
||||
waitForFrameFinished(frameId: number, _timeoutMs: number): Promise<string | null> {
|
||||
return Promise.resolve(frameId > 0 ? "composited" : null);
|
||||
}
|
||||
|
||||
/** The current composite as gray pixels (row-major, compositor size). */
|
||||
composite(): Uint8Array {
|
||||
return this.compositor.composite();
|
||||
}
|
||||
|
||||
getCompositePreview(green = false): ImageSource | null {
|
||||
return previewPixels(this.compositor.composite(), this.compositor.width, this.compositor.height, green);
|
||||
}
|
||||
|
||||
/** Save the current composite as a 4-bit grayscale PNG under Documents; cropping is not supported on iOS. */
|
||||
saveScreenshot(crop?: { x: number; y: number; width: number; height: number }): string {
|
||||
if (crop) return "";
|
||||
const png = FaceclawKitIosProtocol.new().pngDataWidthHeight(toData(this.compositor.composite()), this.compositor.width, this.compositor.height);
|
||||
if (!png) return "";
|
||||
const target = path.join(knownFolders.documents().path, `screen-${timestamp()}.png`);
|
||||
File.fromPath(target).writeSync(png);
|
||||
return target;
|
||||
}
|
||||
|
||||
startScreenRecording(): void {}
|
||||
|
||||
recordScreenFrame(): void {}
|
||||
|
||||
stopScreenRecording(): string { return ""; }
|
||||
}
|
||||
@@ -19,9 +19,11 @@ export type DisplayTarget = Pick<
|
||||
| "configureSurface"
|
||||
| "removeSurface"
|
||||
| "setSurfaceVisible"
|
||||
| "setSurfaceDepth"
|
||||
| "setUnderlayDim"
|
||||
| "setScreenBlanked"
|
||||
| "submitSurfaceFrame"
|
||||
| "submitShellScene"
|
||||
| "waitForFrameFinished"
|
||||
| "getCompositePreview"
|
||||
| "saveScreenshot"
|
||||
@@ -97,6 +99,14 @@ export class PreviewDisplayTarget implements DisplayTarget {
|
||||
this.compositor.setSurfaceVisible(id, Boolean(visible));
|
||||
}
|
||||
|
||||
async setSurfaceDepth(id: string, depth: number): Promise<void> {
|
||||
this.compositor.setSurfaceDepth(id, Math.round(depth));
|
||||
}
|
||||
|
||||
async submitShellScene(bytes: Uint8Array, paintMs = 0, frameId = 0): Promise<void> {
|
||||
this.compositor.submitShellScene(new Uint8Array(bytes).buffer, paintMs, frameId);
|
||||
}
|
||||
|
||||
async setUnderlayDim(belowZOrder: number, factor: number): Promise<void> {
|
||||
this.compositor.setUnderlayDim(Math.round(belowZOrder), dimFactor256(factor));
|
||||
}
|
||||
|
||||
@@ -1,13 +1,16 @@
|
||||
import { createListenerPool, type InterfaceAddress } from '../remote/listeners';
|
||||
declare const FaceclawRemoteInput: any;
|
||||
export function remoteNative() {
|
||||
declare const FaceclawKitIosRemoteInput: any;
|
||||
/** The shared Kotlin RemoteInputSession over POSIX sockets; request notifications arrive on the main queue. */
|
||||
export function remoteNative(onRequestReady: () => void) {
|
||||
return createListenerPool(() => {
|
||||
const native = FaceclawRemoteInput.alloc().init();
|
||||
return { start: (port, address) => String(native.startAddress(port, address)), stop: () => native.stop(),
|
||||
nextRequest: () => native.nextRequest(), complete: (id, response) => native.completeResponse(id, response) };
|
||||
const native = FaceclawKitIosRemoteInput.new();
|
||||
native.setRequestListenerListener(onRequestReady);
|
||||
return { start: (port, address) => String(native.startAddressPortAddress(port, address)), stop: () => { native.setRequestListenerListener(null); native.stop(); },
|
||||
nextRequest: () => native.nextRequest(), complete: (id, response) => native.completeIdValue(id, response) };
|
||||
});
|
||||
}
|
||||
export function remoteInterfaces(): InterfaceAddress[] { return JSON.parse(String(FaceclawRemoteInput.interfaces())); }
|
||||
export function randomTokenSecret(): string { return String(FaceclawRemoteInput.shared().randomSecret()); }
|
||||
export function tokenHash(value: string): string { return String(FaceclawRemoteInput.shared().tokenDigest(value)); }
|
||||
function shared(): any { return FaceclawKitIosRemoteInput.companion.shared; }
|
||||
export function remoteInterfaces(): InterfaceAddress[] { return JSON.parse(String(shared().interfaces())); }
|
||||
export function randomTokenSecret(): string { return String(shared().randomSecret()); }
|
||||
export function tokenHash(value: string): string { return String(shared().tokenDigestValue(value)); }
|
||||
export function copyRemoteToken(value: string): void { UIPasteboard.generalPasteboard.string = value; }
|
||||
@@ -1,10 +1,12 @@
|
||||
import { createListenerPool, type InterfaceAddress } from '../remote/listeners';
|
||||
import { Utils } from '@nativescript/core';
|
||||
declare const com: any;
|
||||
export function remoteNative() {
|
||||
export function remoteNative(onRequestReady: () => void) {
|
||||
return createListenerPool(() => {
|
||||
const native = new com.faceclaw.app.FaceclawRemoteInput();
|
||||
return { start: (port, address) => String(native.startAddress(port, address)), stop: () => native.stop(),
|
||||
const ready = new java.lang.Runnable({ run: onRequestReady });
|
||||
native.setRequestListener(ready);
|
||||
return { start: (port, address) => String(native.startAddress(port, address)), stop: () => { native.setRequestListener(null); native.stop(); },
|
||||
nextRequest: () => native.nextRequest(), complete: (id, response) => native.complete(id, response) };
|
||||
});
|
||||
}
|
||||
|
||||
@@ -18,6 +18,12 @@ function markChanged(key: string): void {
|
||||
}
|
||||
let pollTimer: ReturnType<typeof setInterval> | null = null
|
||||
const listeners = new Set<(key: string) => void>()
|
||||
export function disposeSettingsStore(): void {
|
||||
listeners.clear()
|
||||
if (pollTimer !== null) clearInterval(pollTimer)
|
||||
pollTimer = null
|
||||
observed.clear()
|
||||
}
|
||||
function changed(key: string): void {
|
||||
setTimeout(() => { for (const listener of [...listeners]) listener(key) }, 0)
|
||||
}
|
||||
|
||||
@@ -14,6 +14,13 @@ let javaInstance: any = null;
|
||||
let retainedListenerProxy: any = null;
|
||||
const changeListeners = new Set<(key: string) => void>();
|
||||
|
||||
/** Release this isolate's native observer before its worker is terminated. */
|
||||
export function disposeSettingsStore(): void {
|
||||
changeListeners.clear();
|
||||
if (retainedListenerProxy !== null) getJava().unregisterListener(retainedListenerProxy);
|
||||
retainedListenerProxy = null;
|
||||
}
|
||||
|
||||
function getJava(): any {
|
||||
if (javaInstance === null) {
|
||||
const context = Utils.android?.getApplicationContext?.();
|
||||
|
||||
+12
-21
@@ -1,28 +1,19 @@
|
||||
import { type SseListener } from './sse-types';
|
||||
import { withUserAgent } from '../util/http';
|
||||
import { kotlinListener } from './kotlin-listener.ios';
|
||||
export { type SseListener } from './sse-types';
|
||||
declare const FaceclawSseRequest: any;
|
||||
declare const FaceclawKitIosSseRequest: any, FaceclawKitFaceclawSseListener: any;
|
||||
|
||||
/** Drain native events on the requesting JS isolate, never on a URLSession thread. */
|
||||
/** The shared Kotlin SseStream over NSURLSession; the Kotlin side delivers every callback on the main queue, in order. */
|
||||
export function openSseRequest(url: string, body: string, headers: Record<string, string>, listener: SseListener): { cancel(): void } {
|
||||
const native = FaceclawSseRequest.alloc().initWithURLBodyHeaders(url, body, JSON.stringify(withUserAgent(headers)));
|
||||
let ended = false;
|
||||
const cancel = () => { if (ended) return; ended = true; clearInterval(timer); native.cancel(); };
|
||||
const timer = setInterval(() => {
|
||||
try {
|
||||
for (const event of JSON.parse(native.takeEvents())) {
|
||||
if (ended) break;
|
||||
if (event.kind === 'line') listener.onLine(event.text);
|
||||
else {
|
||||
cancel();
|
||||
if (event.kind === 'complete') listener.onComplete();
|
||||
else if (event.kind === 'http-error') listener.onHttpError(event.code, event.text);
|
||||
else listener.onFailure(event.text);
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
cancel(); listener.onFailure(String((error as Error)?.message ?? error));
|
||||
}
|
||||
}, 25);
|
||||
return { cancel };
|
||||
const once = (deliver: () => void) => { if (ended) return; ended = true; deliver(); };
|
||||
const nativeListener = kotlinListener(FaceclawKitFaceclawSseListener, {
|
||||
onLineLine: (line: string) => { if (!ended) listener.onLine(String(line ?? '')); },
|
||||
onHttpErrorCodeBody: (code: number, errorBody: string) => once(() => listener.onHttpError(Number(code), String(errorBody ?? ''))),
|
||||
onComplete: () => once(() => listener.onComplete()),
|
||||
onFailureMessage_: (message: string) => once(() => listener.onFailure(String(message ?? ''))),
|
||||
});
|
||||
const native = FaceclawKitIosSseRequest.alloc().initWithUrlBodyHeadersJsonListener(url, body, JSON.stringify(withUserAgent(headers)), nativeListener);
|
||||
return { cancel: () => { if (ended) return; ended = true; native.cancel(); } };
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
import { fromData, toData, nativeArray } from './kotlin-data'
|
||||
import type { SessionTexturePlanner, TextureFrame, TexturePlan } from '../g2/texture-planner'
|
||||
declare const FaceclawKitIosTexturePlanner: any
|
||||
|
||||
export class IosTexturePlanner implements SessionTexturePlanner {
|
||||
private readonly native = FaceclawKitIosTexturePlanner.new()
|
||||
reset(): void { this.native.reset() }
|
||||
plan(previous: Uint8Array | null, next: Uint8Array, frame: TextureFrame, firstId: number): TexturePlan | null {
|
||||
const result = this.native.planPreviousNextFrameFirstId(previous ? toData(previous) : null, toData(next), frame.native, firstId)
|
||||
return result ? { payload: fromData(result.payload), uploads: nativeArray(result.uploads, fromData),
|
||||
nextFid: result.nextFid, usedBytes: result.usedBytes } : null
|
||||
}
|
||||
}
|
||||
@@ -15,6 +15,7 @@ type MainPageState = {
|
||||
model: MainViewModel
|
||||
propertyChangeHandler: (args: EventData & { propertyName?: string }) => void
|
||||
orientationHandler: () => void
|
||||
detachPreview: () => void
|
||||
}
|
||||
|
||||
function getPageState(page: Page): MainPageState | undefined {
|
||||
@@ -32,6 +33,7 @@ function cleanupPage(page: Page): void {
|
||||
return
|
||||
}
|
||||
state.model.off(Observable.propertyChangeEvent, state.propertyChangeHandler)
|
||||
state.detachPreview()
|
||||
Application.off(Application.orientationChangedEvent, state.orientationHandler)
|
||||
// navigatingTo builds a fresh model each visit; drop the old one's
|
||||
// controller/settings subscriptions or every navigation leaks a listener.
|
||||
@@ -130,6 +132,8 @@ export function loaded(args: EventData) {
|
||||
|
||||
const state: MainPageState = {
|
||||
model,
|
||||
detachPreview: dashboardController.attachPhonePreview(() =>
|
||||
page.isLoaded && !model.displayPreviewMessage && (!isAndroid || !!page.android?.isShown())),
|
||||
orientationHandler: () => {
|
||||
setTimeout(() => {
|
||||
model.refreshLayoutMetrics()
|
||||
|
||||
+70
-44
@@ -106,50 +106,76 @@
|
||||
<!-- Keyboard-input mode: anchored to the bottom of the (keyboard-
|
||||
shrunk) page, so it sits in the space below the mirror and only
|
||||
overlaps the mirror when the open keyboard leaves too little
|
||||
room. The tabbed controls are collapsed while it is up. -->
|
||||
<StackLayout
|
||||
class="system-name-modal"
|
||||
visibility="{{ textSettingEditorVisibility }}"
|
||||
horizontalAlignment="stretch"
|
||||
verticalAlignment="bottom">
|
||||
<Label text="{{ activeTextEditorTitle }}" class="h2" />
|
||||
<!-- Per-field labels only matter with two fields; a single-field
|
||||
edit's field title is the same string as the editor title. -->
|
||||
<Label text="{{ activeTextSettingTitle }}" visibility="{{ secondaryTextSettingVisibility }}" />
|
||||
<TextField
|
||||
id="settingsTextField"
|
||||
text="{{ activeTextSettingValue }}"
|
||||
textChange="{{ onTextSettingTextChange }}"
|
||||
returnPress="{{ onPrimaryTextSettingReturnPress }}"
|
||||
autocorrect="false"
|
||||
autocapitalizationType="none"
|
||||
keyboardType="{{ activeTextSettingKeyboardType }}"
|
||||
secure="{{ activeTextSettingSecure }}"
|
||||
returnKeyType="{{ primaryTextSettingReturnKeyType }}" />
|
||||
<StackLayout visibility="{{ secondaryTextSettingVisibility }}">
|
||||
<Label text="{{ secondaryTextSettingTitle }}" />
|
||||
<TextField
|
||||
id="secondarySettingsTextField"
|
||||
text="{{ secondaryTextSettingValue }}"
|
||||
textChange="{{ onSecondaryTextSettingTextChange }}"
|
||||
returnPress="{{ onSecondaryTextSettingReturnPress }}"
|
||||
autocorrect="false"
|
||||
autocapitalizationType="none"
|
||||
keyboardType="{{ secondaryTextSettingKeyboardType }}"
|
||||
secure="{{ secondaryTextSettingSecure }}"
|
||||
returnKeyType="done" />
|
||||
</StackLayout>
|
||||
<GridLayout
|
||||
columns="*,auto"
|
||||
visibility="{{ activeTextEditorToggleVisibility }}"
|
||||
class="m-t-8">
|
||||
<Label col="0" text="{{ activeTextEditorToggleLabel }}" verticalAlignment="center" />
|
||||
<Switch
|
||||
col="1"
|
||||
checked="{{ activeTextEditorToggleValue }}"
|
||||
checkedChange="{{ onTextSettingToggleChange }}" />
|
||||
</GridLayout>
|
||||
</StackLayout>
|
||||
room. The tabbed controls are collapsed while it is up.
|
||||
|
||||
NativeScript has no maxHeight, so the transparent outer grid's
|
||||
fixed height is the cap: it shrinks to the page when the keyboard
|
||||
leaves less room than that, and the bottom-aligned modal inside
|
||||
sizes to its content within it. Past the cap the fields scroll
|
||||
and the buttons stay put.
|
||||
|
||||
Any GridLayout in here needs an explicit rows="auto": the implicit
|
||||
single star row stretches to fill all the height offered (Stack-
|
||||
and DockLayout offer it AT_MOST the remaining height), which is
|
||||
what made the form balloon with the keyboard minimized. -->
|
||||
<GridLayout
|
||||
height="440"
|
||||
verticalAlignment="bottom"
|
||||
isPassThroughParentEnabled="true"
|
||||
visibility="{{ textSettingEditorVisibility }}">
|
||||
<DockLayout
|
||||
class="system-name-modal"
|
||||
stretchLastChild="true"
|
||||
horizontalAlignment="stretch"
|
||||
verticalAlignment="bottom">
|
||||
<Label dock="top" text="{{ activeTextEditorTitle }}" class="h2" />
|
||||
<GridLayout dock="bottom" columns="*,*" rows="auto" class="m-t-8">
|
||||
<Button col="0" text="Cancel" tap="{{ onTextSettingCancelTap }}" />
|
||||
<Button col="1" text="Submit" tap="{{ onTextSettingSubmitTap }}" class="-primary" />
|
||||
</GridLayout>
|
||||
<ScrollView>
|
||||
<StackLayout>
|
||||
<!-- Per-field labels only matter with two fields; a single-field
|
||||
edit's field title is the same string as the editor title. -->
|
||||
<Label text="{{ activeTextSettingTitle }}" visibility="{{ secondaryTextSettingVisibility }}" />
|
||||
<TextField
|
||||
id="settingsTextField"
|
||||
text="{{ activeTextSettingValue }}"
|
||||
textChange="{{ onTextSettingTextChange }}"
|
||||
returnPress="{{ onPrimaryTextSettingReturnPress }}"
|
||||
autocorrect="false"
|
||||
autocapitalizationType="none"
|
||||
keyboardType="{{ activeTextSettingKeyboardType }}"
|
||||
secure="{{ activeTextSettingSecure }}"
|
||||
returnKeyType="{{ primaryTextSettingReturnKeyType }}" />
|
||||
<StackLayout visibility="{{ secondaryTextSettingVisibility }}">
|
||||
<Label text="{{ secondaryTextSettingTitle }}" />
|
||||
<TextField
|
||||
id="secondarySettingsTextField"
|
||||
text="{{ secondaryTextSettingValue }}"
|
||||
textChange="{{ onSecondaryTextSettingTextChange }}"
|
||||
returnPress="{{ onSecondaryTextSettingReturnPress }}"
|
||||
autocorrect="false"
|
||||
autocapitalizationType="none"
|
||||
keyboardType="{{ secondaryTextSettingKeyboardType }}"
|
||||
secure="{{ secondaryTextSettingSecure }}"
|
||||
returnKeyType="done" />
|
||||
</StackLayout>
|
||||
<GridLayout
|
||||
columns="*,auto"
|
||||
rows="auto"
|
||||
visibility="{{ activeTextEditorToggleVisibility }}"
|
||||
class="m-t-8">
|
||||
<Label col="0" text="{{ activeTextEditorToggleLabel }}" verticalAlignment="center" />
|
||||
<Switch
|
||||
col="1"
|
||||
checked="{{ activeTextEditorToggleValue }}"
|
||||
checkedChange="{{ onTextSettingToggleChange }}" />
|
||||
</GridLayout>
|
||||
</StackLayout>
|
||||
</ScrollView>
|
||||
</DockLayout>
|
||||
</GridLayout>
|
||||
<!-- Keyboard-input panel (the keyboard button beside the mic button):
|
||||
the phone's end of the keyboard dialog on the glasses
|
||||
(ui/shell/keyboard-input.ts). Same placement and collapsing rules
|
||||
|
||||
@@ -8,6 +8,7 @@ import {
|
||||
Screen,
|
||||
SwipeDirection,
|
||||
type GestureEventData,
|
||||
type TextField,
|
||||
type SwipeGestureEventData,
|
||||
type TouchGestureEventData,
|
||||
type View,
|
||||
@@ -988,6 +989,26 @@ export class MainViewModel extends RemoteControlsViewModel {
|
||||
dashboardController.finishActiveTextSettingEdit();
|
||||
}
|
||||
|
||||
/** The editor's Submit button: the same as the done key on the last field. */
|
||||
onTextSettingSubmitTap(args: { object?: View }): void {
|
||||
// Commit both fields' actual text, in case a final keystroke's textChange
|
||||
// hadn't landed yet (as the return-press handlers do).
|
||||
const page = args?.object?.page;
|
||||
const primaryText = page?.getViewById<TextField>("settingsTextField")?.text;
|
||||
if (typeof primaryText === "string") {
|
||||
dashboardController.setActiveTextSettingValue(primaryText, this.activeTextSettingId ?? undefined);
|
||||
}
|
||||
const secondaryText = page?.getViewById<TextField>("secondarySettingsTextField")?.text;
|
||||
if (this.hasSecondaryTextSetting && typeof secondaryText === "string") {
|
||||
dashboardController.setActiveTextSettingValue(secondaryText, this.secondaryTextSettingId ?? undefined);
|
||||
}
|
||||
dashboardController.finishActiveTextSettingEdit();
|
||||
}
|
||||
|
||||
onTextSettingCancelTap(): void {
|
||||
dashboardController.cancelActiveTextSettingEdit();
|
||||
}
|
||||
|
||||
onTextSettingToggleChange(args: { value?: boolean; object?: { checked?: boolean } }): void {
|
||||
dashboardController.setActiveTextEditorToggleValue(
|
||||
args.object?.checked ?? args.value ?? false,
|
||||
|
||||
@@ -102,7 +102,7 @@ export class RemoteControlsViewModel extends Observable {
|
||||
const raw = typeof args.value === "number" ? args.value : Number(args.object?.value ?? NaN);
|
||||
if (!Number.isFinite(raw)) return;
|
||||
// The setting only has every tenth level; snap to the nearest.
|
||||
const level = Math.min(100, Math.max(0, Math.round(raw / 10) * 10));
|
||||
const level = Math.min(100, Math.max(2, Math.round(raw / 10) * 10));
|
||||
this.lastManualBrightness = level;
|
||||
const value = String(level) as BrightnessSetting;
|
||||
if (brightnessSetting.get() !== value) brightnessSetting.set(value);
|
||||
|
||||
@@ -43,7 +43,7 @@
|
||||
</GridLayout>
|
||||
<GridLayout rows="auto" columns="auto,*,auto" class="settings-row">
|
||||
<Label col="0" text="☀" class="brightness-icon" verticalAlignment="center" />
|
||||
<Slider col="1" width="100%" minValue="0" maxValue="100" value="{{ brightnessPercent }}" valueChange="{{ onBrightnessChange }}" isEnabled="{{ brightnessSliderEnabled }}" />
|
||||
<Slider col="1" width="100%" minValue="2" maxValue="100" value="{{ brightnessPercent }}" valueChange="{{ onBrightnessChange }}" isEnabled="{{ brightnessSliderEnabled }}" />
|
||||
<StackLayout col="2" orientation="horizontal" verticalAlignment="center" class="auto-toggle">
|
||||
<Label text="Auto" verticalAlignment="center" />
|
||||
<Switch checked="{{ brightnessAuto }}" checkedChange="{{ onBrightnessAutoChange }}" />
|
||||
|
||||
+22
-19
@@ -10,17 +10,33 @@ export function remoteInputStatus(): string { return status; }
|
||||
export function remoteInterfaceSelection(): string { return getStringSetting(INTERFACE_STORAGE_KEY, 'tailscale'); }
|
||||
export function selectRemoteInterface(value: string): void { setStringSetting(INTERFACE_STORAGE_KEY, value); }
|
||||
|
||||
/** Owned by the main controller. Native socket work never enters a JS isolate. */
|
||||
/** Owned by the main controller. Native sockets notify JS only for complete requests. */
|
||||
export function startRemoteInput(host: RemoteHost): () => void {
|
||||
const native = remoteNative();
|
||||
let timer: ReturnType<typeof setInterval> | null = null;
|
||||
const native = remoteNative(() => { void drain(); });
|
||||
let busy = false;
|
||||
let stopped = false;
|
||||
let networkTimer: ReturnType<typeof setInterval> | null = null;
|
||||
// A notification while handling another request is covered by the next drain
|
||||
// iteration. No timer or native bridge calls are needed while sockets are idle.
|
||||
const drain = async () => {
|
||||
if (busy || stopped) return;
|
||||
busy = true;
|
||||
try {
|
||||
while (!stopped) {
|
||||
const raw = native.nextRequest();
|
||||
if (!raw) break;
|
||||
const request = JSON.parse(String(raw));
|
||||
if (Date.now() >= request.expiresAt) continue;
|
||||
let reply;
|
||||
try { reply = await handleRequest(request.body, remoteTokens, host); }
|
||||
catch { reply = { ok: false, error: 'failed', message: 'Request failed.' }; }
|
||||
if (!stopped) native.complete(request.id, JSON.stringify(reply));
|
||||
}
|
||||
} finally { busy = false; }
|
||||
};
|
||||
const sync = () => {
|
||||
if (!remoteTokens.list().length) {
|
||||
native.stop();
|
||||
if (timer !== null) clearInterval(timer);
|
||||
timer = null;
|
||||
if (networkTimer !== null) clearInterval(networkTimer);
|
||||
networkTimer = null;
|
||||
status = 'No tokens; listener stopped.';
|
||||
@@ -34,21 +50,8 @@ export function startRemoteInput(host: RemoteHost): () => void {
|
||||
if (failure) status += `\n${failure}`;
|
||||
// Reconcile after VPN reconnects/address changes; unchanged sockets stay open.
|
||||
if (networkTimer === null) networkTimer = setInterval(sync, 3000);
|
||||
if (timer !== null) return;
|
||||
timer = setInterval(() => {
|
||||
if (busy) return;
|
||||
const raw = native.nextRequest();
|
||||
if (!raw) return;
|
||||
const request = JSON.parse(String(raw));
|
||||
if (Date.now() >= request.expiresAt) return;
|
||||
busy = true;
|
||||
void handleRequest(request.body, remoteTokens, host)
|
||||
.then(reply => native.complete(request.id, JSON.stringify(reply)))
|
||||
.catch(() => native.complete(request.id, JSON.stringify({ ok: false, error: 'failed', message: 'Request failed.' })))
|
||||
.finally(() => { busy = false; });
|
||||
}, 15);
|
||||
};
|
||||
sync();
|
||||
const off = onSettingsStoreChanged(key => { if (key === TOKEN_STORAGE_KEY || key === INTERFACE_STORAGE_KEY) sync(); });
|
||||
return () => { off(); if (timer !== null) clearInterval(timer); if (networkTimer !== null) clearInterval(networkTimer); native.stop(); };
|
||||
return () => { stopped = true; off(); if (networkTimer !== null) clearInterval(networkTimer); native.stop(); };
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
import { normalizeNightscoutThreshold, type NightscoutThresholds } from "../apps/nightscout/nightscout-alerts";
|
||||
import { GESTURE_DOUBLE_CLICK, InputEvent } from "./gestures";
|
||||
import { DEFAULT_BRIGHTNESS_CURVE, normalizeBrightnessCurve, brightnessCurveError } from "../g2/brightness-curve";
|
||||
import {
|
||||
getBooleanSetting,
|
||||
getStringSetting,
|
||||
@@ -30,10 +31,9 @@ export type BatteryDisplayMode = "icon" | "percentage" | "stacked" | "stacked-pe
|
||||
export type BatteryIndicatorVisibility = "always" | "low" | "never";
|
||||
export type TimeFormat = "24h" | "12h";
|
||||
export type ScreenTimeoutSetting = "15s" | "30s" | "1m" | "3m" | "never";
|
||||
// "auto" lets the glasses' ambient-light sensor drive brightness; the numeric
|
||||
// values are exact levels on the firmware's 0-100 scale (nonlinear: ~30 is
|
||||
// dim-but-readable indoors, ~60 is bright outdoors).
|
||||
export const BRIGHTNESS_VALUES = ["auto", "0", "10", "20", "30", "40", "50", "60", "70", "80", "90", "100"] as const;
|
||||
// Auto runs Faceclaw's light curve. Numeric values use the calibrated firmware
|
||||
// scale, clamped to 2–100; 2 is also the endpoint for screen fades.
|
||||
export const BRIGHTNESS_VALUES = ["auto", "2", "10", "20", "30", "40", "50", "60", "70", "80", "90", "100"] as const;
|
||||
export type BrightnessSetting = (typeof BRIGHTNESS_VALUES)[number];
|
||||
export type WakeWordAction = "voice-input" | "off" | "turn-screen-on";
|
||||
|
||||
@@ -173,6 +173,7 @@ type ConfigSettingStringOptions<TId extends string> = ConfigSettingOptions<strin
|
||||
glassesEditTitle?: string;
|
||||
inputKind?: "text" | "email" | "password";
|
||||
normalize?: (value: string | null | undefined) => string;
|
||||
validate?: (value: string) => string | null;
|
||||
};
|
||||
|
||||
// Every string setting by id, so isolates that can only pass an id over a
|
||||
@@ -189,6 +190,7 @@ export class ConfigSettingString<TId extends string = string> extends ConfigSett
|
||||
readonly glassesEditTitle: string;
|
||||
readonly inputKind: "text" | "email" | "password";
|
||||
private readonly normalizer: (value: string | null | undefined) => string;
|
||||
private readonly validator?: (value: string) => string | null;
|
||||
|
||||
constructor(options: ConfigSettingStringOptions<TId>) {
|
||||
super(options);
|
||||
@@ -196,6 +198,7 @@ export class ConfigSettingString<TId extends string = string> extends ConfigSett
|
||||
this.glassesEditTitle = options.glassesEditTitle ?? `Edit ${options.label}`;
|
||||
this.inputKind = options.inputKind ?? "text";
|
||||
this.normalizer = options.normalize ?? ((value) => value ?? "");
|
||||
this.validator = options.validate;
|
||||
stringSettingsById.set(this.id, this);
|
||||
}
|
||||
|
||||
@@ -205,9 +208,23 @@ export class ConfigSettingString<TId extends string = string> extends ConfigSett
|
||||
|
||||
set(value: string): string {
|
||||
const normalized = this.normalizer(value);
|
||||
// Preserve drafts for the editor/preview; consumers can use getValidValue().
|
||||
if (this.validator) {
|
||||
const candidate = this.validator(normalized) ? this.get() : normalized;
|
||||
if (!this.validator(candidate)) setStringSetting(this.storageKey + ".valid", candidate);
|
||||
}
|
||||
setStringSetting(this.storageKey, normalized);
|
||||
return normalized;
|
||||
}
|
||||
|
||||
validationError(value = this.get()): string | null { return this.validator?.(value) ?? null; }
|
||||
|
||||
getValidValue(): string {
|
||||
const value = this.get();
|
||||
if (!this.validationError(value)) return value;
|
||||
const saved = this.normalizer(getStringSetting(this.storageKey + ".valid", this.defaultValue));
|
||||
return this.validationError(saved) ? this.defaultValue : saved;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -321,10 +338,36 @@ export const brightnessSetting = new ConfigSettingEnum<BrightnessSetting>({
|
||||
storageKey: "display.brightness",
|
||||
defaultValue: "auto",
|
||||
values: BRIGHTNESS_VALUES,
|
||||
normalize: (value) => value === "0" ? "2" : BRIGHTNESS_VALUES.includes(value as BrightnessSetting) ? value as BrightnessSetting : "auto",
|
||||
formatValue: brightnessLabel,
|
||||
description: "Display brightness. Auto lets the ambient-light sensor pick the level; numbers set an exact level. After picking Auto, it may take some time to first adjust.",
|
||||
description: "Auto uses Faceclaw's ambient-light curve; numbers set a fixed level. 2 is the dimmest level.",
|
||||
});
|
||||
|
||||
const AUTO_BRIGHTNESS_LEVELS = ["2", "5", "10", "20", "25", "30", "40", "50", "60", "70", "80", "90", "100"] as const;
|
||||
export const autoBrightnessMinSetting = new ConfigSettingEnum({
|
||||
id: "autoBrightnessMin", label: "Auto minimum", storageKey: "display.autoBrightnessMin",
|
||||
defaultValue: "20", values: AUTO_BRIGHTNESS_LEVELS,
|
||||
description: "Lowest brightness Auto will select. Screen fades can still reach 2.",
|
||||
});
|
||||
export const autoBrightnessMaxSetting = new ConfigSettingEnum({
|
||||
id: "autoBrightnessMax", label: "Auto maximum", storageKey: "display.autoBrightnessMax",
|
||||
defaultValue: "100", values: AUTO_BRIGHTNESS_LEVELS,
|
||||
description: "Highest brightness Auto will select. A value below the minimum uses the minimum.",
|
||||
});
|
||||
export const autoBrightnessCurveSetting = new ConfigSettingString({
|
||||
id: "autoBrightnessCurve", label: "Auto light curve", storageKey: "display.autoBrightnessCurve",
|
||||
defaultValue: DEFAULT_BRIGHTNESS_CURVE, normalize: normalizeBrightnessCurve, validate: brightnessCurveError,
|
||||
description: "2–16 lux:percent pairs, separated by commas. Percent is within your minimum–maximum range. Start at 0:0, end at 100%, and increase lux without decreasing percent. Incomplete edits stay in the preview; brightness keeps using the last valid curve.",
|
||||
});
|
||||
const SCREEN_FADE_MS = 280;
|
||||
|
||||
export function getBrightnessPreferences() {
|
||||
const level = brightnessSettingToLevel(brightnessSetting.get());
|
||||
return { auto: level === null, level: level ?? 50,
|
||||
minimum: Number(autoBrightnessMinSetting.get()), maximum: Number(autoBrightnessMaxSetting.get()),
|
||||
curve: autoBrightnessCurveSetting.getValidValue(), fadeMs: SCREEN_FADE_MS };
|
||||
}
|
||||
|
||||
export const screenTimeoutSetting = new ConfigSettingEnum<ScreenTimeoutSetting>({
|
||||
id: "screen-timeout",
|
||||
label: "Screen timeout",
|
||||
@@ -1148,6 +1191,9 @@ export class EditTextSettingLayer implements Layer {
|
||||
image.drawText(font, 22, messageTop + index * step, message[index]!, 200);
|
||||
}
|
||||
image.drawText(font, 22, messageTop + (message.length + 2) * step, truncateSetting(this.setting.get(), 52), 220);
|
||||
const error = this.setting.validationError();
|
||||
if (error) wrapText(font, error, width - 48).forEach((line, index) =>
|
||||
image.drawText(font, 22, messageTop + (message.length + 4 + index) * step, line, 180));
|
||||
image.drawText(font, 22, height - 24 - font.lineHeight, `${GESTURE_DOUBLE_CLICK} back`, 110);
|
||||
return image;
|
||||
}
|
||||
|
||||
@@ -37,6 +37,9 @@ import {
|
||||
assistantSkipConfirmationSetting,
|
||||
batteryDisplayModeSetting,
|
||||
brightnessSetting,
|
||||
autoBrightnessMinSetting,
|
||||
autoBrightnessMaxSetting,
|
||||
autoBrightnessCurveSetting,
|
||||
glassesBatteryVisibilitySetting,
|
||||
phoneBatteryVisibilitySetting,
|
||||
ringBatteryVisibilitySetting,
|
||||
@@ -97,6 +100,7 @@ function settingsSections(): SettingsSection[] {
|
||||
// Auto (ambient sensor) or an exact level; pushed to the glasses by
|
||||
// the dashboard controller when changed and on each connect.
|
||||
enumSettingMenuItem(brightnessSetting),
|
||||
autoBrightnessMenuItem(),
|
||||
enumSettingMenuItem(screenTimeoutSetting, {
|
||||
onChange: () => {
|
||||
shell.noteUserActivity();
|
||||
@@ -261,6 +265,20 @@ function settingsSections(): SettingsSection[] {
|
||||
});
|
||||
}
|
||||
|
||||
function autoBrightnessMenuItem(): MenuItem {
|
||||
return {
|
||||
label: "Auto-brightness",
|
||||
description: "Adjust the minimum, maximum, and ambient-light curve used by Auto brightness.",
|
||||
onSelect: (ctx) => {
|
||||
openSettingsSubMenu(ctx, "Auto-brightness", [
|
||||
enumSettingMenuItem(autoBrightnessMinSetting),
|
||||
enumSettingMenuItem(autoBrightnessMaxSetting),
|
||||
textSettingMenuItem(autoBrightnessCurveSetting),
|
||||
]);
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* The Display section's "Battery indicators" row: opens a modal submenu with
|
||||
* the style (icon / percentage / stacked) and, per device, when its
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
import { getDefaultSmallFont } from "../../graphics/ui-fonts";
|
||||
import { GrayImage, type UiFont } from "../../graphics/image";
|
||||
import { wrapText } from "../../graphics/textwrap";
|
||||
import { clamp } from "../../util/numeric-util";
|
||||
import { InputEvent } from "../gestures";
|
||||
import { Layer, LayerContext, PaintBelow } from "../layers";
|
||||
import { drawSelectionHighlight, isMenuItemDisabled, MenuItem, MenuLayer, openModalMenu } from "../menu";
|
||||
import { isMenuItemDisabled, MenuItem, MenuLayer, openModalMenu } from "../menu";
|
||||
import { Menu } from "../menu-core";
|
||||
import { LIST_ROW_TEXT_INSET, lineStep, listRowHeight } from "../metrics";
|
||||
import { shell } from "../shell/shell";
|
||||
|
||||
@@ -30,6 +30,10 @@ export type SettingsSection = {
|
||||
|
||||
const PAD = 8;
|
||||
const LEFT_W = 150;
|
||||
/** Gap between a row's selection box and the next row. */
|
||||
const ROW_GAP = 2;
|
||||
/** Horizontal inset of row text (and custom row content) from its selection box. */
|
||||
const ROW_TEXT_X = 10;
|
||||
const MAX_DESCRIPTION_LINES = 3;
|
||||
// A throwaway menu to satisfy MenuItem.onSelect's second parameter; the
|
||||
// settings items never use it (they act via ctx only).
|
||||
@@ -45,23 +49,62 @@ export class SettingsPanelLayer implements Layer {
|
||||
// Watch swipes map onto the two columns: right goes into a section's
|
||||
// items, left comes back out (and out to the sidebar from the left column).
|
||||
readonly acceptsDirectional = true;
|
||||
private leftIndex = 0;
|
||||
private rightIndex = 0;
|
||||
private focus: "left" | "right" = "left";
|
||||
private leftScroll = 0;
|
||||
private rightScroll = 0;
|
||||
private readonly leftMenu: Menu<SettingsSection>;
|
||||
/** The highlighted section's items; unselected while focus is in the left column (a preview). */
|
||||
private readonly rightMenu: Menu<MenuItem>;
|
||||
/** The context of the paint in progress, handed to item render callbacks. */
|
||||
private paintCtx: LayerContext | null = null;
|
||||
|
||||
constructor(private readonly sections: SettingsSection[]) {}
|
||||
constructor(private readonly sections: SettingsSection[]) {
|
||||
this.leftMenu = new Menu<SettingsSection>({
|
||||
items: sections,
|
||||
wrap: true,
|
||||
rowGap: ROW_GAP,
|
||||
highlight: { radius: 6 },
|
||||
getHeight: () => listRowHeight(getDefaultSmallFont()),
|
||||
draw: ({ image, item, x, y, selected }) => {
|
||||
image.drawText(getDefaultSmallFont(), x + ROW_TEXT_X, y + LIST_ROW_TEXT_INSET, item.label, selected ? 255 : 200);
|
||||
},
|
||||
});
|
||||
this.rightMenu = new Menu<MenuItem>({
|
||||
items: this.section().items,
|
||||
selectedIndex: null,
|
||||
wrap: true,
|
||||
rowGap: ROW_GAP,
|
||||
highlight: { radius: 6 },
|
||||
getHeight: () => listRowHeight(getDefaultSmallFont()),
|
||||
draw: ({ image, item, x, y, width, height, selected }) => {
|
||||
const disabled = isMenuItemDisabled(item);
|
||||
if (item.render) {
|
||||
item.render({
|
||||
image,
|
||||
x: x + ROW_TEXT_X,
|
||||
y,
|
||||
width: width - ROW_TEXT_X - 8,
|
||||
height: height - 1,
|
||||
selected,
|
||||
disabled,
|
||||
text: item.label,
|
||||
ctx: this.paintCtx!,
|
||||
});
|
||||
} else {
|
||||
image.drawText(getDefaultSmallFont(), x + ROW_TEXT_X, y + LIST_ROW_TEXT_INSET, item.label,
|
||||
disabled ? 70 : selected ? 255 : 200);
|
||||
}
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
private section(): SettingsSection {
|
||||
return this.sections[clamp(this.leftIndex, 0, this.sections.length - 1)]!;
|
||||
return this.leftMenu.selectedItem ?? this.sections[0]!;
|
||||
}
|
||||
|
||||
/** Select a left-column section by label (deep link, e.g. from an app's menu). */
|
||||
focusSection(label: string): void {
|
||||
const index = this.sections.findIndex((section) => section.label === label);
|
||||
if (index < 0) return;
|
||||
this.leftIndex = index;
|
||||
this.leftMenu.select(index);
|
||||
this.focus = "left";
|
||||
this.resetRight();
|
||||
}
|
||||
@@ -71,27 +114,16 @@ export class SettingsPanelLayer implements Layer {
|
||||
const { width, height } = ctx.stack.getBaseSize();
|
||||
const image = new GrayImage(width, height, 0);
|
||||
const appFocused = ctx.stack.isFocused();
|
||||
|
||||
this.leftIndex = clamp(this.leftIndex, 0, this.sections.length - 1);
|
||||
const section = this.section();
|
||||
const rightItems = section.items;
|
||||
this.rightIndex = clamp(this.rightIndex, 0, Math.max(0, rightItems.length - 1));
|
||||
this.rightMenu.setItems(rightItems);
|
||||
|
||||
const rowH = listRowHeight(font);
|
||||
const top = PAD;
|
||||
const listBottom = height - PAD;
|
||||
|
||||
// Left column: section labels.
|
||||
const leftRows = Math.max(1, ((listBottom - top) / rowH) | 0);
|
||||
this.leftScroll = clampScroll(this.leftIndex, this.leftScroll, leftRows, this.sections.length);
|
||||
for (let i = this.leftScroll; i < Math.min(this.sections.length, this.leftScroll + leftRows); i++) {
|
||||
const rowY = top + (i - this.leftScroll) * rowH;
|
||||
const selected = i === this.leftIndex;
|
||||
if (selected) {
|
||||
drawSelectionHighlight(image, PAD - 2, rowY, LEFT_W, rowH - 2, appFocused && this.focus === "left", 6);
|
||||
}
|
||||
image.drawText(font, PAD + 8, rowY + LIST_ROW_TEXT_INSET, this.sections[i]!.label, selected ? 255 : 200);
|
||||
}
|
||||
this.leftMenu.paint(image, { x: PAD - 2, y: top, width: LEFT_W, height: listBottom - top },
|
||||
appFocused && this.focus === "left");
|
||||
|
||||
// Column divider (a light rule, not a full border).
|
||||
const divX = PAD + LEFT_W + 6;
|
||||
@@ -108,38 +140,15 @@ export class SettingsPanelLayer implements Layer {
|
||||
if (rightItems.length && rightTop < listBottom) {
|
||||
// Once focus is in the right column, the selected item's extended
|
||||
// description (when it has one) is drawn over the bottom of the pane by
|
||||
// the companion SettingsDescriptionOverlayLayer. The row list keeps the
|
||||
// full column height — rows the description band covers are drawn and
|
||||
// partially occluded by the overlay's plane — but the scroll clamp uses
|
||||
// the shrunk height so the selected row always stays clear of it.
|
||||
const descriptionH = this.descriptionHeight(font, rightW);
|
||||
const clampRows = Math.max(1, ((listBottom - descriptionH - rightTop) / rowH) | 0);
|
||||
const rightRows = Math.max(1, ((listBottom - rightTop) / rowH) | 0);
|
||||
this.rightScroll = clampScroll(this.rightIndex, this.rightScroll, clampRows, rightItems.length);
|
||||
for (let i = this.rightScroll; i < Math.min(rightItems.length, this.rightScroll + rightRows); i++) {
|
||||
const item = rightItems[i]!;
|
||||
const rowY = rightTop + (i - this.rightScroll) * rowH;
|
||||
// A selection only appears once focus is in the right column; before
|
||||
// that the pane is a preview of what tapping would open.
|
||||
const selected = this.focus === "right" && i === this.rightIndex;
|
||||
const disabled = isMenuItemDisabled(item);
|
||||
if (selected) {
|
||||
drawSelectionHighlight(image, rightX - 2, rowY, rightW + 2, rowH - 2, appFocused, 6);
|
||||
}
|
||||
if (item.render) {
|
||||
item.render({
|
||||
image,
|
||||
x: rightX + 8,
|
||||
y: rowY,
|
||||
width: rightW - 16,
|
||||
height: rowH - 3,
|
||||
selected,
|
||||
disabled,
|
||||
text: item.label,
|
||||
ctx,
|
||||
});
|
||||
} else {
|
||||
image.drawText(font, rightX + 8, rowY + LIST_ROW_TEXT_INSET, item.label, disabled ? 70 : selected ? 255 : 200);
|
||||
// the companion SettingsDescriptionOverlayLayer. The list stops above
|
||||
// that band, so the selected row always stays clear of it.
|
||||
const listHeight = listBottom - this.descriptionHeight(font, rightW) - rightTop;
|
||||
if (listHeight > 0) {
|
||||
this.paintCtx = ctx;
|
||||
try {
|
||||
this.rightMenu.paint(image, { x: rightX - 2, y: rightTop, width: rightW + 2, height: listHeight }, appFocused);
|
||||
} finally {
|
||||
this.paintCtx = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -150,8 +159,7 @@ export class SettingsPanelLayer implements Layer {
|
||||
/** The selected item's wrapped help text, or [] when none applies. */
|
||||
private descriptionLines(font: UiFont, rightW: number): string[] {
|
||||
if (this.focus !== "right") return [];
|
||||
const items = this.section().items;
|
||||
const item = items[clamp(this.rightIndex, 0, Math.max(0, items.length - 1))];
|
||||
const item = this.rightMenu.selectedItem;
|
||||
if (!item?.description) return [];
|
||||
return wrapText(font, item.description, rightW - 4).slice(0, MAX_DESCRIPTION_LINES);
|
||||
}
|
||||
@@ -164,9 +172,8 @@ export class SettingsPanelLayer implements Layer {
|
||||
/**
|
||||
* Draw the selected item's help text band (separator + text on an opaque
|
||||
* background) at the bottom of the right column. Called by the companion
|
||||
* overlay layer with the overlay's own canvas: as a separate plane its
|
||||
* raster covers the row list's glyphs, so it can partially occlude a row —
|
||||
* which raster drawn into the panel's own image cannot do.
|
||||
* overlay layer with the overlay's own canvas, so the band stays above
|
||||
* whatever the panel paints (including a section's detail content).
|
||||
*/
|
||||
paintDescriptionOverlay(image: GrayImage, ctx: LayerContext): void {
|
||||
const font = getDefaultSmallFont();
|
||||
@@ -186,18 +193,15 @@ export class SettingsPanelLayer implements Layer {
|
||||
}
|
||||
|
||||
async handleInput(event: InputEvent, ctx: LayerContext): Promise<void> {
|
||||
const scroll = scrollEventFor(event);
|
||||
if (this.focus === "left") {
|
||||
if (scroll) {
|
||||
const before = this.leftMenu.selectedIndex;
|
||||
await this.leftMenu.handleInput(scroll);
|
||||
if (this.leftMenu.selectedIndex !== before) this.resetRight();
|
||||
return;
|
||||
}
|
||||
switch (event.type) {
|
||||
case "scroll-up":
|
||||
case "swipe-up":
|
||||
this.leftIndex = (this.leftIndex + this.sections.length - 1) % this.sections.length;
|
||||
this.resetRight();
|
||||
return;
|
||||
case "scroll-down":
|
||||
case "swipe-down":
|
||||
this.leftIndex = (this.leftIndex + 1) % this.sections.length;
|
||||
this.resetRight();
|
||||
return;
|
||||
case "click":
|
||||
case "swipe-right":
|
||||
// Only enter sections that have something interactive on the right.
|
||||
@@ -215,38 +219,42 @@ export class SettingsPanelLayer implements Layer {
|
||||
}
|
||||
}
|
||||
|
||||
const items = this.section().items;
|
||||
if (scroll) {
|
||||
await this.rightMenu.handleInput(scroll);
|
||||
return;
|
||||
}
|
||||
switch (event.type) {
|
||||
case "scroll-up":
|
||||
case "swipe-up":
|
||||
if (items.length) this.rightIndex = (this.rightIndex + items.length - 1) % items.length;
|
||||
return;
|
||||
case "scroll-down":
|
||||
case "swipe-down":
|
||||
if (items.length) this.rightIndex = (this.rightIndex + 1) % items.length;
|
||||
return;
|
||||
case "click":
|
||||
case "swipe-right":
|
||||
if (items.length) {
|
||||
const item = items[clamp(this.rightIndex, 0, items.length - 1)]!;
|
||||
if (!isMenuItemDisabled(item)) await item.onSelect(ctx, NO_MENU);
|
||||
}
|
||||
case "swipe-right": {
|
||||
const item = this.rightMenu.selectedItem;
|
||||
if (item && !isMenuItemDisabled(item)) await item.onSelect(ctx, NO_MENU);
|
||||
return;
|
||||
}
|
||||
case "double-click":
|
||||
case "swipe-left":
|
||||
// Back to a preview: the right column keeps its scroll but shows no selection.
|
||||
this.focus = "left";
|
||||
this.rightMenu.select(null);
|
||||
return;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/** Show the highlighted section's items from the top, selected on the first only when focus is there. */
|
||||
private resetRight(): void {
|
||||
this.rightIndex = 0;
|
||||
this.rightScroll = 0;
|
||||
this.rightMenu.setItems(this.section().items, this.focus === "right" ? 0 : null);
|
||||
this.rightMenu.scrollTop = 0;
|
||||
}
|
||||
}
|
||||
|
||||
/** Scroll and watch swipe-up/down both move a column's selection. */
|
||||
function scrollEventFor(event: InputEvent): InputEvent | null {
|
||||
if (event.type === "scroll-up" || event.type === "swipe-up") return { ...event, type: "scroll-up" } as InputEvent;
|
||||
if (event.type === "scroll-down" || event.type === "swipe-down") return { ...event, type: "scroll-down" } as InputEvent;
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Companion layer for SettingsPanelLayer, sitting permanently above it in the
|
||||
* settings app's stack: paints the selected item's help text as its own plane
|
||||
@@ -276,12 +284,3 @@ export class SettingsDescriptionOverlayLayer implements Layer {
|
||||
export function openSettingsSubMenu(ctx: LayerContext, title: string, items: MenuItem[]): void {
|
||||
openModalMenu(ctx, title, items);
|
||||
}
|
||||
|
||||
function clampScroll(selected: number, scroll: number, visible: number, count: number): number {
|
||||
if (selected < scroll) {
|
||||
scroll = selected;
|
||||
} else if (selected >= scroll + visible) {
|
||||
scroll = selected - visible + 1;
|
||||
}
|
||||
return clamp(scroll, 0, Math.max(0, count - visible));
|
||||
}
|
||||
+33
-27
@@ -23,7 +23,8 @@ import {
|
||||
type UiFontSelection,
|
||||
} from "../graphics/ui-fonts";
|
||||
import { GESTURE_DOUBLE_CLICK, type InputEvent } from "./gestures";
|
||||
import { drawRightValueMenuItem, drawSelectionHighlight, openModalMenu, type MenuItem } from "./menu";
|
||||
import { drawRightValueMenuItem, openModalMenu, type MenuItem } from "./menu";
|
||||
import { Menu, type MenuDrawArgs } from "./menu-core";
|
||||
import { LIST_ROW_TEXT_INSET, listRowHeight } from "./metrics";
|
||||
import type { Layer, LayerContext } from "./layers";
|
||||
|
||||
@@ -62,7 +63,14 @@ type RowId = (typeof ROWS)[number];
|
||||
export class FontPickerLayer implements Layer {
|
||||
private readonly families: FontFamily[];
|
||||
private draft: Draft;
|
||||
private selectedRow = 0;
|
||||
private readonly menu = new Menu<RowId>({
|
||||
items: ROWS,
|
||||
wrap: true,
|
||||
rowGap: 2,
|
||||
highlight: { radius: 8 },
|
||||
getHeight: () => listRowHeight(getDefaultSmallFont()),
|
||||
draw: (args) => this.drawRow(args),
|
||||
});
|
||||
|
||||
constructor(private readonly options: FontPickerOptions) {
|
||||
this.families = collectFamilies(options.monospaceOnly);
|
||||
@@ -111,26 +119,7 @@ export class FontPickerLayer implements Layer {
|
||||
const rowWidth = width - 2 * rowX;
|
||||
const rowHeight = listRowHeight(font);
|
||||
const rowsTop = 24 + font.lineHeight;
|
||||
for (let index = 0; index < ROWS.length; index++) {
|
||||
const row = ROWS[index]!;
|
||||
const y = rowsTop + index * rowHeight;
|
||||
const disabled = this.rowDisabled(row);
|
||||
if (index === this.selectedRow) {
|
||||
drawSelectionHighlight(image, rowX - 10, y, rowWidth + 20, rowHeight - 2, focused, 8);
|
||||
}
|
||||
if (row === "save") {
|
||||
image.drawText(font, rowX, y + LIST_ROW_TEXT_INSET, "Save", index === this.selectedRow ? 255 : 200);
|
||||
continue;
|
||||
}
|
||||
const label = row === "face" ? "Font" : row === "weight" ? "Weight" : "Size";
|
||||
const value = this.rowValue(row);
|
||||
if (disabled) {
|
||||
image.drawText(font, rowX, y + LIST_ROW_TEXT_INSET, label, 70);
|
||||
image.drawText(font, rowX + rowWidth - font.measureText(value) - 2, y + LIST_ROW_TEXT_INSET, value, 70);
|
||||
} else {
|
||||
drawRightValueMenuItem(image, font, rowX, y, rowWidth, label, value);
|
||||
}
|
||||
}
|
||||
this.menu.paint(image, { x: rowX - 10, y: rowsTop, width: rowWidth + 20, height: ROWS.length * rowHeight }, focused);
|
||||
|
||||
// Preview: a separator, then the sample line in the draft font.
|
||||
const previewTop = rowsTop + ROWS.length * rowHeight + 10;
|
||||
@@ -145,6 +134,25 @@ export class FontPickerLayer implements Layer {
|
||||
return image;
|
||||
}
|
||||
|
||||
/** Row box = highlight box; text sits 10px in from its left edge and 10px from its right. */
|
||||
private drawRow({ image, item: row, x, y, width, selected }: MenuDrawArgs<RowId>): void {
|
||||
const font = getDefaultSmallFont();
|
||||
const textX = x + 10;
|
||||
const textWidth = width - 20;
|
||||
if (row === "save") {
|
||||
image.drawText(font, textX, y + LIST_ROW_TEXT_INSET, "Save", selected ? 255 : 200);
|
||||
return;
|
||||
}
|
||||
const label = row === "face" ? "Font" : row === "weight" ? "Weight" : "Size";
|
||||
const value = this.rowValue(row);
|
||||
if (this.rowDisabled(row)) {
|
||||
image.drawText(font, textX, y + LIST_ROW_TEXT_INSET, label, 70);
|
||||
image.drawText(font, textX + textWidth - font.measureText(value) - 2, y + LIST_ROW_TEXT_INSET, value, 70);
|
||||
} else {
|
||||
drawRightValueMenuItem(image, font, textX, y, textWidth, label, value);
|
||||
}
|
||||
}
|
||||
|
||||
private rowDisabled(row: RowId): boolean {
|
||||
return (row === "weight" || row === "size") && this.draft.kind === "bitmap";
|
||||
}
|
||||
@@ -165,14 +173,12 @@ export class FontPickerLayer implements Layer {
|
||||
handleInput(event: InputEvent, ctx: LayerContext): void {
|
||||
switch (event.type) {
|
||||
case "scroll-up":
|
||||
this.selectedRow = (this.selectedRow + ROWS.length - 1) % ROWS.length;
|
||||
return;
|
||||
case "scroll-down":
|
||||
this.selectedRow = (this.selectedRow + 1) % ROWS.length;
|
||||
void this.menu.handleInput(event);
|
||||
return;
|
||||
case "click": {
|
||||
const row = ROWS[this.selectedRow]!;
|
||||
if (this.rowDisabled(row)) return;
|
||||
const row = this.menu.selectedItem;
|
||||
if (!row || this.rowDisabled(row)) return;
|
||||
if (row === "face") this.openFaceMenu(ctx);
|
||||
else if (row === "weight") this.openWeightMenu(ctx);
|
||||
else if (row === "size") this.openSizeMenu(ctx);
|
||||
|
||||
+17
-4
@@ -80,6 +80,8 @@ export interface LayerContext {
|
||||
}
|
||||
|
||||
export interface Layer {
|
||||
readonly depth?: number;
|
||||
paintParts?(): Plane[];
|
||||
readonly paintOverBase?: boolean;
|
||||
/**
|
||||
* Dim everything painted beneath this layer: a 0..1 brightness factor
|
||||
@@ -116,6 +118,12 @@ export interface Layer {
|
||||
|
||||
export class LayerStack {
|
||||
private readonly layers: Layer[];
|
||||
private static nextShellKey = 3;
|
||||
static allocateShellKey(): number {
|
||||
if (this.nextShellKey > 65535) this.nextShellKey = 3;
|
||||
return this.nextShellKey++;
|
||||
}
|
||||
private readonly shellKeys = new WeakMap<Layer, number>();
|
||||
private readonly ctx: LayerContext;
|
||||
private baseWidth: number;
|
||||
private baseHeight: number;
|
||||
@@ -225,6 +233,8 @@ export class LayerStack {
|
||||
* A stack whose base is transparent over something else (the shell chrome
|
||||
* over the window surfaces) forwards this to what lies beneath.
|
||||
*/
|
||||
paintUndimmed(): Plane[] { return this.paintLayer(this.layers.length - 1, 1, true); }
|
||||
|
||||
baseDim(): number | false {
|
||||
return this.lastBaseDim;
|
||||
}
|
||||
@@ -259,8 +269,11 @@ export class LayerStack {
|
||||
|
||||
|
||||
/** Paint layer `index`; `dimSoFar` is the factor the layers above apply to it. */
|
||||
private paintLayer(index: number, dimSoFar: number): Plane[] {
|
||||
private paintLayer(index: number, dimSoFar: number, raw = false): Plane[] {
|
||||
const layer = this.layers[index]!;
|
||||
if (raw && index === 0 && layer.paintParts) return layer.paintParts();
|
||||
let key = this.shellKeys.get(layer);
|
||||
if (key === undefined) { key = LayerStack.allocateShellKey(); this.shellKeys.set(layer, key); }
|
||||
let canvas: GrayImage | null = null;
|
||||
let belowRequested = false;
|
||||
const image = spanCurrent(`paint[${index}]:${layer.constructor.name}`, () =>
|
||||
@@ -272,7 +285,7 @@ export class LayerStack {
|
||||
return canvas;
|
||||
}),
|
||||
);
|
||||
const ownPlane: Plane = { image, x: 0, y: 0 };
|
||||
const ownPlane: Plane = { image, x: 0, y: 0, shellKey: key, depth: layer.depth, dimUnderneath: layer.dimUnderneath === false ? 1 : layer.dimUnderneath };
|
||||
if (index <= 0) {
|
||||
this.lastBaseDim = dimSoFar;
|
||||
return [ownPlane];
|
||||
@@ -281,7 +294,7 @@ export class LayerStack {
|
||||
return [ownPlane];
|
||||
}
|
||||
const dim = layer.dimUnderneath || 1;
|
||||
const below = this.paintLayer(layer.paintOverBase ? 0 : index - 1, dimSoFar * dim);
|
||||
return [...(dim < 1 ? dimPlanes(below, dim) : below), ownPlane];
|
||||
const below = this.paintLayer(layer.paintOverBase ? 0 : index - 1, dimSoFar * dim, raw);
|
||||
return [...(!raw && dim < 1 ? dimPlanes(below, dim) : below), ownPlane];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,500 @@
|
||||
import { DrawExpression as E } from "../graphics/draw-expression";
|
||||
import { GrayImage } from "../graphics/image";
|
||||
import { menuScrollList, slidingHighlightY, type MenuScrollHighlight } from "../graphics/menu-scroll-list";
|
||||
import type { InputEvent } from "./gestures";
|
||||
import { MenuHighlightMotion } from "./menu-highlight-motion";
|
||||
import { MenuScrollMotion, scrollOffsetExpression, type MenuScrollAnimation } from "./menu-scroll-motion";
|
||||
|
||||
/** Selected-row fill while the menu owns input; the outline alone marks an unfocused menu's selection. */
|
||||
export const MENU_HIGHLIGHT_FILL = 15;
|
||||
export const MENU_HIGHLIGHT_STROKE = 45;
|
||||
const SCROLLBAR_TRACK_VALUE = 30;
|
||||
const SCROLLBAR_THUMB_VALUE = 120;
|
||||
const SCROLLBAR_WIDTH = 3;
|
||||
const SCROLLBAR_MIN_THUMB = 8;
|
||||
/** Largest packed scroll strip, in bytes: the firmware's per-resource limit. */
|
||||
const MAX_SCROLL_STRIP_BYTES = 65536;
|
||||
/** A bounce overshoots by this fraction of the end row's pitch, as if starting to reveal another row. */
|
||||
const BOUNCE_FRACTION = 0.4;
|
||||
const MAX_BOUNCE = 24;
|
||||
|
||||
/** A rectangle in image coordinates. */
|
||||
export type MenuBox = { x: number; y: number; width: number; height: number };
|
||||
|
||||
export type MenuDrawArgs<T> = {
|
||||
/**
|
||||
* Draw into this image, inside the (x, y, width, height) rect. For the
|
||||
* selected row this is a scratch image exactly the rect's size (so x and y
|
||||
* are 0), replayed over the highlight. While a scroll animates, rows are
|
||||
* also drawn into a strip image covering both viewports. Never draw
|
||||
* relative to image.width or image.height, and never outside the rect.
|
||||
*/
|
||||
image: GrayImage;
|
||||
item: T;
|
||||
index: number;
|
||||
x: number;
|
||||
y: number;
|
||||
width: number;
|
||||
height: number;
|
||||
selected: boolean;
|
||||
/** Whether the menu owns input right now: the `focused` flag the host passed to paint. */
|
||||
focused: boolean;
|
||||
};
|
||||
|
||||
export type MenuHighlightStyle = {
|
||||
/** Corner radius of the selection box. Default 8. */
|
||||
radius?: number;
|
||||
/** Stereo depth of the selection box. Default 0. */
|
||||
depth?: number;
|
||||
};
|
||||
|
||||
export type MenuOptions<T> = {
|
||||
items?: readonly T[];
|
||||
/** Initial selection. Default: the first selectable item. */
|
||||
selectedIndex?: number | null;
|
||||
/** Row pitch in pixels (including rowGap) for an item laid out at this width. */
|
||||
getHeight: (item: T, width: number) => number;
|
||||
/** Default: every item is selectable. Non-selectable items (group labels) are skipped by navigation. */
|
||||
isSelectable?: (item: T) => boolean;
|
||||
draw: (args: MenuDrawArgs<T>) => void;
|
||||
/** Called by activate() / a click with the selected item. */
|
||||
onSelect?: (item: T, index: number) => void | Promise<void>;
|
||||
/**
|
||||
* Whether scrolling past either end wraps to the other. When false the
|
||||
* selection stays put and onExitTop / onExitBottom is called instead; with
|
||||
* no callback for that end, the list bounces.
|
||||
*/
|
||||
wrap?: boolean;
|
||||
onExitTop?: () => void;
|
||||
onExitBottom?: () => void;
|
||||
/** Pixels between consecutive rows, excluded from each row's drawable box and highlight. Default 0. */
|
||||
rowGap?: number;
|
||||
/** Selection box style, or false to draw no highlight (the draw callback shows the selection itself). */
|
||||
highlight?: MenuHighlightStyle | false;
|
||||
};
|
||||
|
||||
type MenuLayout = { tops: number[]; heights: number[]; total: number };
|
||||
|
||||
/**
|
||||
* A vertical list with one selected row: the menu proper, independent of the
|
||||
* Layer stack. A host paints it into a rect of its own image and forwards
|
||||
* scroll / click events while the menu has focus; the host keeps the rest
|
||||
* (title, box, focus between several menus, back navigation).
|
||||
*
|
||||
* Rows may vary in height and some may be unselectable; navigation skips
|
||||
* those. Scrolling is pixel-based: the viewport moves the minimum needed to
|
||||
* keep the selection and its neighbors fully visible (just the selection
|
||||
* when they don't all fit), then aligns to a row top so the list never
|
||||
* starts with a partially visible row. A row cut off by the viewport's edge
|
||||
* is drawn clipped, so a box that isn't a whole number of rows tall shows
|
||||
* part of the next row as a hint that there is more.
|
||||
*
|
||||
* Navigation that scrolls (without wrapping) animates on the glasses: the
|
||||
* rows visible before and after are drawn into one strip, and a display list
|
||||
* slides a viewport-sized copy of it while the highlight moves on the same
|
||||
* timeline. Scrolling past an end without wrap or an exit callback bounces
|
||||
* the same way, with the highlight carried along by its row. When the strip
|
||||
* would be too large or the menu doesn't fit the assumptions in scrollStrip,
|
||||
* the motion snaps instead.
|
||||
*/
|
||||
export class Menu<T> {
|
||||
private itemList: readonly T[];
|
||||
private selected: number | null;
|
||||
/**
|
||||
* The caller asked for no selection. Otherwise a null selection only means
|
||||
* nothing was selectable, and the first selectable item is chosen as soon
|
||||
* as one exists (a menu created empty and filled later starts on it).
|
||||
*/
|
||||
private deselected: boolean;
|
||||
private scrollY = 0;
|
||||
private lastBox: MenuBox | null = null;
|
||||
private readonly motion = new MenuHighlightMotion();
|
||||
private readonly scrollMotion = new MenuScrollMotion();
|
||||
|
||||
constructor(private readonly options: MenuOptions<T>) {
|
||||
this.itemList = options.items ?? [];
|
||||
this.deselected = options.selectedIndex === null;
|
||||
this.selected = this.settle(options.selectedIndex ?? null);
|
||||
}
|
||||
|
||||
get items(): readonly T[] {
|
||||
return this.itemList;
|
||||
}
|
||||
|
||||
get selectedIndex(): number | null {
|
||||
this.reconcile();
|
||||
return this.selected;
|
||||
}
|
||||
|
||||
get selectedItem(): T | null {
|
||||
const index = this.selectedIndex;
|
||||
return index === null ? null : this.itemList[index]!;
|
||||
}
|
||||
|
||||
/** Content offset of the viewport's top edge, in pixels. */
|
||||
get scrollTop(): number {
|
||||
return this.scrollY;
|
||||
}
|
||||
|
||||
/** Move the viewport's top edge; the next paint clamps it and scrolls the selection back into view. */
|
||||
set scrollTop(value: number) {
|
||||
this.scrollY = Math.max(0, value | 0);
|
||||
}
|
||||
|
||||
/** True when the content did not fit the box given to the last paint. */
|
||||
get overflows(): boolean {
|
||||
if (!this.lastBox) return false;
|
||||
return this.layout(this.lastBox.width).total > this.lastBox.height;
|
||||
}
|
||||
|
||||
/**
|
||||
* Replace the items. `selectedIndex` names the new selection (null for
|
||||
* none); left out, the selection stays at the same index, clamped into
|
||||
* range, or lands on the first selectable item if there was none. The
|
||||
* viewport keeps the selected row at the same on-screen position when it can.
|
||||
*/
|
||||
setItems(items: readonly T[], selectedIndex?: number | null): void {
|
||||
const width = this.lastBox?.width;
|
||||
const before = this.selected !== null && width !== undefined
|
||||
? this.layout(width).tops[this.selected]! - this.scrollY
|
||||
: null;
|
||||
this.itemList = items;
|
||||
if (selectedIndex !== undefined) this.deselected = selectedIndex === null;
|
||||
this.selected = this.settle(selectedIndex === undefined ? this.selected : selectedIndex);
|
||||
if (before !== null && this.selected !== null && width !== undefined) {
|
||||
this.scrollY = Math.max(0, this.layout(width).tops[this.selected]! - before);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Move the selection; a non-selectable or out-of-range index lands on the
|
||||
* nearest selectable one. null clears it until the user scrolls or a
|
||||
* number is selected.
|
||||
*/
|
||||
select(index: number | null): void {
|
||||
this.deselected = index === null;
|
||||
this.selected = this.settle(index);
|
||||
}
|
||||
|
||||
/** Step the selection by one selectable row; handles wrap and the exit callbacks. */
|
||||
moveSelection(delta: -1 | 1): void {
|
||||
this.reconcile();
|
||||
const from = this.selected;
|
||||
const next = this.nextSelectable(from, delta);
|
||||
if (next !== null) {
|
||||
if (from !== null) this.motion.navigate(from, false);
|
||||
this.scrollMotion.navigate(false);
|
||||
this.selected = next;
|
||||
this.deselected = false;
|
||||
return;
|
||||
}
|
||||
if (from === null && this.scrollContent(delta)) return;
|
||||
if (this.options.wrap) {
|
||||
const wrapped = this.nextSelectable(null, delta);
|
||||
if (wrapped !== null && wrapped !== from) {
|
||||
if (from !== null) this.motion.navigate(from, true);
|
||||
this.scrollMotion.navigate(true);
|
||||
this.selected = wrapped;
|
||||
this.deselected = false;
|
||||
} else if (from === null && this.lastBox) {
|
||||
this.scrollY = delta > 0 ? 0 : this.maxScroll(this.layout(this.lastBox.width), this.lastBox.height);
|
||||
}
|
||||
return;
|
||||
}
|
||||
const exit = delta > 0 ? this.options.onExitBottom : this.options.onExitTop;
|
||||
if (exit) exit();
|
||||
else this.bounce(delta);
|
||||
}
|
||||
|
||||
/** Invoke onSelect for the selected item. Returns false when nothing is selected. */
|
||||
async activate(): Promise<boolean> {
|
||||
this.reconcile();
|
||||
if (this.selected === null) return false;
|
||||
await this.options.onSelect?.(this.itemList[this.selected]!, this.selected);
|
||||
return true;
|
||||
}
|
||||
|
||||
/** Handle scroll-up, scroll-down and click. Returns false for any other event, or a click with nothing selected. */
|
||||
async handleInput(event: InputEvent): Promise<boolean> {
|
||||
switch (event.type) {
|
||||
case "scroll-up":
|
||||
this.moveSelection(-1);
|
||||
return true;
|
||||
case "scroll-down":
|
||||
this.moveSelection(1);
|
||||
return true;
|
||||
case "click":
|
||||
return this.activate();
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/** The selectable item under image-coordinate y in the last painted box, for touch/mirror input. */
|
||||
indexAt(y: number): number | null {
|
||||
const box = this.lastBox;
|
||||
if (!box || y < box.y || y >= box.y + box.height) return null;
|
||||
const layout = this.layout(box.width);
|
||||
const contentY = y - box.y + this.scrollY;
|
||||
for (let index = 0; index < this.itemList.length; index++) {
|
||||
if (contentY >= layout.tops[index]! && contentY < layout.tops[index]! + layout.heights[index]!) {
|
||||
return this.isSelectable(index) ? index : null;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
paint(image: GrayImage, box: MenuBox, focused: boolean): void {
|
||||
this.lastBox = box;
|
||||
this.reconcile();
|
||||
const layout = this.layout(box.width);
|
||||
this.ensureVisible(layout, box.height);
|
||||
const now = Date.now();
|
||||
const scroll = this.scrollMotion.paint(this.scrollY, box, now);
|
||||
const strip = scroll ? this.scrollStrip(image, layout, box, focused, scroll) : null;
|
||||
if (scroll && !strip) this.scrollMotion.cancel();
|
||||
const gap = this.options.rowGap ?? 0;
|
||||
const highlight = this.options.highlight;
|
||||
let scrollHighlight: MenuScrollHighlight | undefined;
|
||||
for (let index = 0; index < this.itemList.length; index++) {
|
||||
const top = layout.tops[index]! - this.scrollY;
|
||||
const height = layout.heights[index]! - gap;
|
||||
const selected = index === this.selected;
|
||||
if (top + height <= 0 || top >= box.height) continue;
|
||||
const rowY = box.y + top;
|
||||
const item = this.itemList[index]!;
|
||||
if (selected && highlight !== false) {
|
||||
const animation = this.motion.paint(index, this.scrollY, box.x, rowY, box.width, height, now, !!strip);
|
||||
const background = focused ? MENU_HIGHLIGHT_FILL : 0;
|
||||
if (strip && scroll) {
|
||||
// The strip already holds this row's content; only the box is drawn over it.
|
||||
let y = slidingHighlightY(top, animation, scroll.startedAt);
|
||||
// A bounce carries the highlight with its row.
|
||||
if (scroll.peak !== undefined) y = y.add(E.i32(scroll.to)).sub(scrollOffsetExpression(scroll));
|
||||
scrollHighlight = { y, width: box.width, height, radius: highlight?.radius ?? 8,
|
||||
background, border: MENU_HIGHLIGHT_STROKE };
|
||||
continue;
|
||||
}
|
||||
// A selected row taller than the box is drawn whole, from its top.
|
||||
const row = new GrayImage(box.width, height, 0);
|
||||
this.options.draw({ image: row, item, index, x: 0, y: 0, width: box.width, height, selected, focused });
|
||||
image.drawMenuSelection(row, box.x, rowY, background, MENU_HIGHLIGHT_STROKE,
|
||||
highlight?.radius ?? 8, highlight?.depth ?? 0, animation);
|
||||
} else {
|
||||
this.drawRow(image, index, box.x, rowY, box.width, height, focused, box.y, box.y + box.height);
|
||||
}
|
||||
}
|
||||
// The rows painted above stay underneath: once the animation ends, the
|
||||
// strip's final frame and the static paint are pixel-identical.
|
||||
if (strip && scroll) {
|
||||
const sourceY = scrollOffsetExpression(scroll).sub(E.i32(strip.top));
|
||||
image.drawDisplayList(menuScrollList(strip.image, strip.x, box.height, scroll, sourceY, scrollHighlight),
|
||||
box.x, box.y, box.width, box.height, 0);
|
||||
}
|
||||
}
|
||||
|
||||
/** Draw a vertical scrollbar for the last painted box (no-op unless the content overflows it). */
|
||||
drawScrollbar(image: GrayImage, x: number, y: number, height: number): void {
|
||||
const box = this.lastBox;
|
||||
if (!box) return;
|
||||
const layout = this.layout(box.width);
|
||||
if (layout.total <= box.height) return;
|
||||
const maxScroll = this.maxScroll(layout, box.height);
|
||||
const thumbHeight = Math.max(SCROLLBAR_MIN_THUMB, (height * box.height / layout.total) | 0);
|
||||
const fraction = Math.min(1, Math.max(0, this.scrollY) / maxScroll);
|
||||
image.fillRect(x, y, SCROLLBAR_WIDTH, height, SCROLLBAR_TRACK_VALUE);
|
||||
image.fillRect(x, y + (((height - thumbHeight) * fraction) | 0), SCROLLBAR_WIDTH, thumbHeight, SCROLLBAR_THUMB_VALUE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Draw one row at (x, y), clipped to target rows [clipTop, clipBottom).
|
||||
* Selected rows with a highlight are drawn in a scratch image the row's
|
||||
* size, like the static selection, which clips their ink to the row.
|
||||
*/
|
||||
private drawRow(target: GrayImage, index: number, x: number, y: number, width: number, height: number,
|
||||
focused: boolean, clipTop: number, clipBottom: number): void {
|
||||
const item = this.itemList[index]!;
|
||||
const selected = index === this.selected;
|
||||
const scratch = selected && this.options.highlight !== false;
|
||||
if (!scratch && y >= clipTop && y + height <= clipBottom) {
|
||||
this.options.draw({ image: target, item, index, x, y, width, height, selected, focused });
|
||||
return;
|
||||
}
|
||||
const row = new GrayImage(width, height, 0);
|
||||
this.options.draw({ image: row, item, index, x: 0, y: 0, width, height, selected, focused });
|
||||
const skip = Math.max(0, clipTop - y);
|
||||
target.bitBlt(row.withDrawsBaked(), x, y + skip,
|
||||
{ sy: skip, height: Math.min(height, clipBottom - y) - skip, transparentZero: true });
|
||||
}
|
||||
|
||||
/**
|
||||
* Render every row visible at any point of the motion into one strip, or
|
||||
* return null to snap. Its first and last frames must match the static
|
||||
* paints on either side, which clip rows at the viewport's edges the same
|
||||
* way. The strip copy is opaque, so this also requires a black background
|
||||
* under the menu, a selected row that fits, and a highlight at the menu's
|
||||
* own depth.
|
||||
*/
|
||||
private scrollStrip(image: GrayImage, layout: MenuLayout, box: MenuBox, focused: boolean,
|
||||
scroll: MenuScrollAnimation): { image: GrayImage; x: number; top: number } | null {
|
||||
const highlight = this.options.highlight;
|
||||
if (highlight !== false && (highlight?.depth ?? 0) !== 0) return null;
|
||||
const gap = this.options.rowGap ?? 0;
|
||||
const selected = this.selected;
|
||||
if (selected !== null && layout.tops[selected]! + layout.heights[selected]! - gap - scroll.to > box.height) return null;
|
||||
const offsets = [scroll.from, scroll.to, scroll.peak ?? scroll.to];
|
||||
const top = Math.min(...offsets);
|
||||
const bottom = Math.max(...offsets) + box.height;
|
||||
const strip = new GrayImage(box.width, bottom - top, 0);
|
||||
for (let index = 0; index < this.itemList.length; index++) {
|
||||
const rowTop = layout.tops[index]!;
|
||||
const height = layout.heights[index]! - gap;
|
||||
if (rowTop + height <= top || rowTop >= bottom) continue;
|
||||
this.drawRow(strip, index, 0, rowTop - top, box.width, height, focused, 0, strip.height);
|
||||
}
|
||||
// Crop to the columns with ink; the static paint shows the rest unchanged.
|
||||
const baked = strip.withDrawsBaked();
|
||||
let left = baked.width, right = -1;
|
||||
for (let y = 0; y < baked.height; y++) {
|
||||
const row = y * baked.width;
|
||||
for (let x = 0; x < left; x++) if (baked.pixels[row + x]! >= 8) { left = x; break; }
|
||||
for (let x = baked.width - 1; x > right; x--) if (baked.pixels[row + x]! >= 8) { right = x; break; }
|
||||
}
|
||||
if (right < left) return null;
|
||||
const width = right - left + 1;
|
||||
if (5 + Math.ceil(width / 2) * baked.height > MAX_SCROLL_STRIP_BYTES) return null;
|
||||
// Whatever the host drew under the copied columns would be covered.
|
||||
for (let y = box.y; y < box.y + box.height; y++) {
|
||||
for (let x = box.x + left; x <= box.x + right; x++) {
|
||||
if (x >= 0 && y >= 0 && x < image.width && y < image.height && image.pixels[y * image.width + x]! >= 8) return null;
|
||||
}
|
||||
}
|
||||
const cropped = new GrayImage(width, baked.height, 0);
|
||||
cropped.bitBlt(baked, -left, 0);
|
||||
return { image: cropped, x: left, top };
|
||||
}
|
||||
|
||||
/** At an end with nowhere to go, overshoot a little past it and come back. */
|
||||
private bounce(delta: -1 | 1): void {
|
||||
const count = this.itemList.length;
|
||||
if (!this.lastBox || !count) return;
|
||||
const layout = this.layout(this.lastBox.width);
|
||||
const pitch = layout.heights[delta > 0 ? count - 1 : 0]!;
|
||||
const amount = Math.min(MAX_BOUNCE, Math.round(pitch * BOUNCE_FRACTION));
|
||||
if (amount > 0) this.scrollMotion.bounce(delta * amount);
|
||||
}
|
||||
|
||||
private isSelectable(index: number): boolean {
|
||||
return this.options.isSelectable?.(this.itemList[index]!) ?? true;
|
||||
}
|
||||
|
||||
/** The next selectable index after `from` in direction `delta`; from null, the first (or last) selectable. */
|
||||
private nextSelectable(from: number | null, delta: -1 | 1): number | null {
|
||||
const count = this.itemList.length;
|
||||
let index = from === null ? (delta > 0 ? 0 : count - 1) : from + delta;
|
||||
for (; index >= 0 && index < count; index += delta) {
|
||||
if (this.isSelectable(index)) return index;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/** Resolve a stored selection: null stays null only when the caller deselected. */
|
||||
private settle(index: number | null): number | null {
|
||||
if (index === null) return this.deselected ? null : this.nextSelectable(null, 1);
|
||||
return this.resolve(index);
|
||||
}
|
||||
|
||||
/** Clamp an index into range and onto a selectable row (nearest below, else nearest above). */
|
||||
private resolve(index: number): number | null {
|
||||
if (!this.itemList.length) return null;
|
||||
const clamped = Math.max(0, Math.min(this.itemList.length - 1, index | 0));
|
||||
if (this.isSelectable(clamped)) return clamped;
|
||||
return this.nextSelectable(clamped, 1) ?? this.nextSelectable(clamped, -1);
|
||||
}
|
||||
|
||||
/** Re-validate the selection against the current items (they may have been edited in place). */
|
||||
private reconcile(): void {
|
||||
this.selected = this.settle(this.selected);
|
||||
}
|
||||
|
||||
private layout(width: number): MenuLayout {
|
||||
const tops: number[] = [];
|
||||
const heights: number[] = [];
|
||||
let total = 0;
|
||||
for (const item of this.itemList) {
|
||||
const height = Math.max(0, this.options.getHeight(item, width) | 0);
|
||||
tops.push(total);
|
||||
heights.push(height);
|
||||
total += height;
|
||||
}
|
||||
if (this.itemList.length) total -= this.options.rowGap ?? 0;
|
||||
return { tops, heights, total };
|
||||
}
|
||||
|
||||
private maxScroll(layout: MenuLayout, viewportHeight: number): number {
|
||||
return Math.max(0, layout.total - viewportHeight);
|
||||
}
|
||||
|
||||
/** Scroll the viewport so the selected row is fully inside it, aligning the top to a row edge when it moves. */
|
||||
private ensureVisible(layout: MenuLayout, viewportHeight: number): void {
|
||||
if (this.selected === null) {
|
||||
this.scrollY = Math.max(0, Math.min(this.maxScroll(layout, viewportHeight), this.scrollY));
|
||||
return;
|
||||
}
|
||||
const gap = this.options.rowGap ?? 0;
|
||||
const selected = this.selected;
|
||||
const top = layout.tops[selected]!;
|
||||
const bottom = top + layout.heights[selected]! - gap;
|
||||
// Keep the neighbors in view too when all three fit, so the user sees
|
||||
// what the next step will reach.
|
||||
const contextTop = selected > 0 ? layout.tops[selected - 1]! : top;
|
||||
const contextBottom = selected + 1 < this.itemList.length
|
||||
? layout.tops[selected + 1]! + layout.heights[selected + 1]! - gap : bottom;
|
||||
const fits = contextBottom - contextTop <= viewportHeight;
|
||||
const wantTop = fits ? contextTop : top;
|
||||
const wantBottom = fits ? contextBottom : bottom;
|
||||
let target = this.scrollY;
|
||||
if (wantTop < target) {
|
||||
target = wantTop;
|
||||
} else if (wantBottom > target + viewportHeight) {
|
||||
// A row taller than the viewport shows from its top.
|
||||
target = Math.min(top, wantBottom - viewportHeight);
|
||||
// Snap to the first row that starts inside the viewport: the wanted
|
||||
// rows still fit (their bottom is within viewportHeight of that top).
|
||||
for (let index = 0; index <= selected; index++) {
|
||||
if (layout.tops[index]! >= target) {
|
||||
target = layout.tops[index]!;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
// Never scroll past the end (setItems can leave the viewport there when
|
||||
// rows are removed or inserted above the selection): pull back to the
|
||||
// first row top that still shows the last row, so no partial row starts
|
||||
// the list. The selected row stays visible, since it ends by the list's end.
|
||||
const maxScroll = this.maxScroll(layout, viewportHeight);
|
||||
if (target > maxScroll) {
|
||||
const aligned = layout.tops.find((rowTop) => rowTop >= maxScroll);
|
||||
target = aligned !== undefined && aligned <= target ? aligned : maxScroll;
|
||||
}
|
||||
this.scrollY = Math.max(0, target);
|
||||
}
|
||||
|
||||
/** With nothing selectable, up/down page the content by rows. Returns false at the end being scrolled past. */
|
||||
private scrollContent(delta: -1 | 1): boolean {
|
||||
if (!this.lastBox) return false;
|
||||
const layout = this.layout(this.lastBox.width);
|
||||
const maxScroll = this.maxScroll(layout, this.lastBox.height);
|
||||
if (delta > 0) {
|
||||
if (this.scrollY >= maxScroll) return false;
|
||||
const next = layout.tops.find((top) => top > this.scrollY);
|
||||
this.scrollY = Math.min(maxScroll, next ?? maxScroll);
|
||||
} else {
|
||||
if (this.scrollY <= 0) return false;
|
||||
let previous = 0;
|
||||
for (const top of layout.tops) if (top < this.scrollY) previous = top;
|
||||
this.scrollY = Math.max(0, previous);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
export const MENU_HIGHLIGHT_DURATION_MS = 240;
|
||||
export type MenuHighlightAnimation = { dx: number; dy: number; startedAt: number; token: number; durationMs: number };
|
||||
|
||||
let nextToken = 1;
|
||||
|
||||
/** A fresh display-list timeline token, shared by all menu animations. */
|
||||
export function nextAnimationToken(): number {
|
||||
return nextToken++;
|
||||
}
|
||||
|
||||
/** Eligibility is decided by menu navigation, before the display-list bridge. */
|
||||
export class MenuHighlightMotion {
|
||||
private previous?: { index: number; scroll: number; x: number; y: number; width: number; height: number };
|
||||
private fromIndex: number | null = null;
|
||||
private animation?: MenuHighlightAnimation;
|
||||
|
||||
navigate(from: number, wrapped: boolean): void {
|
||||
this.fromIndex = wrapped ? null : from;
|
||||
}
|
||||
|
||||
/** `scrolling`: the scroll change since the last paint is itself animated, so the highlight may slide across it. */
|
||||
paint(index: number, scroll: number, x: number, y: number, width: number, height: number, now: number, scrolling = false): MenuHighlightAnimation | undefined {
|
||||
const previous = this.previous;
|
||||
if (!previous || previous.index !== index || previous.scroll !== scroll ||
|
||||
previous.x !== x || previous.y !== y || previous.width !== width || previous.height !== height) {
|
||||
let fromX = previous?.x ?? x, fromY = previous?.y ?? y;
|
||||
if (previous && this.animation) {
|
||||
const t = Math.max(0, Math.min(1, (now - this.animation.startedAt) / this.animation.durationMs));
|
||||
const remaining = 1 - t * t * (3 - 2 * t);
|
||||
fromX += this.animation.dx * remaining;
|
||||
fromY += this.animation.dy * remaining;
|
||||
}
|
||||
const eligible = previous && previous.index !== index && this.fromIndex === previous.index &&
|
||||
(previous.scroll === scroll || scrolling) && previous.width === width && previous.height === height;
|
||||
this.animation = eligible ? { dx: Math.round(fromX - x), dy: Math.round(fromY - y), startedAt: now, token: nextAnimationToken(), durationMs: MENU_HIGHLIGHT_DURATION_MS } : undefined;
|
||||
this.fromIndex = null;
|
||||
this.previous = { index, scroll, x, y, width, height };
|
||||
}
|
||||
return this.animation && now - this.animation.startedAt < this.animation.durationMs ? this.animation : undefined;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
import { DrawExpression as E, ExprOp } from "../graphics/draw-expression";
|
||||
import { MENU_HIGHLIGHT_DURATION_MS, nextAnimationToken } from "./menu-highlight-motion";
|
||||
|
||||
/** Longest scroll, in pixels, that animates; longer jumps snap. */
|
||||
export const MAX_ANIMATED_SCROLL = 96;
|
||||
export const MENU_BOUNCE_DURATION_MS = 320;
|
||||
/** Fraction of a bounce spent moving out to the peak; the rest settles back. */
|
||||
const BOUNCE_PEAK = 0.35;
|
||||
|
||||
/**
|
||||
* The viewport's content offset moving from `from` to `to`, on the same clock
|
||||
* as the highlight. A bounce passes through `peak` on the way (usually past
|
||||
* the end of the list, with `to` where it started).
|
||||
*/
|
||||
export type MenuScrollAnimation = {
|
||||
from: number; to: number; peak?: number;
|
||||
startedAt: number; token: number; durationMs: number;
|
||||
};
|
||||
|
||||
type Box = { x: number; y: number; width: number; height: number };
|
||||
|
||||
const clamp01 = (t: number) => Math.max(0, Math.min(1, t));
|
||||
const smoothstep = (t: number) => t * t * (3 - 2 * t);
|
||||
|
||||
/** The offset at timeline fraction t (0..1). Keep in sync with scrollOffsetExpression. */
|
||||
export function scrollOffsetAt(animation: MenuScrollAnimation, t: number): number {
|
||||
const { from, to, peak } = animation;
|
||||
if (peak === undefined) return from + (to - from) * smoothstep(clamp01(t));
|
||||
const out = 1 - (1 - clamp01(t / BOUNCE_PEAK)) ** 2;
|
||||
const back = smoothstep(clamp01((t - BOUNCE_PEAK) / (1 - BOUNCE_PEAK)));
|
||||
return from + (peak - from) * out + (to - peak) * back;
|
||||
}
|
||||
|
||||
/**
|
||||
* The offset as a draw expression on the animation's own timeline. The easing
|
||||
* ops clamp their input, which splits a bounce into its two phases without
|
||||
* comparisons: the return term stays 0 until the peak, the outward one stays 1 after.
|
||||
*/
|
||||
export function scrollOffsetExpression(animation: MenuScrollAnimation) {
|
||||
const { from, to, peak } = animation;
|
||||
const t = () => E.progress(animation.durationMs);
|
||||
if (peak === undefined) return t().ease().lerp(E.i32(from).toFloat(), E.i32(to).toFloat()).toInt();
|
||||
const out = t().mul(E.f32(1 / BOUNCE_PEAK)).ease(ExprOp.EASE_OUT_QUAD);
|
||||
const back = t().sub(E.f32(BOUNCE_PEAK)).div(E.f32(1 - BOUNCE_PEAK)).ease();
|
||||
return E.i32(from).toFloat()
|
||||
.add(E.i32(peak - from).toFloat().mul(out))
|
||||
.add(E.i32(to - peak).toFloat().mul(back)).toInt();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tracks the painted scroll offset. A change caused by non-wrapping
|
||||
* navigation animates; programmatic scrolls, item edits and geometry changes
|
||||
* snap. A bounce is requested explicitly, at an end of the list. Either starts
|
||||
* from the offset on screen now, so it can interrupt one in flight.
|
||||
*/
|
||||
export class MenuScrollMotion {
|
||||
private previous?: { scroll: number; box: Box };
|
||||
private navigated = false;
|
||||
private bounceBy = 0;
|
||||
private animation?: MenuScrollAnimation;
|
||||
|
||||
navigate(wrapped: boolean): void {
|
||||
this.navigated = !wrapped;
|
||||
}
|
||||
|
||||
/** Overshoot by `offset` pixels (negative past the top) and come back, at the next paint. */
|
||||
bounce(offset: number): void {
|
||||
this.bounceBy = offset;
|
||||
}
|
||||
|
||||
/** Record this paint's settled offset; returns the animation in flight, if any. */
|
||||
paint(scroll: number, box: Box, now: number): MenuScrollAnimation | undefined {
|
||||
const previous = this.previous;
|
||||
const sameBox = !!previous && previous.box.x === box.x && previous.box.y === box.y &&
|
||||
previous.box.width === box.width && previous.box.height === box.height;
|
||||
if (!previous || !sameBox || previous.scroll !== scroll) {
|
||||
const from = previous ? this.offsetAt(previous.scroll, now) : scroll;
|
||||
const eligible = sameBox && this.navigated && from !== scroll && Math.abs(scroll - from) <= MAX_ANIMATED_SCROLL;
|
||||
this.animation = eligible
|
||||
? { from, to: scroll, startedAt: now, token: nextAnimationToken(), durationMs: MENU_HIGHLIGHT_DURATION_MS }
|
||||
: undefined;
|
||||
this.previous = { scroll, box: { ...box } };
|
||||
} else if (this.bounceBy) {
|
||||
const from = this.offsetAt(scroll, now);
|
||||
this.animation = Math.abs(scroll - from) <= MAX_ANIMATED_SCROLL
|
||||
? { from, peak: scroll + this.bounceBy, to: scroll, startedAt: now, token: nextAnimationToken(), durationMs: MENU_BOUNCE_DURATION_MS }
|
||||
: undefined;
|
||||
}
|
||||
this.navigated = false;
|
||||
this.bounceBy = 0;
|
||||
return this.animation && now - this.animation.startedAt < this.animation.durationMs ? this.animation : undefined;
|
||||
}
|
||||
|
||||
/** Cancel an animation in flight, e.g. when it cannot be drawn; the next change snaps from `settled`. */
|
||||
cancel(): void {
|
||||
this.animation = undefined;
|
||||
}
|
||||
|
||||
private offsetAt(settled: number, now: number): number {
|
||||
const animation = this.animation;
|
||||
if (!animation) return settled;
|
||||
return Math.round(scrollOffsetAt(animation, (now - animation.startedAt) / animation.durationMs));
|
||||
}
|
||||
}
|
||||
+85
-80
@@ -3,9 +3,10 @@ import { wrapText } from "../graphics/textwrap";
|
||||
import { getDefaultSmallFont } from "../graphics/ui-fonts";
|
||||
import { clamp } from "../util/numeric-util";
|
||||
import { Layer, LayerContext, PaintBelow } from "./layers";
|
||||
import { Menu, MENU_HIGHLIGHT_FILL, MENU_HIGHLIGHT_STROKE } from "./menu-core";
|
||||
|
||||
import { GESTURE_DOUBLE_CLICK, InputEvent } from "./gestures";
|
||||
import { LIST_ROW_TEXT_INSET, lineStep, listRowHeight, menuTitleHeight } from "./metrics";
|
||||
import { LIST_ROW_TEXT_INSET, centeredTextY, lineStep, listRowHeight, menuTitleHeight } from "./metrics";
|
||||
const DEFAULT_MENU_X = 8;
|
||||
const DEFAULT_MENU_Y = 8;
|
||||
const DEFAULT_MENU_WIDTH = 272;
|
||||
@@ -15,10 +16,14 @@ const DEFAULT_MENU_MIN_HEIGHT = G2_LENS_HEIGHT / 2 - 2 * DEFAULT_MENU_Y;
|
||||
const MENU_BODY_PADDING = 8;
|
||||
/** Gap between the last item row and a footer hint line. */
|
||||
const MENU_FOOTER_GAP = 8;
|
||||
const MENU_HIGHLIGHT_SELECTED_BACKGROUND_FILL = 15;
|
||||
const MENU_HIGHLIGHT_SELECTED_BORDER_STROKE = 45;
|
||||
/** Horizontal inset of the row (highlight) boxes from the menu box's edges. */
|
||||
const MENU_ROW_INSET = 12;
|
||||
/** Horizontal inset of row text from the row box. */
|
||||
const MENU_ROW_TEXT_INSET = 10;
|
||||
|
||||
export type MenuLayout = {
|
||||
/** Stereo depth of the menu surface, including the selected row. */
|
||||
depth?: number;
|
||||
/** Left edge, or "center" to center horizontally on the painted surface. */
|
||||
x: number | "center";
|
||||
y: number;
|
||||
@@ -42,6 +47,7 @@ export type MenuLayout = {
|
||||
* renders over whatever is below it (modal/window menus).
|
||||
*/
|
||||
opaque?: boolean;
|
||||
squareCorners?: boolean;
|
||||
};
|
||||
|
||||
export type MenuItemRenderArgs = {
|
||||
@@ -81,9 +87,9 @@ export function drawSelectionHighlight(
|
||||
radius = 6,
|
||||
): void {
|
||||
if (focused) {
|
||||
image.fillRoundedRect(x, y, width, height, MENU_HIGHLIGHT_SELECTED_BACKGROUND_FILL, radius);
|
||||
image.fillRoundedRect(x, y, width, height, MENU_HIGHLIGHT_FILL, radius);
|
||||
}
|
||||
image.drawRoundedRect(x, y, width, height, MENU_HIGHLIGHT_SELECTED_BORDER_STROKE, radius);
|
||||
image.drawRoundedRect(x, y, width, height, MENU_HIGHLIGHT_STROKE, radius);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -142,7 +148,7 @@ export function drawSubmenuIndicator(
|
||||
): void {
|
||||
const arrow = ">";
|
||||
const x = highlightX + highlightWidth - font.measureText(arrow) - 4;
|
||||
const y = highlightY + (((highlightHeight - font.lineHeight) / 2) | 0);
|
||||
const y = centeredTextY(font, highlightY, highlightHeight);
|
||||
image.drawText(font, x, y, arrow, value);
|
||||
}
|
||||
|
||||
@@ -188,8 +194,11 @@ export function drawRightValueMenuItem(
|
||||
export const CONTEXT_MENU_DIM = 0.25;
|
||||
|
||||
export class MenuLayer implements Layer {
|
||||
private selectedIndex = 0;
|
||||
private scrollRow = 0;
|
||||
private readonly menu: Menu<MenuItem>;
|
||||
/** The context of the paint in progress, handed to item render callbacks. */
|
||||
private paintCtx: LayerContext | null = null;
|
||||
|
||||
get depth(): number { return this.layout.depth ?? 0; }
|
||||
|
||||
get dimUnderneath(): false | number {
|
||||
return this.layout.dimUnderneath ?? false;
|
||||
@@ -197,21 +206,67 @@ export class MenuLayer implements Layer {
|
||||
|
||||
constructor(
|
||||
private readonly title: string | null,
|
||||
private readonly items: MenuItem[],
|
||||
private items: MenuItem[],
|
||||
private readonly layout: MenuLayout = {
|
||||
x: DEFAULT_MENU_X,
|
||||
y: DEFAULT_MENU_Y,
|
||||
width: DEFAULT_MENU_WIDTH,
|
||||
},
|
||||
public readonly paintOverBase = false,
|
||||
) {}
|
||||
) {
|
||||
this.menu = new Menu<MenuItem>({
|
||||
items,
|
||||
wrap: true,
|
||||
rowGap: 1,
|
||||
getHeight: () => listRowHeight(getDefaultSmallFont()),
|
||||
draw: ({ image, item, x, y, width, height, selected }) => {
|
||||
const font = getDefaultSmallFont();
|
||||
const disabled = isMenuItemDisabled(item);
|
||||
if (item.render) {
|
||||
item.render({
|
||||
image,
|
||||
x: x + MENU_ROW_TEXT_INSET,
|
||||
y,
|
||||
width: width - 2 * MENU_ROW_TEXT_INSET,
|
||||
height: height - 2,
|
||||
selected,
|
||||
disabled,
|
||||
text: item.label,
|
||||
ctx: this.paintCtx!,
|
||||
});
|
||||
} else {
|
||||
image.drawText(font, x + MENU_ROW_TEXT_INSET, y + LIST_ROW_TEXT_INSET, item.label,
|
||||
disabled ? 70 : selected ? 255 : 200);
|
||||
}
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
/** Start a newly opened picker on its current value. */
|
||||
selectItem(index: number): this {
|
||||
this.selectedIndex = clamp(index, 0, Math.max(0, this.items.length - 1));
|
||||
this.menu.select(index);
|
||||
return this;
|
||||
}
|
||||
|
||||
/** The selected row's index, or null when nothing is selectable. */
|
||||
get selectedIndex(): number | null {
|
||||
return this.menu.selectedIndex;
|
||||
}
|
||||
|
||||
get selectedItem(): MenuItem | null {
|
||||
return this.menu.selectedItem;
|
||||
}
|
||||
|
||||
/**
|
||||
* Replace the rows in place, keeping scroll and highlight state. The
|
||||
* selection follows Menu.setItems: pass the new index of the previously
|
||||
* selected row, or leave it out to keep the same index, clamped.
|
||||
*/
|
||||
setItems(items: MenuItem[], selectedIndex?: number | null): void {
|
||||
this.items = items;
|
||||
this.menu.setItems(items, selectedIndex);
|
||||
}
|
||||
|
||||
paint(ctx: LayerContext, paintBelow: PaintBelow): GrayImage {
|
||||
const font = getDefaultSmallFont();
|
||||
const rowHeight = listRowHeight(font);
|
||||
@@ -226,6 +281,7 @@ export class MenuLayer implements Layer {
|
||||
const maxHeight = Math.min(
|
||||
this.layout.maxHeight ?? image.height - y - DEFAULT_MENU_Y,
|
||||
image.height - y,
|
||||
(this.layout.squareCorners || this.depth !== 0) ? Math.floor(65530 / Math.ceil(width / 2)) : Infinity,
|
||||
);
|
||||
const footerHeight = this.layout.footer ? MENU_FOOTER_GAP + lineStep(font) : 0;
|
||||
const contentHeight = chromeTop + this.items.length * rowHeight + footerHeight + MENU_BODY_PADDING;
|
||||
@@ -234,18 +290,14 @@ export class MenuLayer implements Layer {
|
||||
1,
|
||||
((height - chromeTop - footerHeight - MENU_BODY_PADDING) / rowHeight) | 0,
|
||||
);
|
||||
this.scrollRow = scrollToKeepSelectionVisible(
|
||||
this.scrollRow,
|
||||
this.selectedIndex,
|
||||
visibleRowCount,
|
||||
this.items.length,
|
||||
);
|
||||
|
||||
// Fill 1, not 0: identical after 4bpp quantization, but 0 is the
|
||||
// transparent color key when a menu paints on the shell surface.
|
||||
image.fillRoundedRect(x, y, width, height, 1);
|
||||
if (this.layout.squareCorners) image.fillRect(x, y, width, height, 1);
|
||||
else image.fillRoundedRect(x, y, width, height, 1);
|
||||
if (this.layout.showBorder !== false) {
|
||||
image.drawRoundedRect(x, y, width, height, 72);
|
||||
if (this.layout.squareCorners) image.drawRect(x, y, width, height, 72);
|
||||
else image.drawRoundedRect(x, y, width, height, 72);
|
||||
}
|
||||
if (this.title) {
|
||||
image.drawText(font, x + 12, y + 8, this.title, 220);
|
||||
@@ -255,79 +307,32 @@ export class MenuLayer implements Layer {
|
||||
}
|
||||
|
||||
const bodyY = y + chromeTop;
|
||||
const focused = ctx.stack.isFocused();
|
||||
const lastVisibleRow = Math.min(this.items.length, this.scrollRow + visibleRowCount);
|
||||
for (let index = this.scrollRow; index < lastVisibleRow; index++) {
|
||||
const item = this.items[index]!;
|
||||
const rowY = bodyY + (index - this.scrollRow) * rowHeight;
|
||||
const selected = index === this.selectedIndex;
|
||||
const disabled = isMenuItemDisabled(item);
|
||||
if (selected) {
|
||||
drawSelectionHighlight(
|
||||
image,
|
||||
x + 12,
|
||||
rowY,
|
||||
width - 24,
|
||||
rowHeight - 1,
|
||||
focused,
|
||||
8,
|
||||
);
|
||||
}
|
||||
if (item.render) {
|
||||
item.render({
|
||||
image,
|
||||
x: x + 22,
|
||||
y: rowY,
|
||||
width: width - 44,
|
||||
height: rowHeight - 3,
|
||||
selected,
|
||||
disabled,
|
||||
text: item.label,
|
||||
ctx,
|
||||
});
|
||||
} else {
|
||||
image.drawText(font, x + 22, rowY + LIST_ROW_TEXT_INSET, item.label, disabled ? 70 : selected ? 255 : 200);
|
||||
}
|
||||
const listHeight = visibleRowCount * rowHeight;
|
||||
this.paintCtx = ctx;
|
||||
try {
|
||||
this.menu.paint(image, { x: x + MENU_ROW_INSET, y: bodyY, width: width - 2 * MENU_ROW_INSET, height: listHeight },
|
||||
ctx.stack.isFocused());
|
||||
} finally {
|
||||
this.paintCtx = null;
|
||||
}
|
||||
|
||||
if (this.items.length > visibleRowCount) {
|
||||
drawListScrollbar(
|
||||
image,
|
||||
x + width - 7,
|
||||
bodyY,
|
||||
visibleRowCount * rowHeight - 4,
|
||||
this.scrollRow,
|
||||
visibleRowCount,
|
||||
this.items.length,
|
||||
);
|
||||
}
|
||||
|
||||
this.menu.drawScrollbar(image, x + width - 7, bodyY, listHeight - 4);
|
||||
return image;
|
||||
}
|
||||
|
||||
async handleInput(event: InputEvent, ctx: LayerContext): Promise<void> {
|
||||
if (!this.items.length) {
|
||||
if (event.type === "double-click") {
|
||||
ctx.stack.pop();
|
||||
}
|
||||
return;
|
||||
}
|
||||
switch (event.type) {
|
||||
case "scroll-up":
|
||||
this.selectedIndex = (this.selectedIndex + this.items.length - 1) % this.items.length;
|
||||
return;
|
||||
case "scroll-down":
|
||||
this.selectedIndex = (this.selectedIndex + 1) % this.items.length;
|
||||
return;
|
||||
case "double-click":
|
||||
ctx.stack.pop();
|
||||
return;
|
||||
case "click":
|
||||
if (!isMenuItemDisabled(this.items[this.selectedIndex]!)) {
|
||||
await this.items[this.selectedIndex]!.onSelect(ctx, this);
|
||||
case "click": {
|
||||
const item = this.menu.selectedItem;
|
||||
if (item && !isMenuItemDisabled(item)) {
|
||||
await item.onSelect(ctx, this);
|
||||
}
|
||||
return;
|
||||
}
|
||||
default:
|
||||
await this.menu.handleInput(event);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
+65
-5
@@ -8,6 +8,8 @@
|
||||
*
|
||||
* The formulas reproduce the pre-TTF constants exactly at the 12px default:
|
||||
* listRowHeight 20, tightRowHeight 16, lineStep 14, menuTitleHeight 16.
|
||||
* tightRowHeight follows the measured ink rather than the line box, which
|
||||
* for Terminus is the same thing.
|
||||
*/
|
||||
import type { UiFont } from "../graphics/image";
|
||||
|
||||
@@ -20,13 +22,71 @@ export function listRowHeight(font: UiFont): number {
|
||||
export const LIST_ROW_TEXT_INSET = 4;
|
||||
|
||||
/**
|
||||
* Height of one row in dense lists (file browser, track lists). Grows at
|
||||
* half the lineHeight's rate above the 12px anchor: TTF line heights carry
|
||||
* internal leading that already reads as row spacing, so full-rate growth
|
||||
* made large-font rows feel too tall.
|
||||
* Characters whose ink bounds a font's text rows: accented capitals (the
|
||||
* highest Latin marks), descenders, brackets, and digits.
|
||||
*/
|
||||
const INK_SAMPLE = "ÅÉÎÖÇÑbdfhklgjpqy()[]{}|/_0123456789";
|
||||
|
||||
export type TextInkBounds = {
|
||||
/** First inked row, relative to the drawText y (the line top); may be negative. */
|
||||
top: number;
|
||||
/** One past the last inked row, relative to the line top; may exceed lineHeight. */
|
||||
bottom: number;
|
||||
};
|
||||
|
||||
const inkBoundsCache = new WeakMap<UiFont, TextInkBounds>();
|
||||
|
||||
/**
|
||||
* The vertical extent of a font's ink over a representative sample, measured
|
||||
* from the rendered glyphs. Bitmap faces fill their line box exactly; TTF
|
||||
* faces may leave internal leading inside it or overshoot it with marks.
|
||||
* Falls back to the line box when the font renders no sample ink.
|
||||
*/
|
||||
export function textInkBounds(font: UiFont): TextInkBounds {
|
||||
const cached = inkBoundsCache.get(font);
|
||||
if (cached) return cached;
|
||||
let top = Infinity;
|
||||
let bottom = -Infinity;
|
||||
if (typeof font.getGlyph === "function") {
|
||||
for (const char of INK_SAMPLE) {
|
||||
const glyph = font.getGlyph(char.codePointAt(0)!);
|
||||
if (!glyph || glyph.bbxHeight <= 0) continue;
|
||||
// Same placement as rasterizeGlyph: baseline at y + ascent, bbxY up from it.
|
||||
const glyphTop = font.ascent - (glyph.bbxHeight + glyph.bbxY);
|
||||
top = Math.min(top, glyphTop);
|
||||
bottom = Math.max(bottom, glyphTop + glyph.bbxHeight);
|
||||
}
|
||||
}
|
||||
const bounds = top < bottom ? { top, bottom } : { top: 0, bottom: font.lineHeight };
|
||||
inkBoundsCache.set(font, bounds);
|
||||
return bounds;
|
||||
}
|
||||
|
||||
/** Height of a font's ink extent (see textInkBounds). */
|
||||
export function textInkHeight(font: UiFont): number {
|
||||
const { top, bottom } = textInkBounds(font);
|
||||
return bottom - top;
|
||||
}
|
||||
|
||||
/**
|
||||
* The drawText y that centers a font's ink vertically in a box, rounding the
|
||||
* spare pixel to the bottom. Pair with row heights derived from
|
||||
* textInkHeight so the ink fits whatever the face's internal leading.
|
||||
*/
|
||||
export function centeredTextY(font: UiFont, boxTop: number, boxHeight: number): number {
|
||||
const { top } = textInkBounds(font);
|
||||
return boxTop + Math.floor((boxHeight - textInkHeight(font)) / 2) - top;
|
||||
}
|
||||
|
||||
/**
|
||||
* Row pitch in dense lists (file browser, track lists): the font's ink height
|
||||
* plus 4px, so a selection box one pixel shorter than the pitch keeps at
|
||||
* least a pixel of clearance around the ink. Draw the text with
|
||||
* centeredTextY; a fixed offset from the row top can overflow faces whose
|
||||
* ink starts below their line top.
|
||||
*/
|
||||
export function tightRowHeight(font: UiFont): number {
|
||||
return font.lineHeight + 4 - Math.floor(Math.max(0, font.lineHeight - 12) / 2);
|
||||
return textInkHeight(font) + 4;
|
||||
}
|
||||
|
||||
/** Step between consecutive lines of body/paragraph text. */
|
||||
|
||||
@@ -7,35 +7,41 @@ import { type InputEvent } from "./gestures";
|
||||
import { type Layer, type LayerContext, type PaintBelow } from "./layers";
|
||||
import { drawToggleMenuItem, MenuLayer, type MenuItem } from "./menu";
|
||||
|
||||
/** Rebuild rows when new sources arrive, keeping the current selection by package. */
|
||||
/** Refresh rows as new sources arrive, keeping the current selection by package. */
|
||||
export class NotificationFilterLayer implements Layer {
|
||||
private menu: MenuLayer | null = null;
|
||||
private packages = "";
|
||||
private selectedPackage = "";
|
||||
private menuWidth = 0;
|
||||
/** The package behind each current row, parallel to the menu's items. */
|
||||
private packages: string[] = [];
|
||||
|
||||
private currentMenu(ctx: LayerContext): MenuLayer {
|
||||
readActiveNotifications(ALL_NOTIFICATIONS);
|
||||
const sources = readNotificationSources().sort((a, b) =>
|
||||
a.appName.localeCompare(b.appName) || a.packageName.localeCompare(b.packageName));
|
||||
const signature = JSON.stringify([ctx.stack.getBaseSize().width, sources.map((source) => [source.packageName, source.appName])]);
|
||||
if (!this.menu || signature !== this.packages) {
|
||||
this.packages = signature;
|
||||
const items: MenuItem[] = sources.map((source) => ({
|
||||
label: source.appName,
|
||||
onSelect: () => setNotificationSourceEnabled(source, !shouldShowNotificationOnGlasses(source.packageName)),
|
||||
render: ({ image, x, y, width, selected }) => {
|
||||
if (selected) this.selectedPackage = source.packageName;
|
||||
const font = getDefaultSmallFont();
|
||||
drawToggleMenuItem(image, font, x, y, width, truncateText(font, source.appName, width - 48),
|
||||
shouldShowNotificationOnGlasses(source.packageName), selected);
|
||||
},
|
||||
}));
|
||||
if (!items.length) items.push({ label: "No notification sources discovered yet.", disabled: true, onSelect: () => {} });
|
||||
const items: MenuItem[] = sources.map((source) => ({
|
||||
label: source.appName,
|
||||
onSelect: () => setNotificationSourceEnabled(source, !shouldShowNotificationOnGlasses(source.packageName)),
|
||||
render: ({ image, x, y, width, selected }) => {
|
||||
const font = getDefaultSmallFont();
|
||||
drawToggleMenuItem(image, font, x, y, width, truncateText(font, source.appName, width - 48),
|
||||
shouldShowNotificationOnGlasses(source.packageName), selected);
|
||||
},
|
||||
}));
|
||||
if (!items.length) items.push({ label: "No notification sources discovered yet.", disabled: true, onSelect: () => {} });
|
||||
const selectedPackage = this.menu?.selectedIndex != null ? this.packages[this.menu.selectedIndex] : undefined;
|
||||
const index = sources.findIndex((source) => source.packageName === selectedPackage);
|
||||
this.packages = sources.map((source) => source.packageName);
|
||||
|
||||
const width = ctx.stack.getBaseSize().width;
|
||||
if (!this.menu || width !== this.menuWidth) {
|
||||
this.menuWidth = width;
|
||||
this.menu = new MenuLayer("Notification filter", items, {
|
||||
x: 8, y: 8, width: ctx.stack.getBaseSize().width - 16,
|
||||
x: 8, y: 8, width: width - 16,
|
||||
opaque: true, showBorder: false,
|
||||
footer: "On = show popups on glasses",
|
||||
}).selectItem(Math.max(0, sources.findIndex((source) => source.packageName === this.selectedPackage)));
|
||||
}).selectItem(Math.max(0, index));
|
||||
} else {
|
||||
this.menu.setItems(items, index >= 0 ? index : undefined);
|
||||
}
|
||||
return this.menu;
|
||||
}
|
||||
|
||||
Loaded 100 of 411 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user