Skip to content

Commit d08de58

Browse files
committed
fix: improve scrollbar container layout and scrolling logic
- Wrap content in a `Box` using the provided modifier when the scrollbar is disabled to ensure consistent layout constraints. - Refine scroll position calculation by using `maxScrollOffset` and coercing the scroll fraction between 0 and 1. - Update grid scroll logic to calculate `targetIndex` based on `totalItemsCount - 1` to prevent index out-of-bounds and improve accuracy. - Add validation to skip scroll updates if the maximum offset is non-positive.
1 parent 00e5fa6 commit d08de58

1 file changed

Lines changed: 12 additions & 5 deletions

File tree

core/presentation/src/jvmMain/kotlin/zed/rainxch/core/presentation/components/ScrollbarContainer.jvm.kt

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ actual fun ScrollbarContainer(
2525
content: @Composable () -> Unit,
2626
) {
2727
if (!enabled) {
28-
content()
28+
Box(modifier = modifier) {
29+
content()
30+
}
2931
return
3032
}
3133
Box(modifier = modifier.padding(start = 8.dp)) {
@@ -55,7 +57,10 @@ actual fun ScrollbarContainer(
5557
content: @Composable () -> Unit,
5658
) {
5759
if (!enabled) {
58-
content()
60+
Box(modifier = modifier) {
61+
content()
62+
}
63+
5964
return
6065
}
6166
Box(modifier = modifier.padding(start = 8.dp)) {
@@ -102,10 +107,12 @@ private class StaggeredGridScrollbarAdapter(
102107
) {
103108
val totalContent = estimatedContentSize()
104109
val layoutInfo = gridState.layoutInfo
105-
if (layoutInfo.totalItemsCount == 0 || totalContent <= 0f) return
106-
val fraction = scrollOffset / totalContent
110+
val maxOffset = maxScrollOffset(containerSize)
111+
if (layoutInfo.totalItemsCount == 0 || totalContent <= 0f || maxOffset <= 0f) return
112+
val fraction = (scrollOffset / maxOffset).coerceIn(0f, 1f)
113+
107114
val targetIndex =
108-
(fraction * layoutInfo.totalItemsCount)
115+
(fraction * (layoutInfo.totalItemsCount - 1))
109116
.toInt()
110117
.coerceIn(0, maxOf(layoutInfo.totalItemsCount - 1, 0))
111118
gridState.scrollToItem(targetIndex)

0 commit comments

Comments
 (0)