From 994a35e15d659a5a9f1ea6bd052b3127154d3629 Mon Sep 17 00:00:00 2001 From: nani <87673002+nanihadesuka@users.noreply.github.com> Date: Sun, 30 Jun 2024 23:22:59 +0200 Subject: [PATCH] Add max thumb length. This allows to also set a static thumb length by setting the min and max thumb length the same value. --- app/build.gradle.kts | 4 +-- .../lazycolumnscrollbar/MainActivity.kt | 2 +- lib/build.gradle.kts | 2 +- .../nanihadesuka/compose/ColumnScrollbar.kt | 1 + .../compose/LazyColumnScrollbar.kt | 3 +- .../compose/LazyHorizontalGridScrollbar.kt | 1 + .../nanihadesuka/compose/LazyRowScrollbar.kt | 1 + .../compose/LazyVerticalGridScrollbar.kt | 1 + .../my/nanihadesuka/compose/RowScrollbar.kt | 1 + .../nanihadesuka/compose/ScrollbarSettings.kt | 8 +++++ .../controller/LazyGridStateController.kt | 11 +++++-- .../controller/LazyListStateController.kt | 7 ++++- .../controller/ScrollStateController.kt | 7 ++++- .../compose/ColumnScrollbarTest.kt | 12 ++++++++ .../compose/LazyColumnScrollbarTest.kt | 12 ++++++++ .../LazyHorizontalGridScrollbarTest.kt | 12 ++++++++ .../compose/LazyRowScrollbarTest.kt | 12 ++++++++ .../compose/LazyVerticalGridScrollbarTest.kt | 12 ++++++++ .../nanihadesuka/compose/RowScrollbarTest.kt | 14 ++++++++- .../my/nanihadesuka/compose/ScrollbarRobot.kt | 22 ++++++++++++++ .../compose/ScrollbarSettingsTest.kt | 30 +++++++++++++++++++ 21 files changed, 165 insertions(+), 10 deletions(-) create mode 100644 lib/src/test/java/my/nanihadesuka/compose/ScrollbarSettingsTest.kt diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 17c98b8..2113713 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -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 { diff --git a/app/src/main/java/my/nanihadesuka/lazycolumnscrollbar/MainActivity.kt b/app/src/main/java/my/nanihadesuka/lazycolumnscrollbar/MainActivity.kt index d201eae..145dcdc 100644 --- a/app/src/main/java/my/nanihadesuka/lazycolumnscrollbar/MainActivity.kt +++ b/app/src/main/java/my/nanihadesuka/lazycolumnscrollbar/MainActivity.kt @@ -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( diff --git a/lib/build.gradle.kts b/lib/build.gradle.kts index c3940f4..498057f 100644 --- a/lib/build.gradle.kts +++ b/lib/build.gradle.kts @@ -5,7 +5,7 @@ plugins { } object MySettings { - val versionName: String = "2.0.7" + val versionName: String = "2.1.0" val namespace = "my.nanihadesuka.lazycolumnscrollbar" } diff --git a/lib/src/main/java/my/nanihadesuka/compose/ColumnScrollbar.kt b/lib/src/main/java/my/nanihadesuka/compose/ColumnScrollbar.kt index 07183a7..cea4ced 100644 --- a/lib/src/main/java/my/nanihadesuka/compose/ColumnScrollbar.kt +++ b/lib/src/main/java/my/nanihadesuka/compose/ColumnScrollbar.kt @@ -47,6 +47,7 @@ fun InternalColumnScrollbar( state = state, visibleLengthDp = visibleLengthDp, thumbMinLength = settings.thumbMinLength, + thumbMaxLength = settings.thumbMaxLength, alwaysShowScrollBar = settings.alwaysShowScrollbar, selectionMode = settings.selectionMode ) diff --git a/lib/src/main/java/my/nanihadesuka/compose/LazyColumnScrollbar.kt b/lib/src/main/java/my/nanihadesuka/compose/LazyColumnScrollbar.kt index 485f46c..bd0fe05 100644 --- a/lib/src/main/java/my/nanihadesuka/compose/LazyColumnScrollbar.kt +++ b/lib/src/main/java/my/nanihadesuka/compose/LazyColumnScrollbar.kt @@ -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( diff --git a/lib/src/main/java/my/nanihadesuka/compose/LazyHorizontalGridScrollbar.kt b/lib/src/main/java/my/nanihadesuka/compose/LazyHorizontalGridScrollbar.kt index dcad631..bddb07a 100644 --- a/lib/src/main/java/my/nanihadesuka/compose/LazyHorizontalGridScrollbar.kt +++ b/lib/src/main/java/my/nanihadesuka/compose/LazyHorizontalGridScrollbar.kt @@ -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 diff --git a/lib/src/main/java/my/nanihadesuka/compose/LazyRowScrollbar.kt b/lib/src/main/java/my/nanihadesuka/compose/LazyRowScrollbar.kt index 7cf1931..32fef25 100644 --- a/lib/src/main/java/my/nanihadesuka/compose/LazyRowScrollbar.kt +++ b/lib/src/main/java/my/nanihadesuka/compose/LazyRowScrollbar.kt @@ -40,6 +40,7 @@ fun InternalLazyRowScrollbar( val controller = rememberLazyListStateController( state = state, thumbMinLength = settings.thumbMinLength, + thumbMaxLength = settings.thumbMaxLength, alwaysShowScrollBar = settings.alwaysShowScrollbar, selectionMode = settings.selectionMode, ) diff --git a/lib/src/main/java/my/nanihadesuka/compose/LazyVerticalGridScrollbar.kt b/lib/src/main/java/my/nanihadesuka/compose/LazyVerticalGridScrollbar.kt index dd3da63..6b5b3f5 100644 --- a/lib/src/main/java/my/nanihadesuka/compose/LazyVerticalGridScrollbar.kt +++ b/lib/src/main/java/my/nanihadesuka/compose/LazyVerticalGridScrollbar.kt @@ -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 diff --git a/lib/src/main/java/my/nanihadesuka/compose/RowScrollbar.kt b/lib/src/main/java/my/nanihadesuka/compose/RowScrollbar.kt index 0070cfa..996715d 100644 --- a/lib/src/main/java/my/nanihadesuka/compose/RowScrollbar.kt +++ b/lib/src/main/java/my/nanihadesuka/compose/RowScrollbar.kt @@ -46,6 +46,7 @@ fun InternalRowScrollbar( state = state, visibleLengthDp = visibleLengthDp, thumbMinLength = settings.thumbMinLength, + thumbMaxLength = settings.thumbMaxLength, alwaysShowScrollBar = settings.alwaysShowScrollbar, selectionMode = settings.selectionMode, ) diff --git a/lib/src/main/java/my/nanihadesuka/compose/ScrollbarSettings.kt b/lib/src/main/java/my/nanihadesuka/compose/ScrollbarSettings.kt index d76d885..0a25d65 100644 --- a/lib/src/main/java/my/nanihadesuka/compose/ScrollbarSettings.kt +++ b/lib/src/main/java/my/nanihadesuka/compose/ScrollbarSettings.kt @@ -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, @@ -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, @@ -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, diff --git a/lib/src/main/java/my/nanihadesuka/compose/controller/LazyGridStateController.kt b/lib/src/main/java/my/nanihadesuka/compose/controller/LazyGridStateController.kt index 8e7c9b2..218c300 100644 --- a/lib/src/main/java/my/nanihadesuka/compose/controller/LazyGridStateController.kt +++ b/lib/src/main/java/my/nanihadesuka/compose/controller/LazyGridStateController.kt @@ -23,6 +23,7 @@ import kotlin.math.floor internal fun rememberLazyGridStateController( state: LazyGridState, thumbMinLength: Float, + thumbMaxLength: Float, alwaysShowScrollBar: Boolean, selectionMode: ScrollbarSelectionMode, orientation: Orientation @@ -30,6 +31,7 @@ internal fun rememberLazyGridStateController( val coroutineScope = rememberCoroutineScope() val thumbMinLengthUpdated = rememberUpdatedState(thumbMinLength) + val thumbMaxLengthUpdated = rememberUpdatedState(thumbMaxLength) val alwaysShowScrollBarUpdated = rememberUpdatedState(alwaysShowScrollBar) val selectionModeUpdated = rememberUpdatedState(selectionMode) val orientationUpdated = rememberUpdatedState(orientation) @@ -114,7 +116,10 @@ internal fun rememberLazyGridStateController( val thumbSizeNormalized = remember { derivedStateOf { - thumbSizeNormalizedReal.value.coerceAtLeast(thumbMinLengthUpdated.value) + thumbSizeNormalizedReal.value.coerceIn( + thumbMinLengthUpdated.value, + thumbMaxLengthUpdated.value, + ) } } @@ -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) } diff --git a/lib/src/main/java/my/nanihadesuka/compose/controller/LazyListStateController.kt b/lib/src/main/java/my/nanihadesuka/compose/controller/LazyListStateController.kt index 12bdd65..bb7b595 100644 --- a/lib/src/main/java/my/nanihadesuka/compose/controller/LazyListStateController.kt +++ b/lib/src/main/java/my/nanihadesuka/compose/controller/LazyListStateController.kt @@ -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 } } @@ -80,7 +82,10 @@ internal fun rememberLazyListStateController( val thumbSizeNormalized = remember { derivedStateOf { - thumbSizeNormalizedReal.value.coerceAtLeast(thumbMinLengthUpdated.value) + thumbSizeNormalizedReal.value.coerceIn( + thumbMinLengthUpdated.value, + thumbMaxLengthUpdated.value, + ) } } diff --git a/lib/src/main/java/my/nanihadesuka/compose/controller/ScrollStateController.kt b/lib/src/main/java/my/nanihadesuka/compose/controller/ScrollStateController.kt index 7f5e117..3c5091c 100644 --- a/lib/src/main/java/my/nanihadesuka/compose/controller/ScrollStateController.kt +++ b/lib/src/main/java/my/nanihadesuka/compose/controller/ScrollStateController.kt @@ -22,6 +22,7 @@ internal fun rememberScrollStateController( state: ScrollState, visibleLengthDp: Dp, thumbMinLength: Float, + thumbMaxLength: Float, alwaysShowScrollBar: Boolean, selectionMode: ScrollbarSelectionMode ): ScrollStateController { @@ -29,6 +30,7 @@ internal fun rememberScrollStateController( val visibleLengthDpUpdated = rememberUpdatedState(visibleLengthDp) val thumbMinLengthUpdated = rememberUpdatedState(thumbMinLength) + val thumbMaxLengthUpdated = rememberUpdatedState(thumbMaxLength) val alwaysShowScrollBarUpdated = rememberUpdatedState(alwaysShowScrollBar) val selectionModeUpdated = rememberUpdatedState(selectionMode) @@ -54,7 +56,10 @@ internal fun rememberScrollStateController( val thumbSizeNormalized = remember { derivedStateOf { - thumbSizeNormalizedReal.value.coerceAtLeast(thumbMinLengthUpdated.value) + thumbSizeNormalizedReal.value.coerceIn( + thumbMinLengthUpdated.value, + thumbMaxLengthUpdated.value, + ) } } diff --git a/lib/src/test/java/my/nanihadesuka/compose/ColumnScrollbarTest.kt b/lib/src/test/java/my/nanihadesuka/compose/ColumnScrollbarTest.kt index 2738aa6..c398a34 100644 --- a/lib/src/test/java/my/nanihadesuka/compose/ColumnScrollbarTest.kt +++ b/lib/src/test/java/my/nanihadesuka/compose/ColumnScrollbarTest.kt @@ -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) @@ -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, @@ -386,6 +397,7 @@ class ColumnScrollbarTest(private val itemCount: Int) { scrollbarPadding = padding, thumbThickness = thickness, thumbMinLength = thumbMinLength, + thumbMaxLength = thumbMaxLength, thumbUnselectedColor = thumbUnselectedColor, thumbSelectedColor = thumbSelectedColor, thumbShape = thumbShape, diff --git a/lib/src/test/java/my/nanihadesuka/compose/LazyColumnScrollbarTest.kt b/lib/src/test/java/my/nanihadesuka/compose/LazyColumnScrollbarTest.kt index 7256f6a..1c06316 100644 --- a/lib/src/test/java/my/nanihadesuka/compose/LazyColumnScrollbarTest.kt +++ b/lib/src/test/java/my/nanihadesuka/compose/LazyColumnScrollbarTest.kt @@ -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) @@ -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, @@ -449,6 +460,7 @@ class LazyColumnScrollbarTest(private val itemCount: Int) { scrollbarPadding = padding, thumbThickness = thickness, thumbMinLength = thumbMinLength, + thumbMaxLength = thumbMaxLength, thumbUnselectedColor = thumbColor, thumbSelectedColor = thumbSelectedColor, thumbShape = thumbShape, diff --git a/lib/src/test/java/my/nanihadesuka/compose/LazyHorizontalGridScrollbarTest.kt b/lib/src/test/java/my/nanihadesuka/compose/LazyHorizontalGridScrollbarTest.kt index a5b718b..ef98c9b 100644 --- a/lib/src/test/java/my/nanihadesuka/compose/LazyHorizontalGridScrollbarTest.kt +++ b/lib/src/test/java/my/nanihadesuka/compose/LazyHorizontalGridScrollbarTest.kt @@ -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) @@ -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, @@ -450,6 +461,7 @@ class LazyHorizontalGridScrollbarTest(private val itemCount: Int) { scrollbarPadding = padding, thumbThickness = thickness, thumbMinLength = thumbMinLength, + thumbMaxLength = thumbMaxLength, thumbUnselectedColor = thumbColor, thumbSelectedColor = thumbSelectedColor, thumbShape = thumbShape, diff --git a/lib/src/test/java/my/nanihadesuka/compose/LazyRowScrollbarTest.kt b/lib/src/test/java/my/nanihadesuka/compose/LazyRowScrollbarTest.kt index abfe410..232e672 100644 --- a/lib/src/test/java/my/nanihadesuka/compose/LazyRowScrollbarTest.kt +++ b/lib/src/test/java/my/nanihadesuka/compose/LazyRowScrollbarTest.kt @@ -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) @@ -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, @@ -447,6 +458,7 @@ class LazyRowScrollbarTest(private val itemCount: Int) { scrollbarPadding = padding, thumbThickness = thickness, thumbMinLength = thumbMinLength, + thumbMaxLength = thumbMaxLength, thumbUnselectedColor = thumbColor, thumbSelectedColor = thumbSelectedColor, thumbShape = thumbShape, diff --git a/lib/src/test/java/my/nanihadesuka/compose/LazyVerticalGridScrollbarTest.kt b/lib/src/test/java/my/nanihadesuka/compose/LazyVerticalGridScrollbarTest.kt index 1c6d8ee..9c6c3b4 100644 --- a/lib/src/test/java/my/nanihadesuka/compose/LazyVerticalGridScrollbarTest.kt +++ b/lib/src/test/java/my/nanihadesuka/compose/LazyVerticalGridScrollbarTest.kt @@ -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) @@ -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, @@ -450,6 +461,7 @@ class LazyVerticalGridScrollbarTest(private val itemCount: Int) { scrollbarPadding = padding, thumbThickness = thickness, thumbMinLength = thumbMinLength, + thumbMaxLength = thumbMaxLength, thumbUnselectedColor = thumbColor, thumbSelectedColor = thumbSelectedColor, thumbShape = thumbShape, diff --git a/lib/src/test/java/my/nanihadesuka/compose/RowScrollbarTest.kt b/lib/src/test/java/my/nanihadesuka/compose/RowScrollbarTest.kt index f41d728..8fc69b5 100644 --- a/lib/src/test/java/my/nanihadesuka/compose/RowScrollbarTest.kt +++ b/lib/src/test/java/my/nanihadesuka/compose/RowScrollbarTest.kt @@ -226,7 +226,17 @@ class RowScrollbarTest(private val itemCount: Int) { } @Test - fun `scrollbar thumb min length`() { + 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) scrollbarScreen(composeRule) { assert { @@ -367,6 +377,7 @@ class RowScrollbarTest(private val itemCount: Int) { thickness: Dp = 6.dp, padding: Dp = 8.dp, thumbMinLength: Float = 0.1f, + thumbMaxLength: Float = 1.0f, thumbColor: Color = Color(0xFF2A59B6), thumbSelectedColor: Color = Color(0xFF5281CA), thumbShape: Shape = CircleShape, @@ -386,6 +397,7 @@ class RowScrollbarTest(private val itemCount: Int) { scrollbarPadding = padding, thumbThickness = thickness, thumbMinLength = thumbMinLength, + thumbMaxLength = thumbMaxLength, thumbUnselectedColor = thumbColor, thumbSelectedColor = thumbSelectedColor, thumbShape = thumbShape, diff --git a/lib/src/test/java/my/nanihadesuka/compose/ScrollbarRobot.kt b/lib/src/test/java/my/nanihadesuka/compose/ScrollbarRobot.kt index 9112914..8c4a5be 100644 --- a/lib/src/test/java/my/nanihadesuka/compose/ScrollbarRobot.kt +++ b/lib/src/test/java/my/nanihadesuka/compose/ScrollbarRobot.kt @@ -173,6 +173,22 @@ class ScrollbarRobot(private val composeRule: ComposeContentTestRule) { assertEqualWithTolerance(thumbPaddingVertical, value, tolerance = 0.1.dp) } + fun hasThumbHeight(value: Float) { + assertEqualWithTolerance( + thumbBounds.height.value / scrollbarContainerBounds.height.value, + value, + tolerance = 0.01f + ) + } + + fun hasThumbWidth(value: Float) { + assertEqualWithTolerance( + thumbBounds.width.value / scrollbarContainerBounds.width.value, + value, + tolerance = 0.01f + ) + } + fun hasThumbMinHeightOrGreater(minValue: Float) { assertEqualOrGreater( thumbBounds.height.value / scrollbarContainerBounds.height.value, @@ -289,6 +305,12 @@ class ScrollbarRobot(private val composeRule: ComposeContentTestRule) { private val DpRect.verticalCenter: Dp get() = (bottom + top) / 2 private val DpRect.horizontalCenter: Dp get() = (right + left) / 2 +private fun assertEqualWithTolerance(valueA: Float, valueB: Float, tolerance: Float = 0.01f) { + assert((valueA - valueB).absoluteValue <= tolerance) { + "Not equal: valueA=$valueA valueB=$valueB tolerance=$tolerance" + } +} + private fun assertEqualWithTolerance(valueA: Dp, valueB: Dp, tolerance: Dp = 2.dp) { assert((valueA - valueB).value.absoluteValue <= tolerance.value) { "Not equal: valueA=$valueA valueB=$valueB tolerance=$tolerance" diff --git a/lib/src/test/java/my/nanihadesuka/compose/ScrollbarSettingsTest.kt b/lib/src/test/java/my/nanihadesuka/compose/ScrollbarSettingsTest.kt new file mode 100644 index 0000000..e7322e0 --- /dev/null +++ b/lib/src/test/java/my/nanihadesuka/compose/ScrollbarSettingsTest.kt @@ -0,0 +1,30 @@ +package my.nanihadesuka.compose + +import org.junit.Test + +class ScrollbarSettingsTest { + + @Test(expected = IllegalArgumentException::class) + fun `min thumb value is greater than max thumb value - crash`() { + ScrollbarSettings( + thumbMinLength = 0.8f, + thumbMaxLength = 0.5f, + ) + } + + @Test + fun `min thumb value is not greater than max thumb value - success`() { + ScrollbarSettings( + thumbMinLength = 0.5f, + thumbMaxLength = 0.8f, + ) + } + + @Test + fun `min thumb value is equal than max thumb value - success`() { + ScrollbarSettings( + thumbMinLength = 0.5f, + thumbMaxLength = 0.5f, + ) + } +} \ No newline at end of file