diff --git a/app/src/main/java/com/example/smarthome/ui/MainScaffold.kt b/app/src/main/java/com/example/smarthome/ui/MainScaffold.kt index 49c4748..c451f51 100644 --- a/app/src/main/java/com/example/smarthome/ui/MainScaffold.kt +++ b/app/src/main/java/com/example/smarthome/ui/MainScaffold.kt @@ -40,6 +40,7 @@ import android.view.SoundEffectConstants import androidx.compose.foundation.horizontalScroll import androidx.compose.foundation.rememberScrollState import androidx.compose.foundation.gestures.detectTapGestures +import androidx.compose.foundation.gestures.detectHorizontalDragGestures import androidx.compose.ui.input.pointer.pointerInput import androidx.compose.foundation.layout.offset import androidx.compose.ui.draw.rotate @@ -1342,23 +1343,47 @@ fun LightList(modifier: Modifier = Modifier) { @Composable fun DotsProgress(percent: Float, onPercentChange: (Float) -> Unit = {}, enabled: Boolean = true) { val total = 12 + Row( horizontalArrangement = Arrangement.spacedBy(4.dp), verticalAlignment = Alignment.CenterVertically, - modifier = Modifier.pointerInput(enabled) { - if (enabled) { - detectTapGestures { offset -> - // 计算点击的是第几个点 - val dotWidth = 6.dp.toPx() + modifier = Modifier + .pointerInput(enabled) { + if (enabled) { + val dotWidth = 8.dp.toPx() val spacing = 4.dp.toPx() val totalWidth = (dotWidth + spacing) * total - spacing - val clickedIndex = ((offset.x / totalWidth) * total).toInt().coerceIn(0, total - 1) - // 更新百分比 - val newPercent = (clickedIndex + 1).toFloat() / total - onPercentChange(newPercent) + + // 支持拖动手势 + detectHorizontalDragGestures( + onDragStart = { offset -> + // 开始拖动时立即更新 + val newPercent = (offset.x / totalWidth).coerceIn(0f, 1f) + onPercentChange(newPercent) + }, + onDragEnd = { }, + onDragCancel = { }, + onHorizontalDrag = { change, _ -> + change.consume() + val newPercent = (change.position.x / totalWidth).coerceIn(0f, 1f) + onPercentChange(newPercent) + } + ) + } + } + .pointerInput(enabled) { + if (enabled) { + val dotWidth = 8.dp.toPx() + val spacing = 4.dp.toPx() + val totalWidth = (dotWidth + spacing) * total - spacing + + // 也支持点击 + detectTapGestures { offset -> + val newPercent = (offset.x / totalWidth).coerceIn(0f, 1f) + onPercentChange(newPercent) + } } } - } ) { repeat(total) { i -> val filled = i < (percent * total).toInt() @@ -1367,11 +1392,6 @@ fun DotsProgress(percent: Float, onPercentChange: (Float) -> Unit = {}, enabled: .size(8.dp) .clip(RoundedCornerShape(4.dp)) .background(if (filled) Color(0xFFA9F0FF) else Color(0xFF3A3A3A)) - .clickable(enabled = enabled) { - // 点击某个点直接设置到该亮度 - val newPercent = (i + 1).toFloat() / total - onPercentChange(newPercent) - } ) } }