Repository navigation
Include animated props in layout reads with the shared animated backend - #60474
fairysword wants to merge 2 commits into
Conversation
Experimental flag, off by default, for including the animated props of the shared animated backend in layout reads from JS (measure, measureInWindow, measureLayout, getBoundingClientRect, ...). Nothing reads it yet. Generated with `yarn featureflags --update`.
With the shared animated backend, non-layout props such as transform are written to the views and to AnimatedPropsRegistry on every frame, but not committed to the shadow tree. AnimationBackendCommitHook applies the registry when React commits, so renders don't revert running animations. Layout reads from JS (measure, measureInWindow, measureLayout, getBoundingClientRect, ...) read the revision captured by LazyShadowTreeRevisionConsistencyManager though, which doesn't include these props. Pressability measures the pressable on press in, so a view moved or scaled by a native-driven animation is measured at its old position and the first move cancels the press (react#51621). Behind the enableAnimatedPropsInLayoutReads flag, the revision that JS reads is cloned with the props from the registry, with the same cloning as the commit hook (moved into cloneWithAnimatedProps). The clones are only used for reads: RSNRU is suspended while cloning, so the references held by React keep pointing to committed nodes. The tree is cloned when JS reads the shadow tree (layout reads, but also DOM traversal like parentNode), not when React commits. Behind the flag, completeSurface only drops the revision captured for the surface, and the next read captures it again with the props from the registry. Capturing it in completeSurface would call AnimatedPropsRegistry::getMap before clearRegistry, which would then clear the props that the UI thread wrote after the commit hook, although they were never committed. This way, commits that are not followed by such a read also don't clone the tree. This covers C++ Animated with the shared backend only. The platform Animated modules don't write to the registry and are unchanged. One of the Fantom tests reads the layout in render and in a layout effect of the same task, so the read after the commit has to see the new commit rather than the revision captured before it. The consistency manager tests cover that invalidating makes the next read capture and transform the revision again.
ddb271c to
29a5b3e
Compare
zeyap
left a comment
There was a problem hiding this comment.
Thanks for putting up the fix. A few things would make this more convincing on the testing side. #59392's test plan is a good model:
-
Flag-on run with the same setup. The same 35 presses on the same builds with
enableAnimatedPropsInLayoutReadson, reported per platform: how many firedonPress, and whatmeasure()returned at press-in. Ideally 0 dropped, andmeasure()matching the on-screen size or position. -
A breakdown of the 19 dropped presses. How many measured 0 x 0 and how many measured the full box? If some drops measured the full size, the stale read probably isn't the cause, and the flag may not fix those.
-
An RNTester repro. A "Pressable inside a natively animated view" example (or reuse the one from #59392), with the device or simulator and OS you tested on, plus a before/after line, e.g. "on
maina press with finger drift only firesonPressIn; with the flag it firesonPressInandonPress." -
Test Plan and Changelog sections in the PR description, listing the commands you ran (
yarn fantom …, the gtests) and anything you couldn't run locally.
Summary:
Pressabilitymeasures a view that a native-driven animation has already moved, but still sees the committed (stale) geometry, so the firstMOVEcancels the press (onPressInfires,onPressdoes not).AnimatedPropsRegistryand the same cloning path asAnimationBackendCommitHook: when JS reads layout (measure,measureInWindow,measureLayout,getBoundingClientRect, …), the revision it sees also has the registry values written into the node props.enableAnimatedPropsInLayoutReads(default off, experimental). Two commits: flag generation, then the fix.Problem
With
useNativeDriver: trueand the shared animated backend:transformare written straight to the mounted views and recorded inAnimatedPropsRegistry.AnimationBackendCommitHookalready writes the registry values into the new tree so React does not wipe the running animation.LazyShadowTreeRevisionConsistencyManager, which still has the last committed props.Pressabilitymixes the two worlds: itmeasure()s the responder on press-in (shadow tree), then compares that rect with native touch coordinates on every move (live view). On a moving / scaling view the measured rect is wrong, so the first finger drift looks likeLEAVE_PRESS_RECTandonPressis dropped.Reproducer: the App from #51621 (and the same pattern as RNTester / community demos linked from that issue).
Bug path
sequenceDiagram participant Anim as Shared Animation Backend participant View as Mounted View participant Reg as AnimatedPropsRegistry participant Tree as Committed Shadow Tree participant Press as Pressability participant Measure as measure / getBoundingClientRect loop Each animation frame Anim->>View: Write transform (synchronous) Anim->>Reg: Record current animated props Note over Tree: No per-frame commit end Press->>Measure: On press in Measure->>Tree: Read current revision Tree-->>Press: Stale transform / size Note over Press: Press rect is wrong Press->>Press: MOVE vs stale rect → LEAVE_PRESS_RECT Note over Press: onPressIn yes, onPress noApproach in this PR
The commit hook already knows how to take a root and produce a tree whose animated nodes carry the registry values (
cloneWithAnimatedProps). Layout reads need the same tree, without committing it and without retargeting React's runtime shadow node references at those clones.cloneWithAnimatedProps.UIManagerAnimationBackend::getRootShadowNodeWithAnimatedProps.LazyShadowTreeRevisionConsistencyManageroptionally transform the revision it exposes to JS;UIManagerwires that to the backend whenuseSharedAnimatedBackendandenableAnimatedPropsInLayoutReadsare on.completeSurface, when the flag is on, only drop the revision captured for the surface instead of capturing it again right away (invalidateCurrentRevision). Capturing it there would callAnimatedPropsRegistry::getMapbetween the commit hook andclearRegistry:getMapmoves the props that the UI thread wrote after the commit hook frompendingMapintomap, andclearRegistrythen clears them although they were never committed. If that was the last frame of an animation, the shadow tree keeps the previous value. The next read in the task captures the new commit with the props from the registry, so the tree is also cloned only when JS reads the shadow tree, not on every React commit. With the flag off,completeSurfaceis unchanged.Fix path
sequenceDiagram participant Anim as Shared Animation Backend participant View as Mounted View participant Reg as AnimatedPropsRegistry participant Cons as LazyShadowTreeRevisionConsistencyManager participant Backend as getRootShadowNodeWithAnimatedProps participant Press as Pressability participant Measure as measure / getBoundingClientRect loop Each animation frame Anim->>View: Write transform (synchronous) Anim->>Reg: Record current animated props end Press->>Measure: On press in Measure->>Cons: getCurrentRevision Cons->>Backend: Transform revision for layout reads Backend->>Reg: getMap(surfaceId) Backend->>Backend: cloneWithAnimatedProps (RSNRU off) Backend-->>Cons: Tree with current animated props Cons-->>Measure: Live geometry Measure-->>Press: Correct press rect Note over Press: MOVE stays inside rect → onPress firesReact commit path (commit hook unchanged, shared helper)
Other approaches
Community proposals for the same symptom, listed here for reviewers' reference.
Measure the mounted native view — #51835, #55025
Make
Pressability(or Fabricmeasureitself) measure the UI-thread view hierarchy instead of the shadow tree, like Paper'sNativeViewHierarchyManager. #51835 adds ameasureAsyncOnUIelement method thatPressabilitycalls; #55025 makesmeasure/measureInWindowinUIManagerBindingask the native mounting layer throughSchedulerDelegate.Record synchronous transforms at the write funnel — #59392
Whenever props are written synchronously to a view, record
transform/transformOriginin a small map onUIManager. Layout metrics (computeRelativeLayoutMetrics) consult that map before falling back to committed props.Why this PR
On current
main, OSS stable already enables C++ Native Animated and the shared animated backend. The registry is already the source of truth for “what the backend last applied.” React commits already trust that registry via the commit hook. Layout reads use the same source for the same reason, with the same clone helper.Scope note: This only applies to animation frontends that write through the shared backend into
AnimatedPropsRegistry.Commits
Add enableAnimatedPropsInLayoutReads feature flag— config +yarn featureflags --updateonly.Include animated props in layout reads with the shared animated backend— C++ wiring, docs, Fantom and C++ unit tests.Changelog:
[General][Fixed] - Fix layout reads such as
measureandgetBoundingClientRectnot seeing the props set by running native-driven animations with the shared animated backend, which made Pressable droponPresson views moved or scaled by them, behind the experimentalenableAnimatedPropsInLayoutReadsflagTest Plan:
AnimatedBackendLayoutReads-itest.js— mid-animationmeasure/getBoundingClientRectmatch the running transform; still correct after a re-render that changes layout; a read after a commit in the same task sees the new commit, while a read before it in the same task doesn't; fails when the flag is off.LazyShadowTreeRevisionConsistencyManagerTest— afterinvalidateCurrentRevision, the next read captures the new revision and the revision is captured again; the revision transformer only runs on the read, once per lock.enableAnimatedPropsInLayoutReads(with shared backend on), run the #51621 reproducer; press the moving/scaling target with slight finger movement —onPressshould fire andmeasure()on press-in should track the live geometry. Done with 20 presses moving 5dp (5pt on iOS) per run: on Android with the same build on a vivo V2338A and a low-end MT6763 device (input swipe), and on an iPhone 17 Pro simulator with iOS 26.5 (idb ui swipe). All 35 presses that landed on the box firedonPress(20 on Android, 15 on iOS), andmeasure()returned the current size (e.g.213.7 x 157.1).onPress(11 of 21 on Android, 8 of 14 on iOS).measure()only returned what was committed at the end of the previous animation (0 x 0or the full size of the box), and every press measured at0 x 0was dropped.