Optimize on-glasses animation

This commit is contained in:
James Babcock committed 2026-09-23 02:58:29 -07:00
1 parent c8d835176f
commit 08f1b9a2a2
8 files changed
+94 -46

No files matched your search

@@ -61,30 +61,31 @@ class DrawExpressionTest {
}
@Test fun smoothstepCoordinatesMatchFirmwareAndSurvivePresentTimeReset() {
val value = DrawValue.animate(10, 110) as DrawValue.Expression
val value = DrawValue.animate(10, 110, 500) as DrawValue.Expression
val code = "010a3001806e300181f432010010300181f43023414031"
assertContentEquals(hex(code), value.program)
for ((time, expected) in listOf(0L to 10, 125L to 25, 250L to 60, 375L to 94, 500L to 110, 900L to 110)) {
assertEquals(expected to (time < 500), evaluate(value.program, time))
}
val continued = DrawValue.animate(10, 110, elapsedMs = 250) as DrawValue.Expression
val continued = DrawValue.animate(10, 110, 500, elapsedMs = 250) as DrawValue.Expression
assertEquals(60 to true, evaluate(continued.program, 0))
assertEquals(110 to false, evaluate(continued.program, 250))
}
@Test fun animatedMenuBridgePreservesElapsedTimeAndTranslation() {
// Tag 6 record emitted by the TypeScript bridge: elapsed=200, delta=(-2,-4).
val wire = hex("06030002000200020001001030020000fefffcffc8002a000000ffffffff")
// Tag 6 record emitted by the TypeScript bridge: elapsed=200, delta=(-2,-4), duration=300.
val wire = hex("06030002000200020001001030020000fefffcffc8002a0000002c01ffffffff")
val input = ArrayByteReader(wire)
assertEquals(6, input.get().toInt())
val row = MenuSelection.read(input, 6).translated(10, 10)
assertEquals(0, input.remaining())
val motion = assertNotNull(row.animation)
assertEquals(42, motion.token)
assertEquals(300, motion.durationMs)
assertEquals(-2, motion.dx)
assertEquals(-4, motion.dy)
val call = row.calls(7, motion.startedAt + 200).first()
for ((elapsed, expected) in listOf(0L to (11 to 9), 300L to (13 to 12))) {
for ((elapsed, expected) in listOf(0L to (12 to 10), 100L to (13 to 12))) {
val reader = DrawReader(call)
assertEquals(8, reader.readU8())
assertEquals(DRAW_FLAG_DEPTH, reader.readU8())
@@ -92,15 +93,29 @@ class DrawExpressionTest {
val frame = DrawEvaluation(elapsed)
assertEquals(expected.first, ExtendedVarint.evaluate(reader, frame))
assertEquals(expected.second, ExtendedVarint.evaluate(reader, frame))
assertEquals(elapsed < 300, frame.animationPending)
assertEquals(elapsed < 100, frame.animationPending)
}
for (end in 1 until wire.size) assertFails {
MenuSelection.read(ArrayByteReader(wire.copyOf(end), 1), 6)
}
}
@Test fun menuCoordinatesUseTheSuppliedDuration() {
val row = MenuSelection(0, 4, 2, 2, 0, 15, 16, 0, ByteArray(2),
animation = MenuSelection.Animation(0, -4, 1000, 42, 120))
val call = row.calls(7, 1000).first()
for ((elapsed, y) in listOf(0L to 0, 60L to 2, 120L to 4)) {
val reader = DrawReader(call)
reader.readBytes(3) // opcode, flags, depth
val frame = DrawEvaluation(elapsed)
assertEquals(0, ExtendedVarint.evaluate(reader, frame))
assertEquals(y, ExtendedVarint.evaluate(reader, frame))
assertEquals(elapsed < 120, frame.animationPending)
}
}
@Test fun playerSchedulesOnlyUnfinishedFramesAndCancelsStaleCallbacks() {
val call = DrawProtocol.roundedRect(DrawValue.Integer(0), DrawValue.animate(0, 4), 2, 2, 0, 15)
val call = DrawProtocol.roundedRect(DrawValue.Integer(0), DrawValue.animate(0, 4, 300), 2, 2, 0, 15)
val renderer = DisplayListRenderer(mapOf(1 to DrawProtocol.displayList(listOf(call))))
val screen = DisplayListRenderer.Target(ByteArray(16), 4, 8)
val output = DisplayListRenderer.Target(ByteArray(16), 4, 8)
@@ -116,7 +131,7 @@ class DrawExpressionTest {
player.present(1)
assertEquals(15, output.get(0, 0))
val first = queued.removeAt(0)
now += 250
now += 150
first()
assertEquals(15, output.get(0, 2))
assertEquals(0, output.get(0, 0))
@@ -125,7 +140,7 @@ class DrawExpressionTest {
assertEquals(15, output.get(0, 0))
stale()
assertEquals(3, displayed)
now += 500
now += 300
queued.removeAt(0)()
assertEquals(15, output.get(0, 4))
assertFalse(renderer.animationPending)
+31 -8
View File
@@ -1,8 +1,8 @@
const test = require('node:test');
const assert = require('node:assert/strict');
const { MenuHighlightMotion } = require('../.test-build/app/ui/menu-highlight-motion.js');
const { MenuHighlightMotion, MENU_HIGHLIGHT_DURATION_MS } = require('../.test-build/app/ui/menu-highlight-motion.js');
const { GrayImage } = require('../.test-build/app/graphics/image.js');
const { encodePresentation, readPresentation } = require('../.test-build/app/graphics/presentation-wire.js');
const { encodePresentation, readPresentation, paintPresentation } = require('../.test-build/app/graphics/presentation-wire.js');
test('highlight animates only navigation that preserves the scroll window', () => {
const motion = new MenuHighlightMotion();
@@ -11,9 +11,10 @@ test('highlight animates only navigation that preserves the scroll window', () =
const moved = motion.paint(1, 0, 10, 40, 100, 20, 100);
assert.equal(moved.dx, 0);
assert.equal(moved.dy, -20);
assert.equal(motion.paint(1, 0, 10, 40, 100, 20, 350).token, moved.token);
assert.equal(moved.durationMs, 300);
assert.equal(motion.paint(1, 0, 10, 40, 100, 20, 250).token, moved.token);
motion.navigate(1, false);
const next = motion.paint(2, 0, 10, 60, 100, 20, 350);
const next = motion.paint(2, 0, 10, 60, 100, 20, 250);
assert.equal(next.dy, -30, 'continue from the halfway position');
motion.navigate(2, false);
assert.equal(motion.paint(3, 1, 10, 60, 100, 20, 400), undefined, 'scroll snaps');
@@ -21,23 +22,45 @@ test('highlight animates only navigation that preserves the scroll window', () =
assert.equal(motion.paint(0, 0, 10, 20, 100, 20, 500), undefined, 'wrap snaps');
motion.navigate(0, false);
motion.paint(1, 0, 10, 40, 100, 20, 600);
assert.equal(motion.paint(1, 0, 10, 40, 100, 20, 1100), undefined, 'finished after 500 ms');
assert.ok(motion.paint(1, 0, 10, 40, 100, 20, 899));
assert.equal(motion.paint(1, 0, 10, 40, 100, 20, 900), undefined, 'finished after 300 ms');
assert.equal(motion.paint(2, 0, 10, 60, 100, 20, 1200), undefined, 'programmatic changes snap');
});
test('bridge and fallback preview use the supplied animation duration', () => {
const now = Date.now;
Date.now = () => 1050;
try {
const image = new GrayImage(8, 2);
image.drawMenuSelection(new GrayImage(2, 2), 5, 0, 0, 255, 0, 0,
{ dx: -4, dy: 0, startedAt: 1000, token: 1, durationMs: 100 });
const record = readPresentation(encodePresentation(image.draws[0]), 0);
assert.equal(record.selection.animation.durationMs, 100);
const screen = new Uint8Array(16), output = new Uint8Array(16);
paintPresentation(output, screen, 8, 2, record.selection);
assert.equal(output[3], 240, 'halfway at 50 ms');
assert.equal(output[1], 0);
Date.now = () => 1100;
output.fill(0);
paintPresentation(output, screen, 8, 2, record.selection);
assert.equal(output[5], 240, 'complete at 100 ms');
assert.equal(output[3], 0);
} finally { Date.now = now; }
});
test('animated selection bridge carries relative start, elapsed time and stable token', () => {
const now = Date.now;
Date.now = () => 1200;
try {
const image = new GrayImage(8, 4);
image.drawMenuSelection(new GrayImage(2, 2, 255), 3, 2, 16, 48, 1, 2,
{ dx: -2, dy: -4, startedAt: 1000, token: 42 });
{ dx: -2, dy: -4, startedAt: 1000, token: 42, durationMs: MENU_HIGHLIGHT_DURATION_MS });
const bytes = encodePresentation(image.draws[0]);
assert.equal(bytes[0], 6);
assert.equal(bytes.length, 30);
assert.equal(bytes.length, 32);
const record = readPresentation(bytes, 0);
assert.equal(record.end, bytes.length);
assert.deepEqual(record.selection.animation, { dx: -2, dy: -4, startedAt: 1000, token: 42 });
assert.deepEqual(record.selection.animation, { dx: -2, dy: -4, startedAt: 1000, token: 42, durationMs: MENU_HIGHLIGHT_DURATION_MS });
assert.equal(record.selection.x, 3);
assert.equal(record.selection.y, 2);
} finally { Date.now = now; }