Moved RemeasureModifier to use an entity
Bug: 181981276
OnRemeasureModifier now uses an entity of LayoutNodeWrapper
rather than a subclass of DelegatingLayoutNodeWrapper.
Test: ran RemeasurementModifierTest
Change-Id: I97999e52b4125f05e5b6cf6ecfef579de0bc2486
diff --git a/compose/ui/ui/src/commonMain/kotlin/androidx/compose/ui/node/DelegatingLayoutNodeWrapper.kt b/compose/ui/ui/src/commonMain/kotlin/androidx/compose/ui/node/DelegatingLayoutNodeWrapper.kt
index b54c1bf..c4f4c9a 100644
--- a/compose/ui/ui/src/commonMain/kotlin/androidx/compose/ui/node/DelegatingLayoutNodeWrapper.kt
+++ b/compose/ui/ui/src/commonMain/kotlin/androidx/compose/ui/node/DelegatingLayoutNodeWrapper.kt
@@ -104,6 +104,7 @@
}
}
}
+ onMeasured()
return this
}
diff --git a/compose/ui/ui/src/commonMain/kotlin/androidx/compose/ui/node/EntityList.kt b/compose/ui/ui/src/commonMain/kotlin/androidx/compose/ui/node/EntityList.kt
index 3abe0a5..a573ee2 100644
--- a/compose/ui/ui/src/commonMain/kotlin/androidx/compose/ui/node/EntityList.kt
+++ b/compose/ui/ui/src/commonMain/kotlin/androidx/compose/ui/node/EntityList.kt
@@ -21,6 +21,7 @@
import androidx.compose.ui.draw.DrawModifier
import androidx.compose.ui.input.pointer.PointerInputModifier
import androidx.compose.ui.layout.OnPlacedModifier
+import androidx.compose.ui.layout.OnRemeasuredModifier
import androidx.compose.ui.layout.ParentDataModifier
import androidx.compose.ui.semantics.SemanticsEntity
import androidx.compose.ui.semantics.SemanticsModifier
@@ -56,6 +57,9 @@
if (modifier is OnPlacedModifier) {
add(SimpleEntity(layoutNodeWrapper, modifier), OnPlacedEntityType.index)
}
+ if (modifier is OnRemeasuredModifier) {
+ add(SimpleEntity(layoutNodeWrapper, modifier), RemeasureEntityType.index)
+ }
}
private fun <T : LayoutNodeEntity<T, *>> add(entity: T, index: Int) {
@@ -137,7 +141,9 @@
@OptIn(ExperimentalComposeUiApi::class)
val OnPlacedEntityType =
EntityType<SimpleEntity<OnPlacedModifier>, OnPlacedModifier>(4)
+ val RemeasureEntityType =
+ EntityType<SimpleEntity<OnRemeasuredModifier>, OnRemeasuredModifier>(5)
- private const val TypeCount = 5
+ private const val TypeCount = 6
}
}
diff --git a/compose/ui/ui/src/commonMain/kotlin/androidx/compose/ui/node/InnerPlaceable.kt b/compose/ui/ui/src/commonMain/kotlin/androidx/compose/ui/node/InnerPlaceable.kt
index 01e668d..3afa7f0 100644
--- a/compose/ui/ui/src/commonMain/kotlin/androidx/compose/ui/node/InnerPlaceable.kt
+++ b/compose/ui/ui/src/commonMain/kotlin/androidx/compose/ui/node/InnerPlaceable.kt
@@ -54,6 +54,7 @@
layoutNode.measureScope.measure(layoutNode.children, constraints)
}
layoutNode.handleMeasureResult(measureResult)
+ onMeasured()
return this
}
diff --git a/compose/ui/ui/src/commonMain/kotlin/androidx/compose/ui/node/LayoutNode.kt b/compose/ui/ui/src/commonMain/kotlin/androidx/compose/ui/node/LayoutNode.kt
index dbb0234..7fc5587 100644
--- a/compose/ui/ui/src/commonMain/kotlin/androidx/compose/ui/node/LayoutNode.kt
+++ b/compose/ui/ui/src/commonMain/kotlin/androidx/compose/ui/node/LayoutNode.kt
@@ -43,7 +43,6 @@
import androidx.compose.ui.layout.MeasureScope
import androidx.compose.ui.layout.ModifierInfo
import androidx.compose.ui.layout.OnGloballyPositionedModifier
-import androidx.compose.ui.layout.OnRemeasuredModifier
import androidx.compose.ui.layout.Placeable
import androidx.compose.ui.layout.Remeasurement
import androidx.compose.ui.layout.RemeasurementModifier
@@ -729,11 +728,6 @@
.initialize()
.assignChained(toWrap)
}
- if (mod is OnRemeasuredModifier) {
- wrapper = RemeasureModifierWrapper(wrapper, mod)
- .initialize()
- .assignChained(toWrap)
- }
wrapper
}
diff --git a/compose/ui/ui/src/commonMain/kotlin/androidx/compose/ui/node/LayoutNodeWrapper.kt b/compose/ui/ui/src/commonMain/kotlin/androidx/compose/ui/node/LayoutNodeWrapper.kt
index ab85ad8..9086834 100644
--- a/compose/ui/ui/src/commonMain/kotlin/androidx/compose/ui/node/LayoutNodeWrapper.kt
+++ b/compose/ui/ui/src/commonMain/kotlin/androidx/compose/ui/node/LayoutNodeWrapper.kt
@@ -242,6 +242,19 @@
return result
}
+ fun onMeasured() {
+ if (entities.has(EntityList.RemeasureEntityType)) {
+ val invokeRemeasureCallbacks = {
+ entities.forEach(EntityList.RemeasureEntityType) {
+ it.modifier.onRemeasured(measuredSize)
+ }
+ }
+ layoutNode.owner?.snapshotObserver?.withNoSnapshotReadObservation(
+ invokeRemeasureCallbacks
+ ) ?: invokeRemeasureCallbacks()
+ }
+ }
+
abstract fun calculateAlignmentLine(alignmentLine: AlignmentLine): Int
final override fun get(alignmentLine: AlignmentLine): Int {
diff --git a/compose/ui/ui/src/commonMain/kotlin/androidx/compose/ui/node/ModifiedLayoutNode.kt b/compose/ui/ui/src/commonMain/kotlin/androidx/compose/ui/node/ModifiedLayoutNode.kt
index 69a1192..fae672fa 100644
--- a/compose/ui/ui/src/commonMain/kotlin/androidx/compose/ui/node/ModifiedLayoutNode.kt
+++ b/compose/ui/ui/src/commonMain/kotlin/androidx/compose/ui/node/ModifiedLayoutNode.kt
@@ -34,11 +34,15 @@
modifier: LayoutModifier
) : DelegatingLayoutNodeWrapper<LayoutModifier>(wrapped, modifier) {
- override fun measure(constraints: Constraints): Placeable = performingMeasure(constraints) {
- with(modifier) {
- measureResult = measureScope.measure(wrapped, constraints)
- this@ModifiedLayoutNode
+ override fun measure(constraints: Constraints): Placeable {
+ val placeable = performingMeasure(constraints) {
+ with(modifier) {
+ measureResult = measureScope.measure(wrapped, constraints)
+ this@ModifiedLayoutNode
+ }
}
+ onMeasured()
+ return placeable
}
override fun minIntrinsicWidth(height: Int): Int =
diff --git a/compose/ui/ui/src/commonMain/kotlin/androidx/compose/ui/node/RemeasureModifierWrapper.kt b/compose/ui/ui/src/commonMain/kotlin/androidx/compose/ui/node/RemeasureModifierWrapper.kt
deleted file mode 100644
index fdb9840..0000000
--- a/compose/ui/ui/src/commonMain/kotlin/androidx/compose/ui/node/RemeasureModifierWrapper.kt
+++ /dev/null
@@ -1,39 +0,0 @@
-/*
- * Copyright 2021 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package androidx.compose.ui.node
-
-import androidx.compose.ui.layout.OnRemeasuredModifier
-import androidx.compose.ui.layout.Placeable
-import androidx.compose.ui.unit.Constraints
-
-/**
- * Wrapper around the [OnRemeasuredModifier] to notify whenever a remeasurement happens.
- */
-internal class RemeasureModifierWrapper(
- wrapped: LayoutNodeWrapper,
- modifier: OnRemeasuredModifier
-) : DelegatingLayoutNodeWrapper<OnRemeasuredModifier>(wrapped, modifier) {
- override fun measure(constraints: Constraints): Placeable {
- val placeable = super.measure(constraints)
- val invokeRemeasureCallbacks = {
- modifier.onRemeasured(measuredSize)
- }
- layoutNode.owner?.snapshotObserver?.withNoSnapshotReadObservation(invokeRemeasureCallbacks)
- ?: invokeRemeasureCallbacks.invoke()
- return placeable
- }
-}
\ No newline at end of file