Display-list based menu slide animation

This commit is contained in:
James Babcock committed 2026-09-23 01:13:03 -07:00
1 parent 92e8def0f0
commit c8d835176f
28 files changed
+1546 -263

No files matched your search

+34
View File
@@ -0,0 +1,34 @@
export type MenuHighlightAnimation = { dx: number; dy: number; startedAt: number; token: number };
let nextToken = 1;
/** Eligibility is decided by menu navigation, before the display-list bridge. */
export class MenuHighlightMotion {
private previous?: { index: number; scroll: number; x: number; y: number; width: number; height: number };
private fromIndex: number | null = null;
private animation?: MenuHighlightAnimation;
navigate(from: number, wrapped: boolean): void {
this.fromIndex = wrapped ? null : from;
}
paint(index: number, scroll: number, x: number, y: number, width: number, height: number, now: number): MenuHighlightAnimation | undefined {
const previous = this.previous;
if (!previous || previous.index !== index || previous.scroll !== scroll ||
previous.x !== x || previous.y !== y || previous.width !== width || previous.height !== height) {
let fromX = previous?.x ?? x, fromY = previous?.y ?? y;
if (previous && this.animation) {
const t = Math.max(0, Math.min(1, (now - this.animation.startedAt) / 500));
const remaining = 1 - t * t * (3 - 2 * t);
fromX += this.animation.dx * remaining;
fromY += this.animation.dy * remaining;
}
const eligible = previous && previous.index !== index && this.fromIndex === previous.index &&
previous.scroll === scroll && previous.width === width && previous.height === height;
this.animation = eligible ? { dx: Math.round(fromX - x), dy: Math.round(fromY - y), startedAt: now, token: nextToken++ } : undefined;
this.fromIndex = null;
this.previous = { index, scroll, x, y, width, height };
}
return this.animation && now - this.animation.startedAt < 500 ? this.animation : undefined;
}
}
+10 -2
View File
@@ -1,3 +1,4 @@
import { MenuHighlightMotion } from "./menu-highlight-motion";
import { G2_LENS_HEIGHT, G2_LENS_WIDTH, GrayImage, type UiFont } from "../graphics/image";
import { wrapText } from "../graphics/textwrap";
import { getDefaultSmallFont } from "../graphics/ui-fonts";
@@ -19,7 +20,7 @@ const MENU_HIGHLIGHT_SELECTED_BACKGROUND_FILL = 15;
const MENU_HIGHLIGHT_SELECTED_BORDER_STROKE = 45;
export type MenuLayout = {
/** Stereo depth of the menu surface; selected rows add +2. */
/** Stereo depth of the menu surface, including the selected row. */
depth?: number;
/** Left edge, or "center" to center horizontally on the painted surface. */
x: number | "center";
@@ -193,6 +194,7 @@ export const CONTEXT_MENU_DIM = 0.25;
export class MenuLayer implements Layer {
private selectedIndex = 0;
private scrollRow = 0;
private readonly highlightMotion = new MenuHighlightMotion();
get depth(): number { return this.layout.depth ?? 0; }
@@ -288,7 +290,11 @@ export class MenuLayer implements Layer {
} else {
row.drawText(font, textX, textY + LIST_ROW_TEXT_INSET, item.label, disabled ? 70 : selected ? 255 : 200);
}
if (selected) image.drawMenuSelection(row, x + 12, rowY, focused ? MENU_HIGHLIGHT_SELECTED_BACKGROUND_FILL : 0, MENU_HIGHLIGHT_SELECTED_BORDER_STROKE, 8, 2);
if (selected) {
const animation = this.highlightMotion.paint(index, this.scrollRow, x + 12, rowY, width - 24, rowHeight - 1, Date.now());
image.drawMenuSelection(row, x + 12, rowY, focused ? MENU_HIGHLIGHT_SELECTED_BACKGROUND_FILL : 0,
MENU_HIGHLIGHT_SELECTED_BORDER_STROKE, 8, 0, animation);
}
}
if (this.items.length > visibleRowCount) {
@@ -315,9 +321,11 @@ export class MenuLayer implements Layer {
}
switch (event.type) {
case "scroll-up":
this.highlightMotion.navigate(this.selectedIndex, this.selectedIndex === 0);
this.selectedIndex = (this.selectedIndex + this.items.length - 1) % this.items.length;
return;
case "scroll-down":
this.highlightMotion.navigate(this.selectedIndex, this.selectedIndex === this.items.length - 1);
this.selectedIndex = (this.selectedIndex + 1) % this.items.length;
return;
case "double-click":