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.Velocity
22 
23 interface SpaceVectorConverter {
toFloatnull24     fun Offset.toFloat(): Float
25     fun Velocity.toFloat(): Float
26     fun Float.toOffset(): Offset
27     fun Float.toVelocity(): Velocity
28 }
29 
30 fun SpaceVectorConverter(orientation: Orientation) =
31     when (orientation) {
32         Orientation.Horizontal -> HorizontalConverter
33         Orientation.Vertical -> VerticalConverter
34     }
35 
36 private val HorizontalConverter =
37     object : SpaceVectorConverter {
toFloatnull38         override fun Offset.toFloat() = x
39         override fun Velocity.toFloat() = x
40         override fun Float.toOffset() = Offset(this, 0f)
41         override fun Float.toVelocity() = Velocity(this, 0f)
42     }
43 
44 private val VerticalConverter =
45     object : SpaceVectorConverter {
46         override fun Offset.toFloat() = y
47         override fun Velocity.toFloat() = y
48         override fun Float.toOffset() = Offset(0f, this)
49         override fun Float.toVelocity() = Velocity(0f, this)
50     }
51