Skip to content

Commit

Permalink
VerticalDraggableScrollbar source code clean up.
Browse files Browse the repository at this point in the history
  • Loading branch information
oxyroid committed Jul 17, 2024
1 parent 8c19af7 commit 625f722
Showing 1 changed file with 45 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import androidx.compose.animation.core.animateDpAsState
import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.animation.core.tween
import androidx.compose.foundation.Canvas
import androidx.compose.foundation.gestures.DraggableState
import androidx.compose.foundation.gestures.Orientation
import androidx.compose.foundation.gestures.draggable
import androidx.compose.foundation.gestures.rememberDraggableState
Expand All @@ -17,6 +18,7 @@ import androidx.compose.foundation.lazy.LazyListState
import androidx.compose.foundation.lazy.grid.LazyGridState
import androidx.compose.foundation.lazy.staggeredgrid.LazyStaggeredGridState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.Immutable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
Expand All @@ -41,11 +43,6 @@ import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch
import kotlin.time.Duration.Companion.milliseconds

data class DraggableScrollbarWidth(
val inactive: Dp,
val active: Dp
)

@Composable
fun VerticalDraggableScrollbar(
lazyListState: LazyListState,
Expand Down Expand Up @@ -133,16 +130,12 @@ fun VerticalDraggableScrollbar(

if (isVisible) {
Canvas(
modifier = modifier
.fillMaxHeight()
.padding(5.dp)
.requiredWidth(currentWidth)
.draggable(
state = draggableState,
orientation = Orientation.Vertical,
onDragStarted = { isDragging = true },
onDragStopped = { isDragging = false }
)
modifier = modifier.verticalDraggableScrollbar(
currentWidth = currentWidth,
draggableState = draggableState,
onDragStarted = { isDragging = true },
onDragStopped = { isDragging = false }
)
) {
drawVerticalDraggableScrollbar(
color = color,
Expand Down Expand Up @@ -242,16 +235,12 @@ fun VerticalDraggableScrollbar(

if (isVisible) {
Canvas(
modifier = modifier
.fillMaxHeight()
.padding(5.dp)
.requiredWidth(currentWidth)
.draggable(
state = draggableState,
orientation = Orientation.Vertical,
onDragStarted = { isDragging = true },
onDragStopped = { isDragging = false }
)
modifier = modifier.verticalDraggableScrollbar(
currentWidth = currentWidth,
draggableState = draggableState,
onDragStarted = { isDragging = true },
onDragStopped = { isDragging = false }
)
) {
drawVerticalDraggableScrollbar(
color = color,
Expand Down Expand Up @@ -350,16 +339,12 @@ fun VerticalDraggableScrollbar(
)
if (isVisible) {
Canvas(
modifier = modifier
.fillMaxHeight()
.padding(5.dp)
.requiredWidth(currentWidth)
.draggable(
state = draggableState,
orientation = Orientation.Vertical,
onDragStarted = { isDragging = true },
onDragStopped = { isDragging = false }
)
modifier = modifier.verticalDraggableScrollbar(
currentWidth = currentWidth,
draggableState = draggableState,
onDragStarted = { isDragging = true },
onDragStopped = { isDragging = false }
)
) {
drawVerticalDraggableScrollbar(
color = color,
Expand All @@ -373,6 +358,30 @@ fun VerticalDraggableScrollbar(
}
}

@Immutable
data class DraggableScrollbarWidth(
val inactive: Dp,
val active: Dp
)

private fun Modifier.verticalDraggableScrollbar(
currentWidth: Dp,
draggableState: DraggableState,
onDragStarted: () -> Unit,
onDragStopped: () -> Unit,
): Modifier = then(
Modifier
.fillMaxHeight()
.padding(5.dp)
.requiredWidth(currentWidth)
.draggable(
state = draggableState,
orientation = Orientation.Vertical,
onDragStarted = { onDragStarted() },
onDragStopped = { onDragStopped() }
)
)

private fun DrawScope.drawVerticalDraggableScrollbar(
color: Color,
currentAlpha: Float,
Expand All @@ -393,4 +402,4 @@ private fun DrawScope.drawVerticalDraggableScrollbar(
),
cornerRadius = CornerRadius(minDimension)
)
}
}

0 comments on commit 625f722

Please sign in to comment.