Add animations to launcher and Files app

This commit is contained in:
James Babcock committed 2026-09-25 13:25:51 -07:00
1 parent 7ad9fe10ad
commit b0a98f5942
24 files changed
+1608 -738

No files matched your search

+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);
}
}
+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();
}
/**
+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;
@@ -7,7 +7,9 @@ package com.faceclaw.app
/** Common draw-call header bits. */
const val DRAW_FLAG_RESOURCE_TARGET = 1
const val DRAW_FLAG_DEPTH = 2
const val DRAW_FLAGS_MASK = DRAW_FLAG_RESOURCE_TARGET or DRAW_FLAG_DEPTH
/** Revision 35: an x/y s16, w/h u16 clip rect follows the depth byte. */
const val DRAW_FLAG_CLIP = 4
const val DRAW_FLAGS_MASK = DRAW_FLAG_RESOURCE_TARGET or DRAW_FLAG_DEPTH or DRAW_FLAG_CLIP
/** Bounding-box payload flag: u16 coordinates/sizes instead of aligned compact units. */
const val DRAW_BBOX_FLAG_U16 = 1
@@ -1,6 +1,19 @@
package com.faceclaw.app
/** Revision 33 wire grammar, shared by scene planning and the local renderer. */
/**
* A draw call's clip rect (revision 35, DRAW_FLAG_CLIP), in the call's own
* coordinates: it moves with the call's depth, narrows any clip inherited
* from an enclosing play-list call, and is dropped by a target override.
*/
class DrawClip(val x: Int, val y: Int, val width: Int, val height: Int) {
init {
require(x in -32768..32767 && y in -32768..32767 && width in 0..65535 && height in 0..65535)
}
fun translated(dx: Int, dy: Int) = DrawClip(x + dx, y + dy, width, height)
}
/** Revision 35 wire grammar, shared by scene planning and the local renderer. */
object DrawProtocol {
/** Mirrors g2flash/patches/zlib_glue.c. */
const val DRAW = CFW_MSG_DRAW_CALLS
@@ -25,21 +38,29 @@ object DrawProtocol {
private fun encode(write: DrawWriter.() -> Unit): ByteArray = DrawWriter().apply(write).toByteArray()
fun call(op: Int, args: ByteArray, target: Int? = null, depth: Int? = null): ByteArray {
fun call(op: Int, args: ByteArray, target: Int? = null, depth: Int? = null, clip: DrawClip? = null): ByteArray {
require(depth == null || depth in -128..127)
val flags = (if (target == null) 0 else DRAW_FLAG_RESOURCE_TARGET) or
(if (depth == null) 0 else DRAW_FLAG_DEPTH)
(if (depth == null) 0 else DRAW_FLAG_DEPTH) or
(if (clip == null) 0 else DRAW_FLAG_CLIP)
return encode {
writeU8(op)
writeU8(flags)
if (target != null) writeU16(target)
if (depth != null) writeS8(depth)
if (clip != null) {
writeS16(clip.x)
writeS16(clip.y)
writeU16(clip.width)
writeU16(clip.height)
}
writeBytes(args)
}
}
private fun call(op: Int, target: Int? = null, depth: Int? = null, write: DrawWriter.() -> Unit): ByteArray =
call(op, encode(write), target, depth)
private fun call(op: Int, target: Int? = null, depth: Int? = null, clip: DrawClip? = null,
write: DrawWriter.() -> Unit): ByteArray =
call(op, encode(write), target, depth, clip)
fun depthOffset(depth: Int, right: Boolean): Int =
if (right) -floorHalf(depth + 1) else floorHalf(depth)
@@ -53,11 +74,11 @@ object DrawProtocol {
fun roundedRect(
x: DrawValue, y: DrawValue, width: Int, height: Int, radius: Int, background: Int,
border: Int = DRAW_ROUNDED_RECT_NO_BORDER, depth: Int? = null,
border: Int = DRAW_ROUNDED_RECT_NO_BORDER, depth: Int? = null, clip: DrawClip? = null,
): ByteArray {
require(width in 1..640 && height in 1..480 && radius in 0..65535)
require(background in 0..15 && border in 0..DRAW_ROUNDED_RECT_NO_BORDER)
return call(DRAW_OP_ROUNDED_RECT, depth = depth) {
return call(DRAW_OP_ROUNDED_RECT, depth = depth, clip = clip) {
writeExtended(x)
writeExtended(y)
writeU16(width)
@@ -86,11 +107,17 @@ object DrawProtocol {
fun image(
id: Int, x: Int, y: Int, options: Int = CFW_TEXTURE_OPT_BRIGHTNESS_MASK,
target: Int? = null, depth: Int? = null,
): ByteArray = call(DRAW_OP_IMAGE, target, depth) {
target: Int? = null, depth: Int? = null, clip: DrawClip? = null,
): ByteArray = image(id, DrawValue.Integer(x), DrawValue.Integer(y), options, target, depth, clip)
/** Revision 35: x and y are extended values and may be expressions. */
fun image(
id: Int, x: DrawValue, y: DrawValue, options: Int = CFW_TEXTURE_OPT_BRIGHTNESS_MASK,
target: Int? = null, depth: Int? = null, clip: DrawClip? = null,
): ByteArray = call(DRAW_OP_IMAGE, target, depth, clip) {
writeU16(id)
writeS16(x)
writeS16(y)
writeExtended(x)
writeExtended(y)
writeU8(options)
}
@@ -103,8 +130,8 @@ object DrawProtocol {
/** Revision 29: source and destination coordinates may be expressions; the source rect must stay in bounds. */
fun rectCopy(
source: Int, x: DrawValue, y: DrawValue, width: Int, height: Int, dx: DrawValue, dy: DrawValue,
target: Int? = null, depth: Int? = null,
): ByteArray = call(DRAW_OP_RECT_COPY, target, depth) {
target: Int? = null, depth: Int? = null, clip: DrawClip? = null,
): ByteArray = call(DRAW_OP_RECT_COPY, target, depth, clip) {
writeU16(source)
writeExtended(x)
writeExtended(y)
@@ -114,10 +141,13 @@ object DrawProtocol {
writeExtended(dy)
}
/** Fill the whole target, ignoring depth. */
fun clear(color: Int = 0, target: Int? = null): ByteArray {
/**
* Fill the whole target, ignoring depth; under a clip (revision 35), just
* the clipped part. Depth still moves the clip rect, like any call's.
*/
fun clear(color: Int = 0, target: Int? = null, clip: DrawClip? = null, depth: Int? = null): ByteArray {
require(color in 0..15)
return call(DRAW_OP_CLEAR, target) { writeU8(color) }
return call(DRAW_OP_CLEAR, target, depth, clip) { writeU8(color) }
}
/**
@@ -139,12 +169,17 @@ object DrawProtocol {
}
}
fun text(id: Int, x: Int, y: Int, options: Int, text: ByteArray): ByteArray {
fun text(id: Int, x: Int, y: Int, options: Int, text: ByteArray): ByteArray =
text(id, DrawValue.Integer(x), DrawValue.Integer(y), options, text)
/** Revision 35: x and y are extended values and may be expressions. */
fun text(id: Int, x: DrawValue, y: DrawValue, options: Int, text: ByteArray, depth: Int? = null,
clip: DrawClip? = null): ByteArray {
require(text.size <= 255)
return call(DRAW_OP_TEXT) {
return call(DRAW_OP_TEXT, depth = depth, clip = clip) {
writeU16(id)
writeS16(x)
writeS16(y)
writeExtended(x)
writeExtended(y)
writeU8(options)
writeU8(text.size)
writeBytes(text)
@@ -802,14 +802,14 @@ class TexturePlanner {
}
/** How many 1..31 control bytes are needed to move the cursor by delta. */
private fun adjustByteCount(delta: Int): Int {
internal fun adjustByteCount(delta: Int): Int {
if ((delta == 0)) {
return 0
}
return (if ((delta > 0)) ((delta + 19) / 20) else ((-delta + 9) / 10))
}
private fun emitAdjust(out: ByteSink, delta: Int): Unit {
internal fun emitAdjust(out: ByteSink, delta: Int): Unit {
var delta = delta
while ((delta != 0)) {
var step: Int = (if ((delta > 0)) minOf(delta, 20) else maxOf(delta, -10))
@@ -8,12 +8,18 @@ class DisplayListRenderer(
) {
class BuiltinGlyph(val image: ByteArray, val advance: Int, val x: Int = 0, val y: Int = 0)
/**
* [clip] (revision 35) is the writable [left, top, right, bottom) in
* shifted target pixels, or null for the whole target. Like the depth
* shift, it is inherited by nested lists and dropped by a target override.
*/
class Target(
val bytes: ByteArray,
val width: Int,
val height: Int,
val offset: Int = 0,
val shiftX: Int = 0,
val clip: IntArray? = null,
) {
val stride = (width + 1) / 2
@@ -26,11 +32,26 @@ class DisplayListRenderer(
return (bytes[offset + y * stride + x / 2].toInt() ushr shift) and 15
}
fun shifted(dx: Int) = Target(bytes, width, height, offset, shiftX + dx)
fun shifted(dx: Int) = Target(bytes, width, height, offset, shiftX + dx, clip)
/** Narrow the clip to a rect in (unshifted) call coordinates. */
fun clipped(x: Int, y: Int, w: Int, h: Int): Target {
val left = x + shiftX
val rect = intArrayOf(left, y, left + w, y + h)
if (clip != null) {
rect[0] = maxOf(rect[0], clip[0]); rect[1] = maxOf(rect[1], clip[1])
rect[2] = minOf(rect[2], clip[2]); rect[3] = minOf(rect[3], clip[3])
}
return Target(bytes, width, height, offset, shiftX, rect)
}
fun writable(x: Int, y: Int): Boolean =
x in 0 until width && y in 0 until height &&
(clip == null || (x >= clip[0] && y >= clip[1] && x < clip[2] && y < clip[3]))
fun put(localX: Int, y: Int, value: Int) {
val x = localX + shiftX
if (x !in 0 until width || y !in 0 until height) return
if (!writable(x, y)) return
val index = offset + y * stride + x / 2
val old = bytes[index].toInt()
bytes[index] = if (x % 2 == 0) {
@@ -220,6 +241,9 @@ class DisplayListRenderer(
if (flags and DRAW_FLAG_DEPTH != 0) {
target = target.shifted(DrawProtocol.depthOffset(reader.readS8(), rightLens))
}
if (flags and DRAW_FLAG_CLIP != 0) {
target = target.clipped(reader.readS16(), reader.readS16(), reader.readU16(), reader.readU16())
}
when (opcode) {
DRAW_OP_BOUNDING_BOX -> drawBoundingBox(reader, target, walk)
DRAW_OP_RECT_COPY -> drawRectCopy(reader, target, walk)
@@ -313,26 +337,31 @@ class DisplayListRenderer(
}
}
/** Mirrors the firmware: far-off image/text coordinates draw nothing (before the depth shift can overflow). */
private fun offTarget(x: Int, y: Int) = x !in -65536..65536 || y !in -65536..65536
private fun drawImage(reader: DrawReader, target: Target, walk: Walk) {
val id = reader.readU16()
val x = reader.readS16()
val y = reader.readS16()
val x = ExtendedVarint.evaluate(reader, walk.frame)
val y = ExtendedVarint.evaluate(reader, walk.frame)
val options = reader.readU8()
reader.requireDone()
walk.references.add(id)
draw(image(resource(id)), target, x, y, options, walk.apply)
val image = image(resource(id))
if (!offTarget(x, y)) draw(image, target, x, y, options, walk.apply)
}
private fun drawText(reader: DrawReader, target: Target, walk: Walk) {
val id = reader.readU16()
var x = reader.readS16()
val y = reader.readS16()
var x = ExtendedVarint.evaluate(reader, walk.frame)
val y = ExtendedVarint.evaluate(reader, walk.frame)
val options = reader.readU8()
val text = reader.readBytes(reader.readU8())
reader.requireDone()
walk.references.add(id)
val data = resource(id)
require(data.size >= 193 && data[0].toInt() == CFW_RESOURCE_TYPE_FONT)
val apply = walk.apply && !offTarget(x, y)
for (byte in text) {
val char = byte.toInt() and 255
if (char in 1..31) {
@@ -343,7 +372,7 @@ class DisplayListRenderer(
val offset = DrawProtocol.u16(data, 1 + (char - 32) * 2)
require(offset >= 193)
val glyph = image(data, offset)
draw(glyph, target, x, y, options, walk.apply)
draw(glyph, target, x, y, options, apply)
x += glyph.width
}
}
@@ -359,7 +388,7 @@ class DisplayListRenderer(
if (!walk.apply) return
for (yy in y until y + height) for (xx in x until x + width) {
val tx = xx + target.shiftX
if (tx !in 0 until target.width) continue
if (!target.writable(tx, yy)) continue
val value = target.get(tx, yy)
val shift = if (value % 2 == 0) 4 else 0
// The even-parity table comes first; parity is of the shifted target pixel.
@@ -371,7 +400,16 @@ class DisplayListRenderer(
val color = reader.readU8()
reader.requireDone()
require(color <= 15)
if (walk.apply) target.bytes.fill((color * 17).toByte(), target.offset, target.offset + target.stride * target.height)
if (!walk.apply) return
val clip = target.clip
if (clip == null) {
target.bytes.fill((color * 17).toByte(), target.offset, target.offset + target.stride * target.height)
} else {
// Revision 35: a clipped clear fills just the clip, which is in shifted pixels already.
for (y in maxOf(0, clip[1]) until minOf(target.height, clip[3])) {
for (x in maxOf(0, clip[0]) until minOf(target.width, clip[2])) target.put(x - target.shiftX, y, color)
}
}
}
private fun drawRoundedRect(reader: DrawReader, target: Target, walk: Walk) {
@@ -11,7 +11,17 @@ interface RetainedDrawing {
internal fun readRetainedDrawing(reader: ByteReader, kind: Int): RetainedDrawing =
if (kind == DrawRecordKind.DISPLAY_LIST) FrameDisplayList.read(reader) else MenuSelection.read(reader, kind)
/** Generic TS-authored calls. No menu or easing policy lives in this bridge. */
/**
* Generic TS-authored calls. No menu or easing policy lives in this bridge.
*
* Besides literal firmware calls, a record may carry a DRAWS call (bridge
* only): glyph and icon records in the frame draw-record format, whose atlas
* identities TS registered while painting. They compile to IMAGE calls on
* ImageAtlas resources, then TEXT runs on FontResourceAtlas fonts, all offset
* by the call's (possibly animated) x/y. When any record cannot be resolved,
* the whole list draws nothing: the frame's own pixels show instead, rather
* than content with a glyph missing.
*/
class FrameDisplayList private constructor(
private val x: Int, private val y: Int, private val depth: Int,
override val resources: List<CachedResource>, private val commands: List<Command>,
@@ -20,9 +30,11 @@ class FrameDisplayList private constructor(
override val fingerprint: String get() = "$x,$y,$depth,$identity"
override fun translated(dx: Int, dy: Int) = FrameDisplayList(x + dx, y + dy, depth, resources, commands, startedAt, identity)
/** Image/text [values] are resource, x offset, y offset, options; the offsets add to [x]/[y]. */
private class Command(val op: Int, val depth: Int, val values: IntArray,
val x: DrawValue = DrawValue.Integer(0), val y: DrawValue = DrawValue.Integer(0),
val dx: DrawValue = DrawValue.Integer(0), val dy: DrawValue = DrawValue.Integer(0))
val dx: DrawValue = DrawValue.Integer(0), val dy: DrawValue = DrawValue.Integer(0),
val clip: DrawClip? = null, val text: ByteArray? = null)
override fun calls(ids: IntArray, nowMs: Long): List<ByteArray> {
require(ids.size == resources.size)
@@ -30,14 +42,19 @@ class FrameDisplayList private constructor(
fun resource(index: Int) = if (index == DrawProtocol.SCREEN) index else ids[index]
return commands.map { c ->
val v = c.values
val clip = c.clip?.translated(x, y)
when (c.op) {
DRAW_OP_ROUNDED_RECT -> DrawProtocol.roundedRect(bind(c.x, elapsed, x), bind(c.y, elapsed, y),
v[0], v[1], v[2], v[3], v[4], depth + c.depth)
DRAW_OP_IMAGE -> DrawProtocol.image(resource(v[0]), x + v[1], y + v[2], v[3], depth = depth + c.depth)
v[0], v[1], v[2], v[3], v[4], depth + c.depth, clip)
DRAW_OP_IMAGE -> DrawProtocol.image(resource(v[0]), bind(c.x, elapsed, x + v[1]), bind(c.y, elapsed, y + v[2]),
v[3], depth = depth + c.depth, clip = clip)
DRAW_OP_TEXT -> DrawProtocol.text(resource(v[0]), bind(c.x, elapsed, x + v[1]), bind(c.y, elapsed, y + v[2]),
v[3], c.text!!, depth + c.depth, clip)
DRAW_OP_RECT_COPY -> DrawProtocol.rectCopy(resource(v[0]),
bind(c.x, elapsed, if (v[0] == DrawProtocol.SCREEN) x else 0),
bind(c.y, elapsed, if (v[0] == DrawProtocol.SCREEN) y else 0),
v[1], v[2], bind(c.dx, elapsed, x), bind(c.dy, elapsed, y), depth = depth + c.depth)
v[1], v[2], bind(c.dx, elapsed, x), bind(c.dy, elapsed, y), depth = depth + c.depth, clip = clip)
DRAW_OP_CLEAR -> DrawProtocol.clear(v[0], clip = clip, depth = if (clip == null) null else depth + c.depth)
else -> error("Unsupported frame display-list opcode")
}
}
@@ -47,6 +64,11 @@ class FrameDisplayList private constructor(
// Bridge-only constant binding, removed before a program reaches either renderer.
private const val ELAPSED = 128
private const val MAX_RECORD_BYTES = 4 * 1024 * 1024
/** Bridge-only: replay glyph/icon draw records (see the class comment). */
private const val DRAWS = 32
/** Bridge opcode bit: an x/y s16, w/h u16 clip rect follows the call's depth. */
private const val CLIPPED = 128
private const val IMAGE_OPTIONS = CFW_TEXTURE_OPT_BRIGHTNESS_MASK or CFW_TEXTURE_OPT_TRANSPARENT
private fun bind(value: DrawValue, elapsed: Int, offset: Int): DrawValue {
if (value is DrawValue.Integer) return DrawValue.Integer(value.value + offset)
@@ -68,6 +90,57 @@ class FrameDisplayList private constructor(
return DrawValue.Expression(program)
}
private class GlyphRecord(val fontId: Int, val encoding: Int, val penX: Int, val lineY: Int, val value: Int)
private class ImageRecord(val imageId: Int, val x: Int, val y: Int)
/**
* Compile one DRAWS call into [out], adding atlas resources through
* [resource]. False when a record has no atlas entry, or its font
* snapshot cannot hold every glyph this call needs.
*/
private fun compileDraws(glyphs: List<GlyphRecord>, images: List<ImageRecord>, x: DrawValue, y: DrawValue,
depth: Int, clip: DrawClip?, resource: (CachedResource) -> Int, out: MutableList<Command>): Boolean {
for (image in images) {
val entry = ImageAtlas.get(image.imageId) ?: return false
out.add(Command(DRAW_OP_IMAGE, depth, intArrayOf(resource(entry.resource), image.x, image.y, IMAGE_OPTIONS),
x, y, clip = clip))
}
for ((fontId, fontGlyphs) in glyphs.groupBy { it.fontId }) {
val encodings = fontGlyphs.map { it.encoding }.toSet()
val font = FontResourceAtlas.get(fontId, encodings)
if (!font.encodings.containsAll(encodings)) return false
val fontIndex = resource(font.resource)
// One run per line and color, like TexturePlanner's: control
// bytes move the pen to each glyph's exact left edge.
val lines = fontGlyphs.groupBy { it.lineY to BmpUtil.nibbleForGray(it.value) }
for ((line, members) in lines) {
val placed = members.map { glyph ->
val atlas = GlyphAtlas.get(fontId, glyph.encoding) ?: return false
Triple(glyph.penX + atlas.bbxX, atlas.width, glyph.encoding)
}.sortedBy { it.first }
var start = 0
while (start < placed.size) {
val text = ByteSink()
var cursor = placed[start].first
var end = start
while (end < placed.size) {
val (left, width, encoding) = placed[end]
if (text.size() + TexturePlanner.adjustByteCount(left - cursor) + 1 > 255) break
TexturePlanner.emitAdjust(text, left - cursor)
text.write(encoding)
cursor = left + width
end++
}
out.add(Command(DRAW_OP_TEXT, depth,
intArrayOf(fontIndex, placed[start].first, line.first, line.second or CFW_TEXTURE_OPT_TRANSPARENT),
x, y, clip = clip, text = text.toByteArray()))
start = end
}
}
}
return true
}
fun read(input: ByteReader, nowMs: Long = drawAnimationTimeMs()): FrameDisplayList {
val length = input.getInt()
require(length in 21..MAX_RECORD_BYTES && length <= input.remaining())
@@ -81,12 +154,15 @@ class FrameDisplayList private constructor(
val elapsed = r.readU16() or (r.readU16() shl 16)
require(elapsed >= 0)
val resourceCount = r.readU16(); require(resourceCount <= 256)
val resources = List(resourceCount) {
val local = List(resourceCount) {
val w = r.readU16(); val h = r.readU16()
require(w in 1..640 && h in 1..480 && 5 + (w + 1) / 2 * h <= 65536)
val gray = r.readBytes(w * h)
CachedResource(DrawProtocol.rawImage(w, h, BmpUtil.pack4bppFromGray8(gray, w, h)))
}
val resources = ArrayList(local)
val atlasIndices = HashMap<CachedResource, Int>()
fun atlasResource(cached: CachedResource): Int = atlasIndices.getOrPut(cached) { resources.add(cached); resources.size - 1 }
fun value(): DrawValue {
val first = r.readU8()
if (first != 255) return DrawValue.Integer(ExtendedVarint.read(r, true, first))
@@ -97,23 +173,27 @@ class FrameDisplayList private constructor(
bind(it, Int.MAX_VALUE, Int.MAX_VALUE)
}
}
fun resource(): Int = r.readU16().also { require(it == DrawProtocol.SCREEN || it in resources.indices) }
fun resource(): Int = r.readU16().also { require(it == DrawProtocol.SCREEN || it in local.indices) }
val count = r.readU16(); require(count <= 4096)
val calls = List(count) {
val op = r.readU8(); val d = r.readS16(); require(depth + d in -128..127)
val commands = ArrayList<Command>()
var replayable = true
repeat(count) {
val header = r.readU8(); val op = header and CLIPPED.inv()
val d = r.readS16(); require(depth + d in -128..127)
val clip = if (header and CLIPPED != 0) DrawClip(r.readS16(), r.readS16(), r.readU16(), r.readU16()) else null
when (op) {
DRAW_OP_ROUNDED_RECT -> {
val xx = value(); val yy = value()
val w = r.readU16(); val h = r.readU16(); val radius = r.readU16()
val background = r.readU8(); val border = r.readU8()
require(w in 1..640 && h in 1..480 && background in 0..15 && border in 0..16)
Command(op, d, intArrayOf(w, h, radius, background, border), xx, yy)
commands.add(Command(op, d, intArrayOf(w, h, radius, background, border), xx, yy, clip = clip))
}
DRAW_OP_IMAGE -> {
val id = resource(); require(id != DrawProtocol.SCREEN)
val xx = r.readS16(); val yy = r.readS16(); val options = r.readU8()
require(options == 15 || options == 31)
Command(op, d, intArrayOf(id, xx, yy, options))
commands.add(Command(op, d, intArrayOf(id, xx, yy, options), clip = clip))
}
DRAW_OP_RECT_COPY -> {
val id = resource(); val sx = value(); val sy = value()
@@ -121,7 +201,29 @@ class FrameDisplayList private constructor(
require(w in 1..640 && h in 1..480)
// Relative SCREEN sources may be negative until placement is added.
require(id == DrawProtocol.SCREEN || listOf(sx, sy).all { it !is DrawValue.Integer || it.value >= 0 })
Command(op, d, intArrayOf(id, w, h), sx, sy, dx, dy)
commands.add(Command(op, d, intArrayOf(id, w, h), sx, sy, dx, dy, clip = clip))
}
DRAW_OP_CLEAR -> {
val color = r.readU8(); require(color in 0..15)
commands.add(Command(op, d, intArrayOf(color), clip = clip))
}
DRAWS -> {
val xx = value(); val yy = value()
val glyphs = ArrayList<GlyphRecord>(); val images = ArrayList<ImageRecord>()
repeat(r.readU16()) {
when (r.readU8()) {
DrawRecordKind.GLYPH -> {
val fontId = r.readU16(); val encoding = r.readU16() or (r.readU16() shl 16)
glyphs.add(GlyphRecord(fontId, encoding, r.readS16(), r.readS16(), r.readU8()))
}
DrawRecordKind.TEXTURE_IMAGE -> {
val imageId = r.readU16() or (r.readU16() shl 16)
images.add(ImageRecord(imageId, r.readS16(), r.readS16()))
}
else -> error("Unsupported display-list draw record")
}
}
if (replayable) replayable = compileDraws(glyphs, images, xx, yy, d, clip, ::atlasResource, commands)
}
else -> error("Unsupported frame display-list opcode")
}
@@ -129,7 +231,11 @@ class FrameDisplayList private constructor(
r.requireDone()
// Elapsed changes while the same timeline is resubmitted; it is not content identity.
bytes.fill(0, 13, 17)
return FrameDisplayList(x, y, depth, resources, calls, nowMs - elapsed, CachedResource(bytes).hash.toString())
val identity = CachedResource(bytes).hash.toString()
if (!replayable || commands.size > 4096 || resources.size > 511) {
return FrameDisplayList(x, y, depth, emptyList(), emptyList(), nowMs - elapsed, identity)
}
return FrameDisplayList(x, y, depth, resources, commands, nowMs - elapsed, identity)
}
}
}
+42
View File
@@ -85,3 +85,45 @@ test('frame submission supports resource-free lists and plain PRESENT-relative t
paintDisplayList(output,screen,8,8,placed,false,1002);
assert.equal(output[2],240); assert.equal(output[0],0);
});
// Also consumed by FrameDisplayListTest.kt, with 0xbeef / 0xcafebabe replaced by
// the font and image ids it registers: a clipped clear, a clipped rounded rect,
// and replayed glyph + icon draws clipped while sliding up. REPLAY_PIXELS is
// what the list paints over a nibble-1 screen, 16x8 at 4 bits per pixel.
const REPLAY = '0780000000020003000800040000070000006400000000000300890000000000000600040002880000010001000200020000000400030000000510a00000000000000600040000ff2502000080c002000000000180c8800180c8161101001732800180c81610300180c830234031020000efbe4100000000000000ff01bebafeca04000100';
const REPLAY_PIXELS = '11111111111111111111111111111111111111111111111111fff28f111111111125522211111111112552221111111111222222111111111111111111111111';
function replayList() {
// 'A' is 3x2 ink one row below a 4px line top; the icon is 2x2.
const font = { fingerprintId: 9001, atlasKey: 'stub-font', ascent: 3, lineHeight: 4 };
const glyph = { encoding: 65, bbxWidth: 3, bbxHeight: 2, bbxX: 0, bbxY: 0, bitmapRows: [0xa0, 0xe0] };
const icon = new GrayImage(2, 2); icon.pixels.set([255, 0, 128, 255]);
const source = new GrayImage(8, 4);
source.drawGlyph(font, glyph, 0, 0, 255);
source.drawImage(icon, 4, 1);
const records = Uint8Array.from([0, 0xef, 0xbe, 65, 0, 0, 0, 0, 0, 0, 0, 255, 1, 0xbe, 0xba, 0xfe, 0xca, 4, 0, 1, 0]);
const clip = { x: 0, y: 0, width: 6, height: 4 };
return { x: 2, y: 3, width: 8, height: 4, depth: 0, displayList: { resources: [], timeline: { token: 7, startedAt: 1000 }, calls: [
{ op: DrawOp.CLEAR, color: 2, clip },
{ op: DrawOp.ROUNDED_RECT, x: 0, y: 0, width: 4, height: 3, radius: 0, background: 5, border: 16, clip: { x: 1, y: 1, width: 2, height: 2 } },
{ op: DrawOp.DRAWS, x: 0, y: E.progress(200).lerp(E.f32(-4), E.f32(0)).toInt(), records, count: 2, source, clip },
] } };
}
test('clipped clears, rects and replayed draws encode, decode and paint', () => {
const placed = replayList();
const hex = Buffer.from(encodeDisplayList(placed, 1100)).toString('hex');
const output = new Uint8Array(16 * 8).fill(16);
paintDisplayList(output, output.slice(), 16, 8, placed, false, 1100);
const nibbles = Array.from(output, (v) => (Math.min(15, (v + 8) >> 4)).toString(16)).join('');
assert.equal(hex, REPLAY);
assert.equal(nibbles, REPLAY_PIXELS);
const decoded = readDisplayList(Buffer.from(REPLAY, 'hex'), 0, 1100).placed;
const [clear, rect, draws] = decoded.displayList.calls;
assert.deepEqual([clear.op, clear.color, clear.clip], [DrawOp.CLEAR, 2, { x: 0, y: 0, width: 6, height: 4 }]);
assert.deepEqual(rect.clip, { x: 1, y: 1, width: 2, height: 2 });
assert.equal(draws.count, 2);
assert.deepEqual(Array.from(draws.records), Array.from(placed.displayList.calls[2].records));
assert.equal(draws.source, undefined, 'decoded draws have no software source');
assert.throws(() => readDisplayList(Buffer.from(REPLAY.replace('01bebafeca', '05bebafeca'), 'hex'), 0, 1100), /draw record/);
});
+300
View File
@@ -0,0 +1,300 @@
const test = require('node:test');
const assert = require('node:assert/strict');
const fs = require('node:fs');
const path = require('node:path');
const vm = require('node:vm');
const ts = require('typescript');
const build = (name) => require(`../.test-build/app/${name}.js`);
const metrics = build('ui/metrics');
const { GrayImage } = build('graphics/image');
const { DrawOp } = build('graphics/display-list');
const { MENU_HIGHLIGHT_DURATION_MS } = build('ui/menu-highlight-motion');
const { MENU_BOUNCE_DURATION_MS } = build('ui/menu-scroll-motion');
const LINE_HEIGHT = 21;
// icon 44 + label gap 2 + line 21 + 8 breathing room.
const ROW_H = 75;
/** A font whose every glyph is a 4x6 block, drawn as deferred glyph draws like a real atlas font. */
function stubFont() {
const glyphs = new Map();
const glyph = (cp) => {
if (!glyphs.has(cp)) glyphs.set(cp, { encoding: cp, bbxWidth: 4, bbxHeight: 6, bbxX: 0, bbxY: 0, bitmapRows: Array(6).fill(0xf0) });
return glyphs.get(cp);
};
const font = {
fingerprintId: 77, atlasKey: 'stub', ascent: 15, descent: 6, lineHeight: LINE_HEIGHT,
measureText: (text) => text.length * 6,
getGlyph: glyph,
hasGlyph: () => true,
drawText(image, x, y, text, value) {
let pen = x;
for (const ch of text) { image.drawGlyph(font, glyph(ch.codePointAt(0)), pen, y, value); pen += 6; }
},
};
return font;
}
/** Transpile one module with its imports resolved from `deps`. */
function load(file, deps) {
const exports = {};
const source = fs.readFileSync(path.join(__dirname, '..', file), 'utf8');
vm.runInNewContext(ts.transpileModule(source, {
compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2020 },
}).outputText, { exports, console, Date, require(name) {
assert.ok(name in deps, `Unexpected import ${name} in ${file}`);
return deps[name];
} });
return exports;
}
function loadIconGrid({ lastInputWasWatch = false } = {}) {
const font = stubFont();
const state = { replayable: true };
const common = {
'../graphics/image': build('graphics/image'),
'../graphics/textwrap': build('graphics/textwrap'),
'../graphics/ui-fonts': { getDefaultSmallFont: () => font },
'../util/numeric-util': build('util/numeric-util'),
'./gestures': build('ui/gestures'),
'./menu-core': build('ui/menu-core'),
'./metrics': metrics,
};
const menu = load('app/ui/menu.ts', { ...common, './layers': {} });
const grid = load('app/ui/icon-grid.ts', {
...common,
'../graphics/draw-expression': build('graphics/draw-expression'),
'../graphics/display-list': build('graphics/display-list'),
// Registration is native; any draws are replayable unless a test says not.
'../graphics/glyph-wire': { encodeReplayDraws: (draws) => state.replayable ? { records: new Uint8Array(0), count: draws.length } : null },
'../graphics/menu-scroll-list': build('graphics/menu-scroll-list'),
'./menu': menu,
'./menu-highlight-motion': build('ui/menu-highlight-motion'),
'./menu-scroll-motion': build('ui/menu-scroll-motion'),
'./shell/shell': { shell: { lastInputWasWatch: () => lastInputWasWatch } },
'./layers': {},
});
return { ...grid, state };
}
const ICON = new GrayImage(44, 44);
ICON.fillRect(10, 10, 24, 24, 200);
const ring = (type) => ({ type });
const watch = (type) => ({ type, source: 'watch' });
const BOX = { x: 0, y: 10, width: 500, height: 180 };
function makeGrid({ items = Array.from({ length: 12 }, (_, i) => `item${i}`), isWide, onActivate, onBack, watchLast } = {}) {
const { IconGrid, state } = loadIconGrid({ lastInputWasWatch: watchLast });
const log = { activated: [], backs: 0 };
const data = { items };
const grid = new IconGrid({
items: () => data.items,
isWide,
describe: (item) => ({ label: item, icon: ICON }),
onActivate: onActivate ?? ((item, index) => { log.activated.push({ item, index }); }),
onBack: onBack ?? (() => { log.backs++; return false; }),
});
return { grid, log, data, state };
}
function withClock(now, fn) {
const original = Date.now;
Date.now = () => now;
try { return fn(); } finally { Date.now = original; }
}
function paint(grid, now, box = BOX) {
return withClock(now, () => {
const image = new GrayImage(500, 200);
grid.paint(image, box, true);
return image;
});
}
const animation = (image) => image.draws.find((draw) => draw.presentation?.displayList)?.presentation.displayList;
/** What the glasses show at `at`, as 4-bit values. */
function shown(image, at) {
return withClock(at, () => Array.from(image.withDrawsBaked().pixels, (v) => Math.min(15, (v + 8) >> 4)));
}
test('rows keep their content height instead of stretching, and the next row peeks clipped', () => {
assert.equal(metrics.iconGridMinRowHeight({ lineHeight: LINE_HEIGHT }, 44, 2), ROW_H);
const { grid } = makeGrid({ items: Array.from({ length: 15 }, (_, i) => `item${i}`) });
const image = paint(grid, 1000);
const iconYs = [...new Set(image.draws.filter((d) => d.kind === 'image' && !d.presentation).map((d) => d.y))];
assert.deepEqual(iconYs, [14, 14 + ROW_H], 'full rows are deferred draws at the natural pitch');
const glyphYs = [...new Set(image.draws.filter((d) => d.kind === 'glyph').map((d) => d.y))];
assert.deepEqual(glyphYs, [60, 60 + ROW_H], 'labels of full rows only');
// The third row's top is baked into the raster, cut off at the box bottom (y 190).
const peekTop = 14 + 2 * ROW_H;
assert.ok(image.pixels[(peekTop + 15) * 500 + 60] > 0, 'the peeking icon shows');
for (let y = 190; y < 200; y++) for (let x = 0; x < 500; x++) assert.equal(image.pixels[y * 500 + x], 0, `nothing below the box at (${x}, ${y})`);
assert.equal(animation(image), undefined, 'a first paint does not animate');
});
test('the pitch only changes with the font, not with how many rows fit', () => {
const { grid } = makeGrid();
for (const height of [150, 180, 224]) {
const image = paint(grid, 1000, { x: 0, y: 0, width: 500, height });
const ys = [...new Set(image.draws.filter((d) => d.kind === 'image').map((d) => d.y))];
for (let i = 1; i < ys.length; i++) assert.equal(ys[i] - ys[i - 1], ROW_H, `height ${height}`);
}
});
test('scrolling rows into view animates from the old paint to the new one', async () => {
const { grid } = makeGrid({ items: Array.from({ length: 15 }, (_, i) => `item${i}`) });
paint(grid, 500);
await grid.handleInput(ring('scroll-down'), null);
const before = paint(grid, 1000);
assert.equal(animation(before) !== undefined, true, 'moving the band animates its slide');
const settledBefore = paint(grid, 2000);
await grid.handleInput(ring('scroll-down'), null);
const moving = paint(grid, 3000);
const list = animation(moving);
assert.ok(list, 'the scroll is retained as a display list');
assert.deepEqual(Array.from(list.calls, (c) => c.op), [DrawOp.CLEAR, DrawOp.ROUNDED_RECT, DrawOp.DRAWS]);
assert.ok(list.calls.every((c) => c.clip && c.clip.width === 500 && c.clip.height === 180), 'every call is clipped to the box');
// First and last frames match the static paints on either side.
assert.deepEqual(shown(moving, 3000), shown(settledBefore, 2000));
const end = 3000 + MENU_HIGHLIGHT_DURATION_MS;
const settled = paint(grid, end);
assert.equal(animation(settled), undefined, 'the static paint takes over once the animation ends');
assert.deepEqual(shown(moving, end), shown(settled, end));
const middle = shown(moving, 3000 + MENU_HIGHLIGHT_DURATION_MS / 2);
assert.notDeepEqual(middle, shown(settled, end));
assert.notDeepEqual(middle, shown(settledBefore, 2000));
});
test('item selection slides the highlight between cells without redrawing past the scrollbar gutter', async () => {
const { IconGrid } = loadIconGrid();
const grid = new IconGrid({
items: () => Array.from({ length: 30 }, (_, i) => `item${i}`),
describe: (item) => ({ label: item, icon: ICON }), onActivate: () => {}, onBack: () => false, scrollbar: true,
});
paint(grid, 500);
await grid.handleInput(ring('click'), null);
const before = paint(grid, 1000);
assert.equal(animation(before), undefined, 'entering item mode snaps');
await grid.handleInput(ring('scroll-down'), null);
const moving = paint(grid, 2000);
const list = animation(moving);
assert.ok(list);
assert.equal(list.calls[0].clip.width, 495, 'the scrollbar gutter is left to the static paint');
const rect = list.calls[1];
const { evaluate } = build('graphics/draw-expression');
assert.equal(evaluate(rect.x, 0).value, 200 + 6, 'starts on the middle cell');
assert.equal(evaluate(rect.x, MENU_HIGHLIGHT_DURATION_MS).value, 300 + 6, 'rests on the next cell');
assert.deepEqual(shown(moving, 2000), shown(before, 1000));
const end = 2000 + MENU_HIGHLIGHT_DURATION_MS;
assert.deepEqual(shown(moving, end), shown(paint(grid, end), end));
});
test('navigating past an end bounces and comes back', async () => {
const { grid } = makeGrid();
const rest = paint(grid, 1000);
await grid.handleInput(ring('scroll-up'), null);
const bouncing = paint(grid, 2000);
const list = animation(bouncing);
assert.ok(list, 'a bounce at the top is animated');
assert.deepEqual(shown(bouncing, 2000), shown(rest, 1000));
const peak = shown(bouncing, 2000 + Math.round(MENU_BOUNCE_DURATION_MS * 0.35));
assert.notDeepEqual(peak, shown(rest, 1000), 'the rows move down past the top');
// The first icon row moved 24px down (0.4 of a row, capped); the highlight moved with it.
// Column 0's icon ink spans x 38..61 at 4-bit level 13.
const iconTop = (image) => { for (let y = 10; y < 200; y++) if (image[y * 500 + 50] === 13) return y; };
assert.equal(iconTop(peak) - iconTop(shown(rest, 1000)), 24);
assert.deepEqual(shown(bouncing, 2000 + MENU_BOUNCE_DURATION_MS), shown(rest, 1000));
});
test('unreplayable rows, host navigation and taps snap', async () => {
const { grid, state } = makeGrid({ onBack: () => { grid.resetSelection(); return false; } });
paint(grid, 1000);
state.replayable = false;
await grid.handleInput(ring('scroll-down'), null);
assert.equal(animation(paint(grid, 2000)), undefined, 'rows that cannot be replayed snap');
state.replayable = true;
assert.equal(animation(paint(grid, 2100)), undefined, 'the cancelled motion does not resume');
await grid.handleInput(ring('double-click'), null);
assert.equal(animation(paint(grid, 3000)), undefined, 'a host reset is not navigation');
await grid.hitTest(250, 20 + ROW_H, null);
assert.equal(animation(paint(grid, 4000)), undefined, 'a tap selects without sliding');
});
test('ring: row mode, then item mode at the middle column, linear traversal, back out', async () => {
const { grid, log } = makeGrid();
assert.equal(grid.selectedItem, null, 'row mode pins no item');
await grid.handleInput(ring('click'), null);
assert.equal(grid.selectedItem, 'item2', 'click enters item mode at the middle column');
await grid.handleInput(ring('scroll-down'), null);
await grid.handleInput(ring('scroll-down'), null);
await grid.handleInput(ring('scroll-down'), null);
assert.equal(grid.selectedItem, 'item5', 'item scrolling continues onto the next row');
await grid.handleInput(ring('scroll-up'), null);
assert.equal(grid.selectedItem, 'item4', 'and back onto the previous row at its end');
await grid.handleInput(ring('click'), null);
assert.deepEqual(log.activated, [{ item: 'item4', index: 4 }]);
await grid.handleInput(ring('double-click'), null);
assert.equal(grid.selectedItem, null, 'double-click drops to row mode');
assert.equal(log.backs, 0);
await grid.handleInput(ring('double-click'), null);
assert.equal(log.backs, 1, 'double-click in row mode backs out');
});
test('watch: four-way cells, left from the first column backs out and stays in item mode', async () => {
const { grid, log, data } = makeGrid({
onBack: () => { log.backs++; grid.resetSelection(); return false; },
});
await grid.handleInput(watch('swipe-down'), null);
await grid.handleInput(watch('swipe-right'), null);
assert.equal(grid.selectedItem, 'item6', 'row 1, column 1');
await grid.handleInput(watch('swipe-down'), null);
assert.equal(grid.selectedItem, 'item11', 'a shorter row clamps the column');
await grid.handleInput(watch('swipe-left'), null);
assert.equal(grid.selectedItem, 'item10');
assert.equal(log.backs, 0);
await grid.handleInput(watch('swipe-left'), null);
assert.equal(log.backs, 1);
assert.equal(grid.selectedItem, 'item0', 'the host reset the selection, and the watch keeps a cell selected');
data.items = ['only'];
await grid.handleInput(watch('click'), null);
assert.deepEqual(log.activated, [{ item: 'only', index: 0 }]);
});
test('activating or backing out of the grid returns it to row mode', async () => {
const { grid } = makeGrid({ onActivate: async () => true, onBack: () => true });
await grid.handleInput(watch('swipe-right'), null);
await grid.handleInput(watch('click'), null);
assert.equal(grid.selectedItem, null, 'a launch leaves row mode behind');
await grid.handleInput(ring('click'), null);
assert.notEqual(grid.selectedItem, null);
await grid.handleInput(watch('double-click'), null);
assert.equal(grid.selectedItem, null, 'leaving via the watch restores row mode too');
});
test('wide items take their own row and count as one item', async () => {
const items = ['notice', 'a', 'b', 'c', 'd', 'e', 'f'];
const { grid, log } = makeGrid({ items, isWide: (item) => item === 'notice' });
assert.equal(grid.selectedItem, 'notice', 'a wide row is selected as a whole even in row mode');
await grid.handleInput(ring('click'), null);
assert.deepEqual(log.activated, [{ item: 'notice', index: 0 }], 'one click opens a wide row');
grid.selectIndex(6);
assert.equal(grid.cursorIndex, 6);
assert.equal(grid.selectedItem, null, 'selectIndex keeps row mode');
await grid.handleInput(ring('click'), null);
assert.equal(grid.selectedItem, 'f', 'the one-item row clamps the middle column');
await grid.handleInput(ring('scroll-up'), null);
assert.equal(grid.selectedItem, 'e', 'item traversal walks back through the full row');
for (let i = 0; i < 5; i++) await grid.handleInput(ring('scroll-up'), null);
assert.equal(grid.selectedItem, 'notice');
});
test('hitTest opens the cell under the touch, including the peeking row', async () => {
const { grid, log } = makeGrid();
paint(grid, 1000);
assert.equal(await grid.hitTest(50, 10 + 2 * ROW_H + 5, null), true);
assert.deepEqual(log.activated, [{ item: 'item10', index: 10 }]);
assert.equal(grid.selectedItem, 'item10', 'the touched cell becomes the selection');
assert.equal(await grid.hitTest(250, 5, null), false, 'above the box');
assert.equal(await grid.hitTest(250, 10 + 2 * ROW_H + 5, null), false, 'past the last cell of a short row');
});
@@ -49,7 +49,7 @@ class DisplayListTest {
val nested = listOf(DrawProtocol.image(11, -1, 0), DrawProtocol.bbox(hex("0c000000"), 2, 1, 0, 1, 1, 10))
resources[20]=DrawProtocol.displayList(nested)
val calls = listOf(DrawProtocol.call(DRAW_OP_DISPLAY_LIST,DrawProtocol.word(20)), DrawProtocol.image(10,3,1),
DrawProtocol.call(DRAW_OP_TEXT,hex("0c00070003000f0141")), DrawProtocol.lut(8,4,128))
DrawProtocol.call(DRAW_OP_TEXT,hex("0c0007030f0141")), DrawProtocol.lut(8,4,128))
resources[21]=DrawProtocol.displayList(calls)
// Like the C harness, seed composition with the screen: this list has no screen copy.
val screen=hex("123456789abcdef0123456789abcdef0"); val output=screen.copyOf()
@@ -114,7 +114,7 @@ class DisplayListTest {
@Test fun roundedRectsAndNestedOddNegativeDepthMatchFirmwareOnBothLenses() {
val resources = mapOf(
40 to hex("0404000200f00f0ff0"),
41 to hex("0202000d0008020201010c0008000300040a0a000402022800050004001f"),
41 to hex("0202000d0008020201010c0008000300040a0800040202280005041f"),
42 to hex("02010005000702fd2900"))
val screen = hex("012345601234560112345601234560122345601234560123345601234560123445601234560123455601234f60123456601234560123456001234560123456011234560123456012234560123456012334560123456012344560123456012345")
val expected = listOf("01234560123456011aaaaaaaaaa56012aa45644444aa0123a4564444456a1234a564f44f564a2345a6444fff644a3456a4444456444a4560aa44456444aa56011aaaaaaaaaa56012234560123456012334560123456012344560123456012345", "012345601234560112aaaaaaaaaa60122aa56444445aa1233a5644444564a2344a644f44f644a3455a4444ff6444a4566a4444564444a5600aa44564444aa60112aaaaaaaaaa6012234560123456012334560123456012344560123456012345")
@@ -267,4 +267,38 @@ class DisplayListTest {
assertContentEquals(app,glasses.screen);assertContentEquals(app,glasses.composition)
assertEquals(196608,ResourceCacheState.CACHE_SIZE)
}
/** Revision 35. Same calls and expectations as ClipTest in g2flash/tests/test_display_list.py. */
@Test fun clipRectsNarrowEveryOpNestThroughListsAndMoveWithDepth() {
val font = ByteArray(198); font[0] = 1; font[1 + 33 * 2] = 193.toByte(); hex("000202ffff").copyInto(font, 193)
val resources = mutableMapOf(1 to hex("000404ffffffffffffffff"), 3 to font,
5 to hex("0202000700" + "0400010000000f" + "0f00" + "04040900000002000200010008000f"),
6 to hex("00040200000000"), 7 to hex("0201000e00080106000000040002000000" + "0f10"))
fun draw(call: String, right: Boolean = false): DisplayListRenderer.Target {
val target = DisplayListRenderer.Target(ByteArray(64) { 0x11 }, 16, 8)
DisplayListRenderer(resources, rightLens = right).execute(DrawProtocol.sequence(listOf(hex(call))), target)
return target
}
fun expect(target: DisplayListRenderer.Target, color: Int, inside: (Int, Int) -> Boolean) {
for (y in 0 until 8) for (x in 0 until 16) assertEquals(if (inside(x, y)) color else 1, target.get(x, y), "($x, $y)")
}
expect(draw("040403000200" + "0a000a00" + "0100" + "0201" + "0f"), 15) { x, y -> x in 3..5 && y in 2..4 }
expect(draw("0400" + "0100" + "ff020105" + "00" + "0f"), 15) { x, y -> x in 5..8 && y < 4 }
expect(draw("0504" + "0100000002000100" + "0300" + "0000" + "0f" + "02" + "4141"), 15) { x, y -> y == 0 && x in 1..2 }
expect(draw("0804" + "0400040004000200" + "0000" + "1000" + "0800" + "0000" + "0f10"), 15) { x, y -> x in 4..7 && y in 4..5 }
expect(draw("0904" + "fefffeff04000400" + "09"), 9) { x, y -> x < 2 && y < 2 }
expect(draw("0904" + "0300050003000100" + "09"), 9) { x, y -> y == 5 && x in 3..5 }
expect(draw("0604" + "0100010001000600" + "0000000010000800" + "77".repeat(16)), 7) { x, y -> x == 1 && y in 1..6 }
expect(draw("0204" + "0200020001000100" + "0100" + "0000" + "04000400" + "0000"), 15) { x, y -> x == 2 && y == 2 }
expect(draw("0104" + "0100010002000100" + "00" + "000001" + "01" + "8f"), 15) { x, y -> y == 1 && x in 1..2 }
expect(draw("0704" + "000000000a000300" + "0500"), 15) { x, y -> (x < 4 && y < 3) || (x == 9 && y < 2) }
val deep = "0806" + "02" + "0000000004000400" + "0000" + "1000" + "0800" + "0000" + "0f10"
expect(draw(deep), 15) { x, y -> x in 1..4 && y < 4 }
expect(draw(deep, right = true), 15) { x, y -> x < 3 && y < 4 }
// A target override starts unclipped: the whole resource fills, the screen stays.
expect(draw("0704" + "0000000001000100" + "0700"), 15) { _, _ -> false }
assertContentEquals(hex("000402ffffffff"), resources.getValue(6))
expect(draw("0804" + "0400040000000000" + "0000" + "1000" + "0800" + "0000" + "0f10"), 15) { _, _ -> false }
// Unknown flag bits, a truncated clip header, and the old s16 image layout all reject.
for (bad in listOf("090800", "0904000000000100", "04000100020001000f")) assertFails(bad) { draw(bad) }
}
}
@@ -5,15 +5,15 @@ import kotlin.test.*
class DrawWireTest {
private fun hex(value: String) = value.chunked(2).map { it.toInt(16).toByte() }.toByteArray()
@Test fun encodersUseRevision29CoordinatesAndPreserveOtherWireBytes() {
@Test fun encodersUseRevision35CoordinatesAndPreserveOtherWireBytes() {
// Literal vectors pin the firmware grammar independently of the reader.
val calls = listOf(
DrawProtocol.image(0x1234, -32768, 32767, 0x1f, target = 511, depth = -128) to
"0403ff018034120080ff7f1f",
"0403ff01803412df8000c07fff1f",
DrawProtocol.rectCopy(DrawProtocol.CURRENT, 1, 2, 3, 4, -5, -6) to
"0200feff0102030004007b7a",
DrawProtocol.stockText(-1, 2, 15, hex("41c3a9")) to "0300ffff02000f0341c3a9",
DrawProtocol.text(511, -2, 3, 31, hex("01417f")) to "0500ff01feff03001f0301417f",
DrawProtocol.text(511, -2, 3, 31, hex("01417f")) to "0500ff017e031f0301417f",
DrawProtocol.playList(511, depth = 127) to "07027fff01",
DrawProtocol.roundedRect(-1, 2, 640, 480, 65535, 15, 16) to
"08007f028002e001ffff0f10",
@@ -21,6 +21,9 @@ class DrawWireTest {
DrawProtocol.bbox(hex("ffffffffffffffff"), 4, 0, 0, 8, 2) to "010000000002010f10",
DrawProtocol.bbox(hex("0f"), 1, 1, 0, 1, 1) to "01000101000000010001001f",
DrawProtocol.clear(15, target = 511) to "0901ff010f",
// Revision 35: the clip rect follows target and depth.
DrawProtocol.clear(3, target = 511, clip = DrawClip(-1, 2, 3, 4)) to "0905ff01ffff02000300040003",
DrawProtocol.image(7, 1, 2, depth = 2, clip = DrawClip(0, 0, 640, 480)) to "040602000000008002e001070001020f",
)
for ((call, expected) in calls) assertContentEquals(hex(expected), call)
assertContentEquals(hex("0100050007027fff01"), DrawProtocol.sequence(listOf(calls[4].first)))
@@ -38,8 +41,8 @@ class DrawWireTest {
@Test fun optimizerFieldsAreReencodedAndOpaqueBytesArePreserved() {
val records = listOf(
"1300020080ff7f1f" to "040000020080ff7f1f",
"14ff01ffff02000f0341017f" to "0500ff01ffff02000f0341017f",
"1300020080ff7f1f" to "04000002df8000c07fff1f",
"14ff01ffff02000f0341017f" to "0500ff017f020f0341017f",
"0fffff02001f0341c3a9" to "0300ffff02001f0341c3a9",
"030001020134120f10" to "010000000102010f10",
"060f10" to "01000100000000080002000f10",
@@ -7,6 +7,11 @@ class FrameDisplayListTest {
private val menu = "077e0000000300020002000200022a00000096000000010002000200ffffffff0200080000ff24017e30020000000001812c8001812c1611010017328001812c16103001812c3023414031ff24017c30020000000001812c8001812c1611010017328001812c16103001812c302341403102000200010001030400000000000000001f"
private val multi = "0744000000030002000400010002000000000000000002000200010000ff010001006003000400000000000000001f0200000100000001000100020002feffffff7f00010001007f00"
private val scroll = "078a000000030002000400020000070000009600000001000200040011223344556677880200020000000000ff250100300102300180f0800180f0161101001732800180f01610300180f0302341403101001102000200010008000000ff280101300100300180f0800180f0161101001732800180f01610300180f030234140310100170100160400020000000103"
// Clipped clear, clipped rounded rect, and replayed glyph + icon records with
// placeholder atlas ids 0xbeef / 0xcafebabe, plus what they paint over a
// nibble-1 16x8 screen (display-list-authoring.test.cjs).
private val replay = "0780000000020003000800040000070000006400000000000300890000000000000600040002880000010001000200020000000400030000000510a00000000000000600040000ff2502000080c002000000000180c8800180c8161101001732800180c81610300180c830234031020000efbe4100000000000000ff01bebafeca04000100"
private val replayPixels = "11111111111111111111111111111111111111111111111111fff28f111111111125522211111111112552221111111111222222111111111111111111111111"
private fun hex(s: String) = s.chunked(2).map { it.toInt(16).toByte() }.toByteArray()
private fun read(bytes: ByteArray, now: Long = 1150): FrameDisplayList {
val reader = ArrayByteReader(bytes)
@@ -107,4 +112,38 @@ class FrameDisplayListTest {
trailing[1] = (trailing[1] + 1).toByte()
assertFails { read(trailing) }
}
/** The TS golden's placeholder ids, replaced by ids registered in this process. */
private fun registeredReplay(imageKnown: Boolean = true): ByteArray {
val key = "stub-font".encodeToByteArray()
val glyph = byteArrayOf(key.size.toByte()) + key + byteArrayOf(4, 1, 0, 65, 0, 0, 0, 0, 1, 3, 2) +
byteArrayOf(0xa0.toByte(), 0, 0, 0, 0xe0.toByte(), 0, 0, 0)
GlyphAtlas.register(ArrayByteReader(glyph))
val fontId = GlyphAtlas.fontId("stub-font")
val imageId = if (imageKnown) ImageAtlas.ensure("stub-replay-icon", 2, 2,
ArrayByteReader(byteArrayOf(255.toByte(), 0, 128.toByte(), 255.toByte()))) else 0x7fffffff
fun le(value: Int, bytes: Int) = (0 until bytes).joinToString("") { ((value ushr (8 * it)) and 255).toString(16).padStart(2, '0') }
return hex(replay.replace("efbe", le(fontId, 2)).replace("bebafeca", le(imageId, 4)))
}
@Test fun replayedDrawsCompileToClippedImageAndTextCallsThatPaintLikeTheSoftwareRenderer() {
val list = read(registeredReplay(), 1100)
assertEquals(2, list.resources.size, "the icon and a font snapshot")
val ids = intArrayOf(1, 2)
val calls = list.calls(ids, 1100)
assertEquals(listOf(DRAW_OP_CLEAR, DRAW_OP_ROUNDED_RECT, DRAW_OP_IMAGE, DRAW_OP_TEXT), calls.map { it[0].toInt() })
assertTrue(calls.all { it[1].toInt() and DRAW_FLAG_CLIP != 0 })
val resources = list.resources.withIndex().associate { (i, resource) -> ids[i] to resource.bytes }
val screen = ByteArray(64) { 0x11 }
val target = DisplayListRenderer.Target(screen, 16, 8)
DisplayListRenderer(resources).execute(DrawProtocol.sequence(calls), target)
val nibbles = (0 until 128).joinToString("") { target.get(it % 16, it / 16).toString(16) }
assertEquals(replayPixels, nibbles)
}
@Test fun replayWithAnUnknownRecordDrawsNothing() {
val list = read(registeredReplay(imageKnown = false), 1100)
assertTrue(list.resources.isEmpty())
assertTrue(list.calls(IntArray(0), 1100).isEmpty())
}
}