forked from Blendman974/faceclaw
Use animated menus in a bunch more places
This commit is contained in:
1 parent
123c1142d6
commit
71c1971992
37 files changed
+1457
-1005
No files matched your search
@@ -44,7 +44,7 @@ test('store install releases serialized input before download and permission app
|
||||
const download = new Promise(resolve => { completeDownload = resolve; });
|
||||
const manifest = { name: 'Test', packageId: 'test', permissions: [{ name: 'network' }], privacyPolicyUrl: '' };
|
||||
const load = loader({}, {
|
||||
'../../graphics/ui-fonts': {}, '../../graphics/image': {}, '../../graphics/textwrap': {}, '../../ui/menu': {}, '../files/text-viewer': {}, '../../ui/metrics': {},
|
||||
'../../graphics/ui-fonts': {}, '../../graphics/image': {}, '../../graphics/textwrap': {}, '../../ui/menu-core': require('../.test-build/app/ui/menu-core.js'), '../files/text-viewer': {}, '../../ui/metrics': {},
|
||||
'./even-api': { evenHubApi: { getStoreAppDetail: async () => null, downloadApp: () => download } },
|
||||
'./installed-apps': { getInstalledEvenHubApp: () => null, readEvenHubPackageManifestBytes: () => manifest,
|
||||
readEvenHubPackageManifest: () => null, installedEvenHubPackagePath: () => '', installedEvenHubAppId: id => id,
|
||||
|
||||
@@ -26,7 +26,8 @@ const loadStoreLayer = () => {
|
||||
'../../graphics/textwrap': { truncateText: (_f, text) => text, wrapText: (_f, text) => [text] },
|
||||
'../../util/numeric-util': { clamp: (v, lo, hi) => Math.min(hi, Math.max(lo, v)) },
|
||||
'../../ui/gestures': { GESTURE_CLICK: 'click' },
|
||||
'../../ui/menu': { drawListScrollbar() {}, drawSelectionHighlight() {}, scrollToKeepSelectionVisible() {} },
|
||||
'../../ui/menu': { drawSelectionHighlight() {} },
|
||||
'../../ui/menu-core': require('../.test-build/app/ui/menu-core.js'),
|
||||
'../../ui/shell/shell': { shell: { yieldFocusToSidebar() {} } },
|
||||
'../../ui/metrics': { lineStep: () => 12 },
|
||||
'../../ui/dashboard-settings': {
|
||||
|
||||
@@ -3,6 +3,7 @@ const assert = require('node:assert/strict');
|
||||
const fs = require('node:fs');
|
||||
const vm = require('node:vm');
|
||||
const ts = require('typescript');
|
||||
const { Menu } = require('../.test-build/app/ui/menu-core.js');
|
||||
function load(file, context) {
|
||||
const sandbox = { exports: {}, ...context };
|
||||
vm.runInNewContext(ts.transpileModule(fs.readFileSync(file, 'utf8'), {
|
||||
@@ -23,7 +24,9 @@ test('iOS Type Into App submits UTF-8 terminal text and delays Enter at the byte
|
||||
client.handleMessage(JSON.stringify({ type: 'connect', command: 'test' }));
|
||||
const { VoiceInputLayer } = load('app/ui/shell/voice-input.ts', {
|
||||
require: id => id.includes('voice-control') ? { voiceControlBridge: { stop() {} } }
|
||||
: id.includes('dashboard-settings') ? { anthropicApiKeySetting: { get: () => '' } } : {},
|
||||
: id.includes('dashboard-settings') ? { anthropicApiKeySetting: { get: () => '' } }
|
||||
: id.includes('input-dialog') ? { createInputDialogMenu: (items, selectedIndex) =>
|
||||
new Menu({ items, selectedIndex, wrap: true, getHeight: () => 1, draw() {} }) } : {},
|
||||
global: { isIOS: true },
|
||||
});
|
||||
for (const text of ['hello terminal', 'café π 🔋', 'line one\n第二行', '']) {
|
||||
|
||||
@@ -238,3 +238,51 @@ test('the scrollbar tracks the pixel scroll fraction', () => {
|
||||
short.menu.drawScrollbar(short.image, 95, 5, 60);
|
||||
assert.equal(short.image.getPixel(95, 5), 0, 'no scrollbar when the content fits');
|
||||
});
|
||||
|
||||
test('a menu created empty starts on the first selectable item once items arrive', () => {
|
||||
const { menu } = stringMenu({ items: [], isSelectable: (item) => !item.startsWith('Group') });
|
||||
assert.equal(menu.selectedIndex, null);
|
||||
menu.setItems(['Group', 'a', 'b']);
|
||||
assert.equal(menu.selectedIndex, 1, 'skips a leading heading');
|
||||
menu.setItems([]);
|
||||
assert.equal(menu.selectedIndex, null);
|
||||
menu.setItems(['x', 'y']);
|
||||
assert.equal(menu.selectedIndex, 0, 'emptying and refilling starts over at the top');
|
||||
});
|
||||
|
||||
test('an explicit deselection survives setItems until the user or caller selects', async () => {
|
||||
const { menu } = stringMenu({ items: [] , selectedIndex: null });
|
||||
menu.setItems(['a', 'b']);
|
||||
assert.equal(menu.selectedIndex, null, 'constructed with no selection');
|
||||
menu.select(1);
|
||||
menu.setItems(['a', 'b', 'c'], null);
|
||||
menu.setItems(['a', 'b', 'c', 'd']);
|
||||
assert.equal(menu.selectedIndex, null, 'setItems without an index keeps the deselection');
|
||||
await menu.handleInput({ type: 'scroll-down' });
|
||||
assert.equal(menu.selectedIndex, 0);
|
||||
menu.setItems([]);
|
||||
menu.setItems(['p', 'q']);
|
||||
assert.equal(menu.selectedIndex, 0, 'scrolling ended the deselection');
|
||||
});
|
||||
|
||||
test('setItems never leaves the viewport scrolled past the end of the list', () => {
|
||||
// Box 60px, rows 20px: a 3-row list fits, so it must sit at the top.
|
||||
const { menu, paint } = stringMenu({ items: ['b', 'c', 'd'] });
|
||||
menu.select(2);
|
||||
paint();
|
||||
assert.equal(menu.scrollTop, 0);
|
||||
menu.setItems(['a', 'b', 'c'], 2);
|
||||
assert.deepEqual(paint().map((r) => r.item), ['a', 'b', 'c'], 'a row sorted in above stays visible');
|
||||
assert.equal(menu.scrollTop, 0);
|
||||
// The selected row at the top of a scrolled viewport loses the rows below it:
|
||||
// keeping its on-screen position would leave a gap, so pull back to show the end.
|
||||
const long = stringMenu({ items: ['a', 'b', 'c', 'd', 'e', 'f', 'g'] });
|
||||
long.menu.select(6);
|
||||
long.paint();
|
||||
long.menu.select(4);
|
||||
long.paint();
|
||||
assert.equal(long.menu.scrollTop, 80, 'e is the top row');
|
||||
long.menu.setItems(['a', 'b', 'c', 'd', 'e'], 4);
|
||||
assert.deepEqual(long.paint().map((r) => r.item), ['c', 'd', 'e']);
|
||||
assert.equal(long.menu.scrollTop, 40);
|
||||
});
|
||||
@@ -5,16 +5,17 @@ const { GrayImage } = require('../.test-build/app/graphics/image.js');
|
||||
const { encodePresentation, readPresentation, paintPresentation } = require('../.test-build/app/graphics/presentation-wire.js');
|
||||
|
||||
test('highlight animates only navigation that preserves the scroll window', () => {
|
||||
const D = MENU_HIGHLIGHT_DURATION_MS;
|
||||
const motion = new MenuHighlightMotion();
|
||||
assert.equal(motion.paint(0, 0, 10, 20, 100, 20, 0), undefined);
|
||||
motion.navigate(0, false);
|
||||
const moved = motion.paint(1, 0, 10, 40, 100, 20, 100);
|
||||
assert.equal(moved.dx, 0);
|
||||
assert.equal(moved.dy, -20);
|
||||
assert.equal(moved.durationMs, 300);
|
||||
assert.equal(motion.paint(1, 0, 10, 40, 100, 20, 250).token, moved.token);
|
||||
assert.equal(moved.durationMs, D);
|
||||
assert.equal(motion.paint(1, 0, 10, 40, 100, 20, 100 + D / 4).token, moved.token);
|
||||
motion.navigate(1, false);
|
||||
const next = motion.paint(2, 0, 10, 60, 100, 20, 250);
|
||||
const next = motion.paint(2, 0, 10, 60, 100, 20, 100 + D / 2);
|
||||
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');
|
||||
@@ -22,8 +23,8 @@ 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.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.ok(motion.paint(1, 0, 10, 40, 100, 20, 600 + D - 1));
|
||||
assert.equal(motion.paint(1, 0, 10, 40, 100, 20, 600 + D), undefined, `finished after ${D} ms`);
|
||||
assert.equal(motion.paint(2, 0, 10, 60, 100, 20, 1200), undefined, 'programmatic changes snap');
|
||||
});
|
||||
|
||||
|
||||
@@ -31,6 +31,7 @@ function fixture() {
|
||||
'../compass/calibration': { calibrateHeading: value => value + 5, normalizeHeading: value => (value + 360) % 360 },
|
||||
'../../native/frame-timings': { startFrame: () => 1, finishFrame() {}, logFrame() {}, span: (_id, _name, fn) => fn(), runWithFrame: (_id, fn) => fn() },
|
||||
'../../native/active-display': { getActiveDisplay: () => null },
|
||||
'../../ui/menu-core': require('../.test-build/app/ui/menu-core.js'),
|
||||
'../../ui/window-menu': { WindowMenu: class { paint() { return []; } isOpen() { return false; } resize() {} } },
|
||||
'../../graphics/plane': { planesFingerprint: () => 'frame' },
|
||||
'../../native/mapbox': {
|
||||
|
||||
@@ -0,0 +1,87 @@
|
||||
const test = require('node:test');
|
||||
const assert = require('node:assert/strict');
|
||||
const fs = require('node:fs');
|
||||
const path = require('node:path');
|
||||
const vm = require('node:vm');
|
||||
const ts = require('typescript');
|
||||
const { GrayImage } = require('../.test-build/app/graphics/image.js');
|
||||
const { textInkBounds, textInkHeight, centeredTextY, tightRowHeight } = require('../.test-build/app/ui/metrics.js');
|
||||
const { Menu } = require('../.test-build/app/ui/menu-core.js');
|
||||
|
||||
function load(file) {
|
||||
const source = fs.readFileSync(path.join(__dirname, '..', file), 'utf8');
|
||||
const context = { exports: {}, require: () => ({}) };
|
||||
vm.runInNewContext(ts.transpileModule(source, {
|
||||
compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2020 },
|
||||
}).outputText, context);
|
||||
return context.exports;
|
||||
}
|
||||
const { BdfFont } = load('app/graphics/bdffont.ts');
|
||||
const terminus = (size) => BdfFont.parse(fs.readFileSync(path.join(__dirname, `../app/fonts/terminus/ter-u${size}n.bdf`), 'utf8'));
|
||||
const SIZES = [12, 14, 16, 18, 20, 22, 24];
|
||||
|
||||
/** A font whose glyphs sit `leading` px below the line top and end `trim` px above its bottom. */
|
||||
function leadingFont(lineHeight, leading, trim) {
|
||||
const ascent = lineHeight - 3;
|
||||
const inkHeight = lineHeight - leading - trim;
|
||||
return {
|
||||
lineHeight, ascent, descent: 3,
|
||||
// bbxY is the ink bottom's height above the baseline (negative below it).
|
||||
getGlyph: () => ({ bbxHeight: inkHeight, bbxY: ascent - leading - inkHeight, bbxWidth: 1, bbxX: 0 }),
|
||||
};
|
||||
}
|
||||
|
||||
test('Terminus ink fills its line box, so the compact row keeps its 12px pitch of 16', () => {
|
||||
for (const size of SIZES) {
|
||||
const font = terminus(size);
|
||||
assert.deepEqual(textInkBounds(font), { top: 0, bottom: font.lineHeight }, `ter-u${size}n`);
|
||||
assert.equal(tightRowHeight(font), font.lineHeight + 4);
|
||||
}
|
||||
assert.equal(tightRowHeight(terminus(12)), 16);
|
||||
});
|
||||
|
||||
test('ink bounds follow the rendered glyphs, not the line box', () => {
|
||||
const font = leadingFont(21, 4, 1);
|
||||
assert.deepEqual(textInkBounds(font), { top: 4, bottom: 20 });
|
||||
assert.equal(textInkHeight(font), 16);
|
||||
assert.equal(tightRowHeight(font), 20);
|
||||
// Centered in a 19px box: 3 spare pixels, 1 above; the line top sits 3 above the box to put ink at +1.
|
||||
assert.equal(centeredTextY(font, 100, 19), 100 + 1 - 4);
|
||||
});
|
||||
|
||||
test('fonts without glyph access fall back to the line box', () => {
|
||||
const font = { lineHeight: 14, ascent: 11, descent: 3 };
|
||||
assert.deepEqual(textInkBounds(font), { top: 0, bottom: 14 });
|
||||
assert.equal(centeredTextY(font, 10, 20), 13);
|
||||
});
|
||||
|
||||
test('centered text keeps at least a pixel of clearance inside a compact selection box', () => {
|
||||
for (const font of [...SIZES.map(terminus), leadingFont(21, 4, 1), leadingFont(17, 0, 2)]) {
|
||||
const box = tightRowHeight(font) - 1;
|
||||
const offset = centeredTextY(font, 0, box);
|
||||
const { top, bottom } = textInkBounds(font);
|
||||
assert.ok(offset + top >= 1, `top clearance at lineHeight ${font.lineHeight}`);
|
||||
assert.ok(offset + bottom <= box - 1, `bottom clearance at lineHeight ${font.lineHeight}`);
|
||||
}
|
||||
});
|
||||
|
||||
test('a selected compact row keeps every inked pixel of its text', () => {
|
||||
const label = 'Jgpqy (É|Å) 0189';
|
||||
const inked = (image) => image.withDrawsBaked().pixels.reduce((n, v) => n + (v !== 0 ? 1 : 0), 0);
|
||||
for (const size of SIZES) {
|
||||
const font = terminus(size);
|
||||
const reference = new GrayImage(400, 80);
|
||||
reference.drawText(font, 0, 20, label, 255);
|
||||
const menu = new Menu({
|
||||
items: [label],
|
||||
rowGap: 1,
|
||||
getHeight: () => tightRowHeight(font),
|
||||
draw: ({ image, item, x, y, height }) => image.drawText(font, x + 6, centeredTextY(font, y, height), item, 255),
|
||||
});
|
||||
const page = new GrayImage(420, 120);
|
||||
menu.paint(page, { x: 4, y: 4, width: 400, height: 100 }, true);
|
||||
const selection = page.draws.find((draw) => draw.presentation);
|
||||
assert.ok(selection, `ter-u${size}n selection`);
|
||||
assert.equal(inked(selection.source), inked(reference), `ter-u${size}n keeps all ink`);
|
||||
}
|
||||
});
|
||||
@@ -0,0 +1,112 @@
|
||||
const test = require('node:test');
|
||||
const assert = require('node:assert/strict');
|
||||
const fs = require('node:fs');
|
||||
const path = require('node:path');
|
||||
const vm = require('node:vm');
|
||||
const ts = require('typescript');
|
||||
|
||||
function load(file, modules) {
|
||||
const source = fs.readFileSync(path.join(__dirname, '..', file), 'utf8');
|
||||
const context = { exports: {}, console, require(name) { assert.ok(name in modules, name); return modules[name]; } };
|
||||
vm.runInNewContext(ts.transpileModule(source, {
|
||||
compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2020 },
|
||||
}).outputText, context);
|
||||
return context.exports;
|
||||
}
|
||||
|
||||
const graphics = require('../.test-build/app/graphics/image.js');
|
||||
const metrics = require('../.test-build/app/ui/metrics.js');
|
||||
const menuCore = require('../.test-build/app/ui/menu-core.js');
|
||||
const { BdfFont } = load('app/graphics/bdffont.ts', { '@nativescript/core': {} });
|
||||
const font = BdfFont.parse(fs.readFileSync(path.join(__dirname, '../app/fonts/terminus/ter-u12n.bdf'), 'utf8'));
|
||||
const textwrap = load('app/graphics/textwrap.ts', {});
|
||||
const fonts = { getDefaultSmallFont: () => font };
|
||||
const menu = load('app/ui/menu.ts', {
|
||||
'../graphics/image': graphics, '../graphics/textwrap': textwrap, '../graphics/ui-fonts': fonts,
|
||||
'../util/numeric-util': { clamp: (n, lo, hi) => Math.max(lo, Math.min(hi, n)) }, './gestures': {},
|
||||
'./metrics': metrics, './menu-core': menuCore,
|
||||
});
|
||||
let yields = 0;
|
||||
const { SettingsPanelLayer } = load('app/ui/dashboard/settings-panel.ts', {
|
||||
'../../graphics/ui-fonts': fonts, '../../graphics/image': graphics, '../../graphics/textwrap': textwrap,
|
||||
'../gestures': {}, '../layers': {}, '../menu': menu, '../menu-core': menuCore, '../metrics': metrics,
|
||||
'../shell/shell': { shell: { yieldFocusToSidebar: () => yields++ } },
|
||||
});
|
||||
|
||||
/** A panel over three sections; records which item callbacks fire. */
|
||||
function panel() {
|
||||
const selected = [];
|
||||
const item = (label, extra = {}) => ({ label, onSelect: () => selected.push(label), ...extra });
|
||||
const layer = new SettingsPanelLayer([
|
||||
{ label: 'Display', items: [item('Brightness'), item('Font'), item('Off', { disabled: true })] },
|
||||
{ label: 'Voice', items: Array.from({ length: 20 }, (_, i) => item(`Voice ${i}`, { description: 'Help text for this option.' })) },
|
||||
{ label: 'About', items: [], renderDetail: () => {} },
|
||||
]);
|
||||
const ctx = { stack: { getBaseSize: () => ({ width: 576, height: 260 }), isFocused: () => true } };
|
||||
const input = (type) => layer.handleInput({ type, timestampMs: 0 }, ctx);
|
||||
return { layer, input, paint: () => layer.paint(ctx), selected };
|
||||
}
|
||||
|
||||
const selections = (image) => image.draws.filter((d) => d.kind === 'image' && d.presentation);
|
||||
|
||||
test('the right column previews the section without a selection until focus enters it', async () => {
|
||||
const p = panel();
|
||||
assert.equal(selections(p.paint()).length, 1, 'only the left column is highlighted');
|
||||
await p.input('click');
|
||||
const both = selections(p.paint());
|
||||
assert.equal(both.length, 2);
|
||||
assert.equal(both[0].presentation.background, 0, 'left column shows an outline once focus moves right');
|
||||
assert.ok(both[1].x > 150, 'the right highlight lands in the right column');
|
||||
await p.input('click');
|
||||
assert.deepEqual(p.selected, ['Brightness']);
|
||||
await p.input('double-click');
|
||||
assert.equal(selections(p.paint()).length, 1, 'back to a preview');
|
||||
const before = yields;
|
||||
await p.input('double-click');
|
||||
assert.equal(yields, before + 1, 'double-click from the left column yields to the sidebar');
|
||||
});
|
||||
|
||||
test('moving between sections resets the right column to its top, unselected', async () => {
|
||||
const p = panel();
|
||||
await p.input('scroll-down');
|
||||
await p.input('click');
|
||||
for (let i = 0; i < 15; i++) await p.input('scroll-down');
|
||||
p.paint();
|
||||
await p.input('double-click');
|
||||
await p.input('scroll-up');
|
||||
await p.input('scroll-down');
|
||||
assert.equal(selections(p.paint()).length, 1);
|
||||
await p.input('click');
|
||||
await p.input('click');
|
||||
assert.deepEqual(p.selected, ['Voice 0'], 'entering starts at the first item');
|
||||
});
|
||||
|
||||
test('watch swipes move within a column, wrap, and skip disabled activation', async () => {
|
||||
const p = panel();
|
||||
await p.input('swipe-up');
|
||||
await p.input('click');
|
||||
assert.deepEqual(p.selected, [], 'About has no items, so focus stays in the left column');
|
||||
await p.input('swipe-down');
|
||||
await p.input('swipe-right');
|
||||
await p.input('swipe-up');
|
||||
await p.input('swipe-right');
|
||||
assert.deepEqual(p.selected, [], 'the wrapped-to item is disabled');
|
||||
await p.input('swipe-up');
|
||||
await p.input('swipe-right');
|
||||
assert.deepEqual(p.selected, ['Font']);
|
||||
});
|
||||
|
||||
test('the selected row stays clear of its description band', async () => {
|
||||
const p = panel();
|
||||
await p.input('scroll-down');
|
||||
await p.input('click');
|
||||
await p.input('scroll-up');
|
||||
const row = selections(p.paint())[1];
|
||||
const overlay = new graphics.GrayImage(576, 260);
|
||||
p.layer.paintDescriptionOverlay(overlay, { stack: { getBaseSize: () => ({ width: 576, height: 260 }) } });
|
||||
let bandTop = -1;
|
||||
for (let y = 0; y < 260 && bandTop < 0; y++) if (overlay.getPixel(300, y) !== 0) bandTop = y;
|
||||
assert.ok(bandTop > 0, 'the band is drawn');
|
||||
assert.ok(row.y + row.source.height <= bandTop, `row ends at ${row.y + row.source.height}, band starts at ${bandTop}`);
|
||||
assert.ok(row.y + row.source.height > bandTop - 30, 'the last item scrolled down to just above the band');
|
||||
});
|
||||
@@ -4,6 +4,7 @@ const fs = require('node:fs');
|
||||
const path = require('node:path');
|
||||
const vm = require('node:vm');
|
||||
const ts = require('typescript');
|
||||
const { Menu } = require('../.test-build/app/ui/menu-core.js');
|
||||
|
||||
function load(file, deps, globals = {}) {
|
||||
const exports = {};
|
||||
@@ -27,7 +28,10 @@ function dialog({ autoSend = false, deferred = true, synchronousFinal, isIOS = f
|
||||
'../../native/anthropic': { refineDictation(options) { refinements.push(options); return { cancel() {} }; } },
|
||||
'../dashboard-settings': { anthropicApiKeySetting: { get: () => 'key' } },
|
||||
'../gestures': { gestureHints: () => '' },
|
||||
'./input-dialog': { paintInputDialog(_image, options) { frame = options; } },
|
||||
'./input-dialog': {
|
||||
paintInputDialog(_image, options) { frame = options; },
|
||||
createInputDialogMenu: (items, selectedIndex) => new Menu({ items, selectedIndex, wrap: true, getHeight: () => 1, draw() {} }),
|
||||
},
|
||||
}, {
|
||||
global: { isIOS },
|
||||
setTimeout(cb) { const id = ++timerId; timers.set(id, cb); return id; },
|
||||
|
||||
Reference in new issue
Block a user