rnett
11/21/2023, 10:43 PMSubcomposeLayout
with a map, and calling subcompose(key){ MyComposable(key, value) }
. The behaviour I'm seeing is that when one key leaves the layout, subcompose
is re-executed for every key, which is definitely not what I want. Any idea what could be causing this?rnett
11/21/2023, 10:45 PMshikasd
11/21/2023, 11:27 PMrnett
11/21/2023, 11:28 PMshikasd
11/21/2023, 11:28 PMSubcomposeLayout
itself.rnett
11/21/2023, 11:30 PM{ constraints ->
with(layout) {
doLayout(constraints)
}
}
where layout
is `remember`ed and doLayout
is a method on it, so I wouldn't think it would re-create itrnett
11/21/2023, 11:30 PMshikasd
11/21/2023, 11:32 PMprintln(this)
from measure policy and compare hash codes.rnett
11/22/2023, 12:33 AMderivedStateOf
from the other thread didn't help eitherrnett
11/22/2023, 12:34 AMval layout = remember {
ViewLayout(
derivedStateOf { staticObjectPositions + movingObjectPositions },
derivedStateOf(structuralEqualityPolicy()) {
(movingObjectPositions.keys + staticObjectPositions.keys).associateWith { updatedObjects.getValue(it) }
},
onClick
)
}
SubcomposeLayout(
remember { SubcomposeLayoutState(SubcomposeSlotReusePolicy(100)) },
Modifier
.fillMaxSize()
.background(Color.Black)
.zIndex(-10f)
.onSizeChanged {
with(density) {
val size = it.toSize().let { DpSize(it.width.toInt().toDp(), it.height.toInt().toDp()) }
if (size != viewportSize)
viewportSize = size
}
},
remember {
return@remember { constraints ->
with(layout) {
doLayout(constraints)
}
}
}
)
and here's the class itself:
@Stable
private data class ViewLayout(
private val objectPositions: State<Map<UUID, DpOffset>>,
private val visibleObjects: State<Map<UUID, SpaceObject<*>>>,
private val onClickState: State<(UUID) -> Unit>
) {
fun SubcomposeMeasureScope.doLayout(constraints: Constraints): MeasureResult {
println("Remeasure $this@SpaceViewLayout")
val placables = visibleObjects.value.mapValues { (id, obj) ->
subcompose(id) {
key(id) {
println("Item Compose")
SpaceObject(obj, {})
}
}
.single()
.measureMinIntrinsics(constraints)
}
return layout(constraints.maxWidth, constraints.maxHeight) {
println("Relayout")
placables.forEach { (id, placable) ->
val offset = objectPositions.value[id] ?: return@forEach
placable.place(offset.run { IntOffset(x.roundToPx(), y.roundToPx()) })
}
}
}
}
rnett
11/22/2023, 12:35 AMRelayout
Remeasure androidx.compose.ui.layout.LayoutNodeSubcompositionsState$Scope@8452c46@SpaceViewLayout
Item Compose
Item Compose
Item Compose
Item Compose
Item Compose
Item Compose
Item Compose
Item Compose
Relayout
Remeasure androidx.compose.ui.layout.LayoutNodeSubcompositionsState$Scope@8452c46@SpaceViewLayout
Item Compose
Item Compose
Item Compose
Item Compose
Item Compose
Item Compose
Relayout
Remeasure androidx.compose.ui.layout.LayoutNodeSubcompositionsState$Scope@8452c46@SpaceViewLayout
Item Compose
Item Compose
Item Compose
Item Compose
Relayout
Remeasure androidx.compose.ui.layout.LayoutNodeSubcompositionsState$Scope@8452c46@SpaceViewLayout
Item Compose
Relayout
rnett
11/22/2023, 12:35 AMshikasd
11/22/2023, 1:00 AMshikasd
11/22/2023, 1:01 AMvisibleObjects: Map<UUID, @Composable () -> Unit>
in this caseshikasd
11/22/2023, 1:02 AMSpaceObject
, so not sure it is that big of a deal 🙂rnett
11/22/2023, 1:03 AMshikasd
11/22/2023, 1:03 AMobj
from mapshikasd
11/22/2023, 1:04 AMkey
to wrap the item, it won't reuse it then. We have ReusableContent
which does kinda the same thing, but I am not sure you need that.rnett
11/22/2023, 1:06 AM