-
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 slider track with disabled range and colors
- Loading branch information
Showing
7 changed files
with
95 additions
and
2 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
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 |
---|---|---|
|
@@ -20,8 +20,10 @@ android { | |
} | ||
} | ||
} | ||
|
||
kotlin { | ||
jvmToolchain(17) | ||
explicitApi() | ||
} | ||
|
||
dependencies { | ||
|
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
9 changes: 9 additions & 0 deletions
9
slider/src/main/java/io/monstarlab/mosaic/slider/SliderColors.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,9 @@ | ||
package io.monstarlab.mosaic.slider | ||
|
||
import androidx.compose.ui.graphics.Color | ||
|
||
public data class SliderColors( | ||
val active: Color, | ||
val disabled: Color = active.copy(alpha = 0.2f), | ||
val inactive: Color = active.copy(alpha = 0.5f), | ||
) |
7 changes: 7 additions & 0 deletions
7
slider/src/main/java/io/monstarlab/mosaic/slider/SliderDefaults.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,7 @@ | ||
package io.monstarlab.mosaic.slider | ||
|
||
import androidx.compose.ui.unit.dp | ||
|
||
internal object SliderDefaults { | ||
val TrackHeight = 4.dp | ||
} |
61 changes: 61 additions & 0 deletions
61
slider/src/main/java/io/monstarlab/mosaic/slider/SliderTrack.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,61 @@ | ||
package io.monstarlab.mosaic.slider | ||
|
||
import androidx.compose.foundation.Canvas | ||
import androidx.compose.foundation.layout.fillMaxWidth | ||
import androidx.compose.foundation.layout.height | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.geometry.Offset | ||
import androidx.compose.ui.geometry.Size | ||
import androidx.compose.ui.graphics.Color | ||
import androidx.compose.ui.tooling.preview.Preview | ||
|
||
@Composable | ||
internal fun SliderTrack( | ||
progress: Float, | ||
colors: SliderColors, | ||
modifier: Modifier = Modifier, | ||
disabledRange: ClosedFloatingPointRange<Float> = 0f..0f, | ||
) { | ||
|
||
check(progress in 0f..1f) { "Invalid progress value should be between 0 and 1" } | ||
Canvas( | ||
modifier = modifier | ||
.fillMaxWidth() | ||
.height(SliderDefaults.TrackHeight) | ||
) { | ||
val activeRectWidth = size.width * progress | ||
drawRect( | ||
color = colors.active, | ||
topLeft = Offset.Zero, | ||
size = Size(activeRectWidth, size.height) | ||
) | ||
|
||
drawRect( | ||
color = colors.inactive, | ||
topLeft = Offset(activeRectWidth, 0f), | ||
size = Size(size.width - activeRectWidth, size.height) | ||
) | ||
|
||
if (!disabledRange.isEmpty()) { | ||
val disabledStart = size.width * disabledRange.start | ||
val disabledEnd = size.width * disabledRange.endInclusive | ||
drawRect( | ||
color = colors.disabled, | ||
topLeft = Offset(size.width * disabledRange.start, 0f), | ||
size = Size(disabledEnd - disabledStart, size.height) | ||
) | ||
} | ||
} | ||
} | ||
|
||
|
||
@Preview | ||
@Composable | ||
private fun PreviewSliderTrack() { | ||
SliderTrack( | ||
progress = 0.5f, | ||
colors = SliderColors(Color.Red), | ||
disabledRange = 0.8f..1f | ||
) | ||
} |