Skip to content

Commit

Permalink
Add max thumb length. This allows to also set a static thumb length b…
Browse files Browse the repository at this point in the history
…y setting the min and max thumb length the same value.
  • Loading branch information
nanihadesuka committed Jun 30, 2024
1 parent 5406b96 commit 994a35e
Show file tree
Hide file tree
Showing 21 changed files with 165 additions and 10 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ android {
applicationId = "my.nanihadesuka.lazycolumnscrollbar"
minSdk = 21
targetSdk = 34
versionCode = 8
versionName = "2.0.0"
versionCode = 9
versionName = "2.1.0"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ fun LazyHorizontalGridView() {

@Composable
fun ColumnView() {
val listData = (0..100).toList()
val listData = (0..18).toList()
val listState = rememberScrollState()
val indicatorContent = @Composable { normalizedOffset: Float, isThumbSelected: Boolean ->
Indicator(
Expand Down
2 changes: 1 addition & 1 deletion lib/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {
}

object MySettings {
val versionName: String = "2.0.7"
val versionName: String = "2.1.0"
val namespace = "my.nanihadesuka.lazycolumnscrollbar"
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ fun InternalColumnScrollbar(
state = state,
visibleLengthDp = visibleLengthDp,
thumbMinLength = settings.thumbMinLength,
thumbMaxLength = settings.thumbMaxLength,
alwaysShowScrollBar = settings.alwaysShowScrollbar,
selectionMode = settings.selectionMode
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ fun InternalLazyColumnScrollbar(
val controller = rememberLazyListStateController(
state = state,
thumbMinLength = settings.thumbMinLength,
thumbMaxLength = settings.thumbMaxLength,
alwaysShowScrollBar = settings.alwaysShowScrollbar,
selectionMode = settings.selectionMode
selectionMode = settings.selectionMode,
)

ElementScrollbar(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ fun InternalLazyHorizontalGridScrollbar(
val controller = rememberLazyGridStateController(
state = state,
thumbMinLength = settings.thumbMinLength,
thumbMaxLength = settings.thumbMaxLength,
alwaysShowScrollBar = settings.alwaysShowScrollbar,
selectionMode = settings.selectionMode,
orientation = Orientation.Horizontal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ fun InternalLazyRowScrollbar(
val controller = rememberLazyListStateController(
state = state,
thumbMinLength = settings.thumbMinLength,
thumbMaxLength = settings.thumbMaxLength,
alwaysShowScrollBar = settings.alwaysShowScrollbar,
selectionMode = settings.selectionMode,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ fun InternalLazyVerticalGridScrollbar(
val controller = rememberLazyGridStateController(
state = state,
thumbMinLength = settings.thumbMinLength,
thumbMaxLength = settings.thumbMaxLength,
alwaysShowScrollBar = settings.alwaysShowScrollbar,
selectionMode = settings.selectionMode,
orientation = Orientation.Vertical
Expand Down
1 change: 1 addition & 0 deletions lib/src/main/java/my/nanihadesuka/compose/RowScrollbar.kt
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ fun InternalRowScrollbar(
state = state,
visibleLengthDp = visibleLengthDp,
thumbMinLength = settings.thumbMinLength,
thumbMaxLength = settings.thumbMaxLength,
alwaysShowScrollBar = settings.alwaysShowScrollbar,
selectionMode = settings.selectionMode,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ data class ScrollbarSettings(
val thumbThickness: Dp = Default.thumbThickness,
val thumbShape: Shape = Default.thumbShape,
val thumbMinLength: Float = Default.thumbMinLength,
val thumbMaxLength: Float = Default.thumbMaxLength,
val thumbUnselectedColor: Color = Default.thumbUnselectedColor,
val thumbSelectedColor: Color = Default.thumbSelectedColor,
val selectionMode: ScrollbarSelectionMode = Default.selectionMode,
Expand All @@ -30,6 +31,12 @@ data class ScrollbarSettings(
val hideEasingAnimation: Easing = Default.hideEasingAnimation,
val durationAnimationMillis: Int = Default.durationAnimationMillis,
) {
init {
require(thumbMinLength <= thumbMaxLength) {
"thumbMinLength ($thumbMinLength) must be less or equal to thumbMaxLength ($thumbMaxLength)"
}
}

companion object {
val Default = ScrollbarSettings(
enabled = true,
Expand All @@ -38,6 +45,7 @@ data class ScrollbarSettings(
thumbThickness = 6.dp,
scrollbarPadding = 8.dp,
thumbMinLength = 0.1f,
thumbMaxLength = 1.0f,
thumbUnselectedColor = Color(0xFF2A59B6),
thumbSelectedColor = Color(0xFF5281CA),
thumbShape = CircleShape,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ import kotlin.math.floor
internal fun rememberLazyGridStateController(
state: LazyGridState,
thumbMinLength: Float,
thumbMaxLength: Float,
alwaysShowScrollBar: Boolean,
selectionMode: ScrollbarSelectionMode,
orientation: Orientation
): LazyGridStateController {
val coroutineScope = rememberCoroutineScope()

val thumbMinLengthUpdated = rememberUpdatedState(thumbMinLength)
val thumbMaxLengthUpdated = rememberUpdatedState(thumbMaxLength)
val alwaysShowScrollBarUpdated = rememberUpdatedState(alwaysShowScrollBar)
val selectionModeUpdated = rememberUpdatedState(selectionMode)
val orientationUpdated = rememberUpdatedState(orientation)
Expand Down Expand Up @@ -114,7 +116,10 @@ internal fun rememberLazyGridStateController(

val thumbSizeNormalized = remember {
derivedStateOf {
thumbSizeNormalizedReal.value.coerceAtLeast(thumbMinLengthUpdated.value)
thumbSizeNormalizedReal.value.coerceIn(
thumbMinLengthUpdated.value,
thumbMaxLengthUpdated.value,
)
}
}

Expand Down Expand Up @@ -142,7 +147,9 @@ internal fun rememberLazyGridStateController(

val firstItem = realFirstVisibleItem.value ?: return@let 0f
val top = firstItem.run {
ceil(index.toFloat() / nElementsMainAxis.value.toFloat()) + fractionHiddenTop(state.firstVisibleItemScrollOffset)
ceil(index.toFloat() / nElementsMainAxis.value.toFloat()) + fractionHiddenTop(
state.firstVisibleItemScrollOffset
)
} / ceil(it.totalItemsCount.toFloat() / nElementsMainAxis.value.toFloat())
offsetCorrection(top)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ import kotlin.math.floor
internal fun rememberLazyListStateController(
state: LazyListState,
thumbMinLength: Float,
thumbMaxLength: Float,
alwaysShowScrollBar: Boolean,
selectionMode: ScrollbarSelectionMode
): LazyListStateController {
val coroutineScope = rememberCoroutineScope()

val thumbMinLengthUpdated = rememberUpdatedState(thumbMinLength)
val thumbMaxLengthUpdated = rememberUpdatedState(thumbMaxLength)
val alwaysShowScrollBarUpdated = rememberUpdatedState(alwaysShowScrollBar)
val selectionModeUpdated = rememberUpdatedState(selectionMode)
val reverseLayout = remember { derivedStateOf { state.layoutInfo.reverseLayout } }
Expand Down Expand Up @@ -80,7 +82,10 @@ internal fun rememberLazyListStateController(

val thumbSizeNormalized = remember {
derivedStateOf {
thumbSizeNormalizedReal.value.coerceAtLeast(thumbMinLengthUpdated.value)
thumbSizeNormalizedReal.value.coerceIn(
thumbMinLengthUpdated.value,
thumbMaxLengthUpdated.value,
)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ internal fun rememberScrollStateController(
state: ScrollState,
visibleLengthDp: Dp,
thumbMinLength: Float,
thumbMaxLength: Float,
alwaysShowScrollBar: Boolean,
selectionMode: ScrollbarSelectionMode
): ScrollStateController {
val coroutineScope = rememberCoroutineScope()

val visibleLengthDpUpdated = rememberUpdatedState(visibleLengthDp)
val thumbMinLengthUpdated = rememberUpdatedState(thumbMinLength)
val thumbMaxLengthUpdated = rememberUpdatedState(thumbMaxLength)
val alwaysShowScrollBarUpdated = rememberUpdatedState(alwaysShowScrollBar)
val selectionModeUpdated = rememberUpdatedState(selectionMode)

Expand All @@ -54,7 +56,10 @@ internal fun rememberScrollStateController(

val thumbSizeNormalized = remember {
derivedStateOf {
thumbSizeNormalizedReal.value.coerceAtLeast(thumbMinLengthUpdated.value)
thumbSizeNormalizedReal.value.coerceIn(
thumbMinLengthUpdated.value,
thumbMaxLengthUpdated.value,
)
}
}

Expand Down
12 changes: 12 additions & 0 deletions lib/src/test/java/my/nanihadesuka/compose/ColumnScrollbarTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,16 @@ class ColumnScrollbarTest(private val itemCount: Int) {
}
}

@Test
fun `scrollbar thumb exact height`() {
setContent(thumbMinLength = 0.4f, thumbMaxLength = 0.4f)
scrollbarScreen(composeRule) {
assert {
hasThumbHeight(value = 0.4f)
}
}
}

@Test
fun `scrollbar thumb min height`() {
setContent(thumbMinLength = 0.2f)
Expand Down Expand Up @@ -367,6 +377,7 @@ class ColumnScrollbarTest(private val itemCount: Int) {
thickness: Dp = 6.dp,
padding: Dp = 8.dp,
thumbMinLength: Float = 0.1f,
thumbMaxLength: Float = 1f,
thumbUnselectedColor: Color = Color(0xFF2A59B6),
thumbSelectedColor: Color = Color(0xFF5281CA),
thumbShape: Shape = CircleShape,
Expand All @@ -386,6 +397,7 @@ class ColumnScrollbarTest(private val itemCount: Int) {
scrollbarPadding = padding,
thumbThickness = thickness,
thumbMinLength = thumbMinLength,
thumbMaxLength = thumbMaxLength,
thumbUnselectedColor = thumbUnselectedColor,
thumbSelectedColor = thumbSelectedColor,
thumbShape = thumbShape,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,16 @@ class LazyColumnScrollbarTest(private val itemCount: Int) {
}
}

@Test
fun `scrollbar thumb exact height`() {
setContent(thumbMinLength = 0.4f, thumbMaxLength = 0.4f)
scrollbarScreen(composeRule) {
assert {
hasThumbHeight(value = 0.4f)
}
}
}

@Test
fun `scrollbar thumb min height`() {
setContent(thumbMinLength = 0.2f)
Expand Down Expand Up @@ -429,6 +439,7 @@ class LazyColumnScrollbarTest(private val itemCount: Int) {
thickness: Dp = 6.dp,
padding: Dp = 8.dp,
thumbMinLength: Float = 0.1f,
thumbMaxLength: Float = 1f,
thumbColor: Color = Color(0xFF2A59B6),
thumbSelectedColor: Color = Color(0xFF5281CA),
thumbShape: Shape = CircleShape,
Expand All @@ -449,6 +460,7 @@ class LazyColumnScrollbarTest(private val itemCount: Int) {
scrollbarPadding = padding,
thumbThickness = thickness,
thumbMinLength = thumbMinLength,
thumbMaxLength = thumbMaxLength,
thumbUnselectedColor = thumbColor,
thumbSelectedColor = thumbSelectedColor,
thumbShape = thumbShape,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,16 @@ class LazyHorizontalGridScrollbarTest(private val itemCount: Int) {
}
}

@Test
fun `scrollbar thumb exact width`() {
setContent(thumbMinLength = 0.4f, thumbMaxLength = 0.4f)
scrollbarScreen(composeRule) {
assert {
hasThumbWidth(value = 0.4f)
}
}
}

@Test
fun `scrollbar thumb min width`() {
setContent(thumbMinLength = 0.2f)
Expand Down Expand Up @@ -430,6 +440,7 @@ class LazyHorizontalGridScrollbarTest(private val itemCount: Int) {
thickness: Dp = 6.dp,
padding: Dp = 8.dp,
thumbMinLength: Float = 0.1f,
thumbMaxLength: Float = 1f,
thumbColor: Color = Color(0xFF2A59B6),
thumbSelectedColor: Color = Color(0xFF5281CA),
thumbShape: Shape = CircleShape,
Expand All @@ -450,6 +461,7 @@ class LazyHorizontalGridScrollbarTest(private val itemCount: Int) {
scrollbarPadding = padding,
thumbThickness = thickness,
thumbMinLength = thumbMinLength,
thumbMaxLength = thumbMaxLength,
thumbUnselectedColor = thumbColor,
thumbSelectedColor = thumbSelectedColor,
thumbShape = thumbShape,
Expand Down
12 changes: 12 additions & 0 deletions lib/src/test/java/my/nanihadesuka/compose/LazyRowScrollbarTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,16 @@ class LazyRowScrollbarTest(private val itemCount: Int) {
}
}

@Test
fun `scrollbar thumb exact width`() {
setContent(thumbMinLength = 0.4f, thumbMaxLength = 0.4f)
scrollbarScreen(composeRule) {
assert {
hasThumbWidth(value = 0.4f)
}
}
}

@Test
fun `scrollbar thumb min width`() {
setContent(thumbMinLength = 0.2f)
Expand Down Expand Up @@ -427,6 +437,7 @@ class LazyRowScrollbarTest(private val itemCount: Int) {
thickness: Dp = 6.dp,
padding: Dp = 8.dp,
thumbMinLength: Float = 0.1f,
thumbMaxLength: Float = 1f,
thumbColor: Color = Color(0xFF2A59B6),
thumbSelectedColor: Color = Color(0xFF5281CA),
thumbShape: Shape = CircleShape,
Expand All @@ -447,6 +458,7 @@ class LazyRowScrollbarTest(private val itemCount: Int) {
scrollbarPadding = padding,
thumbThickness = thickness,
thumbMinLength = thumbMinLength,
thumbMaxLength = thumbMaxLength,
thumbUnselectedColor = thumbColor,
thumbSelectedColor = thumbSelectedColor,
thumbShape = thumbShape,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,16 @@ class LazyVerticalGridScrollbarTest(private val itemCount: Int) {
}
}

@Test
fun `scrollbar thumb exact height`() {
setContent(thumbMinLength = 0.4f, thumbMaxLength = 0.4f)
scrollbarScreen(composeRule) {
assert {
hasThumbHeight(value = 0.4f)
}
}
}

@Test
fun `scrollbar thumb min height`() {
setContent(thumbMinLength = 0.2f)
Expand Down Expand Up @@ -430,6 +440,7 @@ class LazyVerticalGridScrollbarTest(private val itemCount: Int) {
thickness: Dp = 6.dp,
padding: Dp = 8.dp,
thumbMinLength: Float = 0.1f,
thumbMaxLength: Float = 1f,
thumbColor: Color = Color(0xFF2A59B6),
thumbSelectedColor: Color = Color(0xFF5281CA),
thumbShape: Shape = CircleShape,
Expand All @@ -450,6 +461,7 @@ class LazyVerticalGridScrollbarTest(private val itemCount: Int) {
scrollbarPadding = padding,
thumbThickness = thickness,
thumbMinLength = thumbMinLength,
thumbMaxLength = thumbMaxLength,
thumbUnselectedColor = thumbColor,
thumbSelectedColor = thumbSelectedColor,
thumbShape = thumbShape,
Expand Down
Loading

0 comments on commit 994a35e

Please sign in to comment.