pub struct ScrollViewState { /* private fields */ }Expand description
Shared inertial scrolling state used by crate::ScrollView and crate::ListView.
Implementations§
Source§impl ScrollViewState
impl ScrollViewState
Sourcepub const fn is_dragging(&self) -> bool
pub const fn is_dragging(&self) -> bool
Returns whether a drag is currently active.
Sourcepub const fn is_scrolling(&self) -> bool
pub const fn is_scrolling(&self) -> bool
Returns whether drag slop has been crossed and scrolling is active.
Examples found in repository?
src/scroll_view/mod.rs (line 65)
64 pub const fn is_scrolling(&self) -> bool {
65 self.state.is_scrolling()
66 }
67
68 /// Returns the integer content offset.
69 pub fn content_offset_y(&self) -> i32 {
70 self.state.content_offset_y()
71 }
72
73 /// Starts a drag sequence.
74 pub fn begin_drag(&mut self, touch: TouchEvent) {
75 self.state.begin_drag(touch);
76 }
77
78 /// Updates dragging with a new touch sample.
79 pub fn drag(&mut self, touch: TouchEvent, content_height: u32, viewport_height: u32) -> bool {
80 let changed = self.state.drag(touch, content_height, viewport_height);
81 if changed || self.state.is_scrolling() {
82 self.reveal_indicator();
83 }
84 changed
85 }
86
87 /// Ends dragging and starts any fling animation.
88 pub fn end_drag(
89 &mut self,
90 touch: TouchEvent,
91 content_height: u32,
92 viewport_height: u32,
93 ) -> bool {
94 let changed = self.state.end_drag(touch, content_height, viewport_height);
95 if changed || self.state.is_animating(content_height, viewport_height) {
96 self.reveal_indicator();
97 }
98 changed
99 }
100
101 /// Advances animation and indicator fade state.
102 pub fn tick(&mut self, dt_ms: u32, content_height: u32, viewport_height: u32) -> bool {
103 let scrolled = self.state.tick(dt_ms, content_height, viewport_height);
104 if !self.shows_vertical_scroll_indicator || content_height <= viewport_height {
105 let indicator_changed = self.indicator_alpha != 0;
106 self.indicator_alpha = 0;
107 self.indicator_hold_ms = 0;
108 return scrolled || indicator_changed;
109 }
110
111 let was_alpha = self.indicator_alpha;
112 if self.state.is_scrolling() || self.state.is_animating(content_height, viewport_height) {
113 self.reveal_indicator();
114 } else if self.indicator_hold_ms > 0 {
115 self.indicator_hold_ms = self.indicator_hold_ms.saturating_sub(dt_ms);
116 } else if self.indicator_alpha > 0 {
117 self.indicator_alpha = self.indicator_alpha.saturating_sub(
118 (dt_ms.saturating_mul(u32::from(SCROLLBAR_FADE_PER_MS))).min(255) as u8,
119 );
120 }
121 scrolled || was_alpha != self.indicator_alpha
122 }Sourcepub fn content_offset_y(&self) -> i32
pub fn content_offset_y(&self) -> i32
Returns the rounded integer content offset.
Examples found in repository?
More examples
src/list.rs (line 79)
73 pub fn drag(&mut self, touch: TouchEvent, content_height: u32, viewport_height: u32) -> bool {
74 if !self.touch_active {
75 self.begin_drag(touch);
76 return false;
77 }
78
79 let before = self.content_offset_y();
80 let max = max_position_px(content_height, viewport_height);
81 let delta_y = touch.point.y as f32 - self.drag_anchor_y;
82 let delta_abs = delta_y.abs();
83 if !self.scrolling {
84 if delta_abs < DRAG_SLOP_PX {
85 return false;
86 }
87 self.scrolling = true;
88 }
89 self.tracker.add(touch);
90
91 let effective_delta_y = delta_y.signum() * (delta_abs - DRAG_SLOP_PX).max(0.0);
92 let raw_position = self.drag_anchor_position_px - effective_delta_y;
93 self.position_px = apply_rubber_band(raw_position, max, viewport_height as f32);
94 before != self.content_offset_y()
95 }
96
97 /// Ends the drag sequence and computes fling state.
98 pub fn end_drag(
99 &mut self,
100 touch: TouchEvent,
101 content_height: u32,
102 viewport_height: u32,
103 ) -> bool {
104 if !self.touch_active {
105 return false;
106 }
107
108 let changed = self.drag(touch, content_height, viewport_height);
109 self.touch_active = false;
110 let max = max_position_px(content_height, viewport_height);
111 self.velocity_px_per_s = if !self.scrolling || outside_bounds(self.position_px, max) {
112 0.0
113 } else {
114 release_velocity(self.tracker.velocity())
115 };
116 self.scrolling = false;
117 changed
118 }
119
120 /// Advances inertial scrolling and overscroll recovery.
121 pub fn tick(&mut self, dt_ms: u32, content_height: u32, viewport_height: u32) -> bool {
122 if self.touch_active || dt_ms == 0 {
123 return false;
124 }
125
126 let before = self.content_offset_y();
127 let dt = (dt_ms as f32 / 1000.0).min(MAX_DT_SECONDS);
128 let max = max_position_px(content_height, viewport_height);
129
130 if outside_bounds(self.position_px, max) {
131 self.snap_toward_bounds(dt, max);
132 } else if self.velocity_px_per_s.abs() >= MIN_VELOCITY {
133 let next = self.position_px + (self.velocity_px_per_s * dt);
134 if next < 0.0 {
135 self.position_px = 0.0;
136 self.velocity_px_per_s = 0.0;
137 } else if next > max {
138 self.position_px = max;
139 self.velocity_px_per_s = 0.0;
140 } else {
141 self.position_px = next;
142 self.velocity_px_per_s = apply_decay(self.velocity_px_per_s, dt_ms);
143 }
144 }
145
146 if self.velocity_px_per_s.abs() < MIN_VELOCITY {
147 self.velocity_px_per_s = 0.0;
148 }
149 before != self.content_offset_y()
150 }Sourcepub fn content_offset(&self) -> f32
pub fn content_offset(&self) -> f32
Returns the raw floating-point content offset.
Examples found in repository?
src/scroll_view/mod.rs (line 136)
125 pub fn scroll_bar(&self, viewport: Rectangle, content_height: u32) -> Option<ScrollBar> {
126 if !self.shows_vertical_scroll_indicator
127 || self.indicator_alpha == 0
128 || content_height <= viewport.size.height
129 {
130 return None;
131 }
132
133 scroll_bar_thumb(
134 viewport,
135 content_height,
136 self.state.content_offset(),
137 self.indicator_alpha,
138 )
139 }Sourcepub fn begin_drag(&mut self, touch: TouchEvent)
pub fn begin_drag(&mut self, touch: TouchEvent)
Starts a drag sequence.
Examples found in repository?
More examples
src/list.rs (line 75)
73 pub fn drag(&mut self, touch: TouchEvent, content_height: u32, viewport_height: u32) -> bool {
74 if !self.touch_active {
75 self.begin_drag(touch);
76 return false;
77 }
78
79 let before = self.content_offset_y();
80 let max = max_position_px(content_height, viewport_height);
81 let delta_y = touch.point.y as f32 - self.drag_anchor_y;
82 let delta_abs = delta_y.abs();
83 if !self.scrolling {
84 if delta_abs < DRAG_SLOP_PX {
85 return false;
86 }
87 self.scrolling = true;
88 }
89 self.tracker.add(touch);
90
91 let effective_delta_y = delta_y.signum() * (delta_abs - DRAG_SLOP_PX).max(0.0);
92 let raw_position = self.drag_anchor_position_px - effective_delta_y;
93 self.position_px = apply_rubber_band(raw_position, max, viewport_height as f32);
94 before != self.content_offset_y()
95 }Sourcepub fn drag(
&mut self,
touch: TouchEvent,
content_height: u32,
viewport_height: u32,
) -> bool
pub fn drag( &mut self, touch: TouchEvent, content_height: u32, viewport_height: u32, ) -> bool
Applies a drag update.
Examples found in repository?
More examples
src/list.rs (line 108)
98 pub fn end_drag(
99 &mut self,
100 touch: TouchEvent,
101 content_height: u32,
102 viewport_height: u32,
103 ) -> bool {
104 if !self.touch_active {
105 return false;
106 }
107
108 let changed = self.drag(touch, content_height, viewport_height);
109 self.touch_active = false;
110 let max = max_position_px(content_height, viewport_height);
111 self.velocity_px_per_s = if !self.scrolling || outside_bounds(self.position_px, max) {
112 0.0
113 } else {
114 release_velocity(self.tracker.velocity())
115 };
116 self.scrolling = false;
117 changed
118 }Sourcepub fn end_drag(
&mut self,
touch: TouchEvent,
content_height: u32,
viewport_height: u32,
) -> bool
pub fn end_drag( &mut self, touch: TouchEvent, content_height: u32, viewport_height: u32, ) -> bool
Ends the drag sequence and computes fling state.
Examples found in repository?
src/scroll_view/mod.rs (line 94)
88 pub fn end_drag(
89 &mut self,
90 touch: TouchEvent,
91 content_height: u32,
92 viewport_height: u32,
93 ) -> bool {
94 let changed = self.state.end_drag(touch, content_height, viewport_height);
95 if changed || self.state.is_animating(content_height, viewport_height) {
96 self.reveal_indicator();
97 }
98 changed
99 }Sourcepub fn tick(
&mut self,
dt_ms: u32,
content_height: u32,
viewport_height: u32,
) -> bool
pub fn tick( &mut self, dt_ms: u32, content_height: u32, viewport_height: u32, ) -> bool
Advances inertial scrolling and overscroll recovery.
Examples found in repository?
src/scroll_view/mod.rs (line 103)
102 pub fn tick(&mut self, dt_ms: u32, content_height: u32, viewport_height: u32) -> bool {
103 let scrolled = self.state.tick(dt_ms, content_height, viewport_height);
104 if !self.shows_vertical_scroll_indicator || content_height <= viewport_height {
105 let indicator_changed = self.indicator_alpha != 0;
106 self.indicator_alpha = 0;
107 self.indicator_hold_ms = 0;
108 return scrolled || indicator_changed;
109 }
110
111 let was_alpha = self.indicator_alpha;
112 if self.state.is_scrolling() || self.state.is_animating(content_height, viewport_height) {
113 self.reveal_indicator();
114 } else if self.indicator_hold_ms > 0 {
115 self.indicator_hold_ms = self.indicator_hold_ms.saturating_sub(dt_ms);
116 } else if self.indicator_alpha > 0 {
117 self.indicator_alpha = self.indicator_alpha.saturating_sub(
118 (dt_ms.saturating_mul(u32::from(SCROLLBAR_FADE_PER_MS))).min(255) as u8,
119 );
120 }
121 scrolled || was_alpha != self.indicator_alpha
122 }Sourcepub fn is_animating(&self, content_height: u32, viewport_height: u32) -> bool
pub fn is_animating(&self, content_height: u32, viewport_height: u32) -> bool
Returns whether the state is still animating after touch release.
Examples found in repository?
src/scroll_view/mod.rs (line 95)
88 pub fn end_drag(
89 &mut self,
90 touch: TouchEvent,
91 content_height: u32,
92 viewport_height: u32,
93 ) -> bool {
94 let changed = self.state.end_drag(touch, content_height, viewport_height);
95 if changed || self.state.is_animating(content_height, viewport_height) {
96 self.reveal_indicator();
97 }
98 changed
99 }
100
101 /// Advances animation and indicator fade state.
102 pub fn tick(&mut self, dt_ms: u32, content_height: u32, viewport_height: u32) -> bool {
103 let scrolled = self.state.tick(dt_ms, content_height, viewport_height);
104 if !self.shows_vertical_scroll_indicator || content_height <= viewport_height {
105 let indicator_changed = self.indicator_alpha != 0;
106 self.indicator_alpha = 0;
107 self.indicator_hold_ms = 0;
108 return scrolled || indicator_changed;
109 }
110
111 let was_alpha = self.indicator_alpha;
112 if self.state.is_scrolling() || self.state.is_animating(content_height, viewport_height) {
113 self.reveal_indicator();
114 } else if self.indicator_hold_ms > 0 {
115 self.indicator_hold_ms = self.indicator_hold_ms.saturating_sub(dt_ms);
116 } else if self.indicator_alpha > 0 {
117 self.indicator_alpha = self.indicator_alpha.saturating_sub(
118 (dt_ms.saturating_mul(u32::from(SCROLLBAR_FADE_PER_MS))).min(255) as u8,
119 );
120 }
121 scrolled || was_alpha != self.indicator_alpha
122 }Trait Implementations§
Source§impl Clone for ScrollViewState
impl Clone for ScrollViewState
Source§fn clone(&self) -> ScrollViewState
fn clone(&self) -> ScrollViewState
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for ScrollViewState
impl Debug for ScrollViewState
Source§impl Default for ScrollViewState
impl Default for ScrollViewState
Source§fn default() -> ScrollViewState
fn default() -> ScrollViewState
Returns the “default value” for a type. Read more
impl Copy for ScrollViewState
Auto Trait Implementations§
impl Freeze for ScrollViewState
impl RefUnwindSafe for ScrollViewState
impl Send for ScrollViewState
impl Sync for ScrollViewState
impl Unpin for ScrollViewState
impl UnsafeUnpin for ScrollViewState
impl UnwindSafe for ScrollViewState
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CheckedAs for T
impl<T> CheckedAs for T
Source§fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
Casts the value.
Source§impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
Source§fn checked_cast_from(src: Src) -> Option<Dst>
fn checked_cast_from(src: Src) -> Option<Dst>
Casts the value.
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> OverflowingAs for T
impl<T> OverflowingAs for T
Source§fn overflowing_as<Dst>(self) -> (Dst, bool)where
T: OverflowingCast<Dst>,
fn overflowing_as<Dst>(self) -> (Dst, bool)where
T: OverflowingCast<Dst>,
Casts the value.
Source§impl<Src, Dst> OverflowingCastFrom<Src> for Dstwhere
Src: OverflowingCast<Dst>,
impl<Src, Dst> OverflowingCastFrom<Src> for Dstwhere
Src: OverflowingCast<Dst>,
Source§fn overflowing_cast_from(src: Src) -> (Dst, bool)
fn overflowing_cast_from(src: Src) -> (Dst, bool)
Casts the value.
Source§impl<T> SaturatingAs for T
impl<T> SaturatingAs for T
Source§fn saturating_as<Dst>(self) -> Dstwhere
T: SaturatingCast<Dst>,
fn saturating_as<Dst>(self) -> Dstwhere
T: SaturatingCast<Dst>,
Casts the value.
Source§impl<Src, Dst> SaturatingCastFrom<Src> for Dstwhere
Src: SaturatingCast<Dst>,
impl<Src, Dst> SaturatingCastFrom<Src> for Dstwhere
Src: SaturatingCast<Dst>,
Source§fn saturating_cast_from(src: Src) -> Dst
fn saturating_cast_from(src: Src) -> Dst
Casts the value.
Source§impl<T> UnwrappedAs for T
impl<T> UnwrappedAs for T
Source§fn unwrapped_as<Dst>(self) -> Dstwhere
T: UnwrappedCast<Dst>,
fn unwrapped_as<Dst>(self) -> Dstwhere
T: UnwrappedCast<Dst>,
Casts the value.
Source§impl<Src, Dst> UnwrappedCastFrom<Src> for Dstwhere
Src: UnwrappedCast<Dst>,
impl<Src, Dst> UnwrappedCastFrom<Src> for Dstwhere
Src: UnwrappedCast<Dst>,
Source§fn unwrapped_cast_from(src: Src) -> Dst
fn unwrapped_cast_from(src: Src) -> Dst
Casts the value.
Source§impl<T> WrappingAs for T
impl<T> WrappingAs for T
Source§fn wrapping_as<Dst>(self) -> Dstwhere
T: WrappingCast<Dst>,
fn wrapping_as<Dst>(self) -> Dstwhere
T: WrappingCast<Dst>,
Casts the value.
Source§impl<Src, Dst> WrappingCastFrom<Src> for Dstwhere
Src: WrappingCast<Dst>,
impl<Src, Dst> WrappingCastFrom<Src> for Dstwhere
Src: WrappingCast<Dst>,
Source§fn wrapping_cast_from(src: Src) -> Dst
fn wrapping_cast_from(src: Src) -> Dst
Casts the value.