Skip to content

feat(motion)!: add UI-thread worklets and layout animations - #120

Merged
owenthcarey merged 11 commits into
mainfrom
feat/motion
Nov 11, 2026
Merged

owenthcarey merged 11 commits into
mainfrom
feat/motion

Conversation

@owenthcarey

@owenthcarey owenthcarey commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

Implements RFC 0009: Motion, which replaces Animated with 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

Animated could 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

  • Worklets. @pn.worklet functions, and lambdas passed to Motion hooks, are written in a compiled subset of Python. The compiler turns the ast into 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 into pn_worklets.json, so shipped apps don't need the source.
  • Shared values and hooks. use_shared_value, use_derived_value, use_animated_style, use_animated_reaction, and use_frame_callback. Python can read, write, and await shared.animate(...) a shared value.
  • Drivers. with_timing, with_spring (physics or duration and damping ratio), with_decay (with clamping and rubber banding), with_delay, with_sequence, and with_repeat. Each takes on_finish and reduce_motion. The helpers are interpolate, interpolate_color, clamp, snap_point, and run_on_python.
  • Event and gesture worklets. Any on_* prop, including on_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_layout stays in Python.
  • Layout animations. entering, exiting, and layout props, with presets (fade, slide, zoom, and linear and spring transitions), Keyframes, and custom worklets. There's also LayoutAnimationConfig and item_entering, item_exiting, and item_layout on lists. An exiting view stays mounted in the native tree until its animation ends. Screens and modals exit through the platform.
  • Shared elements. shared_tag and SharedTransition. 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.
  • Components. BottomSheet (detents, velocity snapping, a backdrop, and a handle) and Swipeable (left and right actions, and a handle). DrawerNavigator is rebuilt on worklets, with an edge swipe and a scrim.
  • Protocol 8. New C exports: pn_core_motion, pn_core_shared_read, pn_core_worklet_eval, pn_core_exit_finished, and pn_core_shared_elements. Motion writes are a new mount record, and mount op 8 is exit.
  • Hosts. iOS (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 whole transform list, on every host.
  • Tooling. pn lint rules PN201 to PN203 catch worklet mistakes before install, and RenderResult.drag(), presented_style(), and presented_frame() test motion headlessly.
  • Docs and examples. A Motion guide with a migration table from 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

  • The second commit builds the core on MSVC (std::numbers::pi replaces M_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 to macos-15 because GitHub no longer runs macos-14. The deployment target stays at macOS 11.0.
  • The worklet VM also builds on MSVC, and the E2E checksum plugin speaks protocol 8.
  • The E2E flows found four bugs, now fixed: the hooks' mount-only cleanup effects tripped pn lint in release builds; with_repeat and with_delay couldn't opt out of reduce motion; a BottomSheet released 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.
  • The shared-element flow no longer swipes back on iOS, because the simulator's synthesized touches can't start an edge swipe.

Removed

Animated, AnimatedValue, AnimationResult, use_animated_value, Animated.event, the composites, animated.py, animation_graph.py, the core's animation.{h,cpp} and pn_core_animate, and PNAnimator on iOS and Android. Also the browser's cloned "ghost" views and the nine old animation E2E screens and flows.

Also fixed

  • iOS: a Portal mounted 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:

  • iOS 26 back swipe. It starts anywhere on a pushed screen, so it popped the screen while a Swipeable row or another sideways Pan was dragging. It now waits for any Pan that 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 or gesture_enabled=False now turns it off too.
  • resolve_style keeps the animated bindings of an already-resolved entry, so an AnimatedStyle passed to BottomSheet applies.
  • Swipeable and BottomSheet report on_open, on_close, and on_change once the spring settles, as their docs say. A drag that springs a Swipeable back where it started reports nothing.
  • with_repeat raises ValueError for times below 1, in Python and in worklets, instead of repeating forever.
  • A delayed entering animation keeps its first frame when a commit sets one of its keys during the delay.
  • Callback slots are released with their binding, so rows that mount and unmount don't leak them.
  • iOS easing uses the core's exact quad and cubic curves. bounce still plays as ease-out where UIKit runs the animation, which the guide notes.

Skipped: handler worklets with an inline run_on_python target rebind on every render. It's extra work, not a bug, and a general fix risks stale callbacks.

Verified

  • Full pytest (2,896 passed, 14 skipped), Ruff, Black, mypy, pn lint, check-e2e-coverage, and mkdocs build --strict
  • A differential suite that runs each case in CPython and in the core's VM, including 300 generated expressions, plus tests that the verifier rejects malformed programs
  • XCTest (103 tests) on an iOS 26.2 simulator, locally and in CI, including regression tests for both iOS 26 back-swipe fixes; Android unit and Robolectric tests (96) in CI
  • Every Maestro shard in CI, including the 14 Motion flows and the Field Notes flows, on an iOS 18 simulator and an Android 12 (API 31) emulator
  • Browser acceptance tests in headless Chrome, including Motion, against the WebAssembly core rebuilt with Emscripten 4.0.10
  • By hand on the iOS 26.2 simulator: dragging a card sideways on a pushed screen moves the card and keeps the screen (without the fix, the screen pops), and a mid-screen swipe on a screen with a shared element doesn't pop it
  • A release build on the iOS simulator. Precompiled worklets load. A card drag and a swipeable row keep tracking while Python sleeps for 10 seconds. A shared element flies on push and pop, chips animate in and out, and the bottom sheet opens.
  • Benchmarks against main on 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:

  • The interactive shared-element pop on iOS. Simulators' synthesized touches can't start an edge swipe, so the finger-driven flight has never run. Only programmatic pops have.
  • iOS 26 in E2E. The Maestro flows run on iOS 18. On iOS 26, only the hand checks above and the native tests ran.
  • iOS shared-element backdrop. The E2E flow and the hand checks land the push and pop, but nobody inspected the temporary opaque background behind both screens mid-flight.
  • Physical devices and per-frame UI-thread timing on them.

Known rough edges

  • Flights don't retarget if a view relays out mid-flight.
  • A motion frame allocates heavily in the VM's value type, at about 8 µs per animated style on the host. That's fine for typical screens, and a flatter representation can come later.
  • Android release builds precompile only the app's own worklets, not those in third-party packages.
  • DevTools doesn't have Motion panels yet.

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.
@owenthcarey owenthcarey changed the title feat!: add Motion, UI-thread worklets and layout animations (RFC 0009) feat(motion)!: add UI-thread worklets and layout animations Nov 11, 2026
@owenthcarey
owenthcarey merged commit d4caedf into main Nov 11, 2026
48 checks passed
@owenthcarey
owenthcarey deleted the feat/motion branch November 11, 2026 22:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant