Skip to content

Commit

Permalink
Use SubcomposeLayout for ContentAvoidingLayout (#2171)
Browse files Browse the repository at this point in the history
Use `SubcomposeLayout` for `ContentAvoidingLayout`
  • Loading branch information
jmartinesp authored Jan 5, 2024
1 parent e04780f commit 3a2191e
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 9 deletions.
1 change: 1 addition & 0 deletions changelog.d/2155.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Use `SubomposeLayout` for `ContentAvoidingLayout` to prevent wrong measurements in the layout process, leading to cut-off text messages in the timeline.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.Layout
import androidx.compose.ui.layout.SubcomposeLayout
import androidx.compose.ui.platform.LocalLayoutDirection
import androidx.compose.ui.text.TextLayoutResult
import androidx.compose.ui.unit.Constraints
Expand Down Expand Up @@ -58,23 +59,18 @@ fun ContentAvoidingLayout(
) {
val scope = remember { ContentAvoidingLayoutScopeInstance() }

Layout(
SubcomposeLayout(
modifier = modifier,
content = {
scope.content()
overlay()
}
) { measurables, constraints ->
assert(measurables.size == 2) { "ContentAvoidingLayout must have exactly 2 children" }
) { constraints ->

// Measure the `overlay` view first, in case we need to shrink the `content`
val overlayPlaceable = measurables.last().measure(Constraints(minWidth = 0, maxWidth = constraints.maxWidth))
val overlayPlaceable = subcompose(0, overlay).first().measure(Constraints(minWidth = 0, maxWidth = constraints.maxWidth))
val contentConstraints = if (shrinkContent) {
Constraints(minWidth = 0, maxWidth = constraints.maxWidth - overlayPlaceable.width)
} else {
Constraints(minWidth = 0, maxWidth = constraints.maxWidth)
}
val contentPlaceable = measurables.first().measure(contentConstraints)
val contentPlaceable = subcompose(1) { scope.content() }.first().measure(contentConstraints)

var layoutWidth = contentPlaceable.width
var layoutHeight = contentPlaceable.height
Expand Down

0 comments on commit 3a2191e

Please sign in to comment.