Skip to main content

BouncingSimulation

Struct BouncingSimulation 

Source
pub struct BouncingSimulation { /* private fields */ }
Expand description

Flutter BouncingScrollSimulation — the iOS UIScrollView fling.

While the content is inside [min, max] it coasts on exponential decay. The moment the coast would cross a bound, the simulation hands off to a spring anchored at that side’s rest target, so the content overshoots and is pulled back. A release that starts outside the range skips the coast and springs immediately.

§The two pairs of bounds

min/max and leading/trailing are different questions, and conflating them is the reason both are parameters:

  • min / max — the scroll extent: the offsets past which the content is overscrolled. This is where the coast gives way to the spring.
  • leading / trailing — the spring’s rest targets: where overscrolled content comes to rest. Normally leading == min and trailing == max, which is what within fills in. They differ when the resting place is not the crossing place — content that snaps to a sticky header, or to a paged boundary, past the edge it crossed.
use std::time::Duration;
use teksilo_core::kinetic::{BouncingSimulation, ScrollSimulation};

// Released mid-range with plenty of room: pure exponential decay.
let sim = BouncingSimulation::within(0.0, 1000.0, -1.0e6, 1.0e6);
// Flutter FrictionSimulation: x(1s) = v·(0.135 − 1)/ln(0.135).
assert!((sim.position(Duration::from_secs(1)) - 431.96).abs() < 0.5);

Implementations§

Source§

impl BouncingSimulation

Source

pub fn within(position: f32, velocity: f32, min: f32, max: f32) -> Self

The common case: the spring rests exactly at the bound it crossed.

Source

pub fn new( position: f32, velocity: f32, min: f32, max: f32, leading: f32, trailing: f32, ) -> Self

A bouncing fling with explicit spring rest targets, using the shipped physics tokens. See the type docs for what the two pairs mean.

Source

pub fn with_tokens( position: f32, velocity: f32, min: f32, max: f32, leading: f32, trailing: f32, tokens: &ScrollPhysicsTokens, ) -> Self

new against a theme’s own physics tokens.

Source

pub fn spring_time(&self) -> f32

When the spring takes over, in seconds; INFINITY when it never does.

Trait Implementations§

Source§

impl Clone for BouncingSimulation

Source§

fn clone(&self) -> BouncingSimulation

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for BouncingSimulation

Source§

impl Debug for BouncingSimulation

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl ScrollSimulation for BouncingSimulation

Source§

fn position(&self, t: Duration) -> f32

Where the content is at t, in logical pixels.
Source§

fn velocity(&self, t: Duration) -> f32

How fast the content is moving at t, in logical pixels per second.
Source§

fn is_done(&self, t: Duration) -> bool

Whether the content has come to rest at or before t.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.