Handle Constraints shrinking in 1-line text
Previously: Measure would sometimes early exit for 1-line text that would actually clip.
New: Layout is checked for clipping
Bug: b/275369323
Test: ./gradlew :com:found:found:test
Change-Id: I54032f99615ec80807d2d83844b4e2d2c3f70717
diff --git a/compose/foundation/foundation/src/androidAndroidTest/kotlin/androidx/compose/foundation/text/modifiers/LayoutUtilsKtTest.kt b/compose/foundation/foundation/src/androidAndroidTest/kotlin/androidx/compose/foundation/text/modifiers/LayoutUtilsKtTest.kt
index f7b269f..42fc747 100644
--- a/compose/foundation/foundation/src/androidAndroidTest/kotlin/androidx/compose/foundation/text/modifiers/LayoutUtilsKtTest.kt
+++ b/compose/foundation/foundation/src/androidAndroidTest/kotlin/androidx/compose/foundation/text/modifiers/LayoutUtilsKtTest.kt
@@ -91,22 +91,8 @@
}
@Test
- fun canChangeBreak_canWrap_false() {
- val subject = canChangeBreaks(
- canWrap = false,
- newConstraints = Constraints(0),
- oldConstraints = Constraints(0),
- maxIntrinsicWidth = 42f,
- softWrap = true,
- overflow = TextOverflow.Ellipsis
- )
- assertThat(subject).isFalse()
- }
-
- @Test
fun canChangeBreak_sameWidth() {
- val subject = canChangeBreaks(
- canWrap = true,
+ val subject = canChangeLayoutWidth(
newConstraints = Constraints.fixedWidth(50),
oldConstraints = Constraints.fixedWidth(50),
maxIntrinsicWidth = 1234f,
@@ -118,8 +104,7 @@
@Test
fun canChangeBreak_textSmallerThanConstraints() {
- val subject = canChangeBreaks(
- canWrap = true,
+ val subject = canChangeLayoutWidth(
newConstraints = Constraints.fixedWidth(50),
oldConstraints = Constraints.fixedWidth(40),
maxIntrinsicWidth = 12f,
@@ -131,8 +116,7 @@
@Test
fun canChangeBreak_textBiggerThanConstraints() {
- val subject = canChangeBreaks(
- canWrap = true,
+ val subject = canChangeLayoutWidth(
newConstraints = Constraints.fixedWidth(100),
oldConstraints = Constraints.fixedWidth(200),
maxIntrinsicWidth = 300f,
@@ -144,8 +128,7 @@
@Test
fun canChangeBreak_shrinking_textSmallerThanNewConstraints() {
- val subject = canChangeBreaks(
- canWrap = true,
+ val subject = canChangeLayoutWidth(
newConstraints = Constraints.fixedWidth(50),
oldConstraints = Constraints.fixedWidth(60),
maxIntrinsicWidth = 45f,
@@ -157,8 +140,7 @@
@Test
fun canChangeBreak_shrinking_textBiggerThanNewConstraints() {
- val subject = canChangeBreaks(
- canWrap = true,
+ val subject = canChangeLayoutWidth(
newConstraints = Constraints.fixedWidth(50),
oldConstraints = Constraints.fixedWidth(60),
maxIntrinsicWidth = 59f,
@@ -170,8 +152,7 @@
@Test
fun canChangeBreak_growing_textSmallerThanNewConstraints() {
- val subject = canChangeBreaks(
- canWrap = true,
+ val subject = canChangeLayoutWidth(
newConstraints = Constraints.fixedWidth(60),
oldConstraints = Constraints.fixedWidth(50),
maxIntrinsicWidth = 45f,
@@ -183,8 +164,7 @@
@Test
fun canChangeBreak_growing_textBiggerThanNewConstraints() {
- val subject = canChangeBreaks(
- canWrap = true,
+ val subject = canChangeLayoutWidth(
newConstraints = Constraints.fixedWidth(60),
oldConstraints = Constraints.fixedWidth(50),
maxIntrinsicWidth = 59f,
diff --git a/compose/foundation/foundation/src/commonMain/kotlin/androidx/compose/foundation/text/modifiers/LayoutUtils.kt b/compose/foundation/foundation/src/commonMain/kotlin/androidx/compose/foundation/text/modifiers/LayoutUtils.kt
index bb111a3..b62e55f 100644
--- a/compose/foundation/foundation/src/commonMain/kotlin/androidx/compose/foundation/text/modifiers/LayoutUtils.kt
+++ b/compose/foundation/foundation/src/commonMain/kotlin/androidx/compose/foundation/text/modifiers/LayoutUtils.kt
@@ -92,16 +92,13 @@
*
* If text or other text-layout attributes change, this method will not return accurate results.
*/
-internal fun canChangeBreaks(
- canWrap: Boolean,
+internal fun canChangeLayoutWidth(
newConstraints: Constraints,
oldConstraints: Constraints,
maxIntrinsicWidth: Float,
softWrap: Boolean,
overflow: TextOverflow,
): Boolean {
- // no breaks
- if (!canWrap) return false
// we can assume maxIntrinsicWidth is the same, or other invalidate would have happened
// earlier (resetting para, etc)
val prevMaxWidth = finalMaxWidth(oldConstraints, softWrap, overflow, maxIntrinsicWidth)
diff --git a/compose/foundation/foundation/src/commonMain/kotlin/androidx/compose/foundation/text/modifiers/MultiParagraphLayoutCache.kt b/compose/foundation/foundation/src/commonMain/kotlin/androidx/compose/foundation/text/modifiers/MultiParagraphLayoutCache.kt
index b2d6ffe..1a8f966 100644
--- a/compose/foundation/foundation/src/commonMain/kotlin/androidx/compose/foundation/text/modifiers/MultiParagraphLayoutCache.kt
+++ b/compose/foundation/foundation/src/commonMain/kotlin/androidx/compose/foundation/text/modifiers/MultiParagraphLayoutCache.kt
@@ -305,8 +305,7 @@
if (constraints == layoutInput.constraints) return false
// see if width would produce the same wraps
- if (canChangeBreaks(
- canWrap = softWrap && maxLines > 1,
+ if (canChangeLayoutWidth(
newConstraints = constraints,
oldConstraints = layoutInput.constraints,
maxIntrinsicWidth = this.multiParagraph.intrinsics.maxIntrinsicWidth,
diff --git a/compose/foundation/foundation/src/commonMain/kotlin/androidx/compose/foundation/text/modifiers/ParagraphLayoutCache.kt b/compose/foundation/foundation/src/commonMain/kotlin/androidx/compose/foundation/text/modifiers/ParagraphLayoutCache.kt
index 988d0c7..2a592be 100644
--- a/compose/foundation/foundation/src/commonMain/kotlin/androidx/compose/foundation/text/modifiers/ParagraphLayoutCache.kt
+++ b/compose/foundation/foundation/src/commonMain/kotlin/androidx/compose/foundation/text/modifiers/ParagraphLayoutCache.kt
@@ -301,8 +301,7 @@
if (constraints == prevConstraints) return false
// see if width would produce the same wraps
- if (canChangeBreaks(
- canWrap = softWrap && maxLines > 1,
+ if (canChangeLayoutWidth(
newConstraints = constraints,
oldConstraints = prevConstraints,
maxIntrinsicWidth = localParagraphIntrinsics.maxIntrinsicWidth,