Repository navigation
feat(motion)!: add UI-thread worklets and layout animations - #120
Merged
Merged
Conversation
Replace the Animated API with Motion. Worklets are Python functions in a compiled subset that the renderer core verifies and runs on the UI thread, so gestures, scroll-driven styles, and animations keep running while the Python thread is busy. - Core: a worklet VM with Python semantics, timing, spring, and decay drivers, a motion runtime (shared values, derived values, reactions, frame callbacks, animated styles, and event and gesture handlers), entering, exiting, and layout animations, and shared-element pairing. The C ABI moves to protocol 8. - Python: pn.worklet, use_shared_value, use_derived_value, use_animated_style, use_animated_reaction, use_frame_callback, with_timing, with_spring, with_decay, layout presets and Keyframes, SharedTransition, and release-build precompilation into pn_worklets.json. - Components: BottomSheet, Swipeable, and a DrawerNavigator rebuilt on worklets. - Hosts: iOS, Android, and the browser preview apply motion writes, run exits, and fly shared elements natively. iOS portals now stack above a screen mounted in the same commit. - Lint rules PN201 to PN203, a Motion category in the E2E suite, Field Notes adoption, and the Motion guide and API docs. BREAKING CHANGE: Animated, AnimatedValue, AnimationResult, use_animated_value, Animated.event, and the animation graph format are removed with no shims. See "Migrating from Animated" in the Motion guide.
- Use std::numbers::pi instead of M_PI, which MSVC doesn't define without _USE_MATH_DEFINES, so the Windows wheel builds. - Rebuild the browser preview's WebAssembly core with Emscripten 4.0.10 for protocol 8. - Browser tests: wait for a dismissed modal to leave before reusing its tags, and give the layout-animation siblings a paint property so the core doesn't flatten them away. - Exempt the pn.motion module re-export from E2E coverage, as icons is; the Motion demos cover what it exports. - Apply Black 26 to the files CI flagged. - Build arm64 macOS wheels on macos-15, since GitHub no longer runs macos-14. The deployment target stays at macOS 11.0.
… protocol 8 - Replace the GCC overflow builtins in the worklet VM with helpers that keep the builtins on GCC and Clang and use portable checks elsewhere, so MSVC builds the Windows wheel. The portable checks match the builtins on 174 million edge and random cases under UBSan. Rebuild the WebAssembly core to match. - The E2E suite's checksum plugin manifest still declared protocol 7, so every Android E2E build refused it. Bump it, and add a test that every checked-in plugin manifest matches the core's protocol and Yoga version. - Fix the mypy errors in the motion tests, which CI checks along with src.
The shared value and binding come from a ref and the backend is fixed for the component's lifetime, so each effect only releases them on unmount. Suppress PN103 with that reason, as BottomSheet does.
CI's Android emulator runs with system animations off, which the host reports as reduce motion. That made every system-following timing in the with_timing, composition, and layout animation demos jump to its end, so their flows never saw an interruption, a running repeat, or a mid-flight transform. - with_repeat and with_delay take reduce_motion, in Python and inside worklets, and the core honors it: a reduced delay is skipped, and a reduced repeat lands where its last run would end. - The three demos pass reduce_motion="never", since they test motion that's still running. - A repeat yields after 16 instant iterations in one frame, so an endless zero-duration repeat can't spin the UI thread. - Rebuild the WebAssembly core, and document the options in the guide and RFC 0009.
A closed sheet parks 48pt below its bottom edge so its shadow is hidden, but a release snapped against that hidden spot, so closing took a longer drag than it looked like it should. On CI's Android emulator, the BottomSheet flow's swipe down from the lowest detent snapped back to it. A release now decides against the sheet's visual bottom (the tallest detent's offset), then parks the closing sheet lower. A test drags a short, slow distance below the lowest detent and expects it to close. The whole Motion E2E suite passes on an Android 12 emulator with system animations off, as CI runs it.
The core reported a finished animation before publishing its final value, and a publish skipped whenever Python was mid-read. Python reads the shared value as soon as its await resolves, so it could read the previous frame's value. On the Android emulator, the with_spring demo showed "Scale: 1.00" after springing back to 0.50. Before emitting finished messages, the runtime now publishes, waiting for a reader to finish (readers hold the lock only for a lookup). A test reads the value from the finished message's callback.
XCTest's synthesized touches don't count as screen-edge touches on the iOS 18 simulator CI uses, so neither UIKit's back swipe nor the shared-element edge pan starts, and the flow failed on every run. On iOS 26 the full-screen content swipe pops instead, so the step never exercised the interactive transition on either runtime. The RFC now says so and notes that the flows run in CI.
iOS 26's back swipe starts anywhere on a pushed screen. It popped a screen while a Swipeable row or another horizontal Pan was dragging, and on a screen with shared elements it popped without tracking the finger. It now waits for any Pan that horizontal travel can begin, and it's off while the top screen shares elements with the one below, so the edge pan alone drives that pop. Both checked by hand on the iOS 26.2 simulator. - resolve_style keeps the animated bindings of an already-resolved entry, so an AnimatedStyle passed to BottomSheet still applies. - Swipeable and BottomSheet report on_open, on_close, and on_change once the spring settles, as their docs say, and Swipeable stays quiet when a drag springs back where it started. - with_repeat raises ValueError for times below 1 in Python and in worklets, instead of repeating forever. - A commit during a delayed entering animation puts the held value back instead of showing the static one until the delay ends. - Callback slots are dropped with their binding, so rows that mount and unmount no longer leak them. - iOS uses the core's exact quad and cubic curves. The WebAssembly core is rebuilt, and the RFC and guides describe the new behavior.
The E2E flows run on iOS 18, but the native tests run on an iOS 26.2 simulator in CI, so they can guard both iOS 26 back-swipe fixes. One test checks that the full-width swipe waits for a sideways Pan but not for a vertical-only one or UIKit's edge swipe. The other mounts two screens that share an element, checks a touch-down turns that swipe off, and checks it comes back once they stop sharing one. Both fail with the fixes removed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Implements RFC 0009: Motion, which replaces
Animatedwith worklets that run on the UI thread. Like the last few RFCs, it's breaking by design and keeps no shims. The RFC lists every removal.Why
Animatedcould only play animations that Python had already described. Anything decided during an interaction (where a sheet settles, whether a swipe passed its threshold, a style that follows a scroll offset) ran on the Python thread, so a busy render loop dropped frames or froze the gesture. Views also couldn't animate when they appeared, disappeared, or moved, and screens couldn't share an element.What changed
@pn.workletfunctions, and lambdas passed to Motion hooks, are written in a compiled subset of Python. The compiler turns theastinto stack bytecode, and the C++ core verifies it and interprets it on the UI thread with a 50,000-instruction budget. Semantics follow Python: floor division and modulo signs, half-to-even rounding, integer flags, and the exception types. Captures are resolved at install. Release builds precompile every worklet intopn_worklets.json, so shipped apps don't need the source.use_shared_value,use_derived_value,use_animated_style,use_animated_reaction, anduse_frame_callback. Python can read, write, andawait shared.animate(...)a shared value.with_timing,with_spring(physics or duration and damping ratio),with_decay(with clamping and rubber banding),with_delay,with_sequence, andwith_repeat. Each takeson_finishandreduce_motion. The helpers areinterpolate,interpolate_color,clamp,snap_point, andrun_on_python.on_*prop, includingon_scroll, and any gesture callback can take a worklet. The core runs it when the host reports the event, without a round trip to Python.on_layoutstays in Python.entering,exiting, andlayoutprops, with presets (fade, slide, zoom, and linear and spring transitions),Keyframes, and custom worklets. There's alsoLayoutAnimationConfiganditem_entering,item_exiting, anditem_layouton lists. An exiting view stays mounted in the native tree until its animation ends. Screens and modals exit through the platform.shared_tagandSharedTransition. The core pairs the tags between screens, and each host flies the view natively on push and pop. On iOS, an edge back swipe drives the flight interactively. iOS 26's full-width back swipe can't, so it's off while two screens share an element.BottomSheet(detents, velocity snapping, a backdrop, and a handle) andSwipeable(left and right actions, and a handle).DrawerNavigatoris rebuilt on worklets, with an edge swipe and a scrim.pn_core_motion,pn_core_shared_read,pn_core_worklet_eval,pn_core_exit_finished, andpn_core_shared_elements. Motion writes are a new mount record, and mount op 8 isexit.PNMotionApplier,PNSharedElementTransition), Android (PNMotionApplier,PNItemAnimator,SharedElementFlight), and the browser preview apply motion writes in the same frame. An animated key replaces the static key, including the wholetransformlist, on every host.pn lintrules PN201 to PN203 catch worklet mistakes before install, andRenderResult.drag(),presented_style(), andpresented_frame()test motion headlessly.Animated, an API page, a rewritten collapsing-header example, a 14-screen Motion category in the E2E suite, and Field Notes adoption: swipe-to-delete, list item animations, a shared photo, and a tag sheet.CI fixes
std::numbers::pireplacesM_PI), checks in the rebuilt WebAssembly core, fixes two browser test cases that reused tags and relied on views the core flattens, and moves arm64 macOS wheels tomacos-15because GitHub no longer runsmacos-14. The deployment target stays at macOS 11.0.pn lintin release builds;with_repeatandwith_delaycouldn't opt out of reduce motion; aBottomSheetreleased just below its lowest detent snapped back instead of closing; and a finished animation was reported before its final value was published, so Python could read the previous frame's value.Removed
Animated,AnimatedValue,AnimationResult,use_animated_value,Animated.event, the composites,animated.py,animation_graph.py, the core'sanimation.{h,cpp}andpn_core_animate, andPNAnimatoron iOS and Android. Also the browser's cloned "ghost" views and the nine old animation E2E screens and flows.Also fixed
Portalmounted in the same commit as its screen was stacked under the screen. Portals now stay on top, and taps that miss their content fall through.Review fixes
A high-effort review found 10 issues. Nine are fixed, each with a test:
Swipeablerow or another sidewaysPanwas dragging. It now waits for anyPanthat horizontal travel can begin, as it already did for scroll views. On a screen that shares elements with the one below, it popped without following the finger; it's now off there, and the edge swipe alone pops. A guarded screen orgesture_enabled=Falsenow turns it off too.resolve_stylekeeps the animated bindings of an already-resolved entry, so anAnimatedStylepassed toBottomSheetapplies.SwipeableandBottomSheetreporton_open,on_close, andon_changeonce the spring settles, as their docs say. A drag that springs aSwipeableback where it started reports nothing.with_repeatraisesValueErrorfortimesbelow 1, in Python and in worklets, instead of repeating forever.quadandcubiccurves.bouncestill plays as ease-out where UIKit runs the animation, which the guide notes.Skipped: handler worklets with an inline
run_on_pythontarget rebind on every render. It's extra work, not a bug, and a general fix risks stale callbacks.Verified
pn lint,check-e2e-coverage, andmkdocs build --strictmainon the same machine (rfcs/benchmarks/0009-motion.json). Import fell from 269 to 237 ms. Re-render and commit times are unchanged within noise. One frame that drives 200 animated styles takes about 1.6 ms.Not verified
Please don't read this PR as saying these work:
Known rough edges