Skip to content
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/wheel-uikit-picker-values.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@astryxdesign/core': patch
---

[chore] Internal: UIPickerView's values measured on iOS 26.2 join the touch motion tables as internal constants: the wheel's geometry (row pitch, cylinder radius, perspective, the selection band, the band row's magnification), the coast's stop speed and velocity floor, the snap at rest, the tap spin and the selection tick. The pure math a wheel moves and draws with comes with them. It projects the row a release rests on as UIScrollView sums its deceleration, coasts with the rate re-solved to stop on that row, snaps, bounces at an end, rubber-bands, spins to a tapped row, and turns each row onto the cylinder. It is checked against UIKit's recorded frames. Nothing public changes: none of it is exported from the package entry point, and themes do not set these values.

@vjeux
318 changes: 318 additions & 0 deletions packages/core/src/DateInput/wheelDrum.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,318 @@
// Copyright (c) Meta Platforms, Inc. and affiliates.

/**
* @file wheelDrum.test.ts
* @input wheelDrum.ts and wheelPhysics.ts, and a UIPickerView's cell
* transforms read at rest
* @output Proves the drum puts rows where UIKit's cylinder does, that a tap
* finds the row drawn under it through the perspective, and that a baked
* motion stays within tolerance, never sweeps a parked row across the front,
* and leaves rows that never show without animations
* @position Unit coverage for DateInput/wheelDrum.ts
*/

import {describe, expect, it} from 'vitest';
import {
BAKE_TOLERANCE,
PARKED_ANGLE,
drumGeometry,
isRowVisible,
rowAngle,
rowAtPoint,
rowDistance,
rowExtent,
rowKeyframes,
rowOrigin,
rowTransform,
sampleCurve,
stripKeyframes,
stripTranslation,
} from './wheelDrum';
import {planRelease, spinCurve, type WheelLayout} from './wheelPhysics';

const MINUTES: WheelLayout = {rowHeight: 32, rowCount: 60};
const LOOPING: WheelLayout = {...MINUTES, looping: true};
const GEOMETRY = drumGeometry(32);

/** Where CSS puts a row's centre: rotateX(−θ) about an origin R behind it. */
function cylinderPosition(angle: number, radius: number) {
return {y: radius * Math.sin(angle), z: radius * (Math.cos(angle) - 1)};
}

describe('the drum at rest', () => {
it('turns rows ±1 to ±4 as UIPickerView does', () => {
// UIKit's cell transforms: rotations of 18.85, 37.8, 56.7 and 75.7°
// (18.92° a row; its depths fit R = 97.2, which gives 18.86°).
const measured = [18.85, 37.8, 56.7, 75.7];
measured.forEach((degrees, i) => {
const angle = rowAngle(
rowDistance(31 + i + 1, 31 * 32, MINUTES),
GEOMETRY,
);
expect(Math.abs((angle * 180) / Math.PI - degrees)).toBeLessThan(0.3);
const above = rowAngle(
rowDistance(31 - i - 1, 31 * 32, MINUTES),
GEOMETRY,
);
expect(above).toBeCloseTo(-angle, 12);
});
});

it('moves rows onto the cylinder where UIKit’s cells sit', () => {
// UIKit's cells, relative to the band: depth −5.2, −20.4, −43.9, −73.2
// pt; along y 32k minus 0.41, 4.23, 14.5, 33.6 pt (at a rest offset a
// fraction of a point off the row).
const depth = [-5.2, -20.4, -43.9, -73.2];
const pull = [0.41, 4.23, 14.5, 33.6];
depth.forEach((z, i) => {
const k = i + 1;
const position = cylinderPosition(
rowAngle(k * 32, GEOMETRY),
GEOMETRY.radius,
);
expect(Math.abs(position.z - z)).toBeLessThan(0.5);
expect(Math.abs(position.y - (32 * k - pull[i]))).toBeLessThan(0.4);
});
});

it('writes the turn as one rotation about an origin R behind the row', () => {
expect(rowOrigin(GEOMETRY)).toBe('50% 50% -97.2px');
expect(rowTransform(32 / 97.2)).toBe('rotateX(-0.32922rad)');
});

it('parks rows past a quarter turn instead of turning them round', () => {
const angle = rowAngle(400, GEOMETRY);
expect(angle).toBe(PARKED_ANGLE);
expect(isRowVisible(angle)).toBe(false);
expect(isRowVisible(rowAngle(140, GEOMETRY))).toBe(true);
});

it('scales the whole drum with the row height', () => {
const geometry = drumGeometry(28);
expect(geometry.radius).toBeCloseTo(85.05, 2);
expect(geometry.height).toBe(189);
expect(rowAngle(28, geometry)).toBeCloseTo(rowAngle(32, GEOMETRY), 12);
});

it('shows each row of a looping wheel once, at the copy nearest the band', () => {
// Minute 0 at an offset on minute 58 sits two rows below the band.
expect(rowDistance(0, 58 * 32, LOOPING)).toBe(64);
expect(rowDistance(58, 0, LOOPING)).toBe(-64);
expect(rowDistance(30, 0, LOOPING)).toBe(-960);
});
});

type Matrix = number[][];

function multiply(a: Matrix, b: Matrix): Matrix {
return a.map(row =>
b[0].map((_, j) => row.reduce((sum, value, k) => sum + value * b[k][j], 0)),
);
}

const translateZ = (z: number): Matrix => [
[1, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 1, z],
[0, 0, 0, 1],
];

/**
* Where CSS draws a row's edges along y, from the band's centre: the row laid
* out there with `transform: rowTransform(angle)` and `transform-origin:
* rowOrigin(geometry)`, its parent's `perspective` seen from the band's
* centre. Composed as CSS Transforms 2 does, from the strings the drum hands
* CSS, not from `rowExtent`'s algebra.
*/
function cssExtent(angle: number) {
const turn = Number(
/rotateX\((-?[\d.e-]+)rad\)/.exec(rowTransform(angle))?.[1],
);
const originZ = Number(/(-?[\d.]+)px$/.exec(rowOrigin(GEOMETRY))?.[1]);
const rotateX: Matrix = [
[1, 0, 0, 0],
[0, Math.cos(turn), -Math.sin(turn), 0],
[0, Math.sin(turn), Math.cos(turn), 0],
[0, 0, 0, 1],
];
const perspective: Matrix = [
[1, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 1, 0],
[0, 0, -1 / GEOMETRY.perspective, 1],
];
const drawn = multiply(
perspective,
multiply(translateZ(originZ), multiply(rotateX, translateZ(-originZ))),
);
const at = (y: number) => {
const [, screenY, , w] = drawn.map(row => row[1] * y + row[3]);
return screenY / w;
};
return {top: at(-GEOMETRY.rowHeight / 2), bottom: at(GEOMETRY.rowHeight / 2)};
}

describe('which row is under a point', () => {
it('draws each row where CSS draws it, nearer the band than the cylinder alone near the edge', () => {
for (let row = -4; row <= 4; row++) {
const angle = rowAngle(row * 32, GEOMETRY);
const css = cssExtent(angle);
const extent = rowExtent(angle, GEOMETRY);
// The CSS strings round the turn to 1e-5 rad and the origin to 1e-3 px,
// which moves an edge by under 1e-3 px.
expect(extent.top).toBeCloseTo(css.top, 3);
expect(extent.bottom).toBeCloseTo(css.bottom, 3);
}
// The fourth row below the band spans 84.7–89.3 px, where the cylinder
// without its perspective puts the third row.
const fourth = rowExtent(rowAngle(4 * 32, GEOMETRY), GEOMETRY);
expect(fourth.top).toBeCloseTo(84.67, 2);
expect(fourth.bottom).toBeCloseTo(89.34, 2);
});

it('finds the row CSS draws under every point of the column', () => {
// A tap in the middle of the fourth row below or above the band turns
// to that row, not the third.
const fourth = rowExtent(rowAngle(4 * 32, GEOMETRY), GEOMETRY);
const middle = (fourth.top + fourth.bottom) / 2;
expect(rowAtPoint(middle, 31 * 32, MINUTES, GEOMETRY)).toBe(35);
expect(rowAtPoint(-middle, 31 * 32, MINUTES, GEOMETRY)).toBe(27);
for (const offset of [31 * 32, 31 * 32 + 11.5]) {
// Each drawn row by its position along the wheel.
const drawn = Array.from({length: 13}, (_, k) => {
const position = Math.round(offset / 32) - 6 + k;
const angle = rowAngle(position * 32 - offset, GEOMETRY);
return {position, visible: isRowVisible(angle), ...cssExtent(angle)};
}).filter(row => row.visible);
const half = GEOMETRY.height / 2;
for (let dy = -half; dy <= half; dy += 0.25) {
let expected: number | null;
if (Math.abs(dy) < GEOMETRY.bandHeight / 2) {
// The band's flat strip, the drum cut away behind it.
expected = Math.round((offset + dy) / 32);
} else {
const under = drawn.find(row => dy >= row.top && dy <= row.bottom);
const above = drawn.filter(row => row.bottom < dy);
const below = drawn.filter(row => row.top > dy);
if (under != null) {
expected = under.position;
} else if (above.length > 0 && below.length > 0) {
const a = above.reduce((x, y) => (y.bottom > x.bottom ? y : x));
const b = below.reduce((x, y) => (y.top < x.top ? y : x));
expected = dy - a.bottom <= b.top - dy ? a.position : b.position;
} else {
expected = null;
}
}
expect(
rowAtPoint(dy, offset, MINUTES, GEOMETRY),
`dy ${dy} at ${offset}`,
).toBe(expected);
}
}
});

it('inverts the cylinder', () => {
const y = GEOMETRY.radius * Math.sin(32 / GEOMETRY.radius);
expect(rowAtPoint(y, 31 * 32, MINUTES, GEOMETRY)).toBe(32);
expect(rowAtPoint(-y, 31 * 32, MINUTES, GEOMETRY)).toBe(30);
expect(rowAtPoint(0, 31 * 32, MINUTES, GEOMETRY)).toBe(31);
});

it('finds nothing past the drum or past the rows', () => {
expect(
rowAtPoint(GEOMETRY.radius + 1, 31 * 32, MINUTES, GEOMETRY),
).toBeNull();
expect(rowAtPoint(-40, 0, MINUTES, GEOMETRY)).toBeNull();
expect(rowAtPoint(-40, 0, LOOPING, GEOMETRY)).toBe(59);
});
});

describe('a baked motion', () => {
const coast = planRelease(1110, 1016.2, MINUTES).curve;

it('stays within the tolerance between samples', () => {
const samples = sampleCurve(coast);
expect(samples[0].t).toBe(0);
expect(samples[samples.length - 1].t).toBe(coast.duration);
expect(samples.length).toBeLessThan(400);
for (let i = 1; i < samples.length; i++) {
const a = samples[i - 1];
const b = samples[i];
for (const f of [0.1, 0.33, 0.5, 0.66, 0.9]) {
const t = a.t + (b.t - a.t) * f;
const linear = a.value + (b.value - a.value) * f;
expect(Math.abs(coast.value(t) - linear)).toBeLessThanOrEqual(
BAKE_TOLERANCE * 1.5,
);
}
}
});

it('animates only the rows that show during the motion', () => {
const samples = sampleCurve(coast);
// Rows 30 to 55 show at some point between row 34.7 and row 50.
expect(
rowKeyframes(10, samples, coast.duration, MINUTES, GEOMETRY),
).toBeNull();
expect(
rowKeyframes(59, samples, coast.duration, MINUTES, GEOMETRY),
).toBeNull();
const row40 = rowKeyframes(40, samples, coast.duration, MINUTES, GEOMETRY);
expect(row40).not.toBeNull();
expect(row40![0].offset).toBe(0);
expect(row40![row40!.length - 1].offset).toBe(1);
// It ends parked above the band, where row 40 sits when row 50 is on it.
expect(row40![row40!.length - 1].transform).toBe(
rowTransform(-PARKED_ANGLE),
);
const row50 = rowKeyframes(50, samples, coast.duration, MINUTES, GEOMETRY);
expect(row50![row50!.length - 1].transform).toBe(rowTransform(0));
});

it('steps a looping row to its other copy while it is out of sight', () => {
// From minute 58 across 0 to minute 3: minute 30 changes sides behind
// the drum and must never be drawn on the front on the way.
const spin = spinCurve(58 * 32, 63 * 32);
const samples = sampleCurve(spin);
const frames = rowKeyframes(28, samples, spin.duration, LOOPING, GEOMETRY);
expect(frames).toBeNull();
const minute1 = rowKeyframes(1, samples, spin.duration, LOOPING, GEOMETRY)!;
for (const frame of minute1) {
expect(frame.transform).not.toContain('NaN');
}
// Minute 33 is at the back the whole time: no keyframes. Minute 0 comes
// into view from below and passes the band.
const minute0 = rowKeyframes(0, samples, spin.duration, LOOPING, GEOMETRY)!;
expect(minute0[0].transform).toBe(rowTransform(rowAngle(64, GEOMETRY)));
expect(minute0[minute0.length - 1].transform).toBe(
rowTransform(rowAngle(-96, GEOMETRY)),
);
});

it('jumps a parked looping row with two keyframes at the same progress', () => {
// A long looping spin: row 0 goes once round the back.
const spin = spinCurve(0, 40 * 32);
const samples = sampleCurve(spin);
const frames = rowKeyframes(0, samples, spin.duration, LOOPING, GEOMETRY)!;
const steps = frames.filter(
(frame, i) => i > 0 && frame.offset === frames[i - 1].offset,
);
expect(steps.length).toBe(1);
for (let i = 1; i < frames.length; i++) {
expect(frames[i].offset).toBeGreaterThanOrEqual(frames[i - 1].offset);
}
});

it('translates the band strip with the offset, folding a looping strip', () => {
expect(stripTranslation(992, MINUTES)).toBe(-992);
expect(stripTranslation(992, LOOPING)).toBe(-(992 + 1920));
expect(stripTranslation(-32, LOOPING)).toBe(-(1888 + 1920));
const spin = spinCurve(58 * 32, 63 * 32);
const frames = stripKeyframes(sampleCurve(spin), spin.duration, LOOPING);
const steps = frames.filter(
(frame, i) => i > 0 && frame.offset === frames[i - 1].offset,
);
expect(steps.length).toBe(1);
});
});
Loading
Loading