-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/develop' into feature/jaino/#128
- Loading branch information
Showing
22 changed files
with
365 additions
and
162 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.bff.wespot | ||
|
||
enum class BarType { | ||
DEFAULT, | ||
ENTIRE, | ||
NONE; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
154 changes: 154 additions & 0 deletions
154
core/ui/src/main/kotlin/com/bff/wespot/ui/AutoSizeText.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
package com.bff.wespot.ui | ||
|
||
import androidx.compose.foundation.layout.BoxWithConstraints | ||
import androidx.compose.foundation.layout.BoxWithConstraintsScope | ||
import androidx.compose.foundation.text.InlineTextContent | ||
import androidx.compose.foundation.text.InternalFoundationTextApi | ||
import androidx.compose.foundation.text.TextDelegate | ||
import androidx.compose.material3.LocalTextStyle | ||
import androidx.compose.material3.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.graphics.Color | ||
import androidx.compose.ui.platform.LocalDensity | ||
import androidx.compose.ui.platform.LocalFontFamilyResolver | ||
import androidx.compose.ui.platform.LocalLayoutDirection | ||
import androidx.compose.ui.text.AnnotatedString | ||
import androidx.compose.ui.text.TextLayoutResult | ||
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.min | ||
import androidx.compose.ui.unit.sp | ||
|
||
@Composable | ||
fun AutoSizeText( | ||
text: String, | ||
modifier: Modifier = Modifier, | ||
color: Color = Color.Unspecified, | ||
suggestedFontSizes: List<TextUnit> = emptyList(), | ||
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, | ||
onTextLayout: (TextLayoutResult) -> Unit = {}, | ||
style: TextStyle = LocalTextStyle.current, | ||
) { | ||
AutoSizeText( | ||
AnnotatedString(text), | ||
modifier, | ||
color, | ||
suggestedFontSizes, | ||
fontStyle, | ||
fontWeight, | ||
fontFamily, | ||
letterSpacing, | ||
textDecoration, | ||
textAlign, | ||
lineHeight, | ||
overflow, | ||
softWrap, | ||
maxLines, | ||
emptyMap(), | ||
onTextLayout, | ||
style, | ||
) | ||
} | ||
|
||
@Composable | ||
fun AutoSizeText( | ||
text: AnnotatedString, | ||
modifier: Modifier = Modifier, | ||
color: Color = Color.Unspecified, | ||
suggestedFontSizes: List<TextUnit> = emptyList(), | ||
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, | ||
inlineContent: Map<String, InlineTextContent> = mapOf(), | ||
onTextLayout: (TextLayoutResult) -> Unit = {}, | ||
style: TextStyle = LocalTextStyle.current, | ||
) { | ||
BoxWithConstraints( | ||
modifier = modifier, | ||
contentAlignment = Alignment.Center, | ||
) { | ||
var combinedTextStyle = (LocalTextStyle.current + style).copy( | ||
fontSize = min(maxWidth, maxHeight).value.sp, | ||
) | ||
|
||
val fontSizes = suggestedFontSizes.ifEmpty { | ||
MutableList(combinedTextStyle.fontSize.value.toInt()) { | ||
(combinedTextStyle.fontSize.value - it).sp | ||
} | ||
} | ||
|
||
var currentFontIndex = 0 | ||
|
||
while (shouldShrink(text, combinedTextStyle, maxLines)) { | ||
combinedTextStyle = | ||
combinedTextStyle.copy(fontSize = fontSizes[++currentFontIndex]) | ||
} | ||
|
||
Text( | ||
text = text, | ||
color = color, | ||
fontStyle = fontStyle, | ||
fontWeight = fontWeight, | ||
fontFamily = fontFamily, | ||
letterSpacing = letterSpacing, | ||
textDecoration = textDecoration, | ||
textAlign = textAlign, | ||
lineHeight = lineHeight, | ||
overflow = overflow, | ||
softWrap = softWrap, | ||
maxLines = maxLines, | ||
inlineContent = inlineContent, | ||
onTextLayout = onTextLayout, | ||
style = combinedTextStyle, | ||
) | ||
} | ||
} | ||
|
||
@OptIn(InternalFoundationTextApi::class) | ||
@Composable | ||
private fun BoxWithConstraintsScope.shouldShrink( | ||
text: AnnotatedString, | ||
textStyle: TextStyle, | ||
maxLines: Int, | ||
): Boolean { | ||
val textDelegate = TextDelegate( | ||
text = text, | ||
style = textStyle, | ||
maxLines = maxLines, | ||
softWrap = true, | ||
overflow = TextOverflow.Clip, | ||
density = LocalDensity.current, | ||
fontFamilyResolver = LocalFontFamilyResolver.current, | ||
) | ||
|
||
val textLayoutResult = textDelegate.layout( | ||
constraints, | ||
LocalLayoutDirection.current, | ||
) | ||
|
||
return textLayoutResult.hasVisualOverflow | ||
} |
Oops, something went wrong.