|
|
@@ -53,6 +53,8 @@ fun SliderSettingItem(
|
|
|
valueRange: ClosedFloatingPointRange<Float>,
|
|
|
steps: Int = 0,
|
|
|
description: String? = null,
|
|
|
+ valueLabel: ((Float) -> String)? = null,
|
|
|
+ decimal: Boolean = false,
|
|
|
onValueChange: (Float) -> Unit
|
|
|
) {
|
|
|
|
|
|
@@ -61,16 +63,30 @@ fun SliderSettingItem(
|
|
|
var sliderValue by remember(value) { mutableFloatStateOf(value) }
|
|
|
val textFieldState = rememberTextFieldState()
|
|
|
|
|
|
+ // 默认整数模式:滑动吸附到整数;decimal = true 时保留 0.1 精度
|
|
|
+ fun snap(v: Float): Float =
|
|
|
+ if (decimal) (v * 10).roundToInt() / 10f else v.roundToInt().toFloat()
|
|
|
+
|
|
|
+ fun format(v: Float): String =
|
|
|
+ if (v % 1f == 0f) v.toInt().toString() else v.toString()
|
|
|
+
|
|
|
LaunchedEffect(value) {
|
|
|
sliderValue = value
|
|
|
}
|
|
|
|
|
|
- val sliderAccessibilityValue = description ?: sliderValue.toString()
|
|
|
+ // 拖动过程中让标题下的数值实时跟随滑块,松手后才真正应用
|
|
|
+ val displayDescription = when {
|
|
|
+ valueLabel != null -> valueLabel(sliderValue)
|
|
|
+ sliderValue != value -> format(sliderValue)
|
|
|
+ else -> description
|
|
|
+ }
|
|
|
+
|
|
|
+ val sliderAccessibilityValue = displayDescription ?: sliderValue.toString()
|
|
|
|
|
|
LaunchedEffect(isInputMode) {
|
|
|
if (isInputMode) {
|
|
|
textFieldState.edit {
|
|
|
- replace(0, length, value.toString())
|
|
|
+ replace(0, length, format(value))
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
@@ -78,8 +94,7 @@ fun SliderSettingItem(
|
|
|
fun commitValue() {
|
|
|
if (isInputMode) {
|
|
|
textFieldState.text.toString().toFloatOrNull()?.let { num ->
|
|
|
- val rounded = (num * 10).roundToInt() / 10f
|
|
|
- onValueChange(rounded.coerceIn(valueRange))
|
|
|
+ onValueChange(snap(num).coerceIn(valueRange))
|
|
|
}
|
|
|
} else if (sliderValue != value) {
|
|
|
onValueChange(sliderValue)
|
|
|
@@ -96,7 +111,7 @@ fun SliderSettingItem(
|
|
|
) {
|
|
|
BasicComponent(
|
|
|
title = title,
|
|
|
- summary = description,
|
|
|
+ summary = displayDescription,
|
|
|
onClick = {
|
|
|
if (expanded) {
|
|
|
commitValue()
|
|
|
@@ -131,9 +146,9 @@ fun SliderSettingItem(
|
|
|
MiuixSlider(
|
|
|
value = sliderValue,
|
|
|
onValueChange = {
|
|
|
- sliderValue = (it * 10).roundToInt() / 10f
|
|
|
+ sliderValue = snap(it)
|
|
|
textFieldState.edit {
|
|
|
- replace(0, length, sliderValue.toString())
|
|
|
+ replace(0, length, format(sliderValue))
|
|
|
}
|
|
|
},
|
|
|
onValueChangeFinished = {
|
|
|
@@ -157,11 +172,7 @@ fun SliderSettingItem(
|
|
|
onConfirm = {
|
|
|
onValueChange(defaultValue)
|
|
|
textFieldState.edit {
|
|
|
- replace(
|
|
|
- 0,
|
|
|
- length,
|
|
|
- defaultValue.toInt().toString()
|
|
|
- )
|
|
|
+ replace(0, length, format(defaultValue))
|
|
|
}
|
|
|
},
|
|
|
dismissText = if (isInputMode) {
|
|
|
@@ -178,7 +189,7 @@ fun SliderSettingItem(
|
|
|
} else {
|
|
|
SettingItem(
|
|
|
title = title,
|
|
|
- option = description,
|
|
|
+ option = displayDescription,
|
|
|
expanded = expanded,
|
|
|
onExpandChange = {
|
|
|
if (expanded) {
|
|
|
@@ -220,9 +231,9 @@ fun SliderSettingItem(
|
|
|
Slider(
|
|
|
value = sliderValue,
|
|
|
onValueChange = {
|
|
|
- sliderValue = (it * 10).roundToInt() / 10f
|
|
|
+ sliderValue = snap(it)
|
|
|
textFieldState.edit {
|
|
|
- replace(0, length, sliderValue.toString())
|
|
|
+ replace(0, length, format(sliderValue))
|
|
|
}
|
|
|
},
|
|
|
onValueChangeFinished = {
|
|
|
@@ -247,11 +258,7 @@ fun SliderSettingItem(
|
|
|
onConfirm = {
|
|
|
onValueChange(defaultValue)
|
|
|
textFieldState.edit {
|
|
|
- replace(
|
|
|
- 0,
|
|
|
- length,
|
|
|
- defaultValue.toInt().toString()
|
|
|
- )
|
|
|
+ replace(0, length, format(defaultValue))
|
|
|
}
|
|
|
},
|
|
|
dismissText = if (isInputMode) {
|