From f66e0b025fd4e6f7c9ef11022fcdbd82bdfcc5f8 Mon Sep 17 00:00:00 2001 From: James Babcock Date: Thu, 24 Sep 2026 12:23:20 -0700 Subject: [PATCH] Add partial rows and a bounce effect to menus --- CHANGELOG | 1 + app/graphics/display-list.md | 7 +- app/graphics/draw-expression.ts | 5 +- app/graphics/menu-scroll-list.ts | 43 ++++--- app/ui/menu-core.ts | 112 +++++++++++------- app/ui/menu-scroll-motion.ts | 63 +++++++++- tests/brightness-picker.test.cjs | 4 +- tests/display-list-authoring.test.cjs | 4 + .../com/faceclaw/app/FrameDisplayListTest.kt | 2 +- tests/menu-core.test.cjs | 16 ++- tests/menu-display-list.test.cjs | 4 +- tests/menu-scroll-animation.test.cjs | 79 +++++++++++- tests/music-settings.test.cjs | 2 +- tests/notification-filter.test.cjs | 2 +- 14 files changed, 259 insertions(+), 85 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 52e40066..ebdd0757 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -6,6 +6,7 @@ * A new display-list system for glasses-side drawing, unlocking z-effects and animation * Menus animate when changing selection and scrolling +* Menus bounce at the ends, and show part of the next row when there is more below * Depth effects for the window switcher, top bar, and modals * Glanceboard has configurable distance diff --git a/app/graphics/display-list.md b/app/graphics/display-list.md index 4445bd89..8f6562fb 100644 --- a/app/graphics/display-list.md +++ b/app/graphics/display-list.md @@ -68,8 +68,11 @@ 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: a viewport-sized -copy from a strip resource whose source y animates, then the highlight. Navigation eligibility, duration, and easing remain in +image using this API. `menu-scroll-list.ts` authors a menu scroll or bounce: a +viewport-sized copy from a strip resource whose source y animates, then the +highlight. The bounce curve (`app/ui/menu-scroll-motion.ts`) needs no branches: +the easing ops clamp their input to 0..1, so `ease(t / p)` holds at 1 after the +peak and `ease((t - p) / (1 - p))` holds at 0 before it. Navigation eligibility, duration, and easing remain in TypeScript. There is no animated-menu tag or menu animation policy in Kotlin. ## Bridge framing diff --git a/app/graphics/draw-expression.ts b/app/graphics/draw-expression.ts index f122d679..dab4604e 100644 --- a/app/graphics/draw-expression.ts +++ b/app/graphics/draw-expression.ts @@ -61,9 +61,12 @@ export class DrawExpression { static elapsed(): DrawExpression<'i32'> { return new DrawExpression('i32', [ExprOp.ELAPSED]); } /** * A 0..1 timeline fraction that survives subsequent PRESENT clock resets. - * With a delay, it stays 0 for the first delayMs of the timeline. + * With a delay, it stays 0 for the first delayMs of the timeline; a + * negative delay continues a motion that started before the timeline. */ static progress(durationMs: number, delayMs = 0): DrawExpression<'f32'> { + integer(delayMs, -2147483647, 2147483647); + if (delayMs + durationMs <= 0) return DrawExpression.f32(1); const duration = DrawExpression.i32(integer(durationMs, 1, 2147483647)); const end = DrawExpression.i32(integer(delayMs + durationMs, 1, 2147483647)); const elapsed = DrawExpression.elapsed().min(end); diff --git a/app/graphics/menu-scroll-list.ts b/app/graphics/menu-scroll-list.ts index 7517d8e3..0f6cc839 100644 --- a/app/graphics/menu-scroll-list.ts +++ b/app/graphics/menu-scroll-list.ts @@ -1,46 +1,45 @@ import type { MenuHighlightAnimation } from '../ui/menu-highlight-motion'; -import type { MenuScrollAnimation } from '../ui/menu-scroll-motion'; import { DrawExpression as E, type DrawValue } from './draw-expression'; import { DrawOp, type DisplayList, type ListCall, type ListImage } from './display-list'; export type MenuScrollHighlight = { - /** Final row top within the viewport. */ - y: number; + /** Row top within the viewport; clamped inside it. */ + y: DrawValue; width: number; height: number; radius: number; /** Gray8, like drawMenuSelection. */ background: number; border: number; - animation?: MenuHighlightAnimation; }; /** - * A menu viewport scrolling over a pre-rendered strip. `strip` holds every - * row that is visible at any point of the scroll, starting at content offset - * `stripTop` and `stripX` pixels into the viewport. The copy's source row is - * the animated value; the highlight is max-blended over the copied rows, - * sliding on the same timeline and clamped inside the viewport. + * A highlight sliding by (0, dy) to rest at `y`, on a list timeline that + * started at `timelineStart` (the slide may have started before or after it). */ -export function menuScrollList(strip: ListImage, stripX: number, stripTop: number, viewportHeight: number, - scroll: MenuScrollAnimation, highlight?: MenuScrollHighlight): DisplayList { - const eased = (durationMs: number, delayMs = 0) => E.progress(durationMs, delayMs).ease(); - const sourceY: DrawValue = eased(scroll.durationMs) - .lerp(E.i32(scroll.from - stripTop).toFloat(), E.i32(scroll.to - stripTop).toFloat()).toInt(); +export function slidingHighlightY(y: number, motion: MenuHighlightAnimation | undefined, timelineStart: number): E<'i32'> { + if (!motion?.dy) return E.i32(y); + return E.progress(motion.durationMs, motion.startedAt - timelineStart).ease() + .lerp(E.i32(y + motion.dy).toFloat(), E.i32(y).toFloat()).toInt(); +} + +/** + * A menu viewport moving over a pre-rendered strip. `strip` holds every row + * visible at any point of the motion, and sits `stripX` pixels into the + * viewport; `sourceY` is the strip row at the viewport's top, animated on + * `timeline`. The highlight is max-blended over the copied rows. + */ +export function menuScrollList(strip: ListImage, stripX: number, viewportHeight: number, + timeline: { token: number; startedAt: number }, sourceY: DrawValue, highlight?: MenuScrollHighlight): DisplayList { const calls: ListCall[] = [ { op: DrawOp.RECT_COPY, resource: 0, x: 0, y: sourceY, width: strip.width, height: viewportHeight, dx: stripX, dy: 0 }, ]; if (highlight) { const q = (n: number) => Math.min(15, (n + 8) >> 4); - const motion = highlight.animation; - let y: DrawValue = highlight.y; - if (motion?.dy) { - const delay = Math.max(0, motion.startedAt - scroll.startedAt); - y = eased(motion.durationMs, delay).lerp(E.i32(highlight.y + motion.dy).toFloat(), E.i32(highlight.y).toFloat()).toInt() - .max(E.i32(0)).min(E.i32(viewportHeight - highlight.height)); - } + const y = typeof highlight.y === 'number' ? highlight.y + : highlight.y.max(E.i32(0)).min(E.i32(viewportHeight - highlight.height)); calls.push({ op: DrawOp.ROUNDED_RECT, x: 0, y, width: highlight.width, height: highlight.height, radius: highlight.radius, background: q(highlight.background), border: q(highlight.border) }); } - return { resources: [strip], timeline: scroll, calls }; + return { resources: [strip], timeline: { token: timeline.token, startedAt: timeline.startedAt }, calls }; } diff --git a/app/ui/menu-core.ts b/app/ui/menu-core.ts index 97f84479..145998b3 100644 --- a/app/ui/menu-core.ts +++ b/app/ui/menu-core.ts @@ -1,8 +1,9 @@ +import { DrawExpression as E } from "../graphics/draw-expression"; import { GrayImage } from "../graphics/image"; -import { menuScrollList, type MenuScrollHighlight } from "../graphics/menu-scroll-list"; +import { menuScrollList, slidingHighlightY, type MenuScrollHighlight } from "../graphics/menu-scroll-list"; import type { InputEvent } from "./gestures"; import { MenuHighlightMotion } from "./menu-highlight-motion"; -import { MenuScrollMotion, type MenuScrollAnimation } from "./menu-scroll-motion"; +import { MenuScrollMotion, scrollOffsetExpression, type MenuScrollAnimation } from "./menu-scroll-motion"; /** Selected-row fill while the menu owns input; the outline alone marks an unfocused menu's selection. */ export const MENU_HIGHLIGHT_FILL = 15; @@ -13,6 +14,9 @@ const SCROLLBAR_WIDTH = 3; const SCROLLBAR_MIN_THUMB = 8; /** Largest packed scroll strip, in bytes: the firmware's per-resource limit. */ const MAX_SCROLL_STRIP_BYTES = 65536; +/** A bounce overshoots by this fraction of the end row's pitch, as if starting to reveal another row. */ +const BOUNCE_FRACTION = 0.4; +const MAX_BOUNCE = 24; /** A rectangle in image coordinates. */ export type MenuBox = { x: number; y: number; width: number; height: number }; @@ -57,7 +61,8 @@ export type MenuOptions = { onSelect?: (item: T, index: number) => void | Promise; /** * Whether scrolling past either end wraps to the other. When false the - * selection stays put and onExitTop / onExitBottom is called instead. + * selection stays put and onExitTop / onExitBottom is called instead; with + * no callback for that end, the list bounces. */ wrap?: boolean; onExitTop?: () => void; @@ -80,14 +85,17 @@ type MenuLayout = { tops: number[]; heights: number[]; total: number }; * those. Scrolling is pixel-based: the viewport moves the minimum needed to * keep the selection and its neighbors fully visible (just the selection * when they don't all fit), then aligns to a row top so the list never - * starts with a partially visible row. Rows that don't fully fit at the - * bottom are not drawn. + * starts with a partially visible row. A row cut off by the viewport's edge + * is drawn clipped, so a box that isn't a whole number of rows tall shows + * part of the next row as a hint that there is more. * * Navigation that scrolls (without wrapping) animates on the glasses: the * rows visible before and after are drawn into one strip, and a display list * slides a viewport-sized copy of it while the highlight moves on the same - * timeline. When the strip would be too large or the menu doesn't fit the - * assumptions below (see scrollStrip), the scroll snaps instead. + * timeline. Scrolling past an end without wrap or an exit callback bounces + * the same way, with the highlight carried along by its row. When the strip + * would be too large or the menu doesn't fit the assumptions in scrollStrip, + * the motion snaps instead. */ export class Menu { private itemList: readonly T[]; @@ -193,8 +201,9 @@ export class Menu { } return; } - if (delta > 0) this.options.onExitBottom?.(); - else this.options.onExitTop?.(); + const exit = delta > 0 ? this.options.onExitBottom : this.options.onExitTop; + if (exit) exit(); + else this.bounce(delta); } /** Invoke onSelect for the selected item. Returns false when nothing is selected. */ @@ -251,33 +260,35 @@ export class Menu { const top = layout.tops[index]! - this.scrollY; const height = layout.heights[index]! - gap; const selected = index === this.selected; - if (top < 0 || top >= box.height) continue; - // Rows cut off by the bottom edge are not drawn, except a selected row - // taller than the box, which has nowhere better to be. - if (top + height > box.height && !selected) continue; + if (top + height <= 0 || top >= box.height) continue; const rowY = box.y + top; const item = this.itemList[index]!; if (selected && highlight !== false) { const animation = this.motion.paint(index, this.scrollY, box.x, rowY, box.width, height, now, !!strip); const background = focused ? MENU_HIGHLIGHT_FILL : 0; - if (strip) { + if (strip && scroll) { // The strip already holds this row's content; only the box is drawn over it. - scrollHighlight = { y: top, width: box.width, height, radius: highlight?.radius ?? 8, - background, border: MENU_HIGHLIGHT_STROKE, animation }; + let y = slidingHighlightY(top, animation, scroll.startedAt); + // A bounce carries the highlight with its row. + if (scroll.peak !== undefined) y = y.add(E.i32(scroll.to)).sub(scrollOffsetExpression(scroll)); + scrollHighlight = { y, width: box.width, height, radius: highlight?.radius ?? 8, + background, border: MENU_HIGHLIGHT_STROKE }; continue; } + // A selected row taller than the box is drawn whole, from its top. const row = new GrayImage(box.width, height, 0); this.options.draw({ image: row, item, index, x: 0, y: 0, width: box.width, height, selected, focused }); image.drawMenuSelection(row, box.x, rowY, background, MENU_HIGHLIGHT_STROKE, highlight?.radius ?? 8, highlight?.depth ?? 0, animation); } else { - this.options.draw({ image, item, index, x: box.x, y: rowY, width: box.width, height, selected, focused }); + this.drawRow(image, index, box.x, rowY, box.width, height, focused, box.y, box.y + box.height); } } // The rows painted above stay underneath: once the animation ends, the // strip's final frame and the static paint are pixel-identical. if (strip && scroll) { - image.drawDisplayList(menuScrollList(strip.image, strip.x, strip.top, box.height, scroll, scrollHighlight), + const sourceY = scrollOffsetExpression(scroll).sub(E.i32(strip.top)); + image.drawDisplayList(menuScrollList(strip.image, strip.x, box.height, scroll, sourceY, scrollHighlight), box.x, box.y, box.width, box.height, 0); } } @@ -296,12 +307,33 @@ export class Menu { } /** - * Render every row visible at either end of the scroll into one strip, or - * return null to snap. Its final frame must match the static paint, so - * rows the static paint hides (partial ones at the bottom) are left out of - * the part the new viewport shows. The strip copy is opaque, so this also - * requires a black background under the menu, a selected row that fits, - * and a highlight at the menu's own depth. + * Draw one row at (x, y), clipped to target rows [clipTop, clipBottom). + * Selected rows with a highlight are drawn in a scratch image the row's + * size, like the static selection, which clips their ink to the row. + */ + private drawRow(target: GrayImage, index: number, x: number, y: number, width: number, height: number, + focused: boolean, clipTop: number, clipBottom: number): void { + const item = this.itemList[index]!; + const selected = index === this.selected; + const scratch = selected && this.options.highlight !== false; + if (!scratch && y >= clipTop && y + height <= clipBottom) { + this.options.draw({ image: target, item, index, x, y, width, height, selected, focused }); + return; + } + const row = new GrayImage(width, height, 0); + this.options.draw({ image: row, item, index, x: 0, y: 0, width, height, selected, focused }); + const skip = Math.max(0, clipTop - y); + target.bitBlt(row.withDrawsBaked(), x, y + skip, + { sy: skip, height: Math.min(height, clipBottom - y) - skip, transparentZero: true }); + } + + /** + * Render every row visible at any point of the motion into one strip, or + * return null to snap. Its first and last frames must match the static + * paints on either side, which clip rows at the viewport's edges the same + * way. The strip copy is opaque, so this also requires a black background + * under the menu, a selected row that fits, and a highlight at the menu's + * own depth. */ private scrollStrip(image: GrayImage, layout: MenuLayout, box: MenuBox, focused: boolean, scroll: MenuScrollAnimation): { image: GrayImage; x: number; top: number } | null { @@ -310,27 +342,15 @@ export class Menu { const gap = this.options.rowGap ?? 0; const selected = this.selected; if (selected !== null && layout.tops[selected]! + layout.heights[selected]! - gap - scroll.to > box.height) return null; - const top = Math.min(scroll.from, scroll.to); - const bottom = Math.max(scroll.from, scroll.to) + box.height; + const offsets = [scroll.from, scroll.to, scroll.peak ?? scroll.to]; + const top = Math.min(...offsets); + const bottom = Math.max(...offsets) + box.height; const strip = new GrayImage(box.width, bottom - top, 0); for (let index = 0; index < this.itemList.length; index++) { const rowTop = layout.tops[index]!; const height = layout.heights[index]! - gap; - const rowBottom = rowTop + height; - const shownAfter = rowTop >= scroll.to && rowBottom <= scroll.to + box.height; - const outsideAfter = rowBottom <= scroll.to || rowTop >= scroll.to + box.height; - if (!shownAfter && !(outsideAfter && rowTop >= top && rowBottom <= bottom)) continue; - const item = this.itemList[index]!; - const y = rowTop - top; - const isSelected = index === selected; - if (isSelected && highlight !== false) { - // Clip like the static selected row, which is drawn in a scratch image. - const row = new GrayImage(box.width, height, 0); - this.options.draw({ image: row, item, index, x: 0, y: 0, width: box.width, height, selected: true, focused }); - strip.bitBlt(row.withDrawsBaked(), 0, y, { transparentZero: true }); - } else { - this.options.draw({ image: strip, item, index, x: 0, y, width: box.width, height, selected: isSelected, focused }); - } + if (rowTop + height <= top || rowTop >= bottom) continue; + this.drawRow(strip, index, 0, rowTop - top, box.width, height, focused, 0, strip.height); } // Crop to the columns with ink; the static paint shows the rest unchanged. const baked = strip.withDrawsBaked(); @@ -354,6 +374,16 @@ export class Menu { return { image: cropped, x: left, top }; } + /** At an end with nowhere to go, overshoot a little past it and come back. */ + private bounce(delta: -1 | 1): void { + const count = this.itemList.length; + if (!this.lastBox || !count) return; + const layout = this.layout(this.lastBox.width); + const pitch = layout.heights[delta > 0 ? count - 1 : 0]!; + const amount = Math.min(MAX_BOUNCE, Math.round(pitch * BOUNCE_FRACTION)); + if (amount > 0) this.scrollMotion.bounce(delta * amount); + } + private isSelectable(index: number): boolean { return this.options.isSelectable?.(this.itemList[index]!) ?? true; } diff --git a/app/ui/menu-scroll-motion.ts b/app/ui/menu-scroll-motion.ts index 676944fb..81e80bdd 100644 --- a/app/ui/menu-scroll-motion.ts +++ b/app/ui/menu-scroll-motion.ts @@ -1,27 +1,73 @@ +import { DrawExpression as E, ExprOp } from "../graphics/draw-expression"; import { MENU_HIGHLIGHT_DURATION_MS, nextAnimationToken } from "./menu-highlight-motion"; /** Longest scroll, in pixels, that animates; longer jumps snap. */ export const MAX_ANIMATED_SCROLL = 96; +export const MENU_BOUNCE_DURATION_MS = 320; +/** Fraction of a bounce spent moving out to the peak; the rest settles back. */ +const BOUNCE_PEAK = 0.35; -/** A viewport moving from content offset `from` to `to`, on the same clock as the highlight. */ -export type MenuScrollAnimation = { from: number; to: number; startedAt: number; token: number; durationMs: number }; +/** + * The viewport's content offset moving from `from` to `to`, on the same clock + * as the highlight. A bounce passes through `peak` on the way (usually past + * the end of the list, with `to` where it started). + */ +export type MenuScrollAnimation = { + from: number; to: number; peak?: number; + startedAt: number; token: number; durationMs: number; +}; type Box = { x: number; y: number; width: number; height: number }; +const clamp01 = (t: number) => Math.max(0, Math.min(1, t)); +const smoothstep = (t: number) => t * t * (3 - 2 * t); + +/** The offset at timeline fraction t (0..1). Keep in sync with scrollOffsetExpression. */ +export function scrollOffsetAt(animation: MenuScrollAnimation, t: number): number { + const { from, to, peak } = animation; + if (peak === undefined) return from + (to - from) * smoothstep(clamp01(t)); + const out = 1 - (1 - clamp01(t / BOUNCE_PEAK)) ** 2; + const back = smoothstep(clamp01((t - BOUNCE_PEAK) / (1 - BOUNCE_PEAK))); + return from + (peak - from) * out + (to - peak) * back; +} + /** - * Tracks the painted scroll offset. Only a change caused by non-wrapping + * The offset as a draw expression on the animation's own timeline. The easing + * ops clamp their input, which splits a bounce into its two phases without + * comparisons: the return term stays 0 until the peak, the outward one stays 1 after. + */ +export function scrollOffsetExpression(animation: MenuScrollAnimation) { + const { from, to, peak } = animation; + const t = () => E.progress(animation.durationMs); + if (peak === undefined) return t().ease().lerp(E.i32(from).toFloat(), E.i32(to).toFloat()).toInt(); + const out = t().mul(E.f32(1 / BOUNCE_PEAK)).ease(ExprOp.EASE_OUT_QUAD); + const back = t().sub(E.f32(BOUNCE_PEAK)).div(E.f32(1 - BOUNCE_PEAK)).ease(); + return E.i32(from).toFloat() + .add(E.i32(peak - from).toFloat().mul(out)) + .add(E.i32(to - peak).toFloat().mul(back)).toInt(); +} + +/** + * Tracks the painted scroll offset. A change caused by non-wrapping * navigation animates; programmatic scrolls, item edits and geometry changes - * snap. A scroll during an animation starts from the offset on screen now. + * snap. A bounce is requested explicitly, at an end of the list. Either starts + * from the offset on screen now, so it can interrupt one in flight. */ export class MenuScrollMotion { private previous?: { scroll: number; box: Box }; private navigated = false; + private bounceBy = 0; private animation?: MenuScrollAnimation; navigate(wrapped: boolean): void { this.navigated = !wrapped; } + /** Overshoot by `offset` pixels (negative past the top) and come back, at the next paint. */ + bounce(offset: number): void { + this.bounceBy = offset; + } + /** Record this paint's settled offset; returns the animation in flight, if any. */ paint(scroll: number, box: Box, now: number): MenuScrollAnimation | undefined { const previous = this.previous; @@ -34,8 +80,14 @@ export class MenuScrollMotion { ? { from, to: scroll, startedAt: now, token: nextAnimationToken(), durationMs: MENU_HIGHLIGHT_DURATION_MS } : undefined; this.previous = { scroll, box: { ...box } }; + } else if (this.bounceBy) { + const from = this.offsetAt(scroll, now); + this.animation = Math.abs(scroll - from) <= MAX_ANIMATED_SCROLL + ? { from, peak: scroll + this.bounceBy, to: scroll, startedAt: now, token: nextAnimationToken(), durationMs: MENU_BOUNCE_DURATION_MS } + : undefined; } this.navigated = false; + this.bounceBy = 0; return this.animation && now - this.animation.startedAt < this.animation.durationMs ? this.animation : undefined; } @@ -47,7 +99,6 @@ export class MenuScrollMotion { private offsetAt(settled: number, now: number): number { const animation = this.animation; if (!animation) return settled; - const t = Math.max(0, Math.min(1, (now - animation.startedAt) / animation.durationMs)); - return Math.round(animation.from + (animation.to - animation.from) * t * t * (3 - 2 * t)); + return Math.round(scrollOffsetAt(animation, (now - animation.startedAt) / animation.durationMs)); } } diff --git a/tests/brightness-picker.test.cjs b/tests/brightness-picker.test.cjs index 997b131c..ff2faa03 100644 --- a/tests/brightness-picker.test.cjs +++ b/tests/brightness-picker.test.cjs @@ -27,11 +27,11 @@ const layers = load('app/ui/layers.ts', { '../native/frame-timings': { spanCurrent: (_name, paint) => paint() }, }); const menu = load('app/ui/menu.ts', { - './menu-highlight-motion': require('../.test-build/app/ui/menu-highlight-motion.js'), '../graphics/menu-scroll-list': require('../.test-build/app/graphics/menu-scroll-list.js'), './menu-scroll-motion': require('../.test-build/app/ui/menu-scroll-motion.js'), + './menu-highlight-motion': require('../.test-build/app/ui/menu-highlight-motion.js'), '../graphics/menu-scroll-list': require('../.test-build/app/graphics/menu-scroll-list.js'), './menu-scroll-motion': require('../.test-build/app/ui/menu-scroll-motion.js'), '../graphics/draw-expression': require('../.test-build/app/graphics/draw-expression.js'), '../graphics/image': graphics, '../graphics/ui-fonts': fonts, '../graphics/textwrap': textwrap, '../util/numeric-util': numeric, './gestures': gestures, './metrics': load('app/ui/metrics.ts'), './menu-core': load('app/ui/menu-core.ts', { - '../graphics/image': graphics, './menu-highlight-motion': require('../.test-build/app/ui/menu-highlight-motion.js'), '../graphics/menu-scroll-list': require('../.test-build/app/graphics/menu-scroll-list.js'), './menu-scroll-motion': require('../.test-build/app/ui/menu-scroll-motion.js'), + '../graphics/image': graphics, './menu-highlight-motion': require('../.test-build/app/ui/menu-highlight-motion.js'), '../graphics/menu-scroll-list': require('../.test-build/app/graphics/menu-scroll-list.js'), './menu-scroll-motion': require('../.test-build/app/ui/menu-scroll-motion.js'), '../graphics/draw-expression': require('../.test-build/app/graphics/draw-expression.js'), }), }); diff --git a/tests/display-list-authoring.test.cjs b/tests/display-list-authoring.test.cjs index 490941e2..3c84e937 100644 --- a/tests/display-list-authoring.test.cjs +++ b/tests/display-list-authoring.test.cjs @@ -32,6 +32,10 @@ test('typed expressions follow i32/f32 arithmetic, easing, time and error semant assert.deepEqual(evaluate(E.i32(300).time(),0,300), {value:300,pending:false}); assert.deepEqual(evaluate(E.i32(300).time().div(E.i32(0)),0,150), {value:0,pending:false}); assert.deepEqual(evaluate(E.decode(Uint8Array.of(ExprOp.IADD)),0), {value:0,pending:false}); + // Delayed timelines: a later start holds at 0, an earlier one continues, a finished one is constant. + const pct = (d, delay, elapsed) => evaluate(E.progress(d, delay).mul(E.f32(100)).toInt(), elapsed).value; + assert.deepEqual([pct(200, 100, 50), pct(200, 100, 200), pct(200, -50, 0), pct(200, -50, 150), pct(200, -300, 0)], [0, 50, 25, 100, 100]); + assert.equal(Buffer.from(E.progress(300, 0).code).toString('hex'), Buffer.from(E.progress(300).code).toString('hex')); }); test('menu authors the golden generic list and stable timeline identity', () => { diff --git a/tests/kotlin/src/commonTest/kotlin/com/faceclaw/app/FrameDisplayListTest.kt b/tests/kotlin/src/commonTest/kotlin/com/faceclaw/app/FrameDisplayListTest.kt index 0c4a677a..69efbfa9 100644 --- a/tests/kotlin/src/commonTest/kotlin/com/faceclaw/app/FrameDisplayListTest.kt +++ b/tests/kotlin/src/commonTest/kotlin/com/faceclaw/app/FrameDisplayListTest.kt @@ -6,7 +6,7 @@ class FrameDisplayListTest { // Golden records emitted by display-list-authoring.test.cjs. private val menu = "077e0000000300020002000200022a00000096000000010002000200ffffffff0200080000ff24017e30020000000001812c8001812c1611010017328001812c16103001812c3023414031ff24017c30020000000001812c8001812c1611010017328001812c16103001812c302341403102000200010001030400000000000000001f" private val multi = "0744000000030002000400010002000000000000000002000200010000ff010001006003000400000000000000001f0200000100000001000100020002feffffff7f00010001007f00" - private val scroll = "0787000000030002000400020000070000009600000001000200040011223344556677880200020000000000ff220100300102300180f0800180f0161101001732800180f01610300180f0302341403102000200010008000000ff280101300100300180f0800180f0161101001732800180f01610300180f030234140310100170100160400020000000103" + private val scroll = "078a000000030002000400020000070000009600000001000200040011223344556677880200020000000000ff250100300102300180f0800180f0161101001732800180f01610300180f0302341403101001102000200010008000000ff280101300100300180f0800180f0161101001732800180f01610300180f030234140310100170100160400020000000103" 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) diff --git a/tests/menu-core.test.cjs b/tests/menu-core.test.cjs index 2b16b034..633ad57c 100644 --- a/tests/menu-core.test.cjs +++ b/tests/menu-core.test.cjs @@ -168,11 +168,13 @@ test('setItems keeps the index by default, takes an explicit one, and holds the assert.equal(menu.selectedIndex, 1, 'an out-of-range index clamps'); }); -test('variable heights: the layout follows getHeight and partial rows are hidden', () => { +test('variable heights: the layout follows getHeight and partial rows are clipped', () => { const heights = { a: 10, b: 30, c: 15, d: 40, e: 5 }; const { menu, paint } = stringMenu({ getHeight: (item) => heights[item] }); let rows = paint(); - assert.deepEqual(rows.map((r) => [r.item, r.y, r.height]), [['a', 0, 10], ['b', 15, 30], ['c', 45, 15]]); + assert.deepEqual(rows.map((r) => [r.item, r.y, r.height, r.scratch]), + [['a', 0, 10, true], ['b', 15, 30, false], ['c', 45, 15, false], ['d', 0, 40, true]], + 'd is drawn in a scratch image and clipped to the viewport'); menu.select(3); rows = paint(); assert.equal(menu.scrollTop, 40, 'aligned to the top of row c'); @@ -180,6 +182,16 @@ test('variable heights: the layout follows getHeight and partial rows are hidden assert.equal(menu.overflows, true); }); +test('a partially visible row shows only its top, hinting at more rows', () => { + const menu = new Menu({ items: ['a', 'b', 'c', 'd'], getHeight: () => 20, + draw: ({ image, x, y, width, height }) => image.fillRect(x, y, width, height, 200) }); + const image = new GrayImage(20, 60); + menu.paint(image, { x: 0, y: 5, width: 20, height: 50 }, true); + const column = Array.from({ length: 60 }, (_, y) => image.pixels[y * 20]); + assert.deepEqual(column.slice(25, 55), new Array(30).fill(200), 'rows b and the top half of c'); + assert.deepEqual(column.slice(55), new Array(5).fill(0), 'nothing below the box'); +}); + test('a selected row taller than the box is drawn from its top', () => { const { menu, paint } = stringMenu({ getHeight: (item) => (item === 'c' ? 90 : 20) }); menu.select(2); diff --git a/tests/menu-display-list.test.cjs b/tests/menu-display-list.test.cjs index 89d8bee8..2a176bea 100644 --- a/tests/menu-display-list.test.cjs +++ b/tests/menu-display-list.test.cjs @@ -16,12 +16,12 @@ function load(file,modules) { const font={lineHeight:12,ascent:10,descent:2,measureText:text=>text.length*4, drawText:(image,x,y,text,value)=>{for(let i=0;ifont}, '../util/numeric-util':{clamp:(n,a,b)=>Math.max(a,Math.min(b,n))},'./gestures':{}, - './menu-highlight-motion': require('../.test-build/app/ui/menu-highlight-motion.js'), '../graphics/menu-scroll-list': require('../.test-build/app/graphics/menu-scroll-list.js'), './menu-scroll-motion': require('../.test-build/app/ui/menu-scroll-motion.js'), + './menu-highlight-motion': require('../.test-build/app/ui/menu-highlight-motion.js'), '../graphics/menu-scroll-list': require('../.test-build/app/graphics/menu-scroll-list.js'), './menu-scroll-motion': require('../.test-build/app/ui/menu-scroll-motion.js'), '../graphics/draw-expression': require('../.test-build/app/graphics/draw-expression.js'), './metrics':require('../.test-build/app/ui/metrics.js'),'./menu-core':menuCore, }); const {prepareFrameDraws}=load('app/graphics/glyph-wire.ts',{'./presentation-wire':wire,'../native/texture-atlas':{textureAtlasAvailable:()=>false}}); diff --git a/tests/menu-scroll-animation.test.cjs b/tests/menu-scroll-animation.test.cjs index 42531672..cf7047c8 100644 --- a/tests/menu-scroll-animation.test.cjs +++ b/tests/menu-scroll-animation.test.cjs @@ -5,10 +5,12 @@ const { MENU_HIGHLIGHT_DURATION_MS } = require('../.test-build/app/ui/menu-highl const { GrayImage } = require('../.test-build/app/graphics/image.js'); const { DrawOp, encodeDisplayList, readDisplayList } = require('../.test-build/app/graphics/display-list.js'); const { evaluate } = require('../.test-build/app/graphics/draw-expression.js'); -const { menuScrollList } = require('../.test-build/app/graphics/menu-scroll-list.js'); +const { DrawExpression: E } = require('../.test-build/app/graphics/draw-expression.js'); +const { menuScrollList, slidingHighlightY } = require('../.test-build/app/graphics/menu-scroll-list.js'); +const { MENU_BOUNCE_DURATION_MS, scrollOffsetExpression } = require('../.test-build/app/ui/menu-scroll-motion.js'); const { encodePresentation } = require('../.test-build/app/graphics/presentation-wire.js'); // Also consumed by FrameDisplayListTest.kt. -const SCROLL = '0787000000030002000400020000070000009600000001000200040011223344556677880200020000000000ff220100300102300180f0800180f0161101001732800180f01610300180f0302341403102000200010008000000ff280101300100300180f0800180f0161101001732800180f01610300180f030234140310100170100160400020000000103'; +const SCROLL = '078a000000030002000400020000070000009600000001000200040011223344556677880200020000000000ff250100300102300180f0800180f0161101001732800180f01610300180f0302341403101001102000200010008000000ff280101300100300180f0800180f0161101001732800180f01610300180f030234140310100170100160400020000000103'; const BOX = { x: 10, y: 5, width: 40, height: 60 }; const ITEMS = ['a', 'b', 'c', 'd', 'e', 'f', 'g']; @@ -127,10 +129,79 @@ test('wraps, programmatic moves, busy backgrounds and oversized strips snap', () assert.equal(wide(40)[0].resources[0].width, 40); }); +test('rows cut off by the viewport edge scroll without popping in either direction', () => { + // 70px box, 20px rows: at rest the fourth visible row shows its top half. + const box = { x: 10, y: 5, width: 40, height: 70 }; + const menu = inkMenu(); + menu.select(1); + let before = paint(menu, 1000, { box }); + for (const [delta, at] of [[1, 2000], [1, 3000], [-1, 4000], [-1, 5000]]) { + const scrollTop = menu.scrollTop; + menu.moveSelection(delta); + const moving = paint(menu, at, { box }); + assert.notEqual(menu.scrollTop, scrollTop, `step at ${at} scrolls`); + assert.equal(scrollLists(moving).length, 1); + assert.deepEqual(shown(moving, at), shown(before, at - 1000), `first frame at ${at}`); + const end = at + MENU_HIGHLIGHT_DURATION_MS; + before = paint(menu, end, { box }); + assert.deepEqual(shown(moving, end), shown(before, end), `last frame at ${at}`); + } +}); + +test('an end without wrap or exit callback bounces, carrying the highlight with its row', () => { + for (const [start, delta] of [[6, 1], [0, -1]]) { + const menu = inkMenu(); + menu.select(start); + const rest = paint(menu, 1000); + const scrollTop = menu.scrollTop; + menu.moveSelection(delta); + assert.equal(menu.selectedIndex, start); + const bouncing = paint(menu, 2000); + assert.equal(menu.scrollTop, scrollTop); + const [list] = scrollLists(bouncing); + assert.ok(list, `bounce ${delta}`); + const [copy, box] = list.calls; + const at = (value, ms) => evaluate(value, ms, 0).value; + const peak = Math.round(MENU_BOUNCE_DURATION_MS * 0.35); + // Offsets relative to rest: 8px (0.4 of a 20px row) past the end at the peak. + assert.equal(at(copy.y, peak) - at(copy.y, 0), 8 * delta); + assert.equal(at(copy.y, MENU_BOUNCE_DURATION_MS), at(copy.y, 0)); + assert.equal(at(box.y, peak) - at(box.y, 0), -8 * delta, 'the highlight moves with its row'); + assert.ok(evaluate(copy.y, MENU_BOUNCE_DURATION_MS - 1, 0).pending); + assert.deepEqual(shown(bouncing, 2000), shown(rest, 1000)); + const end = 2000 + MENU_BOUNCE_DURATION_MS; + assert.deepEqual(shown(bouncing, end), shown(paint(menu, end), end)); + } + const exits = []; + const exiting = inkMenu({ onExitBottom: () => exits.push('bottom') }); + exiting.select(6); + paint(exiting, 1000); + exiting.moveSelection(1); + assert.deepEqual(exits, ['bottom']); + assert.equal(scrollLists(paint(exiting, 2000)).length, 0, 'an exit callback replaces the bounce'); +}); + +test('a bounce during a scroll starts from the offset on screen', () => { + const menu = inkMenu(); + menu.select(4); + paint(menu, 1000); + menu.moveSelection(1); + paint(menu, 2000); // 60 -> 80, the end + menu.moveSelection(1); + paint(menu, 2060); // the highlight moves to the last row; no further scroll + assert.equal(menu.selectedIndex, 6); + menu.moveSelection(1); + const [list] = scrollLists(paint(menu, 2120)); + const offsets = [0, MENU_BOUNCE_DURATION_MS].map((ms) => evaluate(list.calls[0].y, ms, 0).value); + assert.ok(offsets[0] < offsets[1], 'it starts where the scroll had got to, above its settled offset'); +}); + test('scroll lists survive the bridge with animated copy coordinates', () => { const strip = { width: 2, height: 4, pixels: Uint8Array.of(17, 34, 51, 68, 85, 102, 119, 136) }; - const list = menuScrollList(strip, 1, 0, 2, { from: 0, to: 2, startedAt: 1000, token: 7, durationMs: MENU_HIGHLIGHT_DURATION_MS }, - { y: 0, width: 4, height: 2, radius: 0, background: 15, border: 45, animation: { dx: 0, dy: 1, startedAt: 1000, token: 7, durationMs: MENU_HIGHLIGHT_DURATION_MS } }); + const timeline = { from: 0, to: 2, startedAt: 1000, token: 7, durationMs: MENU_HIGHLIGHT_DURATION_MS }; + const y = slidingHighlightY(0, { dx: 0, dy: 1, startedAt: 1000, token: 7, durationMs: MENU_HIGHLIGHT_DURATION_MS }, 1000); + const list = menuScrollList(strip, 1, 2, timeline, scrollOffsetExpression(timeline).sub(E.i32(0)), + { y, width: 4, height: 2, radius: 0, background: 15, border: 45 }); const bytes = encodeDisplayList({ displayList: list, x: 3, y: 2, width: 4, height: 2, depth: 0 }, 1150); assert.equal(Buffer.from(bytes).toString('hex'), SCROLL); const { placed } = readDisplayList(bytes, 0, 1150); diff --git a/tests/music-settings.test.cjs b/tests/music-settings.test.cjs index b98e3b54..97da8432 100644 --- a/tests/music-settings.test.cjs +++ b/tests/music-settings.test.cjs @@ -153,7 +153,7 @@ test('Music settings keeps toggles and selection when a new app arrives and supp '../graphics/textwrap': textwrap, '../graphics/ui-fonts': { getDefaultSmallFont: () => font }, '../util/numeric-util': { clamp: (n, lo, hi) => Math.max(lo, Math.min(hi, n)) }, - './menu-highlight-motion': require('../.test-build/app/ui/menu-highlight-motion.js'), '../graphics/menu-scroll-list': require('../.test-build/app/graphics/menu-scroll-list.js'), './menu-scroll-motion': require('../.test-build/app/ui/menu-scroll-motion.js'), + './menu-highlight-motion': require('../.test-build/app/ui/menu-highlight-motion.js'), '../graphics/menu-scroll-list': require('../.test-build/app/graphics/menu-scroll-list.js'), './menu-scroll-motion': require('../.test-build/app/ui/menu-scroll-motion.js'), '../graphics/draw-expression': require('../.test-build/app/graphics/draw-expression.js'), './metrics': load('app/ui/metrics.ts'), './gestures': {}, }; deps['./menu-core'] = load('app/ui/menu-core.ts', (name) => deps[name]); diff --git a/tests/notification-filter.test.cjs b/tests/notification-filter.test.cjs index 95cc86a2..90e57a9c 100644 --- a/tests/notification-filter.test.cjs +++ b/tests/notification-filter.test.cjs @@ -84,7 +84,7 @@ function ui(fontSize = 12) { '../native/notification-sources': prefs, '../native/notification-access': { isNotificationListenerEnabled: () => true }, '../util/render-freshness': { renderPassAllowsStaleData: () => false }, - './menu-highlight-motion': require('../.test-build/app/ui/menu-highlight-motion.js'), '../graphics/menu-scroll-list': require('../.test-build/app/graphics/menu-scroll-list.js'), './menu-scroll-motion': require('../.test-build/app/ui/menu-scroll-motion.js'), + './menu-highlight-motion': require('../.test-build/app/ui/menu-highlight-motion.js'), '../graphics/menu-scroll-list': require('../.test-build/app/graphics/menu-scroll-list.js'), './menu-scroll-motion': require('../.test-build/app/ui/menu-scroll-motion.js'), '../graphics/draw-expression': require('../.test-build/app/graphics/draw-expression.js'), './metrics': load('app/ui/metrics.ts'), './gestures': {}, };