@@ -11,8 +11,12 @@ import androidx.compose.ui.Alignment
1111import androidx.compose.ui.Modifier
1212import androidx.compose.ui.text.font.FontWeight
1313import androidx.compose.ui.unit.dp
14+ import dev.paulee.ui.App
15+ import dev.paulee.ui.Config
1416import dev.paulee.ui.LocalI18n
17+ import java.util.Locale
1518import kotlin.math.abs
19+ import kotlin.math.round
1620
1721@Composable
1822fun SliderControl (
@@ -22,6 +26,9 @@ fun SliderControl(
2226 minValue : Float ,
2327 maxValue : Float ,
2428 defaultValue : Float ,
29+ scaleFactor : Float = 1f,
30+ decimalCount : Int = 1,
31+ postfix : String = "",
2532 modifier : Modifier = Modifier ,
2633) {
2734 val locale = LocalI18n .current
@@ -61,7 +68,7 @@ fun SliderControl(
6168 border = BorderStroke (1 .dp, MaterialTheme .colors.primary.copy(alpha = 0.14f ))
6269 ) {
6370 Text (
64- text = formatSliderValue(clampedValue),
71+ text = formatSliderValue(clampedValue, scaleFactor, decimalCount, postfix ),
6572 modifier = Modifier .padding(horizontal = 12 .dp, vertical = 6 .dp),
6673 style = MaterialTheme .typography.caption.copy(fontWeight = FontWeight .SemiBold ),
6774 color = MaterialTheme .colors.primary
@@ -92,18 +99,27 @@ fun SliderControl(
9299 horizontalArrangement = Arrangement .SpaceBetween ,
93100 ) {
94101 Text (
95- text = formatSliderValue(minValue),
102+ text = formatSliderValue(minValue, scaleFactor, decimalCount, postfix ),
96103 style = MaterialTheme .typography.caption,
97104 color = supportingColor
98105 )
99106
100107 Text (
101- text = formatSliderValue(maxValue),
108+ text = formatSliderValue(maxValue, scaleFactor, decimalCount, postfix ),
102109 style = MaterialTheme .typography.caption,
103110 color = supportingColor
104111 )
105112 }
106113 }
107114}
108115
109- private fun formatSliderValue (value : Float ): String = String .format(" %.2f" , value)
116+ private fun formatSliderValue (value : Float , scaleFactor : Float , decimalCount : Int , postfix : String ): String {
117+ val pattern = buildString {
118+ append(" %." )
119+ append(decimalCount.coerceAtLeast(0 ))
120+ append(" f" )
121+ }
122+
123+ val numberAsStr = String .format(Locale .forLanguageTag(App .Language .current.tag), pattern, value * scaleFactor)
124+ return if (postfix.isNotBlank()) " $numberAsStr$postfix " else numberAsStr
125+ }
0 commit comments