Skip to content

Commit 4235aa7

Browse files
committed
fix(Slider): Add requiredSize for the SliderLayout
1 parent a96014d commit 4235aa7

File tree

2 files changed

+26
-15
lines changed

2 files changed

+26
-15
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,6 @@ import androidx.compose.ui.unit.dp
55
internal object SliderDefaults {
66
val TrackHeight = 4.dp
77
val ThumbSize = 16.dp
8+
val HandleHeight = 16.0.dp
9+
val HandleWidth = 20.0.dp
810
}

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

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import androidx.compose.foundation.background
44
import androidx.compose.foundation.layout.Box
55
import androidx.compose.foundation.layout.fillMaxWidth
66
import androidx.compose.foundation.layout.height
7+
import androidx.compose.foundation.layout.requiredSizeIn
78
import androidx.compose.foundation.layout.size
89
import androidx.compose.foundation.shape.CircleShape
910
import androidx.compose.runtime.Composable
@@ -13,16 +14,19 @@ import androidx.compose.ui.layout.Layout
1314
import androidx.compose.ui.layout.layoutId
1415
import androidx.compose.ui.tooling.preview.Preview
1516
import androidx.compose.ui.unit.dp
17+
import androidx.compose.ui.unit.offset
1618
import kotlin.math.max
1719
import kotlin.math.roundToInt
1820

1921
@Composable
20-
internal fun SliderLayout(
22+
public fun SliderLayout(
2123
progress: Float,
24+
modifier: Modifier = Modifier,
2225
track: @Composable () -> Unit,
2326
thumb: @Composable () -> Unit
2427
) {
2528
Layout(
29+
modifier = modifier.requiredSizeIn(SliderDefaults.HandleWidth, SliderDefaults.HandleHeight),
2630
content = {
2731
Box(modifier = Modifier.layoutId(SliderLayoutElements.Track)) {
2832
track()
@@ -32,22 +36,24 @@ internal fun SliderLayout(
3236
}
3337
}) { mesuarables, constraints ->
3438

35-
val trackPlaceable = mesuarables
36-
.first { it.layoutId == SliderLayoutElements.Track }
37-
.measure(constraints)
38-
3939
val thumbPlaceable = mesuarables
4040
.first { it.layoutId == SliderLayoutElements.Thumb }
4141
.measure(constraints)
4242

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)
4451
val sliderWidth = trackPlaceable.width
4552
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)
5157
}
5258
}
5359
}
@@ -62,18 +68,21 @@ internal enum class SliderLayoutElements {
6268
@Composable
6369
private fun PreviewSliderLayout() {
6470
SliderLayout(
71+
modifier = Modifier.fillMaxWidth(1f),
6572
progress = 0.5f,
6673
track = {
6774
Box(
6875
modifier = Modifier
6976
.background(Color.Yellow)
77+
.height(15.dp)
7078
.fillMaxWidth()
71-
.height(20.dp)
7279
)
7380
},
7481
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+
)
7887
})
7988
}

0 commit comments

Comments
 (0)