Skip to content

Commit ec76916

Browse files
committed
feat(Slider): Add basic slider track with disabled range and colors
1 parent 8d71d84 commit ec76916

File tree

7 files changed

+95
-2
lines changed

7 files changed

+95
-2
lines changed

demo/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ android {
2929
compose = true
3030
}
3131
composeOptions {
32-
kotlinCompilerExtensionVersion = "1.5.1"
32+
kotlinCompilerExtensionVersion = libs.versions.composeCompiler.get()
3333
}
3434
packaging {
3535
resources {

gradle/libs.versions.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ junitVersion = "1.1.5"
77
espresso = "3.5.1"
88
lifecyle = "2.7.0"
99
activityCompose = "1.8.2"
10+
composeCompiler = "1.5.1"
1011
composeBom = "2024.02.00"
1112
spotless="6.25.0"
1213
detekt="1.23.5"
1314

1415
[libraries]
16+
kotlin_gradle_plugin = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin", version.ref = "kotlin"}
1517
android_gradle_plugin = { module = "com.android.tools.build:gradle", version.ref = "agp" }
1618
androidx-core-ktx = { module = "androidx.core:core-ktx", version.ref = "coreKtx" }
1719
junit = { module = "junit:junit", version.ref = "junit" }
@@ -23,6 +25,7 @@ androidx-activity-compose = { module = "androidx.activity:activity-compose", ver
2325
compose-bom = { module = "androidx.compose:compose-bom", version.ref = "composeBom" }
2426
compose-ui = { module = "androidx.compose.ui:ui" }
2527
compose-ui-graphics = { module = "androidx.compose.ui:ui-graphics" }
28+
compose-foundation = { module = "androidx.compose.foundation:foundation" }
2629
compose-ui-tooling = { module = "androidx.compose.ui:ui-tooling" }
2730
compose-ui-tooling-preview = { module = "androidx.compose.ui:ui-tooling-preview" }
2831
compose-ui-test-manifest = { module = "androidx.compose.ui:ui-test-manifest" }

lib/build.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@ android {
2020
}
2121
}
2222
}
23+
2324
kotlin {
2425
jvmToolchain(17)
26+
explicitApi()
2527
}
2628

2729
dependencies {

slider/build.gradle.kts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,29 @@ android {
1919
)
2020
}
2121
}
22+
23+
buildFeatures {
24+
compose = true
25+
}
26+
27+
composeOptions {
28+
kotlinCompilerExtensionVersion = libs.versions.composeCompiler.get()
29+
}
2230
}
2331

2432
kotlin {
2533
jvmToolchain(17)
34+
explicitApi()
2635
}
2736

2837
dependencies {
29-
3038
implementation(libs.androidx.core.ktx)
3139
implementation(platform(libs.compose.bom))
3240
implementation(libs.compose.ui)
41+
implementation(libs.compose.ui.graphics)
3342
implementation(libs.compose.ui.tooling.preview)
43+
implementation(libs.compose.ui.tooling)
44+
implementation(libs.compose.foundation)
3445
testImplementation(libs.junit)
3546
androidTestImplementation(libs.androidx.junit)
3647
androidTestImplementation(libs.androidx.espresso.core)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package io.monstarlab.mosaic.slider
2+
3+
import androidx.compose.ui.graphics.Color
4+
5+
public data class SliderColors(
6+
val active: Color,
7+
val disabled: Color = active.copy(alpha = 0.2f),
8+
val inactive: Color = active.copy(alpha = 0.5f),
9+
)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package io.monstarlab.mosaic.slider
2+
3+
import androidx.compose.ui.unit.dp
4+
5+
internal object SliderDefaults {
6+
val TrackHeight = 4.dp
7+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package io.monstarlab.mosaic.slider
2+
3+
import androidx.compose.foundation.Canvas
4+
import androidx.compose.foundation.layout.fillMaxWidth
5+
import androidx.compose.foundation.layout.height
6+
import androidx.compose.runtime.Composable
7+
import androidx.compose.ui.Modifier
8+
import androidx.compose.ui.geometry.Offset
9+
import androidx.compose.ui.geometry.Size
10+
import androidx.compose.ui.graphics.Color
11+
import androidx.compose.ui.tooling.preview.Preview
12+
13+
@Composable
14+
internal fun SliderTrack(
15+
progress: Float,
16+
colors: SliderColors,
17+
modifier: Modifier = Modifier,
18+
disabledRange: ClosedFloatingPointRange<Float> = 0f..0f,
19+
) {
20+
21+
check(progress in 0f..1f) { "Invalid progress value should be between 0 and 1" }
22+
Canvas(
23+
modifier = modifier
24+
.fillMaxWidth()
25+
.height(SliderDefaults.TrackHeight)
26+
) {
27+
val activeRectWidth = size.width * progress
28+
drawRect(
29+
color = colors.active,
30+
topLeft = Offset.Zero,
31+
size = Size(activeRectWidth, size.height)
32+
)
33+
34+
drawRect(
35+
color = colors.inactive,
36+
topLeft = Offset(activeRectWidth, 0f),
37+
size = Size(size.width - activeRectWidth, size.height)
38+
)
39+
40+
if (!disabledRange.isEmpty()) {
41+
val disabledStart = size.width * disabledRange.start
42+
val disabledEnd = size.width * disabledRange.endInclusive
43+
drawRect(
44+
color = colors.disabled,
45+
topLeft = Offset(size.width * disabledRange.start, 0f),
46+
size = Size(disabledEnd - disabledStart, size.height)
47+
)
48+
}
49+
}
50+
}
51+
52+
53+
@Preview
54+
@Composable
55+
private fun PreviewSliderTrack() {
56+
SliderTrack(
57+
progress = 0.5f,
58+
colors = SliderColors(Color.Red),
59+
disabledRange = 0.8f..1f
60+
)
61+
}

0 commit comments

Comments
 (0)