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.dp
25 
26 /** The edge of a [SceneTransitionLayout]. */
27 enum class Edge : SwipeSource {
28     Left,
29     Right,
30     Top,
31     Bottom,
32 }
33 
34 val DefaultEdgeDetector = FixedSizeEdgeDetector(40.dp)
35 
36 /** An [SwipeSourceDetector] that detects edges assuming a fixed edge size of [size]. */
37 class FixedSizeEdgeDetector(val size: Dp) : SwipeSourceDetector {
sourcenull38     override fun source(
39         layoutSize: IntSize,
40         position: IntOffset,
41         density: Density,
42         orientation: Orientation,
43     ): Edge? {
44         val axisSize: Int
45         val axisPosition: Int
46         val topOrLeft: Edge
47         val bottomOrRight: Edge
48         when (orientation) {
49             Orientation.Horizontal -> {
50                 axisSize = layoutSize.width
51                 axisPosition = position.x
52                 topOrLeft = Edge.Left
53                 bottomOrRight = Edge.Right
54             }
55             Orientation.Vertical -> {
56                 axisSize = layoutSize.height
57                 axisPosition = position.y
58                 topOrLeft = Edge.Top
59                 bottomOrRight = Edge.Bottom
60             }
61         }
62 
63         val sizePx = with(density) { size.toPx() }
64         return when {
65             axisPosition <= sizePx -> topOrLeft
66             axisPosition >= axisSize - sizePx -> bottomOrRight
67             else -> null
68         }
69     }
70 }
71