1 /* 2 * Copyright (C) 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 18 19 import androidx.compose.foundation.gestures.Orientation 20 import androidx.compose.ui.unit.Density 21 import androidx.compose.ui.unit.Dp 22 import androidx.compose.ui.unit.IntOffset 23 import androidx.compose.ui.unit.IntSize 24 import androidx.compose.ui.unit.LayoutDirection 25 import androidx.compose.ui.unit.dp 26 27 /** The edge of a [SceneTransitionLayout]. */ 28 enum class Edge(private val resolveEdge: (LayoutDirection) -> Resolved) : SwipeSource { <lambda>null29 Top(resolveEdge = { Resolved.Top }), <lambda>null30 Bottom(resolveEdge = { Resolved.Bottom }), <lambda>null31 Left(resolveEdge = { Resolved.Left }), <lambda>null32 Right(resolveEdge = { Resolved.Right }), <lambda>null33 Start(resolveEdge = { if (it == LayoutDirection.Ltr) Resolved.Left else Resolved.Right }), <lambda>null34 End(resolveEdge = { if (it == LayoutDirection.Ltr) Resolved.Right else Resolved.Left }); 35 resolvenull36 override fun resolve(layoutDirection: LayoutDirection): Resolved { 37 return resolveEdge(layoutDirection) 38 } 39 40 enum class Resolved : SwipeSource.Resolved { 41 Left, 42 Right, 43 Top, 44 Bottom, 45 } 46 } 47 48 val DefaultEdgeDetector = FixedSizeEdgeDetector(40.dp) 49 50 /** An [SwipeSourceDetector] that detects edges assuming a fixed edge size of [size]. */ 51 class FixedSizeEdgeDetector(val size: Dp) : SwipeSourceDetector { sourcenull52 override fun source( 53 layoutSize: IntSize, 54 position: IntOffset, 55 density: Density, 56 orientation: Orientation, 57 ): Edge.Resolved? { 58 val axisSize: Int 59 val axisPosition: Int 60 val topOrLeft: Edge.Resolved 61 val bottomOrRight: Edge.Resolved 62 when (orientation) { 63 Orientation.Horizontal -> { 64 axisSize = layoutSize.width 65 axisPosition = position.x 66 topOrLeft = Edge.Resolved.Left 67 bottomOrRight = Edge.Resolved.Right 68 } 69 Orientation.Vertical -> { 70 axisSize = layoutSize.height 71 axisPosition = position.y 72 topOrLeft = Edge.Resolved.Top 73 bottomOrRight = Edge.Resolved.Bottom 74 } 75 } 76 77 val sizePx = with(density) { size.toPx() } 78 return when { 79 axisPosition <= sizePx -> topOrLeft 80 axisPosition >= axisSize - sizePx -> bottomOrRight 81 else -> null 82 } 83 } 84 } 85