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

Commit 9ff8f95b authored by Android Build Coastguard Worker's avatar Android Build Coastguard Worker
Browse files

Snap for 13812341 from 9f109d7c to 25Q4-release

Change-Id: I2d76243988ebac76cebdf85b372a5384c110c0aa
parents a7adca8e 9f109d7c
Loading
Loading
Loading
Loading
+79 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2025 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.mechanics

import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.SideEffect
import androidx.compose.runtime.remember
import com.android.mechanics.spec.MotionSpec

@Composable
fun rememberMotionValue(
    input: () -> Float,
    spec: () -> MotionSpec,
    gestureContext: GestureContext,
    stableThreshold: Float = 0.01f,
    label: String? = null,
): MotionValue {
    val motionValue =
        remember(input) {
            MotionValue(
                input,
                gestureContext,
                initialSpec = spec(),
                label = label,
                stableThreshold = stableThreshold,
            )
        }

    val currentSpec = spec()
    SideEffect {
        // New spec is intentionally only applied after recomposition.
        motionValue.spec = currentSpec
    }

    LaunchedEffect(motionValue) { motionValue.keepRunning() }
    return motionValue
}

@Composable
fun rememberDerivedMotionValue(
    input: MotionValue,
    spec: () -> MotionSpec,
    stableThreshold: Float = 0.01f,
    label: String? = null,
): MotionValue {
    val motionValue =
        remember(input) {
            MotionValue.createDerived(
                input,
                initialSpec = spec(),
                label = label,
                stableThreshold = stableThreshold,
            )
        }

    val currentSpec = spec()
    SideEffect {
        // New spec is intentionally only applied after recomposition.
        motionValue.spec = currentSpec
    }

    LaunchedEffect(motionValue) { motionValue.keepRunning() }
    return motionValue
}