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.ui.util
18 
19 import androidx.compose.foundation.gestures.Orientation
20 import androidx.compose.ui.geometry.Offset
21 import androidx.compose.ui.unit.IntOffset
22 import androidx.compose.ui.unit.Velocity
23 
24 interface SpaceVectorConverter {
toFloatnull25     fun Offset.toFloat(): Float
26 
27     fun Velocity.toFloat(): Float
28 
29     fun IntOffset.toInt(): Int
30 
31     fun Float.toOffset(): Offset
32 
33     fun Float.toVelocity(): Velocity
34 
35     fun Int.toIntOffset(): IntOffset
36 }
37 
38 fun SpaceVectorConverter(orientation: Orientation) =
39     when (orientation) {
40         Orientation.Horizontal -> HorizontalConverter
41         Orientation.Vertical -> VerticalConverter
42     }
43 
44 private data object HorizontalConverter : SpaceVectorConverter {
toFloatnull45     override fun Offset.toFloat() = x
46 
47     override fun Velocity.toFloat() = x
48 
49     override fun IntOffset.toInt() = x
50 
51     override fun Float.toOffset() = Offset(this, 0f)
52 
53     override fun Float.toVelocity() = Velocity(this, 0f)
54 
55     override fun Int.toIntOffset() = IntOffset(this, 0)
56 }
57 
58 private data object VerticalConverter : SpaceVectorConverter {
59     override fun Offset.toFloat() = y
60 
61     override fun Velocity.toFloat() = y
62 
63     override fun IntOffset.toInt() = y
64 
65     override fun Float.toOffset() = Offset(0f, this)
66 
67     override fun Float.toVelocity() = Velocity(0f, this)
68 
69     override fun Int.toIntOffset() = IntOffset(0, this)
70 }
71