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.effect 18 19 import androidx.compose.foundation.OverscrollEffect 20 import androidx.compose.ui.geometry.Offset 21 import androidx.compose.ui.input.nestedscroll.NestedScrollSource 22 import androidx.compose.ui.unit.Velocity 23 24 /** An overscroll effect that ensures only a single fling animation is triggered. */ 25 internal class GestureEffect(private val delegate: OverscrollEffect) : <lambda>null26 OverscrollEffect by delegate { 27 private var shouldFling = false 28 29 override fun applyToScroll( 30 delta: Offset, 31 source: NestedScrollSource, 32 performScroll: (Offset) -> Offset, 33 ): Offset { 34 shouldFling = true 35 return delegate.applyToScroll(delta, source, performScroll) 36 } 37 38 override suspend fun applyToFling( 39 velocity: Velocity, 40 performFling: suspend (Velocity) -> Velocity, 41 ) { 42 if (!shouldFling) { 43 performFling(velocity) 44 return 45 } 46 shouldFling = false 47 delegate.applyToFling(velocity, performFling) 48 } 49 50 suspend fun ensureApplyToFlingIsCalled() { 51 applyToFling(Velocity.Zero) { Velocity.Zero } 52 } 53 } 54