Skip to content

Commit

Permalink
Add scroll to percent (#237)
Browse files Browse the repository at this point in the history
* Add scroll to percent

* Rename method
  • Loading branch information
jrmoulton authored Jan 1, 2024
1 parent deb40f5 commit 533ca7d
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/views/scroll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ enum ScrollState {
EnsureVisible(Rect),
ScrollDelta(Vec2),
ScrollTo(Point),
ScrollToPercent(f32),
ScrollToView(Id),
HiddenBar(bool),
PropagatePointerWheel(bool),
Expand Down Expand Up @@ -157,6 +158,16 @@ impl Scroll {
self
}

/// Scroll the scroll view to a percent (0-100)
pub fn scroll_to_percent(self, percent: impl Fn() -> f32 + 'static) -> Self {
let id = self.id();
create_effect(move |_| {
let percent = percent() / 100.;
id.update_state(ScrollState::ScrollToPercent(percent), true);
});
self
}

pub fn scroll_to_view(self, view: impl Fn() -> Option<Id> + 'static) -> Self {
let id = self.id();
create_effect(move |_| {
Expand Down Expand Up @@ -614,6 +625,12 @@ impl View for Scroll {
ScrollState::ScrollTo(origin) => {
self.scroll_to(cx.app_state, origin);
}
ScrollState::ScrollToPercent(percent) => {
let mut child_size = self.child_size;
child_size *= percent as f64;
let point = child_size.to_vec2().to_point();
self.scroll_to(cx.app_state, point);
}
ScrollState::ScrollToView(id) => {
if cx.app_state.get_layout(id).is_some()
&& !cx.app_state.is_hidden_recursive(id)
Expand Down

0 comments on commit 533ca7d

Please sign in to comment.