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

Conversation

juancampa
Copy link
Contributor

@juancampa juancampa commented Feb 13, 2022

Problem

response.scroll_to_me works great. However, in some cases, you only want to bring a UI into view if it's not visible, and don't do anything if it's already visible.

Solution

Accept an Option<Align> instead of just Align. If None is passed, the minimum scrolling necessary to fully bring the ui into view will be applied.

🎥

Peek.2022-02-13.09-49.mp4

@@ -40,7 +42,7 @@ impl Default for FrameState {
used_by_panels: Rect::NAN,
tooltip_rect: None,
scroll_delta: Vec2::ZERO,
scroll_target: [None; 2],
scroll_target: [None, None],
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Had to change this because RangeInclusive is not Copy.

Copy link
Owner

@emilk emilk left a comment

Choose a reason for hiding this comment

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

Very nice! This will be useful for #165

egui/src/containers/scroll_area.rs Outdated Show resolved Hide resolved
/// }
/// });
/// # });
/// ```
pub fn scroll_to_cursor(&mut self, align: Align) {
pub fn scroll_to_cursor(&mut self, align: Option<Align>) {
Copy link
Owner

Choose a reason for hiding this comment

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

This needs documentation for what align=None means

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added docs!

@@ -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 ☝️

@emilk emilk merged commit 635c657 into emilk:master Feb 15, 2022
@emilk
Copy link
Owner

emilk commented Feb 15, 2022

Thanks!

@juancampa juancampa deleted the allow-scroll-into-view branch February 16, 2022 00:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants