Guilherme Delgado
12/15/2021, 10:39 AMinputs.forEach {
TextFieldWrapper(
id = ...,
text = ...,
onTextChanged = { my callback implementation }
)
...
}
More in thread.@Composable
fun TextFieldWrapper(
id: String,
text: String,
...
onTextChanged: ((id: String, text: String) -> Unit)? = null
) {
...
BasicTextField(
value = input,
onValueChange = {
input = it
onTextChanged?.invoke(id, it.text)
},
...
)
}
But if instead, I define the callback like this:
val onInputTextChanged: (id: String, text: String) -> Unit = remember(key) { { id, text -> ... } }
inputs.forEach {
TextFieldWrapper(
id = ...,
text = ...,
onTextChanged = onInputTextChanged
)
...
}
The performance issues appear to be gone.
Any thoughts on this? I mean it makes sense, but I would like to hear from you.Zach Klippenstein (he/him) [MOD]
12/15/2021, 7:08 PMgraphicsLayer
, shouldn’t trigger any recomposition or layout or text events. Can you share your transformation code?Guilherme Delgado
12/16/2021, 9:36 AMval imageModifier = remember(fillHeight) {
Modifier.onGloballyPositioned { coordinates ->
imageTopLeft = coordinates.boundsInRoot().topLeft
imageBottomRight = coordinates.boundsInRoot().bottomRight
if (imageDrawnSize == IntSize.Zero) {
imageDrawnSize = coordinates.size
}
}
}
...
Box(modifier = Modifier
.fillMaxSize()
.background(Color.LightGray.copy(alpha = 0.4f))
.pointerInput(gesturesEnabled) {
if (gesturesEnabled) {
detectTransformGestures { _, pan, zoom, _ ->
val new = scale * zoom
scale = when {
new < 1f -> 1f
new > 3f -> 3f
else -> new
}
if (scale > 1) {
translateOffset += pan
val inBounds = imageTopLeft.x <= 0f && imageTopLeft.y <= 0f &&
imageBottomRight.x >= imageDrawnSize.width.toFloat() && imageBottomRight.y >= imageDrawnSize.height.toFloat()
if (!inBounds) {
var deltaX = 0f
if (imageTopLeft.x > 0f) {
deltaX = -imageTopLeft.x
} else if (imageBottomRight.x < imageDrawnSize.width.toFloat()) {
deltaX = imageDrawnSize.width.toFloat() - imageBottomRight.x
}
var deltaY = 0f
if (imageTopLeft.y > 0f) {
deltaY = -imageTopLeft.y
} else if (imageBottomRight.y < imageDrawnSize.height.toFloat()) {
deltaY = imageDrawnSize.height.toFloat() - imageBottomRight.y
}
translateOffset = Offset(translateOffset.x + deltaX, translateOffset.y + deltaY)
}
} else {
translateOffset = Offset(0f, 0f)
}
}
}
}
.graphicsLayer(scaleX = scale, scaleY = scale, translationX = translateOffset.x, translationY = translateOffset.y)
)
Zach Klippenstein (he/him) [MOD]
12/16/2021, 2:58 PMgraphicsLayer
for stuff like this so that the layer can be adjusted during animation/gestures without recomposing.Guilherme Delgado
12/16/2021, 3:45 PMinput = it
, but in the end, if recomposition is triggered, the source of truth will provide the value. Dunno if I explained myself 😅