Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 3671ae22 authored by Jordan Demeulenaere's avatar Jordan Demeulenaere
Browse files

Extract internal GestureEffect out of ContentOverscrollEffect

This CL is extracting the internal GestureEffect and VisualEffect
classes out of ContentOverscrollEffect, because ContentOverscrollEffect
and OffsetOverscrollEffect are going to be moved to PlatformComposeCore
in ag/31063651.

Bug: 378470603
Test: N/A simple move
Flag: EXEMPT simple move
Change-Id: I1520254d250e798546461e4a411941ca82cff076
parent 451b7bcb
Loading
Loading
Loading
Loading
+0 −53
Original line number Diff line number Diff line
@@ -118,56 +118,3 @@ open class BaseContentOverscrollEffect(
        }
    }
}

/** An overscroll effect that ensures only a single fling animation is triggered. */
internal class GestureEffect(private val delegate: ContentOverscrollEffect) :
    ContentOverscrollEffect by delegate {
    private var shouldFling = false

    override fun applyToScroll(
        delta: Offset,
        source: NestedScrollSource,
        performScroll: (Offset) -> Offset,
    ): Offset {
        shouldFling = true
        return delegate.applyToScroll(delta, source, performScroll)
    }

    override suspend fun applyToFling(
        velocity: Velocity,
        performFling: suspend (Velocity) -> Velocity,
    ) {
        if (!shouldFling) {
            performFling(velocity)
            return
        }
        shouldFling = false
        delegate.applyToFling(velocity, performFling)
    }

    suspend fun ensureApplyToFlingIsCalled() {
        applyToFling(Velocity.Zero) { Velocity.Zero }
    }
}

/**
 * An overscroll effect that only applies visual effects and does not interfere with the actual
 * scrolling or flinging behavior.
 */
internal class VisualEffect(private val delegate: ContentOverscrollEffect) :
    ContentOverscrollEffect by delegate {
    override fun applyToScroll(
        delta: Offset,
        source: NestedScrollSource,
        performScroll: (Offset) -> Offset,
    ): Offset {
        return performScroll(delta)
    }

    override suspend fun applyToFling(
        velocity: Velocity,
        performFling: suspend (Velocity) -> Velocity,
    ) {
        performFling(velocity)
    }
}
+74 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.compose.animation.scene.effect

import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.input.nestedscroll.NestedScrollSource
import androidx.compose.ui.unit.Velocity

/** An overscroll effect that ensures only a single fling animation is triggered. */
internal class GestureEffect(private val delegate: ContentOverscrollEffect) :
    ContentOverscrollEffect by delegate {
    private var shouldFling = false

    override fun applyToScroll(
        delta: Offset,
        source: NestedScrollSource,
        performScroll: (Offset) -> Offset,
    ): Offset {
        shouldFling = true
        return delegate.applyToScroll(delta, source, performScroll)
    }

    override suspend fun applyToFling(
        velocity: Velocity,
        performFling: suspend (Velocity) -> Velocity,
    ) {
        if (!shouldFling) {
            performFling(velocity)
            return
        }
        shouldFling = false
        delegate.applyToFling(velocity, performFling)
    }

    suspend fun ensureApplyToFlingIsCalled() {
        applyToFling(Velocity.Zero) { Velocity.Zero }
    }
}

/**
 * An overscroll effect that only applies visual effects and does not interfere with the actual
 * scrolling or flinging behavior.
 */
internal class VisualEffect(private val delegate: ContentOverscrollEffect) :
    ContentOverscrollEffect by delegate {
    override fun applyToScroll(
        delta: Offset,
        source: NestedScrollSource,
        performScroll: (Offset) -> Offset,
    ): Offset {
        return performScroll(delta)
    }

    override suspend fun applyToFling(
        velocity: Velocity,
        performFling: suspend (Velocity) -> Velocity,
    ) {
        performFling(velocity)
    }
}