Skip to content

Commit

Permalink
Make ScrollbarAdapter for LazyList take contentPadding into account. (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
m-sasha authored and eymar committed Jan 13, 2023
1 parent 221ca51 commit 795359d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,11 @@ private class LazyScrollbarAdapter(
}

override fun maxScrollOffset(containerSize: Int) =
(averageItemSize * itemCount - containerSize).coerceAtLeast(0f)
(averageItemSize * itemCount
+ scrollState.layoutInfo.beforeContentPadding
+ scrollState.layoutInfo.afterContentPadding
- containerSize
).coerceAtLeast(0f)

private val itemCount get() = scrollState.layoutInfo.totalItemsCount

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import androidx.compose.foundation.gestures.ScrollConfig
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ColumnScope
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.size
Expand Down Expand Up @@ -286,6 +287,36 @@ class ScrollbarTest {
}
}

@Test
fun `scroll lazy column to bottom with content padding`() {
runBlocking(Dispatchers.Main) {
val listState = LazyListState()
rule.setContent {
LazyTestBox(
state = listState,
size = 100.dp,
childSize = 10.dp,
childCount = 20,
scrollbarWidth = 10.dp,
contentPadding = PaddingValues(vertical = 25.dp)
)
}
rule.awaitIdle()

// Drag to the bottom
rule.onNodeWithTag("scrollbar").performMouseInput {
instantDrag(start = Offset(0f, 20f), end = Offset(0f, 80f))
}

rule.awaitIdle()

// Note that if the scrolling is incorrect, this can fail not only with a wrong value, but also by not
// finding the box node, as it may have not scrolled into view.
// Last box should be at containerSize - bottomPadding - boxSize
rule.onNodeWithTag("box19").assertTopPositionInRootIsEqualTo(100.dp - 25.dp - 10.dp)
}
}

// TODO(demin): write a test when we support DesktopComposeTestRule.mainClock:
// see https://github.com/JetBrains/compose-jb/issues/637
// fun `move mouse to the slider and drag it`() {
Expand Down Expand Up @@ -662,12 +693,14 @@ class ScrollbarTest {
childSize: Dp,
childCount: Int,
scrollbarWidth: Dp,
reverseLayout: Boolean = false
contentPadding: PaddingValues = PaddingValues(0.dp),
reverseLayout: Boolean = false,
) = withTestEnvironment {
Box(Modifier.size(size)) {
LazyColumn(
Modifier.fillMaxSize().testTag("column"),
state,
contentPadding = contentPadding,
reverseLayout = reverseLayout
) {
items((0 until childCount).toList()) {
Expand Down

0 comments on commit 795359d

Please sign in to comment.