Skip to content

How to accurately scroll to a specific line in ScrollArea when using show_rows()? #5646

Closed Answered by unknownuser2503
unknownuser2503 asked this question in Q&A
Discussion options

You must be logged in to vote

Found a solution!

Looking into the show_rows() function, I figured out that the actual "height" for each line is:

let row_height_with_spacing = row_height + ui.spacing().item_spacing.y;

Then you can calculate the scroll offset for the line, target_line, as so:

let scroll_offset = (target_line as f32) * row_height_with_spacing - ui.spacing().item_spacing.y;

I want the target line to be displayed close to the top, so I find the total window height and subtract 20% of that to the scroll offset:

let window_rect = ctx.input(|i: &egui::InputState| i.screen_rect());
let window_height: f32 = window_rect.max[1] - window_rect.min[1];

my_scroll_area = my_scroll_area.vertical_scroll_offset(scroll_…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by unknownuser2503
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant