Skip to content

Commit 07d9877

Browse files
committed
Update thumb movement range by thumb size, in app scrollbars
Change-Id: I4438debd5037965a837758e0c78b110ab8197da5
1 parent b39f3f3 commit 07d9877

File tree

2 files changed

+23
-13
lines changed
  • core/designsystem/src/main/kotlin/com/google/samples/apps/nowinandroid/core/designsystem/component/scrollbar

2 files changed

+23
-13
lines changed

core/designsystem/src/main/kotlin/com/google/samples/apps/nowinandroid/core/designsystem/component/scrollbar/Scrollbar.kt

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,12 @@ class ScrollbarState {
9191
*/
9292
val thumbMovedPercent
9393
get() = unpackFloat2(packedValue)
94+
95+
/**
96+
* Returns the max distance the thumb can travel as a percentage of total track size
97+
*/
98+
val thumbTrackSizePercent
99+
get() = 1f - unpackFloat1(packedValue)
94100
}
95101

96102
/**
@@ -310,27 +316,30 @@ fun Scrollbar(
310316
b = minThumbSize.toPx(),
311317
)
312318

313-
val thumbTravelPercent = when {
314-
interactionThumbTravelPercent.isNaN() -> state.thumbMovedPercent
315-
else -> interactionThumbTravelPercent
319+
val trackSizePx = when (state.thumbTrackSizePercent) {
320+
0f -> track.size
321+
else -> (track.size - thumbSizePx) / state.thumbTrackSizePercent
316322
}
317323

318-
val thumbMovedPx = min(
319-
a = track.size * thumbTravelPercent,
320-
b = track.size - thumbSizePx,
324+
val thumbTravelPercent = max(
325+
a = min(
326+
a = when {
327+
interactionThumbTravelPercent.isNaN() -> state.thumbMovedPercent
328+
else -> interactionThumbTravelPercent
329+
},
330+
b = state.thumbTrackSizePercent,
331+
),
332+
b = 0f,
321333
)
322334

323-
val scrollbarThumbMovedPx = max(
324-
a = thumbMovedPx.roundToInt(),
325-
b = 0,
326-
)
335+
val thumbMovedPx = trackSizePx * thumbTravelPercent
327336

328337
val y = when (orientation) {
329338
Horizontal -> 0
330-
Vertical -> scrollbarThumbMovedPx
339+
Vertical -> thumbMovedPx.roundToInt()
331340
}
332341
val x = when (orientation) {
333-
Horizontal -> scrollbarThumbMovedPx
342+
Horizontal -> thumbMovedPx.roundToInt()
334343
Vertical -> 0
335344
}
336345

core/designsystem/src/main/kotlin/com/google/samples/apps/nowinandroid/core/designsystem/component/scrollbar/ThumbExt.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import androidx.compose.runtime.mutableFloatStateOf
2626
import androidx.compose.runtime.remember
2727
import androidx.compose.runtime.rememberUpdatedState
2828
import androidx.compose.runtime.setValue
29+
import kotlin.math.roundToInt
2930

3031
/**
3132
* Remembers a function to react to [Scrollbar] thumb position displacements for a [LazyListState]
@@ -79,7 +80,7 @@ private inline fun rememberDraggableScroller(
7980

8081
LaunchedEffect(percentage) {
8182
if (percentage.isNaN()) return@LaunchedEffect
82-
val indexToFind = (itemCount * percentage).toInt()
83+
val indexToFind = (itemCount * percentage).roundToInt()
8384
scroll(indexToFind)
8485
}
8586
return remember {

0 commit comments

Comments
 (0)