Upstream: crates/rune/README.md § Shared-element motion at 65f8eb43f710, MIT © Anomaly. Bundled by build-rune-apps; links point at this snapshot or at GitHub at that rev.

Shared-element motion

El::matched(id) is SwiftUI's matchedGeometryEffect (Motion's layoutId). When an element with an id leaves the tree in the same commit that another with that id mounts somewhere else, the new one starts where the old one was seen, with its velocity, corner radius, fill and opacity, and springs to its own layout. Both places are ordinary layout:

fn tile(s: &State, i: usize) -> El {
    let id = format!("tile-{i}");
    el().key(id.as_str()).matched(id.as_str()).grow(1.0).radius(20.0).clip_rounded(20.0)
        .child(el().w_full().h_full().pixels_fit(&s.pictures[i], SurfaceFit::Fill))
}

if s.zoomed {
    stage.child(tile(s, s.picked))                      // the whole stage
} else {
    stage.child(col().key("grid").grow(1.0).gap(12.0)   // a 2×2 grid
        .transition(Transition::scale(0.96))
        .child(row().key("top").grow(1.0).gap(12.0).child(tile(s, 0)).child(tile(s, 1)))
        .child(row().key("bottom").grow(1.0).gap(12.0).child(tile(s, 2)).child(tile(s, 3))))
}

examples/matched is this Grid → One zoom (1–4 pick, ↩ zoom, Esc back).

  • Scale, not reflow. The flying element's children keep their final layout, scaled uniformly to cover the moving frame (centred) and clipped to it, so a filled picture zooms without distorting and nothing re-lays out per frame. Text moves with its box but keeps its size, as under any scale, so a label doesn't pop at the swap.
  • Above everything. A flight paints after the rest of the window, outside its ancestors' opacity, scale, blur and rounded clips, so an entering or exiting parent (the grid's scale-and-fade above) never hides it.
  • Clipping. It keeps its destination's rectangular clips, a scroll viewport's included, and starts from where the source was seen after its own viewport's scroll.
  • Interruptible. Matching back mid-flight starts from the presented rect with its velocity; a commit that leaves its layout alone doesn't re-time it.
  • Spring. tok::motion::REFLOW unless the commit's Tx::motion or the element's own .motion() sets one. Under reduced motion nothing is matched: the old element exits and the new one enters by their own transitions, in place. Settle and first frames swap.
  • Nesting and removal. A matched element inside a flying one flies its own path. A flight lands once the ancestors it sits in stop moving, scaling and fading. One removed mid-way exits by its own transition, still above everything.
  • Ids are per window. When two present elements share an id, the last in tree order is matched; --tree lists a warning and debug builds print one.
  • Elements without a counterpart play their own transitions. Without any matched element, the runtime does no extra work and draws the same pixels. A flight costs a scan of the flying list per drawn node, and its subtree paints in tree order.