forked from Blendman974/faceclaw
Add partial rows and a bounce effect to menus
This commit is contained in:
1 parent
c801bd5185
commit
f66e0b025f
14 files changed
+259
-85
No files matched your search
@@ -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'),
|
||||
}),
|
||||
});
|
||||
|
||||
|
||||
@@ -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', () => {
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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;i<text.length;i++) image.fillRect(x+i*4,y+2,2,6,value);}};
|
||||
const menuCore=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'),
|
||||
});
|
||||
const {MenuLayer}=load('app/ui/menu.ts',{
|
||||
'../graphics/image':graphics,'../graphics/textwrap':{},'../graphics/ui-fonts':{getDefaultSmallFont:()=>font},
|
||||
'../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}});
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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]);
|
||||
|
||||
@@ -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': {},
|
||||
};
|
||||
|
||||
Reference in new issue
Block a user