How to accurately scroll to a specific line in ScrollArea when using show_rows()? #5646
-
ProblemI've been trying to implement search functionality in egui by allowing the user to scroll to a specific line in a Things I've triedThe only approach I've gotten to at least scroll in a sensible way is using
With this approach however, the scroll is not accurate. It is often close if there aren't a lot of lines being shown, but when there are a lot of lines it is no where near accurate enough. My first instinct was the row height being incorrect, so first I tried setting the scroll_offset to the following:
That didn't work, so I tested it on some repeating text to see how far off the distance scrolled was. Then I tried doing
and adjusted the multiplier until the distance scrolled was perfect, and I got multiplier=1.662. This however, doesn't work when just testing it on some random text with a bunch of lines. It's often close if there aren't a lot of lines, but when testing on a thousands of lines, it's no where close enough. I've thought it might have something to do with lines wrapping and thus taking the space of 2 lines. Testing this on a big screen without any lines wrapping, that doesn't seem to solve the problem, which to me suggests that isn't a problem? I've also tried to use some of the Why is this approach not working, and is there a solution to the problem? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Found a solution! Looking into the
Then you can calculate the scroll offset for the line,
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:
|
Beta Was this translation helpful? Give feedback.
Found a solution!
Looking into the
show_rows()
function, I figured out that the actual "height" for each line is:Then you can calculate the scroll offset for the line,
target_line
, as so: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: