Skip to main content

SwipeableState

Struct SwipeableState 

Source
pub struct SwipeableState<T: Clone + PartialEq + 'static> { /* private fields */ }
Expand description

A generic swipeable state that tracks an animated offset and snaps to the nearest anchor point on release.

§Type Parameters

  • T - The anchor value type (e.g. DismissValue, usize for pager pages).

§Usage

use repose_core::*;

let state = SwipeableState::new(
    vec![(0.0, "start"), (-200.0, "end")],
    SwipeableConfig::default(),
);

// Wire pointer events using on_pointer_down/move/up modifier callbacks:
// state.on_pointer_down(e.position.x)
// state.on_pointer_move(e.position.x)
// state.on_pointer_up()

// Read offset each frame:
let offset = state.offset();

Implementations§

Source§

impl<T: Clone + PartialEq + 'static> SwipeableState<T>

Source

pub fn new(anchors: Vec<(f32, T)>, config: SwipeableConfig) -> Self

Create a new swipeable state.

anchors - pairs of (offset_px, value) sorted by offset. The first anchor is the initial position. At least one anchor is required.

Source

pub fn offset(&self) -> f32

Current animated offset in pixels. Call every frame that needs the value. Requests a re-draw while the spring is still running.

Source

pub fn current_value(&self) -> T

Resolve the nearest anchor value from the current offset.

Source

pub fn snap_to(&self, off: f32)

Snap to an absolute offset instantly (used during active drag).

Source

pub fn animate_to(&self, value: &T)

Animate to the offset corresponding to a target value.

Source

pub fn is_animating(&self) -> bool

Returns true if the spring is still running.

Source

pub fn on_pointer_down(&self, axis_pos: f32)

Call from on_pointer_down with the position along the drag axis.

Source

pub fn on_pointer_move(&self, axis_pos: f32)

Call from on_pointer_move with the position along the drag axis.

Source

pub fn on_pointer_up(&self)

Call from on_pointer_up. Snaps to the appropriate anchor based on position and velocity.

Trait Implementations§

Source§

impl<T: Clone + PartialEq + 'static> Clone for SwipeableState<T>

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

Auto Trait Implementations§

§

impl<T> !RefUnwindSafe for SwipeableState<T>

§

impl<T> !Send for SwipeableState<T>

§

impl<T> !Sync for SwipeableState<T>

§

impl<T> !UnwindSafe for SwipeableState<T>

§

impl<T> Freeze for SwipeableState<T>

§

impl<T> Unpin for SwipeableState<T>

§

impl<T> UnsafeUnpin for SwipeableState<T>

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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.