Skip to content

Commit

Permalink
Merge pull request #23 from KuhakuPixel/master
Browse files Browse the repository at this point in the history
Add `alwaysShowScrollbar` param to always show the scroll bar
  • Loading branch information
nanihadesuka committed Oct 8, 2023
2 parents d5d4cd9 + 1fd8e89 commit 76b3081
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 5 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ indicatorContent = { index, isThumbSelected ->
fun LazyColumnScrollbar(
listState: LazyListState,
rightSide: Boolean = true,
alwaysShowScrollBar: Boolean = false,
thickness: Dp = 6.dp,
padding: Dp = 8.dp,
thumbMinHeight: Float = 0.1f,
Expand Down Expand Up @@ -121,6 +122,7 @@ ColumnScrollbar(listState) {
fun ColumnScrollbar(
state: ScrollState,
rightSide: Boolean = true,
alwaysShowScrollBar: Boolean = false,
thickness: Dp = 6.dp,
padding: Dp = 8.dp,
thumbMinHeight: Float = 0.1f,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ fun LazyColumnView() {
LazyColumnScrollbar(
listState,
selectionMode = ScrollbarSelectionMode.Thumb,
alwaysShowScrollBar = true,
indicatorContent = { index, isThumbSelected ->
Surface {
Text(
Expand Down Expand Up @@ -152,6 +153,7 @@ fun ColumnView() {
state = listState,
indicatorContent = indicatorContent,
selectionMode = ScrollbarSelectionMode.Disabled,
alwaysShowScrollBar = true,
) {
Column(
modifier = Modifier.verticalScroll(listState)
Expand Down
9 changes: 7 additions & 2 deletions lib/src/main/java/my/nanihadesuka/compose/ColumnScrollbar.kt
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import kotlinx.coroutines.launch
fun ColumnScrollbar(
state: ScrollState,
rightSide: Boolean = true,
alwaysShowScrollBar: Boolean = false,
thickness: Dp = 6.dp,
padding: Dp = 8.dp,
thumbMinHeight: Float = 0.1f,
Expand All @@ -70,6 +71,7 @@ fun ColumnScrollbar(
state = state,
modifier = Modifier,
rightSide = rightSide,
alwaysShowScrollBar = alwaysShowScrollBar,
thickness = thickness,
padding = padding,
thumbMinHeight = thumbMinHeight,
Expand Down Expand Up @@ -100,6 +102,7 @@ fun InternalColumnScrollbar(
state: ScrollState,
modifier: Modifier = Modifier,
rightSide: Boolean = true,
alwaysShowScrollBar: Boolean = false,
thickness: Dp = 6.dp,
padding: Dp = 8.dp,
thumbMinHeight: Float = 0.1f,
Expand Down Expand Up @@ -171,7 +174,7 @@ fun InternalColumnScrollbar(
}
}

val isInAction = state.isScrollInProgress || isSelected
val isInAction = state.isScrollInProgress || isSelected || alwaysShowScrollBar

val isInActionSelectable = remember { mutableStateOf(isInAction) }
val durationAnimationMillis: Int = 500
Expand Down Expand Up @@ -304,6 +307,8 @@ fun InternalColumnScrollbar(
ScrollbarSelectionActionable.Always -> true
ScrollbarSelectionActionable.WhenVisible -> isInActionSelectable.value
}
) { DraggableBar() }
) {
DraggableBar()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import kotlin.math.floor
fun LazyColumnScrollbar(
listState: LazyListState,
rightSide: Boolean = true,
alwaysShowScrollBar: Boolean = false,
thickness: Dp = 6.dp,
padding: Dp = 8.dp,
thumbMinHeight: Float = 0.1f,
Expand All @@ -71,6 +72,7 @@ fun LazyColumnScrollbar(
listState = listState,
modifier = Modifier,
rightSide = rightSide,
alwaysShowScrollBar = alwaysShowScrollBar,
thickness = thickness,
padding = padding,
thumbMinHeight = thumbMinHeight,
Expand Down Expand Up @@ -99,6 +101,7 @@ fun InternalLazyColumnScrollbar(
listState: LazyListState,
modifier: Modifier = Modifier,
rightSide: Boolean = true,
alwaysShowScrollBar: Boolean = false,
thickness: Dp = 6.dp,
padding: Dp = 8.dp,
thumbMinHeight: Float = 0.1f,
Expand Down Expand Up @@ -228,7 +231,7 @@ fun InternalLazyColumnScrollbar(
}
}

val isInAction = listState.isScrollInProgress || isSelected
val isInAction = listState.isScrollInProgress || isSelected || alwaysShowScrollBar

val isInActionSelectable = remember { mutableStateOf(isInAction) }
val durationAnimationMillis: Int = 500
Expand Down Expand Up @@ -369,6 +372,8 @@ fun InternalLazyColumnScrollbar(
ScrollbarSelectionActionable.Always -> true
ScrollbarSelectionActionable.WhenVisible -> isInActionSelectable.value
}
) { DraggableBar() }
) {
DraggableBar()
}
}
}
32 changes: 31 additions & 1 deletion lib/src/test/java/my/nanihadesuka/compose/ColumnScrollbarTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,34 @@ class ColumnScrollbarTest(private val itemCount: Int) {
}
}

@Test
fun `always show scrollbar false`() {
if (itemCount == 0) return

setContent(
alwaysShowScrollBar = false,
)

scrollbarScreen(composeRule) {
// not visible without scrolling
assert { isItemHidden(TestTagsScrollbar.scrollbar) }
}
}

@Test
fun `always show scrollbar true`() {
if (itemCount == 0) return

setContent(
alwaysShowScrollBar = true,
)

scrollbarScreen(composeRule) {
// always visible without scrolling
assert { isItemVisible(TestTagsScrollbar.scrollbar) }
}
}

@Test
fun `scroll list to the bottom`() {
if (itemCount == 0) return
Expand Down Expand Up @@ -329,6 +357,7 @@ class ColumnScrollbarTest(private val itemCount: Int) {
private fun setContent(
state: ScrollState = ScrollState(initial = 0),
rightSide: Boolean = true,
alwaysShowScrollBar: Boolean = false,
thickness: Dp = 6.dp,
padding: Dp = 8.dp,
thumbMinHeight: Float = 0.1f,
Expand All @@ -345,6 +374,7 @@ class ColumnScrollbarTest(private val itemCount: Int) {
ColumnScrollbar(
state = state,
rightSide = rightSide,
alwaysShowScrollBar = alwaysShowScrollBar,
thickness = thickness,
padding = padding,
enabled = enabled,
Expand All @@ -355,7 +385,7 @@ class ColumnScrollbarTest(private val itemCount: Int) {
indicatorContent = indicatorContent,
selectionMode = selectionMode,
selectionActionable = selectionActionable,
) {
) {
Column(Modifier.verticalScroll(state = state)) {
repeat(listItemsCount) {
Text(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,34 @@ class LazyColumnScrollbarTest(private val itemCount: Int) {
}
}

@Test
fun `always show scrollbar false`() {
if (itemCount == 0) return

setContent(
alwaysShowScrollBar = false,
)

scrollbarScreen(composeRule) {
// not visible without scrolling
assert { isItemHidden(TestTagsScrollbar.scrollbar) }
}
}

@Test
fun `always show scrollbar true`() {
if (itemCount == 0) return

setContent(
alwaysShowScrollBar = true,
)

scrollbarScreen(composeRule) {
// always visible without scrolling
assert { isItemVisible(TestTagsScrollbar.scrollbar) }
}
}

@Test
fun `scroll list to the bottom`() {
if (itemCount == 0) return
Expand Down Expand Up @@ -389,6 +417,7 @@ class LazyColumnScrollbarTest(private val itemCount: Int) {
private fun setContent(
state: LazyListState = LazyListState(),
rightSide: Boolean = true,
alwaysShowScrollBar: Boolean = false,
thickness: Dp = 6.dp,
padding: Dp = 8.dp,
thumbMinHeight: Float = 0.1f,
Expand All @@ -406,6 +435,7 @@ class LazyColumnScrollbarTest(private val itemCount: Int) {
LazyColumnScrollbar(
listState = state,
rightSide = rightSide,
alwaysShowScrollBar = alwaysShowScrollBar,
thickness = thickness,
padding = padding,
enabled = enabled,
Expand Down

0 comments on commit 76b3081

Please sign in to comment.