Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make ScrollbarAdapter for LazyList take contentPadding into account. #365

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
igordmn marked this conversation as resolved.
Show resolved Hide resolved
+ 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