summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorlucasmerlin <lucasmeurer96@gmail.com>2024-04-02 17:53:54 +0200
committerEmil Ernerfeldt <emil.ernerfeldt@gmail.com>2024-04-02 17:54:26 +0200
commit3897be3774a76efba43be10f466757f8970e403a (patch)
treeff9a6807b48a82366a663746c444e807c71659e3
parent5c8df022b7098825bef327d03f76ebd68e5d022f (diff)
Allow disabling animations on a ScrollArea (#4309)
<!-- Please read the "Making a PR" section of [`CONTRIBUTING.md`](https://github.com/emilk/egui/blob/master/CONTRIBUTING.md) before opening a Pull Request! * Keep your PR:s small and focused. * The PR title is what ends up in the changelog, so make it descriptive! * If applicable, add a screenshot or gif. * If it is a non-trivial addition, consider adding a demo for it to `egui_demo_lib`, or a new example. * Do NOT open PR:s from your `master` branch, as that makes it hard for maintainers to add commits to your PR. * Remember to run `cargo fmt` and `cargo cranky`. * Open the PR as a draft until you have self-reviewed it and run `./scripts/check.sh`. * When you have addressed a PR comment, mark it as resolved. Please be patient! I will review your PR, but my time is limited! --> Adds a flag to ScrollArea that disables animations for the scroll_to_* functions
-rw-r--r--crates/egui/src/containers/scroll_area.rs21
1 files changed, 20 insertions, 1 deletions
diff --git a/crates/egui/src/containers/scroll_area.rs b/crates/egui/src/containers/scroll_area.rs
index c35cca34..2ad4e72b 100644
--- a/crates/egui/src/containers/scroll_area.rs
+++ b/crates/egui/src/containers/scroll_area.rs
@@ -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 {
@@ -219,6 +222,7 @@ impl ScrollArea {
scrolling_enabled: true,
drag_to_scroll: true,
stick_to_end: Vec2b::FALSE,
+ animated: true,
}
}
@@ -383,6 +387,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]
@@ -449,6 +462,7 @@ struct Prepared {
scrolling_enabled: bool,
stick_to_end: Vec2b,
+ animated: bool,
}
impl ScrollArea {
@@ -465,6 +479,7 @@ impl ScrollArea {
scrolling_enabled,
drag_to_scroll,
stick_to_end,
+ animated,
} = self;
let ctx = ui.ctx().clone();
@@ -640,6 +655,7 @@ impl ScrollArea {
viewport,
scrolling_enabled,
stick_to_end,
+ animated,
}
}
@@ -751,6 +767,7 @@ impl Prepared {
viewport: _,
scrolling_enabled,
stick_to_end,
+ animated,
} = self;
let content_size = content_ui.min_size();
@@ -794,7 +811,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;