Skip to content

Commit

Permalink
Merge pull request #127 from JetBrains/feature/fix_density_change_crash
Browse files Browse the repository at this point in the history
Fix crash in MeasureAndLayoutDelegate.forceMeasureTheSubtree()
  • Loading branch information
igordmn authored Nov 25, 2021
2 parents c9896b9 + 03f1af1 commit ca6db90
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,34 @@ class NodesRemeasuredOnceTest {
assertThat(remeasurements).isEqualTo(2)
}
}

@Test
fun remeasuringChildWithExtraLayer_notPlacedChild() {
val height = mutableStateOf(10)
var remeasurements = 0

rule.setContent {
WrapChild(onMeasured = { actualHeight ->
assertThat(actualHeight).isEqualTo(height.value)
remeasurements++
}) {
NotPlaceChild(height) {
WrapChild {
Child(height)
}
}
}
}

rule.runOnIdle {
assertThat(remeasurements).isEqualTo(1)
height.value = 20
}

rule.runOnIdle {
assertThat(remeasurements).isEqualTo(2)
}
}
}

@Composable
Expand All @@ -133,6 +161,16 @@ private fun WrapChild(onMeasured: (Int) -> Unit = {}, content: @Composable () ->
}
}

@Composable
private fun NotPlaceChild(height: State<Int>, content: @Composable () -> Unit) {
Layout(content = content) { measurables, constraints ->
layout(constraints.maxWidth, height.value) {
measurables.first()
.measure(constraints.copy(minHeight = 0, maxHeight = Constraints.Infinity))
}
}
}

@Composable
private fun Child(height: State<Int>) {
Layout { _, constraints ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,9 @@ internal class MeasureAndLayoutDelegate(private val root: LayoutNode) {

/**
* Makes sure the passed [layoutNode] and its subtree is remeasured and has the final sizes.
*
* The node or some of the nodes in its subtree can still be kept unmeasured if they are
* not placed and don't affect the parent size. See [requestRemeasure] for details.
*/
fun forceMeasureTheSubtree(layoutNode: LayoutNode) {
// if there is nothing in `relayoutNodes` everything is remeasured.
Expand All @@ -273,8 +276,13 @@ internal class MeasureAndLayoutDelegate(private val root: LayoutNode) {
remeasureAndRelayoutIfNeeded(child)
}

// run recursively for the subtree.
forceMeasureTheSubtree(child)
// if the child is still in NeedsRemeasure state then this child remeasure wasn't
// needed. it can happen for example when this child is not placed and can't affect
// the parent size. we can skip the whole subtree.
if (child.layoutState != NeedsRemeasure) {
// run recursively for the subtree.
forceMeasureTheSubtree(child)
}
}

// if the child was resized during the remeasurement it could request a remeasure on
Expand All @@ -283,8 +291,6 @@ internal class MeasureAndLayoutDelegate(private val root: LayoutNode) {
if (layoutNode.layoutState == NeedsRemeasure && relayoutNodes.remove(layoutNode)) {
remeasureAndRelayoutIfNeeded(layoutNode)
}

require(layoutNode.layoutState != NeedsRemeasure)
}

/**
Expand Down

0 comments on commit ca6db90

Please sign in to comment.