arc-motion 0.4.0

Physics-first animation & motion engine for GPUI: analytical ODE springs, cubic bezier, FLIP shared layout, presence, drag/rubber-band, inertia, and SVG path morphing.
Documentation
//! 嵌套滚动的事件消费适配:沿用 GPUI 默认位移与命中,只补边界收敛和冒泡规则。

use std::cell::Cell;

use gpui_kit::{App, ScrollEvent, ScrollHandle, Window, point, px};

/// 在 `.track_scroll(handle).overflow_*_scroll()` 后注册到同一元素的 `on_scroll`。
///
/// GPUI 0.11 的内置滚动先于用户监听运行,但移动后仍继续冒泡。
/// 每次绘制从当前 offset 建立基线,每次事件后更新,支持同一帧内多次滚轮。
/// 本次实际移动(即使大增量越过边界)即消费;已经贴边才交还祖先。
/// 程序化设置 offset 后应像 GPUI 默认约定一样请求重新绘制。
/// `horizontal` 为 true 时支持 Shift+纵向滚轮映射到横向,不重复滚动纵轴。
pub fn scroll_chain_handler(
    handle: &ScrollHandle,
    horizontal: bool,
) -> impl Fn(&ScrollEvent, &mut Window, &mut App) + 'static {
    let handle = handle.clone();
    let previous = Cell::new(handle.offset());
    move |event, window, cx| {
        let max = handle.max_offset();
        let min = point((-max.x).min(px(0.0)), (-max.y).min(px(0.0)));
        let before = previous.get();
        let before = point(
            before.x.clamp(min.x, px(0.0)),
            before.y.clamp(min.y, px(0.0)),
        );
        let raw = handle.offset();
        let delta = event.delta.pixel_delta(window.line_height());
        let next = if horizontal && event.modifiers.shift && delta.x == px(0.0) {
            point((before.x + delta.y).clamp(min.x, px(0.0)), before.y)
        } else {
            point(raw.x.clamp(min.x, px(0.0)), raw.y.clamp(min.y, px(0.0)))
        };
        if next != raw {
            handle.set_offset(next);
            window.refresh();
        }
        previous.set(next);
        if next != before {
            cx.stop_propagation();
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use gpui_kit::test::TestWindowExt as _;
    use gpui_kit::{
        Context, IntoElement, ParentElement, Render, ScrollDelta, Styled, TestAppContext,
        TouchPhase, div, prelude::*,
    };

    struct Host(ScrollHandle);
    impl Render for Host {
        fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
            div()
                .id("scroll-test")
                .w(px(120.0))
                .h(px(120.0))
                .overflow_scroll()
                .track_scroll(&self.0)
                .child(div().w(px(600.0)).h(px(600.0)).flex_none())
        }
    }

    #[gpui_kit::test]
    fn chain_clamps_overshoot_and_tracks_multiple_events_per_frame(cx: &mut TestAppContext) {
        let handle = ScrollHandle::new();
        let (_, window) = cx.add_window_view(|_, _| Host(handle.clone()));
        window.update(|w, cx| {
            w.render_frame(cx);
            let callback = scroll_chain_handler(&handle, true);
            let event = |dx, dy, shift| {
                let modifiers = gpui_kit::Modifiers {
                    shift,
                    ..Default::default()
                };
                ScrollEvent {
                    position: point(px(10.0), px(10.0)),
                    delta: ScrollDelta::Pixels(point(px(dx), px(dy))),
                    modifiers,
                    touch_phase: TouchPhase::Moved,
                    source: Default::default(),
                }
            };
            // 模拟默认滚动先移动 offset;多次事件之间不重绘。
            for dy in [-30.0, -10000.0, 20.0] {
                let before = handle.offset();
                handle.set_offset(point(before.x, before.y + px(dy)));
                callback(&event(0.0, dy, false), w, cx);
                assert!(handle.offset().y >= -handle.max_offset().y);
                assert!(handle.offset().y <= px(0.0));
            }
            let before = handle.offset();
            handle.set_offset(point(before.x, before.y - px(40.0)));
            callback(&event(0.0, -40.0, true), w, cx);
            assert_eq!(handle.offset().x, px(-40.0));
            assert_eq!(
                handle.offset().y,
                before.y,
                "Shift 横向滚动不能重复移动纵轴"
            );
        });
    }
}