Skip to content

Commit

Permalink
Use unified data class for all the settings: ScrollbarSettings.kt
Browse files Browse the repository at this point in the history
  • Loading branch information
nanihadesuka committed Apr 21, 2024
1 parent 2196a58 commit a6f3c4d
Show file tree
Hide file tree
Showing 19 changed files with 289 additions and 519 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import my.nanihadesuka.compose.RowScrollbar
import my.nanihadesuka.compose.ScrollbarSelectionActionable
import my.nanihadesuka.compose.ScrollbarSelectionMode
import my.nanihadesuka.compose.ScrollbarLayoutSide
import my.nanihadesuka.compose.ScrollbarSettings
import my.nanihadesuka.lazycolumnscrollbar.ui.theme.LazyColumnScrollbarTheme

class MainActivity : ComponentActivity() {
Expand Down Expand Up @@ -115,8 +116,10 @@ fun LazyColumnView() {
) {
LazyColumnScrollbar(
listState,
selectionMode = ScrollbarSelectionMode.Thumb,
alwaysShowScrollBar = true,
settings = ScrollbarSettings(
selectionMode = ScrollbarSelectionMode.Thumb,
alwaysShowScrollbar = true,
),
indicatorContent = { index, isThumbSelected ->
Indicator(text = "i : $index", isThumbSelected = isThumbSelected)
}
Expand Down Expand Up @@ -168,8 +171,10 @@ fun LazyRowView() {
) {
LazyRowScrollbar(
listState,
selectionMode = ScrollbarSelectionMode.Thumb,
alwaysShowScrollBar = true,
settings = ScrollbarSettings(
selectionMode = ScrollbarSelectionMode.Thumb,
alwaysShowScrollbar = true,
),
indicatorContent = { index, isThumbSelected ->
Indicator(text = "i : $index", isThumbSelected = isThumbSelected)
}
Expand Down Expand Up @@ -222,8 +227,10 @@ fun LazyVerticalGridView() {
) {
LazyVerticalGridScrollbar(
state = lazyGridState,
selectionMode = ScrollbarSelectionMode.Thumb,
alwaysShowScrollBar = true,
settings = ScrollbarSettings(
selectionMode = ScrollbarSelectionMode.Thumb,
alwaysShowScrollbar = true,
),
indicatorContent = { index, isThumbSelected ->
Indicator(text = "i:$index", isThumbSelected = isThumbSelected)
}
Expand Down Expand Up @@ -270,8 +277,10 @@ fun LazyHorizontalGridView() {
) {
LazyHorizontalGridScrollbar(
state = lazyGridState,
selectionMode = ScrollbarSelectionMode.Thumb,
alwaysShowScrollBar = false,
settings = ScrollbarSettings(
selectionMode = ScrollbarSelectionMode.Thumb,
alwaysShowScrollbar = false
),
indicatorContent = { index, isThumbSelected ->
Indicator(text = "i:$index", isThumbSelected = isThumbSelected)
}
Expand Down Expand Up @@ -323,10 +332,12 @@ fun ColumnView() {
ColumnScrollbar(
state = listState,
indicatorContent = indicatorContent,
selectionMode = ScrollbarSelectionMode.Thumb,
selectionActionable = ScrollbarSelectionActionable.WhenVisible,
alwaysShowScrollBar = true,
side = ScrollbarLayoutSide.Start,
settings = ScrollbarSettings(
selectionMode = ScrollbarSelectionMode.Thumb,
selectionActionable = ScrollbarSelectionActionable.WhenVisible,
alwaysShowScrollbar = true,
side = ScrollbarLayoutSide.Start,
),
) {
Column(
modifier = Modifier.verticalScroll(listState)
Expand Down Expand Up @@ -363,10 +374,12 @@ fun RowView() {
RowScrollbar(
state = listState,
indicatorContent = indicatorContent,
selectionMode = ScrollbarSelectionMode.Thumb,
selectionActionable = ScrollbarSelectionActionable.WhenVisible,
alwaysShowScrollBar = false,
side = ScrollbarLayoutSide.End,
settings = ScrollbarSettings(
selectionMode = ScrollbarSelectionMode.Thumb,
selectionActionable = ScrollbarSelectionActionable.WhenVisible,
alwaysShowScrollbar = false,
side = ScrollbarLayoutSide.End,
),
) {
Row(
modifier = Modifier.horizontalScroll(listState)
Expand Down
75 changes: 11 additions & 64 deletions lib/src/main/java/my/nanihadesuka/compose/ColumnScrollbar.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,112 +3,59 @@ package my.nanihadesuka.compose
import androidx.compose.foundation.ScrollState
import androidx.compose.foundation.gestures.Orientation
import androidx.compose.foundation.layout.BoxWithConstraints
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Shape
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import my.nanihadesuka.compose.controller.rememberScrollStateController
import my.nanihadesuka.compose.generic.ElementScrollbar

/**
* @param thickness Thickness of the scrollbar thumb
* @param padding Padding of the scrollbar
* @param thumbMinHeight Thumb minimum height proportional to total scrollbar's height (eg: 0.1 -> 10% of total)
*/

@Composable
fun ColumnScrollbar(
state: ScrollState,
modifier: Modifier = Modifier,
side: ScrollbarLayoutSide = ScrollbarLayoutSide.End,
alwaysShowScrollBar: Boolean = false,
thickness: Dp = 6.dp,
padding: Dp = 8.dp,
thumbMinHeight: Float = 0.1f,
thumbColor: Color = Color(0xFF2A59B6),
thumbSelectedColor: Color = Color(0xFF5281CA),
thumbShape: Shape = CircleShape,
enabled: Boolean = true,
selectionMode: ScrollbarSelectionMode = ScrollbarSelectionMode.Thumb,
selectionActionable: ScrollbarSelectionActionable = ScrollbarSelectionActionable.Always,
hideDelayMillis: Int = 400,
settings: ScrollbarSettings = ScrollbarSettings.Default,
indicatorContent: (@Composable (normalizedOffset: Float, isThumbSelected: Boolean) -> Unit)? = null,
content: @Composable () -> Unit
) {
if (!enabled) content()
else BoxWithConstraints(modifier = modifier) {
if (!settings.enabled) content()
else BoxWithConstraints(modifier) {
content()
InternalColumnScrollbar(
state = state,
modifier = Modifier,
side = side,
alwaysShowScrollBar = alwaysShowScrollBar,
thickness = thickness,
padding = padding,
thumbMinLength = thumbMinHeight,
thumbColor = thumbColor,
thumbSelectedColor = thumbSelectedColor,
thumbShape = thumbShape,
settings = settings,
visibleLengthDp = with(LocalDensity.current) { constraints.maxHeight.toDp() },
indicatorContent = indicatorContent,
selectionMode = selectionMode,
selectionActionable = selectionActionable,
hideDelayMillis = hideDelayMillis,
)
}
}

/**
* Scrollbar for Column
* Use this variation if you want to place the scrollbar independently of the Column position
*
* @param thickness Thickness of the scrollbar thumb
* @param padding Padding of the scrollbar
* @param thumbMinLength Thumb minimum length proportional to total scrollbar's length (eg: 0.1 -> 10% of total)
* Use this variation if you want to place the scrollbar independently of the list position
* @param visibleLengthDp Visible length of column view
*/
@Composable
fun InternalColumnScrollbar(
state: ScrollState,
modifier: Modifier = Modifier,
side: ScrollbarLayoutSide = ScrollbarLayoutSide.End,
alwaysShowScrollBar: Boolean = false,
thickness: Dp = 6.dp,
padding: Dp = 8.dp,
thumbMinLength: Float = 0.1f,
thumbColor: Color = Color(0xFF2A59B6),
thumbSelectedColor: Color = Color(0xFF5281CA),
thumbShape: Shape = CircleShape,
selectionMode: ScrollbarSelectionMode = ScrollbarSelectionMode.Thumb,
selectionActionable: ScrollbarSelectionActionable = ScrollbarSelectionActionable.Always,
hideDelayMillis: Int = 400,
settings: ScrollbarSettings = ScrollbarSettings.Default,
indicatorContent: (@Composable (normalizedOffset: Float, isThumbSelected: Boolean) -> Unit)? = null,
visibleLengthDp: Dp,
) {
val stateController = rememberScrollStateController(
state = state,
visibleLengthDp = visibleLengthDp,
thumbMinLength = thumbMinLength,
alwaysShowScrollBar = alwaysShowScrollBar,
selectionMode = selectionMode
thumbMinLength = settings.thumbMinLength,
alwaysShowScrollBar = settings.alwaysShowScrollbar,
selectionMode = settings.selectionMode
)

ElementScrollbar(
orientation = Orientation.Vertical,
stateController = stateController,
modifier = modifier,
side = side,
thickness = thickness,
padding = padding,
thumbColor = thumbColor,
thumbSelectedColor = thumbSelectedColor,
thumbShape = thumbShape,
selectionMode = selectionMode,
selectionActionable = selectionActionable,
hideDelayMillis = hideDelayMillis,
settings = settings,
indicatorContent = indicatorContent,
)
}
78 changes: 12 additions & 66 deletions lib/src/main/java/my/nanihadesuka/compose/LazyColumnScrollbar.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,106 +3,52 @@ package my.nanihadesuka.compose
import androidx.compose.foundation.gestures.Orientation
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.lazy.LazyListState
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Shape
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import my.nanihadesuka.compose.controller.rememberLazyListStateController
import my.nanihadesuka.compose.generic.ElementScrollbar

/**
* @param thickness Thickness of the scrollbar thumb
* @param padding Padding of the scrollbar
* @param thumbMinLength Thumb minimum length proportional to total scrollbar's length (eg: 0.1 -> 10% of total)
*/
@Composable
fun LazyColumnScrollbar(
listState: LazyListState,
state: LazyListState,
modifier: Modifier = Modifier,
side: ScrollbarLayoutSide = ScrollbarLayoutSide.End,
alwaysShowScrollBar: Boolean = false,
thickness: Dp = 6.dp,
padding: Dp = 8.dp,
thumbMinLength: Float = 0.1f,
thumbColor: Color = Color(0xFF2A59B6),
thumbSelectedColor: Color = Color(0xFF5281CA),
thumbShape: Shape = CircleShape,
selectionMode: ScrollbarSelectionMode = ScrollbarSelectionMode.Thumb,
selectionActionable: ScrollbarSelectionActionable = ScrollbarSelectionActionable.Always,
hideDelayMillis: Int = 400,
enabled: Boolean = true,
settings: ScrollbarSettings = ScrollbarSettings.Default,
indicatorContent: (@Composable (index: Int, isThumbSelected: Boolean) -> Unit)? = null,
content: @Composable () -> Unit
) {
if (!enabled) content()
else Box(modifier = modifier) {
if (!settings.enabled) content()
else Box(modifier) {
content()
InternalLazyColumnScrollbar(
state = listState,
modifier = Modifier,
side = side,
alwaysShowScrollBar = alwaysShowScrollBar,
thickness = thickness,
padding = padding,
thumbMinLength = thumbMinLength,
thumbColor = thumbColor,
thumbSelectedColor = thumbSelectedColor,
selectionActionable = selectionActionable,
hideDelayMillis = hideDelayMillis,
thumbShape = thumbShape,
selectionMode = selectionMode,
state = state,
settings = settings,
indicatorContent = indicatorContent,
)
}
}

/**
* Use this variation if you want to place the scrollbar independently of the list position
*
* @param thickness Thickness of the scrollbar thumb
* @param padding Padding of the scrollbar
* @param thumbMinLength Thumb minimum length proportional to total scrollbar's length (eg: 0.1 -> 10% of total)
*/
@Composable
fun InternalLazyColumnScrollbar(
state: LazyListState,
modifier: Modifier = Modifier,
side: ScrollbarLayoutSide = ScrollbarLayoutSide.End,
alwaysShowScrollBar: Boolean = false,
thickness: Dp = 6.dp,
padding: Dp = 8.dp,
thumbMinLength: Float = 0.1f,
thumbColor: Color = Color(0xFF2A59B6),
thumbSelectedColor: Color = Color(0xFF5281CA),
thumbShape: Shape = CircleShape,
selectionMode: ScrollbarSelectionMode = ScrollbarSelectionMode.Thumb,
selectionActionable: ScrollbarSelectionActionable = ScrollbarSelectionActionable.Always,
hideDelayMillis: Int = 400,
indicatorContent: (@Composable (index: Int, isThumbSelected: Boolean) -> Unit)? = null,
settings: ScrollbarSettings = ScrollbarSettings.Default,
indicatorContent: @Composable() ((index: Int, isThumbSelected: Boolean) -> Unit)? = null,
) {
val controller = rememberLazyListStateController(
state = state,
thumbMinLength = thumbMinLength,
alwaysShowScrollBar = alwaysShowScrollBar,
selectionMode = selectionMode
thumbMinLength = settings.thumbMinLength,
alwaysShowScrollBar = settings.alwaysShowScrollbar,
selectionMode = settings.selectionMode
)

ElementScrollbar(
orientation = Orientation.Vertical,
stateController = controller,
modifier = modifier,
side = side,
thickness = thickness,
padding = padding,
thumbColor = thumbColor,
thumbSelectedColor = thumbSelectedColor,
thumbShape = thumbShape,
selectionMode = selectionMode,
selectionActionable = selectionActionable,
hideDelayMillis = hideDelayMillis,
settings = settings,
indicatorContent = indicatorContent,
)
}
Loading

0 comments on commit a6f3c4d

Please sign in to comment.