Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow scroll into view without specifying an alignment #1247

Merged
merged 9 commits into from Feb 15, 2022
7 changes: 5 additions & 2 deletions egui/src/containers/scroll_area.rs
Expand Up @@ -493,14 +493,17 @@ impl Prepared {
let clip_rect = content_ui.clip_rect();
let visible_range = min..=min + clip_rect.size()[d];

// if the ui is too big to completely fit in the scroll view, align it to the left/top
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this the best behavior?

Another alternative is to scroll the minimum amount that makes the scroll view fill with the ui-range. So for instance, if we want to scroll to an image, and that image is very big then we either:

  • scroll so that the top of the image is at the top of the scroll view
  • scroll so that the bottom of the image is at the bottom of the scroll view (if that is closer)
  • don't scroll at all, if all we are currently seeing is the image

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I like the simmetry. I fixed it to have that behavior ☝️

let too_big = (scroll.end() - scroll.start()) > clip_rect.size()[d];

let center_factor = if let Some(align) = align {
align.to_factor()
} else if *scroll.start() < clip_rect.min[d] {
} else if too_big || *scroll.start() < clip_rect.min[d] {
0.0
} else if *scroll.end() > clip_rect.max[d] {
1.0
} else {
// Ui os already in view, no need to adjust scroll offset.
// Ui is already in view, no need to adjust scroll.
continue;
};

Expand Down