@@ -4,6 +4,7 @@ import androidx.compose.foundation.background
4
4
import androidx.compose.foundation.layout.Box
5
5
import androidx.compose.foundation.layout.fillMaxWidth
6
6
import androidx.compose.foundation.layout.height
7
+ import androidx.compose.foundation.layout.requiredSizeIn
7
8
import androidx.compose.foundation.layout.size
8
9
import androidx.compose.foundation.shape.CircleShape
9
10
import androidx.compose.runtime.Composable
@@ -13,16 +14,19 @@ import androidx.compose.ui.layout.Layout
13
14
import androidx.compose.ui.layout.layoutId
14
15
import androidx.compose.ui.tooling.preview.Preview
15
16
import androidx.compose.ui.unit.dp
17
+ import androidx.compose.ui.unit.offset
16
18
import kotlin.math.max
17
19
import kotlin.math.roundToInt
18
20
19
21
@Composable
20
- internal fun SliderLayout (
22
+ public fun SliderLayout (
21
23
progress : Float ,
24
+ modifier : Modifier = Modifier ,
22
25
track : @Composable () -> Unit ,
23
26
thumb : @Composable () -> Unit
24
27
) {
25
28
Layout (
29
+ modifier = modifier.requiredSizeIn(SliderDefaults .HandleWidth , SliderDefaults .HandleHeight ),
26
30
content = {
27
31
Box (modifier = Modifier .layoutId(SliderLayoutElements .Track )) {
28
32
track()
@@ -32,22 +36,24 @@ internal fun SliderLayout(
32
36
}
33
37
}) { mesuarables, constraints ->
34
38
35
- val trackPlaceable = mesuarables
36
- .first { it.layoutId == SliderLayoutElements .Track }
37
- .measure(constraints)
38
-
39
39
val thumbPlaceable = mesuarables
40
40
.first { it.layoutId == SliderLayoutElements .Thumb }
41
41
.measure(constraints)
42
42
43
- val sliderHeight = max(trackPlaceable.height, thumbPlaceable.height)
43
+ val trackPlaceable = mesuarables
44
+ .first { it.layoutId == SliderLayoutElements .Track }
45
+ .measure(constraints.offset(
46
+ horizontal = - thumbPlaceable.width
47
+ ).copy(minHeight = 0 ))
48
+
49
+
50
+ val sliderHeight = max(thumbPlaceable.height, trackPlaceable.height)
44
51
val sliderWidth = trackPlaceable.width
45
52
layout(sliderWidth, sliderHeight) {
46
- val trackY = sliderHeight / 2 - trackPlaceable.height / 2
47
- val thumbY = sliderHeight / 2 - thumbPlaceable.height / 2
48
- val thumbX = (sliderWidth * progress).roundToInt() - thumbPlaceable.width / 2
49
- trackPlaceable.place(0 , trackY)
50
- thumbPlaceable.place(thumbX, thumbY)
53
+ val thumbX = (trackPlaceable.width * progress).roundToInt() - thumbPlaceable.width / 2
54
+ val trackY = sliderHeight - trackPlaceable.height
55
+ trackPlaceable.placeRelative(0 , trackY / 2 )
56
+ thumbPlaceable.place(thumbX, 0 )
51
57
}
52
58
}
53
59
}
@@ -62,18 +68,21 @@ internal enum class SliderLayoutElements {
62
68
@Composable
63
69
private fun PreviewSliderLayout () {
64
70
SliderLayout (
71
+ modifier = Modifier .fillMaxWidth(1f ),
65
72
progress = 0.5f ,
66
73
track = {
67
74
Box (
68
75
modifier = Modifier
69
76
.background(Color .Yellow )
77
+ .height(15 .dp)
70
78
.fillMaxWidth()
71
- .height(20 .dp)
72
79
)
73
80
},
74
81
thumb = {
75
- Box (modifier = Modifier
76
- .background(Color .Blue , shape = CircleShape )
77
- .size(50 .dp))
82
+ Box (
83
+ modifier = Modifier
84
+ .size(32 .dp)
85
+ .background(Color .Black , shape = CircleShape )
86
+ )
78
87
})
79
88
}
0 commit comments