-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Slider): add basic SliderLayout to position slider elements
- Loading branch information
Showing
7 changed files
with
208 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
demo/src/main/java/io/monstarlab/mosaic/features/SliderDemo.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package io.monstarlab.mosaic.features | ||
|
||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.foundation.shape.RoundedCornerShape | ||
import androidx.compose.material3.Scaffold | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.draw.clip | ||
import androidx.compose.ui.graphics.Color | ||
import androidx.compose.ui.tooling.preview.Preview | ||
import androidx.compose.ui.unit.dp | ||
import io.monstarlab.mosaic.slider.Slider | ||
import io.monstarlab.mosaic.slider.SliderColors | ||
|
||
@Composable | ||
fun SliderDemo() = Scaffold(modifier = Modifier) { | ||
Column(modifier = Modifier.padding(it).padding(16.dp)) { | ||
Slider( | ||
value = 0.5f, | ||
onValueChange = {}, | ||
colors = SliderColors(Color.Red), | ||
modifier = Modifier.clip(RoundedCornerShape(2.dp)) | ||
) | ||
} | ||
} | ||
|
||
@Preview | ||
@Composable | ||
private fun PreviewSliderDemo() { | ||
SliderDemo() | ||
} |
62 changes: 62 additions & 0 deletions
62
slider/src/main/java/io/monstarlab/mosaic/slider/Slider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package io.monstarlab.mosaic.slider | ||
|
||
import androidx.compose.foundation.background | ||
import androidx.compose.foundation.layout.Arrangement | ||
import androidx.compose.foundation.layout.Box | ||
import androidx.compose.foundation.layout.BoxScope | ||
import androidx.compose.foundation.layout.BoxWithConstraints | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.fillMaxSize | ||
import androidx.compose.foundation.layout.fillMaxWidth | ||
import androidx.compose.foundation.layout.size | ||
import androidx.compose.foundation.shape.CircleShape | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.graphics.Color | ||
import androidx.compose.ui.layout.Layout | ||
import androidx.compose.ui.tooling.preview.Preview | ||
|
||
@Composable | ||
public fun Slider( | ||
value: Float, | ||
onValueChange: (Float) -> Unit, | ||
colors: SliderColors, | ||
modifier: Modifier = Modifier, | ||
thumb: @Composable () -> Unit = { DefaultSliderThumb(colors = colors) } | ||
) { | ||
|
||
SliderLayout( | ||
progress = value, | ||
thumb = thumb, | ||
track = { | ||
SliderTrack( | ||
modifier = modifier, | ||
progress = value, | ||
colors = colors, | ||
) | ||
} | ||
) | ||
} | ||
|
||
@Composable | ||
internal fun DefaultSliderThumb(colors: SliderColors) { | ||
Box( | ||
modifier = Modifier | ||
.size(SliderDefaults.ThumbSize) | ||
.background( | ||
color = colors.active, | ||
shape = CircleShape | ||
) | ||
) | ||
} | ||
|
||
@Preview | ||
@Composable | ||
private fun PreviewSlider() { | ||
Slider( | ||
value = 0.5f, | ||
onValueChange = {}, | ||
colors = SliderColors(Color.Yellow) | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
slider/src/main/java/io/monstarlab/mosaic/slider/SliderLayout.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package io.monstarlab.mosaic.slider | ||
|
||
import androidx.compose.foundation.background | ||
import androidx.compose.foundation.layout.Box | ||
import androidx.compose.foundation.layout.fillMaxWidth | ||
import androidx.compose.foundation.layout.height | ||
import androidx.compose.foundation.layout.size | ||
import androidx.compose.foundation.shape.CircleShape | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.graphics.Color | ||
import androidx.compose.ui.layout.Layout | ||
import androidx.compose.ui.layout.layoutId | ||
import androidx.compose.ui.tooling.preview.Preview | ||
import androidx.compose.ui.unit.dp | ||
import kotlin.math.max | ||
import kotlin.math.roundToInt | ||
|
||
@Composable | ||
internal fun SliderLayout( | ||
progress: Float, | ||
track: @Composable () -> Unit, | ||
thumb: @Composable () -> Unit | ||
) { | ||
Layout( | ||
content = { | ||
Box(modifier = Modifier.layoutId(SliderLayoutElements.Track)) { | ||
track() | ||
} | ||
Box(modifier = Modifier.layoutId(SliderLayoutElements.Thumb)) { | ||
thumb() | ||
} | ||
}) { mesuarables, constraints -> | ||
|
||
val trackPlaceable = mesuarables | ||
.first { it.layoutId == SliderLayoutElements.Track } | ||
.measure(constraints) | ||
|
||
val thumbPlaceable = mesuarables | ||
.first { it.layoutId == SliderLayoutElements.Thumb } | ||
.measure(constraints) | ||
|
||
val sliderHeight = max(trackPlaceable.height, thumbPlaceable.height) | ||
val sliderWidth = trackPlaceable.width | ||
layout(sliderWidth, sliderHeight) { | ||
val trackY = sliderHeight / 2 - trackPlaceable.height / 2 | ||
val thumbY = sliderHeight / 2 - thumbPlaceable.height / 2 | ||
val thumbX = (sliderWidth * progress).roundToInt() - thumbPlaceable.width / 2 | ||
trackPlaceable.place(0, trackY) | ||
thumbPlaceable.place(thumbX, thumbY) | ||
} | ||
} | ||
} | ||
|
||
|
||
internal enum class SliderLayoutElements { | ||
Track, Thumb | ||
} | ||
|
||
|
||
@Preview | ||
@Composable | ||
private fun PreviewSliderLayout() { | ||
SliderLayout( | ||
progress = 0.5f, | ||
track = { | ||
Box( | ||
modifier = Modifier | ||
.background(Color.Yellow) | ||
.fillMaxWidth() | ||
.height(20.dp) | ||
) | ||
}, | ||
thumb = { | ||
Box(modifier = Modifier | ||
.background(Color.Blue, shape = CircleShape) | ||
.size(50.dp)) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters