Merge remote-tracking branch 'upstream/main' into pr36-revive

This commit is contained in:
JMO committed 2026-09-26 19:29:19 -07:00
commit fb2a6a3759
58 files changed
+2405 -952

No files matched your search

+18 -31
View File
@@ -27,7 +27,6 @@ import { cardinalDirection, createCompassBackground, drawCompassRose, layoutComp
import { CompassCalibrationLayer } from "./calibration-layer";
import { getDeclinationAvailability, onDeclinationChanged, refreshDeclination } from "./declination";
import { getNorthReference, resolveHeading, setNorthReference, type NorthReference } from "./heading";
import { compassDebugLines, isCompassDebugEnabled, setCompassDebugEnabled } from "./debug";
export const COMPASS_WINDOW_ID = "compass";
export const COMPASS_SURFACE_ID = "window:compass";
@@ -92,11 +91,6 @@ class CompassLayer implements Layer {
this.requestRender();
}
toggleDebugInfo(): void {
setCompassDebugEnabled(!isCompassDebugEnabled());
this.requestRender();
}
stop(): void {
if (this.removed) return;
this.removed = true;
@@ -162,17 +156,9 @@ class CompassLayer implements Layer {
const headingText = heading === null ? "--°" : `${Math.round(heading)}° ${cardinalDirection(heading)}`;
const statusLines = wrapText(small, this.statusText(), width - STACK_GAP * 2);
const smallStep = lineStep(small);
const debugLines = isCompassDebugEnabled() ? compassDebugLines(this.diagnostics) : [];
const debugWidth = Math.max(0, ...debugLines.map((line) => small.measureText(line)));
const debugGap = debugLines.length ? 12 : 0;
// Keep diagnostics beside the heading even in narrow windows. The rose
// stays centred on the optical axis; the combined readout fits the viewport.
const headingFont = large.measureText(headingText) + debugGap + debugWidth <= width - EDGE_PAD * 2
? large : small;
const headingWidth = headingFont.measureText(headingText);
const readoutWidth = headingWidth + debugGap + debugWidth;
const debugHeight = debugLines.length ? (debugLines.length - 1) * smallStep + small.lineHeight : 0;
const readoutHeight = Math.max(headingFont.lineHeight, debugHeight);
const headingFont = large.measureText(headingText) <= width - EDGE_PAD * 2 ? large : small;
const readoutWidth = headingFont.measureText(headingText);
const readoutHeight = headingFont.lineHeight;
// One column centred on the display's true centre, so the rose sits where
// the wearer is looking rather than 32px right of it.
@@ -204,17 +190,14 @@ class CompassLayer implements Layer {
const image = this.background.image.clone();
drawCompassRose(image, cx, cy, radius, heading);
const readoutX = Math.round(Math.max(EDGE_PAD, Math.min(width - EDGE_PAD - readoutWidth, cx - readoutWidth / 2)));
image.drawText(headingFont, readoutX, y + Math.floor((readoutHeight - headingFont.lineHeight) / 2),
headingText, heading === null ? 150 : 255);
for (let i = 0; i < debugLines.length; i++) {
image.drawText(small, readoutX + headingWidth + debugGap,
y + Math.floor((readoutHeight - debugHeight) / 2) + i * smallStep, debugLines[i]!, 175);
}
image.drawText(headingFont, readoutX, y, headingText, heading === null ? 150 : 255);
y += readoutHeight + 4;
for (const line of statusLines) {
image.drawText(small, Math.round(cx - small.measureText(line) / 2), y, line, 125);
y += smallStep;
}
const accuracyText = magneticAccuracyText(this.diagnostics);
image.drawText(small, width - EDGE_PAD - small.measureText(accuracyText), TOP_PAD, accuracyText, 175);
return image;
}
@@ -255,14 +238,6 @@ export function createCompassAppWindow(options: InProcessAppOptions): InProcessW
layer.toggleNorthReference();
},
},
{
label: `Debug information: ${isCompassDebugEnabled() ? "On" : "Off"}`,
description: "Show magnetic accuracy (0–3), anomaly flags (0–2), and orientation source beside the heading.",
onSelect: (ctx) => {
ctx.stack.pop();
layer.toggleDebugInfo();
},
},
],
baseLayer: new YieldAtRootLayer(layer),
submitFrame: options.submitFrame,
@@ -299,3 +274,15 @@ function frameStatusText(): string {
return "Magnetic heading - waiting for location";
}
}
/**
* The firmware's magnetometer calibration readiness (0-3) as filled/empty
* dots, e.g. "Calibration: ●●○" for 2/3. This is the glasses' own sensor
* calibration, not the wearer-fit offset set on the calibration screen.
*/
function magneticAccuracyText(diagnostics: CompassDiagnostics | null): string {
const accuracy = diagnostics && (diagnostics.flags & 0x80) ? diagnostics.magneticAccuracy : -1;
if (accuracy < 0) return "Calibration: --";
const filled = Math.min(3, accuracy);
return `Calibration: ${"●".repeat(filled)}${"○".repeat(3 - filled)}`;
}
-21
View File
@@ -1,21 +0,0 @@
import type { CompassDiagnostics } from "../../native/compass";
import { getBooleanSetting, setBooleanSetting } from "../../native/settings-store";
const DEBUG_KEY = "compass.debugInfo";
export function isCompassDebugEnabled(): boolean {
return getBooleanSetting(DEBUG_KEY, false);
}
export function setCompassDebugEnabled(enabled: boolean): void {
setBooleanSetting(DEBUG_KEY, enabled);
}
export function compassDebugLines(diagnostics: CompassDiagnostics | null): string[] {
if (!diagnostics) return ["Diagnostics", "unavailable"];
if (!(diagnostics.flags & 0x80)) return ["Sample data", "unavailable"];
const accuracy = diagnostics.magneticAccuracy < 0 ? "--" : `${diagnostics.magneticAccuracy}/3`;
const anomalies = diagnostics.magneticAnomalies < 0 ? "--" : String(diagnostics.magneticAnomalies);
const source = ["--", "GRV", "GMRV", "RV"][diagnostics.orientationSource] ?? "--";
return [`Mag: ${accuracy}`, `Anom: ${anomalies}`, `Src: ${source}`];
}
+2 -2
View File
@@ -1,5 +1,6 @@
import { Application } from "@nativescript/core";
import { fetchWithUserAgent } from "../../util/http";
import { copyToJavaByteBuffer } from "../../native/java-direct-buffer";
declare const android: any;
declare const androidx: any;
@@ -93,8 +94,7 @@ async function downloadAndOpenPdf(url: string, appName: string, activity: any):
const file = new java.io.File(directory, `policy-${hash.toString(16)}.pdf`);
const stream = new java.io.FileOutputStream(file);
try {
const buffer = bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength);
stream.getChannel().write(buffer);
stream.getChannel().write(copyToJavaByteBuffer(bytes));
} finally {
stream.close();
}
+54 -316
View File
@@ -2,10 +2,10 @@ import { getDefaultSmallFont } from "../../graphics/ui-fonts";
import { truncateText, truncateLeft } from "../../graphics/textwrap";
import { GrayImage, type UiFont } from "../../graphics/image";
import { renderIcon, type IconName } from "../../graphics/icons";
import { clamp } from "../../util/numeric-util";
import { drawListScrollbar, drawSelectionHighlight, scrollToKeepSelectionVisible, type MenuItem } from "../../ui/menu";
import { type MenuItem } from "../../ui/menu";
import { Menu, type MenuDrawArgs } from "../../ui/menu-core";
import { centeredTextY, iconGridMinRowHeight, tightRowHeight } from "../../ui/metrics";
import { IconGrid, ICON_GRID_ICON_SIZE } from "../../ui/icon-grid";
import { centeredTextY, tightRowHeight } from "../../ui/metrics";
import { ConfigSettingEnum } from "../../ui/dashboard-settings";
import { getStringSetting, setStringSetting } from "../../native/settings-store";
import {
@@ -16,9 +16,8 @@ import {
statPath,
type DirectoryEntry,
} from "../../native/file-access";
import { directionalFallback, isWatchInput, type InputEvent } from "../../ui/gestures";
import { directionalFallback, type InputEvent } from "../../ui/gestures";
import { Layer, type LayerContext } from "../../ui/layers";
import { shell } from "../../ui/shell/shell";
const LIST_X = 20;
/** List view: the selection box extends this far left and right of the row text. */
@@ -29,10 +28,6 @@ function headerHeight(font: UiFont): number {
return 10 + font.lineHeight;
}
const GRID_COLS = 5;
const ICON_SIZE = 44;
const LABEL_GAP = 2;
export type FilesViewMode = "icons" | "list";
export const filesViewModeSetting = new ConfigSettingEnum<FilesViewMode>({
@@ -105,23 +100,14 @@ type EntryItem = {
type BrowserItem = { kind: "grant"; label: string } | { kind: "info"; label: string } | EntryItem;
type IconRow =
| { kind: "special"; item: BrowserItem; flatIndex: number }
| { kind: "grid"; items: EntryItem[]; firstIndex: number };
type IconMode = "row" | "item";
/**
* Filesystem browser with two views. The top "Places" level lists bookmarks
* and the storage roots; descending browses real directories. Icons view is a
* 5-column grid with launcher-style two-level selection (scroll picks a row,
* click drops into the row, scroll picks the item); list view is a flat list.
* and the storage roots; descending browses real directories. Icons view is
* the launcher's IconGrid (two-level ring selection, four-way watch
* selection; notice rows span the full width); list view is a flat list.
* Click descends into a directory or picks a supported file; double-click
* backs out one level (item selection, then parent directory, then Places,
* then leaving the browser). That is the ring's scheme; in icons view the
* watch (see handleIconsWatchInput) skips row mode and moves one cell at a
* time in four directions, exactly as in the launcher. Sized to its hosting
* stack.
* then leaving the browser). Sized to its hosting stack.
*/
export class FileBrowserLayer implements Layer {
// Watch swipes are spatial in icons view: up/down move between rows,
@@ -136,7 +122,7 @@ export class FileBrowserLayer implements Layer {
private entries: DirectoryEntry[] | null = null;
private listingFailed = false;
/** List view. Its selected index is the flat-row index both views translate through (setViewMode). */
/** List view. Both views index the same flatRows(), which is how setViewMode carries the selection across. */
private readonly listMenu = new Menu<BrowserItem>({
wrap: false,
rowGap: 1,
@@ -144,10 +130,18 @@ export class FileBrowserLayer implements Layer {
getHeight: () => tightRowHeight(getDefaultSmallFont()),
draw: (args) => this.drawListRow(args),
});
private selectedRow = 0;
private selectedCol = 0;
private iconMode: IconMode = "row";
private scrollRow = 0;
/** Icons view. */
private readonly grid = new IconGrid<BrowserItem>({
items: () => this.flatRows(),
isWide: (item) => item.kind !== "entry",
describe: (item, selected) =>
item.kind === "entry"
? { label: item.label, icon: gridIcon(item), labelValue: item.supported ? 210 : 100 }
: { label: item.label, labelValue: itemValue(item, selected) },
onActivate: (item, _index, ctx) => this.activateItem(item, ctx),
onBack: () => this.navigateUp(),
scrollbar: true,
});
constructor(private readonly options: FileBrowserOptions) {}
@@ -155,7 +149,6 @@ export class FileBrowserLayer implements Layer {
const font = getDefaultSmallFont();
const { width, height } = ctx.stack.getBaseSize();
const image = new GrayImage(width, height, 0);
const rows = this.flatRows();
image.drawText(font, 20, 8, "Files", 220);
const pathLabel = this.location === null ? "Places" : this.location;
@@ -163,17 +156,18 @@ export class FileBrowserLayer implements Layer {
image.drawText(font, pathX, 8, truncateLeft(font, pathLabel, width - pathX - 20), 130);
if (filesViewModeSetting.get() === "list") {
this.paintList(image, font, rows, ctx);
this.paintList(image, font, ctx);
} else {
this.paintIcons(image, font, rows, ctx);
const headerH = headerHeight(font);
this.grid.paint(image, { x: 0, y: headerH, width, height: height - headerH - 6 }, ctx.stack.isFocused());
}
return image;
}
private paintList(image: GrayImage, font: UiFont, rows: BrowserItem[], ctx: LayerContext): void {
private paintList(image: GrayImage, font: UiFont, ctx: LayerContext): void {
const { width, height } = ctx.stack.getBaseSize();
const headerH = headerHeight(font);
this.listMenu.setItems(rows);
this.listMenu.setItems(this.flatRows());
// Row boxes start one pixel above the header's bottom edge.
this.listMenu.paint(
image,
@@ -188,96 +182,19 @@ export class FileBrowserLayer implements Layer {
image.drawText(font, x + LIST_HIGHLIGHT_PAD, centeredTextY(font, y, height), label, itemValue(item, selected));
}
private paintIcons(image: GrayImage, font: UiFont, rows: BrowserItem[], ctx: LayerContext): void {
const { width, height } = ctx.stack.getBaseSize();
const focused = ctx.stack.isFocused();
const iconRows = buildIconRows(rows);
this.selectedRow = clamp(this.selectedRow, 0, Math.max(0, iconRows.length - 1));
const gridTop = headerHeight(font);
const gridBottom = height - 6;
const visibleRows = Math.max(1, Math.floor((gridBottom - gridTop) / iconGridMinRowHeight(font, ICON_SIZE, LABEL_GAP)));
const rowH = Math.floor((gridBottom - gridTop) / visibleRows);
const colW = width / GRID_COLS;
this.scrollRow = scrollToKeepSelectionVisible(this.scrollRow, this.selectedRow, visibleRows, iconRows.length);
const lastVisible = Math.min(iconRows.length, this.scrollRow + visibleRows);
for (let rowIndex = this.scrollRow; rowIndex < lastVisible; rowIndex++) {
const row = iconRows[rowIndex]!;
const y = gridTop + (rowIndex - this.scrollRow) * rowH;
const selected = rowIndex === this.selectedRow;
if (row.kind === "special") {
const textY = y + (((rowH - font.lineHeight) / 2) | 0);
if (selected) {
drawSelectionHighlight(image, LIST_X - 6, textY - 2, width - 2 * LIST_X + 12, font.lineHeight + 4, focused, 4);
}
image.drawText(font, LIST_X, textY, truncateText(font, row.item.label, width - 2 * LIST_X), itemValue(row.item, selected));
continue;
}
if (selected) {
// Same preview rule as the launcher: while defocused with the watch
// as the last-used source, show the cell a click would land on (watch
// focus enters item mode directly; see onFocus) instead of a row band
// the watch scheme never shows.
const cellHighlight = this.iconMode === "item" || (!focused && shell.lastInputWasWatch());
if (cellHighlight) {
this.selectedCol = clamp(this.selectedCol, 0, row.items.length - 1);
drawSelectionHighlight(image, this.selectedCol * colW + 4, y + 1, colW - 8, rowH - 2, focused, 6);
} else {
drawSelectionHighlight(image, 4, y + 1, width - 8, rowH - 2, focused, 6);
}
}
for (let col = 0; col < row.items.length; col++) {
const item = row.items[col]!;
const centerX = col * colW + colW / 2;
const blockTop = y + Math.max(2, (rowH - ICON_SIZE - font.lineHeight - LABEL_GAP) / 2);
const icon = renderIcon(item.icon, ICON_SIZE);
if (icon) {
image.bitBlt(item.supported ? icon : dimmedIcon(item.icon, icon), Math.round(centerX - icon.width / 2), Math.round(blockTop), {
transparentZero: true,
});
}
const label = truncateText(font, item.label, colW - 8);
const labelY = Math.round(blockTop + ICON_SIZE + LABEL_GAP);
image.drawText(font, Math.round(centerX - font.measureText(label) / 2), labelY, label, item.supported ? 210 : 100);
}
}
if (iconRows.length > visibleRows) {
drawListScrollbar(image, width - 5, gridTop, visibleRows * rowH - 4, this.scrollRow, visibleRows, iconRows.length);
}
}
async handleInput(event: InputEvent, ctx: LayerContext): Promise<void> {
if (filesViewModeSetting.get() === "list") {
// The flat list has no spatial meaning for swipes; give them the
// standard scroll / select / back meanings (the layer opted into
// directional delivery for the icons view, so the stack won't).
await this.handleListInput(directionalFallback(event), ctx);
} else if (isWatchInput(event)) {
await this.handleIconsWatchInput(event, ctx);
} else {
await this.handleIconsInput(event, ctx);
await this.grid.handleInput(event, ctx);
}
}
/**
* Focus arriving from the watch goes straight to item selection, as in the
* launcher: the watch has left/right swipes, so it never needs row mode.
* Any other source keeps the two-level scheme and enters in row mode.
*/
onFocus(lastInput: InputEvent | null): void {
if (filesViewModeSetting.get() !== "icons") return;
if (lastInput && isWatchInput(lastInput)) {
this.iconMode = "item";
const iconRows = buildIconRows(this.flatRows());
this.clampColToRow(iconRows[clamp(this.selectedRow, 0, Math.max(0, iconRows.length - 1))]);
} else {
this.iconMode = "row";
}
if (filesViewModeSetting.get() === "icons") this.grid.onFocus(lastInput);
}
private async handleListInput(event: InputEvent, ctx: LayerContext): Promise<void> {
@@ -300,147 +217,6 @@ export class FileBrowserLayer implements Layer {
}
}
private async handleIconsInput(event: InputEvent, ctx: LayerContext): Promise<void> {
const rows = this.flatRows();
const iconRows = buildIconRows(rows);
this.selectedRow = clamp(this.selectedRow, 0, Math.max(0, iconRows.length - 1));
const row = iconRows[this.selectedRow];
switch (event.type) {
case "scroll-up":
case "scroll-down": {
const delta = event.type === "scroll-down" ? 1 : -1;
if (this.iconMode === "item") {
// Item selection traverses linearly: past a row's edge it continues
// onto the adjacent row (a special row counts as a single item).
if (row?.kind === "grid") {
const next = clamp(this.selectedCol, 0, row.items.length - 1) + delta;
if (next >= 0 && next < row.items.length) {
this.selectedCol = next;
return;
}
}
const adjacentIndex = this.selectedRow + delta;
if (adjacentIndex < 0 || adjacentIndex >= iconRows.length) return;
this.selectedRow = adjacentIndex;
const adjacent = iconRows[adjacentIndex]!;
if (adjacent.kind === "grid") {
this.selectedCol = delta > 0 ? 0 : adjacent.items.length - 1;
}
} else {
this.selectedRow = clamp(this.selectedRow + delta, 0, Math.max(0, iconRows.length - 1));
}
return;
}
case "click": {
if (!row) return;
if (row.kind === "special") {
await this.activateItem(row.item, ctx);
return;
}
if (this.iconMode === "row") {
this.iconMode = "item";
// Default to the middle column (clamped to the row's item count).
this.selectedCol = Math.min(Math.floor(GRID_COLS / 2), row.items.length - 1);
} else {
const item = row.items[clamp(this.selectedCol, 0, row.items.length - 1)];
if (item) await this.activateItem(item, ctx);
}
return;
}
case "double-click":
if (this.iconMode === "item") {
this.iconMode = "row";
} else {
this.navigateUp();
}
return;
default:
return;
}
}
/**
* The watch's scheme for the icons view — the launcher's: there is no row
* mode, the selection is always one cell (a special row counts as its
* single full-width item). Up/down (and the crown) move between rows
* keeping the column, right/left move within the row, and left from the
* leftmost column keeps going out — parent directory, Places, then the
* sidebar. Row mode is restored on the way out so a ring user finds the
* grid as they left it.
*/
private async handleIconsWatchInput(event: InputEvent, ctx: LayerContext): Promise<void> {
const iconRows = buildIconRows(this.flatRows());
this.selectedRow = clamp(this.selectedRow, 0, Math.max(0, iconRows.length - 1));
const row = iconRows[this.selectedRow];
if (this.iconMode === "row") {
this.iconMode = "item";
this.clampColToRow(row);
}
switch (event.type) {
case "swipe-up":
case "swipe-down":
case "scroll-up":
case "scroll-down": {
const delta = event.type === "swipe-down" || event.type === "scroll-down" ? 1 : -1;
this.selectedRow = clamp(this.selectedRow + delta, 0, Math.max(0, iconRows.length - 1));
// Keep the column; a shorter row (or a special one) clamps it.
this.clampColToRow(iconRows[this.selectedRow]);
return;
}
case "swipe-right":
if (row?.kind === "grid") {
this.selectedCol = clamp(this.selectedCol + 1, 0, row.items.length - 1);
}
return;
case "swipe-left":
if (row?.kind === "grid" && this.selectedCol > 0) {
this.selectedCol--;
} else {
this.watchNavigateUp();
}
return;
case "click": {
if (!row) return;
if (row.kind === "special") {
await this.activateItem(row.item, ctx);
} else {
const item = row.items[clamp(this.selectedCol, 0, row.items.length - 1)];
if (item) await this.activateItem(item, ctx);
}
// Descending into a directory resets the selection for the ring (row
// mode at the top); the watch scheme stays in single-item selection.
this.iconMode = "item";
return;
}
case "double-click":
this.watchNavigateUp();
return;
default:
return;
}
}
private clampColToRow(row: IconRow | undefined): void {
if (row?.kind === "grid") {
this.selectedCol = clamp(this.selectedCol, 0, row.items.length - 1);
}
}
/**
* navigateUp for the watch scheme: inside the browser the selection stays
* in item mode (resetSelection/selectPath put it back in row mode for the
* ring); leaving from Places restores row mode on the way out.
*/
private watchNavigateUp(): void {
if (this.location === null) {
this.iconMode = "row";
this.options.onLeave();
return;
}
this.navigateUp();
this.iconMode = "item";
}
/**
* App-specific entries for the window long-press menu: the view switch,
* plus bookmark/unbookmark for the selected entry (in icons view only once
@@ -480,34 +256,23 @@ export class FileBrowserLayer implements Layer {
this.listMenu.setItems(rows);
return this.listMenu.selectedItem;
}
const iconRows = buildIconRows(rows);
const row = iconRows[clamp(this.selectedRow, 0, iconRows.length - 1)];
if (!row) return null;
if (row.kind === "special") return row.item;
if (this.iconMode !== "item") return null;
return row.items[clamp(this.selectedCol, 0, row.items.length - 1)] ?? null;
return this.grid.selectedItem;
}
private setViewMode(mode: FilesViewMode): void {
if (filesViewModeSetting.get() === mode) return;
const rows = this.flatRows();
const iconRows = buildIconRows(rows);
this.listMenu.setItems(rows);
this.listMenu.setItems(this.flatRows());
if (mode === "list") {
// Carry the icon-grid selection into the flat list (clamped column when
// only a row was selected).
const row = iconRows[clamp(this.selectedRow, 0, Math.max(0, iconRows.length - 1))];
if (row) {
this.listMenu.select(
row.kind === "special" ? row.flatIndex : row.firstIndex + clamp(this.selectedCol, 0, row.items.length - 1),
);
}
// Carry the icon-grid selection into the flat list (the remembered
// column when only a row was selected).
const index = this.grid.cursorIndex;
if (index !== null) this.listMenu.select(index);
} else {
this.setGridSelectionFromFlatIndex(rows, this.listMenu.selectedIndex ?? 0);
this.iconMode = "row";
const index = this.listMenu.selectedIndex ?? 0;
this.grid.resetSelection();
this.grid.selectIndex(index);
}
filesViewModeSetting.set(mode);
this.scrollRow = 0;
this.listMenu.scrollTop = 0;
}
@@ -534,11 +299,12 @@ export class FileBrowserLayer implements Layer {
this.resetSelection();
}
private navigateUp(): void {
/** Back out one level; true when that left the browser (from Places). */
private navigateUp(): boolean {
const from = this.location;
if (from === null) {
this.options.onLeave();
return;
return true;
}
if (from === this.navRoot || from === "/") {
this.location = null;
@@ -546,20 +312,18 @@ export class FileBrowserLayer implements Layer {
this.resetSelection();
this.selectPath(this.navRoot ?? from);
this.navRoot = null;
return;
return false;
}
const parentRaw = from.slice(0, from.lastIndexOf("/"));
this.navigateTo(parentRaw.length ? parentRaw : "/");
this.selectPath(from);
return false;
}
private resetSelection(): void {
this.listMenu.select(0);
this.listMenu.scrollTop = 0;
this.selectedRow = 0;
this.selectedCol = 0;
this.iconMode = "row";
this.scrollRow = 0;
this.grid.resetSelection();
}
/** Select the item with the given path (e.g. the directory just left). */
@@ -570,27 +334,17 @@ export class FileBrowserLayer implements Layer {
// Items first, then the selection, so the viewport scrolls minimally from the top.
this.listMenu.setItems(rows);
this.listMenu.select(index);
this.setGridSelectionFromFlatIndex(rows, index);
}
private setGridSelectionFromFlatIndex(rows: BrowserItem[], index: number): void {
const specials = leadingSpecialCount(rows);
if (index < specials) {
this.selectedRow = index;
this.selectedCol = 0;
return;
}
const ordinal = index - specials;
this.selectedRow = specials + Math.floor(ordinal / GRID_COLS);
this.selectedCol = ordinal % GRID_COLS;
this.grid.selectIndex(index);
}
private flatRows(): BrowserItem[] {
const rows: BrowserItem[] = [];
if (!hasAllFilesAccess()) {
rows.push({ kind: "grant", label: "Grant file access (opens phone Settings)" });
}
if (this.location === null) {
// Only on Places: without All Files access some folders (e.g. Download)
// may still be readable, and the prompt shouldn't crowd their listings.
if (!hasAllFilesAccess()) {
rows.push({ kind: "grant", label: "Grant file access (opens phone Settings)" });
}
for (const path of getBookmarkedPaths()) {
rows.push(this.bookmarkItem(path));
}
@@ -663,27 +417,6 @@ export class FileBrowserLayer implements Layer {
}
}
function buildIconRows(flat: BrowserItem[]): IconRow[] {
const rows: IconRow[] = [];
let index = 0;
// Special rows (grant prompt, empty/unreadable info) lead the list; each
// gets a full-width row of its own. Entries follow, chunked into grid rows.
while (index < flat.length && flat[index]!.kind !== "entry") {
rows.push({ kind: "special", item: flat[index]!, flatIndex: index });
index++;
}
for (; index < flat.length; index += GRID_COLS) {
rows.push({ kind: "grid", items: flat.slice(index, index + GRID_COLS) as EntryItem[], firstIndex: index });
}
return rows;
}
function leadingSpecialCount(rows: BrowserItem[]): number {
let count = 0;
while (count < rows.length && rows[count]!.kind !== "entry") count++;
return count;
}
function itemValue(item: BrowserItem, selected: boolean): number {
if (item.kind === "info") return 120;
if (item.kind === "grant") return selected ? 255 : 210;
@@ -691,6 +424,11 @@ function itemValue(item: BrowserItem, selected: boolean): number {
return selected ? 255 : 200;
}
function gridIcon(item: EntryItem): GrayImage | null {
const icon = renderIcon(item.icon, ICON_GRID_ICON_SIZE);
return icon && !item.supported ? dimmedIcon(item.icon, icon) : icon;
}
// Unsupported files draw their grid icon at half brightness; rendered once
// per icon name and cached (all grid icons share one size).
const dimmedIconCache = new Map<IconName, GrayImage>();
+53 -275
View File
@@ -4,11 +4,10 @@ import { truncateText } from "../../graphics/textwrap";
import { GrayImage } from "../../graphics/image";
import { type Plane } from "../../graphics/plane";
import { renderIcon, type IconName } from "../../graphics/icons";
import { clamp } from "../../util/numeric-util";
import { InputEvent, isWatchInput } from "../../ui/gestures";
import { InputEvent } from "../../ui/gestures";
import { IconGrid, ICON_GRID_ICON_SIZE } from "../../ui/icon-grid";
import { Layer, LayerActions, LayerContext } from "../../ui/layers";
import { drawSelectionHighlight, MenuLayer, scrollToKeepSelectionVisible, type MenuItem } from "../../ui/menu";
import { iconGridMinRowHeight } from "../../ui/metrics";
import { MenuLayer, type MenuItem } from "../../ui/menu";
import { WINDOW_MENU_LAYOUT } from "../../ui/window-menu";
import { onAnySettingChanged } from "../../ui/dashboard-settings";
import { createInProcessWindow } from "../../ui/shell/in-process-window";
@@ -45,12 +44,7 @@ export type LauncherOptions = {
export const LAUNCHER_WINDOW_ID = "launcher";
export const LAUNCHER_SURFACE_ID = "window:launcher";
const COLS = 5;
const GRID_TOP = 6;
const ICON_SIZE = 44;
const LABEL_GAP = 2;
type LauncherMode = "row" | "item";
/** One cell of the grid: an app, or a folder holding some of the apps. */
type LauncherGridEntry =
@@ -58,15 +52,11 @@ type LauncherGridEntry =
| { kind: "folder"; label: string; name: string };
/**
* The launcher grid: app and folder icons with labels, arranged in a grid.
* Navigation has two levels so the max number of swipes to any app is halved:
* entering from the sidebar starts in "row" mode (scroll picks a row); a tap
* drops into "item" mode on that row, defaulting to the middle column (scroll
* picks the item); a tap launches an app or opens a folder (the same grid,
* restricted to that folder's apps). Double-click backs out one level (item →
* row, folder → top grid), and from the top level yields to the sidebar.
* That is the ring's scheme; the watch (see handleWatchInput) skips row mode
* and moves one cell at a time in four directions.
* The launcher grid: app and folder icons with labels, in an IconGrid (which
* owns layout, selection and the ring/watch navigation schemes). Opening a
* folder shows the same grid restricted to that folder's apps; backing out
* of a folder returns to the top grid, and from the top grid yields to the
* sidebar.
*
* The folder grouping lives in the settings store (see launcher-folders.ts)
* and is re-read every paint, so assistant folder tools take effect without
@@ -78,28 +68,24 @@ class LauncherGridLayer implements Layer {
// open folder if there is one, else yields to the sidebar — "left" points
// toward the sidebar all the way out.
readonly acceptsDirectional = true;
private mode: LauncherMode = "row";
private selectedRow = 0;
private selectedCol = 1;
private scrollRow = 0;
/** Folder whose contents the grid is showing, or null for the top grid. */
private currentFolder: string | null = null;
private readonly grid = new IconGrid<LauncherGridEntry>({
items: () => this.entries(),
describe: (entry) => ({
label: entry.label,
icon: entry.kind === "app"
? entry.renderIcon?.(ICON_GRID_ICON_SIZE) ?? renderIcon(entry.icon, ICON_GRID_ICON_SIZE)
: renderIcon("folder-filled", ICON_GRID_ICON_SIZE),
}),
onActivate: (entry) => this.openEntry(entry),
onBack: () => this.back(),
});
constructor(private readonly options: LauncherOptions) {}
/**
* Focus arriving from the watch goes straight to item selection: the watch
* has left/right swipes, so it never needs row mode (see handleWatchInput),
* and starting there would paint a row band its scheme can't produce. Any
* other source keeps the two-level scheme and enters in row mode.
*/
onFocus(lastInput: InputEvent | null): void {
if (lastInput && isWatchInput(lastInput)) {
this.mode = "item";
this.selectedCol = clamp(this.selectedCol, 0, this.itemsInRow(this.entries().length, this.selectedRow) - 1);
} else {
this.mode = "row";
}
this.grid.onFocus(lastInput);
}
/**
@@ -150,22 +136,38 @@ class LauncherGridLayer implements Layer {
return entries.sort((a, b) => a.label.localeCompare(b.label));
}
private rowCount(entryCount: number): number {
return Math.max(1, Math.ceil(entryCount / COLS));
}
private itemsInRow(entryCount: number, row: number): number {
return Math.max(0, Math.min(COLS, entryCount - row * COLS));
}
/** Leave the current folder, putting the selection back on its cell. */
private exitFolder(): void {
const folder = this.currentFolder;
this.currentFolder = null;
this.mode = "row";
this.grid.resetSelection();
const index = this.entries().findIndex((entry) => entry.kind === "folder" && entry.name === folder);
this.selectedRow = index >= 0 ? Math.floor(index / COLS) : 0;
this.selectedCol = index >= 0 ? index % COLS : 0;
if (index >= 0) this.grid.selectIndex(index);
}
/** Enter a folder or launch an app; true when that left the grid (a launch). */
private async openEntry(entry: LauncherGridEntry): Promise<boolean> {
if (entry.kind === "folder") {
this.currentFolder = entry.name;
this.grid.resetSelection();
return false;
}
// Return to the top grid before launching, so the launcher never
// shows (even briefly) stale folder contents when re-entered.
if (this.currentFolder !== null) this.exitFolder();
this.grid.enterRowMode();
await this.options.launchApp(entry.appId);
return true;
}
/** Leave the open folder, else yield to the sidebar; true when that left the grid. */
private back(): boolean {
if (this.currentFolder !== null) {
this.exitFolder();
return false;
}
shell.yieldFocusToSidebar();
return true;
}
/**
@@ -174,8 +176,7 @@ class LauncherGridLayer implements Layer {
* so anywhere else the menu falls back to just the defaults.
*/
menuItems(): MenuItem[] {
if (this.mode !== "item") return [];
const entry = this.entries()[this.selectedRow * COLS + this.selectedCol];
const entry = this.grid.selectedItem;
if (!entry || entry.kind !== "app") return [];
return [
{
@@ -202,7 +203,7 @@ class LauncherGridLayer implements Layer {
confirmCtx.stack.pop();
setAppFolder(entry.appId, null);
await this.options.uninstallApp(entry.appId);
this.mode = "row";
this.grid.enterRowMode();
},
},
{ label: "Cancel", onSelect: (confirmCtx) => confirmCtx.stack.pop() },
@@ -257,246 +258,23 @@ class LauncherGridLayer implements Layer {
const font = getDefaultSmallFont();
const { width, height } = ctx.stack.getBaseSize();
const image = new GrayImage(width, height, 0);
const focused = ctx.stack.isFocused();
const entries = this.entries();
const rows = this.rowCount(entries.length);
this.selectedRow = clamp(this.selectedRow, 0, rows - 1);
this.selectedCol = clamp(
this.selectedCol,
0,
Math.max(0, this.itemsInRow(entries.length, this.selectedRow) - 1),
);
// The folder-name header band scales with the font.
const gridTop = this.currentFolder !== null ? GRID_TOP + font.lineHeight + 4 : GRID_TOP;
if (this.currentFolder !== null) {
image.drawText(font, 8, GRID_TOP - 2, truncateText(font, this.currentFolder, width - 16), 160);
}
const gridBottom = height - 4;
// Rows are exactly as tall as their content (icon + label + breathing
// room), so row height tracks the font instead of stretching to fill the
// viewport. Leftover space below the last full row shows the top of the
// next row when there are more apps to scroll to.
const rowH = iconGridMinRowHeight(font, ICON_SIZE, LABEL_GAP);
const fullRows = Math.max(1, Math.floor((gridBottom - gridTop) / rowH));
const colW = width / COLS;
// Scroll to keep the selected row among the fully-visible rows.
this.scrollRow = scrollToKeepSelectionVisible(this.scrollRow, this.selectedRow, fullRows, rows);
const rowY = (row: number) => gridTop + (row - this.scrollRow) * rowH;
// Selection highlight: a row band, or a single cell in item mode. While
// defocused with the watch as the last-used source, the cell outline is
// shown even in row mode: watch focus enters item mode directly (see
// onFocus), so the outline previews the cell a click would land on
// rather than a row band the watch scheme never shows.
const cellHighlight = this.mode === "item" || (!focused && shell.lastInputWasWatch());
const selY = rowY(this.selectedRow);
if (cellHighlight) {
drawSelectionHighlight(image, this.selectedCol * colW + 6, selY + 2, colW - 12, rowH - 4, focused, 6);
} else {
drawSelectionHighlight(image, 4, selY + 2, width - 8, rowH - 4, focused, 6);
}
for (let index = 0; index < entries.length; index++) {
const entry = entries[index]!;
const row = Math.floor(index / COLS);
if (row < this.scrollRow) continue;
const blockTop = rowY(row) + Math.max(2, (rowH - ICON_SIZE - font.lineHeight - LABEL_GAP) / 2);
if (blockTop >= gridBottom) break; // fully below the grid
const centerX = (index % COLS) * colW + colW / 2;
const icon = entry.kind === "app"
? entry.renderIcon?.(ICON_SIZE) ?? renderIcon(entry.icon, ICON_SIZE)
: renderIcon("folder-filled", ICON_SIZE);
if (icon) {
// Clip the icon at the grid bottom so a peeking row shows only its top.
// A fully visible icon goes through the deferred-image path (texture
// cacheable); the clipped peeking row falls back to a raster blit,
// since cached draws are whole-image only.
// Icons narrower/shorter than ICON_SIZE (a non-square logo scaled to
// fit) are centered in the square the row reserves for them.
const iconX = Math.round(centerX - icon.width / 2);
const iconY = Math.round(blockTop + Math.max(0, (ICON_SIZE - icon.height) / 2));
const clipHeight = Math.min(icon.height, Math.floor(gridBottom - iconY));
if (clipHeight >= icon.height) {
image.drawImage(icon, iconX, iconY);
} else if (clipHeight > 0) {
image.bitBlt(icon, iconX, iconY, { height: clipHeight, transparentZero: true });
}
}
const labelY = Math.round(blockTop + ICON_SIZE + LABEL_GAP);
if (labelY + font.lineHeight <= gridBottom) {
const label = truncateText(font, entry.label, colW - 8);
image.drawText(font, Math.round(centerX - font.measureText(label) / 2), labelY, label, 210);
}
}
this.grid.paint(image, { x: 0, y: gridTop, width, height: gridBottom - gridTop }, ctx.stack.isFocused());
return image;
}
async handleInput(event: InputEvent, ctx: LayerContext): Promise<void> {
if (isWatchInput(event)) {
await this.handleWatchInput(event, ctx);
return;
}
const entries = this.entries();
const rows = this.rowCount(entries.length);
switch (event.type) {
case "scroll-up":
case "scroll-down": {
const delta = event.type === "scroll-down" ? 1 : -1;
if (this.mode === "row") {
this.selectedRow = clamp(this.selectedRow + delta, 0, rows - 1);
} else {
// Item selection traverses the grid linearly: past a row's edge it
// continues onto the adjacent row (stopping at the grid's ends).
const itemCount = this.itemsInRow(entries.length, this.selectedRow);
const next = clamp(this.selectedCol, 0, itemCount - 1) + delta;
if (next >= 0 && next < itemCount) {
this.selectedCol = next;
} else if (next < 0 && this.selectedRow > 0) {
this.selectedRow--;
this.selectedCol = this.itemsInRow(entries.length, this.selectedRow) - 1;
} else if (next >= itemCount && this.selectedRow < rows - 1) {
this.selectedRow++;
this.selectedCol = 0;
}
}
return;
}
case "click": {
if (this.mode === "row") {
this.mode = "item";
// Default to the middle column (clamped to the row's item count).
this.selectedCol = Math.min(
Math.floor(COLS / 2),
this.itemsInRow(entries.length, this.selectedRow) - 1,
);
} else {
await this.openSelected(entries);
}
return;
}
case "double-click":
if (this.mode === "item") {
this.mode = "row";
} else if (this.currentFolder !== null) {
this.exitFolder();
} else {
shell.yieldFocusToSidebar();
}
return;
default:
return;
}
await this.grid.handleInput(event, ctx);
}
/**
* The watch's scheme: there is no row mode to enter first. The selection is
* always one cell; up/down/left/right (and the crown) move it spatially,
* select opens it, back leaves the folder or the grid, and stepping left
* past the first column leaves for the sidebar, which is where "left" goes.
* Row mode is restored on the way out so a ring user finds the grid as
* they left it.
*/
private async handleWatchInput(event: InputEvent, ctx: LayerContext): Promise<void> {
const entries = this.entries();
const rows = this.rowCount(entries.length);
if (this.mode === "row") {
this.mode = "item";
this.selectedCol = clamp(this.selectedCol, 0, this.itemsInRow(entries.length, this.selectedRow) - 1);
}
const leave = () => {
this.mode = "row";
shell.yieldFocusToSidebar();
};
switch (event.type) {
case "swipe-up":
case "swipe-down":
case "scroll-up":
case "scroll-down": {
const delta = event.type === "swipe-down" || event.type === "scroll-down" ? 1 : -1;
this.selectedRow = clamp(this.selectedRow + delta, 0, rows - 1);
// Keep the column; a shorter last row clamps it.
this.selectedCol = clamp(this.selectedCol, 0, this.itemsInRow(entries.length, this.selectedRow) - 1);
return;
}
case "swipe-right":
this.selectedCol = clamp(this.selectedCol + 1, 0, this.itemsInRow(entries.length, this.selectedRow) - 1);
return;
case "swipe-left":
if (this.selectedCol > 0) {
this.selectedCol--;
} else if (this.currentFolder !== null) {
this.exitFolder();
this.mode = "item";
} else {
leave();
}
return;
case "click":
await this.openSelected(entries, true);
return;
case "double-click":
if (this.currentFolder !== null) {
this.exitFolder();
this.mode = "item";
} else {
leave();
}
return;
default:
return;
}
}
/**
* A touch on the phone's mirror: the cell under it becomes the selection
* and opens (the same as a watch select on it). Uses the geometry paint
* lays the grid out with, so it lands on what the mirror showed.
*/
/** A touch on the phone's mirror opens the cell under it. */
async hitTest(x: number, y: number, ctx: LayerContext): Promise<boolean> {
const font = getDefaultSmallFont();
const { width, height } = ctx.stack.getBaseSize();
const entries = this.entries();
const gridTop = this.currentFolder !== null ? GRID_TOP + font.lineHeight + 4 : GRID_TOP;
const gridBottom = height - 4;
if (y < gridTop || y >= gridBottom || x < 0 || x >= width) return false;
const rowH = iconGridMinRowHeight(font, ICON_SIZE, LABEL_GAP);
const colW = width / COLS;
const row = this.scrollRow + Math.floor((y - gridTop) / rowH);
const col = Math.floor(x / colW);
if (row < 0 || row >= this.rowCount(entries.length)) return false;
if (col < 0 || col >= this.itemsInRow(entries.length, row)) return false;
this.mode = "item";
this.selectedRow = row;
this.selectedCol = col;
await this.openSelected(entries);
return true;
}
/**
* Open the selected cell: enter a folder, or launch the app. A folder
* entered by watch input stays in single-item selection (the watch scheme
* has no row mode), starting at the folder's first cell.
*/
private async openSelected(entries: readonly LauncherGridEntry[], watch = false): Promise<void> {
const entry = entries[this.selectedRow * COLS + this.selectedCol];
if (!entry) return;
if (entry.kind === "folder") {
this.currentFolder = entry.name;
this.mode = watch ? "item" : "row";
this.selectedRow = 0;
this.selectedCol = watch ? 0 : this.selectedCol;
this.scrollRow = 0;
} else {
// Return to the top grid before launching, so the launcher never
// shows (even briefly) stale folder contents when re-entered.
if (this.currentFolder !== null) this.exitFolder();
this.mode = "row";
await this.options.launchApp(entry.appId);
}
return await this.grid.hitTest(x, y, ctx);
}
}
+45 -8
View File
@@ -2,7 +2,8 @@
* Terminal app, hosted in its own worker thread. Window model:
* - "terminal:hub": the window opened from the launcher; shows the list of
* live g2mirror sessions across every connected host (grouped by host when
* more than one is connected), and hosts the Manage Connections section
* more than one is connected; selecting a 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
@@ -1047,10 +1048,7 @@ function windowMenuItems(window: TerminalWindow): MenuItem[] {
label,
onSelect: (ctx) => {
ctx.stack.pop();
launchAndOpenView(control, preset).catch((error) => {
// The hub status line also shows the server's error message.
console.warn(`terminal launch ${preset} failed: ${error}`);
});
launchFromUi(control, preset);
},
});
}
@@ -1180,7 +1178,10 @@ function applyHistoryReply(window: ViewWindow, reply: { start: number; oldest: n
type HubItem = {
label: string;
/** Group heading (host name): drawn differently and skipped by selection. */
/**
* Group heading (host name): drawn unindented and dimmer. Selectable only
* when it has an onSelect (launch a shell on that host).
*/
heading?: boolean;
/** Session with recent output: an animated indicator marks the row. */
active?: boolean;
@@ -1210,7 +1211,7 @@ function hubMenu(window: HubWindow): Menu<HubItem> {
function drawHubRow({ image, item, x, y, width, selected }: MenuDrawArgs<HubItem>): void {
const font = chromeFont();
if (item.heading) {
image.drawText(font, x, y + HUB_ROW_TEXT_INSET, item.label, 140);
image.drawText(font, x, y + HUB_ROW_TEXT_INSET, item.label, selected ? 255 : 140);
return;
}
// Activity gutter, open-window number column, then the label, truncated
@@ -1282,9 +1283,14 @@ function hubSessionItems(window: HubWindow): HubItem[] {
];
const connected = connectedControls();
const multiHost = connected.length > 1;
const canLaunch = launchPresetNames().length > 0;
for (const control of connected) {
if (multiHost) {
items.push({ label: connectionDisplayName(control.config), heading: true });
items.push({
label: connectionDisplayName(control.config),
heading: true,
onSelect: canLaunch ? () => launchOnHost(window, control) : undefined,
});
}
const sessions = orderedSessions(window, control);
for (const session of sessions) {
@@ -1568,6 +1574,37 @@ function launchPresetNames(): string[] {
return names;
}
/**
* A host heading in the hub was selected: launch a shell there. With a single
* launch preset it starts right away; with several, a submenu titled with the
* host's name picks which.
*/
function launchOnHost(window: HubWindow, control: ControlConnection): void {
const presets = launchPresetNames();
if (presets.length === 1) {
launchFromUi(control, presets[0]!);
return;
}
windowMenu(window).open(
presets.map((preset) => ({
label: `Launch ${preset}`,
onSelect: (ctx) => {
ctx.stack.pop();
launchFromUi(control, preset);
},
})),
connectionDisplayName(control.config),
);
}
/** Launch started from a menu or list row; failures only get logged. */
function launchFromUi(control: ControlConnection, preset: string): void {
launchAndOpenView(control, preset).catch((error) => {
// The hub status line also shows the server's error message.
console.warn(`terminal launch ${preset} failed: ${error}`);
});
}
/** Launch a preset on a host and open a view window on the new session. */
async function launchAndOpenView(control: ControlConnection, preset: string): Promise<string> {
if (!control.client) throw new Error("Not connected to the g2mirror server.");
+32 -14
View File
@@ -510,7 +510,7 @@ class DashboardController {
window.relayout();
await this.configureWindowSurface(
window.surfaceId,
window.windowId === foregroundWindowId,
this.isForegroundWindow(window.windowId),
window.heightMode,
);
} else if (window.closeable) {
@@ -548,7 +548,7 @@ class DashboardController {
void (async () => {
for (const window of Array.from(shell.getWindows())) {
if (!changed.includes(window.appId)) continue;
await this.configureWindowSurface(window.surfaceId, window.windowId === shell.foregroundWindow()?.windowId, window.heightMode);
await this.configureWindowSurface(window.surfaceId, this.isForegroundWindow(window.windowId), window.heightMode);
window.relayout?.();
}
shell.foregroundWindow()?.requestRender();
@@ -560,12 +560,11 @@ class DashboardController {
const position = verticalPositionSetting.get();
if (position === this.lastVerticalPosition) return;
this.lastVerticalPosition = position;
const foregroundWindowId = shell.foregroundWindow()?.windowId;
void (async () => {
for (const window of shell.getWindows()) {
await this.configureWindowSurface(
window.surfaceId,
window.windowId === foregroundWindowId,
this.isForegroundWindow(window.windowId),
window.heightMode,
);
}
@@ -1021,7 +1020,13 @@ class DashboardController {
return this.glassesDisplayLabel();
}
if (this.silentMode && this.phase === "connected") {
return "Connected (Silent mode enabled)";
// Say how to leave it. Silent mode is entered and exited only by the
// same gesture on the glasses, the firmware ignores all input and blanks
// the display while it is on, and nothing here can turn it off -- there
// is no setter, only onSilentMode. So a user who triggered it by
// accident sees a connected pair of glasses that answers nothing, with
// no way to find out why. The instruction belongs where they are looking.
return "Connected (Silent mode — long-press both touchpads on the glasses to exit)";
}
// The preview compositor keeps the mirror live (including as a black
// frame while the simulated screen is off), so never cover it.
@@ -1243,11 +1248,10 @@ class DashboardController {
// loop re-applies the current dim from here.
await target.setUnderlayDim(SHELL_SURFACE_Z_ORDER, 1);
this.appliedUnderlayDim = 1;
const foregroundWindowId = shell.foregroundWindow()?.windowId;
for (const window of shell.getWindows()) {
await this.configureWindowSurface(
window.surfaceId,
window.windowId === foregroundWindowId,
this.isForegroundWindow(window.windowId),
window.heightMode,
target,
);
@@ -1532,11 +1536,10 @@ class DashboardController {
await communicator.setUnderlayDim(SHELL_SURFACE_Z_ORDER, 1);
this.appliedUnderlayDim = 1;
await this.configureLockSurface(communicator);
const foregroundWindowId = shell.foregroundWindow()?.windowId;
for (const window of shell.getWindows()) {
await this.configureWindowSurface(
window.surfaceId,
window.windowId === foregroundWindowId,
this.isForegroundWindow(window.windowId),
window.heightMode,
);
}
@@ -2270,8 +2273,7 @@ class DashboardController {
reconfigureSurface: (heightMode) => {
// Resize the surface rect to the new band; a foreground window stays
// visible. The shell re-renders so the chrome (top bar) follows.
const visible = shell.foregroundWindow()?.windowId === windowId;
void this.configureWindowSurface(surfaceId, visible, heightMode);
void this.configureWindowSurface(surfaceId, this.isForegroundWindow(windowId), heightMode);
this.requestShellRender();
},
onClosed: () => {
@@ -2424,10 +2426,21 @@ class DashboardController {
}
}
/** Create/refresh a window surface on the compositor, if a display target exists. */
/**
* Create/refresh a window surface on the compositor, if a display target exists.
*
* `visible` may be a function, asked only after the surface is configured,
* right when the visibility call is queued. Callers that loop over windows
* or resize one in place must pass one (isForegroundWindow): the configure
* call yields, the foreground can change meanwhile, and a visibility decided
* before the await then lands AFTER the focus change's own calls, since the
* bridge runs Java calls in order. At startup, restoreOpenApps focuses
* windows while connect() registers surfaces, so a foreground read before
* that loop left the old window visible and the new foreground hidden.
*/
private async configureWindowSurface(
surfaceId: string,
visible: boolean,
visible: boolean | (() => boolean),
heightMode: WindowHeightMode = "min",
// ensurePreviewDisplay passes its not-yet-published target explicitly.
target: DisplayTarget | null = this.display,
@@ -2438,7 +2451,12 @@ class DashboardController {
zOrder: 0,
transparency: "opaque",
});
await target.setSurfaceVisible(surfaceId, visible);
await target.setSurfaceVisible(surfaceId, typeof visible === "function" ? visible() : visible);
}
/** A late answer to "is this window in front?", for configureWindowSurface. */
private isForegroundWindow(windowId: string): () => boolean {
return () => shell.foregroundWindow()?.windowId === windowId;
}
private removeWindowSurface(surfaceId: string): void {
+1 -1
View File
@@ -26,7 +26,7 @@ export type FirmwareInfo = {
};
/** The Faceclaw firmware revision this build of the app needs. */
export const REQUIRED_FACECLAW_FIRMWARE_VERSION = 34;
export const REQUIRED_FACECLAW_FIRMWARE_VERSION = 35;
const FACECLAW_PREFIX = "Faceclaw/";
const LEGACY_PREFIX = "EVENCFW";
+34 -34
View File
@@ -19,12 +19,12 @@ export type FirmwarePatchSet = {
export const CFW_PATCH_SET: FirmwarePatchSet = {
"base": "g2_2.3.0.24.bin",
"baseSha256": "187ccf2bcc5c17a212106e8a376745511e8289c4232b634a7ea94b9bf25a0979",
"outputSha256": "7d8764f8b720252354dcd1695d23578b11f9b7cf5df9895d08b9f6668e9d0ee7",
"outputSha256": "d7971b68add0c5187fec81d1cb1ef8cc12322db426a98cb445f1b3a274272817",
"patches": [
{
"offset": 1045797,
"old": "38b584b0",
"new": "5df3a9b8",
"new": "5df3b9b8",
"desc": "ring receiver entry: timestamped unfiltered reports under framebuffer lease"
},
{
@@ -36,49 +36,49 @@ export const CFW_PATCH_SET: FirmwarePatchSet = {
{
"offset": 1437923,
"old": "fef788ff",
"new": "fdf2b8fd",
"new": "fdf2c8fd",
"desc": "cfw_ancs_event"
},
{
"offset": 1437951,
"old": "fef7e8fb",
"new": "fdf2f2fa",
"new": "fdf202fb",
"desc": "cfw_ancs_open"
},
{
"offset": 1437969,
"old": "fef7e7fb",
"new": "fdf209fb",
"new": "fdf219fb",
"desc": "cfw_ancs_close"
},
{
"offset": 1388387,
"old": "39694d00",
"new": "59637d00",
"new": "79637d00",
"desc": "connection-bound ANCS EUS write callback"
},
{
"offset": 971497,
"old": "fff717f8",
"new": "6ff3d9f9",
"new": "6ff3e9f9",
"desc": "bl faceclaw_input_bridge_received (ring relay and ordered private transport before worker pool)"
},
{
"offset": 971845,
"old": "fef769ff",
"new": "6ff32bf9",
"new": "6ff33bf9",
"desc": "bl faceclaw_input_bridge_received (ring relay and ordered private transport before worker pool)"
},
{
"offset": 979579,
"old": "fdf74ef8",
"new": "6df310fa",
"new": "6df320fa",
"desc": "bl faceclaw_input_bridge_received (ring relay and ordered private transport before worker pool)"
},
{
"offset": 1428895,
"old": "f8f76cfd",
"new": "fcf228fa",
"new": "fcf238fa",
"desc": "bl cfw_receive_packet (private SID-f0 probe before TPL reassembly)"
},
{
@@ -108,145 +108,145 @@ export const CFW_PATCH_SET: FirmwarePatchSet = {
{
"offset": 1248487,
"old": "d4f712fb",
"new": "2af3f2fa",
"new": "2af302fb",
"desc": "bl settings_send_wrapper (append caps field 100)"
},
{
"offset": 1246183,
"old": "f5f7e2f8",
"new": "2af376fc",
"new": "2af386fc",
"desc": "bl settings_decode_wrapper (Faceclaw lease field 101)"
},
{
"offset": 975219,
"old": "02f083fc",
"new": "6cf3e2fc",
"new": "6cf3f2fc",
"desc": "bl faceclaw_display_start @ 0x467d68 (fail-open wake takeover)"
},
{
"offset": 975411,
"old": "02f023fc",
"new": "6cf346fd",
"new": "6cf356fd",
"desc": "bl faceclaw_display_start_headup @ 0x467e28 (fail-open wake takeover)"
},
{
"offset": 975103,
"old": "07f085fb",
"new": "6ef3e2f9",
"new": "6ef3f2f9",
"desc": "bl headup_gate (head-up sensor event -> EvenHub sys event 12 under lease)"
},
{
"offset": 830549,
"old": "19f03efb",
"new": "91f391fb",
"new": "91f3a1fb",
"desc": "bl gesture_ring_press_mode (observe ring touch-down, preserve stock dispatch)"
},
{
"offset": 830795,
"old": "1ff071ff",
"new": "91f34afb",
"new": "91f35afb",
"desc": "bl gesture_press (owned event 9 before stock menu gate)"
},
{
"offset": 830887,
"old": "1ff043ff",
"new": "91f344fb",
"new": "91f354fb",
"desc": "bl gesture_short_long (Faceclaw lease gates event 11 vs stock Menu path)"
},
{
"offset": 831763,
"old": "1ff08dfd",
"new": "91f37af9",
"new": "91f38af9",
"desc": "bl gesture_release (owned event 10 before stock menu gate)"
},
{
"offset": 1570209,
"old": "7fb50600",
"new": "dbf283be",
"new": "dbf293be",
"desc": "even_ai_display_ctrl entry -> conditional Faceclaw lease trampoline"
},
{
"offset": 1051545,
"old": "f6f787f9",
"new": "55f311f9",
"new": "55f321f9",
"desc": "bl display_copy_hook @ 0x47a78e (640x480 direct framebuffer)"
},
{
"offset": 1051861,
"old": "f6f7e9f8",
"new": "55f373f8",
"new": "55f383f8",
"desc": "bl display_copy_hook @ 0x47a8ca (640x480 direct framebuffer)"
},
{
"offset": 1262237,
"old": "d9f7d0fd",
"new": "26f333fb",
"new": "26f343fb",
"desc": "bl faceclaw_send_wear_event @ 0x4ade92 (outside onboarding)"
},
{
"offset": 1262337,
"old": "d9f79efd",
"new": "26f301fb",
"new": "26f311fb",
"desc": "bl faceclaw_send_wear_event @ 0x4adef6 (outside onboarding)"
},
{
"offset": 1309405,
"old": "67f02dff",
"new": "1af30bfc",
"new": "1af31bfc",
"desc": "bl compass_decode_capture (sample-matched GAF diagnostics)"
},
{
"offset": 1307865,
"old": "fff7c9fb",
"new": "1af389ff",
"new": "1af399ff",
"desc": "bl compass_report_event (stock UI + heading with diagnostics over BLE)"
},
{
"offset": 1300937,
"old": "00210170",
"new": "1cf36ffc",
"new": "1cf37ffc",
"desc": "bl compass_preserve_accuracy (retain magnetic calibration under Faceclaw framebuffer lease)"
},
{
"offset": 4537963,
"old": "",
"new": "b0b581ea00039b0701d0034637e0c2b110f0030315d000f1010ca2f1010e0023cc5c9e45c45403f1010404d0634413f003032346f4d121440319121b102a14d302e00346102a10d3d1f800c0d1f804e0cc688d68103adc609d60c3f804e043f810cb0f2a01f11001eed8042a07d300bf51f8045b043a032a43f8045bf8d8002a08bfb0bd0139013b11f8015f013a03f8015ff9d1b0bd00bf884218bf002a00d1704710b50b1a38bf431a934207d288420ad281ea00039b0726d0034669e0fff79fffbde81040704781ea000300eb020c9b0701eb020e42d15fea8c732bd0a0f1010c00bf8b1813f801ec8418531e04f801ec04d0624412f003021a46f2d101eb030e00eb030c17e0830736d0a2f1010c00f1010e002300bfcc5c9c45c45403f1010404d0734413f003032346f4d1121b2144031922e01346042b0ed3aef10402acf1040152f80449043b032b41f80449f8d802f1040e01f1040c1a4653b1aef10101acf1010300bf11f80149013a03f80149f9d1bde8104070470346042a06d351f8044b043a032a43f8044bf8d8002af0d00139013b00bf11f8014f013a03f8014ff9d1e6e700bf80b5002a18bf10f0030301d186460ce000f1010c864600bf013a0ef8011b04d01cf003030cf1010cf6d1102a11d35ffa81fc4ff001330cfb03fc00bf103acef80cc0cef808c0cef804c04ef810cb0f2af4d8042a0bd35ffa81fc4ff001330cfb03fc00bf043a032a4ef804cbfad8002a08bf80bdaef10103013a03f8011ffbd180bd00bf2de9f04fc1b04ff62c484ef66c694ff29c5b1446cff6ff78ccf2ff09c2f2290b4ff6ff75002701260993cde90a0106e048f2a351c0f24501bff32f8f8847dbf800a0bff35f8fdbf800000321cff680710140b1f1005fbff35f8f03d10168494500f0ba804ff4757007f0a6fa002800f0078241464218463182f8d47382f8d57382f8d67382f8d77382f8d87382f8d97382f8da7382f8db7382f8dc7382f8dd7382f8de7382f8df7382f8e07382f8e17382f8e27382f8e37382f8e47382f8e57382f8e67382f8e77382f8e87382f8e97382f8ea7382f8eb7382f8ec7382f8ed7382f8ee7382f8ef7382f8f07382f8f17382f8f27382f8f37382f8f47382f8f57382f8f67382f8f77382f8f87382f8f97382f8fa7382f8fb7382f8fc7382f8fd7382f8fe7382f8ff7382f8007482f8017482f8027482f8037482f8047482f8057482f8067482f8077482f8087482f8097482f80a7482f80b7482f80c7482f80d7482f80e7482f80f7482f8107482f8117482f8127482f8137482f8147482f8157482f8167482f8177482f8187482f819747ff470afc0f8009086728583c583058445848584c584058545858585c585058645868586c586058745875be8001f51457ff436afbff35f8f4be8000121b15be8001f5145f8d02be70b99012641ea04410024c0f8701206e048f2a351c0f24501bff32f8f8847dbf80070bff35f8fdbf800000321cff680710140b1f1005fbff35f8f03d10168494500f0ba804ff4757007f0caf9002800f02b8141464218463182f8d44382f8d54382f8d64382f8d74382f8d84382f8d94382f8da4382f8db4382f8dc4382f8dd4382f8de4382f8df4382f8e04382f8e14382f8e24382f8e34382f8e44382f8e54382f8e64382f8e74382f8e84382f8e94382f8ea4382f8eb4382f8ec4382f8ed4382f8ee4382f8ef4382f8f04382f8f14382f8f24382f8f34382f8f44382f8f54382f8f64382f8f74382f8f84382f8f94382f8fa4382f8fb4382f8fc4382f8fd4382f8fe4382f8ff4382f8004482f8014482f8024482f8034482f8044482f8054482f8064482f8074482f8084482f8094482f80a4482f80b4482f80c4482f80d4482f80e4482f80f4482f8104482f8114482f8124482f8134482f8144482f8154482f8164482f8174482f8184482f819447ff470afc0f8009086728583c583058445848584c584058545858585c585058645868586c586058745875be8001fb9427ff436afbff35f8f4be8000121b15be8001fb942f8d02be7d0f874a242f6f766c0f24406baf1000fbff35f8f4ad006f19a0250464ff0ff314ff0ff379047002840f09d810b98002861d00a980778a7f1170002285bd80b980c2840f086810a9e7078b178084300f08081f0780138022800f27b81b279f379747a42ea0322337a307942ea0342717942ea0463b27af67a192f42ea062200f01082172f40f02682002a40f06281fe2800f06d86812800f06086002840f0738651ea030040f0558100f0bdbe04460020b047e8b104f51d7151e800afbaf1000f0cd1bff35f8f00bf41e80002002a00f0f58051e800afbaf1000ff5d00021bff32f8f0029bff35f8f00f0e480824692e74ff0ff3737e147f6df40c0f24f008047002701240d97adf8387006e048f2a351c0f24501bff32f8f8847dbf80060bff35f8fdbf800000321cff680710140b1f1005fbff35f8f03d10168494500f0c1804ff4757007f070f8002800f0b98041464218463182f8d47382f8d57382f8d67382f8d77382f8d87382f8d97382f8da7382f8db7382f8dc7382f8dd7382f8de7382f8df7382f8e07382f8e17382f8e27382f8e37382f8e47382f8e57382f8e67382f8e77382f8e87382f8e97382f8ea7382f8eb7382f8ec7382f8ed7382f8ee7382f8ef7382f8f07382f8f17382f8f27382f8f37382f8f47382f8f57382f8f67382f8f77382f8f87382f8f97382f8fa7382f8fb7382f8fc7382f8fd7382f8fe7382f8ff7382f8007482f8017482f8027482f8037482f8047482f8057482f8067482f8077482f8087482f8097482f80a7482f80b7482f80c7482f80d7482f80e7482f80f7482f8107482f8117482f8127482f8137482f8147482f8157482f8167482f8177482f8187482f819747ff470afc0f8009084728583c583058445848584c584058545858585c585058645868586c586058745875be8001fb1427ff436afbff35f8f4be8000159b15be8001fb142f8d02be706f5a9718847ace6012110e700200a9999b10b9a8ab1097801f07f02b2f10b0618bf0126b2f11c0718bf01278a2924dc0b2926d01c2924d03fe009900ca804f097fa4ff0ff3701249df8390000280ca818bf4ff0ff3704f0d3fb054644b99df8380028b94af21f30c0f247004c3080470998002818bfc56013e09c2901d08b291bd160b14af21f31c0f247010446884794f8641119b994f80413204669b14ff0ff3742f6f760c0f2440000f5807150468847384641b0bde8f08f09900ca806ea070404f055fa0a98017801f07f061e2e04d10b9908f076fb0746b5e71d2e01f07e0018bf1a2811d1dbf800100322cff680720a40b2f1005fbff35f8f0ad10a684a4504bf002281f8812303e0a6f115010129e9d90f2e0adc092e00f37381052e00f0ef82072e40f01c8300279ae0142e00f32182102e00f0eb82112e40f011830b98022841f0c6800a9908781Line truncated
"new": "b0b581ea00039b0701d0034637e0c2b110f0030315d000f1010ca2f1010e0023cc5c9e45c45403f1010404d0634413f003032346f4d121440319121b102a14d302e00346102a10d3d1f800c0d1f804e0cc688d68103adc609d60c3f804e043f810cb0f2a01f11001eed8042a07d300bf51f8045b043a032a43f8045bf8d8002a08bfb0bd0139013b11f8015f013a03f8015ff9d1b0bd00bf884218bf002a00d1704710b50b1a38bf431a934207d288420ad281ea00039b0726d0034669e0fff79fffbde81040704781ea000300eb020c9b0701eb020e42d15fea8c732bd0a0f1010c00bf8b1813f801ec8418531e04f801ec04d0624412f003021a46f2d101eb030e00eb030c17e0830736d0a2f1010c00f1010e002300bfcc5c9c45c45403f1010404d0734413f003032346f4d1121b2144031922e01346042b0ed3aef10402acf1040152f80449043b032b41f80449f8d802f1040e01f1040c1a4653b1aef10101acf1010300bf11f80149013a03f80149f9d1bde8104070470346042a06d351f8044b043a032a43f8044bf8d8002af0d00139013b00bf11f8014f013a03f8014ff9d1e6e700bf80b5002a18bf10f0030301d186460ce000f1010c864600bf013a0ef8011b04d01cf003030cf1010cf6d1102a11d35ffa81fc4ff001330cfb03fc00bf103acef80cc0cef808c0cef804c04ef810cb0f2af4d8042a0bd35ffa81fc4ff001330cfb03fc00bf043a032a4ef804cbfad8002a08bf80bdaef10103013a03f8011ffbd180bd00bf2de9f04fc5b04ff62c484ef66c694ff29c5b1446cff6ff78ccf2ff09c2f2290b4ff6ff75002701260d93cde90e0106e048f2a351c0f24501bff32f8f8847dbf800a0bff35f8fdbf800000321cff680710140b1f1005fbff35f8f03d10168494500f0ba804ff4757007f0b6fa002800f0078241464218463182f8d47382f8d57382f8d67382f8d77382f8d87382f8d97382f8da7382f8db7382f8dc7382f8dd7382f8de7382f8df7382f8e07382f8e17382f8e27382f8e37382f8e47382f8e57382f8e67382f8e77382f8e87382f8e97382f8ea7382f8eb7382f8ec7382f8ed7382f8ee7382f8ef7382f8f07382f8f17382f8f27382f8f37382f8f47382f8f57382f8f67382f8f77382f8f87382f8f97382f8fa7382f8fb7382f8fc7382f8fd7382f8fe7382f8ff7382f8007482f8017482f8027482f8037482f8047482f8057482f8067482f8077482f8087482f8097482f80a7482f80b7482f80c7482f80d7482f80e7482f80f7482f8107482f8117482f8127482f8137482f8147482f8157482f8167482f8177482f8187482f819747ff470afc0f8009086728583c583058445848584c584058545858585c585058645868586c586058745875be8001f51457ff436afbff35f8f4be8000121b15be8001f5145f8d02be70f99012641ea04410024c0f8701206e048f2a351c0f24501bff32f8f8847dbf80070bff35f8fdbf800000321cff680710140b1f1005fbff35f8f03d10168494500f0ba804ff4757007f0daf9002800f02b8141464218463182f8d44382f8d54382f8d64382f8d74382f8d84382f8d94382f8da4382f8db4382f8dc4382f8dd4382f8de4382f8df4382f8e04382f8e14382f8e24382f8e34382f8e44382f8e54382f8e64382f8e74382f8e84382f8e94382f8ea4382f8eb4382f8ec4382f8ed4382f8ee4382f8ef4382f8f04382f8f14382f8f24382f8f34382f8f44382f8f54382f8f64382f8f74382f8f84382f8f94382f8fa4382f8fb4382f8fc4382f8fd4382f8fe4382f8ff4382f8004482f8014482f8024482f8034482f8044482f8054482f8064482f8074482f8084482f8094482f80a4482f80b4482f80c4482f80d4482f80e4482f80f4482f8104482f8114482f8124482f8134482f8144482f8154482f8164482f8174482f8184482f819447ff470afc0f8009086728583c583058445848584c584058545858585c585058645868586c586058745875be8001fb9427ff436afbff35f8f4be8000121b15be8001fb942f8d02be7d0f874a242f6f766c0f24406baf1000fbff35f8f4ad006f19a0250464ff0ff314ff0ff379047002840f09d810f98002861d00e980778a7f1170002285bd80f980c2840f086810e9e7078b178084300f08081f0780138022800f27b81b279f379747a42ea0322337a307942ea0342717942ea0463b27af67a192f42ea062200f01082172f40f02682002a40f06281fe2800f07786812800f06a86002840f07d8651ea030040f0558100f0c7be04460020b047e8b104f51d7151e800afbaf1000f0cd1bff35f8f00bf41e80002002a00f0f58051e800afbaf1000ff5d00021bff32f8f0029bff35f8f00f0e480824692e74ff0ff3737e147f6df40c0f24f008047002701241197adf8487006e048f2a351c0f24501bff32f8f8847dbf80060bff35f8fdbf800000321cff680710140b1f1005fbff35f8f03d10168494500f0c1804ff4757007f080f8002800f0b98041464218463182f8d47382f8d57382f8d67382f8d77382f8d87382f8d97382f8da7382f8db7382f8dc7382f8dd7382f8de7382f8df7382f8e07382f8e17382f8e27382f8e37382f8e47382f8e57382f8e67382f8e77382f8e87382f8e97382f8ea7382f8eb7382f8ec7382f8ed7382f8ee7382f8ef7382f8f07382f8f17382f8f27382f8f37382f8f47382f8f57382f8f67382f8f77382f8f87382f8f97382f8fa7382f8fb7382f8fc7382f8fd7382f8fe7382f8ff7382f8007482f8017482f8027482f8037482f8047482f8057482f8067482f8077482f8087482f8097482f80a7482f80b7482f80c7482f80d7482f80e7482f80f7482f8107482f8117482f8127482f8137482f8147482f8157482f8167482f8177482f8187482f819747ff470afc0f8009084728583c583058445848584c584058545858585c585058645868586c586058745875be8001fb1427ff436afbff35f8f4be8000159b15be8001fb142f8d02be706f5a9718847ace6012110e700200e9999b10f9a8ab1097801f07f02b2f10b0618bf0126b2f11c0718bf01278a2924dc0b2926d01c2924d03fe00d9010a804f0a7fa4ff0ff3701249df84900002810a818bf4ff0ff3704f0e3fb054644b99df8480028b94af21f30c0f247004c3080470d98002818bfc56013e09c2901d08b291bd160b14af21f31c0f247010446884794f8641119b994f80413204669b14ff0ff3742f6f760c0f2440000f5807150468847384645b0bde8f08f0d9010a806ea070404f065fa0e98017801f07f061e2e04d10f9908f086fb0746b5e71d2e01f07e0018bf1a2811d1dbf800100322cff680720a40b2f1005fbff35f8f0ad10a684a4504bf002281f8812303e0a6f115010129e9d90f2e0adc092e00f37381052e00f0ef82072e40f01c8300279ae0142e00f32182102e00f0eb82112e40f011830f98022841f0d5800e9908781Line truncated
"desc": "append injected blobs to main-app payload"
},
{
"offset": 779123,
"old": "805a3900",
"new": "e06a3a00",
"new": "34733a00",
"desc": "main-app subheader payload size (ps)"
},
{
"offset": 152,
"old": "005b3900",
"new": "606b3a00",
"new": "b4733a00",
"desc": "main-app TOC entry size (ps + 128)"
},
{
"offset": 779243,
"old": "805a3904",
"new": "e06a3a04",
"new": "34733a04",
"desc": "main-app preamble length (low 24 bits)"
},
{
"offset": 779247,
"old": "effdbcea",
"new": "38f29188",
"new": "85d22153",
"desc": "[5] ota/s200_firmware_ota.bin preamble crc32"
},
{
"offset": 156,
"old": "c0c6322a",
"new": "f8d63d83",
"new": "817b5baa",
"desc": "[5] ota/s200_firmware_ota.bin component crc32c (TOC)"
},
{
"offset": 779127,
"old": "c0c6322a",
"new": "f8d63d83",
"new": "817b5baa",
"desc": "[5] ota/s200_firmware_ota.bin component crc32c (subheader)"
}
]
+29 -5
View File
@@ -24,7 +24,7 @@ const list: DisplayList = {
frame.drawDisplayList(list, 20, 30, 140, 20, 0);
```
Supported calls in this first pass:
Supported calls:
- `ROUNDED_RECT`: literal or expression `x`/`y`, literal dimensions/radius, and
4-bit colors (0–15). Border 16 means no border. Fill takes the maximum of the
@@ -38,6 +38,24 @@ Supported calls in this first pass:
rejects the list. `SCREEN` reads the uncomposed frame; its source coordinates
are also relative to this list's placement. Each call may add its own `depth`
to the list's depth. A screen restore normally uses the negative of the list depth.
- `CLEAR` (revision 35 with a clip): fill with a 4-bit `color`. Under a clip it
fills just the clip rect, which is how to blank a region; without one it
fills the whole screen.
- `DRAWS` (bridge-only): replay glyph and icon draws offset by `x`/`y`, which
may be expressions. `records`/`count` come from glyph-wire's
`encodeReplayDraws(image.draws)`, which registers the rasters and returns null
unless every draw is a replayable glyph (atlas font, ASCII, ink inside the
line box) or plain image. Kotlin compiles them to firmware IMAGE calls on
ImageAtlas resources and TEXT runs on FontResourceAtlas fonts (one run per
line and color). `source` keeps the draws for software painting. If Kotlin
cannot resolve a record, the whole list draws nothing, so the frame's own
pixels show rather than content with a hole in it.
Any call may carry a `clip` (revision 35): a rect in list coordinates outside
which it draws nothing. It moves with the call's depth, like the call itself.
The firmware flag also nests (a clipped play-list call clips everything inside
it, intersected with the calls' own clips), but TS lists are inlined into the
root, so each call carries its own.
Kotlin allocates/cache-pins all image resources, resolves the local indices, applies
surface placement, and includes the calls in the root firmware display list. The
@@ -68,7 +86,9 @@ exclude the changing elapsed duration. Without a timeline, raw `time()` expressi
start at each PRESENT. Native compilation strips the bridge-only elapsed opcode.
`menu-selection-list.ts` authors both the rounded highlight and the transparent row
image using this API. `menu-scroll-list.ts` authors a menu scroll or bounce: a
image using this API. `IconGrid` (app/ui/icon-grid.ts) animates a whole grid
this way: a clipped CLEAR of the grid box, the sliding highlight, then a DRAWS
of every row visible during the scroll, offset by the scroll expression. `menu-scroll-list.ts` authors a menu scroll or bounce: a
viewport-sized copy from a strip resource whose source y animates, then the
highlight. The bounce curve (`app/ui/menu-scroll-motion.ts`) needs no branches:
the easing ops clamp their input to 0..1, so `ease(t / p)` holds at 1 after the
@@ -80,9 +100,13 @@ TypeScript. There is no animated-menu tag or menu animation policy in Kotlin.
Tag 7 is followed by a little-endian u32 byte length and a bounded body:
placement (i16 x/y, u16 width/height, i8 depth), timeline (u32 token/elapsed),
u16 resource count, gray8 images (u16 width/height followed by pixels), u16 call
count, then calls. Calls start with the firmware opcode and an i16 relative depth;
operands otherwise use their fixed-width fields, except rounded x/y and rect-copy
x/y/dx/dy which use extended varints. Expressions use the firmware bytecode plus bridge-only
count, then calls. Calls start with the opcode (bit 7 set when a clip follows)
and an i16 relative depth, then the clip if any (i16 x/y, u16 width/height);
operands otherwise use their fixed-width fields, except rounded x/y, rect-copy
x/y/dx/dy and DRAWS x/y which use extended varints. CLEAR is a u8 color. DRAWS
(opcode 32) is followed by a u16 record count and the records, in the frame
draw-buffer formats: glyph `[0][fontId u16][encoding u32][penX s16][lineY s16][value u8]`
and image `[1][imageId u32][x s16][y s16]`. Expressions use the firmware bytecode plus bridge-only
`ELAPSED = 128`. Resource indices are local to this record, never firmware IDs.
Rect-copy source x/y are signed here to permit relative SCREEN coordinates.
+69 -13
View File
@@ -1,15 +1,31 @@
import { DrawBytes, encodeValue, evaluate, integer, readValue, type DrawValue } from './draw-expression';
import type { GrayImage } from './image';
/** Local resources are assigned firmware cache IDs by Kotlin. SCREEN reads the uncomposed frame. */
export const SCREEN = 65535;
export const DrawOp = { RECT_COPY: 2, IMAGE: 4, ROUNDED_RECT: 8 } as const;
/**
* Firmware opcodes, plus DRAWS: a bridge-only call that replays glyph and icon
* draws (from glyph-wire's encodeReplayDraws) as firmware image/text calls.
*/
export const DrawOp = { RECT_COPY: 2, IMAGE: 4, ROUNDED_RECT: 8, CLEAR: 9, DRAWS: 32 } as const;
export const DISPLAY_LIST_RECORD = 7;
/** Bridge opcode bit: a clip rect follows the call's depth. */
const CLIPPED = 128;
export type ListImage = { readonly width: number; readonly height: number; readonly pixels: Uint8Array };
/** Revision 35: a call draws only inside its clip, in list coordinates (and moves with its depth). */
export type ListClip = { readonly x: number; readonly y: number; readonly width: number; readonly height: number };
export type ListCall = (
| { op: typeof DrawOp.ROUNDED_RECT; x: DrawValue; y: DrawValue; width: number; height: number; radius: number; background: number; border: number }
| { op: typeof DrawOp.IMAGE; resource: number; x: number; y: number; transparent: boolean }
| { op: typeof DrawOp.RECT_COPY; resource: number; x: DrawValue; y: DrawValue; width: number; height: number; dx: DrawValue; dy: DrawValue }
) & { depth?: number };
/** Fill with a 4-bit color: the clip rect when there is one, else the whole screen. */
| { op: typeof DrawOp.CLEAR; color: number }
/**
* Glyph and icon draws offset by (x, y). `records`/`count` are the wire form;
* `source` holds the same draws for software painting (a decoded list has none).
*/
| { op: typeof DrawOp.DRAWS; x: DrawValue; y: DrawValue; records: Uint8Array; count: number; source?: GrayImage }
) & { depth?: number; clip?: ListClip };
export type DisplayList = {
readonly resources: readonly ListImage[];
readonly calls: readonly ListCall[];
@@ -45,12 +61,18 @@ export function encodeDisplayList(placed: PlacedDisplayList, now = Date.now()):
}
integer(list.calls.length, 0, 4096); out.u16(list.calls.length);
for (const call of list.calls) {
out.u8(call.op); out.i16(call.depth ?? 0);
out.u8(call.op | (call.clip ? CLIPPED : 0)); out.i16(call.depth ?? 0);
integer(placed.depth + (call.depth ?? 0), -128, 127);
if (call.clip) { out.i16(call.clip.x); out.i16(call.clip.y); out.u16(call.clip.width); out.u16(call.clip.height); }
if (call.op === DrawOp.ROUNDED_RECT) {
out.raw(encodeValue(call.x)); out.raw(encodeValue(call.y));
out.u16(integer(call.width, 1, 640)); out.u16(integer(call.height, 1, 480)); out.u16(call.radius);
out.u8(integer(call.background, 0, 15)); out.u8(integer(call.border, 0, 16));
} else if (call.op === DrawOp.CLEAR) {
out.u8(integer(call.color, 0, 15));
} else if (call.op === DrawOp.DRAWS) {
out.raw(encodeValue(call.x)); out.raw(encodeValue(call.y));
out.u16(call.count); out.raw(call.records);
} else {
if (call.resource !== SCREEN) integer(call.resource, 0, list.resources.length - 1);
if (call.op === DrawOp.RECT_COPY && call.resource !== SCREEN && [call.x, call.y].some(v => typeof v === 'number' && v < 0)) throw new Error('Negative image source coordinate');
@@ -68,6 +90,9 @@ export function encodeDisplayList(placed: PlacedDisplayList, now = Date.now()):
return Uint8Array.from(header.bytes);
}
/** Replay-record sizes by tag, as in glyph-wire's frame draw buffer. */
const RECORD_BYTES: Record<number, number> = { 0: 12, 1: 9 };
export function readDisplayList(bytes: Uint8Array, offset: number, now = Date.now()): { placed: PlacedDisplayList; end: number } {
const envelope = new DrawBytes(bytes.subarray(offset));
if (envelope.u8() !== DISPLAY_LIST_RECORD) throw new Error('Invalid display-list record');
@@ -79,11 +104,22 @@ export function readDisplayList(bytes: Uint8Array, offset: number, now = Date.no
for (let i = 0; i < count; i++) { const width = r.u16(), height = r.u16(); resources.push({ width, height, pixels: r.take(width * height) }); }
const calls: ListCall[] = [], callCount = r.u16(); integer(callCount, 0, 4096);
for (let i = 0; i < callCount; i++) {
const op = r.u8(), depth = r.i16();
if (op === DrawOp.ROUNDED_RECT) calls.push({ op, depth, x: readValue(r), y: readValue(r), width: r.u16(), height: r.u16(), radius: r.u16(), background: r.u8(), border: r.u8() });
else if (op === DrawOp.IMAGE) calls.push({ op, depth, resource: r.u16(), x: r.i16(), y: r.i16(), transparent: (r.u8() & 16) !== 0 });
else if (op === DrawOp.RECT_COPY) calls.push({ op, depth, resource: r.u16(), x: readValue(r), y: readValue(r), width: r.u16(), height: r.u16(), dx: readValue(r), dy: readValue(r) });
else throw new Error('Unsupported display-list call');
const header = r.u8(), op = header & ~CLIPPED, depth = r.i16();
const clip = header & CLIPPED ? { x: r.i16(), y: r.i16(), width: r.u16(), height: r.u16() } : undefined;
const common = clip ? { depth, clip } : { depth };
if (op === DrawOp.ROUNDED_RECT) calls.push({ op, ...common, x: readValue(r), y: readValue(r), width: r.u16(), height: r.u16(), radius: r.u16(), background: r.u8(), border: r.u8() });
else if (op === DrawOp.IMAGE) calls.push({ op, ...common, resource: r.u16(), x: r.i16(), y: r.i16(), transparent: (r.u8() & 16) !== 0 });
else if (op === DrawOp.RECT_COPY) calls.push({ op, ...common, resource: r.u16(), x: readValue(r), y: readValue(r), width: r.u16(), height: r.u16(), dx: readValue(r), dy: readValue(r) });
else if (op === DrawOp.CLEAR) calls.push({ op, ...common, color: r.u8() });
else if (op === DrawOp.DRAWS) {
const x = readValue(r), y = readValue(r), records = r.u16(), start = r.offset;
for (let n = 0; n < records; n++) {
const size = RECORD_BYTES[r.bytes[r.offset]!];
if (!size) throw new Error('Unsupported display-list draw record');
r.take(size);
}
calls.push({ op, ...common, x, y, count: records, records: r.bytes.slice(start, r.offset) });
} else throw new Error('Unsupported display-list call');
}
if (r.offset !== length) throw new Error('Trailing display-list bytes');
const placed = { x, y, width, height, depth, displayList: { resources, calls, presentedAt: now, timeline: { token, startedAt: now - elapsed } } };
@@ -92,13 +128,23 @@ export function readDisplayList(bytes: Uint8Array, offset: number, now = Date.no
return { placed, end: offset + envelope.offset };
}
/**
* The list at `factor` brightness. Replayed icons cannot be dimmed on the
* glasses, so a list with DRAWS calls becomes empty instead: the dimmed frame
* underneath then shows its final state.
*/
export function dimDisplayList(list: DisplayList, factor: number): DisplayList {
if (list.calls.some(c => c.op === DrawOp.DRAWS)) return { ...list, calls: [] };
const dim = (v: number) => v === 0 ? 0 : Math.max(1, Math.round(v * factor));
return { ...list, resources: list.resources.map(r => ({ ...r, pixels: r.pixels.map(dim) })),
calls: list.calls.map(c => c.op === DrawOp.ROUNDED_RECT ? { ...c, background: Math.min(15, Math.round(c.background * factor)), border: c.border === 16 ? 16 : Math.min(15, Math.round(c.border * factor)) } : c) };
calls: list.calls.map(c => c.op === DrawOp.ROUNDED_RECT ? { ...c, background: Math.min(15, Math.round(c.background * factor)), border: c.border === 16 ? 16 : Math.min(15, Math.round(c.border * factor)) }
: c.op === DrawOp.CLEAR ? { ...c, color: Math.min(15, Math.round(c.color * factor)) } : c) };
}
/** Fallback compositor; native preview and glasses replay the compiled list with their existing timers. */
/**
* Fallback compositor; native preview and glasses replay the compiled list with their existing timers.
* A DRAWS call paints from its `source`, so a decoded list skips them.
*/
export function paintDisplayList(output: Uint8Array, screen: Uint8Array, width: number, height: number, placed: PlacedDisplayList, right = false, now = Date.now()): void {
const q = (n: number) => Math.min(15, (n + 8) >> 4) * 16;
const list = placed.displayList;
@@ -112,10 +158,20 @@ export function paintDisplayList(output: Uint8Array, screen: Uint8Array, width:
};
for (const c of placed.displayList.calls) {
const depth = placed.depth + (c.depth ?? 0), shift = right ? -Math.floor((depth + 1) / 2) : Math.floor(depth / 2);
if (c.op === DrawOp.ROUNDED_RECT) {
// Writable region in screen pixels: the clip moves with the call's depth, like its content.
const clip = c.clip ? { x: placed.x + c.clip.x + shift, y: placed.y + c.clip.y, width: c.clip.width, height: c.clip.height }
: { x: 0, y: 0, width, height };
const writable = (x: number, y: number) => x >= 0 && y >= 0 && x < width && y < height &&
x >= clip.x && y >= clip.y && x < clip.x + clip.width && y < clip.y + clip.height;
if (c.op === DrawOp.CLEAR) {
for (let y = 0; y < height; y++) for (let x = 0; x < width; x++) if (writable(x, y)) output[y * width + x] = c.color * 16;
} else if (c.op === DrawOp.DRAWS) {
const at = (v: DrawValue) => evaluate(v, elapsed, presentTime).value;
c.source?.paintReplayDraws(output, width, height, placed.x + at(c.x) + shift, placed.y + at(c.y), clip);
} else if (c.op === DrawOp.ROUNDED_RECT) {
const x = placed.x + evaluate(c.x, elapsed, presentTime).value + shift, y = placed.y + evaluate(c.y, elapsed, presentTime).value;
for (let yy = 0; yy < c.height; yy++) for (let xx = 0; xx < c.width; xx++) {
if (x + xx < 0 || y + yy < 0 || x + xx >= width || y + yy >= height || !inside(xx, yy, c.width, c.height, c.radius)) continue;
if (!writable(x + xx, y + yy) || !inside(xx, yy, c.width, c.height, c.radius)) continue;
const p = (y + yy) * width + x + xx;
output[p] = c.border !== 16 && !inside(xx - 1, yy - 1, c.width - 2, c.height - 2, Math.max(0, c.radius - 1)) ? c.border * 16 : Math.max(output[p], c.background * 16);
}
@@ -126,7 +182,7 @@ export function paintDisplayList(output: Uint8Array, screen: Uint8Array, width:
const sx = c.op === DrawOp.IMAGE ? 0 : at(c.x) + (c.resource === SCREEN ? placed.x : 0), sy = c.op === DrawOp.IMAGE ? 0 : at(c.y) + (c.resource === SCREEN ? placed.y : 0);
const x = placed.x + (c.op === DrawOp.IMAGE ? c.x : at(c.dx)) + shift, y = placed.y + (c.op === DrawOp.IMAGE ? c.y : at(c.dy));
for (let yy = 0; yy < h; yy++) for (let xx = 0; xx < w; xx++) {
if (x + xx < 0 || y + yy < 0 || x + xx >= width || y + yy >= height || sx + xx < 0 || sy + yy < 0 || sx + xx >= source.width || sy + yy >= source.height) continue;
if (!writable(x + xx, y + yy) || sx + xx < 0 || sy + yy < 0 || sx + xx >= source.width || sy + yy >= source.height) continue;
const v = q(source.pixels[(sy + yy) * source.width + sx + xx]);
if (c.op !== DrawOp.IMAGE || !c.transparent || v !== 0) output[(y + yy) * width + x + xx] = v;
}
+87 -28
View File
@@ -21,7 +21,7 @@ import { DrawRecordKind, encodePresentation } from "./presentation-wire";
* only means "no wire savings for this draw".
*/
import { Glyph } from "./bdffont";
import { GrayImage, type DeferredDraw, type GlyphFont, type PlacedFwText, type PlacedImage } from "./image";
import { GrayImage, type DeferredDraw, type GlyphFont, type PlacedFwText, type PlacedGlyph, type PlacedImage } from "./image";
import { textureAtlasAvailable, textureFontId, registerTextureGlyphs, registerFirmwareGlyphs, textureImageId } from "../native/texture-atlas";
@@ -167,15 +167,7 @@ export function prepareFrameDraws(draws: readonly DeferredDraw[]): ArrayBuffer |
if (!state || !representableGlyph(placed.font, placed.glyph)) continue;
if (!inRange16(placed.x) || !inRange16(placed.y)) continue;
bytes += GLYPH_RECORD_BYTES;
if (state.registered.has(placed.glyph.encoding)) continue;
state.registered.add(placed.glyph.encoding);
registration ??= new Map();
let group = registration.get(state);
if (!group) {
group = { font: placed.font, glyphs: [], aaGlyphs: [] };
registration.set(state, group);
}
(placed.glyph.coverage ? group.aaGlyphs : group.glyphs).push(placed.glyph);
registration = queueRegistration(registration, state, placed);
} else if (placed.kind === "image") {
if (!inRange16(placed.x) || !inRange16(placed.y)) continue;
if (placed.presentation) { const record = encodePresentation(placed); presentations.set(placed, record); bytes += record.length; }
@@ -186,12 +178,7 @@ export function prepareFrameDraws(draws: readonly DeferredDraw[]): ArrayBuffer |
if (ok) bytes += FWTEXT_HEADER_BYTES + placed.glyphs.length * FWTEXT_MEMBER_BYTES;
}
}
if (registration) {
const plainBuffer = buildRegistrationBuffer(registration);
if (plainBuffer) registerTextureGlyphs(plainBuffer, false);
const aaBuffer = buildAaRegistrationBuffer(registration);
if (aaBuffer) registerTextureGlyphs(aaBuffer, true);
}
registerQueued(registration);
if (bytes === 0) return null;
// Pass 2: the frame's draw records.
@@ -202,23 +189,13 @@ export function prepareFrameDraws(draws: readonly DeferredDraw[]): ArrayBuffer |
if (!inRange16(placed.x) || !inRange16(placed.y)) continue;
const state = fontWireState(placed.font);
if (!state || !representableGlyph(placed.font, placed.glyph)) continue;
out.setUint8(offset, DrawRecordKind.GLYPH);
out.setUint16(offset + 1, state.fontId, true);
out.setUint32(offset + 3, placed.glyph.encoding, true);
out.setInt16(offset + 7, placed.x, true);
out.setInt16(offset + 9, placed.y, true);
out.setUint8(offset + 11, placed.value);
offset += GLYPH_RECORD_BYTES;
offset = writeGlyphRecord(out, offset, state, placed);
} else if (placed.kind === "image") {
if (!inRange16(placed.x) || !inRange16(placed.y)) continue;
if (placed.presentation) { const bytes = presentations.get(placed)!; new Uint8Array(out.buffer).set(bytes, offset); offset += bytes.length; continue; }
const id = imageId(placed);
if (id === null) continue;
out.setUint8(offset, DrawRecordKind.TEXTURE_IMAGE);
out.setUint32(offset + 1, id, true);
out.setInt16(offset + 5, placed.x, true);
out.setInt16(offset + 7, placed.y, true);
offset += IMAGE_RECORD_BYTES;
offset = writeImageRecord(out, offset, id, placed);
} else {
if (!fwRunOk.get(placed)) continue;
out.setUint8(offset, DrawRecordKind.FIRMWARE_TEXT);
@@ -238,6 +215,88 @@ export function prepareFrameDraws(draws: readonly DeferredDraw[]): ArrayBuffer |
return out.buffer;
}
/**
* Encode draws for replay inside a display list (DrawOp.DRAWS), registering
* unseen glyph rasters and images as prepareFrameDraws does, in its glyph and
* image record formats. Null unless every draw is a replayable glyph or plain
* image: a list must reproduce all of its content or none of it.
*/
export function encodeReplayDraws(draws: readonly DeferredDraw[]): { records: Uint8Array; count: number } | null {
if (!textureAtlasAvailable()) return null;
let bytes = 0;
for (const placed of draws) {
if (!inRange16(placed.x) || !inRange16(placed.y)) return null;
if (placed.kind === "glyph") {
if (!fontWireState(placed.font) || !representableGlyph(placed.font, placed.glyph)) return null;
bytes += GLYPH_RECORD_BYTES;
} else if (placed.kind === "image" && !placed.presentation && imageId(placed) !== null) {
bytes += IMAGE_RECORD_BYTES;
} else {
return null;
}
}
let registration: Map<FontWireState, RegistrationGroup> | null = null;
const out = new DataView(new ArrayBuffer(bytes));
let offset = 0;
for (const placed of draws) {
if (placed.kind === "glyph") {
const state = fontWireState(placed.font)!;
registration = queueRegistration(registration, state, placed);
offset = writeGlyphRecord(out, offset, state, placed);
} else if (placed.kind === "image") {
offset = writeImageRecord(out, offset, imageId(placed)!, placed);
}
}
registerQueued(registration);
return { records: new Uint8Array(out.buffer), count: draws.length };
}
/** [0][fontId u16][encoding u32][penX s16][lineY s16][value u8] */
function writeGlyphRecord(out: DataView, offset: number, state: FontWireState, placed: PlacedGlyph): number {
out.setUint8(offset, DrawRecordKind.GLYPH);
out.setUint16(offset + 1, state.fontId, true);
out.setUint32(offset + 3, placed.glyph.encoding, true);
out.setInt16(offset + 7, placed.x, true);
out.setInt16(offset + 9, placed.y, true);
out.setUint8(offset + 11, placed.value);
return offset + GLYPH_RECORD_BYTES;
}
/** [1][imageId u32][x s16][y s16] */
function writeImageRecord(out: DataView, offset: number, id: number, placed: PlacedImage): number {
out.setUint8(offset, DrawRecordKind.TEXTURE_IMAGE);
out.setUint32(offset + 1, id, true);
out.setInt16(offset + 5, placed.x, true);
out.setInt16(offset + 7, placed.y, true);
return offset + IMAGE_RECORD_BYTES;
}
/** Queue a glyph raster for registerQueued unless this JS context already registered it. */
function queueRegistration(
registration: Map<FontWireState, RegistrationGroup> | null,
state: FontWireState,
placed: PlacedGlyph,
): Map<FontWireState, RegistrationGroup> | null {
if (state.registered.has(placed.glyph.encoding)) return registration;
state.registered.add(placed.glyph.encoding);
registration ??= new Map();
let group = registration.get(state);
if (!group) {
group = { font: placed.font, glyphs: [], aaGlyphs: [] };
registration.set(state, group);
}
(placed.glyph.coverage ? group.aaGlyphs : group.glyphs).push(placed.glyph);
return registration;
}
function registerQueued(registration: Map<FontWireState, RegistrationGroup> | null): void {
if (!registration) return;
const plainBuffer = buildRegistrationBuffer(registration);
if (plainBuffer) registerTextureGlyphs(plainBuffer, false);
const aaBuffer = buildAaRegistrationBuffer(registration);
if (aaBuffer) registerTextureGlyphs(aaBuffer, true);
}
type RegistrationGroup = {
font: GlyphFont;
/** 1bpp (BDF) glyphs for GlyphAtlas.register. */
+32 -1
View File
@@ -517,6 +517,34 @@ export class GrayImage {
presentation: { radius, background, border, depth, displayList: menuSelectionList(baked, background, border, radius, animation) } });
}
/**
* Paint this image's glyph and image draws onto a gray8 buffer at (dx, dy),
* inside `clip`, the way the glasses replay them from a display list (see
* DrawOp.DRAWS): glyphs firmware-exact, images skipping pixels whose 4-bit
* level is 0. Raster pixels and other draw kinds are not painted.
*/
paintReplayDraws(pixels: Uint8Array, width: number, height: number, dx: number, dy: number,
clip: { x: number; y: number; width: number; height: number }): void {
const left = Math.max(0, clip.x), top = Math.max(0, clip.y);
const right = Math.min(width, clip.x + clip.width), bottom = Math.min(height, clip.y + clip.height);
const sink: PixelSink = {
setPixel: (x, y, value) => {
if (x >= left && y >= top && x < right && y < bottom) pixels[y * width + x] = clampByte(value);
},
};
for (const placed of this.drawList) {
if (placed.kind === "glyph") {
rasterizeGlyph(sink, placed.font, placed.glyph, placed.x + dx, placed.y + dy, placed.value);
} else if (placed.kind === "image" && !placed.presentation) {
const source = placed.source;
for (let y = 0; y < source.height; y++) for (let x = 0; x < source.width; x++) {
const level = grayToNibble(source.pixels[y * source.width + x]!);
if (level) sink.setPixel(placed.x + dx + x, placed.y + dy + y, level * 16);
}
}
}
}
/** Submit a retained list alongside this frame; coordinates are relative to x/y. */
drawDisplayList(displayList: DisplayList, x: number, y: number, width: number, height: number, depth = 0): void {
this.drawList.push({ kind: "image", source: new GrayImage(width, height), x, y,
@@ -661,9 +689,12 @@ export function grayToNibble(value: number): number {
return value <= 0 ? 0 : Math.min(15, (value + 8) >> 4);
}
/** Where rasterizeGlyph writes: an image, or a clipped buffer (paintReplayDraws). */
type PixelSink = { setPixel(x: number, y: number, value: number): void };
/** Bake one glyph into an image's pixel buffer (y is the top of the line). */
function rasterizeGlyph(
target: GrayImage,
target: PixelSink,
font: GlyphFont,
glyph: Glyph,
x: number,
+7 -2
View File
@@ -18,9 +18,14 @@ export type MenuScrollHighlight = {
* started at `timelineStart` (the slide may have started before or after it).
*/
export function slidingHighlightY(y: number, motion: MenuHighlightAnimation | undefined, timelineStart: number): E<'i32'> {
if (!motion?.dy) return E.i32(y);
return slidingCoordinate(y, motion?.dy ?? 0, motion, timelineStart);
}
/** One coordinate of a highlight sliding by `delta` to rest at `value`; see slidingHighlightY. */
export function slidingCoordinate(value: number, delta: number, motion: MenuHighlightAnimation | undefined, timelineStart: number): E<'i32'> {
if (!motion || !delta) return E.i32(value);
return E.progress(motion.durationMs, motion.startedAt - timelineStart).ease()
.lerp(E.i32(y + motion.dy).toFloat(), E.i32(y).toFloat()).toInt();
.lerp(E.i32(value + delta).toFloat(), E.i32(value).toFloat()).toInt();
}
/**
+39 -3
View File
@@ -1,18 +1,54 @@
import { JavaDirectBuffer } from "./java-direct-buffer";
declare const com: any;
// Reused Java-side buffers for this isolate's frames: passing a JS ArrayBuffer
// to Java leaks it (java-direct-buffer.ts). Module state is per isolate, so
// each worker gets its own pair.
const framePixelsBuffer = new JavaDirectBuffer(640 * 480);
const frameDrawsBuffer = new JavaDirectBuffer();
/**
* The Java object a worker isolate submits surface frames to: the live BLE
* The display a worker isolate submits surface frames to: the live BLE
* communicator, or the preview-only compositor standing in when no glasses
* are paired (both expose the same submitSurfaceFrame signature). Located
* through Java statics because those are shared across isolates, unlike any
* JS-side state.
* JS-side state. Null when neither exists.
*
* Frame submission only. Buzzer effects use worker-buzzer, which routes to
* the platform BLE session and skips playback when disconnected.
*/
export function getActiveDisplay(): any {
export function getActiveDisplay(): typeof display | null {
return activeJavaDisplay() ? display : null;
}
function activeJavaDisplay(): any {
return (
com.faceclaw.app.FaceclawBleCommunicator.getActive() ??
com.faceclaw.app.FaceclawPreviewCompositor.getActive()
);
}
/**
* Takes the same ArrayBuffer arguments as the iOS active display and copies
* them into the reused Java buffers right before the synchronous Java call.
*/
const display = {
submitSurfaceFrame(buffer: ArrayBuffer, surfaceId: string, x: number, y: number, width: number, height: number,
fingerprint: string, paintMs: number, frameId: number, draws: ArrayBuffer | null = null): void {
const target = activeJavaDisplay();
if (!target) return;
target.submitSurfaceFrame(
framePixelsBuffer.load(new Uint8Array(buffer)),
surfaceId,
x,
y,
width,
height,
fingerprint,
paintMs,
frameId,
frameDrawsBuffer.loadOptional(draws),
);
},
};
+20 -7
View File
@@ -1,6 +1,7 @@
import type { RingInput } from "../g2/ring-input";
import { ImageSource, Utils } from "@nativescript/core";
import * as frameTimings from "./frame-timings";
import { JavaDirectBuffer } from "./java-direct-buffer";
declare const com: any;
@@ -132,6 +133,13 @@ export class FaceclawCommunicatorBridge {
private javaCallQueue: Promise<void> = Promise.resolve();
/** Calls waiting on javaCallQueue; 0 means enqueueJavaCall's fast path is safe. */
private queuedJavaCalls = 0;
// Reused Java-side buffers for byte payloads; passing a JS ArrayBuffer to
// Java leaks it (see java-direct-buffer.ts). Loaded inside the queued call,
// immediately before the synchronous Java call that consumes them.
private readonly framePixelsBuffer = new JavaDirectBuffer(640 * 480);
private readonly frameDrawsBuffer = new JavaDirectBuffer();
private readonly shellSceneBuffer = new JavaDirectBuffer();
private readonly buzzerBuffer = new JavaDirectBuffer();
private readonly frameMetricWaiters = new Set<(metrics: FrameMetrics) => void>();
// Recent frame-finished outcomes from the Java side, so waitForFrameFinished
// does not race against finishes that land before the wait starts.
@@ -538,7 +546,10 @@ export class FaceclawCommunicatorBridge {
*/
async submitShellScene(bytes: Uint8Array, paintMs = 0, frameId = 0): Promise<void> {
const snapshot = new Uint8Array(bytes);
await this.enqueueJavaCall(() => this.communicator.submitShellScene(snapshot.buffer, paintMs, frameId), true);
await this.enqueueJavaCall(
() => this.communicator.submitShellScene(this.shellSceneBuffer.load(snapshot), paintMs, frameId),
true,
);
}
async setUnderlayDim(belowZOrder: number, factor: number): Promise<void> {
@@ -594,15 +605,17 @@ export class FaceclawCommunicatorBridge {
*/
glyphs: ArrayBuffer | null = null,
): Promise<void> {
// Snapshot because the Java call is deferred; the buffer is passed as an
// ArrayBuffer, which NativeScript marshals to a ByteBuffer without the
// ~150ms per-element copy a byte[] parameter would need.
// Snapshot because the Java call is deferred. The bytes reach Java as a
// ByteBuffer (no ~150ms per-element copy, as a byte[] parameter would
// need), but through a reused Java direct buffer rather than by passing
// the ArrayBuffer itself, which NativeScript never frees
// (java-direct-buffer.ts).
const snapshot = new Uint8Array(pixels8bpp);
// inlineWhenIdle: this is the frame path, the Java side of it measures
// ~5ms (composite + pack), and the caller awaits it either way.
await this.enqueueJavaCall(() => {
this.communicator.submitSurfaceFrame(
snapshot.buffer,
this.framePixelsBuffer.load(snapshot),
surfaceId,
Math.round(rect.x),
Math.round(rect.y),
@@ -611,7 +624,7 @@ export class FaceclawCommunicatorBridge {
fingerprint,
Math.round(nonNegativeNumber(paintMs)),
Math.round(nonNegativeNumber(frameId)),
glyphs,
this.frameDrawsBuffer.loadOptional(glyphs),
);
}, true);
}
@@ -673,7 +686,7 @@ export class FaceclawCommunicatorBridge {
async playBuzzerSequence(payload: Uint8Array): Promise<void> {
const snapshot = new Uint8Array(payload);
await this.enqueueJavaCall(() => {
this.communicator.playBuzzerSequence(snapshot.buffer);
this.communicator.playBuzzerSequence(this.buzzerBuffer.load(snapshot));
});
}
+4 -4
View File
@@ -5,6 +5,7 @@
* reasonable for a sideloaded personal tool).
*/
import { Utils } from "@nativescript/core";
import { copyToJavaByteBuffer } from "./java-direct-buffer";
declare const android: any;
declare const java: any;
@@ -150,10 +151,9 @@ export function writeBinaryFile(path: string, bytes: Uint8Array): boolean {
if (parent) parent.mkdirs();
const stream = new java.io.FileOutputStream(file);
try {
// An ArrayBuffer marshals to java.nio.ByteBuffer (the same conversion
// submitSurfaceFrame relies on), so write through the channel.
const buffer = bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength);
stream.getChannel().write(buffer);
// Write through the channel from a Java direct buffer: a bulk copy, and
// passing the ArrayBuffer itself would leak it (java-direct-buffer.ts).
stream.getChannel().write(copyToJavaByteBuffer(bytes));
} finally {
stream.close();
}
+7 -2
View File
@@ -1,3 +1,8 @@
import { copyToJavaByteBuffer } from './java-direct-buffer'
declare const com: any
export function firmwareSha256(buffer: ArrayBuffer): string { return String(com.faceclaw.app.FaceclawFirmwareUtil.sha256Hex(buffer)) }
export function writeFirmwareFile(path: string, buffer: ArrayBuffer): void { com.faceclaw.app.FaceclawFirmwareUtil.writeFile(path, buffer) }
// The image crosses as a fresh Java buffer: passing a JS ArrayBuffer to Java
// leaks it (java-direct-buffer.ts).
export function firmwareSha256(buffer: ArrayBuffer): string { return String(com.faceclaw.app.FaceclawFirmwareUtil.sha256Hex(copyToJavaByteBuffer(new Uint8Array(buffer)))) }
export function writeFirmwareFile(path: string, buffer: ArrayBuffer): void { com.faceclaw.app.FaceclawFirmwareUtil.writeFile(path, copyToJavaByteBuffer(new Uint8Array(buffer))) }
+6 -5
View File
@@ -1,16 +1,17 @@
import { ImageSource } from "@nativescript/core";
import { type GrayImage } from "../graphics/image";
import { copyToJavaByteBuffer } from "./java-direct-buffer";
declare const com: any;
const PREVIEW_BRIGHTEN_GAMMA = 0.7;
/**
* Build the phone-UI preview for a frame. The pixel buffer is passed to Java
* as an ArrayBuffer (marshalled to a ByteBuffer) and expanded to ARGB there;
* doing the per-pixel work in JS or copying element-by-element across the
* bridge costs ~150ms per frame.
* Build the phone-UI preview for a frame. The pixels are copied into a Java
* direct ByteBuffer and expanded to ARGB there; doing the per-pixel work in JS
* or copying element-by-element across the bridge costs ~150ms per frame, and
* passing the ArrayBuffer itself would leak it (java-direct-buffer.ts).
*/
export function grayImageToPreviewSource(image: GrayImage): ImageSource | null {
if (!global.isAndroid) {
@@ -19,7 +20,7 @@ export function grayImageToPreviewSource(image: GrayImage): ImageSource | null {
const baked = image.withDrawsBaked();
const bitmap = com.faceclaw.app.PreviewBitmapUtil.fromGray(
baked.pixels.buffer,
copyToJavaByteBuffer(baked.pixels),
baked.width,
baked.height,
PREVIEW_BRIGHTEN_GAMMA,
+3 -1
View File
@@ -1,7 +1,9 @@
import { grayImageFromPacket } from './image-files'
import { type GrayImage } from '../graphics/image'
import { copyToJavaByteBuffer } from './java-direct-buffer'
declare const com: any
/** The encoded bytes cross as a fresh Java buffer; passing the ArrayBuffer leaks it (java-direct-buffer.ts). */
export function decodeImageBytes(bytes: ArrayBuffer, width: number, height: number): GrayImage | null {
return grayImageFromPacket(com.faceclaw.app.ImageFileLoader.loadGrayFromBytes(bytes, width, height))
return grayImageFromPacket(com.faceclaw.app.ImageFileLoader.loadGrayFromBytes(copyToJavaByteBuffer(new Uint8Array(bytes)), width, height))
}
+112
View File
@@ -0,0 +1,112 @@
/**
* Hands bytes to a Java `java.nio.ByteBuffer` parameter without leaking them.
*
* Why this exists: NativeScript Android (9.0.4 through at least 9.1.1)
* converts a JS ArrayBuffer or typed array passed to a Java method by wrapping
* its backing store in `NewDirectByteBuffer` and taking a JNI global ref that is
* never deleted (JsArgConverter.cpp, `buffer = env.NewGlobalRef(buffer)`). The
* ByteBuffer is also linked to the JS ArrayBuffer as its native counterpart.
* The leaked global ref keeps the ByteBuffer reachable forever, so the
* runtime's GC handshake (Runtime.makeInstanceWeakAndCheckIfAlive) always says
* "alive", the JS finalizer re-arms, and V8 never frees the ArrayBuffer. Every
* call leaked its whole buffer plus one global ref.
*
* On the frame path that meant a full surface (576x260, 640x452 or 640x480
* bytes) per submitted frame, including frames the Java side then discards as
* unchanged. Measured on a Galaxy Z Fold7, native-heap growth matched frame
* bytes submitted to within a few percent, and Samsung's Heimdall memory
* manager killed the app at about 6.4 GB. ART also aborts the process once
* 51,200 global refs are held, so the ref leak is a second ceiling behind the
* memory one.
*
* The workaround keeps the conversion out of the call: the bytes are copied
* into a Java-allocated direct ByteBuffer that is reused, through an
* ArrayBuffer view made once with `ArrayBuffer.from` (an external backing
* store over the Java memory, no global ref), and the Java ByteBuffer object
* itself is passed. A JS proxy for an existing Java object takes the ordinary
* object path, which does not leak.
*
* Contract: the buffer returned by load() is only valid until the next load()
* on the same instance, so the Java callee must consume it synchronously and
* not keep a reference. The Kotlin consumers (SurfaceCompositor via
* AndroidByteReader, playBuzzerSequence, ShellScene.decode) copy or parse
* before returning, which satisfies this. One instance belongs to one JS
* context (main thread or one worker); instances are not shared across
* isolates.
*/
declare const java: any;
/** Minimal surface of java.nio.ByteBuffer this module uses. */
export interface JavaByteBufferLike {
clear(): unknown;
limit(newLimit: number): unknown;
}
/** How to allocate a direct buffer and view its memory; injectable for tests. */
export interface DirectBufferBackend {
allocateDirect(capacity: number): JavaByteBufferLike;
viewOf(buffer: JavaByteBufferLike): Uint8Array;
}
const nativeScriptBackend: DirectBufferBackend = {
allocateDirect: (capacity) => java.nio.ByteBuffer.allocateDirect(capacity),
viewOf: (buffer) => new Uint8Array((ArrayBuffer as any).from(buffer)),
};
/** Capacity grows in steps of this many bytes, so a slowly growing payload does not reallocate every call. */
const CAPACITY_STEP_BYTES = 4096;
export class JavaDirectBuffer {
private javaBuffer: JavaByteBufferLike | null = null;
private view: Uint8Array | null = null;
/** Number of Java buffers allocated so far (a test and diagnostics hook). */
allocations = 0;
constructor(
private readonly initialCapacity = 0,
private readonly backend: DirectBufferBackend = nativeScriptBackend,
) {}
/**
* Copy `bytes` into the reused Java buffer and return it with position 0 and
* limit `bytes.length`, so `remaining()` equals the byte count. Valid until
* the next load() on this instance.
*/
load(bytes: Uint8Array): JavaByteBufferLike {
if (!this.javaBuffer || !this.view || this.view.length < bytes.length) {
const wanted = Math.max(bytes.length, this.initialCapacity, 1);
const capacity = Math.ceil(wanted / CAPACITY_STEP_BYTES) * CAPACITY_STEP_BYTES;
this.javaBuffer = this.backend.allocateDirect(capacity);
this.view = this.backend.viewOf(this.javaBuffer);
this.allocations++;
}
this.view.set(bytes);
this.javaBuffer.clear();
this.javaBuffer.limit(bytes.length);
return this.javaBuffer;
}
/** As load(), for an optional ArrayBuffer (e.g. a frame's draw list); null stays null. */
loadOptional(buffer: ArrayBuffer | null | undefined): JavaByteBufferLike | null {
if (!buffer || buffer.byteLength === 0) return null;
return this.load(new Uint8Array(buffer));
}
}
/**
* Copy `bytes` into a fresh Java direct buffer (position 0, limit
* `bytes.length`) for a one-off crossing such as an atlas registration or a
* file write. Unlike a JavaDirectBuffer nothing is pinned between calls: the
* Java buffer is an ordinary object, freed once the JS side drops it.
*/
export function copyToJavaByteBuffer(
bytes: Uint8Array,
backend: DirectBufferBackend = nativeScriptBackend,
): JavaByteBufferLike {
// A zero-capacity direct buffer may have no address to view.
const javaBuffer = backend.allocateDirect(Math.max(bytes.length, 1));
backend.viewOf(javaBuffer).set(bytes);
javaBuffer.limit(bytes.length);
return javaBuffer;
}
+5 -3
View File
@@ -1,3 +1,5 @@
import { copyToJavaByteBuffer } from './java-direct-buffer'
declare const com: any
/** Development-only checks of NativeScript metadata and bulk ByteBuffer crossings. */
@@ -10,11 +12,11 @@ export function runKotlinProtocolSmokeTest(): void {
const compositor = new native.SurfaceCompositor()
compositor.configureScreen(3, 2)
compositor.configureSurface('test', 0, 0, 3, 2, 0, 0)
const pixels = new Uint8Array([0, 16, 255, 32, 48, 64])
compositor.submitSurface('test', new native.AndroidByteReader(pixels.buffer), 0, 0, 3, 2, '1')
const pixels = copyToJavaByteBuffer(new Uint8Array([0, 16, 255, 32, 48, 64]))
compositor.submitSurface('test', new native.AndroidByteReader(pixels), 0, 0, 3, 2, '1')
assert(hex(compositor.composite().gray) === '0010ff203040', 'composition/ByteBuffer')
assert(hex(native.BmpUtil.pack4bppFromGray8(compositor.composite().gray, 3, 2)) === '01f02340', 'packing')
const id = native.ImageAtlas.ensure('kotlin-smoke', 3, 2, new native.AndroidByteReader(pixels.buffer))
const id = native.ImageAtlas.ensure('kotlin-smoke', 3, 2, new native.AndroidByteReader(pixels))
assert(id > 0 && native.ImageAtlas.get(id).width === 3, 'image atlas/reused buffer')
console.log('FACECLAW_KOTLIN_PROTOCOL_PASS Android')
}
+12 -6
View File
@@ -1,5 +1,6 @@
import { ImageSource, Utils } from "@nativescript/core";
import { dimFactor256, type FaceclawCommunicatorBridge, type SurfaceOptions } from "./faceclaw-communicator";
import { JavaDirectBuffer } from "./java-direct-buffer";
declare const com: any;
declare const java: any;
@@ -49,6 +50,11 @@ function nonNegativeNumber(value: number): number {
*/
export class PreviewDisplayTarget implements DisplayTarget {
private readonly compositor: any;
// Reused Java-side buffers: passing a JS ArrayBuffer to Java leaks it
// (java-direct-buffer.ts).
private readonly framePixelsBuffer = new JavaDirectBuffer(640 * 480);
private readonly frameDrawsBuffer = new JavaDirectBuffer();
private readonly shellSceneBuffer = new JavaDirectBuffer();
constructor() {
const context = Utils.android.getApplicationContext();
@@ -104,7 +110,7 @@ export class PreviewDisplayTarget implements DisplayTarget {
}
async submitShellScene(bytes: Uint8Array, paintMs = 0, frameId = 0): Promise<void> {
this.compositor.submitShellScene(new Uint8Array(bytes).buffer, paintMs, frameId);
this.compositor.submitShellScene(this.shellSceneBuffer.load(bytes), paintMs, frameId);
}
async setUnderlayDim(belowZOrder: number, factor: number): Promise<void> {
@@ -124,11 +130,11 @@ export class PreviewDisplayTarget implements DisplayTarget {
frameId = 0,
glyphs: ArrayBuffer | null = null,
): Promise<void> {
// Copy for the same reason the bridge does: the source may be a view on a
// larger buffer, and Java receives the backing ArrayBuffer.
const snapshot = new Uint8Array(pixels8bpp);
// Copied into a reused Java direct buffer, sized to exactly this view's
// bytes; passing the ArrayBuffer itself would leak it
// (java-direct-buffer.ts).
this.compositor.submitSurfaceFrame(
snapshot.buffer,
this.framePixelsBuffer.load(pixels8bpp),
surfaceId,
Math.round(rect.x),
Math.round(rect.y),
@@ -137,7 +143,7 @@ export class PreviewDisplayTarget implements DisplayTarget {
fingerprint,
Math.round(nonNegativeNumber(paintMs)),
Math.round(nonNegativeNumber(frameId)),
glyphs,
this.frameDrawsBuffer.loadOptional(glyphs),
);
// The Java side finishes the frame: the composite is the end of the line,
// and worker isolates submit to the same object without coming through here.
+8 -4
View File
@@ -1,17 +1,21 @@
import { copyToJavaByteBuffer } from './java-direct-buffer'
declare const com: any
// Byte payloads cross as fresh Java buffers: passing a JS ArrayBuffer to Java
// leaks it (java-direct-buffer.ts).
/** Android bridge to the process-wide shared Kotlin atlases. */
export function textureAtlasAvailable(): boolean { return !!global.isAndroid }
export function textureFontId(key: string): number { return com.faceclaw.app.GlyphAtlas.fontId(key) }
export function registerTextureGlyphs(buffer: ArrayBuffer, aa: boolean): void {
const reader = new com.faceclaw.app.AndroidByteReader(buffer)
const reader = new com.faceclaw.app.AndroidByteReader(copyToJavaByteBuffer(new Uint8Array(buffer)))
if (aa) com.faceclaw.app.GlyphAtlas.registerAa(reader)
else com.faceclaw.app.GlyphAtlas.register(reader)
}
export function registerFirmwareGlyphs(buffer: ArrayBuffer): void {
com.faceclaw.app.FwGlyphAtlas.register(new com.faceclaw.app.AndroidByteReader(buffer))
com.faceclaw.app.FwGlyphAtlas.register(new com.faceclaw.app.AndroidByteReader(copyToJavaByteBuffer(new Uint8Array(buffer))))
}
export function textureImageId(key: string, width: number, height: number, pixels: Uint8Array): number {
const buffer = pixels.byteOffset === 0 && pixels.byteLength === pixels.buffer.byteLength ? pixels.buffer : pixels.slice().buffer
return com.faceclaw.app.ImageAtlas.ensure(key, width, height, new com.faceclaw.app.AndroidByteReader(buffer))
return com.faceclaw.app.ImageAtlas.ensure(key, width, height, new com.faceclaw.app.AndroidByteReader(copyToJavaByteBuffer(pixels)))
}
+6 -1
View File
@@ -1,12 +1,17 @@
import type { WorkerAppReply } from "../ui/shell/worker-window";
import { JavaDirectBuffer } from "./java-direct-buffer";
declare const com: any;
// Reused Java-side buffer for this isolate's tone payloads: passing a JS
// ArrayBuffer to Java leaks it (java-direct-buffer.ts).
const buzzerBuffer = new JavaDirectBuffer();
/** Workers use the main-thread BLE session on iOS and the native queue on Android. */
export function playWorkerBuzzerSequence(payload: Uint8Array): void {
if (global.isIOS) {
global.postMessage({ type: "buzzer-sequence", payload: Array.from(payload) } satisfies WorkerAppReply);
} else {
com.faceclaw.app.FaceclawBleCommunicator.getActive()?.playBuzzerSequence(new Uint8Array(payload).buffer);
com.faceclaw.app.FaceclawBleCommunicator.getActive()?.playBuzzerSequence(buzzerBuffer.load(payload));
}
}
+573
View File
@@ -0,0 +1,573 @@
import { DrawExpression as E } from "../graphics/draw-expression";
import { DrawOp, type DisplayList, type ListCall, type ListClip } from "../graphics/display-list";
import { encodeReplayDraws } from "../graphics/glyph-wire";
import { GrayImage, type UiFont } from "../graphics/image";
import { slidingCoordinate } from "../graphics/menu-scroll-list";
import { getDefaultSmallFont } from "../graphics/ui-fonts";
import { truncateText } from "../graphics/textwrap";
import { clamp } from "../util/numeric-util";
import { isWatchInput, type InputEvent } from "./gestures";
import type { LayerContext } from "./layers";
import { drawListScrollbar, drawSelectionHighlight, scrollToKeepSelectionVisible } from "./menu";
import { BOUNCE_FRACTION, MAX_BOUNCE, MENU_HIGHLIGHT_FILL, MENU_HIGHLIGHT_STROKE, type MenuBox } from "./menu-core";
import { MenuHighlightMotion, type MenuHighlightAnimation } from "./menu-highlight-motion";
import { MenuScrollMotion, scrollOffsetExpression, type MenuScrollAnimation } from "./menu-scroll-motion";
import { iconGridMinRowHeight } from "./metrics";
import { shell } from "./shell/shell";
/** Size of the square each grid cell reserves for its icon; render icons at this size. */
export const ICON_GRID_ICON_SIZE = 44;
const LABEL_GAP = 2;
const DEFAULT_COLUMNS = 5;
const DEFAULT_LABEL_VALUE = 210;
/** Wide rows: text inset from the grid's left and right edges. */
const WIDE_TEXT_X = 20;
/** The scrollbar's columns at the right edge, which animations leave alone. */
const SCROLLBAR_GUTTER = 5;
export type IconGridCell = {
label: string;
/**
* Icon for a grid cell (ignored for wide rows). Drawn as a deferred image,
* so it must be long-lived and never mutated: a memoized render, not a
* scratch buffer. Icons smaller than ICON_GRID_ICON_SIZE are centered in
* the square.
*/
icon?: GrayImage | null;
/** Label brightness. Default 210. */
labelValue?: number;
};
export type IconGridOptions<T> = {
/** The items to show, fetched fresh at every paint, input, and selection call. */
items: () => readonly T[];
/** Columns per grid row. Default 5. */
columns?: number;
/** How to draw an item. `selected` is true while it alone is selected (item mode, or a wide row). */
describe: (item: T, selected: boolean) => IconGridCell;
/**
* Items drawn as a full-width text row of their own (a notice or prompt)
* instead of a grid cell. Selection treats such a row as one item.
*/
isWide?: (item: T) => boolean;
/**
* Open an item. Return true when this left the grid (e.g. launched an app
* or yielded focus) rather than navigating within it; see onBack.
*/
onActivate: (item: T, index: number, ctx: LayerContext) => boolean | void | Promise<boolean | void>;
/**
* Back out one level: a ring double-click in row mode, or the watch's back
* or left swipe from the first column. Return true when this left the grid.
*
* After either callback the grid settles its mode: leaving puts it back in
* row mode, so a ring user returning finds it as they left it; staying
* with watch input keeps single-item selection (which the watch scheme
* always uses), even if the host reset the selection while navigating.
*/
onBack: (ctx: LayerContext) => boolean | void;
/** Draw a scrollbar at the right edge when rows overflow. Default false. */
scrollbar?: boolean;
};
type GridRow<T> =
| { kind: "wide"; item: T; firstIndex: number }
| { kind: "cells"; items: T[]; firstIndex: number };
type GridMode = "row" | "item";
type GridLayout = { box: MenuBox; rowH: number; colW: number };
/**
* The selection highlight in image coordinates. `key` names what is
* selected for highlight motion: a flat item index, or -1 - row for a
* row-mode band.
*/
type Highlight = { key: number; x: number; y: number; width: number; height: number; radius: number };
/**
* An icon grid: items laid out as labeled icons in fixed-width columns, with
* optional full-width text rows (see isWide). Like Menu, it is independent of
* the Layer stack: the host paints it into a rect of its own image and
* forwards input, and keeps the rest (header, what items mean, where back
* goes).
*
* Rows are exactly as tall as their content (icon, label line, breathing
* room), so they grow smoothly with the font; they never stretch to fill the
* box, which would make the padding jump whenever a font step changed how
* many rows fit. Space left below the last full row shows the top of the
* next row, cut off at the box's edge, as a hint that there is more to scroll to.
*
* Ring input navigates on two levels, halving the scrolls to reach an item:
* in row mode scroll picks a row and a click drops into item mode on it,
* defaulting to the middle column; in item mode scroll traverses items
* linearly (continuing onto adjacent rows) and a click opens the item.
* Double-click backs out: item mode to row mode, then onBack. A wide row is
* opened by a click in either mode. Watch input skips row mode and moves one
* cell at a time in four directions: up/down (and the crown) between rows
* keeping the column, right/left within the row, and left from the first
* column (or a wide row) backs out, as does the watch's back.
*
* Navigation animates on the glasses as menus do: the highlight slides to
* the new selection, rows scroll into view, and moving past an end bounces.
* A grid is too big for Menu's pre-rendered strip, so the animation is a
* display list that clears the grid and redraws it from the rows' glyph and
* icon draws, offset by the animated scroll and clipped to the box
* (revision 35 firmware). Its last frame equals the static paint. When a
* row can't be replayed that way (a label outside ASCII, say), it snaps.
*/
export class IconGrid<T> {
private mode: GridMode = "row";
private selectedRow = 0;
private selectedCol = 0;
private scrollRow = 0;
/** Geometry of the last paint, for hitTest and bounces: what the phone mirror showed. */
private layout: GridLayout | null = null;
private readonly highlightMotion = new MenuHighlightMotion();
private readonly scrollMotion = new MenuScrollMotion();
constructor(private readonly options: IconGridOptions<T>) {}
private get columns(): number {
return this.options.columns ?? DEFAULT_COLUMNS;
}
/** Back to row mode on the first row, scrolled to the top (e.g. after entering a directory). */
resetSelection(): void {
this.mode = "row";
this.selectedRow = 0;
this.selectedCol = 0;
this.scrollRow = 0;
}
/** Drop from item mode to row mode, keeping the selected row. */
enterRowMode(): void {
this.mode = "row";
}
/**
* Move the selection onto the item at a flat index (e.g. the directory
* just left), leaving the mode and scroll position alone; the next paint
* scrolls the minimum needed to show it.
*/
selectIndex(index: number): void {
const rows = this.buildRows();
const rowIndex = rows.findIndex((row) => index >= row.firstIndex && index < row.firstIndex + rowLength(row));
if (rowIndex < 0) return;
this.selectedRow = rowIndex;
this.selectedCol = index - rows[rowIndex]!.firstIndex;
}
/**
* Flat index of the item under the selection. In row mode on a grid row
* that is the cell at the remembered column, though no single item is
* selected yet (see selectedItem). Null when there are no items.
*/
get cursorIndex(): number | null {
const row = this.currentRow();
if (!row) return null;
return row.kind === "wide" ? row.firstIndex : row.firstIndex + clamp(this.selectedCol, 0, row.items.length - 1);
}
/** The single selected item: a wide row, or a cell in item mode. Null in row mode on a grid row. */
get selectedItem(): T | null {
const row = this.currentRow();
if (!row) return null;
if (row.kind === "wide") return row.item;
if (this.mode !== "item") return null;
return row.items[clamp(this.selectedCol, 0, row.items.length - 1)] ?? null;
}
/**
* Focus arriving from the watch goes straight to item selection: the watch
* has left/right swipes, so it never needs row mode, and starting there
* would paint a row band its scheme can't produce. Any other source enters
* in row mode.
*/
onFocus(lastInput: InputEvent | null): void {
if (lastInput && isWatchInput(lastInput)) {
this.mode = "item";
this.clampSelection(this.buildRows());
} else {
this.mode = "row";
}
}
paint(image: GrayImage, box: MenuBox, focused: boolean): void {
const font = getDefaultSmallFont();
const rows = this.buildRows();
this.clampSelection(rows);
const rowH = iconGridMinRowHeight(font, ICON_GRID_ICON_SIZE, LABEL_GAP);
const fullRows = Math.max(1, Math.floor(box.height / rowH));
// Scroll to keep the selected row among the fully-visible rows.
this.scrollRow = scrollToKeepSelectionVisible(this.scrollRow, this.selectedRow, fullRows, rows.length);
const layout: GridLayout = { box, rowH, colW: box.width / this.columns };
this.layout = layout;
const highlight = this.highlight(rows, layout, font, focused);
const bottom = box.y + box.height;
for (let rowIndex = this.scrollRow; rowIndex < rows.length; rowIndex++) {
const y = box.y + (rowIndex - this.scrollRow) * rowH;
if (y >= bottom) break;
if (highlight && rowIndex === this.selectedRow) {
drawSelectionHighlight(image, highlight.x, highlight.y, highlight.width, highlight.height, focused, highlight.radius);
}
this.drawRow(image, rows[rowIndex]!, rowIndex, box.x, y, layout, font, bottom);
}
const scrollbar = !!this.options.scrollbar && rows.length > fullRows;
if (scrollbar) {
drawListScrollbar(image, box.x + box.width - SCROLLBAR_GUTTER, box.y, box.height, this.scrollRow, fullRows, rows.length);
}
this.paintMotion(image, rows, layout, font, focused, highlight, scrollbar);
}
async handleInput(event: InputEvent, ctx: LayerContext): Promise<void> {
if (isWatchInput(event)) {
await this.handleWatchInput(event, ctx);
} else {
await this.handleRingInput(event, ctx);
}
}
/**
* A touch on the phone's mirror: the item under it becomes the selection
* and opens, as a ring click on it would. Uses the last paint's geometry,
* so it lands on what the mirror showed.
*/
async hitTest(x: number, y: number, ctx: LayerContext): Promise<boolean> {
if (!this.layout) return false;
const { box, rowH, colW } = this.layout;
if (x < box.x || x >= box.x + box.width || y < box.y || y >= box.y + box.height) return false;
const rowIndex = this.scrollRow + Math.floor((y - box.y) / rowH);
const row = this.buildRows()[rowIndex];
if (!row) return false;
if (row.kind === "cells") {
const col = Math.floor((x - box.x) / colW);
if (col >= row.items.length) return false;
this.selectedCol = col;
}
this.selectedRow = rowIndex;
this.mode = "item";
await this.activateSelected(false, ctx);
return true;
}
/**
* The highlight for the current selection: a row band, a single cell in
* item mode, or a box around a wide row's text. While defocused with the
* watch as the last-used source, the cell outline is shown even in row
* mode: watch focus enters item mode directly (see onFocus), so the
* outline previews the cell a click would land on rather than a row band
* the watch scheme never shows. Every cell's box has the same width, so
* the highlight can slide between any two.
*/
private highlight(rows: readonly GridRow<T>[], layout: GridLayout, font: UiFont, focused: boolean): Highlight | null {
const row = rows[this.selectedRow];
if (!row) return null;
const { box, rowH, colW } = layout;
const y = box.y + (this.selectedRow - this.scrollRow) * rowH;
if (row.kind === "wide") {
const textY = y + wideTextOffset(rowH, font);
return { key: row.firstIndex, x: box.x + WIDE_TEXT_X - 6, y: textY - 2,
width: box.width - 2 * WIDE_TEXT_X + 12, height: font.lineHeight + 4, radius: 4 };
}
if (this.mode === "item" || (!focused && shell.lastInputWasWatch())) {
return { key: row.firstIndex + this.selectedCol, x: Math.round(box.x + this.selectedCol * colW) + 6, y: y + 2,
width: Math.round(colW) - 12, height: rowH - 4, radius: 6 };
}
return { key: -1 - this.selectedRow, x: box.x + 4, y: y + 2, width: box.width - 8, height: rowH - 4, radius: 6 };
}
/**
* Draw one row's icons and labels (not the highlight) with its top at y.
* A row crossing `clipBottom` goes through a scratch image cut off there,
* so the static peeking row matches an animation's clipped frames.
*/
private drawRow(target: GrayImage, row: GridRow<T>, rowIndex: number, x0: number, y: number,
layout: GridLayout, font: UiFont, clipBottom?: number): void {
const { rowH, colW } = layout;
if (clipBottom !== undefined && y + rowH > clipBottom) {
const scratch = new GrayImage(layout.box.width, rowH);
this.drawRow(scratch, row, rowIndex, 0, 0, layout, font);
target.bitBlt(scratch.withDrawsBaked(), x0, y, { height: clipBottom - y, transparentZero: true });
return;
}
const rowSelected = rowIndex === this.selectedRow;
if (row.kind === "wide") {
const { label, labelValue = DEFAULT_LABEL_VALUE } = this.options.describe(row.item, rowSelected);
const width = layout.box.width - 2 * WIDE_TEXT_X;
target.drawText(font, x0 + WIDE_TEXT_X, y + wideTextOffset(rowH, font), truncateText(font, label, width), labelValue);
return;
}
const blockTop = y + Math.max(2, (rowH - ICON_GRID_ICON_SIZE - font.lineHeight - LABEL_GAP) / 2);
for (let col = 0; col < row.items.length; col++) {
const cell = this.options.describe(row.items[col]!, rowSelected && this.mode === "item" && col === this.selectedCol);
const centerX = x0 + col * colW + colW / 2;
if (cell.icon) {
const icon = cell.icon;
target.drawImage(icon, Math.round(centerX - icon.width / 2),
Math.round(blockTop + Math.max(0, (ICON_GRID_ICON_SIZE - icon.height) / 2)));
}
const label = truncateText(font, cell.label, colW - 8);
target.drawText(font, Math.round(centerX - font.measureText(label) / 2), Math.round(blockTop + ICON_GRID_ICON_SIZE + LABEL_GAP),
label, cell.labelValue ?? DEFAULT_LABEL_VALUE);
}
}
/**
* Track this paint's scroll offset and highlight, and while either is
* moving, retain the display list that animates them over the static paint.
*/
private paintMotion(image: GrayImage, rows: readonly GridRow<T>[], layout: GridLayout, font: UiFont,
focused: boolean, highlight: Highlight | null, scrollbar: boolean): void {
const now = Date.now();
const scrollOffset = this.scrollRow * layout.rowH;
const scroll = this.scrollMotion.paint(scrollOffset, layout.box, now);
const slide = highlight
? this.highlightMotion.paint(highlight.key, scrollOffset, highlight.x, highlight.y, highlight.width, highlight.height, now, !!scroll)
: undefined;
if (!scroll && !slide) return;
const { box } = layout;
const clip: ListClip = { x: 0, y: 0, width: box.width - (scrollbar ? SCROLLBAR_GUTTER : 0), height: box.height };
const list = this.motionList(rows, layout, font, focused, highlight, clip, scroll, slide);
if (!list) {
this.scrollMotion.cancel();
this.highlightMotion.cancel();
return;
}
image.drawDisplayList(list, box.x, box.y, clip.width, clip.height);
}
/**
* The animation, in box coordinates: clear the box, draw the highlight
* (sliding, and carried along by a bounce), then replay every row visible
* at any point of the scroll, offset by it. Null when those rows hold
* anything the glasses can't replay.
*/
private motionList(rows: readonly GridRow<T>[], layout: GridLayout, font: UiFont, focused: boolean,
highlight: Highlight | null, clip: ListClip, scroll: MenuScrollAnimation | undefined,
slide: MenuHighlightAnimation | undefined): DisplayList | null {
const { box, rowH } = layout;
const scrollOffset = this.scrollRow * rowH;
const offsets = scroll ? [scroll.from, scroll.to, scroll.peak ?? scroll.to] : [scrollOffset];
const firstRow = Math.max(0, Math.floor(Math.min(...offsets) / rowH));
const lastRow = Math.min(rows.length, Math.ceil((Math.max(...offsets) + box.height) / rowH));
const content = new GrayImage(box.width, Math.max(1, (lastRow - firstRow) * rowH));
for (let rowIndex = firstRow; rowIndex < lastRow; rowIndex++) {
this.drawRow(content, rows[rowIndex]!, rowIndex, 0, (rowIndex - firstRow) * rowH, layout, font);
}
if (content.pixels.some((value) => value !== 0)) return null;
const replay = encodeReplayDraws(content.draws);
if (!replay) return null;
// One timeline: the newer motion's. The other, if any, continues on it with a negative delay.
const timeline = scroll && (!slide || scroll.startedAt >= slide.startedAt) ? scroll : slide!;
const offset = scroll ? scrollOffsetExpression(scroll, timeline.startedAt) : E.i32(scrollOffset);
const calls: ListCall[] = [{ op: DrawOp.CLEAR, color: 0, clip }];
if (highlight) {
let y = slidingCoordinate(highlight.y - box.y, slide?.dy ?? 0, slide, timeline.startedAt);
// A bounce carries the highlight with its row.
if (scroll?.peak !== undefined) y = y.add(E.i32(scroll.to)).sub(offset);
const q = (gray: number) => Math.min(15, (gray + 8) >> 4);
calls.push({ op: DrawOp.ROUNDED_RECT, x: slidingCoordinate(highlight.x - box.x, slide?.dx ?? 0, slide, timeline.startedAt), y,
width: highlight.width, height: highlight.height, radius: highlight.radius,
background: focused ? q(MENU_HIGHLIGHT_FILL) : 0, border: q(MENU_HIGHLIGHT_STROKE), clip });
}
// Without a scroll the rows stay put; a literal keeps every compiled call small.
const rowsY = scroll ? E.i32(firstRow * rowH).sub(offset) : (firstRow - this.scrollRow) * rowH;
calls.push({ op: DrawOp.DRAWS, x: 0, y: rowsY, records: replay.records, count: replay.count, source: content, clip });
return { resources: [], timeline: { token: timeline.token, startedAt: timeline.startedAt }, calls };
}
private async handleRingInput(event: InputEvent, ctx: LayerContext): Promise<void> {
const rows = this.buildRows();
this.clampSelection(rows);
const row = rows[this.selectedRow];
const from = this.selectionKey(rows);
switch (event.type) {
case "scroll-up":
case "scroll-down": {
const delta = event.type === "scroll-down" ? 1 : -1;
if (this.mode === "row") {
const next = this.selectedRow + delta;
if (next < 0 || next >= rows.length) {
this.bounce(delta);
return;
}
this.selectedRow = next;
this.moved(from);
return;
}
// Past a row's edge, item selection continues onto the adjacent row
// (a wide row counts as a single item), stopping at the grid's ends.
if (row?.kind === "cells") {
const next = this.selectedCol + delta;
if (next >= 0 && next < row.items.length) {
this.selectedCol = next;
this.moved(from);
return;
}
}
const adjacent = rows[this.selectedRow + delta];
if (!adjacent) {
this.bounce(delta);
return;
}
this.selectedRow += delta;
if (adjacent.kind === "cells") {
this.selectedCol = delta > 0 ? 0 : adjacent.items.length - 1;
}
this.moved(from);
return;
}
case "click":
if (!row) return;
if (row.kind === "cells" && this.mode === "row") {
this.mode = "item";
this.selectedCol = Math.min(Math.floor(this.columns / 2), row.items.length - 1);
return;
}
await this.activateSelected(false, ctx);
return;
case "double-click":
if (this.mode === "item") {
this.mode = "row";
} else {
this.settleAfter(this.options.onBack(ctx) === true, false);
}
return;
default:
return;
}
}
private async handleWatchInput(event: InputEvent, ctx: LayerContext): Promise<void> {
const rows = this.buildRows();
this.mode = "item";
this.clampSelection(rows);
const row = rows[this.selectedRow];
const from = this.selectionKey(rows);
const back = () => this.settleAfter(this.options.onBack(ctx) === true, true);
switch (event.type) {
case "swipe-up":
case "swipe-down":
case "scroll-up":
case "scroll-down": {
const delta = event.type === "swipe-down" || event.type === "scroll-down" ? 1 : -1;
const next = this.selectedRow + delta;
if (next < 0 || next >= rows.length) {
this.bounce(delta);
return;
}
this.selectedRow = next;
// Keep the column; a shorter row (or a wide one) clamps it.
this.clampSelection(rows);
this.moved(from);
return;
}
case "swipe-right":
if (row?.kind === "cells" && this.selectedCol + 1 < row.items.length) {
this.selectedCol++;
this.moved(from);
}
return;
case "swipe-left":
if (row?.kind === "cells" && this.selectedCol > 0) {
this.selectedCol--;
this.moved(from);
} else {
back();
}
return;
case "click":
await this.activateSelected(true, ctx);
return;
case "double-click":
back();
return;
default:
return;
}
}
/** The highlight key (see Highlight) of the selection while focused. */
private selectionKey(rows: readonly GridRow<T>[]): number | null {
const row = rows[this.selectedRow];
if (!row) return null;
if (row.kind === "wide") return row.firstIndex;
return this.mode === "item" ? row.firstIndex + this.selectedCol : -1 - this.selectedRow;
}
/** The selection just moved by navigation, from `from`: the next paint animates the change. */
private moved(from: number | null): void {
if (from !== null) this.highlightMotion.navigate(from, false);
this.scrollMotion.navigate(false);
}
/** Navigation past an end: overshoot a little and come back, as menus do. */
private bounce(delta: -1 | 1): void {
const rowH = this.layout?.rowH;
if (!rowH) return;
this.scrollMotion.bounce(delta * Math.min(MAX_BOUNCE, Math.round(rowH * BOUNCE_FRACTION)));
}
private async activateSelected(watch: boolean, ctx: LayerContext): Promise<void> {
const index = this.cursorIndex;
if (index === null) return;
const item = this.options.items()[index];
if (item === undefined) return;
this.settleAfter((await this.options.onActivate(item, index, ctx)) === true, watch);
}
/** The mode rule after a host callback; see IconGridOptions.onBack. */
private settleAfter(left: boolean, watch: boolean): void {
if (left) {
this.mode = "row";
} else if (watch) {
this.mode = "item";
this.clampSelection(this.buildRows());
}
}
private currentRow(): GridRow<T> | undefined {
const rows = this.buildRows();
return rows[clamp(this.selectedRow, 0, Math.max(0, rows.length - 1))];
}
private clampSelection(rows: readonly GridRow<T>[]): void {
this.selectedRow = clamp(this.selectedRow, 0, Math.max(0, rows.length - 1));
const row = rows[this.selectedRow];
this.selectedCol = row?.kind === "cells" ? clamp(this.selectedCol, 0, row.items.length - 1) : 0;
}
/** Wide items get a row of their own; runs of other items are chunked into rows of `columns`. */
private buildRows(): GridRow<T>[] {
const items = this.options.items();
const isWide = this.options.isWide;
const rows: GridRow<T>[] = [];
let cells: Extract<GridRow<T>, { kind: "cells" }> | null = null;
for (let index = 0; index < items.length; index++) {
const item = items[index]!;
if (isWide?.(item)) {
rows.push({ kind: "wide", item, firstIndex: index });
cells = null;
continue;
}
if (!cells || cells.items.length >= this.columns) {
cells = { kind: "cells", items: [], firstIndex: index };
rows.push(cells);
}
cells.items.push(item);
}
return rows;
}
}
function rowLength<T>(row: GridRow<T>): number {
return row.kind === "wide" ? 1 : row.items.length;
}
/** A wide row's text top, relative to the row's top. */
function wideTextOffset(rowH: number, font: UiFont): number {
return ((rowH - font.lineHeight) / 2) | 0;
}
+2 -2
View File
@@ -15,8 +15,8 @@ const SCROLLBAR_MIN_THUMB = 8;
/** Largest packed scroll strip, in bytes: the firmware's per-resource limit. */
const MAX_SCROLL_STRIP_BYTES = 65536;
/** A bounce overshoots by this fraction of the end row's pitch, as if starting to reveal another row. */
const BOUNCE_FRACTION = 0.4;
const MAX_BOUNCE = 24;
export const BOUNCE_FRACTION = 0.4;
export const MAX_BOUNCE = 24;
/** A rectangle in image coordinates. */
export type MenuBox = { x: number; y: number; width: number; height: number };
+5
View File
@@ -38,4 +38,9 @@ export class MenuHighlightMotion {
}
return this.animation && now - this.animation.startedAt < this.animation.durationMs ? this.animation : undefined;
}
/** Cancel an animation in flight, e.g. when it cannot be drawn; the next change snaps from where it rests. */
cancel(): void {
this.animation = undefined;
}
}
+5 -4
View File
@@ -32,13 +32,14 @@ export function scrollOffsetAt(animation: MenuScrollAnimation, t: number): numbe
}
/**
* The offset as a draw expression on the animation's own timeline. The easing
* ops clamp their input, which splits a bounce into its two phases without
* The offset as a draw expression, on a list timeline that started at
* `timelineStart` (by default the animation's own start). The easing ops
* clamp their input, which splits a bounce into its two phases without
* comparisons: the return term stays 0 until the peak, the outward one stays 1 after.
*/
export function scrollOffsetExpression(animation: MenuScrollAnimation) {
export function scrollOffsetExpression(animation: MenuScrollAnimation, timelineStart = animation.startedAt) {
const { from, to, peak } = animation;
const t = () => E.progress(animation.durationMs);
const t = () => E.progress(animation.durationMs, animation.startedAt - timelineStart);
if (peak === undefined) return t().ease().lerp(E.i32(from).toFloat(), E.i32(to).toFloat()).toInt();
const out = t().mul(E.f32(1 / BOUNCE_PEAK)).ease(ExprOp.EASE_OUT_QUAD);
const back = t().sub(E.f32(BOUNCE_PEAK)).div(E.f32(1 - BOUNCE_PEAK)).ease();
+5 -4
View File
@@ -100,10 +100,11 @@ export function menuTitleHeight(font: UiFont): number {
}
/**
* Minimum height of an icon-grid cell (icon, label line, breathing room).
* Grid views divide their available height into as many rows of at least
* this height as fit, so cells grow with the font instead of the label
* overflowing a fixed-height row.
* Height of an icon-grid row (icon, label line, breathing room). IconGrid
* uses it as the row pitch directly, so cells grow smoothly with the font
* instead of the label overflowing a fixed-height row. Don't stretch rows to
* fill the viewport: the padding would jump whenever a font step changes how
* many rows fit.
*/
export function iconGridMinRowHeight(font: UiFont, iconSize: number, labelGap: number): number {
return iconSize + labelGap + font.lineHeight + 8;
+32 -11
View File
@@ -1,15 +1,15 @@
import { G2_LENS_WIDTH, type GrayImage } from "../../graphics/image";
import { G2_LENS_WIDTH, GrayImage, type UiFont } from "../../graphics/image";
import { wrapText, truncateText } from "../../graphics/textwrap";
import { getDefaultSmallFont } from "../../graphics/ui-fonts";
import { Menu, type MenuDrawArgs } from "../menu-core";
import { listRowHeight } from "../metrics";
import { listRowHeight, textInkBounds } from "../metrics";
import { MIN_WINDOW_HEIGHT, minWindowTop } from "./geometry";
/**
* The shared look of the shell's text dialogs (voice input, phone keyboard
* input, and the assistant's reply): a solid box over whatever is on screen with a title line,
* a status line, the message so far, and either a menu of destinations at
* the bottom or a gesture hint.
* input, and the assistant's reply): a solid box over whatever is on screen with a header
* line (title, then status), the message so far, and either a menu of
* destinations at the bottom or a gesture hint.
*/
const DIALOG_X = 40;
@@ -27,6 +27,9 @@ const MENU_W = DIALOG_W - 24;
const MENU_ROW_GAP = 2;
const MENU_LABEL_INSET_X = 8;
const MENU_LABEL_INSET_Y = 4;
const HEADER_STATUS_GAP = 10;
const HINT_VALUE = 100;
const HINT_DEPTH = 2;
/** Dialog top edge; band-relative, so computed per paint. */
function dialogY(): number {
@@ -89,15 +92,20 @@ export function paintInputDialog<T extends InputDialogRow>(image: GrayImage, con
image.fillRect(DIALOG_X, top, DIALOG_W, DIALOG_H, 1);
image.drawRect(DIALOG_X, top, DIALOG_W, DIALOG_H, 90);
// Title and status share one header line, the status in the space after the title.
const left = TEXT_LEFT;
image.drawText(font, left, top + 12, content.title, 220);
image.drawText(font, left, top + 30, truncateText(font, content.status, TEXT_MAX_WIDTH), content.statusValue ?? 130);
const headerY = top + 12;
image.drawText(font, left, headerY, content.title, 220);
const statusLeft = left + Math.ceil(font.measureText(content.title)) + HEADER_STATUS_GAP;
const statusWidth = TEXT_LEFT + TEXT_MAX_WIDTH - statusLeft;
image.drawText(font, statusLeft, headerY, truncateText(font, content.status, statusWidth), content.statusValue ?? 130);
const menu = content.menu;
const rowCount = menu?.items.length ?? 0;
// Reserve space for the actual number of rows this menu has.
const textBottom = rowCount > 0 ? top + DIALOG_H - rowCount * menuRowH - 8 : top + DIALOG_H - 8;
const textTop = top + 56;
// Reserve space for the actual number of rows this menu has, or one row for the hint.
const reservedRows = rowCount > 0 ? rowCount : content.hint ? 1 : 0;
const textBottom = top + DIALOG_H - reservedRows * menuRowH - 8;
const textTop = headerY + font.lineHeight + 12;
const maxLines = Math.max(1, ((textBottom - textTop) / 16) | 0);
// The tail of a long message stays in view: it is what was said last (or
@@ -113,6 +121,19 @@ export function paintInputDialog<T extends InputDialogRow>(image: GrayImage, con
const menuTop = top + DIALOG_H - rowCount * menuRowH - 4;
menu.paint(image, { x: MENU_X, y: menuTop, width: MENU_W, height: rowCount * menuRowH - MENU_ROW_GAP }, true);
} else if (content.hint) {
image.drawText(font, left, top + DIALOG_H - 14, content.hint, 110 - font.lineHeight);
// On the line the last menu row's label would occupy, floating in front of the dialog.
drawDepthText(image, font, truncateText(font, content.hint, TEXT_MAX_WIDTH), left, top + DIALOG_H - menuRowH, HINT_VALUE, HINT_DEPTH);
}
}
/**
* Draw one line of text at stereo depth: rendered into its own image (sized
* to the font's ink, which can overshoot the line box) and replayed at depth.
*/
function drawDepthText(image: GrayImage, font: UiFont, text: string, x: number, y: number, value: number, depth: number): void {
const ink = textInkBounds(font);
const above = Math.max(0, -ink.top);
const source = new GrayImage(Math.ceil(font.measureText(text)) + 2, above + Math.max(font.lineHeight, ink.bottom));
source.drawText(font, 0, above, text, value);
image.drawDepthImage(source, x, y - above, depth);
}
+10 -2
View File
@@ -52,11 +52,19 @@ export class ShellModalLayer implements Layer {
this.stack.setBaseSize(interior);
// Flattening bakes the inner stack's planes (glyphs included) so the blit
// below transplants the finished modal content into this layer's plane.
const inner = flattenPlanes(this.stack.paint(), interior);
// The top plane's retained presentations (a menu's selection highlight
// and scroll strip) stay unbaked: they are display lists the glasses
// animate, and baking one freezes it on its first frame. Lower planes'
// are baked so the top plane's raster still covers them.
const planes = this.stack.paint();
const top = planes[planes.length - 1]!;
const inner = flattenPlanes(planes.map((plane) => plane === top ? { ...plane, image: plane.image.withDrawsBaked(false) } : plane), interior);
const rect = modalRect();
const innerX = rect.x + MODAL_PADDING, innerY = rect.y + MODAL_PADDING;
image.fillRect(rect.x, rect.y, rect.width, rect.height, SHELL_OPAQUE_BLACK);
image.drawRect(rect.x, rect.y, rect.width, rect.height, 110);
image.bitBlt(inner, rect.x + MODAL_PADDING, rect.y + MODAL_PADDING, { transparentZero: true });
image.bitBlt(inner, innerX, innerY, { transparentZero: true });
top.image.copyPresentationsInto(image, innerX + top.x, innerY + top.y);
return image;
}
+4 -4
View File
@@ -92,10 +92,10 @@ export class WindowMenu {
/**
* Open the menu, or the shell's system menu when this window has no
* entries. `items` overrides the window's own entries for an app that
* reuses this host for another list (a per-row action menu).
* entries. `items` (and optionally `title`) override the window's own for
* an app that reuses this host for another list (a per-row action menu).
*/
open(items: MenuItem[] = this.options.items()): void {
open(items: MenuItem[] = this.options.items(), title: string = this.options.title()): void {
if (this.stack) return;
if (!items.length) {
this.options.post({ type: "open-system-menu", windowId: this.options.windowId });
@@ -106,7 +106,7 @@ export class WindowMenu {
handleInput: () => {},
};
const stack = new LayerStack(base, { ...noopLayerActions }, this.options.size, this.options.isFocused);
stack.push(new WindowMenuLayer(this.options.title(), items));
stack.push(new WindowMenuLayer(title, items));
this.stack = stack;
}
+1 -1
View File
@@ -1,2 +1,2 @@
export const FACECLAW_VERSION = "0.7.2";
export const FACECLAW_VERSION = "0.8.0";
export const USER_AGENT = "Faceclaw/"+FACECLAW_VERSION;