Skip to content

Commit

Permalink
Experiment with using Modifier.renderInSharedTransitionScopeOverlay
Browse files Browse the repository at this point in the history
  • Loading branch information
tunjid committed Jul 29, 2024
1 parent 60b6dc2 commit 053ae10
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,11 @@ fun AdaptiveContentRoot(
SharedTransitionLayout(
modifier = Modifier.drawWithContent {
drawContent()
adaptiveContentState.overlays.forEach { overlay ->
with(overlay) {
drawInOverlay()
}
}
// adaptiveContentState.overlays.forEach { overlay ->
// with(overlay) {
// drawInOverlay()
// }
// }
}
) {
CompositionLocalProvider(LocalSharedTransitionScope provides this) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import androidx.compose.runtime.setValue
import androidx.compose.runtime.snapshotFlow
import androidx.compose.ui.Modifier
import androidx.compose.ui.composed
import androidx.compose.ui.draw.drawWithContent
import androidx.compose.ui.graphics.drawscope.ContentDrawScope
import androidx.compose.ui.graphics.drawscope.translate
import androidx.compose.ui.graphics.layer.GraphicsLayer
Expand Down Expand Up @@ -130,87 +129,84 @@ internal class MovableSharedElementData<T>(
movableSharedElementData: MovableSharedElementData<*>,
): Modifier = composed {
val sharedTransitionScope = LocalSharedTransitionScope.current
val coroutineScope = rememberCoroutineScope()
with(sharedTransitionScope) {
val coroutineScope = rememberCoroutineScope()

val sizeAnimInProgress = movableSharedElementData.isInProgress(
MovableSharedElementData<*>::sizeAnimation
)
.also { movableSharedElementData.sizeAnimInProgress = it }

val offsetAnimInProgress = movableSharedElementData.isInProgress(
MovableSharedElementData<*>::offsetAnimation
)
.also { movableSharedElementData.offsetAnimInProgress = it }
val sizeAnimInProgress = movableSharedElementData.isInProgress(
MovableSharedElementData<*>::sizeAnimation
)
.also { movableSharedElementData.sizeAnimInProgress = it }

val layer = rememberGraphicsLayer().also {
movableSharedElementData.layer = it
}
approachLayout(
isMeasurementApproachInProgress = { lookaheadSize ->
movableSharedElementData.sizeAnimation.updateTarget(
target = lookaheadSize,
coroutineScope = coroutineScope,
animationSpec = sizeSpec
)
sizeAnimInProgress
},
isPlacementApproachInProgress = { lookaheadCoordinates ->
val lookaheadOffset = with(sharedTransitionScope) {
lookaheadScopeCoordinates.localLookaheadPositionOf(
sourceCoordinates = lookaheadCoordinates
).round()
}
movableSharedElementData.offsetAnimation.updateTarget(
target = lookaheadOffset,
coroutineScope = coroutineScope,
animationSpec = offsetSpec,
)
offsetAnimInProgress
},
approachMeasure = { measurable, _ ->
val (width, height) = movableSharedElementData.sizeAnimation.updateTarget(
target = lookaheadSize,
coroutineScope = coroutineScope,
animationSpec = sizeSpec
)
val animatedConstraints = Constraints.fixed(width, height)
val placeable = measurable.measure(animatedConstraints)
val offsetAnimInProgress = movableSharedElementData.isInProgress(
MovableSharedElementData<*>::offsetAnimation
)
.also { movableSharedElementData.offsetAnimInProgress = it }

layout(placeable.width, placeable.height) layout@{
val currentCoordinates = coordinates ?: return@layout placeable.place(
x = 0,
y = 0
val layer = rememberGraphicsLayer().also {
movableSharedElementData.layer = it
}
approachLayout(
isMeasurementApproachInProgress = { lookaheadSize ->
movableSharedElementData.sizeAnimation.updateTarget(
target = lookaheadSize,
coroutineScope = coroutineScope,
animationSpec = sizeSpec
)
sizeAnimInProgress
},
isPlacementApproachInProgress = { lookaheadCoordinates ->
val lookaheadOffset = with(sharedTransitionScope) {
lookaheadScopeCoordinates.localLookaheadPositionOf(
sourceCoordinates = currentCoordinates
sourceCoordinates = lookaheadCoordinates
).round()
}
movableSharedElementData.offsetAnimation.updateTarget(
target = lookaheadOffset,
coroutineScope = coroutineScope,
animationSpec = offsetSpec,
)
offsetAnimInProgress
},
approachMeasure = { measurable, _ ->
val (width, height) = movableSharedElementData.sizeAnimation.updateTarget(
target = lookaheadSize,
coroutineScope = coroutineScope,
animationSpec = sizeSpec
)
val animatedConstraints = Constraints.fixed(width, height)
val placeable = measurable.measure(animatedConstraints)

movableSharedElementData.apply {
targetOffset = offsetAnimation.updateTarget(
target = lookaheadOffset,
coroutineScope = coroutineScope,
animationSpec = offsetSpec
layout(placeable.width, placeable.height) layout@{
val currentCoordinates = coordinates ?: return@layout placeable.place(
x = 0,
y = 0
)
}
val lookaheadOffset = with(sharedTransitionScope) {
lookaheadScopeCoordinates.localLookaheadPositionOf(
sourceCoordinates = currentCoordinates
).round()
}

val (x, y) = movableSharedElementData.targetOffset - lookaheadOffset
placeable.place(
x = x,
y = y
)
}
}
)
.drawWithContent {
layer.record {
this@drawWithContent.drawContent()
}
if (!movableSharedElementData.canDrawInOverlay) {
drawLayer(layer)
movableSharedElementData.apply {
targetOffset = offsetAnimation.updateTarget(
target = lookaheadOffset,
coroutineScope = coroutineScope,
animationSpec = offsetSpec
)
}

val (x, y) = movableSharedElementData.targetOffset - lookaheadOffset
placeable.place(
x = x,
y = y
)
}
}
}
)
.renderInSharedTransitionScopeOverlay(
renderInOverlay = movableSharedElementData::canDrawInOverlay
)
}
}


Expand Down

0 comments on commit 053ae10

Please sign in to comment.