1 /* 2 * Copyright (C) 2024 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.input.pointer.PointerInputChange 21 import androidx.compose.ui.input.pointer.positionChange 22 import androidx.compose.ui.unit.Density 23 import androidx.compose.ui.unit.IntOffset 24 import androidx.compose.ui.unit.IntSize 25 import kotlin.math.abs 26 27 private const val TRAVEL_RATIO_THRESHOLD = .5f 28 29 /** 30 * {@link CommunalSwipeDetector} provides an implementation of {@link SwipeDetector} and {@link 31 * SwipeSourceDetector} to enable fullscreen swipe handling to transition to and from the glanceable 32 * hub. 33 */ 34 class CommunalSwipeDetector(private var lastDirection: SwipeSource? = null) : 35 SwipeSourceDetector, SwipeDetector { sourcenull36 override fun source( 37 layoutSize: IntSize, 38 position: IntOffset, 39 density: Density, 40 orientation: Orientation 41 ): SwipeSource? { 42 return lastDirection 43 } 44 detectSwipenull45 override fun detectSwipe(change: PointerInputChange): Boolean { 46 if (change.positionChange().x > 0) { 47 lastDirection = Edge.Left 48 } else { 49 lastDirection = Edge.Right 50 } 51 52 // Determine whether the ratio of the distance traveled horizontally to the distance 53 // traveled vertically exceeds the threshold. 54 return abs(change.positionChange().x / change.positionChange().y) > TRAVEL_RATIO_THRESHOLD 55 } 56 } 57