From bf98b8035b5def2ca18acc446b0b7dced0709bdb Mon Sep 17 00:00:00 2001 From: Adetunji Dahunsi Date: Wed, 23 Oct 2024 07:42:34 -0400 Subject: [PATCH] Simplify modifier chain for movable shared elements --- .../MovableSharedElementState.kt | 51 +++++++------------ 1 file changed, 18 insertions(+), 33 deletions(-) diff --git a/library/compose/src/commonMain/kotlin/com/tunjid/treenav/compose/moveablesharedelement/MovableSharedElementState.kt b/library/compose/src/commonMain/kotlin/com/tunjid/treenav/compose/moveablesharedelement/MovableSharedElementState.kt index 92bdbc4..9be768f 100644 --- a/library/compose/src/commonMain/kotlin/com/tunjid/treenav/compose/moveablesharedelement/MovableSharedElementState.kt +++ b/library/compose/src/commonMain/kotlin/com/tunjid/treenav/compose/moveablesharedelement/MovableSharedElementState.kt @@ -43,7 +43,7 @@ internal class MovableSharedElementState( var paneScope by mutableStateOf(paneScope) - private var inCount by mutableIntStateOf(0) + private var composedRefCount by mutableIntStateOf(0) private var layer: GraphicsLayer? = null var animInProgress by mutableStateOf(false) @@ -61,21 +61,32 @@ internal class MovableSharedElementState( val moveableSharedElement: @Composable (Any?, Modifier) -> Unit = movableContentOf { state, modifier -> animInProgress = isInProgress() + val layer = rememberGraphicsLayer().also { + this.layer = it + } @Suppress("UNCHECKED_CAST") sharedElement( // The shared element composable will be created by the first screen and reused by // subsequent screens. This updates the state from other screens so changes are seen. state as State, - Modifier - .movableSharedElement( - state = this, - ) then modifier, + modifier + .animateBounds( + state = animatedBoundsState + ) + .drawWithContent { + layer.record { + this@drawWithContent.drawContent() + } + if (!canDrawInOverlay) { + drawLayer(layer) + } + }, ) DisposableEffect(Unit) { - ++inCount + ++composedRefCount onDispose { - if (--inCount <= 0) onRemoved() + if (--composedRefCount <= 0) onRemoved() } } } @@ -98,32 +109,6 @@ internal class MovableSharedElementState( private val hasBeenShared get() = panesKeysToSeenCount.size > 1 companion object { - /** - * Allows a custom modifier to animate the local position and size of the layout within the - * LookaheadLayout, whenever there's a change in the layout. - */ - @OptIn( - ExperimentalSharedTransitionApi::class - ) - @Composable - internal fun Modifier.movableSharedElement( - state: MovableSharedElementState<*, Pane, Destination>, - ): Modifier { - val layer = rememberGraphicsLayer().also { - state.layer = it - } - return animateBounds( - state = state.animatedBoundsState - ) - .drawWithContent { - layer.record { - this@drawWithContent.drawContent() - } - if (!state.canDrawInOverlay) { - drawLayer(layer) - } - } - } @Composable private fun MovableSharedElementState<*, Pane, Destination>.isInProgress(): Boolean {