Skip to content

Commit 5eeffd0

Browse files
committed
feat(Slider): Add Tap functionality
1 parent 256a67b commit 5eeffd0

File tree

4 files changed

+42
-15
lines changed

4 files changed

+42
-15
lines changed

demo/src/main/java/io/monstarlab/mosaic/features/SliderDemo.kt

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ fun SliderDemo() = Scaffold(modifier = Modifier) {
3131
Column(
3232
modifier = Modifier
3333
.padding(it)
34-
.padding(16.dp)
35-
.background(Color.LightGray),
34+
.padding(16.dp),
3635
verticalArrangement = Arrangement.spacedBy(32.dp),
3736
) {
3837
MaterialSlider(
@@ -74,8 +73,12 @@ fun MosaicSliderDemo(
7473
horizontalArrangement = Arrangement.Start,
7574
modifier = Modifier,
7675
) {
77-
Text(text = range.start.toString())
76+
Text(
77+
text = range.start.toString(),
78+
modifier = Modifier.weight(0.2f),
79+
)
7880
Slider(
81+
modifier = Modifier.weight(0.8f),
7982
value = value,
8083
onValueChange = { value = it },
8184
colors = SliderColors(Color.Black),
@@ -91,8 +94,11 @@ fun MosaicSliderDemo(
9194
}
9295
},
9396
)
97+
Text(
98+
text = range.endInclusive.toString(),
99+
modifier = Modifier.weight(0.2f),
100+
)
94101
}
95-
Text(text = range.endInclusive.toString())
96102
}
97103
}
98104

slider/src/main/java/io/monstarlab/mosaic/slider/Slider.kt

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package io.monstarlab.mosaic.slider
22

33
import androidx.compose.foundation.background
4+
import androidx.compose.foundation.focusable
45
import androidx.compose.foundation.gestures.Orientation
6+
import androidx.compose.foundation.gestures.detectTapGestures
57
import androidx.compose.foundation.gestures.draggable
68
import androidx.compose.foundation.interaction.MutableInteractionSource
79
import androidx.compose.foundation.layout.Box
@@ -11,6 +13,7 @@ import androidx.compose.runtime.Composable
1113
import androidx.compose.runtime.remember
1214
import androidx.compose.ui.Modifier
1315
import androidx.compose.ui.graphics.Color
16+
import androidx.compose.ui.input.pointer.pointerInput
1417
import androidx.compose.ui.platform.LocalLayoutDirection
1518
import androidx.compose.ui.tooling.preview.Preview
1619
import androidx.compose.ui.unit.LayoutDirection
@@ -45,17 +48,27 @@ public fun Slider(
4548
state.range = range
4649
state.onValueChange = onValueChange
4750

48-
val draggable = modifier.draggable(
51+
val tap = Modifier.pointerInput(state, interactionSource) {
52+
detectTapGestures(
53+
onPress = { state.handlePress(it) },
54+
)
55+
}
56+
57+
val drag = Modifier.draggable(
4958
state = state,
5059
orientation = Orientation.Horizontal,
5160
enabled = true,
5261
interactionSource = interactionSource,
53-
startDragImmediately = true,
62+
startDragImmediately = state.isDragging,
5463
reverseDirection = isRtl,
64+
onDragStopped = {},
5565
)
5666

5767
SliderLayout(
58-
modifier = modifier.then(draggable),
68+
modifier = modifier
69+
.focusable(true, interactionSource)
70+
.then(tap)
71+
.then(drag),
5972
thumb = thumb,
6073
track = {
6174
SliderTrack(

slider/src/main/java/io/monstarlab/mosaic/slider/SliderState.kt

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import androidx.compose.runtime.mutableFloatStateOf
1010
import androidx.compose.runtime.mutableStateOf
1111
import androidx.compose.runtime.remember
1212
import androidx.compose.runtime.setValue
13+
import androidx.compose.ui.geometry.Offset
1314
import kotlinx.coroutines.coroutineScope
1415

1516
/**
@@ -53,13 +54,7 @@ public class SliderState(
5354
override fun dispatchRawDelta(delta: Float) {
5455
val newRawOffset = rawOffset + delta
5556
val userValue = scaleToUserValue(newRawOffset)
56-
if (userValue != value) {
57-
if (onValueChange != null) {
58-
onValueChange?.invoke(userValue)
59-
} else {
60-
rawOffset = newRawOffset
61-
}
62-
}
57+
handleValueUpdate(userValue, newRawOffset)
6358
}
6459

6560
override suspend fun drag(
@@ -71,11 +66,25 @@ public class SliderState(
7166
isDragging = false
7267
}
7368

69+
internal fun handlePress(offset: Offset) {
70+
println(offset)
71+
val userValue = scaleToUserValue(offset.x)
72+
handleValueUpdate(userValue, offset.x)
73+
}
74+
7475
internal fun updateDimensions(totalWidth: Float, thumbWidth: Float) {
7576
this.totalWidth = totalWidth
7677
this.thumbWidth = thumbWidth
7778
}
7879

80+
private fun handleValueUpdate(value: Float, offset: Float) {
81+
if (onValueChange != null) {
82+
onValueChange?.invoke(value)
83+
} else {
84+
rawOffset = offset
85+
}
86+
}
87+
7988
private fun scaleToUserValue(offset: Float): Float {
8089
val rangeStart = valueDistribution.interpolate(range.start)
8190
val rangeEnd = valueDistribution.interpolate(range.endInclusive)

slider/src/main/java/io/monstarlab/mosaic/slider/SliderValueDistribution.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ public interface SliderValueDistribution {
5555
}
5656
}
5757

58-
5958
/**
6059
* Represents a parabolic distribution strategy for slider values.
6160
*

0 commit comments

Comments
 (0)