Skip to main content

ScrollPhysics

Trait ScrollPhysics 

Source
pub trait ScrollPhysics: Debug {
    // Provided methods
    fn parent(&self) -> Option<&dyn ScrollPhysics> { ... }
    fn apply_physics_to_user_offset(
        &self,
        metrics: &ScrollMetrics,
        offset: f64,
    ) -> f64 { ... }
    fn apply_boundary_conditions(
        &self,
        metrics: &ScrollMetrics,
        value: f64,
    ) -> f64 { ... }
    fn create_ballistic_simulation(
        &self,
        metrics: &ScrollMetrics,
        velocity: f64,
    ) -> Option<Box<dyn Simulation>> { ... }
    fn should_accept_user_offset(&self, metrics: &ScrollMetrics) -> bool { ... }
    fn carried_momentum(&self, existing_velocity: f64) -> f64 { ... }
    fn min_fling_velocity(&self) -> f64 { ... }
    fn max_fling_velocity(&self) -> f64 { ... }
    fn tolerance_for(&self, metrics: &ScrollMetrics) -> Tolerance { ... }
    fn spring(&self) -> SpringDescription { ... }
}
Expand description

A pluggable scroll-motion strategy — Flutter’s ScrollPhysics contract.

§Chaining

Physics compose by parenting, not inheritance: Flutter’s const BouncingScrollPhysics().applyTo(const AlwaysScrollableScrollPhysics()) idiom becomes a concrete type storing an optional boxed parent (Option<Box<dyn ScrollPhysics>>) behind ScrollPhysics::parent, and a child overrides only the method(s) its own domain cares about — every other method’s default body here asks the parent for its answer, and only falls back to a hardcoded value when there is no parent at all. A type that overrides a method entirely opts out of that delegation for that method only (e.g. Snap(parent: Bouncing) overrides just create_ballistic_simulation, so every other method — including apply_boundary_conditions — still walks up to Bouncing).

Concrete physics expose a pub fn chain(self, parent: impl ScrollPhysics + 'static) -> Self building Some(Box::new(parent)) for ScrollPhysics::parent to return — every later physics type in this module follows that exact convention (same method name, same signature shape) so they compose with each other and with a caller’s own type uniformly.

Provided Methods§

Source

fn parent(&self) -> Option<&dyn ScrollPhysics>

The chained parent physics, if this one was built via chain(...). None for a physics built standalone (the chain’s root).

Source

fn apply_physics_to_user_offset( &self, metrics: &ScrollMetrics, offset: f64, ) -> f64

Map a raw user drag delta (finger px, signed in the content’s own direction) to the delta actually applied to the position. Must not alter the in-bounds portion of a drag — only a physics with an out-of-bounds opinion (e.g. added resistance) touches this.

Default: delegates to ScrollPhysics::parent if chained, otherwise the identity mapping (offset unchanged).

Source

fn apply_boundary_conditions(&self, metrics: &ScrollMetrics, value: f64) -> f64

Given a proposed new pixels value, return the portion the position must not absorb — the boundary-rejected excess. 0.0 means the proposal is fully allowed (a bouncing physics past an edge); the full proposed − clamped distance means none of it is (a clamping physics).

Default: delegates to ScrollPhysics::parent if chained, otherwise 0.0 (nothing rejected).

Source

fn create_ballistic_simulation( &self, metrics: &ScrollMetrics, velocity: f64, ) -> Option<Box<dyn Simulation>>

Build the ballistic motion to run after a gesture releases with velocity (px/s, signed like ScrollMetrics::pixels). None means no animation — the consuming widget falls back to its own legacy path/rest handling.

Default: delegates to ScrollPhysics::parent if chained, otherwise None.

Source

fn should_accept_user_offset(&self, metrics: &ScrollMetrics) -> bool

Whether a user drag is allowed to move the position at all.

Default: delegates to ScrollPhysics::parent if chained, otherwise whether the surface actually has scrollable content (max_scroll_extent > min_scroll_extent).

Source

fn carried_momentum(&self, existing_velocity: f64) -> f64

Momentum (px/s) to add onto a new fling’s initial velocity when motion is already live (a re-fling mid-animation carries some of the old velocity forward rather than starting cold).

Default: delegates to ScrollPhysics::parent if chained, otherwise 0.0.

Source

fn min_fling_velocity(&self) -> f64

The minimum release speed that starts a fling.

Default: delegates to ScrollPhysics::parent if chained, otherwise MIN_FLING_VELOCITY.

Source

fn max_fling_velocity(&self) -> f64

The fastest fling speed this physics honors.

Default: delegates to ScrollPhysics::parent if chained, otherwise MAX_FLING_VELOCITY.

Source

fn tolerance_for(&self, metrics: &ScrollMetrics) -> Tolerance

The velocity/distance tolerance a ballistic simulation settles within.

Default: delegates to ScrollPhysics::parent if chained, otherwise Tolerance::for_device_pixel_ratio over metrics.device_pixel_ratio.

Source

fn spring(&self) -> SpringDescription

The spring a bounce-back/snap simulation is built from.

Default: delegates to ScrollPhysics::parent if chained, otherwise SpringDescription::default_scroll_spring.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§