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

Commit 9f109d7c authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge "refactor(MM): Move ComposableMotionValue to mechanics lib (1/2)" into main

parents 0e46bf2d 973a250a
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
}