From 2480fe52c0c98373acf084cf5a60874f9f6d1e4b Mon Sep 17 00:00:00 2001 From: vfsfitvnm Date: Sun, 3 Jul 2022 23:46:47 +0200 Subject: [PATCH] Add drawSteps parameter to SeekBar --- .../vimusic/ui/components/SeekBar.kt | 31 +++++++++++++++++-- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/components/SeekBar.kt b/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/components/SeekBar.kt index a574595..cfeec74 100644 --- a/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/components/SeekBar.kt +++ b/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/components/SeekBar.kt @@ -31,18 +31,32 @@ fun SeekBar( scrubberColor: Color = color, scrubberRadius: Dp = 6.dp, shape: Shape = RectangleShape, + drawSteps: Boolean = false, ) { Box( modifier = modifier .pointerInput(minimumValue, maximumValue) { if (maximumValue < minimumValue) return@pointerInput + var acc = 0f + detectHorizontalDragGestures( onHorizontalDrag = { _, delta -> - onDrag((delta / size.width * (maximumValue - minimumValue)).roundToLong()) + acc += delta / size.width * (maximumValue - minimumValue) + + if (acc !in -1f..1f) { + onDrag(acc.toLong()) + acc -= acc.toLong() + } }, - onDragEnd = onDragEnd, - onDragCancel = onDragEnd + onDragEnd = { + acc = 0f + onDragEnd() + }, + onDragCancel = { + acc = 0f + onDragEnd() + } ) } .pointerInput(minimumValue, maximumValue) { @@ -72,6 +86,17 @@ fun SeekBar( radius = scrubberRadius.toPx(), center = center.copy(x = scrubberPosition) ) + + if (drawSteps) { + for (i in value + 1 .. maximumValue) { + val stepPosition = (i.toFloat() - minimumValue) / (maximumValue - minimumValue) * size.width + drawCircle( + color = scrubberColor, + radius = scrubberRadius.toPx() / 2, + center = center.copy(x = stepPosition), + ) + } + } } ) { Spacer(