Skip to content

Commit

Permalink
Allow disabling animations on a ScrollArea
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasmerlin committed Apr 2, 2024
1 parent 3ee4890 commit 0041b59
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion crates/egui/src/containers/scroll_area.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,9 @@ pub struct ScrollArea {
/// end position until user manually changes position. It will become true
/// again once scroll handle makes contact with end.
stick_to_end: Vec2b,

/// If false, `scroll_to_*` functions will not be animated
animated: bool,
}

impl ScrollArea {
Expand Down Expand Up @@ -219,6 +222,7 @@ impl ScrollArea {
scrolling_enabled: true,
drag_to_scroll: true,
stick_to_end: Vec2b::FALSE,
animated: true,
}
}

Expand Down Expand Up @@ -393,6 +397,15 @@ impl ScrollArea {
self
}

/// Should the scroll area animate scroll_to_* functions?
///
/// Default: `true`.
#[inline]
pub fn animated(mut self, animated: bool) -> Self {
self.animated = animated;
self
}

/// Is any scrolling enabled?
pub(crate) fn is_any_scroll_enabled(&self) -> bool {
self.scroll_enabled[0] || self.scroll_enabled[1]
Expand Down Expand Up @@ -459,6 +472,7 @@ struct Prepared {

scrolling_enabled: bool,
stick_to_end: Vec2b,
animated: bool,
}

impl ScrollArea {
Expand All @@ -475,6 +489,7 @@ impl ScrollArea {
scrolling_enabled,
drag_to_scroll,
stick_to_end,
animated,
} = self;

let ctx = ui.ctx().clone();
Expand Down Expand Up @@ -650,6 +665,7 @@ impl ScrollArea {
viewport,
scrolling_enabled,
stick_to_end,
animated,
}
}

Expand Down Expand Up @@ -761,6 +777,7 @@ impl Prepared {
viewport: _,
scrolling_enabled,
stick_to_end,
animated,
} = self;

let content_size = content_ui.min_size();
Expand Down Expand Up @@ -804,7 +821,9 @@ impl Prepared {
if delta != 0.0 {
let target_offset = state.offset[d] + delta;

if let Some(animation) = &mut state.offset_target[d] {
if !animated {
state.offset[d] = target_offset;
} else if let Some(animation) = &mut state.offset_target[d] {
// For instance: the user is continuously calling `ui.scroll_to_cursor`,
// so we don't want to reset the animation, but perhaps update the target:
animation.target_offset = target_offset;
Expand Down

0 comments on commit 0041b59

Please sign in to comment.