Merge upstream main preserving custom feature integrations

This commit is contained in:
MasterControl committed 2026-11-10 17:08:15 +00:00
commit 6150a1ac57
228 files changed
+16844 -1388

No files matched your search

+7
View File
@@ -30,6 +30,13 @@ export type AppDefinition = {
boot?: (ctx: AppContext) => void;
/** False for apps that must not appear in the launcher grid (the launcher itself). */
showInLauncher?: boolean;
/**
* False while the paired glasses can't run the app (absent means they can).
* It then leaves the launcher grid and what mirrors it: the watch remote and
* the apps.launch tool. A window restored from before a re-pair still opens,
* so the app needs to say what's wrong rather than assume it never runs.
*/
isSupported?: () => boolean;
/** Present on the app that handles text shared via the Android share intent. */
openSharedText?: (ctx: AppContext, title: string, text: string) => void;
/**
+8 -4
View File
@@ -10,6 +10,7 @@ import {
type CompassDiagnostics,
} from "../../native/compass";
import { wrapText } from "../../graphics/textwrap";
import { glassesHaveCompass } from "../../ui/dashboard-settings";
import { type InputEvent } from "../../ui/gestures";
import { type Layer, type LayerContext } from "../../ui/layers";
import { lineStep } from "../../ui/metrics";
@@ -151,6 +152,7 @@ class CompassLayer implements Layer {
}
private statusText(): string {
if (!glassesHaveCompass()) return "These glasses have no compass sensor";
if (this.firmwareStatus !== null) return this.firmwareStatus;
if (!this.enabled) return "Compass paused";
if (this.rawHeading === null) return "Waiting for compass data…";
@@ -206,8 +208,10 @@ class CompassLayer implements Layer {
image.drawText(small, Math.round(cx - small.measureText(line) / 2), y, line, 125);
y += smallStep;
}
const accuracyText = magneticAccuracyText(this.diagnostics);
image.drawText(small, width - ACCURACY_RIGHT_PAD - small.measureText(accuracyText), ACCURACY_TOP_PAD, accuracyText, 175);
if (glassesHaveCompass()) {
const accuracyText = magneticAccuracyText(this.diagnostics);
image.drawText(small, width - ACCURACY_RIGHT_PAD - small.measureText(accuracyText), ACCURACY_TOP_PAD, accuracyText, 175);
}
return image;
}
@@ -216,7 +220,7 @@ class CompassLayer implements Layer {
// "Uncalibrated" prompt advertises, and re-calibrating is a normal thing to
// want once the glasses have been taken off and put back on.
handleInput(event: InputEvent, ctx: LayerContext): void {
if (event.type === "click") this.openCalibration(ctx);
if (event.type === "click" && glassesHaveCompass()) this.openCalibration(ctx);
}
}
@@ -231,7 +235,7 @@ export function createCompassAppWindow(options: InProcessAppOptions): InProcessW
icon: "compass",
closeable: true,
actions: options.actions,
menuItems: () => [
menuItems: () => !glassesHaveCompass() ? [] : [
{
label: "Calibrate",
onSelect: (ctx) => {
+2
View File
@@ -1,3 +1,4 @@
import { glassesHaveCompass } from "../../ui/dashboard-settings";
import { type AppDefinition } from "../app-definition";
import { COMPASS_SURFACE_ID, COMPASS_WINDOW_ID, createCompassAppWindow } from "./compass-app";
@@ -5,6 +6,7 @@ const compassApp: AppDefinition = {
appId: "compass",
title: "Compass",
icon: "compass",
isSupported: glassesHaveCompass,
launch: (ctx) => ctx.launchInProcessApp(COMPASS_WINDOW_ID, COMPASS_SURFACE_ID, createCompassAppWindow),
};
+2 -2
View File
@@ -4,7 +4,7 @@ import { type InputEvent } from "../../ui/gestures";
import { Layer, type LayerContext } from "../../ui/layers";
import { shell } from "../../ui/shell/shell";
import { MenuLayer, type MenuItem } from "../../ui/menu";
import { ringConnectionModeSetting } from "../../ui/dashboard-settings";
import { effectiveRingConnectionMode, ringConnectionModeSetting } from "../../ui/dashboard-settings";
import { loadDeviceAddresses } from "../../g2/device-addresses";
import {
getRingSessionStatus,
@@ -146,7 +146,7 @@ export class RingStatusLayer implements Layer {
}
if (!s.directEnabled) {
const pending = ringConnectionModeSetting.get() !== "glasses";
const pending = effectiveRingConnectionMode() !== "glasses";
rows.push({ label: "Direct link", value: pending ? "on after glasses reconnect" : "off (only via glasses)", dim: true });
} else {
const state = s.directNotificationsReady ? "subscribed"
+129 -48
View File
@@ -7,16 +7,23 @@
* match what apps expect. Extended layouts may pick other fonts per container
* (fonts.ts), which wrap with Faceclaw's own text wrapping.
*
* Containers stack in stock's order (paintOrder): without zOrderIndex, lists,
* then texts, then images, so an image container covers text beneath it. Text
* containers are transparent windows that clip their text and scroll it.
*
* Known deviations from stock, acceptable for now:
* - List rendering is a plain vertical list. A non-zero itemWidth sets the
* selection outline's width and truncates long items to fit, as on stock;
* whether stock also lays narrow items out side by side is unknown.
* - Lists scroll with our menus' animation and bounce (see list-menu.ts).
* - Text scrolls in jumps; stock eases each 28px step over 200ms and
* rubber-bands at the ends.
*/
import { GrayImage } from "../../graphics/image";
import { containerFace } from "./fonts";
import {
type EvenHubContainer,
borderGrey,
paintOrder,
type EvenHubImageContainer,
type EvenHubListContainer,
type EvenHubPage,
@@ -30,35 +37,122 @@ export const EVENHUB_SCREEN_HEIGHT = 288;
const TEXT_WHITE = 255;
/**
* SDK 0.0.14 textColor levels 0..4 as grey values. Level 4 is the default and
* the only one apps got before 0.0.14, so it must stay exactly TEXT_WHITE.
* Level 0 is the firmware's "fully dim" step, which over the black page
* background means the text is effectively not shown.
* SDK 0.0.14 textColor levels 0..4 as the greys stock draws them (measured
* in the firmware emulator): level 0 is dim, not off. A missing or
* out-of-range level draws white, the only colour before 0.0.14.
*/
const TEXT_BRIGHTNESS_GREYS = [0, 64, 128, 191, 255];
const TEXT_BRIGHTNESS_GREYS = [51, 102, 153, 204, 255];
function textGrey(level: number | undefined): number {
return level === undefined ? TEXT_WHITE : (TEXT_BRIGHTNESS_GREYS[level] ?? TEXT_WHITE);
}
/** Chars the font lacks are skipped silently on stock (no tofu); EvenHubFont
* skips unknown glyphs, matching that. */
function paintTextContainer(image: GrayImage, container: EvenHubTextContainer): void {
// Only the text dims: textColor is a text-brightness level, and stock draws
// the container's border at full brightness regardless.
paintBorder(image, container);
/**
* The scrollbar stock draws on any text container whose text overflows
* (LV_SCROLLBAR_MODE_AUTO), whether or not it captures input: 4px wide, 10px
* in from the right edge, in a track 6px in from top and bottom, with its
* corner pixels cut. Measured in the firmware emulator.
*/
const SCROLLBAR_GREY = 187;
const SCROLLBAR_WIDTH = 4;
const SCROLLBAR_RIGHT_INSET = 10;
const SCROLLBAR_TRACK_INSET = 6;
type Rect = { x: number; y: number; width: number; height: number };
/** A text container's wrapped lines and the geometry its scrolling works in. */
export type TextLayout = {
lines: string[];
lineHeight: number;
/** Left edge and unscrolled top of the text. */
contentX: number;
contentY: number;
/** Height of the window the text scrolls in (inside border and padding). */
viewportHeight: number;
/** Largest scrollY: how far the text overflows that window. */
maxScroll: number;
};
/**
* Stock lays text out at x + border + padding and wraps it at width - 2 *
* padding: the label is sized ignoring the border, so a line can run into
* the right border, where it is clipped.
*/
export function layoutText(container: EvenHubTextContainer): TextLayout {
const inset = container.borderWidth + container.paddingLength;
const face = containerFace(container.font);
const lines = face.wrap(container.content, Math.max(1, container.width - 2 * inset));
for (let i = 0; i < lines.length; i++) {
face.drawText(image, container.x + inset, container.y + inset + i * face.lineHeight, lines[i]!, textGrey(container.textColor));
const lines = face.wrap(container.content, Math.max(1, container.width - 2 * container.paddingLength));
const viewportHeight = Math.max(0, container.height - 2 * inset);
return {
lines,
lineHeight: face.lineHeight,
contentX: container.x + inset,
contentY: container.y + inset,
viewportHeight,
maxScroll: Math.max(0, lines.length * face.lineHeight - viewportHeight),
};
}
/**
* Text is clipped at the border's inner edge (at the container's edge when it
* has no border). Lines wholly inside stay deferred firmware-text draws; a
* line the edge cuts is rasterized and blitted clipped. Chars the font lacks
* are skipped silently, as on stock (no tofu).
*/
function paintTextContainer(image: GrayImage, container: EvenHubTextContainer): void {
// Only the text dims: textColor is a text-brightness level; the border has its own color.
paintBorder(image, container);
const layout = layoutText(container);
const face = containerFace(container.font);
const grey = textGrey(container.textColor);
const border = container.borderWidth;
const clip: Rect = {
x: container.x + border,
y: container.y + border,
width: Math.max(0, container.width - 2 * border),
height: Math.max(0, container.height - 2 * border),
};
for (let i = 0; i < layout.lines.length; i++) {
const y = layout.contentY + i * layout.lineHeight - container.scrollY;
const bottom = y + layout.lineHeight;
if (bottom <= clip.y || y >= clip.y + clip.height) continue;
const line = layout.lines[i]!;
if (y >= clip.y && bottom <= clip.y + clip.height && layout.contentX + face.measureLine(line) <= clip.x + clip.width) {
face.drawText(image, layout.contentX, y, line, grey);
continue;
}
const strip = new GrayImage(container.width, layout.lineHeight, 0);
face.drawText(strip, layout.contentX - container.x, 0, line, grey);
const visibleTop = Math.max(y, clip.y);
image.bitBlt(strip.withDrawsBaked(), clip.x, visibleTop, {
sx: clip.x - container.x,
sy: visibleTop - y,
width: clip.width,
height: Math.min(bottom, clip.y + clip.height) - visibleTop,
transparentZero: true,
});
}
if (layout.maxScroll > 0) paintScrollbar(image, container, layout);
}
function paintScrollbar(image: GrayImage, container: EvenHubTextContainer, layout: TextLayout): void {
const height = container.height;
const track = height - 2 * SCROLLBAR_TRACK_INSET;
const length = Math.floor((track * height) / (height + layout.maxScroll));
if (track <= 0 || length <= 0) return;
const top = container.y + SCROLLBAR_TRACK_INSET + Math.floor(((track - length) * container.scrollY) / layout.maxScroll);
const left = container.x + container.width - SCROLLBAR_RIGHT_INSET;
for (let row = 0; row < length; row++) {
const end = row === 0 || row === length - 1;
image.fillRect(end ? left + 1 : left, top + row, end ? SCROLLBAR_WIDTH - 2 : SCROLLBAR_WIDTH, 1, SCROLLBAR_GREY);
}
}
function paintBorder(
image: GrayImage,
container: { x: number; y: number; width: number; height: number; borderWidth: number; borderRadius: number },
container: { x: number; y: number; width: number; height: number; borderWidth: number; borderColor: number; borderRadius: number },
): void {
const grey = borderGrey(container.borderColor);
for (let i = 0; i < container.borderWidth; i++) {
if (container.borderRadius > 0) {
image.drawRoundedRect(
@@ -66,28 +160,28 @@ function paintBorder(
container.y + i,
container.width - 2 * i,
container.height - 2 * i,
TEXT_WHITE,
grey,
Math.max(0, container.borderRadius - i),
);
} else {
image.drawRect(container.x + i, container.y + i, container.width - 2 * i, container.height - 2 * i, TEXT_WHITE);
image.drawRect(container.x + i, container.y + i, container.width - 2 * i, container.height - 2 * i, grey);
}
}
}
/**
* An opaque bitmap (black pixels cover what's beneath). Updates are converted
* to exactly the container's size (image-pipeline.ts); pixels an extended
* layout carried over from a differently sized container are clipped.
*/
function paintImageContainer(image: GrayImage, container: EvenHubImageContainer): void {
if (!container.pixels || container.pixelsWidth <= 0 || container.pixelsHeight <= 0) return;
const source = new GrayImage(container.pixelsWidth, container.pixelsHeight, 0);
source.pixels.set(container.pixels);
// Stock quirk: raw data smaller than the container tiles/repeats to fill it.
for (let ty = 0; ty < container.height; ty += container.pixelsHeight) {
for (let tx = 0; tx < container.width; tx += container.pixelsWidth) {
image.bitBlt(source, container.x + tx, container.y + ty, {
width: Math.min(container.pixelsWidth, container.width - tx),
height: Math.min(container.pixelsHeight, container.height - ty),
});
}
}
image.bitBlt(source, container.x, container.y, {
width: Math.min(container.pixelsWidth, container.width),
height: Math.min(container.pixelsHeight, container.height),
});
}
function paintListContainer(image: GrayImage, container: EvenHubListContainer, focused: boolean): void {
@@ -95,36 +189,23 @@ function paintListContainer(image: GrayImage, container: EvenHubListContainer, f
paintListItems(image, container, containerFace(container.font), focused);
}
/** Bottom-to-top paint order for a page's containers. */
function paintOrder(containers: EvenHubContainer[]): EvenHubContainer[] {
const anyZ = containers.some((c) => c.zOrderIndex !== undefined);
if (anyZ) {
// Explicit z-order: larger zOrderIndex renders closer to the front, i.e.
// painted later. (Stock requires all-or-nothing + unique values.)
return [...containers].sort((a, b) => (a.zOrderIndex ?? 0) - (b.zOrderIndex ?? 0));
}
// Default (no z-order): image containers sit below lists and text. Cross-type
// order isn't defined by the wire format (separate object arrays), so this is
// our choice, matching stock. Order within each group is preserved.
const images = containers.filter((c) => c.kind === "image");
const rest = containers.filter((c) => c.kind !== "image");
return [...images, ...rest];
}
/** Render a page into a fresh app-viewport image (positioned by the shell). */
export function compositePage(page: EvenHubPage | null, size: { width: number; height: number }, focused: boolean): GrayImage {
const image = new GrayImage(size.width, size.height, 0);
if (!page) return image;
for (const container of paintOrder(page.containers)) {
switch (container.kind) {
case "image":
case "image": {
if (!container.pixels) break; // Nothing is drawn before the first update.
// Text painted so far is deferred draws (firmware-text runs), which
// would otherwise render above this container's raster. Stock puts a
// later image container OVER earlier text, so bake the pending draws
// first; text painted after this container stays deferred (and above).
image.bakeDeferredDrawsInPlace();
// would otherwise render above this container's raster. The image is
// opaque and covers what's under it, so bake the draws it overlaps
// first; the others, and text painted later, stay deferred.
const box: Rect = { x: container.x, y: container.y, width: container.width, height: container.height };
image.bakeDeferredDrawsWithin(box);
paintImageContainer(image, container);
break;
}
case "text":
paintTextContainer(image, container);
break;
+130 -13
View File
@@ -6,6 +6,7 @@
* reads go through loose key matching (the SDK's pickLoose behavior).
*/
import { mergeFontSpecs, parseFontSpec, type EvenHubFontSpec } from "./font-spec";
import { inStockImageRange } from "./image-pipeline";
export type EvenHubTextContainer = {
kind: "text";
@@ -16,6 +17,8 @@ export type EvenHubTextContainer = {
width: number;
height: number;
borderWidth: number;
/** Firmware border color index 0..16; see borderGrey. */
borderColor: number;
borderRadius: number;
paddingLength: number;
isEventCapture: boolean;
@@ -27,6 +30,11 @@ export type EvenHubTextContainer = {
textColor: number | undefined;
/** Extended-layout font (merged with the layout's default); undefined is the stock font. */
font: EvenHubFontSpec | undefined;
/**
* How far the text is scrolled up, in pixels. Host-local like a list's
* selection: stock scrolls an overflowing capture container itself.
*/
scrollY: number;
};
export type EvenHubImageContainer = {
@@ -54,6 +62,8 @@ export type EvenHubListContainer = {
width: number;
height: number;
borderWidth: number;
/** Firmware border color index 0..16; see borderGrey. */
borderColor: number;
borderRadius: number;
paddingLength: number;
isEventCapture: boolean;
@@ -80,7 +90,7 @@ export type EvenHubMenuItem = {
};
export type EvenHubPage = {
/** In declaration order (lists, images, texts); z-sorted at paint time. */
/** Lists, then texts, then images, each in declaration order (stock's container list order); z-sorted at paint time. */
containers: EvenHubContainer[];
/**
* The page's contextual-menu entries (SDK 0.0.14 `menuObject`). Empty when
@@ -92,6 +102,26 @@ export type EvenHubPage = {
/** Stock draws at most a 5px border; wider requests are clamped. */
const MAX_BORDER_WIDTH = 5;
/**
* Stock firmware limits for list and text containers (common_text_create /
* common_list_create clamp to these), from the evenRealities-openCFW
* reconstruction. Extended layouts are exempt: they have their own canvas.
*/
const STOCK_SCREEN_WIDTH = 576;
const STOCK_SCREEN_HEIGHT = 288;
const STOCK_MAX_BORDER_COLOR = 16;
const STOCK_MAX_BORDER_RADIUS = 10;
const STOCK_MAX_PADDING = 32;
const STOCK_MAX_LIST_ITEMS = 20;
/**
* The border color index's grey: indices 0..15 step by 0x11 (0 is black, so a
* border without a color is invisible) and 16 is white.
*/
export function borderGrey(index: number): number {
return index >= 0 && index < 16 ? index * 0x11 : 0xff;
}
/** Firmware brightness levels for text; 4 is the device default. */
const MIN_TEXT_BRIGHTNESS = 0;
const MAX_TEXT_BRIGHTNESS = 4;
@@ -154,6 +184,11 @@ function readBorderWidth(json: Record<string, unknown>): number {
return Math.max(0, Math.min(MAX_BORDER_WIDTH, readNumber(json, "borderWidth", 0)));
}
/** A protobuf field, so an omitted borderColor is 0 (black). */
function readBorderColor(json: Record<string, unknown>): number {
return Math.max(0, readNumber(json, "borderColor", 0));
}
/** A container's font: its own `font` over the layout-wide default. */
function readFont(json: Record<string, unknown>, layoutFont: EvenHubFontSpec | undefined): EvenHubFontSpec | undefined {
return mergeFontSpecs(layoutFont, parseFontSpec(pickLoose(json, "font")));
@@ -169,6 +204,7 @@ function parseTextContainer(json: Record<string, unknown>, layoutFont: EvenHubFo
width: readNumber(json, "width", 0),
height: readNumber(json, "height", 0),
borderWidth: readBorderWidth(json),
borderColor: readBorderColor(json),
borderRadius: readNumber(json, "borderRadius", 0),
paddingLength: readNumber(json, "paddingLength", 0),
isEventCapture: readFlag(json, "isEventCapture"),
@@ -177,6 +213,7 @@ function parseTextContainer(json: Record<string, unknown>, layoutFont: EvenHubFo
content: readString(json, "content", ""),
textColor: readTextBrightness(json),
font: readFont(json, layoutFont),
scrollY: 0,
};
}
@@ -262,6 +299,7 @@ function parseListContainer(json: Record<string, unknown>, layoutFont: EvenHubFo
width: readNumber(json, "width", 0),
height: readNumber(json, "height", 0),
borderWidth: readBorderWidth(json),
borderColor: readBorderColor(json),
borderRadius: readNumber(json, "borderRadius", 0),
paddingLength: readNumber(json, "paddingLength", 0),
isEventCapture: readFlag(json, "isEventCapture"),
@@ -277,9 +315,8 @@ function parseListContainer(json: Record<string, unknown>, layoutFont: EvenHubFo
/**
* Parse a CreateStartUpPageContainer / RebuildPageContainer payload into a
* page. Containers keep declaration order within and across the three arrays
* (lists, then images, then texts — texts last so info panels and capture
* containers sit above image tiles when no zOrderIndex is given).
* page. Containers are kept in stock's container-list order: lists, then
* texts, then images, each in declaration order (evenhub_build_container_list).
*
* The whole page is replaced on rebuild, which is also what gives an omitted
* menuObject its documented meaning: the previous contextual menu goes away.
@@ -292,20 +329,100 @@ export function parsePage(data: Record<string, unknown>): EvenHubPage {
if (Array.isArray(lists)) {
for (const item of lists) containers.push(parseListContainer(asRecord(item), layoutFont));
}
const images = pickLoose(data, "imageObject");
if (Array.isArray(images)) {
for (const item of images) containers.push(parseImageContainer(asRecord(item)));
}
const texts = pickLoose(data, "textObject");
if (Array.isArray(texts)) {
for (const item of texts) containers.push(parseTextContainer(asRecord(item), layoutFont));
}
const images = pickLoose(data, "imageObject");
if (Array.isArray(images)) {
for (const item of images) containers.push(parseImageContainer(asRecord(item)));
}
return { containers, menuItems: parseMenuItems(data) };
}
/** The container that owns gesture events (exactly one per valid page). */
export function eventCaptureContainer(page: EvenHubPage): EvenHubContainer | undefined {
return page.containers.find(
(container) => container.kind !== "image" && container.isEventCapture,
);
/**
* Clamp a stock page's containers the way the firmware does when it creates
* them: positions and sizes to the 576x288 screen (images keep their size,
* which a separate check rejects instead), border color, radius and padding to
* their maxima, and lists to 20 items. Not for extended layouts.
*/
export function clampToStockLimits(page: EvenHubPage): void {
for (const c of page.containers) {
c.x = Math.min(c.x, STOCK_SCREEN_WIDTH);
c.y = Math.min(c.y, STOCK_SCREEN_HEIGHT);
if (c.kind === "image") continue;
c.width = Math.min(c.width, STOCK_SCREEN_WIDTH);
c.height = Math.min(c.height, STOCK_SCREEN_HEIGHT);
c.borderColor = Math.min(c.borderColor, STOCK_MAX_BORDER_COLOR);
c.borderRadius = Math.min(c.borderRadius, STOCK_MAX_BORDER_RADIUS);
c.paddingLength = Math.min(c.paddingLength, STOCK_MAX_PADDING);
if (c.kind === "list" && c.itemNames.length > STOCK_MAX_LIST_ITEMS) {
c.itemNames = c.itemNames.slice(0, STOCK_MAX_LIST_ITEMS);
}
}
}
/** The firmware's fixed protobuf arrays: more containers than this fail to decode. */
const STOCK_MAX_LISTS = 4;
const STOCK_MAX_TEXTS = 8;
const STOCK_MAX_IMAGES = 4;
/** EhStartUpPageCreateResult values for a page the firmware can't build. */
export const PAGE_CREATE_INVALID = 1;
export const PAGE_CREATE_OUT_OF_MEMORY = 3;
/**
* Why the stock firmware would fail to build this page, with the create result
* the app gets, or null when it builds. Too many containers overflow the
* firmware's protobuf arrays, so decoding fails (it replies with the code the
* phone app maps to outOfMemory). An image container outside 20..288 x 20..144
* fails to create (invalid container).
*/
export function stockPageFailure(page: EvenHubPage): { code: number; reason: string } | null {
const count = (kind: EvenHubContainer["kind"]) => page.containers.filter((c) => c.kind === kind).length;
const [lists, texts, images] = [count("list"), count("text"), count("image")];
if (lists > STOCK_MAX_LISTS || texts > STOCK_MAX_TEXTS || images > STOCK_MAX_IMAGES) {
return {
code: PAGE_CREATE_OUT_OF_MEMORY,
reason: `${lists} list, ${texts} text and ${images} image containers (at most ${STOCK_MAX_LISTS}, ${STOCK_MAX_TEXTS} and ${STOCK_MAX_IMAGES})`,
};
}
for (const c of page.containers) {
if (c.kind === "image" && !inStockImageRange(c.width, c.height)) {
return { code: PAGE_CREATE_INVALID, reason: `image container ${JSON.stringify(c.name)} is ${c.width}x${c.height}, outside 20..288 x 20..144` };
}
}
return null;
}
/**
* Bottom-to-top paint order, as stock stacks its containers: the container
* list order (lists, texts, images), stably sorted by zOrderIndex (larger is
* closer to the front) only when every container has one and there are at
* least two. So without z-order, images are drawn over text.
*/
export function paintOrder(containers: readonly EvenHubContainer[]): EvenHubContainer[] {
const allHaveZ = containers.every((c) => c.zOrderIndex !== undefined);
if (!allHaveZ || containers.length < 2) return [...containers];
return [...containers].sort((a, b) => (a.zOrderIndex ?? 0) - (b.zOrderIndex ?? 0));
}
/**
* The container that owns gesture events. Stock binds the first list or text
* container with isEventCapture in paint order and ignores any later ones.
*/
export function eventCaptureContainer(page: EvenHubPage): EvenHubTextContainer | EvenHubListContainer | undefined {
for (const c of paintOrder(page.containers)) {
if (c.kind !== "image" && c.isEventCapture) return c;
}
return undefined;
}
/**
* The container stock resolves an id to: the first one with that id in paint
* order, whatever its kind, so an update aimed at a later container that
* shares the id never reaches it.
*/
export function containerById(page: EvenHubPage | null, id: number): EvenHubContainer | undefined {
return page ? paintOrder(page.containers).find((c) => c.id === id) : undefined;
}
@@ -0,0 +1,65 @@
import { wrapText } from "../../graphics/textwrap";
import { type LayerContext } from "../../ui/layers";
import { lineStep } from "../../ui/metrics";
import { ScrollingDialogLayer, type DialogBody, type DialogRow } from "./scrolling-dialog";
/** Wider than the permission dialog: the RayNeo iO's window is short, so fewer, longer lines fit better. */
const DIALOG_WIDTH = 420;
/**
* Shown over an EvenHub app the first time it runs with a RayNeo iO paired
* (see glasses-compat.ts): the app is made for Even Realities glasses and
* won't display correctly on the iO's screen. The app starts underneath.
* Continue (or double-click) dismisses the warning; Close app closes the
* app's window. Either answer counts as having seen it; a window closed with
* the warning still up shows it again next time.
*/
export class EvenHubDisplayWarningLayer extends ScrollingDialogLayer {
constructor(
private readonly appName: string,
private readonly onAnswered: () => void,
private readonly closeApp: () => void,
) {
super(DIALOG_WIDTH);
}
protected bodyRows({ image, font, left, textWidth }: DialogBody): DialogRow[] {
const step = lineStep(font);
const rows: DialogRow[] = [];
for (const line of wrapText(font, "Compatibility warning", textWidth)) {
rows.push({ height: step + 2, draw: (top) => image.drawText(font, left, top, line, 235) });
}
rows[rows.length - 1]!.height += 6;
const message =
`${this.appName} is an EvenHub app, made for Even Realities glasses. It will not display correctly ` +
"on the RayNeo iO, because the iO's screen has a different resolution.";
// One row per wrapped line, so a large font scrolls line by line.
const lines = wrapText(font, message, textWidth);
for (let index = 0; index < lines.length; index++) {
rows.push({
height: step + (index === lines.length - 1 ? 8 : 0),
draw: (top) => image.drawText(font, left, top, lines[index]!, 190),
});
}
return rows;
}
protected actions(): string[] {
return ["Continue", "Close app"];
}
protected activate(action: string, ctx: LayerContext): void {
this.close(ctx, () => {
this.onAnswered();
if (action === "Close app") this.closeApp();
});
}
protected backedOut(): void {
this.onAnswered();
}
protected abandoned(): void {
// Not answered: the next launch shows it again.
}
}
+13
View File
@@ -16,6 +16,8 @@ import { shell } from "../../ui/shell/shell";
import { makeImageWindowIcon, windowIcon } from "../../ui/shell/chrome-layer";
import { EvenHubSession } from "./session";
import { renderInstalledEvenHubIcon } from "./installed-apps";
import { markDisplayWarningShown, needsDisplayWarning } from "./glasses-compat";
import { EvenHubDisplayWarningLayer } from "./display-warning-dialog";
class EvenHubAppLayer implements Layer {
/**
@@ -158,5 +160,16 @@ export function createEvenHubWindow(
session.setScreenOn(on);
};
// The app's first run with a RayNeo iO paired: warn, over the app itself,
// that it won't display correctly there.
const packageId = session.manifest.packageId;
if (needsDisplayWarning(packageId)) {
created.stack.push(new EvenHubDisplayWarningLayer(
session.manifest.name,
() => markDisplayWarningShown(packageId),
() => shell.closeWindow(windowId),
));
}
return created;
}
+38
View File
@@ -0,0 +1,38 @@
/**
* EvenHub with glasses that aren't Even Realities': with a RayNeo iO paired,
* the store is off (it is Even's, and its apps are made for their glasses),
* while apps sideloaded through Files or the Developer app still run, after a
* warning the first time each one launches that it won't display correctly
* (EvenHub apps lay out for the G2's 576x288 display; the iO's is 540x220).
*/
import { glassesTypeSetting } from "../../ui/dashboard-settings";
import { getStringSetting, setStringSetting } from "../../native/settings-store";
/** The package IDs whose RayNeo iO display warning has been answered, as a JSON array. */
const DISPLAY_WARNED_KEY = "evenhub.rayneoDisplayWarned";
/** Whether the EvenHub store can be used: with Even Realities glasses paired, not a RayNeo iO. */
export function isEvenHubStoreAvailable(): boolean {
return glassesTypeSetting.get() !== "rayneo-io";
}
/** Whether launching this package should warn that it won't display correctly on these glasses. */
export function needsDisplayWarning(packageId: string): boolean {
return glassesTypeSetting.get() === "rayneo-io" && !displayWarnedPackages().includes(packageId);
}
/** The user answered the package's display warning: don't show it again. */
export function markDisplayWarningShown(packageId: string): void {
const warned = displayWarnedPackages();
if (warned.includes(packageId)) return;
setStringSetting(DISPLAY_WARNED_KEY, JSON.stringify([...warned, packageId]));
}
function displayWarnedPackages(): string[] {
try {
const parsed: unknown = JSON.parse(getStringSetting(DISPLAY_WARNED_KEY, "[]"));
return Array.isArray(parsed) ? parsed.filter((id): id is string => typeof id === "string") : [];
} catch {
return [];
}
}
+247
View File
@@ -0,0 +1,247 @@
/**
* updateImageRawData the way stock turns an app's imageData into the 4-bit
* bitmap the glasses show. The Even phone app does the conversion
* (EvenHubAppService.updateImageRawData → _prepare / _prepareLegacyRaw, read
* from its blutter decompilation); the glasses firmware then shows the result
* only if it is exactly the container's size (common_image_container_set_data).
*
* - imageData must be a JSON array of integers; a base64 string or an object
* (a typed array that skipped the SDK's toJson) fails as sendFailed.
* - PNG, JPEG, WebP, GIF and BMP are decoded, scaled to cover the container
* (aspect-fill, centre crop, nearest neighbour) and converted to grey with
* alpha ignored.
* - Anything else is raw pixels, accepted only with compressMode 2 (which the
* SDK sends since 0.0.12) and exactly the container's size: one byte per
* pixel (values up to 15 are already 4-bit levels) or 4-bit packed rows.
*
* Native-free so it runs under Node; JPEG/WebP/GIF decoding is injected.
*/
const UPNG = require("upng-js");
/** EhImageUpdateResult, as the SDK reads the host's integer. */
export const IMAGE_SUCCESS = 0;
export const IMAGE_EXCEPTION = 1;
export const IMAGE_SIZE_INVALID = 2;
export const IMAGE_TO_GRAY4_FAILED = 3;
export const IMAGE_SEND_FAILED = 4;
/** Stock image containers are 20..288 wide and 20..144 high. */
export const STOCK_IMAGE_MIN_SIZE = 20;
export const STOCK_IMAGE_MAX_WIDTH = 288;
export const STOCK_IMAGE_MAX_HEIGHT = 144;
/** The compressMode the SDK sends, and the phone app requires for raw pixels. */
const RAW_COMPRESS_MODE = 2;
export function inStockImageRange(width: number, height: number): boolean {
return width >= STOCK_IMAGE_MIN_SIZE && width <= STOCK_IMAGE_MAX_WIDTH &&
height >= STOCK_IMAGE_MIN_SIZE && height <= STOCK_IMAGE_MAX_HEIGHT;
}
export type ImageFormat = "png" | "jpeg" | "webp" | "gif" | "bmp" | "raw";
/** Format by magic bytes, as the phone app's EvenHubImageFormatDetector. */
export function detectImageFormat(b: Uint8Array): ImageFormat {
const at = (i: number, ...values: number[]) => values.every((v, j) => b[i + j] === v);
if (at(0, 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a)) return "png";
if (at(0, 0xff, 0xd8, 0xff)) return "jpeg";
if (at(0, 0x52, 0x49, 0x46, 0x46) && at(8, 0x57, 0x45, 0x42, 0x50)) return "webp";
if (at(0, 0x47, 0x49, 0x46, 0x38, 0x37, 0x61) || at(0, 0x47, 0x49, 0x46, 0x38, 0x39, 0x61)) return "gif";
if (at(0, 0x42, 0x4d)) return "bmp";
return "raw";
}
/** Decoded pixels, RGBA, not premultiplied. */
export type RgbaImage = { width: number; height: number; rgba: Uint8Array };
export type NativeImageDecoder = (bytes: Uint8Array) => RgbaImage | null;
/** 8-bit grey from RGB (alpha ignored), then the nearest of 16 levels. */
export function grayLevel(r: number, g: number, b: number): number {
const g8 = Math.floor((299 * r + 587 * g + 114 * b + 500) / 1000);
return Math.min(15, Math.max(0, Math.floor((g8 * 15 + 127) / 255)));
}
export function rgbaToLevels(image: RgbaImage): Uint8Array {
const levels = new Uint8Array(image.width * image.height);
for (let i = 0; i < levels.length; i++) {
const o = i * 4;
levels[i] = grayLevel(image.rgba[o]!, image.rgba[o + 1]!, image.rgba[o + 2]!);
}
return levels;
}
/**
* Scale to exactly width x height covering it (aspect-fill), cropping the
* overflow equally on both sides, sampling the nearest source pixel centre.
*/
export function resizeRgbaCover(image: RgbaImage, width: number, height: number): RgbaImage {
const scale = Math.max(width / image.width, height / image.height);
const offX = (image.width - width / scale) / 2;
const offY = (image.height - height / scale) / 2;
const rgba = new Uint8Array(width * height * 4);
for (let y = 0; y < height; y++) {
const sy = Math.min(image.height - 1, Math.max(0, Math.floor(offY + (y + 0.5) / scale)));
for (let x = 0; x < width; x++) {
const sx = Math.min(image.width - 1, Math.max(0, Math.floor(offX + (x + 0.5) / scale)));
const from = (sy * image.width + sx) * 4;
rgba.set(image.rgba.subarray(from, from + 4), (y * width + x) * 4);
}
}
return { width, height, rgba };
}
function decodePng(bytes: Uint8Array): RgbaImage | null {
try {
const decoded = UPNG.decode(bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength));
const frames = UPNG.toRGBA8(decoded) as ArrayBuffer[];
if (!frames.length) return null;
return { width: decoded.width, height: decoded.height, rgba: new Uint8Array(frames[0]!) };
} catch {
return null;
}
}
/**
* Uncompressed Windows BMP ('BM', BI_RGB): 1/4/8-bit paletted and 24/32-bit,
* top-down or bottom-up. Null for other variants (the native decoder may still
* read them). EvenChess sends 1-bit BMPs for full-board refreshes.
*/
export function decodeBmp(bytes: Uint8Array): RgbaImage | null {
if (bytes.length < 54 || bytes[0] !== 0x42 || bytes[1] !== 0x4d) return null;
const u16 = (o: number) => bytes[o]! | (bytes[o + 1]! << 8);
const u32 = (o: number) => (bytes[o]! | (bytes[o + 1]! << 8) | (bytes[o + 2]! << 16) | (bytes[o + 3]! << 24)) >>> 0;
const i32 = (o: number) => bytes[o]! | (bytes[o + 1]! << 8) | (bytes[o + 2]! << 16) | (bytes[o + 3]! << 24);
const pixelOffset = u32(10);
const dibSize = u32(14);
const width = i32(18);
const heightRaw = i32(22);
const bitCount = u16(28);
if (u32(30) !== 0) return null; // only BI_RGB
if (width <= 0 || width > 4096) return null;
const topDown = heightRaw < 0;
const height = Math.abs(heightRaw);
if (height === 0 || height > 4096) return null;
if (![1, 4, 8, 24, 32].includes(bitCount)) return null;
// Palette entries are 4 bytes, BGRx.
let palette: Uint8Array | null = null;
if (bitCount <= 8) {
let count = u32(46);
if (count === 0 || count > 1 << bitCount) count = 1 << bitCount;
palette = new Uint8Array(count * 3);
for (let i = 0; i < count; i++) {
const o = 14 + dibSize + i * 4;
if (o + 2 >= bytes.length) break;
palette.set([bytes[o + 2]!, bytes[o + 1]!, bytes[o]!], i * 3);
}
}
const stride = ((width * bitCount + 31) >> 5) << 2;
const rgba = new Uint8Array(width * height * 4);
for (let y = 0; y < height; y++) {
const row = pixelOffset + (topDown ? y : height - 1 - y) * stride;
if (row >= bytes.length) break;
for (let x = 0; x < width; x++) {
let r: number, g: number, b: number;
if (palette) {
let index: number;
if (bitCount === 1) index = ((bytes[row + (x >> 3)] ?? 0) >> (7 - (x & 7))) & 1;
else if (bitCount === 4) index = (x & 1) === 0 ? (bytes[row + (x >> 1)] ?? 0) >> 4 : (bytes[row + (x >> 1)] ?? 0) & 0x0f;
else index = bytes[row + x] ?? 0;
if (index * 3 >= palette.length) index = 0;
[r, g, b] = [palette[index * 3]!, palette[index * 3 + 1]!, palette[index * 3 + 2]!];
} else {
const o = row + x * (bitCount / 8);
[r, g, b] = [bytes[o + 2] ?? 0, bytes[o + 1] ?? 0, bytes[o] ?? 0];
}
rgba.set([r, g, b, 255], (y * width + x) * 4);
}
}
return { width, height, rgba };
}
/** imageData as bytes: a JSON array of integers only (Uint8List.fromList keeps the low 8 bits). */
function imageDataBytes(raw: unknown): Uint8Array | null {
if (raw === undefined || raw === null) return new Uint8Array(0);
if (!Array.isArray(raw)) return null;
const out = new Uint8Array(raw.length);
for (let i = 0; i < raw.length; i++) {
const value: unknown = raw[i];
if (typeof value !== "number" || !Number.isInteger(value)) return null;
out[i] = value & 0xff;
}
return out;
}
export type PreparedImage = {
result: number;
/** 4-bit levels (0..15), row-major, when result is success. */
levels: Uint8Array | null;
width: number;
height: number;
format: ImageFormat | null;
/** Why it failed, for the debug log. */
reason: string;
};
export type ImageRequest = {
imageData: unknown;
compressMode: number;
/** width/height sent with the update; when both are positive they replace the container's. */
requestWidth: number;
requestHeight: number;
/** The image container's size, looked up by id or name; null when none matched. */
geometry: { width: number; height: number } | null;
decodeNative: NativeImageDecoder;
/** Stock pages limit image sizes; Faceclaw's extended layouts don't. */
stockLimits: boolean;
};
/** Convert an update's imageData to 4-bit levels the way the phone app does. */
export function prepareImageUpdate(request: ImageRequest): PreparedImage {
const fail = (result: number, reason: string, format: ImageFormat | null = null): PreparedImage =>
({ result, levels: null, width: 0, height: 0, format, reason });
const bytes = imageDataBytes(request.imageData);
if (!bytes) return fail(IMAGE_SEND_FAILED, "imageData is not an array of integers");
const inRange = (w: number, h: number) => (request.stockLimits ? inStockImageRange(w, h) : w > 0 && h > 0);
const [width, height] = request.requestWidth > 0 && request.requestHeight > 0
? [request.requestWidth, request.requestHeight]
: [request.geometry?.width ?? 0, request.geometry?.height ?? 0];
const format = detectImageFormat(bytes);
if (format === "raw") {
if (request.stockLimits && request.compressMode !== RAW_COMPRESS_MODE) {
return fail(IMAGE_EXCEPTION, `raw pixels need compressMode ${RAW_COMPRESS_MODE}`, format);
}
if (!inRange(width, height)) return fail(IMAGE_SIZE_INVALID, `raw size ${width}x${height} out of range`, format);
const levels = new Uint8Array(width * height);
const packedStride = Math.ceil(width / 2);
if (bytes.length === width * height) {
for (let i = 0; i < levels.length; i++) {
const v = bytes[i]!;
levels[i] = v <= 15 ? v : Math.floor((v * 15 + 127) / 255);
}
} else if (bytes.length === packedStride * height) {
for (let y = 0; y < height; y++) {
for (let x = 0; x < width; x++) {
const byte = bytes[y * packedStride + (x >> 1)]!;
levels[y * width + x] = (x & 1) === 0 ? byte >> 4 : byte & 0x0f;
}
}
} else {
return fail(IMAGE_EXCEPTION, `raw length ${bytes.length} fits neither ${width}x${height} 8-bit nor 4-bit`, format);
}
return { result: IMAGE_SUCCESS, levels, width, height, format, reason: "" };
}
let decoded = format === "png" ? decodePng(bytes) : format === "bmp" ? decodeBmp(bytes) : null;
decoded ??= request.decodeNative(bytes);
if (!decoded || decoded.width <= 0 || decoded.height <= 0) return fail(IMAGE_EXCEPTION, `${format} decode failed`, format);
if (inRange(width, height) && (decoded.width !== width || decoded.height !== height)) {
decoded = resizeRgbaCover(decoded, width, height);
}
if (!inRange(decoded.width, decoded.height)) {
return fail(IMAGE_SIZE_INVALID, `${decoded.width}x${decoded.height} out of range`, format);
}
return { result: IMAGE_SUCCESS, levels: rgbaToLevels(decoded), width: decoded.width, height: decoded.height, format, reason: "" };
}
+14 -1
View File
@@ -5,6 +5,9 @@
* a phone WebView (its runtime + phone UI) and a 576x288 glasses window
* composited by Faceclaw instead of the stock firmware.
*
* With a RayNeo iO paired the store is unavailable and sideloaded apps warn
* on first run that they won't display correctly; see glasses-compat.ts.
*
* Design notes: notes/evenhub_compatibility.txt.
*/
import { type AppContext, type AppDefinition } from "../app-definition";
@@ -15,6 +18,7 @@ import { createInProcessWindow, type InProcessWindow } from "../../ui/shell/in-p
import { renderEvenRealitiesLogo } from "../../graphics/even-realities-logo";
import { makeImageWindowIcon, windowIcon } from "../../ui/shell/chrome-layer";
import { type InstalledEvenHubApp } from "./installed-apps";
import { isEvenHubStoreAvailable } from "./glasses-compat";
const EVENHUB_STORE_WINDOW_ID = "evenhub:store";
const EVENHUB_STORE_SURFACE_ID = "window:evenhub:store";
@@ -104,8 +108,17 @@ const evenhubApp: AppDefinition = {
title: "EvenHub",
icon: "package",
renderIcon: renderEvenRealitiesLogo,
// Off the launcher with a RayNeo iO paired. A store window that opens
// anyway (restored from before a re-pair, or the launcher's route for an
// installed app whose package is missing) says why instead of loading.
isSupported: isEvenHubStoreAvailable,
launch: async (ctx) => {
await ensureStoreWindow(ctx);
const { window } = await ensureStoreWindow(ctx);
if (!isEvenHubStoreAvailable()) {
// Store pages opened while Even glasses were paired would cover the notice.
window.stack.clearToBase();
window.requestRender();
}
},
};
+181
View File
@@ -0,0 +1,181 @@
/**
* Debug-log descriptions of EvenHub page layouts and container updates.
* session.ts sends them to logcat at debug level (tag FaceclawEvenHubLayout).
*
* "note:" lines point out page quirks that change what stock (and so we) do
* with it: shared ids, missing or extra capture containers, a wrong
* containerTotalNum.
*/
import {
paintOrder,
readOptionalNumber,
type EvenHubContainer,
type EvenHubListContainer,
type EvenHubPage,
type EvenHubTextContainer,
} from "./containers";
import type { PreparedImage } from "./image-pipeline";
const CONTENT_PREVIEW_CHARS = 40;
const RAW_STRING_CHARS = 120;
const RAW_ARRAY_ITEMS = 32;
function label(c: EvenHubContainer): string {
return `${c.kind}#${c.id} ${JSON.stringify(c.name)}`;
}
function preview(text: string): string {
return JSON.stringify(text.length > CONTENT_PREVIEW_CHARS ? `${text.slice(0, CONTENT_PREVIEW_CHARS)}…` : text);
}
function decoration(c: EvenHubTextContainer | EvenHubListContainer): string {
let out = "";
if (c.borderWidth > 0) out += ` border=${c.borderWidth}c${c.borderColor}${c.borderRadius > 0 ? `r${c.borderRadius}` : ""}`;
if (c.paddingLength > 0) out += ` pad=${c.paddingLength}`;
if (c.isEventCapture) out += " capture";
if (c.font) out += " font";
return out;
}
function describeContainer(c: EvenHubContainer): string {
const z = c.zOrderIndex === undefined ? "" : ` z=${c.zOrderIndex}`;
const head = `${label(c)} @${c.x},${c.y} ${c.width}x${c.height}${z}`;
switch (c.kind) {
case "image":
return `${head} ${c.pixels ? `pixels=${c.pixelsWidth}x${c.pixelsHeight}` : "no pixels yet"}`;
case "text": {
const color = c.textColor === undefined ? "" : ` color=${c.textColor}`;
return `${head}${decoration(c)}${color} len=${c.content.length} ${preview(c.content)}`;
}
case "list":
return `${head}${decoration(c)} items=${c.itemNames.length} itemWidth=${c.itemWidth}${c.selectBorder ? " selectBorder" : ""} selected=${c.selectedIndex}`;
}
}
/** Quirks of a page that change how stock treats it. */
export function pageNotes(page: EvenHubPage, declaredTotal?: number): string[] {
const notes: string[] = [];
const ordered = paintOrder(page.containers);
const byId = new Map<number, EvenHubContainer[]>();
for (const c of ordered) byId.set(c.id, [...(byId.get(c.id) ?? []), c]);
byId.forEach((shared, id) => {
if (shared.length > 1) {
notes.push(`id ${id} is shared by ${shared.map(label).join(", ")}; updates and events resolve to ${label(shared[0]!)} only`);
}
});
const captures = ordered.filter((c) => c.kind !== "image" && c.isEventCapture);
if (captures.length === 0) notes.push("no event-capture container, so the app gets no input");
else if (captures.length > 1) notes.push(`${captures.length} event-capture containers; only the first, ${label(captures[0]!)}, gets input`);
if (declaredTotal !== undefined && declaredTotal !== page.containers.length) {
notes.push(`containerTotalNum is ${declaredTotal} but ${page.containers.length} containers were sent (nothing checks it)`);
}
return notes;
}
/** The payload, as compact JSON with long strings and arrays cut short. */
function rawJson(data: Record<string, unknown>): string {
try {
return JSON.stringify(data, (_key, value: unknown) => {
if (typeof value === "string" && value.length > RAW_STRING_CHARS) {
return `${value.slice(0, RAW_STRING_CHARS)}…(${value.length} chars)`;
}
if (Array.isArray(value) && value.length > RAW_ARRAY_ITEMS) {
return [...value.slice(0, RAW_ARRAY_ITEMS), `…(${value.length} items)`];
}
return value;
});
} catch (error) {
return `(unserializable: ${String(error)})`;
}
}
/** A create/rebuild/extension layout: one line per container in paint order, then notes, then the raw payload. */
export function describePage(method: string, page: EvenHubPage, data: Record<string, unknown>): string[] {
const containers = page.containers;
const count = (kind: EvenHubContainer["kind"]) => containers.filter((c) => c.kind === kind).length;
const declaredTotal = readOptionalNumber(data, "containerTotalNum");
const total = declaredTotal === undefined ? "" : `, containerTotalNum=${declaredTotal}`;
const menu = page.menuItems.length > 0 ? `, ${page.menuItems.length} menu items` : "";
const lines = [
`${method}: ${containers.length} containers (${count("list")} list, ${count("text")} text, ${count("image")} image)` +
`${total}${menu}; painted bottom to top:`,
];
paintOrder(containers).forEach((c, i) => lines.push(` ${i}: ${describeContainer(c)}`));
for (const note of pageNotes(page, declaredTotal)) lines.push(` note: ${note}`);
lines.push(` raw: ${rawJson(data)}`);
return lines;
}
function payloadKind(raw: unknown): string {
if (raw === undefined || raw === null) return "missing";
if (Array.isArray(raw)) return `number[${raw.length}]`;
if (typeof raw === "string") return `string(${raw.length} chars)`;
if (typeof raw === "object") return `object(${Object.keys(raw).length} keys)`;
return typeof raw;
}
/** Lit-pixel count and bounding box of 4-bit levels, so a blank "hide this" image is recognizable. */
function litSummary(levels: Uint8Array, width: number): string {
let lit = 0;
let max = 0;
let x0 = width;
let y0 = Infinity;
let x1 = -1;
let y1 = -1;
for (let i = 0; i < levels.length; i++) {
const level = levels[i]!;
if (level === 0) continue;
lit++;
if (level > max) max = level;
const x = i % width;
const y = Math.floor(i / width);
if (x < x0) x0 = x;
if (x > x1) x1 = x;
if (y < y0) y0 = y;
if (y > y1) y1 = y;
}
return lit === 0 ? "blank" : `lit=${lit}px max=${max}/15 bbox=${x0},${y0}-${x1},${y1}`;
}
export type ImageUpdateDebug = {
sentId: number;
sentName: string;
raw: unknown;
/** The container the id resolves to. */
target: EvenHubContainer | undefined;
prepared: PreparedImage;
/** Whether the container now shows the update. */
shown: boolean;
/** EhImageUpdateResult returned to the app. */
result: number;
};
export function describeImageUpdate(u: ImageUpdateDebug): string[] {
const parts = [`updateImageRawData id=${u.sentId} ${JSON.stringify(u.sentName)}`];
parts.push(`-> ${u.target ? label(u.target) : "no container"}`);
parts.push(`payload=${payloadKind(u.raw)}`);
if (u.prepared.format) parts.push(`format=${u.prepared.format}`);
if (u.prepared.levels) parts.push(`${u.prepared.width}x${u.prepared.height} ${litSummary(u.prepared.levels, u.prepared.width)}`);
parts.push(`result=${u.result}`, u.shown ? "shown" : "not shown");
const lines = [parts.join(" ")];
if (u.prepared.reason) lines.push(` ${u.prepared.reason}`);
if (u.prepared.result === 0 && u.target?.kind !== "image") {
lines.push(` the id ${u.target ? `resolves to ${label(u.target)}` : "matches no container"}, so the glasses refuse the transfer`);
} else if (u.prepared.result === 0 && !u.shown && u.target) {
lines.push(` ${u.prepared.width}x${u.prepared.height} isn't the container's ${u.target.width}x${u.target.height}, so the glasses drop it`);
}
return lines;
}
export function describeTextUpgrade(
sentId: number,
sentName: string,
container: EvenHubTextContainer | null,
offset: number | undefined,
sentLength: number,
): string {
if (!container) return `textContainerUpgrade id=${sentId} ${JSON.stringify(sentName)}: no container with that id`;
const at = offset === undefined ? "" : ` offset=${offset}`;
const color = container.textColor === undefined ? "" : ` color=${container.textColor}`;
return `textContainerUpgrade ${label(container)}${at} sent=${sentLength} chars -> len=${container.content.length}${color} ${preview(container.content)}`;
}
+141
View File
@@ -0,0 +1,141 @@
/**
* The checks the Even phone app applies to createStartUpPageContainer,
* rebuildPageContainer and textContainerUpgrade payloads before it sends
* anything to the glasses (read from its blutter decompilation:
* EvenHubProtoJsonMapper.validateTextBrightness, mapProtoJsonKeys and
* protobuf.dart's tag-keyed JSON parsing):
*
* - every key must be one the message defines (a numeric key passes as a raw
* protobuf tag, which we then ignore);
* - numeric fields take an integer or a numeric string — not a boolean, a
* fraction or null; string fields take a string; repeated fields an array;
* - any key named textColor, anywhere, must be an integer from 0 to 4.
*
* A failure means the call fails without reaching the glasses: create
* returns invalid (1), rebuild and textContainerUpgrade return false.
* Faceclaw's extended layouts are not checked; they carry their own keys.
*/
type FieldType = "uint" | "string" | "any" | { list: FieldType } | { message: Schema };
type Schema = Readonly<Record<string, FieldType>>;
const BOX: Schema = {
xPosition: "uint",
yPosition: "uint",
width: "uint",
height: "uint",
containerID: "uint",
containerName: "string",
zOrderIndex: "uint",
};
const DECORATED: Schema = {
...BOX,
borderWidth: "uint",
borderColor: "uint",
borderRadius: "uint",
paddingLength: "uint",
isEventCapture: "uint",
};
const TEXT: Schema = { ...DECORATED, content: "string", textColor: "uint" };
const ITEM_CONTAINER: Schema = {
itemCount: "uint",
itemWidth: "uint",
isItemSelectBorderEn: "uint",
itemName: { list: "string" },
};
const LIST: Schema = { ...DECORATED, itemContainer: { message: ITEM_CONTAINER } };
// The app knows position, subItems and checkedIndex (second-level menus, not
// yet in the SDK); their value types are unconfirmed, so they aren't checked.
const MENU_ITEM: Schema = { itemName: "string", itemID: "uint", position: "any", subItems: "any", checkedIndex: "any" };
const MENU: Schema = { menuItems: { list: { message: MENU_ITEM } } };
const REBUILD: Schema = {
containerTotalNum: "uint",
listObject: { list: { message: LIST } },
textObject: { list: { message: TEXT } },
imageObject: { list: { message: BOX } },
menuObject: { message: MENU },
};
const CREATE: Schema = { ...REBUILD, widgetId: "uint" };
const TEXT_UPGRADE: Schema = {
containerID: "uint",
containerName: "string",
contentOffset: "uint",
contentLength: "uint",
content: "string",
textColor: "uint",
};
const SCHEMAS = { create: CREATE, rebuild: REBUILD, textUpgrade: TEXT_UPGRADE } as const;
/** What Dart's int.parse accepts. */
const INT_STRING = /^\s*[+-]?(?:\d+|0[xX][0-9a-fA-F]+)\s*$/;
function isRecord(value: unknown): value is Record<string, unknown> {
return !!value && typeof value === "object" && !Array.isArray(value);
}
function checkValue(type: FieldType, value: unknown, path: string): string | null {
if (type === "any") return null;
if (value === null || value === undefined) return `${path} is null`;
if (type === "uint") {
if (typeof value === "number" ? Number.isInteger(value) : typeof value === "string" && INT_STRING.test(value)) return null;
return `${path} must be an integer, not ${JSON.stringify(value)}`;
}
if (type === "string") return typeof value === "string" ? null : `${path} must be a string, not ${JSON.stringify(value)}`;
if ("list" in type) {
if (!Array.isArray(value)) return `${path} must be an array`;
for (let i = 0; i < value.length; i++) {
const error = checkValue(type.list, value[i], `${path}[${i}]`);
if (error) return error;
}
return null;
}
return isRecord(value) ? checkMessage(type.message, value, path) : `${path} must be an object`;
}
function checkMessage(schema: Schema, message: Record<string, unknown>, path: string): string | null {
for (const [key, value] of Object.entries(message)) {
const field = schema[key];
if (!field) {
if (INT_STRING.test(key)) continue;
return `unknown key ${path ? `${path}.` : ""}${key}`;
}
const error = checkValue(field, value, path ? `${path}.${key}` : key);
if (error) return error;
}
return null;
}
/** validateTextBrightness: every textColor key, at any depth, holds an integer 0..4. */
function checkTextColors(value: unknown, path: string): string | null {
if (Array.isArray(value)) {
for (let i = 0; i < value.length; i++) {
const error = checkTextColors(value[i], `${path}[${i}]`);
if (error) return error;
}
} else if (isRecord(value)) {
for (const [key, child] of Object.entries(value)) {
const at = path ? `${path}.${key}` : key;
if (key === "textColor" && !(typeof child === "number" && Number.isInteger(child) && child >= 0 && child <= 4)) {
return `${at} must be an integer from 0 to 4, not ${JSON.stringify(child)}`;
}
const error = checkTextColors(child, at);
if (error) return error;
}
}
return null;
}
/** Why the Even app would refuse this payload without sending it, or null if it would send it. */
export function stockPayloadError(kind: keyof typeof SCHEMAS, data: Record<string, unknown>): string | null {
return checkTextColors(data, "") ?? checkMessage(SCHEMAS[kind], data, "");
}
+31 -152
View File
@@ -1,17 +1,11 @@
import { getDefaultSmallFont } from "../../graphics/ui-fonts";
import { GrayImage } from "../../graphics/image";
import { truncateText, wrapText } from "../../graphics/textwrap";
import { type InputEvent } from "../../ui/gestures";
import { Layer, type LayerContext, type PaintBelow } from "../../ui/layers";
import { drawListScrollbar, drawSelectionHighlight } from "../../ui/menu";
import { type LayerContext } from "../../ui/layers";
import { permissionDetail, permissionLabel, type EvenHubPermission } from "./permissions";
import { openPrivacyPolicyOnPhone } from "./privacy-policy";
import { lineStep, listRowHeight } from "../../ui/metrics";
import { lineStep } from "../../ui/metrics";
import { ScrollingDialogLayer, type DialogBody, type DialogRow } from "./scrolling-dialog";
/** Minimum gap between the dialog and the edges of the window it's centered in. */
const DIALOG_MARGIN = 8;
const DIALOG_WIDTH = 320;
const PADDING = 10;
const PERM_GAP = 4;
/**
@@ -20,56 +14,26 @@ const PERM_GAP = 4;
* Cancel (or double-click) backs out. A privacy-policy-only app still reaches
* this dialog so the user can review the policy before allowing first run.
*
* When the list overflows the viewport the dialog scrolls: scroll events move
* through the content row by row, and only once the bottom is fully visible do
* further scrolls move the action selection. The actions are inert until then
* (a click just scrolls), so the user can't Allow without having had the whole
* list on screen.
* A long list scrolls a page at a time, and Allow stays inert until the
* bottom has been reached (see ScrollingDialogLayer), so the user can't
* Allow without having had the whole list on screen.
*/
export class EvenHubPermissionDialogLayer implements Layer {
/** Floats in front of the window content, like the context menus. */
readonly depth = 2;
/** Starts on Allow; the optional privacy-policy action is inserted after it. */
private selectedIndex = 0;
/** First content row drawn; rows above it are scrolled off. */
private firstRow = 0;
/** Scroll position at which the last row is fully visible; set by paint. */
private maxFirstRow = 0;
private resolved = false;
export class EvenHubPermissionDialogLayer extends ScrollingDialogLayer {
constructor(
private readonly appName: string,
private readonly permissions: EvenHubPermission[],
private readonly privacyPolicyUrl: string,
private readonly onConfirm: () => void,
private readonly onCancel: () => void,
) {}
/** Whether the dialog is scrolled to the bottom, making the actions live. */
private atBottom(): boolean {
return this.firstRow >= this.maxFirstRow;
) {
super(DIALOG_WIDTH);
}
paint(ctx: LayerContext, paintBelow: PaintBelow): GrayImage {
const font = getDefaultSmallFont();
const { width: viewportWidth, height: viewportHeight } = ctx.stack.getBaseSize();
const image = paintBelow();
const dialogWidth = Math.min(DIALOG_WIDTH, viewportWidth - 2 * DIALOG_MARGIN);
const dialogX = ((viewportWidth - dialogWidth) / 2) | 0;
const textWidth = dialogWidth - 2 * PADDING - 4;
const left = dialogX + PADDING + 2;
protected bodyRows({ image, font, left, textWidth }: DialogBody): DialogRow[] {
const headerStep = lineStep(font) + 2;
const permLabelStep = lineStep(font) + 1;
const permDetailStep = lineStep(font) - 1;
const actionRowH = listRowHeight(font);
const actions = this.actions();
const focused = ctx.stack.isFocused();
// The dialog body as scrollable rows; each row's height includes the gap
// below it, so scrolling and visibility checks work on whole rows.
type Row = { height: number; draw: (top: number) => void };
const rows: Row[] = [];
const rows: DialogRow[] = [];
const heading = this.permissions.length ? `${this.appName} needs:` : `${this.appName} is ready to run`;
rows.push({
height: headerStep + 6,
@@ -98,113 +62,28 @@ export class EvenHubPermissionDialogLayer implements Layer {
});
}
}
for (let index = 0; index < actions.length; index++) {
rows.push({
height: actionRowH,
draw: (top) => {
const selected = this.atBottom() && index === this.selectedIndex;
if (selected) {
drawSelectionHighlight(image, dialogX + 12, top, dialogWidth - 24, actionRowH - 1, focused, 8);
}
image.drawText(font, dialogX + 22, top + 3, actions[index]!, selected ? 255 : 200);
},
});
}
const contentHeight = rows.reduce((sum, row) => sum + row.height, 0);
const height = Math.min(contentHeight + 2 * PADDING, viewportHeight - 2 * DIALOG_MARGIN);
const dialogY = ((viewportHeight - height) / 2) | 0;
const visibleHeight = height - 2 * PADDING;
// The bottommost scroll position: the largest suffix of rows that fits.
let maxFirstRow = rows.length;
for (let suffixHeight = 0; maxFirstRow > 0; maxFirstRow--) {
const rowHeight = rows[maxFirstRow - 1]!.height;
if (suffixHeight + rowHeight > visibleHeight) break;
suffixHeight += rowHeight;
}
this.maxFirstRow = maxFirstRow;
this.firstRow = Math.max(0, Math.min(this.firstRow, maxFirstRow));
// Fill 1 (transparent color key is 0), outline for the dialog edge.
image.fillRoundedRect(dialogX, dialogY, dialogWidth, height, 1);
image.drawRoundedRect(dialogX, dialogY, dialogWidth, height, 72);
let y = dialogY + PADDING;
for (let index = this.firstRow; index < rows.length; index++) {
const row = rows[index]!;
if (y + row.height > dialogY + PADDING + visibleHeight) break;
row.draw(y);
y += row.height;
}
if (maxFirstRow > 0) {
drawListScrollbar(
image,
dialogX + dialogWidth - 7,
dialogY + PADDING,
visibleHeight,
this.firstRow,
rows.length - maxFirstRow,
rows.length,
);
}
return image;
return rows;
}
handleInput(event: InputEvent, ctx: LayerContext): void {
const actions = this.actions();
switch (event.type) {
case "scroll-up":
if (this.atBottom() && this.selectedIndex > 0) {
this.selectedIndex -= 1;
} else if (this.firstRow > 0) {
this.firstRow -= 1;
}
return;
case "scroll-down":
if (!this.atBottom()) {
this.firstRow += 1;
} else {
this.selectedIndex = Math.min(actions.length - 1, this.selectedIndex + 1);
}
return;
case "click":
if (!this.atBottom()) {
this.firstRow += 1;
return;
}
if (actions[this.selectedIndex] === "Privacy policy") {
openPrivacyPolicyOnPhone(this.privacyPolicyUrl, this.appName);
} else {
this.resolve(ctx, actions[this.selectedIndex] === "Allow");
}
return;
case "double-click":
this.resolve(ctx, false);
return;
default:
return;
}
}
onRemoved(): void {
// Dismissed some other way (window closed, back button): treat as cancel.
if (!this.resolved) {
this.resolved = true;
this.onCancel();
}
}
private resolve(ctx: LayerContext, confirmed: boolean): void {
if (this.resolved) return;
this.resolved = true;
ctx.stack.pop();
if (confirmed) this.onConfirm();
else this.onCancel();
}
private actions(): string[] {
protected actions(): string[] {
return this.privacyPolicyUrl ? ["Allow", "Privacy policy", "Cancel"] : ["Allow", "Cancel"];
}
protected activate(action: string, ctx: LayerContext): void {
if (action === "Privacy policy") {
openPrivacyPolicyOnPhone(this.privacyPolicyUrl, this.appName);
} else if (action === "Allow") {
this.close(ctx, () => this.onConfirm());
} else {
this.close(ctx, () => this.onCancel());
}
}
protected backedOut(): void {
this.onCancel();
}
protected abandoned(): void {
this.onCancel();
}
}
+201
View File
@@ -0,0 +1,201 @@
import { getDefaultSmallFont } from "../../graphics/ui-fonts";
import { type GrayImage, type UiFont } from "../../graphics/image";
import { type InputEvent } from "../../ui/gestures";
import { type Layer, type LayerContext, type PaintBelow } from "../../ui/layers";
import { drawListScrollbar, drawSelectionHighlight } from "../../ui/menu";
import { listRowHeight } from "../../ui/metrics";
/** Minimum gap between the dialog and the edges of the window it's centered in. */
const DIALOG_MARGIN = 8;
const PADDING = 10;
/** One row of a dialog's content. Its height includes the gap below it, so scrolling works on whole rows. */
export type DialogRow = { height: number; draw: (top: number) => void };
/** What a dialog's body rows draw with: the canvas, the font, and the text column. */
export type DialogBody = { image: GrayImage; font: UiFont; left: number; textWidth: number };
/**
* A dialog box centered over the window it opens in: body rows, then a
* column of actions, starting on the first. EvenHub's confirmations and
* warnings (permissions, the RayNeo iO display warning) are built on it.
*
* When the content overflows the viewport the dialog scrolls a page at a
* time, keeping one line of overlap (the last line of one page is the first
* of the next), and only once the bottom is fully visible do further scrolls
* move the action selection. The actions are inert until then (a click just
* pages down), so nothing can be chosen without the whole dialog having been
* on screen. Double-click backs out.
*/
export abstract class ScrollingDialogLayer implements Layer {
/** Floats in front of the window content, like the context menus. */
readonly depth = 2;
private selectedIndex = 0;
/** First content row drawn; rows above it are scrolled off. */
private firstRow = 0;
/** Scroll position at which the last row is fully visible; set by paint. */
private maxFirstRow = 0;
/** Heights of the content rows and of the viewport they scroll in, as last painted (for paging). */
private rowHeights: number[] = [];
private visibleHeight = 0;
private resolved = false;
constructor(private readonly maxWidth: number) {}
/** The rows above the actions. */
protected abstract bodyRows(body: DialogBody): DialogRow[];
/** The action labels, top to bottom. */
protected abstract actions(): string[];
/** A click on an action once the actions are live; close() dismisses the dialog with it. */
protected abstract activate(action: string, ctx: LayerContext): void;
/** Double-click: the user backed out of the dialog. */
protected abstract backedOut(): void;
/** The dialog left the stack unanswered (its window closed). */
protected abstract abandoned(): void;
/** Whether the dialog is scrolled to the bottom, making the actions live. */
private atBottom(): boolean {
return this.firstRow >= this.maxFirstRow;
}
paint(ctx: LayerContext, paintBelow: PaintBelow): GrayImage {
const font = getDefaultSmallFont();
const { width: viewportWidth, height: viewportHeight } = ctx.stack.getBaseSize();
const image = paintBelow();
const dialogWidth = Math.min(this.maxWidth, viewportWidth - 2 * DIALOG_MARGIN);
const dialogX = ((viewportWidth - dialogWidth) / 2) | 0;
const textWidth = dialogWidth - 2 * PADDING - 4;
const left = dialogX + PADDING + 2;
const actionRowH = listRowHeight(font);
const actions = this.actions();
const focused = ctx.stack.isFocused();
const rows = this.bodyRows({ image, font, left, textWidth });
for (let index = 0; index < actions.length; index++) {
rows.push({
height: actionRowH,
draw: (top) => {
const selected = this.atBottom() && index === this.selectedIndex;
if (selected) {
drawSelectionHighlight(image, dialogX + 12, top, dialogWidth - 24, actionRowH - 1, focused, 8);
}
image.drawText(font, dialogX + 22, top + 3, actions[index]!, selected ? 255 : 200);
},
});
}
const contentHeight = rows.reduce((sum, row) => sum + row.height, 0);
const height = Math.min(contentHeight + 2 * PADDING, viewportHeight - 2 * DIALOG_MARGIN);
const dialogY = ((viewportHeight - height) / 2) | 0;
const visibleHeight = height - 2 * PADDING;
// The bottommost scroll position: the largest suffix of rows that fits.
let maxFirstRow = rows.length;
for (let suffixHeight = 0; maxFirstRow > 0; maxFirstRow--) {
const rowHeight = rows[maxFirstRow - 1]!.height;
if (suffixHeight + rowHeight > visibleHeight) break;
suffixHeight += rowHeight;
}
this.maxFirstRow = maxFirstRow;
this.firstRow = Math.max(0, Math.min(this.firstRow, maxFirstRow));
this.rowHeights = rows.map((row) => row.height);
this.visibleHeight = visibleHeight;
// Fill 1 (transparent color key is 0), outline for the dialog edge.
image.fillRoundedRect(dialogX, dialogY, dialogWidth, height, 1);
image.drawRoundedRect(dialogX, dialogY, dialogWidth, height, 72);
let y = dialogY + PADDING;
for (let index = this.firstRow; index < rows.length; index++) {
const row = rows[index]!;
if (y + row.height > dialogY + PADDING + visibleHeight) break;
row.draw(y);
y += row.height;
}
if (maxFirstRow > 0) {
drawListScrollbar(
image,
dialogX + dialogWidth - 7,
dialogY + PADDING,
visibleHeight,
this.firstRow,
rows.length - maxFirstRow,
rows.length,
);
}
return image;
}
handleInput(event: InputEvent, ctx: LayerContext): void {
const actions = this.actions();
switch (event.type) {
case "scroll-up":
if (this.atBottom() && this.selectedIndex > 0) {
this.selectedIndex -= 1;
} else if (this.firstRow > 0) {
this.pageUp();
}
return;
case "scroll-down":
if (!this.atBottom()) {
this.pageDown();
} else {
this.selectedIndex = Math.min(actions.length - 1, this.selectedIndex + 1);
}
return;
case "click":
if (!this.atBottom()) {
this.pageDown();
return;
}
this.activate(actions[this.selectedIndex]!, ctx);
return;
case "double-click":
this.close(ctx, () => this.backedOut());
return;
default:
return;
}
}
/** Scroll so the last fully visible row becomes the first (one row of overlap). */
private pageDown(): void {
let y = 0;
let lastVisible = this.firstRow;
for (let index = this.firstRow; index < this.rowHeights.length; index++) {
y += this.rowHeights[index]!;
if (y > this.visibleHeight) break;
lastVisible = index;
}
this.firstRow = Math.min(this.maxFirstRow, Math.max(this.firstRow + 1, lastVisible));
}
/** Scroll so the current first row becomes the last fully visible one (one row of overlap). */
private pageUp(): void {
let first = this.firstRow;
let y = this.rowHeights[first] ?? 0;
while (first > 0 && y + this.rowHeights[first - 1]! <= this.visibleHeight) {
first -= 1;
y += this.rowHeights[first]!;
}
this.firstRow = Math.max(0, Math.min(this.firstRow - 1, first));
}
onRemoved(): void {
// Dismissed some other way (window closed, back button).
if (!this.resolved) {
this.resolved = true;
this.abandoned();
}
}
/** Take the dialog down, then run the answer's effect (once, however it was answered). */
protected close(ctx: LayerContext, then: () => void): void {
if (this.resolved) return;
this.resolved = true;
ctx.stack.pop();
then();
}
}
+191 -219
View File
@@ -38,7 +38,11 @@ import {
import { EvenHubApiKeyDialogLayer } from "./api-key-dialog";
import {
asRecord,
clampToStockLimits,
containerById,
declaredMenuItems,
PAGE_CREATE_INVALID,
stockPageFailure,
eventCaptureContainer,
parsePage,
readNumber,
@@ -50,8 +54,13 @@ import {
type EvenHubContainer,
type EvenHubMenuItem,
type EvenHubPage,
type EvenHubTextContainer,
} from "./containers";
import { compositePage } from "./compositor";
import { compositePage, layoutText } from "./compositor";
import { describeImageUpdate, describePage, describeTextUpgrade } from "./layout-debug";
import { IMAGE_SEND_FAILED, IMAGE_SUCCESS, prepareImageUpdate } from "./image-pipeline";
import { stockPayloadError } from "./payload-validation";
import { decodeImageRgba } from "../../native/image-rgba";
import { stepListSelection } from "./list-menu";
import { type EvenHubManifest } from "./ehpk";
import { permissionsIncludeMicrophone, permissionsInclude } from "./permissions";
@@ -74,7 +83,15 @@ import { parseWindowIcon, WindowIconRenderer } from "./window-icon";
import { availableFontFamilies, containerFace, measureContainerText } from "./fonts";
import { parseFontSpec } from "./font-spec";
const UPNG = require("upng-js");
/**
* Page layouts and container updates go to logcat at debug level (see
* layout-debug.ts): `adb logcat -s FaceclawEvenHubLayout:D`. Android only.
*/
const LAYOUT_LOG_TAG = "FaceclawEvenHubLayout";
const LAYOUT_LOG_ENABLED = !!global.isAndroid;
/** Logcat truncates entries around 4 KB; split longer lines below that. */
const LOGCAT_CHUNK_CHARS = 3000;
/** OsEventTypeList values (PB ordinals). */
const CLICK_EVENT = 0;
@@ -83,12 +100,20 @@ const SCROLL_BOTTOM_EVENT = 2;
const DOUBLE_CLICK_EVENT = 3;
const FOREGROUND_ENTER_EVENT = 4;
const FOREGROUND_EXIT_EVENT = 5;
const ABNORMAL_EXIT_EVENT = 6;
const SYSTEM_EXIT_EVENT = 7;
const IMU_DATA_REPORT_EVENT = 8;
/** SDK 0.0.14: the press gesture, delivered like any other list/text/sys event. */
const LONG_PRESS_EVENT = 9;
const LONG_PRESS_RELEASE_EVENT = 10;
/** After a failed rebuild, how long the app gets to hear ABNORMAL_EXIT before it is closed. */
const ABNORMAL_EXIT_CLOSE_DELAY_MS = 200;
/** Stock text scrolling: one step per swipe, and "at an end" within this tolerance. */
const TEXT_SCROLL_STEP_PX = 28;
const TEXT_SCROLL_EDGE_PX = 3;
/**
* Injected into every served HTML document at document start (before any app
* JS): (1) the promise-returning flutter_inappwebview.callHandler the EvenHub
@@ -416,6 +441,8 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH
private page: EvenHubPage | null = null;
private pageCreated = false;
/** The page came from Faceclaw's extended layout API, which isn't held to stock limits. */
private extendedLayout = false;
private webViewHandle: EvenHubWebViewHandle | null = null;
private windowHooks: EvenHubWindowHooks | null = null;
private closed = false;
@@ -596,6 +623,8 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH
const eventType = event.type === "scroll-up" ? SCROLL_TOP_EVENT : SCROLL_BOTTOM_EVENT;
if (capture?.kind === "list") {
this.scrollList(capture, eventType, inputSource);
} else if (capture?.kind === "text") {
this.scrollText(capture, eventType, inputSource);
} else {
this.emitSysEvent(eventType, 0, inputSource);
}
@@ -620,6 +649,25 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH
this.pushMessage("evenHubEvent", { type: "menuItemClickEvent", jsonData: { itemID } });
}
/**
* Stock scrolls a capture text container that overflows by itself, a 28px
* step per swipe, and tells the app only at the ends (a textEvent naming the
* container). Text that fits reports every swipe as reaching an end.
*/
private scrollText(text: EvenHubTextContainer, eventType: number, inputSource?: ExtensionInputSource): void {
const { maxScroll } = layoutText(text);
const forward = eventType === SCROLL_BOTTOM_EVENT;
const atEnd = forward ? maxScroll - text.scrollY < TEXT_SCROLL_EDGE_PX : text.scrollY < TEXT_SCROLL_EDGE_PX;
if (maxScroll === 0 || atEnd) {
this.emitTextEvent(text, eventType, inputSource);
return;
}
text.scrollY = forward
? Math.min(maxScroll, text.scrollY + TEXT_SCROLL_STEP_PX)
: Math.max(0, text.scrollY - TEXT_SCROLL_STEP_PX);
this.windowHooks?.requestRender();
}
private scrollList(list: EvenHubListContainer, eventType: number, inputSource?: ExtensionInputSource): void {
// Swipe up selects the previous item, swipe down the next (stock direction).
const delta = eventType === SCROLL_TOP_EVENT ? -1 : 1;
@@ -808,7 +856,8 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH
deliverImu(reading: ImuReading): void {
this.pushEvenHubEvent("sysEvent", {
eventType: IMU_DATA_REPORT_EVENT,
imuData: { x: reading.x, y: reading.y, z: reading.z },
// The Even app's field name (protobuf IMU_Data); the SDK reads it as imuData.
iMUData: { x: reading.x, y: reading.y, z: reading.z },
});
}
@@ -858,7 +907,8 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH
const first = args[0];
const message = asRecord(typeof first === "string" ? JSON.parse(first) : first);
const method = readString(message, "method", "");
const data = asRecord(message.data);
// As the Even app: `data`, else `payload`.
const data = asRecord(message.data ?? message.payload);
result = await this.dispatch(method, data);
} catch (error) {
this.log(`evenhub bridge call failed: ${error}`);
@@ -916,13 +966,29 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH
private async createStartUpPage(data: Record<string, unknown>): Promise<number> {
if (this.pageCreated) {
// One-shot per session on stock: a second call blocks ~2s, then fails.
this.layoutLog(() => ["createStartUpPageContainer: repeated call, rejected as stock does"]);
await delay(DUPLICATE_CREATE_DELAY_MS);
return 1;
}
// The Even app refuses some payloads without sending them; then the
// firmware may fail to build the page.
const payloadError = stockPayloadError("create", data);
if (payloadError) {
this.layoutLog(() => [`createStartUpPageContainer refused, as the Even app would: ${payloadError}`]);
return PAGE_CREATE_INVALID;
}
const page = parsePage(data);
this.warnOnInvalidPage(page, data);
const failure = stockPageFailure(page);
if (failure) {
this.layoutLog(() => [`createStartUpPageContainer failed, as the firmware would: ${failure.reason}`]);
return failure.code;
}
clampToStockLimits(page);
this.warnOnDroppedMenuItems(page, data);
this.layoutLog(() => describePage("createStartUpPageContainer", page, data));
this.page = page;
this.pageCreated = true;
this.extendedLayout = false;
// The launch focus arrived before the page existed; deliver the ENTER now.
if (this.foreground && !this.launchEnterSent) {
this.launchEnterSent = true;
@@ -933,16 +999,39 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH
}
private rebuildPage(data: Record<string, unknown>): boolean {
// Also serves as create for apps that fall back to rebuild-on-error.
// Only createStartUpPageContainer starts an EvenHub page on the glasses.
if (!this.pageCreated) {
this.layoutLog(() => ["rebuildPageContainer before createStartUpPageContainer: no page to rebuild"]);
return false;
}
const payloadError = stockPayloadError("rebuild", data);
if (payloadError) {
this.layoutLog(() => [`rebuildPageContainer refused, as the Even app would: ${payloadError}`]);
return false;
}
const page = parsePage(data);
this.warnOnInvalidPage(page, data);
const failure = stockPageFailure(page);
if (failure) {
// The firmware has already torn the old page down; it reports an
// abnormal exit and the Even app closes the app.
this.layoutLog(() => [`rebuildPageContainer failed, as the firmware would: ${failure.reason}; closing the app`]);
this.page = { containers: [], menuItems: [] };
this.windowHooks?.requestRender();
this.emitSysEvent(ABNORMAL_EXIT_EVENT, 0);
this.systemExitSent = true;
setTimeout(() => this.close(), ABNORMAL_EXIT_CLOSE_DELAY_MS);
return false;
}
clampToStockLimits(page);
this.warnOnDroppedMenuItems(page, data);
this.layoutLog(() => describePage("rebuildPageContainer", page, data));
this.page = page;
this.pageCreated = true;
this.extendedLayout = false;
this.windowHooks?.requestRender();
return true;
}
private warnOnInvalidPage(page: EvenHubPage, data: Record<string, unknown>): void {
private warnOnDroppedMenuItems(page: EvenHubPage, data: Record<string, unknown>): void {
const declaredMenuCount = declaredMenuItems(data).length;
if (declaredMenuCount > page.menuItems.length) {
this.log(
@@ -950,64 +1039,106 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH
"items (max 10, itemID must be non-zero and unique, itemName at most 32 UTF-8 bytes)",
);
}
// Stock rejects these outright; we accept-and-log until rejection is
// proven necessary for compatibility.
const captures = page.containers.filter((c) => c.kind !== "image" && c.isEventCapture).length;
if (captures !== 1) this.log(`evenhub: page has ${captures} event-capture containers (stock requires exactly 1)`);
const texts = page.containers.filter((c) => c.kind === "text").length;
const images = page.containers.filter((c) => c.kind === "image").length;
if (texts > 8 || images > 4) this.log(`evenhub: page exceeds stock limits (${texts} text, ${images} image)`);
}
private textUpgrade(data: Record<string, unknown>): boolean {
const id = readNumber(data, "containerID", -1);
// containerID is the identity; containerName is optional in the wild (e.g.
// level-even-g2 omits it). Match by id, enforcing the name only when the app
// actually supplies a non-empty one. (updateImage already matches by id.)
const id = readNumber(data, "containerID", 0);
const name = readString(data, "containerName", "");
const container = this.page?.containers.find(
(c) => c.kind === "text" && c.id === id && (name === "" || c.name === name),
);
if (!container || container.kind !== "text") return false;
const content = readString(data, "content", "");
const offset = readOptionalNumber(data, "contentOffset");
// (contentLength is intentionally ignored — see below.)
// Stock semantics are "write `content` at contentOffset and truncate the
// rest" (a null-terminating strcpy(buf+offset, content)), NOT a splice that
// keeps the old tail after offset+contentLength. Keeping the tail left stray
// trailing text when an app (e.g. g2oom) shrank a string with an
// under-sized contentLength; dropping it matches stock and still supports
// suffix rewrites (offset>0) and full replaces (offset 0 / omitted).
if (offset !== undefined && offset > 0) {
container.content = container.content.slice(0, offset) + content;
} else {
container.content = content;
const payloadError = stockPayloadError("textUpgrade", data);
if (payloadError) {
this.layoutLog(() => [`textContainerUpgrade refused, as the Even app would: ${payloadError}`]);
return false;
}
// The firmware finds the container by id alone (its first container with
// that id, of any kind); the name isn't checked anywhere.
const target = containerById(this.page, id);
if (!target) {
this.layoutLog(() => [describeTextUpgrade(id, name, null, offset, content.length)]);
return false;
}
// Aimed at a list or image, the firmware writes into it as if it were
// text and reports success; nothing we could show.
if (target.kind !== "text") return true;
// The content replaces the whole text: stock ignores contentOffset and
// contentLength (firmware 2.2.9.22 in the emulator showed "XY" alone for
// every offset/length tried). The firmware re-sets the label, scrolled
// back to the top, only for non-empty text, so emptying a container
// leaves its old text showing (apps send " " to clear one).
if (content !== "") {
target.content = content;
target.scrollY = 0;
}
// SDK 0.0.14: textColor on an incremental update sets the container's
// brightness; omitting it keeps whatever level the container has.
const brightness = readTextBrightness(data);
if (brightness !== undefined) container.textColor = brightness;
if (brightness !== undefined) target.textColor = brightness;
this.layoutLog(() => [describeTextUpgrade(id, name, target, offset, content.length)]);
this.windowHooks?.requestRender();
return true;
}
/** BleEhSendImageResult: 0 success, 1 imageException, 2 sizeInvalid, 3 sendFailed. */
/** EhImageUpdateResult: 0 success, 1 imageException, 2 sizeInvalid, 3 toGray4Failed, 4 sendFailed. */
private updateImage(data: Record<string, unknown>): number {
const id = readNumber(data, "containerID", -1);
const container = this.page?.containers.find((c) => c.kind === "image" && c.id === id);
if (!container || container.kind !== "image") return 3;
const bytes = imagePayloadBytes(data.imageData ?? data.rawData ?? asRecord(data).mapRawData);
if (!bytes || bytes.length === 0) return 1;
const decoded = decodeImageBytes(bytes, container);
if (!decoded) return 1;
container.pixels = decoded.pixels;
container.pixelsWidth = decoded.width;
container.pixelsHeight = decoded.height;
this.windowHooks?.requestRender();
return 0;
const id = readNumber(data, "containerID", 0);
const name = readString(data, "containerName", "");
const raw = data.imageData;
// Before the page exists (and after shutdown) the Even app fails it outright.
if (!this.pageCreated || !this.page) {
this.layoutLog(() => [`updateImageRawData id=${id}: no page yet, sendFailed`]);
return IMAGE_SEND_FAILED;
}
// The Even app sizes the bitmap from the image container it finds by id,
// then by trimmed name.
const images = this.page.containers.filter((c): c is EvenHubImageContainer => c.kind === "image");
const geometry = (id > 0 ? images.find((c) => c.id === id) : undefined) ??
(name.trim() ? images.find((c) => c.name.trim() === name.trim()) : undefined);
const prepared = prepareImageUpdate({
imageData: raw,
compressMode: readNumber(data, "compressMode", 0),
requestWidth: readNumber(data, "width", 0),
requestHeight: readNumber(data, "height", 0),
geometry: geometry ?? null,
decodeNative: decodeImageRgba,
stockLimits: !this.extendedLayout,
});
// The firmware takes the transfer only for an id whose first container is
// an image, and then shows the bitmap only if it's exactly that size.
const target = containerById(this.page, id);
let result = prepared.result;
let shown = false;
if (result === IMAGE_SUCCESS && target?.kind !== "image") {
result = IMAGE_SEND_FAILED;
} else if (result === IMAGE_SUCCESS && target?.kind === "image" && prepared.levels &&
prepared.width === target.width && prepared.height === target.height) {
target.pixels = prepared.levels.map((level) => level * 17);
target.pixelsWidth = prepared.width;
target.pixelsHeight = prepared.height;
shown = true;
this.windowHooks?.requestRender();
}
this.layoutLog(() => describeImageUpdate({ sentId: id, sentName: name, raw, target, prepared, shown, result }));
return result;
}
/** Debug-level logcat for layouts and updates; `describe` runs only when logging is on. */
private layoutLog(describe: () => string[]): void {
if (!LAYOUT_LOG_ENABLED) return;
try {
const prefix = `[${this.manifest.packageId}] `;
for (const line of describe()) {
for (let i = 0; i < line.length; i += LOGCAT_CHUNK_CHARS) {
android.util.Log.d(LAYOUT_LOG_TAG, prefix + line.slice(i, i + LOGCAT_CHUNK_CHARS));
}
}
} catch (error) {
console.warn(`evenhub: layout log failed: ${error}`);
}
}
private shutDown(exitMode: number): boolean {
this.layoutLog(() => [`shutDownPageContainer exitMode=${exitMode}`]);
if (exitMode === 1) {
// The app asked to quit (stock would pop an on-glasses confirm). We
// decline and hand focus to the app switcher instead, leaving the app
@@ -1075,6 +1206,15 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH
this.pushEvenHubEvent("sysEvent", { eventType, eventSource, [INPUT_SOURCE_FIELD]: inputSource });
}
private emitTextEvent(text: EvenHubTextContainer, eventType: number, inputSource?: ExtensionInputSource): void {
this.pushEvenHubEvent("textEvent", {
containerID: text.id,
containerName: text.name,
eventType,
[INPUT_SOURCE_FIELD]: inputSource,
});
}
private emitListEvent(list: EvenHubListContainer, eventType: number, inputSource?: ExtensionInputSource): void {
this.pushEvenHubEvent("listEvent", {
containerID: list.id,
@@ -1252,8 +1392,10 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH
for (const container of page.containers) {
if (container.preserve) preserveContent(container, previous);
}
this.layoutLog(() => describePage("extension layout", page, data));
this.page = page;
this.pageCreated = true;
this.extendedLayout = true;
// The extended layout uses the taller app area.
this.windowHooks?.setTallCanvas(true);
if (this.foreground && !this.launchEnterSent) {
@@ -1559,82 +1701,6 @@ function normalizeBuzzerSteps(raw: unknown[]): Step[] {
return out;
}
/**
* Decode an uncompressed Windows BMP ('BM', BI_RGB) to 8bpp grayscale. Handles
* 1/4/8-bit paletted and 24/32-bit BGR(A), top-down or bottom-up. Returns null
* for compressed or unsupported variants. Apps like EvenChess send 1-bit BMPs
* (black/white palette) for full-board refreshes.
*/
function decodeBmp(bytes: Uint8Array): { pixels: Uint8Array; width: number; height: number } | null {
if (bytes.length < 54 || bytes[0] !== 0x42 || bytes[1] !== 0x4d) return null;
const u16 = (o: number) => bytes[o]! | (bytes[o + 1]! << 8);
const u32 = (o: number) => (bytes[o]! | (bytes[o + 1]! << 8) | (bytes[o + 2]! << 16) | (bytes[o + 3]! << 24)) >>> 0;
const i32 = (o: number) => bytes[o]! | (bytes[o + 1]! << 8) | (bytes[o + 2]! << 16) | (bytes[o + 3]! << 24);
const pixelOffset = u32(10);
const dibSize = u32(14);
const width = i32(18);
const heightRaw = i32(22);
const bitCount = u16(28);
const compression = u32(30);
if (compression !== 0) return null; // only BI_RGB (uncompressed)
if (width <= 0 || width > 4096) return null;
const topDown = heightRaw < 0;
const height = Math.abs(heightRaw);
if (height === 0 || height > 4096) return null;
// Palette (grayscale per index) for <=8bpp; each entry is 4 bytes BGRA.
let palette: Uint8Array | null = null;
if (bitCount <= 8) {
let numColors = u32(46);
if (numColors === 0 || numColors > (1 << bitCount)) numColors = 1 << bitCount;
const palOffset = 14 + dibSize;
palette = new Uint8Array(numColors);
for (let i = 0; i < numColors; i++) {
const o = palOffset + i * 4;
if (o + 2 >= bytes.length) break;
palette[i] = luminance(bytes[o + 2]!, bytes[o + 1]!, bytes[o]!);
}
}
const stride = (((width * bitCount + 31) >> 5) << 2); // rows are 4-byte aligned
const pixels = new Uint8Array(width * height);
for (let y = 0; y < height; y++) {
const srcY = topDown ? y : height - 1 - y;
const rowStart = pixelOffset + srcY * stride;
if (rowStart >= bytes.length) break;
const dstRow = y * width;
for (let x = 0; x < width; x++) {
let gray: number;
if (bitCount === 1) {
const bit = ((bytes[rowStart + (x >> 3)] ?? 0) >> (7 - (x & 7))) & 1;
gray = palette?.[bit] ?? (bit ? 255 : 0);
} else if (bitCount === 4) {
const byte = bytes[rowStart + (x >> 1)] ?? 0;
const nib = (x & 1) === 0 ? byte >> 4 : byte & 0x0f;
gray = palette?.[nib] ?? nib * 17;
} else if (bitCount === 8) {
const idx = bytes[rowStart + x] ?? 0;
gray = palette?.[idx] ?? idx;
} else if (bitCount === 24) {
const o = rowStart + x * 3;
gray = luminance(bytes[o + 2] ?? 0, bytes[o + 1] ?? 0, bytes[o] ?? 0);
} else if (bitCount === 32) {
const o = rowStart + x * 4;
gray = luminance(bytes[o + 2] ?? 0, bytes[o + 1] ?? 0, bytes[o] ?? 0);
} else {
return null;
}
pixels[dstRow + x] = gray;
}
}
return { pixels, width, height };
}
function luminance(r: number, g: number, b: number): number {
return Math.round(0.2126 * r + 0.7152 * g + 0.0722 * b);
}
/**
* Input-device names in the extension API: the touchDown event's source, and
* the `faceclawInputSource` field added to stock gesture events.
@@ -1686,97 +1752,3 @@ function elideZeroFields(obj: Record<string, unknown>): Record<string, unknown>
return out;
}
/** Accept the payload forms seen on the wire: number[], JSON-ified typed
* array ({"0": 1, ...}), or base64 string. */
function imagePayloadBytes(raw: unknown): Uint8Array | null {
if (Array.isArray(raw)) {
const out = new Uint8Array(raw.length);
for (let i = 0; i < raw.length; i++) out[i] = Number(raw[i]) & 0xff;
return out;
}
if (typeof raw === "string") return base64Decode(raw);
if (raw && typeof raw === "object") {
const record = raw as Record<string, unknown>;
const keys = Object.keys(record);
const out = new Uint8Array(keys.length);
for (let i = 0; i < keys.length; i++) {
const value = record[String(i)];
if (typeof value !== "number") return null;
out[i] = value & 0xff;
}
return out;
}
return null;
}
const BASE64_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
function base64Decode(text: string): Uint8Array | null {
const clean = text.replace(/[\s=]+$/g, "").replace(/\s+/g, "");
const out = new Uint8Array(Math.floor((clean.length * 3) / 4));
let bits = 0;
let bitCount = 0;
let outIndex = 0;
for (const char of clean) {
const value = BASE64_ALPHABET.indexOf(char);
if (value < 0) return null;
bits = (bits << 6) | value;
bitCount += 6;
if (bitCount >= 8) {
bitCount -= 8;
out[outIndex++] = (bits >> bitCount) & 0xff;
}
}
return out.subarray(0, outIndex);
}
/**
* Decode an image payload: PNG or BMP (both are sent by real apps — EvenChess
* force-refreshes as 1-bit BMP but streams live moves as PNG, so a host that
* only did PNG dropped the initial board), or raw grayscale sized to the
* container (8bpp, or 4bpp packed two pixels per byte, high nibble first).
*/
function decodeImageBytes(
bytes: Uint8Array,
container: EvenHubImageContainer,
): { pixels: Uint8Array; width: number; height: number } | null {
if (bytes.length > 2 && bytes[0] === 0x42 && bytes[1] === 0x4d) {
const bmp = decodeBmp(bytes);
if (bmp) return bmp;
}
if (bytes.length > 4 && bytes[0] === 0x89 && bytes[1] === 0x50 && bytes[2] === 0x4e && bytes[3] === 0x47) {
try {
const buffer = bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength);
const decoded = UPNG.decode(buffer);
const frames = UPNG.toRGBA8(decoded) as ArrayBuffer[];
if (!frames.length) return null;
const rgba = new Uint8Array(frames[0]!);
const pixels = new Uint8Array(decoded.width * decoded.height);
for (let i = 0; i < pixels.length; i++) {
const offset = i * 4;
const alpha = (rgba[offset + 3] ?? 0) / 255;
pixels[i] = Math.round(
(0.2126 * (rgba[offset] ?? 0) + 0.7152 * (rgba[offset + 1] ?? 0) + 0.0722 * (rgba[offset + 2] ?? 0)) * alpha,
);
}
return { pixels, width: decoded.width, height: decoded.height };
} catch (error) {
console.warn(`evenhub: PNG decode failed: ${error}`);
return null;
}
}
const area = container.width * container.height;
if (bytes.length === area) {
return { pixels: Uint8Array.from(bytes), width: container.width, height: container.height };
}
if (bytes.length === Math.ceil(area / 2)) {
const pixels = new Uint8Array(area);
for (let i = 0; i < area; i++) {
const byte = bytes[i >> 1]!;
const nibble = i % 2 === 0 ? byte >> 4 : byte & 0x0f;
pixels[i] = nibble * 17;
}
return { pixels, width: container.width, height: container.height };
}
return null;
}
+110 -20
View File
@@ -1,7 +1,7 @@
import { getDefaultSmallFont } from "../../graphics/ui-fonts";
import { GrayImage, type UiFont } from "../../graphics/image";
import { truncateText, wrapText } from "../../graphics/textwrap";
import { GESTURE_CLICK, type InputEvent } from "../../ui/gestures";
import { directionalFallback, GESTURE_CLICK, isDirectionalInput, type InputEvent } from "../../ui/gestures";
import { type Layer, type LayerActions, type LayerContext, type LayerStack } from "../../ui/layers";
import { drawSelectionHighlight, type MenuItem } from "../../ui/menu";
import { Menu } from "../../ui/menu-core";
@@ -14,6 +14,7 @@ import { EvenHubStoreDetailLayer, type EvenHubStoreDetailOptions } from "./store
import { cleanError, installStoreApp } from "./store-install";
import { checkForEvenHubUpdates, describeUpdate, type EvenHubAppUpdate } from "./updates";
import { lineStep } from "../../ui/metrics";
import { isEvenHubStoreAvailable } from "./glasses-compat";
const LIST_X = 18;
/** Horizontal gap between tab labels. */
@@ -55,7 +56,32 @@ type StoreRow =
/** The Search tab's first row: shows the query and opens the phone editor. */
| { kind: "search-query" }
/** A registry entry whose package is gone and which the store no longer lists. */
| { kind: "orphan"; update: EvenHubAppUpdate };
| { kind: "orphan"; update: EvenHubAppUpdate }
/** The Updates tab's first row while any update is installable; `busy` while Update All runs. */
| { kind: "update-all"; count: number; busy: boolean };
type AppRow = Extract<StoreRow, { kind: "app" }>;
/** An Updates row that installing the store's version would satisfy. */
function isUpdatableRow(row: StoreRow): row is AppRow {
return row.kind === "app" && row.update !== undefined;
}
/** An Updates row for one installed app (not the Update All row). */
function isInstalledAppRow(row: StoreRow): boolean {
return row.kind === "app" || row.kind === "orphan";
}
function rowPackageId(row: StoreRow): string | undefined {
switch (row.kind) {
case "app":
return row.app.packageId;
case "orphan":
return row.update.installed.packageId;
default:
return undefined;
}
}
type TabState = {
rows: StoreRow[];
@@ -118,10 +144,18 @@ export type EvenHubStoreLayerOptions = {
* The EvenHub storefront: Top (leaderboard), New (by first publication),
* Search, and Updates (installed apps that are stale or whose package is
* missing). The tab row is its own focus level above the list: double-click
* in the list focuses the tabs, scrolling there switches tabs, and a click
* returns to the list.
* in the list focuses the tabs, scrolling (or a watch swipe in any direction)
* there switches tabs, and a click returns to the list.
*
* With glasses that can't use the store (a RayNeo iO; see glasses-compat.ts)
* it shows only a notice saying so, and loads nothing. That is checked on
* every paint and input, so a window open across a re-pair follows the
* glasses either way.
*/
export class EvenHubStoreLayer implements Layer {
/** Swipes left/right switch tabs on the tab strip; elsewhere they get the usual fallback. */
readonly acceptsDirectional = true;
private readonly tabs: Record<StoreTab, TabState> = {
top: emptyTab(),
new: emptyTab(),
@@ -140,12 +174,15 @@ export class EvenHubStoreLayer implements Layer {
private updatingAll = false;
/** An app page requested while the login form was up; opened once signed in. */
private pendingPackage: InstalledEvenHubApp | null = null;
/** An app page requested while the store was unavailable, named on its notice. */
private unavailablePackageName: string | null = null;
constructor(private readonly options: EvenHubStoreLayerOptions) {
if (this.showingLogin) resetEvenHubLoginForm();
}
paint(ctx: LayerContext): GrayImage {
if (!isEvenHubStoreAvailable()) return this.paintUnavailable(ctx);
if (!this.started) {
this.started = true;
// Do NOT open the phone credential editor here. This runs on the first
@@ -205,6 +242,23 @@ export class EvenHubStoreLayer implements Layer {
return image;
}
/** The notice that replaces the store when the paired glasses can't use it. */
private paintUnavailable(ctx: LayerContext): GrayImage {
const font = getDefaultSmallFont();
const { width, height } = ctx.stack.getBaseSize();
const image = new GrayImage(width, height, 0);
image.drawText(font, LIST_X, 8, "EvenHub", 220);
if (this.unavailablePackageName) {
const status = `${this.unavailablePackageName}'s package is missing.`;
image.drawText(font, LIST_X, 8 + lineStep(font), truncateText(font, status, width - LIST_X * 2), 125);
}
const message = "Using the Even Realities app store requires Even Realities glasses.";
for (const [index, line] of wrapText(font, message, width - LIST_X * 2).entries()) {
image.drawText(font, LIST_X, headerHeight(font) + 16 + index * lineStep(font), line, 190);
}
return image;
}
/** Tab labels to the right of the title; the active one is bright and underlined. */
private paintTabs(image: GrayImage, font: UiFont, ctx: LayerContext): void {
const tabsFocused = ctx.stack.isFocused() && this.focus === "tabs";
@@ -229,7 +283,7 @@ export class EvenHubStoreLayer implements Layer {
const label = TABS.find((entry) => entry.id === tab)!.label;
if (tab !== "updates") return label;
const updates = this.tabs.updates;
const count = updates.rows.filter((row) => row.kind !== "search-query").length;
const count = updates.rows.filter(isInstalledAppRow).length;
return updates.loaded && count > 0 ? `${label} (${count})` : label;
}
@@ -245,9 +299,11 @@ export class EvenHubStoreLayer implements Layer {
if (!state.query) return "Search public apps";
return results === 1 ? `1 result for "${state.query}"` : `${results} results for "${state.query}"`;
}
case "updates":
case "updates": {
if (!state.loaded) return "Installed apps with newer versions";
return count === 0 ? "All installed apps are up to date" : count === 1 ? "1 app needs attention" : `${count} apps need attention`;
const apps = state.rows.filter(isInstalledAppRow).length;
return apps === 0 ? "All installed apps are up to date" : apps === 1 ? "1 app needs attention" : `${apps} apps need attention`;
}
}
}
@@ -263,7 +319,14 @@ export class EvenHubStoreLayer implements Layer {
}
}
async handleInput(event: InputEvent, ctx: LayerContext): Promise<void> {
async handleInput(rawEvent: InputEvent, ctx: LayerContext): Promise<void> {
if (!isEvenHubStoreAvailable()) {
if (directionalFallback(rawEvent).type === "double-click") shell.yieldFocusToSidebar();
return;
}
const tabSwipe = !this.showingLogin && this.focus === "tabs"
&& (rawEvent.type === "swipe-left" || rawEvent.type === "swipe-right");
const event = isDirectionalInput(rawEvent) && !tabSwipe ? directionalFallback(rawEvent) : rawEvent;
if (this.showingLogin) {
if (event.type === "click" && !this.loginBusy) {
this.openCredentialEditor(ctx);
@@ -301,9 +364,11 @@ export class EvenHubStoreLayer implements Layer {
const index = TABS.findIndex((tab) => tab.id === this.activeTab);
switch (event.type) {
case "scroll-up":
case "swipe-left":
this.switchTab(ctx, TABS[Math.max(0, index - 1)]!.id);
return;
case "scroll-down":
case "swipe-right":
this.switchTab(ctx, TABS[Math.min(TABS.length - 1, index + 1)]!.id);
return;
case "click":
@@ -333,6 +398,9 @@ export class EvenHubStoreLayer implements Layer {
case "search-query":
this.openSearchEditor(ctx);
return;
case "update-all":
void this.updateAll(ctx);
return;
case "orphan": {
// Nothing can be reinstalled; the only useful action is to drop the
// dead registry entry so the launcher stops offering it.
@@ -358,6 +426,12 @@ export class EvenHubStoreLayer implements Layer {
* placeholder built from the registry entry stands in.
*/
showInstalledPackage(stack: LayerStack, installed: InstalledEvenHubApp): void {
if (!isEvenHubStoreAvailable()) {
this.unavailablePackageName = installed.name;
stack.clearToBase();
return;
}
this.unavailablePackageName = null;
if (this.showingLogin) {
this.pendingPackage = installed;
return;
@@ -369,7 +443,7 @@ export class EvenHubStoreLayer implements Layer {
/** Dictated text becomes a search (the shell's "Type Into App" / voice input). */
receiveTextInput(text: string, ctx: LayerContext): void {
if (this.showingLogin) return;
if (this.showingLogin || !isEvenHubStoreAvailable()) return;
const query = evenHubSearchQuerySetting.set(text);
if (!query) return;
this.closePhoneEditor(ctx.actions, "search");
@@ -379,7 +453,7 @@ export class EvenHubStoreLayer implements Layer {
}
buildMenuItems(): MenuItem[] {
if (this.showingLogin) return [];
if (this.showingLogin || !isEvenHubStoreAvailable()) return [];
const items: MenuItem[] = [];
if (this.activeTab === "search") {
items.push({
@@ -532,8 +606,7 @@ export class EvenHubStoreLayer implements Layer {
const rows: StoreRow[] = updates.map((update) =>
update.latest ? { kind: "app", app: update.latest, update } : { kind: "orphan", update },
);
state.rows = rows;
state.menu.setItems(rows, 0);
this.setUpdateRows(state, rows, 0);
state.menu.scrollTop = 0;
state.status = "";
state.exhausted = true;
@@ -548,10 +621,20 @@ export class EvenHubStoreLayer implements Layer {
}
}
private updatableRows(): Extract<StoreRow, { kind: "app" }>[] {
return this.tabs.updates.rows.filter(
(row): row is Extract<StoreRow, { kind: "app" }> => row.kind === "app" && row.update !== undefined,
);
private updatableRows(): AppRow[] {
return this.tabs.updates.rows.filter(isUpdatableRow);
}
/**
* Replace the Updates rows, putting an Update All row on top while any of
* them can be installed (and dropping it once none can).
*/
private setUpdateRows(state: TabState, entries: readonly StoreRow[], selectedIndex?: number): void {
const rows = entries.filter(isInstalledAppRow);
const count = rows.filter(isUpdatableRow).length;
if (count > 0) rows.unshift({ kind: "update-all", count, busy: this.updatingAll });
state.rows = rows;
state.menu.setItems(rows, selectedIndex);
}
/** Install every pending update in turn; a declined permission dialog skips that app. */
@@ -559,6 +642,7 @@ export class EvenHubStoreLayer implements Layer {
if (this.updatingAll) return;
this.updatingAll = true;
const state = this.tabs.updates;
this.setUpdateRows(state, state.rows);
const pending = this.updatableRows();
let installed = 0;
const failures: string[] = [];
@@ -593,6 +677,7 @@ export class EvenHubStoreLayer implements Layer {
: installed === 1 ? "Updated 1 app." : `Updated ${installed} apps.`;
} finally {
this.updatingAll = false;
this.setUpdateRows(this.tabs.updates, this.tabs.updates.rows);
if (!this.closed) ctx.actions.requestRender();
}
}
@@ -609,10 +694,7 @@ export class EvenHubStoreLayer implements Layer {
private removeUpdateRow(packageId: string): void {
const state = this.tabs.updates;
state.rows = state.rows.filter((row) =>
row.kind === "search-query" ? true : row.kind === "app" ? row.app.packageId !== packageId : row.update.installed.packageId !== packageId,
);
state.menu.setItems(state.rows);
this.setUpdateRows(state, state.rows.filter((row) => rowPackageId(row) !== packageId));
}
// ---- login and phone editors --------------------------------------------
@@ -751,6 +833,14 @@ function rowText(row: StoreRow): { title: string; detail: string; titleValue: nu
titleValue: 200,
};
}
case "update-all":
return {
title: row.busy ? "Updating..." : "Update All",
detail: row.busy
? "Installing updates; progress is shown above."
: row.count === 1 ? "Install the 1 available update" : `Install all ${row.count} available updates`,
titleValue: row.busy ? 150 : 230,
};
case "orphan":
return {
title: row.update.installed.name,
+50 -19
View File
@@ -1,4 +1,4 @@
import { ConfigSettingBoolean, ConfigSettingEnum } from "../../ui/dashboard-settings";
import { ConfigSettingBoolean, ConfigSettingEnum, glassesHaveCompass } from "../../ui/dashboard-settings";
import { QUADRANT_LAYOUT, SIX_SLOT_LAYOUT, slotsVerticallyAdjacent, type GlanceLayout } from "./layout";
import { type GlanceWidgetId } from "./widget";
import { glanceWidgetSpans } from "./widgets";
@@ -23,6 +23,11 @@ export function glanceSlotChoiceLabel(choice: GlanceSlotChoice): string {
return SLOT_CHOICE_LABELS[choice] ?? choice;
}
/** Whether the paired glasses can drive this widget; if not, it is left out of the slot picker. */
function slotChoiceAvailable(choice: GlanceSlotChoice): boolean {
return choice !== "compass" || glassesHaveCompass();
}
/**
* Slot order follows QUADRANT_LAYOUT: top left, top right, bottom left, bottom
* right. Calendar in both right slots merges into one double-height region.
@@ -56,9 +61,28 @@ export const glanceboardEnabledSetting = new ConfigSettingBoolean({
"While the display is asleep, a tap, a long-press or a head-tilt shows the Glanceboard instead of the regular UI. Double-tap still wakes the regular UI.",
});
export type GlanceTapDuration = "off" | "3s" | "5s" | "7s" | "10s";
const TAP_DURATION_SECONDS = {
"3s": 3,
"5s": 5,
"7s": 7,
"10s": 10,
"20s": 20,
"30s": 30,
"1min": 60,
} as const;
const DEFAULT_TAP_DURATION: GlanceTapDuration = "5s";
type GlanceTapDurationTimed = keyof typeof TAP_DURATION_SECONDS;
export type GlanceTapDuration = "off" | GlanceTapDurationTimed;
const TAP_DURATION_VALUES: readonly GlanceTapDuration[] = ["off", ...(Object.keys(TAP_DURATION_SECONDS) as GlanceTapDurationTimed[])];
const DEFAULT_TAP_DURATION: GlanceTapDurationTimed = "5s";
function formatTapDuration(value: GlanceTapDuration): string {
if (value === "off") return "Disabled";
const seconds = TAP_DURATION_SECONDS[value];
return seconds % 60 === 0 ? `${seconds / 60} min` : `${seconds} s`;
}
/**
* How long a tap (or head-tilt) keeps the board up; each further tap restarts
@@ -70,8 +94,8 @@ export const glanceTapDurationSetting = new ConfigSettingEnum<GlanceTapDuration>
label: "Show on tap",
storageKey: "glanceboard.tapDuration",
defaultValue: DEFAULT_TAP_DURATION,
values: ["off", "3s", "5s", "7s", "10s"],
formatValue: (value) => (value === "off" ? "Disabled" : value.replace("s", " s")),
values: TAP_DURATION_VALUES,
formatValue: formatTapDuration,
description:
"How long a single tap (or a head-tilt) keeps the Glanceboard on screen. Disabled: a single tap does not show the Glanceboard.",
});
@@ -83,8 +107,7 @@ export function glanceShowOnTap(): boolean {
export function glanceTapTimeoutMs(): number {
const duration = glanceTapDurationSetting.get();
const seconds = duration === "off" ? DEFAULT_TAP_DURATION : duration;
return Number.parseInt(seconds, 10) * 1000;
return TAP_DURATION_SECONDS[duration === "off" ? DEFAULT_TAP_DURATION : duration] * 1000;
}
/** A long-press holds the board up until the press is released. */
@@ -146,18 +169,26 @@ export const glanceShowLinesSetting = new ConfigSettingBoolean({
export function glanceSlotSettings(layout: GlanceLayout = glanceLayout()): ConfigSettingEnum<GlanceSlotChoice>[] {
const grid = layout === QUADRANT_LAYOUT || layout === SIX_SLOT_LAYOUT;
const storageId = grid ? QUADRANT_LAYOUT.id : layout.id;
return layout.slots.map(
(slot, index) =>
new ConfigSettingEnum<GlanceSlotChoice>({
id: `glanceboard-${layout.id}-slot-${index}`,
label: slot.label,
storageKey: `glanceboard.${storageId}.slot.${index}`,
defaultValue: (grid ? DEFAULT_QUADRANT_CHOICES[index] : undefined) ?? "none",
values: SLOT_CHOICES,
formatValue: glanceSlotChoiceLabel,
description: `Which widget fills the ${slot.label.toLowerCase()} slot of the Glanceboard.`,
}),
);
return layout.slots.map((slot, index) => {
const defaultValue: GlanceSlotChoice = (grid ? DEFAULT_QUADRANT_CHOICES[index] : undefined) ?? "none";
return new ConfigSettingEnum<GlanceSlotChoice>({
id: `glanceboard-${layout.id}-slot-${index}`,
label: slot.label,
storageKey: `glanceboard.${storageId}.slot.${index}`,
defaultValue,
values: SLOT_CHOICES.filter(slotChoiceAvailable),
// A stored choice the glasses can't drive reads as Empty, not the slot's
// default (which could put a widget on the board twice). The stored value
// stays, so pairing glasses that can drive it brings it back.
normalize: (value) => {
const choice = value as GlanceSlotChoice;
if (!SLOT_CHOICES.includes(choice)) return defaultValue;
return slotChoiceAvailable(choice) ? choice : "none";
},
formatValue: glanceSlotChoiceLabel,
description: `Which widget fills the ${slot.label.toLowerCase()} slot of the Glanceboard.`,
});
});
}
/**
+2 -1
View File
@@ -18,6 +18,7 @@ import {
batteryIndicatorVisible,
ConfigSettingBoolean,
glassesBatteryVisibilitySetting,
glassesModelName,
onAnySettingChanged,
phoneBatteryVisibilitySetting,
ringBatteryVisibilitySetting,
@@ -328,7 +329,7 @@ function collectBatteryItems(): BatteryItem[] {
push(phoneBatteryVisibilitySetting.get(), "Phone", "phone", phone.battery, phone.charging);
const levels = shell.getBatteryLevels();
push(watchBatteryVisibilitySetting.get(), "Watch", "watch", levels.watch, levels.watchCharging);
push(glassesBatteryVisibilitySetting.get(), "G2", "glasses", levels.headset, levels.headsetCharging);
push(glassesBatteryVisibilitySetting.get(), glassesModelName(), "glasses", levels.headset, levels.headsetCharging);
if (levels.ring !== null && Number.isInteger(levels.ring) && levels.ring >= 0 && levels.ring <= 100) {
push(ringBatteryVisibilitySetting.get(), "R1", "ring", levels.ring, levels.ringCharging);
}
+1 -1
View File
@@ -11,7 +11,7 @@ import {
export function launcherEntries(apps: readonly AppDefinition[]) {
return [
...apps
.filter((app) => app.showInLauncher !== false)
.filter((app) => app.showInLauncher !== false && app.isSupported?.() !== false)
.map((app) => ({
appId: app.appId,
label: app.title,
+2 -1
View File
@@ -84,7 +84,8 @@ function buildNightscoutTrayIcon(state: NightscoutState): GrayImage {
const font = getDefaultSmallFont();
const nowMs = Date.now();
const label = state.latest ? `${state.latest.sgv}` : "--";
const labelWidth = font.measureText(label);
// A TTF face measures fractionally; the icon's size and layout need whole pixels.
const labelWidth = Math.ceil(font.measureText(label));
const warning = evaluateNightscoutAlerts(state, loadNightscoutThresholds(), nowMs).any;
const graphEnd = labelWidth + 4 + TRAY_GRAPH_WIDTH;
const image = new GrayImage(graphEnd + (warning ? 24 : 0), TRAY_HEIGHT, 0);
+6 -1
View File
@@ -14,7 +14,12 @@ const notificationsApp: AppDefinition = {
if (listenerState === "no-access" || listenerState === "stuck") {
requestNotificationListenerAccess();
}
return ctx.launchInProcessApp(NOTIFICATIONS_WINDOW_ID, NOTIFICATIONS_SURFACE_ID, createNotificationsAppWindow);
return ctx.launchInProcessApp(NOTIFICATIONS_WINDOW_ID, NOTIFICATIONS_SURFACE_ID, (options) =>
createNotificationsAppWindow(options, (section) => {
void ctx.launchApp("settings", { section }).catch((error) => {
ctx.appendLog(`settings launch failed: ${error}`);
});
}));
},
};
+9 -5
View File
@@ -1,5 +1,5 @@
import { NotificationsListLayer } from "../../ui/notifications";
import { NotificationFilterLayer } from "../../ui/notification-filter";
import { NOTIFICATIONS_SETTINGS_SECTION } from "../../ui/dashboard/settings-menus";
import { ALL_NOTIFICATIONS, dismissAllNotifications, onAndroidNotificationsChanged, readActiveNotifications } from "../../native/notification-icons";
import {
createInProcessWindow,
@@ -14,9 +14,13 @@ export const NOTIFICATIONS_SURFACE_ID = "window:notifications";
/**
* The Notifications app: the Android-notification list (previously a
* dashboard page) in its own in-process window; selecting a notification
* opens the detail view with its quick actions.
* opens the detail view with its quick actions. `openSettings` opens a
* Settings section (the menu's "Settings" goes to Notifications).
*/
export function createNotificationsAppWindow(options: InProcessAppOptions): InProcessWindow {
export function createNotificationsAppWindow(
options: InProcessAppOptions,
openSettings: (section: string) => void,
): InProcessWindow {
// Newly posted notifications repaint the list while the window is open.
let offNotificationsChanged: (() => void) | null = null;
const created = createInProcessWindow({
@@ -27,10 +31,10 @@ export function createNotificationsAppWindow(options: InProcessAppOptions): InPr
icon: "bell",
closeable: true,
menuItems: () => [{
label: "Notification filter",
label: "Settings",
onSelect: (ctx) => {
ctx.stack.pop();
ctx.stack.push(new NotificationFilterLayer());
openSettings(NOTIFICATIONS_SETTINGS_SECTION);
},
}, {
label: "Dismiss all notifications",
-43
View File
@@ -1,43 +0,0 @@
import { IOS_PRIVACY_NOTICE } from "../../analytics/privacy-disclosure";
import type { GrayImage } from "../../graphics/image";
import { wrapText } from "../../graphics/textwrap";
import type { InputEvent } from "../../ui/gestures";
import { onboardingFont } from "./onboarding-fonts";
import { drawPageHeading, PAGE_X, type OnboardingNav, type OnboardingPage } from "./onboarding-page";
/** Paginate the notice so no disclosure is silently clipped on a short glasses viewport. */
export class IosPrivacyPage implements OnboardingPage {
readonly immersive = true;
private page = 0;
private pageCount = 1;
enter(): void { this.page = 0; }
paint(image: GrayImage): void {
const top = drawPageHeading(image, "iOS / TestFlight privacy", "") + 8;
const font = onboardingFont("detail");
const lines = IOS_PRIVACY_NOTICE.split("\n\n").flatMap((paragraph, index) => [
...(index ? [""] : []), ...wrapText(font, paragraph, image.width - 2 * PAGE_X),
]);
const perPage = Math.max(1, Math.floor((image.height - top - font.lineHeight - 20) / font.lineHeight));
this.pageCount = Math.ceil(lines.length / perPage);
this.page = Math.min(this.page, this.pageCount - 1);
lines.slice(this.page * perPage, (this.page + 1) * perPage).forEach((line, index) => {
image.drawText(font, PAGE_X, top + index * font.lineHeight, line, 200);
});
const action = this.page + 1 < this.pageCount ? "Tap for more" : "Tap to choose statistics";
image.drawText(font, PAGE_X, image.height - font.lineHeight - 8,
`${this.page + 1}/${this.pageCount} - ${action}`, 230);
}
handleInput(event: InputEvent, nav: OnboardingNav): void {
if (event.type === "click") {
if (this.page + 1 < this.pageCount) this.page++;
else nav.next();
} else if (event.type === "scroll-down" || event.type === "swipe-up") {
this.page = Math.min(this.page + 1, this.pageCount - 1);
} else if (event.type === "scroll-up" || event.type === "swipe-down") {
this.page = Math.max(0, this.page - 1);
}
}
}
+22 -11
View File
@@ -3,9 +3,9 @@
* page: to scale in real-display pixels, simplified, and drawn brighter
* than the real chrome, which they shrink to a few pixels.
*/
import { G2_LENS_WIDTH, type GrayImage } from "../../graphics/image";
import type { GrayImage } from "../../graphics/image";
import { statusBarPositionSetting, type AppSwitcherPosition } from "../../ui/dashboard-settings";
import { SIDEBAR_WIDTH, SWITCHER_ROW_HEIGHT, MIN_WINDOW_HEIGHT, TOP_BAR_HEIGHT } from "../../ui/shell/geometry";
import { MIN_WINDOW_HEIGHT, screenHeight, screenWidth, SIDEBAR_WIDTH, SWITCHER_ROW_HEIGHT, TOP_BAR_HEIGHT } from "../../ui/shell/geometry";
export const AREA_OUTLINE = 30;
const CHROME_LINE = 100;
@@ -80,14 +80,14 @@ export function drawLayout(d: Diagram, position: AppSwitcherPosition, band: Diag
switch (position) {
case "left":
case "right": {
const stripLeft = position === "left" ? 0 : G2_LENS_WIDTH - SIDEBAR_WIDTH;
const stripLeft = position === "left" ? 0 : screenWidth() - SIDEBAR_WIDTH;
const contentLeft = position === "left" ? SIDEBAR_WIDTH : 0;
const windowWidth = G2_LENS_WIDTH - SIDEBAR_WIDTH;
const windowWidth = screenWidth() - SIDEBAR_WIDTH;
drawStatusBar(d, contentLeft, top, windowWidth, TOP_BAR_HEIGHT - 1);
drawContent(d, contentLeft, top + TOP_BAR_HEIGHT, windowWidth, height - TOP_BAR_HEIGHT, 1);
// The strip keeps to the 288px band's height, even beside taller windows.
// The strip keeps to the min band's height (288px, or the whole short screen), even beside taller windows.
const separator = position === "left" ? SIDEBAR_WIDTH - 1 : stripLeft;
d.vline(separator, top + TOP_BAR_HEIGHT, MIN_WINDOW_HEIGHT - TOP_BAR_HEIGHT, CHROME_LINE);
d.vline(separator, top + TOP_BAR_HEIGHT, diagramBandHeight("left") - TOP_BAR_HEIGHT, CHROME_LINE);
// One column of icons against the separator.
const column = position === "left" ? SIDEBAR_WIDTH - 36 : stripLeft;
for (let slot = 0; slot < 4; slot++) drawIcon(d, column + 2, top + TOP_BAR_HEIGHT + 10 + slot * 40, slot === 0);
@@ -131,11 +131,11 @@ export function drawLayout(d: Diagram, position: AppSwitcherPosition, band: Diag
}
}
/** A bottom row or popup layout's window: centred at 576 wide and framed, or the panel's width without a frame. */
/** A bottom row or popup layout's window: centred, SIDEBAR_WIDTH narrower, framed; or the screen's width without a frame. */
function windowSpan(band: DiagramBand): { left: number; width: number; framed: boolean } {
return band.fullWidth
? { left: 0, width: G2_LENS_WIDTH, framed: false }
: { left: SIDEBAR_WIDTH / 2, width: G2_LENS_WIDTH - SIDEBAR_WIDTH, framed: true };
? { left: 0, width: screenWidth(), framed: false }
: { left: SIDEBAR_WIDTH / 2, width: screenWidth() - SIDEBAR_WIDTH, framed: true };
}
/**
@@ -173,5 +173,16 @@ function drawContent(d: Diagram, x: number, y: number, width: number, height: nu
}
}
/** Real-display rows the Layout page's diagrams cover: a 288px band and a bottom switcher row under it. */
export const LAYOUT_AREA_HEIGHT = MIN_WINDOW_HEIGHT + SWITCHER_ROW_HEIGHT;
/**
* Real-display rows the Layout page's diagrams cover: a 288px band and a
* bottom switcher row under it, or the whole screen where that is shorter
* (the RayNeo iO's 220 rows).
*/
export function layoutAreaHeight(): number {
return Math.min(MIN_WINDOW_HEIGHT + SWITCHER_ROW_HEIGHT, screenHeight());
}
/** A layout's min-height band within the diagram area: 288px, less a bottom row where the screen is short. */
export function diagramBandHeight(position: AppSwitcherPosition): number {
return Math.min(MIN_WINDOW_HEIGHT, layoutAreaHeight() - (position === "bottom" ? SWITCHER_ROW_HEIGHT : 0));
}
+26 -19
View File
@@ -1,9 +1,9 @@
import { G2_LENS_WIDTH, type GrayImage, type UiFont } from "../../graphics/image";
import { appSwitcherPositionSetting, type AppSwitcherPosition } from "../../ui/dashboard-settings";
import type { GrayImage, UiFont } from "../../graphics/image";
import { appSwitcherPositionSetting, glassesTypeSetting, type AppSwitcherPosition } from "../../ui/dashboard-settings";
import type { InputEvent } from "../../ui/gestures";
import { MENU_HIGHLIGHT_FILL, MENU_HIGHLIGHT_STROKE } from "../../ui/menu-core";
import { MIN_WINDOW_HEIGHT } from "../../ui/shell/geometry";
import { AREA_OUTLINE, Diagram, drawLayout, LAYOUT_AREA_HEIGHT } from "./layout-diagram";
import { screenWidth } from "../../ui/shell/geometry";
import { AREA_OUTLINE, Diagram, diagramBandHeight, drawLayout, layoutAreaHeight } from "./layout-diagram";
import { onboardingFont } from "./onboarding-fonts";
import { drawPageHeading, type OnboardingNav, type OnboardingPage } from "./onboarding-page";
@@ -19,8 +19,13 @@ const ROW_GAP = 6;
const DEPTH_SUBTITLE = "Customizable Depth";
/** Grid order, in rows of two: the layouts with depth on top. */
const LAYOUT_ORDER: readonly AppSwitcherPosition[] = ["bottom", "popup", "left", "right"];
/** Selected on the first visit, and marked. */
const RECOMMENDED_LAYOUT: AppSwitcherPosition = "bottom";
/**
* Selected on the first visit, and marked: Bottom on the G2, whose 480 rows
* leave room for the row; a side strip on the RayNeo iO, whose 220 can't.
*/
export function recommendedLayout(): AppSwitcherPosition {
return glassesTypeSetting.get() === "rayneo-io" ? "left" : "bottom";
}
/** Whether a layout leaves the side margins the display's stereo depth shifts into (geometry.ts uiDepth). */
export function layoutHasDepth(position: AppSwitcherPosition): boolean {
@@ -29,7 +34,7 @@ export function layoutHasDepth(position: AppSwitcherPosition): boolean {
/**
* The app switcher position, as a 2×2 grid of captioned diagrams of each
* layout, Bottom (recommended, and selected at first) and Popup on top;
* layout, Bottom and Popup on top, the recommended one selected at first;
* swipes move through them in reading order, a tap saves the selected one.
* The page is immersive, so the diagrams get 40% of the display's width
* each. Choosing a layout reflows open windows (the dashboard controller
@@ -38,7 +43,7 @@ export function layoutHasDepth(position: AppSwitcherPosition): boolean {
export class LayoutPage implements OnboardingPage {
readonly immersive = true;
/** Kept across visits, so going back returns to the last choice. */
private selected = LAYOUT_ORDER.indexOf(RECOMMENDED_LAYOUT);
private selected = LAYOUT_ORDER.indexOf(recommendedLayout());
paint(image: GrayImage, focused: boolean): void {
const top = drawPageHeading(
@@ -53,11 +58,11 @@ export class LayoutPage implements OnboardingPage {
// Short of DIAGRAM_SCALE only where two rows of cells wouldn't fit.
const scale = Math.min(
DIAGRAM_SCALE,
((available - ROW_GAP) / 2 - 2 * CELL_PADDING - captionHeight) / LAYOUT_AREA_HEIGHT,
((image.width - COLUMN_GAP) / 2 - 2 * CELL_PADDING) / G2_LENS_WIDTH,
((available - ROW_GAP) / 2 - 2 * CELL_PADDING - captionHeight) / layoutAreaHeight(),
((image.width - COLUMN_GAP) / 2 - 2 * CELL_PADDING) / screenWidth(),
);
const diagramWidth = Math.round(G2_LENS_WIDTH * scale);
const diagramHeight = Math.round(LAYOUT_AREA_HEIGHT * scale);
const diagramWidth = Math.round(screenWidth() * scale);
const diagramHeight = Math.round(layoutAreaHeight() * scale);
const cellWidth = diagramWidth + 2 * CELL_PADDING;
const cellHeight = diagramHeight + 2 * CELL_PADDING + captionHeight;
const left = Math.round((image.width - 2 * cellWidth - COLUMN_GAP) / 2);
@@ -75,7 +80,7 @@ export class LayoutPage implements OnboardingPage {
image.drawText(font, x + Math.round((cellWidth - font.measureText(text)) / 2), textY, text, value);
const captionTop = y + CELL_PADDING + diagramHeight + CAPTION_GAP;
const name = appSwitcherPositionSetting.displayValue(position);
const caption = position === RECOMMENDED_LAYOUT ? `${name} (Recommended)` : name;
const caption = position === recommendedLayout() ? `${name} (Recommended)` : name;
centre(caption, title, captionTop, selected ? 255 : 190);
if (layoutHasDepth(position)) {
centre(DEPTH_SUBTITLE, subtitle, captionTop + title.lineHeight, selected ? 210 : 150);
@@ -103,12 +108,14 @@ export class LayoutPage implements OnboardingPage {
}
/**
* A layout's diagram: the 288px band, with the popup switcher open. Only a
* bottom switcher row takes rows under the band; without one, the band sits
* centred in their place, as the band and row share the screen's slack.
* A layout's diagram: the min-height band, with the popup switcher open.
* Only a bottom switcher row takes rows under the band; without one, the
* band sits centred in their place, as the band and row share the screen's
* slack (none on a screen as short as the RayNeo iO's: the band fills it).
*/
function drawLayoutDiagram(d: Diagram, position: AppSwitcherPosition): void {
d.outline(0, 0, G2_LENS_WIDTH, LAYOUT_AREA_HEIGHT, AREA_OUTLINE, 12);
const top = position === "bottom" ? 0 : (LAYOUT_AREA_HEIGHT - MIN_WINDOW_HEIGHT) / 2;
drawLayout(d, position, { top, height: MIN_WINDOW_HEIGHT, fullWidth: false }, true);
d.outline(0, 0, screenWidth(), layoutAreaHeight(), AREA_OUTLINE, 12);
const height = diagramBandHeight(position);
const top = position === "bottom" ? 0 : (layoutAreaHeight() - height) / 2;
drawLayout(d, position, { top, height, fullWidth: false }, true);
}
+1 -4
View File
@@ -7,7 +7,6 @@ import { findSoundEffect, playSoundEffect } from "../../ui/sound-effects";
import { DataCollectionPage } from "./data-collection-page";
import { DepthPage } from "./depth-page";
import { DisplayModePage } from "./display-mode-page";
import { IosPrivacyPage } from "./ios-privacy-page";
import { LayoutPage } from "./layout-page";
import { markGlassesOnboardingFinished, ONBOARDING_APP_ID } from "./onboarding-progress";
import type { OnboardingPage } from "./onboarding-page";
@@ -26,9 +25,7 @@ export const ONBOARDING_SURFACE_ID = "window:onboarding";
*/
export function createOnboardingAppWindow(options: InProcessAppOptions): InProcessWindow {
const pages: OnboardingPage[] = [
new WelcomePage(), new TextSizePage(),
...(global.isIOS ? [new IosPrivacyPage()] : []),
new DataCollectionPage(), new LayoutPage(), new DepthPage(), new DisplayModePage(),
new WelcomePage(), new TextSizePage(), new DataCollectionPage(), new LayoutPage(), new DepthPage(), new DisplayModePage(),
];
const app = createInProcessWindow({
appId: ONBOARDING_APP_ID,
+5 -5
View File
@@ -3,6 +3,7 @@ import { installedFontPath } from "../../graphics/installed-fonts";
import { wrapText } from "../../graphics/textwrap";
import { TtfFont } from "../../graphics/ttf-font";
import {
defaultUiFontSize,
fontSelectionLabel,
getDefaultSmallFont,
getUiFontSelection,
@@ -25,8 +26,6 @@ const ROW_GAP = 2;
const NOTE_GAP = 8;
/** The sizes offered here; Settings has the full range. */
const SIZE_CHOICES = FONT_SIZE_CHOICES.filter((size) => size >= 12 && size <= 18);
/** Selected on the first visit, and marked. */
const RECOMMENDED_SIZE = 16;
const RECOMMENDED_NOTE = "Recommended";
/** Drawn while they fit, so smaller sizes visibly show more words. */
const PREVIEW_PARAGRAPHS = [
@@ -41,7 +40,8 @@ type Face =
| { kind: "fixed"; label: string };
/**
* The UI font's size, 12 to 18px (16 recommended, and selected at first),
* The UI font's size, 12 to 18px (the default size recommended, and
* selected at first: 16px, or 14px on the RayNeo iO),
* beside a preview of text in the selected size. The face and weight stay
* as they are (they live in the Settings font picker); a bitmap face has
* only one size, so the page just says so. The size is saved on tap, not
@@ -67,7 +67,7 @@ export class TextSizePage implements OnboardingPage {
if (sizes.length) {
this.face = { kind: "sized", file: selection.file, path, sizes };
// The recommended size the first time; on a return, what was left selected.
this.sizeMenu.setItems(sizes, nearestIndex(sizes, this.sizeMenu.selectedItem ?? RECOMMENDED_SIZE));
this.sizeMenu.setItems(sizes, nearestIndex(sizes, this.sizeMenu.selectedItem ?? defaultUiFontSize()));
return;
}
}
@@ -137,7 +137,7 @@ export class TextSizePage implements OnboardingPage {
const body = onboardingFont("body");
const label = sizeLabel(size);
image.drawText(body, x + 10, y + LIST_ROW_TEXT_INSET, label, selected ? 255 : 190);
if (size !== RECOMMENDED_SIZE) return;
if (size !== defaultUiFontSize()) return;
const detail = onboardingFont("detail");
const noteY = y + Math.round((height - detail.lineHeight) / 2);
image.drawText(detail, x + 10 + body.measureText(label) + NOTE_GAP, noteY, RECOMMENDED_NOTE, selected ? 210 : 150);
+171 -26
View File
@@ -6,9 +6,15 @@
* connected host's heading launches a shell there), and hosts the Manage
* Connections section
* where g2mirror:// connections are added, removed, and toggled.
* - "terminal:view:N": opened by selecting a session in the hub; each has
* its own websocket connection (the protocol allows one attached session
* per connection) and its own xterm emulator.
* - Selecting a session in the hub shows it in the hub's own window: a view
* takes the hub's place in `windows` under the same window id, and
* double-click puts the hub back. The window grows to the view's height
* meanwhile. Sessions already open in a window of their own focus it instead.
* - "terminal:view:N": a session in a window of its own, opened with "Open in
* new window" from the window menu (or by launching a session).
*
* Each view has its own websocket connection (the protocol allows one attached
* session per connection) and its own xterm emulator.
*
* Connections are g2mirror://<token>@<host>[:port] strings (g2mirrors:// for
* TLS), stored as JSON in the settings store (see connections.ts). Each
@@ -69,7 +75,7 @@ import { drawSubmenuIndicator, submenuItem, type MenuItem } from "../../ui/menu"
import { Menu, type MenuDrawArgs } from "../../ui/menu-core";
import { centeredTextY, lineStep, listRowHeight, textInkHeight } from "../../ui/metrics";
import { WindowMenu, WindowMenuLayer } from "../../ui/window-menu";
import { appViewportSize } from "../../ui/shell/geometry";
import { appViewportSize, type WindowHeightMode } from "../../ui/shell/geometry";
import type { WorkerAppMessage, WorkerAppReply } from "../../ui/shell/worker-window";
import type { ToolResult, ToolSpec } from "../../assistant/tool-registry";
@@ -109,7 +115,10 @@ const LAUNCH_PRESETS_KEY = "terminal.launchPresets";
type BaseWindow = {
windowId: string;
surfaceId: string;
/** Shell-side window title, echoed back in the open-window message. */
/**
* Window menu title: the shell-side window title, echoed back in the
* open-window message, or the session's label for a view in the hub's window.
*/
title: string;
/** Content viewport from the shell's open-window message; grid = viewport / cell. */
viewportWidth: number;
@@ -155,6 +164,11 @@ type HubWindow = BaseWindow & {
type ViewWindow = BaseWindow & {
kind: "view";
/**
* Set when this view is showing in the hub's window rather than one of its
* own: the hub it replaced, which double-click restores.
*/
hub: HubWindow | null;
/** Grid font and integer cell pitch, captured at window creation. */
font: UiFont;
cellWidth: number;
@@ -202,8 +216,13 @@ type PendingView = {
options: G2MirrorClientOptions;
};
type Viewport = { width: number; height: number };
const windows = new Map<string, TerminalWindow>();
const pendingViews = new Map<string, PendingView>();
// Windows waiting on the shell's answer to a set-height-mode, by window id:
// what to do with the viewport it comes back with (see changeWindowHeight).
const pendingHeightChanges = new Map<string, (viewport: Viewport) => void>();
// The view an assistant tool acts on when the terminal isn't foregrounded:
// the last view to be foregrounded or receive input.
let activeViewId: string | null = null;
@@ -338,6 +357,12 @@ global.onmessage = (event: { data: WorkerAppMessage }) => {
openWindow(message.windowId, message.surfaceId, message.title, message.viewport);
break;
case "resize-window": {
const heightChange = pendingHeightChanges.get(message.windowId);
if (heightChange) {
pendingHeightChanges.delete(message.windowId);
heightChange(message.viewport);
break;
}
const window = windows.get(message.windowId);
if (!window || (window.viewportWidth === message.viewport.width && window.viewportHeight === message.viewport.height)) break;
window.viewportWidth = message.viewport.width;
@@ -543,18 +568,24 @@ function openWindow(windowId: string, surfaceId: string, title: string, viewport
updateHubAnimation();
}
/** Drop a view's connection for good: its window closed or the hub took its place. */
function stopViewConnection(window: ViewWindow): void {
for (const unsubscribe of window.unsubscribers.splice(0)) {
unsubscribe();
}
if (window.reconnectTimer) {
clearTimeout(window.reconnectTimer);
window.reconnectTimer = null;
}
window.client.stop();
}
function closeWindow(windowId: string): void {
const window = windows.get(windowId);
if (!window) return;
pendingHeightChanges.delete(windowId);
if (window.kind === "view") {
for (const unsubscribe of window.unsubscribers.splice(0)) {
unsubscribe();
}
if (window.reconnectTimer) {
clearTimeout(window.reconnectTimer);
window.reconnectTimer = null;
}
window.client.stop();
stopViewConnection(window);
}
if (window.kind === "hub" && window.mode === "add") {
// Window closed mid-add: shut the phone editor down.
@@ -759,10 +790,11 @@ function routeBell(connectionId: string, socket: string, lastBellAtMs: number):
}
}
if (terminalWakeOnBellSetting.get()) {
// Wake to the belling session's view window, or the hub if it has none.
// The shell drops the message unless the glasses are actually asleep.
// Wake to the belling session's view window, or the hub's window (whatever
// it is showing) if it has none. The shell drops the message unless the
// glasses are actually asleep.
const viewId = viewWindowIdForSocket(connectionId, socket);
const hubId = [...windows.values()].find((window) => window.kind === "hub")?.windowId ?? null;
const hubId = [...windows.values()].find((window) => window.kind === "hub" || window.hub)?.windowId ?? null;
const target = viewId ?? hubId;
if (target) post({ type: "wake-window", windowId: target });
}
@@ -895,6 +927,7 @@ function createViewWindow(
const client = new G2MirrorClient({ ...view.options, cols: gridCols, rows: gridRows });
const window: ViewWindow = {
kind: "view",
hub: null,
windowId,
surfaceId,
title,
@@ -1053,12 +1086,41 @@ function connectedControls(): ControlConnection[] {
return [...controls.values()].filter(isControlConnected);
}
/**
* What the window menu's "Open in new window" does here, if it applies: to the
* session selected in the hub's list, or the session showing in the hub's
* window (which gives way to the list, as a session has one window at a time).
*/
function openInNewWindowAction(window: TerminalWindow): (() => void) | null {
if (window.kind === "hub") {
const row = hubMenu(window).selectedItem?.sessionRow;
if (!row) return null;
return () => {
if (!focusSessionWindow(row.control, row.session)) openViewWindowFor(row.control, row.session);
};
}
const control = window.hub && controls.get(window.connectionId);
if (!control) return null;
return () => returnToHub(window, () => openViewWindow(control, window.socket, window.label));
}
function windowMenuItems(window: TerminalWindow): MenuItem[] {
const items: MenuItem[] = [
const items: MenuItem[] = [];
const openInNewWindow = openInNewWindowAction(window);
if (openInNewWindow) {
items.push({
label: "Open in new window",
onSelect: (ctx) => {
ctx.stack.pop();
openInNewWindow();
},
});
}
items.push(
submenuItem("Settings", (ctx) => {
ctx.stack.push(new WindowMenuLayer("Terminal settings", settingsMenuItems()));
}),
];
);
if (window.kind === "hub") {
const connected = connectedControls();
const multiHost = connected.length > 1;
@@ -1142,8 +1204,14 @@ function handleInput(window: TerminalWindow, event: InputEvent, frameId: number)
return;
}
if (event.type === "double-click") {
// Inside the hub's sub-sections, double-click backs out one level; at the
// top level (and in views) it yields focus to the sidebar as everywhere.
// Inside the hub's sub-sections and a session shown in its window,
// double-click backs out one level; at the top level (and in views with
// windows of their own) it yields focus to the sidebar as everywhere.
if (window.kind === "view" && window.hub) {
frameTimings.finishFrame(frameId, "discarded: terminal returning to the session list");
returnToHub(window);
return;
}
if (window.kind === "hub" && window.mode === "add") {
cancelAddConnection(window);
renderAndSubmit(window, frameId);
@@ -1233,6 +1301,8 @@ type HubItem = {
active?: boolean;
/** Glyph of the view window showing this session (the number column). */
openGlyph?: string | null;
/** The live session this row lists, for the window menu's "Open in new window". */
sessionRow?: { control: ControlConnection; session: G2MirrorSession };
onSelect?: () => void;
};
@@ -1371,13 +1441,9 @@ function hubSessionItems(window: HubWindow): HubItem[] {
label: sessionLabel(session),
active: isSessionActive(control.config.id, session.socket),
openGlyph: viewGlyphForSocket(control.config.id, session.socket),
sessionRow: { control, session },
onSelect: () => {
const windowId = viewWindowIdForSocket(control.config.id, session.socket);
if (windowId) {
post({ type: "focus-window", windowId });
} else {
openViewWindowFor(control, session);
}
if (!focusSessionWindow(control, session)) showSessionInHub(window, control, session);
},
});
}
@@ -1587,10 +1653,89 @@ function viewWindowIdForSocket(connectionId: string, socket: string): string | n
return null;
}
/** Focus the window already showing a session; false if it has none. */
function focusSessionWindow(control: ControlConnection, session: G2MirrorSession): boolean {
const windowId = viewWindowIdForSocket(control.config.id, session.socket);
if (!windowId) return false;
post({ type: "focus-window", windowId });
return true;
}
function openViewWindowFor(control: ControlConnection, session: G2MirrorSession): void {
openViewWindow(control, session.socket, sessionLabel(session));
}
/**
* Ask the shell to move a window to another height band, then run `apply` with
* the viewport it answers with: only the shell knows what a band comes to (the
* bottom switcher's height lives on the main thread), and a view's grid goes
* into its websocket handshake, so the size has to be right before it starts.
* Ignored if the window is already waiting on an answer (a repeated gesture).
*/
function changeWindowHeight(windowId: string, heightMode: WindowHeightMode, apply: (viewport: Viewport) => void): void {
if (pendingHeightChanges.has(windowId)) return;
pendingHeightChanges.set(windowId, apply);
post({ type: "set-height-mode", windowId, heightMode });
}
/** Make `incoming` what its window id shows, in place of `outgoing`. */
function replaceWindowContent(outgoing: TerminalWindow, incoming: TerminalWindow): void {
incoming.foreground = outgoing.foreground;
incoming.focused = outgoing.focused;
incoming.lastSubmittedFingerprint = "";
// Its pending timers and emulator callbacks must not paint over the surface.
outgoing.foreground = false;
windows.set(incoming.windowId, incoming);
updateHubAnimation();
renderAndSubmit(incoming, 0);
}
/** A session was selected in the list: show it in the hub's own window. */
function showSessionInHub(hub: HubWindow, control: ControlConnection, session: G2MirrorSession): void {
const options = clientOptionsFor(control.config);
if (!options) return;
const label = sessionLabel(session);
// Tall like a session in its own window (see openViewWindow).
changeWindowHeight(hub.windowId, "max", (viewport) => {
const view = createViewWindow(hub.windowId, hub.surfaceId, label, viewport, {
connectionId: control.config.id,
socket: session.socket,
label,
glyph: "",
options,
});
view.hub = hub;
if (hub.foreground) activeViewId = view.windowId;
replaceWindowContent(hub, view);
});
}
/**
* Back from a session in the hub's window to the list. The list keeps the order
* it had, with this session's row selected: viewing a session bumps its
* recency, and re-sorting on every return would shuffle the list while the user
* steps through it. `then` runs once the session's connection is gone.
*/
function returnToHub(view: ViewWindow, then?: () => void): void {
const hub = view.hub;
if (!hub) return;
changeWindowHeight(view.windowId, "min", (viewport) => {
stopViewConnection(view);
view.emulator.dispose();
if (activeViewId === view.windowId) activeViewId = null;
hub.viewportWidth = viewport.width;
hub.viewportHeight = viewport.height;
hub.menu?.resize(viewport);
const items = hubItems(hub);
const row = items.findIndex(
(item) => item.sessionRow?.control.config.id === view.connectionId && item.sessionRow.session.socket === view.socket,
);
hubMenu(hub).setItems(items, row >= 0 ? row : undefined);
replaceWindowContent(view, hub);
then?.();
});
}
/**
* Lowest unused sidebar-icon character for a new view window (its terminal
* icon shows ">N"). Glyphs free up when their window closes, since usage is