Skip to content

Commit 6452ed1

Browse files
committed
library: Add Slider background indication
1 parent 0c4c8f2 commit 6452ed1

File tree

1 file changed

+29
-10
lines changed
  • miuix/src/commonMain/kotlin/top/yukonga/miuix/kmp/basic

1 file changed

+29
-10
lines changed

miuix/src/commonMain/kotlin/top/yukonga/miuix/kmp/basic/Slider.kt

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
package top.yukonga.miuix.kmp.basic
22

3+
import androidx.compose.animation.animateColorAsState
4+
import androidx.compose.animation.core.animateFloatAsState
5+
import androidx.compose.animation.core.spring
6+
import androidx.compose.animation.core.tween
37
import androidx.compose.foundation.Canvas
8+
import androidx.compose.foundation.LocalIndication
49
import androidx.compose.foundation.background
10+
import androidx.compose.foundation.clickable
511
import androidx.compose.foundation.gestures.detectHorizontalDragGestures
12+
import androidx.compose.foundation.indication
13+
import androidx.compose.foundation.interaction.MutableInteractionSource
14+
import androidx.compose.foundation.interaction.collectIsPressedAsState
615
import androidx.compose.foundation.layout.fillMaxWidth
716
import androidx.compose.foundation.layout.height
817
import androidx.compose.runtime.Composable
@@ -16,6 +25,8 @@ import androidx.compose.runtime.setValue
1625
import androidx.compose.ui.Alignment
1726
import androidx.compose.ui.Modifier
1827
import androidx.compose.ui.draw.clip
28+
import androidx.compose.ui.draw.drawBehind
29+
import androidx.compose.ui.draw.drawWithContent
1930
import androidx.compose.ui.geometry.CornerRadius
2031
import androidx.compose.ui.geometry.Offset
2132
import androidx.compose.ui.geometry.Size
@@ -99,6 +110,7 @@ fun Slider(
99110
}
100111
)
101112
}
113+
.indication(remember { MutableInteractionSource() }, LocalIndication.current)
102114
} else {
103115
modifier
104116
},
@@ -107,12 +119,13 @@ fun Slider(
107119
SliderBackground(
108120
modifier = Modifier.fillMaxWidth().height(height),
109121
height = height,
110-
backgroundColor = colors.backgroundColor(enabled),
122+
backgroundColor = colors.backgroundColor(),
111123
foregroundColor = colors.foregroundColor(enabled),
112124
effect = effect,
113125
progress = progress,
114126
minValue = minValue,
115127
maxValue = maxValue,
128+
isDragging = isDragging,
116129
)
117130
}
118131
}
@@ -127,8 +140,16 @@ fun SliderBackground(
127140
progress: Float,
128141
minValue: Float,
129142
maxValue: Float,
143+
isDragging: Boolean,
130144
) {
131-
Canvas(modifier = modifier.clip(SmoothRoundedCornerShape(height)).background(backgroundColor)) {
145+
val backgroundAlpha = animateFloatAsState(
146+
targetValue = if (isDragging) 0.044f else 0f,
147+
animationSpec = tween(150)
148+
).value
149+
Canvas(
150+
modifier = modifier.clip(SmoothRoundedCornerShape(height)).background(backgroundColor)
151+
.drawBehind { drawRect(Color.Black.copy(alpha = backgroundAlpha)) }
152+
) {
132153
val barHeight = size.height
133154
val barWidth = size.width
134155
val progressWidth = barWidth * ((progress - minValue) / (maxValue - minValue))
@@ -157,14 +178,12 @@ object SliderDefaults {
157178
fun sliderColors(
158179
foregroundColor: Color = MiuixTheme.colorScheme.primary,
159180
disabledForegroundColor: Color = MiuixTheme.colorScheme.disabledPrimarySlider,
160-
backgroundColor: Color = MiuixTheme.colorScheme.tertiaryContainerVariant,
161-
disabledBackgroundColor: Color = MiuixTheme.colorScheme.disabledSecondary
181+
backgroundColor: Color = MiuixTheme.colorScheme.tertiaryContainerVariant
162182
): SliderColors {
163183
return SliderColors(
164184
foregroundColor = foregroundColor,
165185
disabledForegroundColor = disabledForegroundColor,
166-
backgroundColor = backgroundColor,
167-
disabledBackgroundColor = disabledBackgroundColor
186+
backgroundColor = backgroundColor
168187
)
169188
}
170189
}
@@ -174,12 +193,12 @@ object SliderDefaults {
174193
class SliderColors(
175194
private val foregroundColor: Color,
176195
private val disabledForegroundColor: Color,
177-
private val backgroundColor: Color,
178-
private val disabledBackgroundColor: Color
196+
private val backgroundColor: Color
179197
) {
180198
@Stable
181-
internal fun foregroundColor(enabled: Boolean): Color = if (enabled) foregroundColor else disabledForegroundColor
199+
internal fun foregroundColor(enabled: Boolean): Color =
200+
if (enabled) foregroundColor else disabledForegroundColor
182201

183202
@Stable
184-
internal fun backgroundColor(enabled: Boolean): Color = if (enabled) backgroundColor else disabledBackgroundColor
203+
internal fun backgroundColor(): Color = backgroundColor
185204
}

0 commit comments

Comments
 (0)