这是indexloc提供的服务,不要输入任何密码
Skip to content

Introduce concept of a sticky shared element vs a seekable one #40

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import androidx.compose.runtime.Composable
import androidx.compose.runtime.Stable
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.composed
import com.tunjid.treenav.Node
import com.tunjid.treenav.compose.PaneScope
import com.tunjid.treenav.compose.PaneSharedTransitionScope
Expand Down Expand Up @@ -69,7 +68,6 @@ private class ThreePaneSharedTransitionScope<Destination : Node> @OptIn(
boundsTransform: BoundsTransform,
placeHolderSize: PlaceHolderSize,
renderInOverlayDuringTransition: Boolean,
visible: Boolean?,
zIndexInOverlay: Float,
clipInOverlayDuringTransition: OverlayClip,
): Modifier = when (paneScope.paneState.pane) {
Expand All @@ -93,4 +91,33 @@ private class ThreePaneSharedTransitionScope<Destination : Node> @OptIn(
ThreePane.Overlay,
-> this
}

override fun Modifier.paneStickySharedElement(
sharedContentState: SharedTransitionScope.SharedContentState,
boundsTransform: BoundsTransform,
placeHolderSize: PlaceHolderSize,
renderInOverlayDuringTransition: Boolean,
zIndexInOverlay: Float,
clipInOverlayDuringTransition: OverlayClip
): Modifier = when (paneScope.paneState.pane) {
null -> throw IllegalArgumentException(
"Shared elements may only be used in non null panes"
)
// Allow shared elements in the primary or transient primary content only
ThreePane.Primary -> sharedElementWithCallerManagedVisibility(
sharedContentState = sharedContentState,
visible = isActive,
boundsTransform = boundsTransform,
placeHolderSize = placeHolderSize,
renderInOverlayDuringTransition = renderInOverlayDuringTransition,
zIndexInOverlay = zIndexInOverlay,
clipInOverlayDuringTransition = clipInOverlayDuringTransition,
)

// In the other panes use the element as is
ThreePane.Secondary,
ThreePane.Tertiary,
ThreePane.Overlay,
-> this
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -152,39 +152,100 @@ private class ThreePaneMovableSharedElementScope<Destination : Node>(
)

// In the secondary pane allow shared elements only if certain conditions match
ThreePane.Secondary -> when {
canAnimateSecondary() -> { state, modifier ->
with(hostState) {
Box(modifier) {
Box(
Modifier
.fillMaxConstraints()
.sharedElementWithCallerManagedVisibility(
sharedContentState = sharedContentState,
visible = false,
placeHolderSize = placeHolderSize,
renderInOverlayDuringTransition = renderInOverlayDuringTransition,
zIndexInOverlay = zIndexInOverlay,
clipInOverlayDuringTransition = clipInOverlayDuringTransition,
)
)
(alternateOutgoingSharedElement ?: sharedElement)(
state,
Modifier
.fillMaxConstraints(),
)
}
}
}
ThreePane.Secondary -> secondaryPaneSharedElement(
sharedContentState = sharedContentState,
placeHolderSize = placeHolderSize,
renderInOverlayDuringTransition = renderInOverlayDuringTransition,
zIndexInOverlay = zIndexInOverlay,
clipInOverlayDuringTransition = clipInOverlayDuringTransition,
alternateOutgoingSharedElement = alternateOutgoingSharedElement,
sharedElement = sharedElement
)

else -> alternateOutgoingSharedElement ?: sharedElement
}
// In the other panes use the element as is
ThreePane.Tertiary,
ThreePane.Overlay,
-> alternateOutgoingSharedElement ?: sharedElement
}

@OptIn(ExperimentalSharedTransitionApi::class)
override fun <T> movableStickySharedElementOf(
sharedContentState: SharedTransitionScope.SharedContentState,
boundsTransform: BoundsTransform,
placeHolderSize: PlaceHolderSize,
renderInOverlayDuringTransition: Boolean,
zIndexInOverlay: Float,
clipInOverlayDuringTransition: OverlayClip,
alternateOutgoingSharedElement: (@Composable (T, Modifier) -> Unit)?,
sharedElement: @Composable (T, Modifier) -> Unit
): @Composable (T, Modifier) -> Unit = when (paneState.pane) {
null -> throw IllegalArgumentException(
"Shared elements may only be used in non null panes"
)
// Allow movable shared elements in the primary pane only
ThreePane.Primary -> delegate.movableStickySharedElementOf(
sharedContentState = sharedContentState,
boundsTransform = boundsTransform,
placeHolderSize = placeHolderSize,
renderInOverlayDuringTransition = renderInOverlayDuringTransition,
zIndexInOverlay = zIndexInOverlay,
clipInOverlayDuringTransition = clipInOverlayDuringTransition,
alternateOutgoingSharedElement = alternateOutgoingSharedElement,
sharedElement = sharedElement
)

// In the secondary pane allow shared elements only if certain conditions match
ThreePane.Secondary -> secondaryPaneSharedElement(
sharedContentState = sharedContentState,
placeHolderSize = placeHolderSize,
renderInOverlayDuringTransition = renderInOverlayDuringTransition,
zIndexInOverlay = zIndexInOverlay,
clipInOverlayDuringTransition = clipInOverlayDuringTransition,
alternateOutgoingSharedElement = alternateOutgoingSharedElement,
sharedElement = sharedElement
)

// In the other panes use the element as is
ThreePane.Tertiary,
ThreePane.Overlay,
-> alternateOutgoingSharedElement ?: sharedElement
}

private fun <T> secondaryPaneSharedElement(
sharedContentState: SharedTransitionScope.SharedContentState,
placeHolderSize: PlaceHolderSize,
renderInOverlayDuringTransition: Boolean,
zIndexInOverlay: Float,
clipInOverlayDuringTransition: OverlayClip,
alternateOutgoingSharedElement: @Composable ((T, Modifier) -> Unit)?,
sharedElement: @Composable (T, Modifier) -> Unit
) = when {
canAnimateSecondary() -> { state, modifier ->
with(hostState) {
Box(modifier) {
Box(
Modifier
.fillMaxConstraints()
.sharedElementWithCallerManagedVisibility(
sharedContentState = sharedContentState,
visible = false,
placeHolderSize = placeHolderSize,
renderInOverlayDuringTransition = renderInOverlayDuringTransition,
zIndexInOverlay = zIndexInOverlay,
clipInOverlayDuringTransition = clipInOverlayDuringTransition,
)
)
(alternateOutgoingSharedElement ?: sharedElement)(
state,
Modifier
.fillMaxConstraints(),
)
}
}
}

else -> alternateOutgoingSharedElement ?: sharedElement
}
}

private fun PaneScope<ThreePane, *>.canAnimateSecondary(): Boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,12 @@ interface PaneSharedTransitionScope<Pane, Destination : Node> :
PaneScope<Pane, Destination>, SharedTransitionScope {

/**
* Conceptual equivalent of [SharedTransitionScope.sharedElement], with the exception
* of a key being passed instead of [SharedTransitionScope.SharedContentState]. This is because
* each [PaneState.pane] may need its own [SharedTransitionScope.SharedContentState] and
* will need to be managed by the implementation of this method.
* Creates a shared element transition where the shared element is seekable
* with the transition with the transition driving the [PaneScope].
*
* This is a pane specific implementation of [SharedTransitionScope.sharedElement], where
* implementations can specify extra logic for how the shared element behaves when
* rendered concurrently in multiple panes.
*
* @see [SharedTransitionScope.sharedElement].
*/
Expand All @@ -50,7 +52,26 @@ interface PaneSharedTransitionScope<Pane, Destination : Node> :
boundsTransform: BoundsTransform = Defaults.DefaultBoundsTransform,
placeHolderSize: PlaceHolderSize = contentSize,
renderInOverlayDuringTransition: Boolean = true,
visible: Boolean? = null,
zIndexInOverlay: Float = 0f,
clipInOverlayDuringTransition: OverlayClip = Defaults.ParentClip,
): Modifier

/**
* Creates a shared element transition where the shared element is __NOT__ seekable
* with the transition. Instead, it always "sticks" to the "active" pane as specified by
* [PaneScope.isActive].
*
* This is a pane specific implementation of [SharedTransitionScope.sharedElement], where
* implementations can specify extra logic for how the shared element behaves when
* rendered concurrently in multiple panes.
*
* @see [SharedTransitionScope.sharedElement].
*/
fun Modifier.paneStickySharedElement(
sharedContentState: SharedTransitionScope.SharedContentState,
boundsTransform: BoundsTransform = Defaults.DefaultBoundsTransform,
placeHolderSize: PlaceHolderSize = contentSize,
renderInOverlayDuringTransition: Boolean = true,
zIndexInOverlay: Float = 0f,
clipInOverlayDuringTransition: OverlayClip = Defaults.ParentClip,
): Modifier
Expand Down
Loading