Merge pull request #1080 from oikvpqya/fix-scrollbar-thumb

Fix #1079: Update thumb movement range by thumb size, in app scrollbars
pull/1084/head
Adetunji Dahunsi 1 year ago committed by GitHub
commit 63bba2ede7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -91,6 +91,12 @@ class ScrollbarState {
*/
val thumbMovedPercent
get() = unpackFloat2(packedValue)
/**
* Returns the max distance the thumb can travel as a percentage of total track size
*/
val thumbTrackSizePercent
get() = 1f - thumbSizePercent
}
/**
@ -310,27 +316,30 @@ fun Scrollbar(
b = minThumbSize.toPx(),
)
val thumbTravelPercent = when {
interactionThumbTravelPercent.isNaN() -> state.thumbMovedPercent
else -> interactionThumbTravelPercent
val trackSizePx = when (state.thumbTrackSizePercent) {
0f -> track.size
else -> (track.size - thumbSizePx) / state.thumbTrackSizePercent
}
val thumbMovedPx = min(
a = track.size * thumbTravelPercent,
b = track.size - thumbSizePx,
val thumbTravelPercent = max(
a = min(
a = when {
interactionThumbTravelPercent.isNaN() -> state.thumbMovedPercent
else -> interactionThumbTravelPercent
},
b = state.thumbTrackSizePercent,
),
b = 0f,
)
val scrollbarThumbMovedPx = max(
a = thumbMovedPx.roundToInt(),
b = 0,
)
val thumbMovedPx = trackSizePx * thumbTravelPercent
val y = when (orientation) {
Horizontal -> 0
Vertical -> scrollbarThumbMovedPx
Vertical -> thumbMovedPx.roundToInt()
}
val x = when (orientation) {
Horizontal -> scrollbarThumbMovedPx
Horizontal -> thumbMovedPx.roundToInt()
Vertical -> 0
}

@ -26,6 +26,7 @@ import androidx.compose.runtime.mutableFloatStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberUpdatedState
import androidx.compose.runtime.setValue
import kotlin.math.roundToInt
/**
* Remembers a function to react to [Scrollbar] thumb position displacements for a [LazyListState]
@ -79,7 +80,7 @@ private inline fun rememberDraggableScroller(
LaunchedEffect(percentage) {
if (percentage.isNaN()) return@LaunchedEffect
val indexToFind = (itemCount * percentage).toInt()
val indexToFind = (itemCount * percentage).roundToInt()
scroll(indexToFind)
}
return remember {

Loading…
Cancel
Save