Skip to content

Commit

Permalink
Implement QEText that will remove orphan words
Browse files Browse the repository at this point in the history
  • Loading branch information
AdamGrzybkowski committed Jan 28, 2025
1 parent a366224 commit 3b8a117
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 2 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.gravatar.quickeditor.ui.components

import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
Expand All @@ -12,7 +11,7 @@ import com.gravatar.ui.GravatarTheme

@Composable
internal fun QESectionMessage(message: String, modifier: Modifier = Modifier) {
Text(
QEText(
text = message,
fontSize = 15.sp,
color = MaterialTheme.colorScheme.tertiary,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.gravatar.quickeditor.ui.components

import androidx.compose.material3.LocalTextStyle
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
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.tooling.preview.Preview
import androidx.compose.ui.unit.TextUnit

/**
* Wrapper for the default Text component to make sure line is not broken just before the last word.
*/
@Composable
internal fun QEText(
text: String,
modifier: Modifier = Modifier,
color: Color = Color.Unspecified,
fontSize: TextUnit = TextUnit.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,
minLines: Int = 1,
onTextLayout: ((TextLayoutResult) -> Unit)? = null,
style: TextStyle = LocalTextStyle.current,
) {
Text(
text = text.removeOrphans,
modifier = modifier,
color = color,
fontSize = fontSize,
fontStyle = fontStyle,
fontWeight = fontWeight,
fontFamily = fontFamily,
letterSpacing = letterSpacing,
textDecoration = textDecoration,
textAlign = textAlign,
lineHeight = lineHeight,
overflow = overflow,
softWrap = softWrap,
maxLines = maxLines,
minLines = minLines,
onTextLayout = onTextLayout,
style = style,
)
}

private val String.removeOrphans: String
get() {
val space = " "
val index = lastIndexOf(space, ignoreCase = true)
return if (index < 0) this else this.replaceRange(index, index + space.length, "\u00A0")
}

@Preview(showBackground = true, widthDp = 380)
@Composable
private fun QETextPreview() {
QEText(text = "This is a long text that should not break just before the last word.")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.gravatar.quickeditor.ui.components

import androidx.compose.foundation.layout.width
import androidx.compose.material3.Surface
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import com.gravatar.quickeditor.ui.gravatarScreenshotTest
import com.gravatar.uitestutils.RoborazziTest
import org.junit.Test

class QETextTest : RoborazziTest() {
@Test
fun qeTextTestNoOrphan() = gravatarScreenshotTest {
Surface(
modifier = Modifier.width(250.dp),
) {
QEText(text = "This is a long text that should not break just before the last word.")
}
}
}

0 comments on commit 3b8a117

Please sign in to comment.