ScrollComponent: Fix scrollbar position set to NaN when scroll range is empty - #188
Conversation
| // Run our scroll adjust event, normally updating [scrollBarGrip] | ||
| var percent = (innerPadding - horizontalOffset) / horizontalRange.width() | ||
| var percent = if (horizontalRange.width() == 0f) 0f else (innerPadding - horizontalOffset) / horizontalRange.width() | ||
| var percentageOfParent = width / actualWidth |
There was a problem hiding this comment.
Question / issue: actualWidth & actualHeight also seems like it can be 0, so theoretically percentageOfParent can also NaN?
I havent looked too deeply, and cant see any usages to tell if it'll cause a problem
There was a problem hiding this comment.
Hm, yeah, I believe it's used for the scrollbar size, so could be problematic too.
Re-visiting the scenario in which I ran into my issue, I realize that the problem actually wasn't that the scroller was empty, but merely that its size was matching the size of its content, so verticalRange was empty, while actualHeight was not.
Setting up a synthetic scenario with an empty scroller does confirm that actualHeight can also become 0. percentageOfParent thereby becomes Infinity though, not NaN. And I guess that kind of makes sense - or at least I wouldn't know how to reasonably fix it without a significant change to behavior - if you consider what happens as the content becomes smaller and smaller compared to the scroller: The percentageOfParent already grows and grows far beyond 1; it reaching Infinity as the content size reaches 0 is kind of what one should expect based on that behavior.
Looking at the scrollbar, that code seems to already clamp the percentageOfParent to 1 (ScrollComponent.kt:455), so I think percentageOfParent is actually fine to keep as is.
No description provided.