1 /* 2 * Copyright 2023 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.compose.animation.scene.transformation 18 19 import androidx.compose.ui.geometry.Offset 20 import androidx.compose.ui.unit.Dp 21 import androidx.compose.ui.unit.dp 22 import com.android.compose.animation.scene.Element 23 import com.android.compose.animation.scene.ElementMatcher 24 import com.android.compose.animation.scene.OverscrollScope 25 import com.android.compose.animation.scene.SceneKey 26 import com.android.compose.animation.scene.SceneTransitionLayoutImpl 27 import com.android.compose.animation.scene.TransitionState 28 29 internal class Translate( 30 override val matcher: ElementMatcher, 31 private val x: Dp = 0.dp, 32 private val y: Dp = 0.dp, 33 ) : PropertyTransformation<Offset> { transformnull34 override fun transform( 35 layoutImpl: SceneTransitionLayoutImpl, 36 scene: SceneKey, 37 element: Element, 38 sceneState: Element.SceneState, 39 transition: TransitionState.Transition, 40 value: Offset, 41 ): Offset { 42 return with(layoutImpl.density) { 43 Offset( 44 value.x + x.toPx(), 45 value.y + y.toPx(), 46 ) 47 } 48 } 49 } 50 51 internal class OverscrollTranslate( 52 override val matcher: ElementMatcher, <lambda>null53 val x: OverscrollScope.() -> Float = { 0f }, <lambda>null54 val y: OverscrollScope.() -> Float = { 0f }, 55 ) : PropertyTransformation<Offset> { transformnull56 override fun transform( 57 layoutImpl: SceneTransitionLayoutImpl, 58 scene: SceneKey, 59 element: Element, 60 sceneState: Element.SceneState, 61 transition: TransitionState.Transition, 62 value: Offset, 63 ): Offset { 64 // As this object is created by OverscrollBuilderImpl and we retrieve the current 65 // OverscrollSpec only when the transition implements HasOverscrollProperties, we can assume 66 // that this method was invoked after performing this check. 67 val overscrollProperties = transition as TransitionState.HasOverscrollProperties 68 69 return Offset( 70 x = value.x + overscrollProperties.overscrollScope.x(), 71 y = value.y + overscrollProperties.overscrollScope.y(), 72 ) 73 } 74 } 75