33
44package top.yukonga.miuix.kmp.basic
55
6- import androidx.compose.foundation.Canvas
76import androidx.compose.foundation.background
87import androidx.compose.foundation.border
98import androidx.compose.foundation.gestures.detectHorizontalDragGestures
109import androidx.compose.foundation.layout.Arrangement
1110import androidx.compose.foundation.layout.Box
1211import androidx.compose.foundation.layout.Column
13- import androidx.compose.foundation.layout.fillMaxSize
1412import androidx.compose.foundation.layout.fillMaxWidth
1513import androidx.compose.foundation.layout.height
1614import androidx.compose.foundation.layout.offset
@@ -25,6 +23,8 @@ import androidx.compose.runtime.setValue
2523import androidx.compose.ui.Alignment
2624import androidx.compose.ui.Modifier
2725import androidx.compose.ui.draw.clip
26+ import androidx.compose.ui.draw.drawBehind
27+ import androidx.compose.ui.geometry.CornerRadius
2828import androidx.compose.ui.graphics.Brush
2929import androidx.compose.ui.graphics.Color
3030import androidx.compose.ui.graphics.ImageBitmap
@@ -33,10 +33,12 @@ import androidx.compose.ui.graphics.Paint
3333import androidx.compose.ui.graphics.ShaderBrush
3434import androidx.compose.ui.graphics.TileMode
3535import androidx.compose.ui.graphics.drawscope.DrawScope
36+ import androidx.compose.ui.graphics.drawscope.Stroke
3637import androidx.compose.ui.input.pointer.pointerInput
3738import androidx.compose.ui.layout.onGloballyPositioned
3839import androidx.compose.ui.platform.LocalDensity
3940import androidx.compose.ui.platform.LocalHapticFeedback
41+ import androidx.compose.ui.unit.Density
4042import androidx.compose.ui.unit.Dp
4143import androidx.compose.ui.unit.dp
4244import top.yukonga.miuix.kmp.utils.ColorUtils
@@ -207,7 +209,10 @@ fun SaturationSlider(
207209 hapticEffect : SliderDefaults .SliderHapticEffect = SliderDefaults .DefaultHapticEffect
208210) {
209211 val saturationColors = remember(currentHue) {
210- listOf (Color .hsv(currentHue, 0f , 1f , 1f ), Color .hsv(currentHue, 1f , 1f , 1f ))
212+ listOf (
213+ Color .hsv(currentHue, 0f , 1f , 1f ),
214+ Color .hsv(currentHue, 1f , 1f , 1f )
215+ )
211216 }
212217 ColorSlider (
213218 value = currentSaturation,
@@ -284,11 +289,16 @@ fun AlphaSlider(
284289 drawBrushColors = alphaColors,
285290 modifier = Modifier .fillMaxWidth(),
286291 hapticEffect = hapticEffect,
287- drawBackground = { _, _ -> drawRect(brush = checkerBrush) }
292+ drawBackground = { _, _ ->
293+ drawRoundRect(
294+ brush = checkerBrush,
295+ cornerRadius = CornerRadius (size.height / 2 , size.height / 2 )
296+ )
297+ }
288298 )
289299}
290300
291- private fun createCheckerboardBrush (density : androidx.compose.ui.unit. Density ): ShaderBrush {
301+ private fun createCheckerboardBrush (density : Density ): ShaderBrush {
292302 val lightColor = Color (0xFFCCCCCC )
293303 val darkColor = Color (0xFFAAAAAA )
294304 val checkerSizeDp = 3 .dp
@@ -327,52 +337,61 @@ private fun ColorSlider(
327337 val indicatorSizeDp = 20 .dp
328338 val sliderHeightDp = 26 .dp
329339 val sliderHeightPx = with (density) { sliderHeightDp.toPx() }
330- val sliderWidthPx = with (density) { sliderWidth.toPx() }
331- val halfSliderHeightPx = sliderHeightPx / 2f
332340 val hapticFeedback = LocalHapticFeedback .current
333341 val hapticState = remember { SliderHapticState () }
334342
335- val gradientBrush = remember(drawBrushColors, sliderWidthPx, halfSliderHeightPx) {
343+ val gradientBrush = remember(drawBrushColors, sliderWidth) {
344+ val widthPx = with (density) { sliderWidth.toPx() }
345+ val halfSliderHeightPx = with (density) { sliderHeightDp.toPx() } / 2f
336346 Brush .horizontalGradient(
337347 colors = drawBrushColors,
338348 startX = halfSliderHeightPx,
339- endX = sliderWidthPx - halfSliderHeightPx,
349+ endX = widthPx - halfSliderHeightPx,
340350 tileMode = TileMode .Clamp
341351 )
342352 }
343353
344354 Box (
345355 modifier = modifier
346356 .height(sliderHeightDp)
347- .clip(SmoothRoundedCornerShape (50 .dp))
348- .border(0.5 .dp, Color .Gray .copy(0.1f ), SmoothRoundedCornerShape (50 .dp))
357+ .onGloballyPositioned { coordinates ->
358+ sliderWidth = with (density) { coordinates.size.width.toDp() }
359+ }
360+ .drawBehind {
361+ drawBackground?.invoke(this , size.width, size.height)
362+ drawRoundRect(
363+ brush = gradientBrush,
364+ cornerRadius = CornerRadius (size.height / 2 , size.height / 2 )
365+ )
366+ drawRoundRect(
367+ color = Color .Gray .copy(0.1f ),
368+ style = Stroke (width = with (density) { 0.5 .dp.toPx() }),
369+ cornerRadius = CornerRadius (size.height / 2 , size.height / 2 )
370+ )
371+ }
372+ .pointerInput(Unit ) {
373+ detectHorizontalDragGestures(
374+ onDragStart = { offset ->
375+ val newValue =
376+ handleSliderInteraction(offset.x, size.width.toFloat(), with (density) { sliderHeightDp.toPx() }).coerceIn(
377+ 0f ,
378+ 1f
379+ )
380+ onValueChanged(newValue)
381+ hapticState.reset(newValue)
382+ },
383+ onHorizontalDrag = { change, _ ->
384+ change.consume()
385+ val newValue = handleSliderInteraction(
386+ change.position.x,
387+ size.width.toFloat(),
388+ with (density) { sliderHeightDp.toPx() }).coerceIn(0f , 1f )
389+ onValueChanged(newValue)
390+ hapticState.handleHapticFeedback(newValue, hapticEffect, hapticFeedback)
391+ }
392+ )
393+ }
349394 ) {
350- Canvas (
351- modifier = Modifier
352- .fillMaxSize()
353- .onGloballyPositioned { coordinates ->
354- sliderWidth = with (density) { coordinates.size.width.toDp() }
355- }
356- .pointerInput(Unit ) {
357- detectHorizontalDragGestures(
358- onDragStart = { offset ->
359- val newValue = handleSliderInteraction(offset.x, size.width.toFloat(), sliderHeightPx).coerceIn(0f , 1f )
360- onValueChanged(newValue)
361- hapticState.reset(newValue)
362- },
363- onHorizontalDrag = { change, _ ->
364- change.consume()
365- val newValue = handleSliderInteraction(change.position.x, size.width.toFloat(), sliderHeightPx).coerceIn(0f , 1f )
366- onValueChanged(newValue)
367- hapticState.handleHapticFeedback(newValue, hapticEffect, hapticFeedback)
368- }
369- )
370- }
371- ) {
372- drawBackground?.invoke(this , size.width, size.height)
373- drawRect(gradientBrush)
374- }
375-
376395 SliderIndicator (
377396 modifier = Modifier .align(Alignment .CenterStart ),
378397 value = value,
0 commit comments