Better menu class; use it in the Music app

This commit is contained in:
James Babcock committed 2026-09-23 22:05:44 -07:00
1 parent 440674b261
commit 123c1142d6
9 files changed
+694 -163

No files matched your search

+3
View File
@@ -30,6 +30,9 @@ const menu = load('app/ui/menu.ts', {
'./menu-highlight-motion': require('../.test-build/app/ui/menu-highlight-motion.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'),
}),
});
function fixture(initial = '50', viewport = { x: 64, y: 124, width: 576, height: 260 }) {
+240
View File
@@ -0,0 +1,240 @@
const test = require('node:test');
const assert = require('node:assert/strict');
const { Menu } = require('../.test-build/app/ui/menu-core.js');
const { GrayImage } = require('../.test-build/app/graphics/image.js');
/** A menu of string items whose draw callback records every row it paints. */
function stringMenu(overrides = {}) {
const drawn = [];
const exits = { top: 0, bottom: 0 };
const selected = [];
const menu = new Menu({
items: ['a', 'b', 'c', 'd', 'e'],
getHeight: () => 20,
draw: (args) => drawn.push({ item: args.item, x: args.x, y: args.y, width: args.width, height: args.height,
selected: args.selected, focused: args.focused, scratch: args.image !== drawn.image }),
onSelect: (item, index) => selected.push({ item, index }),
onExitTop: () => exits.top++,
onExitBottom: () => exits.bottom++,
...overrides,
});
const image = new GrayImage(100, 100);
drawn.image = image;
const paint = (focused = true, box = { x: 10, y: 5, width: 80, height: 60 }) => {
drawn.length = 0;
menu.paint(image, box, focused);
return drawn.slice();
};
return { menu, paint, drawn, exits, selected, image };
}
test('defaults to the first selectable item and lays rows out from the box top', () => {
const { menu, paint } = stringMenu();
assert.equal(menu.selectedIndex, 0);
const rows = paint();
assert.deepEqual(rows.map((r) => r.item), ['a', 'b', 'c'], 'only rows that fully fit are drawn');
assert.deepEqual(rows.map((r) => r.y), [0, 25, 45], 'the selected row draws into a scratch image at y 0');
assert.equal(rows[0].scratch, true);
assert.equal(rows[1].scratch, false);
assert.equal(rows[0].width, 80);
assert.equal(rows[0].height, 20);
assert.ok(rows.every((r) => r.focused));
assert.equal(paint(false)[0].focused, false);
});
test('rowGap shrinks the drawable box without changing the pitch', () => {
const { paint } = stringMenu({ rowGap: 1 });
const rows = paint();
assert.deepEqual(rows.map((r) => r.height), [19, 19, 19]);
assert.deepEqual(rows.map((r) => r.y), [0, 25, 45]);
});
test('scrolling keeps the selection visible and aligns the viewport to a row top', async () => {
const { menu, paint } = stringMenu();
await menu.handleInput({ type: 'scroll-down' });
await menu.handleInput({ type: 'scroll-down' });
await menu.handleInput({ type: 'scroll-down' });
assert.equal(menu.selectedIndex, 3);
assert.deepEqual(paint().map((r) => r.item), ['b', 'c', 'd']);
assert.equal(menu.scrollTop, 20);
await menu.handleInput({ type: 'scroll-up' });
await menu.handleInput({ type: 'scroll-up' });
await menu.handleInput({ type: 'scroll-up' });
assert.equal(menu.selectedIndex, 0);
assert.deepEqual(paint().map((r) => r.item), ['a', 'b', 'c']);
assert.equal(menu.scrollTop, 0);
});
test('without wrap the ends call the exit callbacks and hold the selection', async () => {
const { menu, exits } = stringMenu();
await menu.handleInput({ type: 'scroll-up' });
assert.equal(menu.selectedIndex, 0);
assert.equal(exits.top, 1);
menu.select(4);
await menu.handleInput({ type: 'scroll-down' });
assert.equal(menu.selectedIndex, 4);
assert.equal(exits.bottom, 1);
});
test('with wrap the ends connect and the exit callbacks stay quiet', async () => {
const { menu, exits } = stringMenu({ wrap: true });
await menu.handleInput({ type: 'scroll-up' });
assert.equal(menu.selectedIndex, 4);
await menu.handleInput({ type: 'scroll-down' });
assert.equal(menu.selectedIndex, 0);
assert.deepEqual(exits, { top: 0, bottom: 0 });
});
test('navigation skips unselectable items and never lands on them', async () => {
const { menu } = stringMenu({
items: ['Group', 'a', 'b', 'Group 2', 'c'],
isSelectable: (item) => !item.startsWith('Group'),
});
assert.equal(menu.selectedIndex, 1, 'initial selection skips the leading heading');
await menu.handleInput({ type: 'scroll-down' });
await menu.handleInput({ type: 'scroll-down' });
assert.equal(menu.selectedIndex, 4, 'the heading between b and c is skipped');
await menu.handleInput({ type: 'scroll-down' });
assert.equal(menu.selectedIndex, 4);
menu.select(3);
assert.equal(menu.selectedIndex, 4, 'selecting a heading lands on the next selectable item');
menu.select(0);
assert.equal(menu.selectedIndex, 1);
});
test('a null selection draws no highlight, and scrolling from it picks an end', async () => {
const { menu, paint } = stringMenu({ selectedIndex: null });
assert.equal(menu.selectedIndex, null);
assert.equal(menu.selectedItem, null);
assert.ok(paint().every((r) => !r.selected && !r.scratch));
assert.equal(await menu.handleInput({ type: 'click' }), false);
await menu.handleInput({ type: 'scroll-up' });
assert.equal(menu.selectedIndex, 4);
menu.select(null);
await menu.handleInput({ type: 'scroll-down' });
assert.equal(menu.selectedIndex, 0);
});
test('with nothing selectable, up and down page the content and exit at the ends', async () => {
const { menu, paint, exits } = stringMenu({ isSelectable: () => false });
paint();
assert.equal(menu.selectedIndex, null);
await menu.handleInput({ type: 'scroll-down' });
assert.equal(menu.scrollTop, 20);
await menu.handleInput({ type: 'scroll-down' });
assert.equal(menu.scrollTop, 40, 'clamped to the content end');
await menu.handleInput({ type: 'scroll-down' });
assert.equal(exits.bottom, 1);
await menu.handleInput({ type: 'scroll-up' });
await menu.handleInput({ type: 'scroll-up' });
assert.equal(menu.scrollTop, 0);
await menu.handleInput({ type: 'scroll-up' });
assert.equal(exits.top, 1);
});
test('click activates the selected item and reports other events as unhandled', async () => {
const { menu, selected } = stringMenu();
menu.select(2);
assert.equal(await menu.handleInput({ type: 'click' }), true);
assert.deepEqual(selected, [{ item: 'c', index: 2 }]);
assert.equal(await menu.handleInput({ type: 'double-click' }), false);
});
test('setItems keeps the index by default, takes an explicit one, and holds the selected row in place', () => {
const { menu, paint } = stringMenu();
menu.select(3);
paint();
assert.equal(menu.scrollTop, 20, 'row d sits at the bottom of the viewport');
menu.setItems(['x', 'y', 'z', 'd2', 'e2', 'f2', 'g2']);
assert.equal(menu.selectedIndex, 3);
assert.equal(menu.scrollTop, 20);
menu.setItems(['x', 'y', 'z', 'w', 'v', 'u', 'd3'], 6);
assert.equal(menu.selectedIndex, 6);
paint();
assert.equal(menu.scrollTop, 80, 'row d3 (top 120) stays 40 px below the viewport top');
menu.setItems(['only'], null);
assert.equal(menu.selectedIndex, null);
menu.setItems(['p', 'q'], 9);
assert.equal(menu.selectedIndex, 1, 'an out-of-range index clamps');
});
test('variable heights: the layout follows getHeight and partial rows are hidden', () => {
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]]);
menu.select(3);
rows = paint();
assert.equal(menu.scrollTop, 40, 'aligned to the top of row c');
assert.deepEqual(rows.map((r) => [r.item, r.y]), [['c', 5], ['d', 0], ['e', 60]], 'e fits under d exactly');
assert.equal(menu.overflows, true);
});
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);
const rows = paint();
assert.deepEqual(rows.map((r) => r.item), ['c']);
assert.equal(menu.scrollTop, 40);
});
test('the selection re-validates against items edited in place', async () => {
const items = ['a', 'b', 'c'];
const { menu } = stringMenu({ items });
menu.select(2);
items.length = 1;
assert.equal(menu.selectedIndex, 0);
items.length = 0;
assert.equal(menu.selectedIndex, null);
items.push('z');
await menu.handleInput({ type: 'scroll-down' });
assert.equal(menu.selectedIndex, 0);
});
test('the highlight is a replayed selection over the row scratch image', () => {
const { paint, image } = stringMenu();
paint();
const selection = image.draws.find((d) => d.presentation);
assert.ok(selection);
assert.equal(selection.x, 10);
assert.equal(selection.y, 5);
assert.equal(selection.presentation.radius, 8);
assert.equal(selection.presentation.background, 15);
assert.equal(selection.presentation.border, 45);
const unfocused = new GrayImage(100, 100);
stringMenu().menu.paint(unfocused, { x: 0, y: 0, width: 50, height: 50 }, false);
assert.equal(unfocused.draws.find((d) => d.presentation).presentation.background, 0);
const plain = new GrayImage(100, 100);
stringMenu({ highlight: false }).menu.paint(plain, { x: 0, y: 0, width: 50, height: 50 }, true);
assert.equal(plain.draws.some((d) => d.presentation), false);
});
test('indexAt maps a viewport coordinate to the selectable item under it', () => {
const { menu, paint } = stringMenu({ isSelectable: (item) => item !== 'b' });
paint();
assert.equal(menu.indexAt(4), null, 'above the box');
assert.equal(menu.indexAt(5), 0);
assert.equal(menu.indexAt(30), null, 'row b is not selectable');
assert.equal(menu.indexAt(50), 2);
assert.equal(menu.indexAt(65), null, 'below the box');
});
test('the scrollbar tracks the pixel scroll fraction', () => {
const { menu, paint, image } = stringMenu();
paint();
image.clear();
menu.drawScrollbar(image, 95, 5, 60);
assert.equal(image.getPixel(95, 5), 120, 'thumb at the top');
assert.equal(image.getPixel(95, 60), 30, 'track below it');
menu.select(4);
paint();
image.clear();
menu.drawScrollbar(image, 95, 5, 60);
assert.equal(image.getPixel(95, 5), 30);
assert.equal(image.getPixel(95, 64), 120, 'thumb at the bottom');
const short = stringMenu({ items: ['a'] });
short.paint();
short.image.clear();
short.menu.drawScrollbar(short.image, 95, 5, 60);
assert.equal(short.image.getPixel(95, 5), 0, 'no scrollbar when the content fits');
});
+4 -1
View File
@@ -15,11 +15,14 @@ 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'),
});
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'),
'./metrics':require('../.test-build/app/ui/metrics.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}});
function menu() {
+1
View File
@@ -156,6 +156,7 @@ test('Music settings keeps toggles and selection when a new app arrives and supp
'./menu-highlight-motion': require('../.test-build/app/ui/menu-highlight-motion.js'),
'./metrics': load('app/ui/metrics.ts'), './gestures': {},
};
deps['./menu-core'] = load('app/ui/menu-core.ts', (name) => deps[name]);
const menu = load('app/ui/menu.ts', (name) => deps[name]);
const uiDeps = {
'../../graphics/image': deps['../graphics/image'],
+1
View File
@@ -92,6 +92,7 @@ function ui(fontSize = 12) {
assert.ok(name in dependencies, `Unexpected dependency ${name}`);
return dependencies[name];
};
dependencies['./menu-core'] = load('app/ui/menu-core.ts', requireModule);
dependencies['./menu'] = load('app/ui/menu.ts', requireModule);
const { SingleNotificationLayer } = load('app/ui/notifications.ts', requireModule);
const { NotificationFilterLayer } = load('app/ui/notification-filter.ts', requireModule);
+1
View File
@@ -25,6 +25,7 @@
"../app/graphics/plane.ts",
"../app/graphics/shell-scene.ts",
"../app/ui/metrics.ts",
"../app/ui/menu-core.ts",
"../app/phone-ui/phone-gestures.ts",
"../app/g2/firmware-compat.ts",
"../app/g2/glasses-hardware-identity.ts",