|
|
@@ -1,74 +1,38 @@
|
|
|
package eu.mjdev.desktop.components.text
|
|
|
|
|
|
-import androidx.compose.material.LocalTextStyle
|
|
|
+import androidx.compose.foundation.layout.BoxWithConstraints
|
|
|
import androidx.compose.material.Text
|
|
|
import androidx.compose.runtime.*
|
|
|
import androidx.compose.ui.Modifier
|
|
|
-import androidx.compose.ui.draw.drawWithContent
|
|
|
import androidx.compose.ui.graphics.Color
|
|
|
-import androidx.compose.ui.text.TextStyle
|
|
|
import androidx.compose.ui.text.font.FontFamily
|
|
|
import androidx.compose.ui.text.font.FontStyle
|
|
|
import androidx.compose.ui.text.font.FontWeight
|
|
|
import androidx.compose.ui.text.style.TextAlign
|
|
|
-import androidx.compose.ui.text.style.TextDecoration
|
|
|
import androidx.compose.ui.text.style.TextOverflow
|
|
|
-import androidx.compose.ui.unit.TextUnit
|
|
|
+import androidx.compose.ui.unit.dp
|
|
|
import androidx.compose.ui.unit.sp
|
|
|
-import eu.mjdev.desktop.extensions.Compose.rememberState
|
|
|
+import kotlin.math.min
|
|
|
|
|
|
+// todo may be need refactor
|
|
|
@Composable
|
|
|
fun AutoResizeText(
|
|
|
text: String,
|
|
|
- fontSizeRange: FontSizeRange,
|
|
|
modifier: Modifier = Modifier,
|
|
|
color: Color = Color.Unspecified,
|
|
|
fontStyle: FontStyle? = null,
|
|
|
fontWeight: FontWeight? = null,
|
|
|
fontFamily: FontFamily? = null,
|
|
|
- letterSpacing: TextUnit = TextUnit.Unspecified,
|
|
|
- textDecoration: TextDecoration? = null,
|
|
|
- textAlign: TextAlign? = null,
|
|
|
- lineHeight: TextUnit = TextUnit.Unspecified,
|
|
|
- overflow: TextOverflow = TextOverflow.Clip,
|
|
|
- softWrap: Boolean = true,
|
|
|
- maxLines: Int = Int.MAX_VALUE,
|
|
|
- style: TextStyle = LocalTextStyle.current,
|
|
|
-) {
|
|
|
- var fontSizeValue by remember(text, fontSizeRange) { mutableStateOf(fontSizeRange.max.value) }
|
|
|
- var readyToDraw by rememberState(false)
|
|
|
+) = BoxWithConstraints(modifier = modifier) {
|
|
|
Text(
|
|
|
text = text,
|
|
|
color = color,
|
|
|
- maxLines = maxLines,
|
|
|
+ maxLines = Int.MAX_VALUE,
|
|
|
+ fontSize = min(constraints.maxWidth, constraints.maxHeight).dp.minus(8.dp).value.sp,
|
|
|
fontStyle = fontStyle,
|
|
|
fontWeight = fontWeight,
|
|
|
fontFamily = fontFamily,
|
|
|
- letterSpacing = letterSpacing,
|
|
|
- textDecoration = textDecoration,
|
|
|
- textAlign = textAlign,
|
|
|
- lineHeight = lineHeight,
|
|
|
- overflow = overflow,
|
|
|
- softWrap = softWrap,
|
|
|
- style = style,
|
|
|
- fontSize = fontSizeValue.sp,
|
|
|
- onTextLayout = { textLayout ->
|
|
|
- if (textLayout.didOverflowHeight && !readyToDraw) {
|
|
|
- val nextFontSizeValue = fontSizeValue - fontSizeRange.step.value
|
|
|
- if (nextFontSizeValue <= fontSizeRange.min.value) {
|
|
|
- fontSizeValue = fontSizeRange.min.value
|
|
|
- readyToDraw = true
|
|
|
- } else {
|
|
|
- fontSizeValue = nextFontSizeValue
|
|
|
- }
|
|
|
- } else {
|
|
|
- readyToDraw = true
|
|
|
- }
|
|
|
- },
|
|
|
- modifier = modifier.drawWithContent {
|
|
|
- if (readyToDraw) {
|
|
|
- drawContent()
|
|
|
- }
|
|
|
- }
|
|
|
+ textAlign = TextAlign.Center,
|
|
|
+ overflow = TextOverflow.Clip,
|
|
|
)
|
|
|
-}
|
|
|
+}
|