Skip to content

Commit 3897be3

Browse files
lucasmerlinemilk
authored andcommitted
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
1 parent 5c8df02 commit 3897be3

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

crates/egui/src/containers/scroll_area.rs

+20-1
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,9 @@ pub struct ScrollArea {
176176
/// end position until user manually changes position. It will become true
177177
/// again once scroll handle makes contact with end.
178178
stick_to_end: Vec2b,
179+
180+
/// If false, `scroll_to_*` functions will not be animated
181+
animated: bool,
179182
}
180183

181184
impl ScrollArea {
@@ -219,6 +222,7 @@ impl ScrollArea {
219222
scrolling_enabled: true,
220223
drag_to_scroll: true,
221224
stick_to_end: Vec2b::FALSE,
225+
animated: true,
222226
}
223227
}
224228

@@ -383,6 +387,15 @@ impl ScrollArea {
383387
self
384388
}
385389

390+
/// Should the scroll area animate `scroll_to_*` functions?
391+
///
392+
/// Default: `true`.
393+
#[inline]
394+
pub fn animated(mut self, animated: bool) -> Self {
395+
self.animated = animated;
396+
self
397+
}
398+
386399
/// Is any scrolling enabled?
387400
pub(crate) fn is_any_scroll_enabled(&self) -> bool {
388401
self.scroll_enabled[0] || self.scroll_enabled[1]
@@ -449,6 +462,7 @@ struct Prepared {
449462

450463
scrolling_enabled: bool,
451464
stick_to_end: Vec2b,
465+
animated: bool,
452466
}
453467

454468
impl ScrollArea {
@@ -465,6 +479,7 @@ impl ScrollArea {
465479
scrolling_enabled,
466480
drag_to_scroll,
467481
stick_to_end,
482+
animated,
468483
} = self;
469484

470485
let ctx = ui.ctx().clone();
@@ -640,6 +655,7 @@ impl ScrollArea {
640655
viewport,
641656
scrolling_enabled,
642657
stick_to_end,
658+
animated,
643659
}
644660
}
645661

@@ -751,6 +767,7 @@ impl Prepared {
751767
viewport: _,
752768
scrolling_enabled,
753769
stick_to_end,
770+
animated,
754771
} = self;
755772

756773
let content_size = content_ui.min_size();
@@ -794,7 +811,9 @@ impl Prepared {
794811
if delta != 0.0 {
795812
let target_offset = state.offset[d] + delta;
796813

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

0 commit comments

Comments
 (0)