|
| 1 | +import { interpolateGesturePoint, sampleGestureOffsets } from './gesture-plan.ts'; |
| 2 | +import type { |
| 3 | + GesturePlan, |
| 4 | + MultiTouchGesturePlan, |
| 5 | + PointerTrajectory, |
| 6 | + PointerTrajectorySample, |
| 7 | + SinglePointerGesturePlan, |
| 8 | +} from './gesture-plan-types.ts'; |
| 9 | +import type { Rect } from '@agent-device/kernel/snapshot'; |
| 10 | + |
| 11 | +export type AndroidLongPressTouchPlan = { |
| 12 | + topology: 'single'; |
| 13 | + intent: 'longPress'; |
| 14 | + durationMs: number; |
| 15 | + pointers: readonly [PointerTrajectory]; |
| 16 | +}; |
| 17 | + |
| 18 | +export type AndroidTouchPlan = GesturePlan | AndroidLongPressTouchPlan; |
| 19 | + |
| 20 | +/** |
| 21 | + * Transport samples are strictly denser than the canonical endpoint pair, so the shared plan a |
| 22 | + * command builds cannot satisfy the transport types: skipping the lowering below is a type error |
| 23 | + * at every injection seam rather than a silently sparse gesture. |
| 24 | + */ |
| 25 | +type AndroidTransportSamples = readonly [ |
| 26 | + PointerTrajectorySample, |
| 27 | + PointerTrajectorySample, |
| 28 | + PointerTrajectorySample, |
| 29 | + ...PointerTrajectorySample[], |
| 30 | +]; |
| 31 | + |
| 32 | +type AndroidTransportSinglePointerTrajectory = { |
| 33 | + pointerId: 0; |
| 34 | + samples: AndroidTransportSamples; |
| 35 | +}; |
| 36 | + |
| 37 | +export type AndroidTransportSinglePointerGesturePlan = Omit< |
| 38 | + SinglePointerGesturePlan, |
| 39 | + 'pointers' |
| 40 | +> & { |
| 41 | + pointers: readonly [AndroidTransportSinglePointerTrajectory]; |
| 42 | +}; |
| 43 | + |
| 44 | +export type AndroidTransportGesturePlan = |
| 45 | + | AndroidTransportSinglePointerGesturePlan |
| 46 | + | MultiTouchGesturePlan; |
| 47 | + |
| 48 | +export type AndroidLoweredTouchPlan = AndroidTransportGesturePlan | AndroidLongPressTouchPlan; |
| 49 | + |
| 50 | +export type AndroidProviderTouchPlan = |
| 51 | + | AndroidTransportGesturePlan |
| 52 | + | (AndroidLongPressTouchPlan & { viewport: Rect }); |
| 53 | + |
| 54 | +export function lowerAndroidTouchPlan(plan: AndroidTouchPlan): AndroidLoweredTouchPlan { |
| 55 | + if (plan.topology === 'two' || plan.intent === 'longPress') return plan; |
| 56 | + |
| 57 | + const [{ pointerId, samples: canonicalSamples }] = plan.pointers; |
| 58 | + const offsets = [ |
| 59 | + ...new Set([ |
| 60 | + ...sampleGestureOffsets(plan.durationMs, 'android'), |
| 61 | + ...canonicalSamples.map(({ offsetMs }) => offsetMs), |
| 62 | + ]), |
| 63 | + ].sort((left, right) => left - right); |
| 64 | + let segmentIndex = 0; |
| 65 | + const dense = offsets.map((offsetMs) => { |
| 66 | + while ( |
| 67 | + segmentIndex < canonicalSamples.length - 2 && |
| 68 | + offsetMs > canonicalSamples[segmentIndex + 1]!.offsetMs |
| 69 | + ) { |
| 70 | + segmentIndex += 1; |
| 71 | + } |
| 72 | + const start = canonicalSamples[segmentIndex]!; |
| 73 | + const end = canonicalSamples[segmentIndex + 1]!; |
| 74 | + const segmentDurationMs = end.offsetMs - start.offsetMs; |
| 75 | + return { |
| 76 | + offsetMs, |
| 77 | + point: |
| 78 | + offsetMs === end.offsetMs || segmentDurationMs === 0 |
| 79 | + ? end.point |
| 80 | + : interpolateGesturePoint( |
| 81 | + start.point, |
| 82 | + end.point, |
| 83 | + (offsetMs - start.offsetMs) / segmentDurationMs, |
| 84 | + ), |
| 85 | + }; |
| 86 | + }); |
| 87 | + // The sampler floors the frame count at three, so `dense` always holds at least four samples. |
| 88 | + const samples: AndroidTransportSamples = [dense[0]!, dense[1]!, dense[2]!, ...dense.slice(3)]; |
| 89 | + |
| 90 | + return { |
| 91 | + ...plan, |
| 92 | + pointers: [{ pointerId, samples }], |
| 93 | + }; |
| 94 | +} |
0 commit comments