Skip to main content

Spring

Struct Spring 

Source
pub struct Spring {
    pub stiffness: f32,
    pub damping: f32,
    pub mass: f32,
    pub velocity: Option<f32>,
    pub frame_rate: Option<f32>,
}
Expand description

How a spring moves (TS’s defaults: stiffness 170, damping 26, mass 1): an animated value’s (AnimatedValue::spring) and a layout animation’s (LayoutTransition::spring).

Made as gpui-base’s Spring is, from how long it takes and how much it overshoots (Spring::new, Spring::damping_ratio), or from a stiffness, as Material 3’s motion tokens give it (Spring::from_stiffness); its fields are the physics either way.

use nsh_core::element::Spring;

// `gpui-component`'s `spring_move`: a 280 ms response, a little springy.
let spring = Spring::new(280.0).damping_ratio(0.85);
assert!((spring.settle_ms() - 362.2).abs() < 0.1);
// Material 3 Expressive's fast spatial spring.
let spring = Spring::from_stiffness(800.0).damping_ratio(0.6);
assert!((spring.settle_ms() - 407.0).abs() < 0.1);

Fields§

§stiffness: f32

Stiffness k.

§damping: f32

Damping coefficient c (not a ratio: the ratio is c / (2 √(k m)), Spring::damping_ratio sets it).

§mass: f32

Mass m.

§velocity: Option<f32>

The velocity it starts with (per second); None keeps the current one.

§frame_rate: Option<f32>

The highest frame rate it needs, in Hz (TS’s frameRate); None: the screen’s highest (Driver::frame_rate).

Implementations§

Source§

impl Spring

Source

pub fn new(response_ms: f32) -> Self

gpui-base’s Spring::new(response): a mass of 1 that would swing once in response_ms undamped, critically damped (no overshoot). The response is the scale the motion is felt at, not when it stops (Spring::settle_ms).

Source

pub fn from_stiffness(stiffness: f32) -> Self

A mass of 1 on a spring of stiffness, critically damped: a design system’s stiffness token, its damping ratio set with Spring::damping_ratio.

Source

pub fn damping_ratio(self, ratio: f32) -> Self

Damped at ratio (gpui-base’s with_damping): 1 does not overshoot, below 1 it passes its target and comes back, above 1 it approaches slowly. Sets Spring::damping from the stiffness and mass it has, so set those first.

Source

pub fn progress(self, ms: f32) -> f32

How far it has gone, from 0 to 1, ms milliseconds after it was released at rest: the damped oscillator’s closed form, past 1 when it is underdamped. A spring that never settles (no damping, or a stiffness or mass that is not positive) is at 1 at once.

Source

pub fn settle_ms(self) -> f32

How long it takes, in milliseconds, released at rest, to stay within a thousandth of its travel: how long a layout animation on it lasts. 0 for a spring that never settles.

Source

pub const fn frame_rate(self, hz: f32) -> Self

Needing at most hz frames per second (a small, slow spring: 60).

Trait Implementations§

Source§

impl Clone for Spring

Source§

fn clone(&self) -> Self

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 Spring

Source§

impl Debug for Spring

Source§

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

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

impl Default for Spring

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl JsValue for Spring

A spring a component’s prop takes (TS { stiffness?: number; damping?: number; mass?: number; velocity?: number; frameRate?: number }): the options of AnimatedValue.spring in nsh/animated, with its defaults.

Source§

fn ts_type(_types: &mut TsTypes) -> String

Its TypeScript type (string, AlertButton), declaring the named types it uses in types.
Source§

fn from_js(value: JsIn<'_>) -> Result<Self, PropError>

The value of JS value value.
Source§

fn to_js<'a>(&self, host: &'a dyn JsHost) -> Result<JsOwned<'a>, PropError>

The value as a JS value of host.
Source§

impl PartialEq for Spring

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Spring

Auto Trait Implementations§

§

impl Forget for Spring

§

impl Freeze for Spring

§

impl Move for Spring

§

impl RefUnwindSafe for Spring

§

impl Send for Spring

§

impl Sync for Spring

§

impl Unpin for Spring

§

impl UnsafeUnpin for Spring

§

impl UnwindSafe for Spring

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> Arg for T
where T: JsValue,

Source§

fn ts_type(types: &mut TsTypes) -> String

Its TypeScript type.
Source§

fn from_js(value: JsIn<'_>) -> Result<T, PropError>

The value of a JS argument.
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> CallbackArgs for T
where T: Ret,

Source§

const ARITY: usize = const ARITY: usize = if T::UNIT { 0 } else { 1 };

How many there are.
Source§

fn ts_params(types: &mut TsTypes, names: &[&str]) -> String

Its TypeScript parameters, named names (name: string, amount: number), or without names value for one and a, b, … for several.
Source§

fn into_js_args<'a>( self, host: &'a dyn JsHost, ) -> Result<Vec<JsOwned<'a>>, PropError>

The arguments as JS values.
Source§

fn classes(out: &mut Vec<&'static ClassDef>)

Source§

impl<T> CallbackRet for T
where T: JsValue,

Source§

fn ts_type(types: &mut TsTypes) -> String

Its TypeScript type.
Source§

fn from_js(value: JsIn<'_>) -> Result<T, PropError>

The value the function returned.
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> Ret for T
where T: JsValue,

Source§

fn ts_type(types: &mut TsTypes) -> String

Its TypeScript type.
Source§

fn into_js<'a>(self, host: &'a dyn JsHost) -> Result<JsOwned<'a>, PropError>

The value for JS.
Source§

const UNIT: bool = false

(): nothing (a callback taking it has no parameters).
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToJs for T
where T: Ret + 'static,

Source§

fn to_js_value<'a>( self: Box<T>, host: &'a dyn JsHost, ) -> Result<JsOwned<'a>, PropError>

The value as a JS value of host.
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.