diff options
| author | 2024-11-05 13:48:46 +0000 | |
|---|---|---|
| committer | 2024-11-05 13:48:46 +0000 | |
| commit | 340d94002427878d3534b9879aab4cf85fcb475d (patch) | |
| tree | 7df28df5dc235b7d64f6d8ee5b5e313eb9453e42 | |
| parent | ac951f83c1d34b2f60f545da2898ab4df0baa528 (diff) | |
| parent | 99a000c39cbaa1281ac1705c679afbaa0565600e (diff) | |
Merge "Move TransformationRange outside of Transformation" into main
7 files changed, 77 insertions, 88 deletions
diff --git a/packages/SystemUI/compose/scene/src/com/android/compose/animation/scene/Element.kt b/packages/SystemUI/compose/scene/src/com/android/compose/animation/scene/Element.kt index 63c5d7aed3e1..e7b66c5f0d2f 100644 --- a/packages/SystemUI/compose/scene/src/com/android/compose/animation/scene/Element.kt +++ b/packages/SystemUI/compose/scene/src/com/android/compose/animation/scene/Element.kt @@ -52,6 +52,7 @@ import com.android.compose.animation.scene.content.Content import com.android.compose.animation.scene.content.state.TransitionState import com.android.compose.animation.scene.transformation.PropertyTransformation import com.android.compose.animation.scene.transformation.SharedElementTransformation +import com.android.compose.animation.scene.transformation.TransformationWithRange import com.android.compose.modifiers.thenIf import com.android.compose.ui.graphics.drawInContainer import com.android.compose.ui.util.lerp @@ -187,6 +188,7 @@ private fun Modifier.maybeElevateInContent( state.transformationSpec .transformations(key, content.key) .shared + ?.transformation ?.elevateInContent == content.key && isSharedElement(stateByContent, state) && isSharedElementEnabled(key, state) && @@ -901,7 +903,7 @@ private fun shouldPlaceElement( } val sharedTransformation = sharedElementTransformation(element.key, transition) - if (sharedTransformation?.enabled == false) { + if (sharedTransformation?.transformation?.enabled == false) { return true } @@ -954,13 +956,13 @@ private fun isSharedElementEnabled( element: ElementKey, transition: TransitionState.Transition, ): Boolean { - return sharedElementTransformation(element, transition)?.enabled ?: true + return sharedElementTransformation(element, transition)?.transformation?.enabled ?: true } internal fun sharedElementTransformation( element: ElementKey, transition: TransitionState.Transition, -): SharedElementTransformation? { +): TransformationWithRange<SharedElementTransformation>? { val transformationSpec = transition.transformationSpec val sharedInFromContent = transformationSpec.transformations(element, transition.fromContent).shared @@ -1244,7 +1246,7 @@ private inline fun <T> computeValue( element: Element, transition: TransitionState.Transition?, contentValue: (Element.State) -> T, - transformation: (ElementTransformations) -> PropertyTransformation<T>?, + transformation: (ElementTransformations) -> TransformationWithRange<PropertyTransformation<T>>?, currentValue: () -> T, isSpecified: (T) -> Boolean, lerp: (T, T, Float) -> T, @@ -1280,7 +1282,7 @@ private inline fun <T> computeValue( checkNotNull(if (currentContent == toContent) toState else fromState) val idleValue = contentValue(overscrollState) val targetValue = - with(propertySpec) { + with(propertySpec.transformation) { layoutImpl.propertyTransformationScope.transform( currentContent, element.key, @@ -1375,7 +1377,7 @@ private inline fun <T> computeValue( val idleValue = contentValue(contentState) val isEntering = content == toContent val previewTargetValue = - with(previewTransformation) { + with(previewTransformation.transformation) { layoutImpl.propertyTransformationScope.transform( content, element.key, @@ -1386,7 +1388,7 @@ private inline fun <T> computeValue( val targetValueOrNull = transformation?.let { transformation -> - with(transformation) { + with(transformation.transformation) { layoutImpl.propertyTransformationScope.transform( content, element.key, @@ -1461,7 +1463,7 @@ private inline fun <T> computeValue( val idleValue = contentValue(contentState) val targetValue = - with(transformation) { + with(transformation.transformation) { layoutImpl.propertyTransformationScope.transform( content, element.key, diff --git a/packages/SystemUI/compose/scene/src/com/android/compose/animation/scene/SceneTransitionLayoutState.kt b/packages/SystemUI/compose/scene/src/com/android/compose/animation/scene/SceneTransitionLayoutState.kt index e1e2411da080..61332b61ed1b 100644 --- a/packages/SystemUI/compose/scene/src/com/android/compose/animation/scene/SceneTransitionLayoutState.kt +++ b/packages/SystemUI/compose/scene/src/com/android/compose/animation/scene/SceneTransitionLayoutState.kt @@ -764,7 +764,8 @@ internal class MutableSceneTransitionLayoutStateImpl( return@fastForEach } - state.transformationSpec.transformations.fastForEach { transformation -> + state.transformationSpec.transformations.fastForEach { transformationWithRange -> + val transformation = transformationWithRange.transformation if ( transformation is SharedElementTransformation && transformation.elevateInContent != null diff --git a/packages/SystemUI/compose/scene/src/com/android/compose/animation/scene/SceneTransitions.kt b/packages/SystemUI/compose/scene/src/com/android/compose/animation/scene/SceneTransitions.kt index 8866fbfbf194..b083f79aebf5 100644 --- a/packages/SystemUI/compose/scene/src/com/android/compose/animation/scene/SceneTransitions.kt +++ b/packages/SystemUI/compose/scene/src/com/android/compose/animation/scene/SceneTransitions.kt @@ -33,10 +33,10 @@ import com.android.compose.animation.scene.transformation.EdgeTranslate import com.android.compose.animation.scene.transformation.Fade import com.android.compose.animation.scene.transformation.OverscrollTranslate import com.android.compose.animation.scene.transformation.PropertyTransformation -import com.android.compose.animation.scene.transformation.RangedPropertyTransformation import com.android.compose.animation.scene.transformation.ScaleSize import com.android.compose.animation.scene.transformation.SharedElementTransformation import com.android.compose.animation.scene.transformation.Transformation +import com.android.compose.animation.scene.transformation.TransformationWithRange import com.android.compose.animation.scene.transformation.Translate /** The transitions configuration of a [SceneTransitionLayout]. */ @@ -233,7 +233,7 @@ interface TransformationSpec { val distance: UserActionDistance? /** The list of [Transformation] applied to elements during this transition. */ - val transformations: List<Transformation> + val transformations: List<TransformationWithRange<*>> companion object { internal val Empty = @@ -325,7 +325,7 @@ internal class TransformationSpecImpl( override val progressSpec: AnimationSpec<Float>, override val swipeSpec: SpringSpec<Float>?, override val distance: UserActionDistance?, - override val transformations: List<Transformation>, + override val transformations: List<TransformationWithRange<*>>, ) : TransformationSpec { private val cache = mutableMapOf<ElementKey, MutableMap<ContentKey, ElementTransformations>>() @@ -340,59 +340,65 @@ internal class TransformationSpecImpl( element: ElementKey, content: ContentKey, ): ElementTransformations { - var shared: SharedElementTransformation? = null - var offset: PropertyTransformation<Offset>? = null - var size: PropertyTransformation<IntSize>? = null - var drawScale: PropertyTransformation<Scale>? = null - var alpha: PropertyTransformation<Float>? = null - - fun <T> onPropertyTransformation( - root: PropertyTransformation<T>, - current: PropertyTransformation<T> = root, - ) { - when (current) { + var shared: TransformationWithRange<SharedElementTransformation>? = null + var offset: TransformationWithRange<PropertyTransformation<Offset>>? = null + var size: TransformationWithRange<PropertyTransformation<IntSize>>? = null + var drawScale: TransformationWithRange<PropertyTransformation<Scale>>? = null + var alpha: TransformationWithRange<PropertyTransformation<Float>>? = null + + transformations.fastForEach { transformationWithRange -> + val transformation = transformationWithRange.transformation + if (!transformation.matcher.matches(element, content)) { + return@fastForEach + } + + when (transformation) { + is SharedElementTransformation -> { + throwIfNotNull(shared, element, name = "shared") + shared = + transformationWithRange + as TransformationWithRange<SharedElementTransformation> + } is Translate, is OverscrollTranslate, is EdgeTranslate, is AnchoredTranslate -> { throwIfNotNull(offset, element, name = "offset") - offset = root as PropertyTransformation<Offset> + offset = + transformationWithRange + as TransformationWithRange<PropertyTransformation<Offset>> } is ScaleSize, is AnchoredSize -> { throwIfNotNull(size, element, name = "size") - size = root as PropertyTransformation<IntSize> + size = + transformationWithRange + as TransformationWithRange<PropertyTransformation<IntSize>> } is DrawScale -> { throwIfNotNull(drawScale, element, name = "drawScale") - drawScale = root as PropertyTransformation<Scale> + drawScale = + transformationWithRange + as TransformationWithRange<PropertyTransformation<Scale>> } is Fade -> { throwIfNotNull(alpha, element, name = "alpha") - alpha = root as PropertyTransformation<Float> - } - is RangedPropertyTransformation -> onPropertyTransformation(root, current.delegate) - } - } - - transformations.fastForEach { transformation -> - if (!transformation.matcher.matches(element, content)) { - return@fastForEach - } - - when (transformation) { - is SharedElementTransformation -> { - throwIfNotNull(shared, element, name = "shared") - shared = transformation + alpha = + transformationWithRange + as TransformationWithRange<PropertyTransformation<Float>> } - is PropertyTransformation<*> -> onPropertyTransformation(transformation) + else -> error("Unknown transformation: $transformation") } } return ElementTransformations(shared, offset, size, drawScale, alpha) } - private fun throwIfNotNull(previous: Transformation?, element: ElementKey, name: String) { + private fun throwIfNotNull( + previous: TransformationWithRange<*>?, + element: ElementKey, + name: String, + ) { if (previous != null) { error("$element has multiple $name transformations") } @@ -401,9 +407,9 @@ internal class TransformationSpecImpl( /** The transformations of an element during a transition. */ internal class ElementTransformations( - val shared: SharedElementTransformation?, - val offset: PropertyTransformation<Offset>?, - val size: PropertyTransformation<IntSize>?, - val drawScale: PropertyTransformation<Scale>?, - val alpha: PropertyTransformation<Float>?, + val shared: TransformationWithRange<SharedElementTransformation>?, + val offset: TransformationWithRange<PropertyTransformation<Offset>>?, + val size: TransformationWithRange<PropertyTransformation<IntSize>>?, + val drawScale: TransformationWithRange<PropertyTransformation<Scale>>?, + val alpha: TransformationWithRange<PropertyTransformation<Float>>?, ) diff --git a/packages/SystemUI/compose/scene/src/com/android/compose/animation/scene/TransitionDslImpl.kt b/packages/SystemUI/compose/scene/src/com/android/compose/animation/scene/TransitionDslImpl.kt index 269d91b02e7d..e461f9ccc295 100644 --- a/packages/SystemUI/compose/scene/src/com/android/compose/animation/scene/TransitionDslImpl.kt +++ b/packages/SystemUI/compose/scene/src/com/android/compose/animation/scene/TransitionDslImpl.kt @@ -34,12 +34,11 @@ import com.android.compose.animation.scene.transformation.DrawScale import com.android.compose.animation.scene.transformation.EdgeTranslate import com.android.compose.animation.scene.transformation.Fade import com.android.compose.animation.scene.transformation.OverscrollTranslate -import com.android.compose.animation.scene.transformation.PropertyTransformation -import com.android.compose.animation.scene.transformation.RangedPropertyTransformation import com.android.compose.animation.scene.transformation.ScaleSize import com.android.compose.animation.scene.transformation.SharedElementTransformation import com.android.compose.animation.scene.transformation.Transformation import com.android.compose.animation.scene.transformation.TransformationRange +import com.android.compose.animation.scene.transformation.TransformationWithRange import com.android.compose.animation.scene.transformation.Translate internal fun transitionsImpl(builder: SceneTransitionsBuilder.() -> Unit): SceneTransitions { @@ -158,7 +157,7 @@ private class SceneTransitionsBuilderImpl : SceneTransitionsBuilder { } internal abstract class BaseTransitionBuilderImpl : BaseTransitionBuilder { - val transformations = mutableListOf<Transformation>() + val transformations = mutableListOf<TransformationWithRange<*>>() private var range: TransformationRange? = null protected var reversed = false override var distance: UserActionDistance? = null @@ -174,19 +173,13 @@ internal abstract class BaseTransitionBuilderImpl : BaseTransitionBuilder { range = null } - protected fun transformation(transformation: PropertyTransformation<*>) { - val transformation = - if (range != null) { - RangedPropertyTransformation(transformation, range!!) - } else { - transformation - } - + protected fun transformation(transformation: Transformation) { + val transformationWithRange = TransformationWithRange(transformation, range) transformations.add( if (reversed) { - transformation.reversed() + transformationWithRange.reversed() } else { - transformation + transformationWithRange } ) } @@ -264,7 +257,7 @@ internal class TransitionBuilderImpl(override val transition: TransitionState.Tr "(${transition.toContent.debugName})" } - transformations.add(SharedElementTransformation(matcher, enabled, elevateInContent)) + transformation(SharedElementTransformation(matcher, enabled, elevateInContent)) } override fun timestampRange( diff --git a/packages/SystemUI/compose/scene/src/com/android/compose/animation/scene/transformation/DrawScale.kt b/packages/SystemUI/compose/scene/src/com/android/compose/animation/scene/transformation/DrawScale.kt index 7223dad43a2e..47692d07e8dc 100644 --- a/packages/SystemUI/compose/scene/src/com/android/compose/animation/scene/transformation/DrawScale.kt +++ b/packages/SystemUI/compose/scene/src/com/android/compose/animation/scene/transformation/DrawScale.kt @@ -33,7 +33,6 @@ internal class DrawScale( private val scaleY: Float, private val pivot: Offset = Offset.Unspecified, ) : PropertyTransformation<Scale> { - override fun PropertyTransformationScope.transform( content: ContentKey, element: ElementKey, diff --git a/packages/SystemUI/compose/scene/src/com/android/compose/animation/scene/transformation/Transformation.kt b/packages/SystemUI/compose/scene/src/com/android/compose/animation/scene/transformation/Transformation.kt index d38067d9af38..2ca7d36c22d0 100644 --- a/packages/SystemUI/compose/scene/src/com/android/compose/animation/scene/transformation/Transformation.kt +++ b/packages/SystemUI/compose/scene/src/com/android/compose/animation/scene/transformation/Transformation.kt @@ -36,14 +36,6 @@ sealed interface Transformation { */ val matcher: ElementMatcher - /** - * The range during which the transformation is applied. If it is `null`, then the - * transformation will be applied throughout the whole scene transition. - */ - // TODO(b/240432457): Move this back to PropertyTransformation. - val range: TransformationRange? - get() = null - /* * Reverse this transformation. This is called when we use Transition(from = A, to = B) when * animating from B to A and there is no Transition(from = B, to = A) defined. @@ -82,20 +74,15 @@ interface PropertyTransformationScope : Density, ElementStateScope { val layoutDirection: LayoutDirection } -/** - * A [PropertyTransformation] associated to a range. This is a helper class so that normal - * implementations of [PropertyTransformation] don't have to take care of reversing their range when - * they are reversed. - */ -internal class RangedPropertyTransformation<T>( - val delegate: PropertyTransformation<T>, - override val range: TransformationRange, -) : PropertyTransformation<T> by delegate { - override fun reversed(): Transformation { - return RangedPropertyTransformation( - delegate.reversed() as PropertyTransformation<T>, - range.reversed(), - ) +/** A pair consisting of a [transformation] and optional [range]. */ +class TransformationWithRange<out T : Transformation>( + val transformation: T, + val range: TransformationRange?, +) { + fun reversed(): TransformationWithRange<T> { + if (range == null) return this + + return TransformationWithRange(transformation = transformation, range = range.reversed()) } } diff --git a/packages/SystemUI/compose/scene/tests/src/com/android/compose/animation/scene/TransitionDslTest.kt b/packages/SystemUI/compose/scene/tests/src/com/android/compose/animation/scene/TransitionDslTest.kt index d66d6b3ab219..d31711496ff0 100644 --- a/packages/SystemUI/compose/scene/tests/src/com/android/compose/animation/scene/TransitionDslTest.kt +++ b/packages/SystemUI/compose/scene/tests/src/com/android/compose/animation/scene/TransitionDslTest.kt @@ -28,8 +28,8 @@ import com.android.compose.animation.scene.TestScenes.SceneB import com.android.compose.animation.scene.TestScenes.SceneC import com.android.compose.animation.scene.content.state.TransitionState import com.android.compose.animation.scene.transformation.OverscrollTranslate -import com.android.compose.animation.scene.transformation.Transformation import com.android.compose.animation.scene.transformation.TransformationRange +import com.android.compose.animation.scene.transformation.TransformationWithRange import com.android.compose.test.transition import com.google.common.truth.Correspondence import com.google.common.truth.Truth.assertThat @@ -310,7 +310,8 @@ class TransitionDslTest { } val overscrollSpec = transitions.overscrollSpecs.single() - val transformation = overscrollSpec.transformationSpec.transformations.single() + val transformation = + overscrollSpec.transformationSpec.transformations.single().transformation assertThat(transformation).isInstanceOf(OverscrollTranslate::class.java) } @@ -344,7 +345,7 @@ class TransitionDslTest { companion object { private val TRANSFORMATION_RANGE = - Correspondence.transforming<Transformation, TransformationRange?>( + Correspondence.transforming<TransformationWithRange<*>, TransformationRange?>( { it?.range }, "has range equal to", ) |