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

Commit d639c1e4 authored by Mike Schneider's avatar Mike Schneider
Browse files

Make buildDirectionalMotionSpec match the style of `motionSpec` builder

- Move to the builder sub-package
- create spatial/effects specific variants on MotionBuilderContext
- remove the `build` from the name

Bug: 406734758
Test: Existing unit tests
Flag: com.android.systemui.scene_container
Change-Id: Id36082ac52f4b1f9765dce354f62edd880f261ab
parent 917d8ee9
Loading
Loading
Loading
Loading
+3 −3
Original line number Diff line number Diff line
@@ -29,7 +29,7 @@ import com.android.mechanics.spec.Guarantee
import com.android.mechanics.spec.InputDirection
import com.android.mechanics.spec.Mapping
import com.android.mechanics.spec.MotionSpec
import com.android.mechanics.spec.buildDirectionalMotionSpec
import com.android.mechanics.spec.builder.directionalMotionSpec
import com.android.mechanics.spring.SpringParameters
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.launch
@@ -169,7 +169,7 @@ class MotionValueBenchmark {
    private val MotionSpec.Companion.ZeroToOne_AtOne
        get() =
            MotionSpec(
                buildDirectionalMotionSpec(
                directionalMotionSpec(
                    defaultSpring = SpringParameters(stiffness = 300f, dampingRatio = .9f),
                    initialMapping = Mapping.Zero,
                ) {
@@ -228,7 +228,7 @@ class MotionValueBenchmark {
    private val MotionSpec.Companion.ZeroToOne_AtOne_WithGuarantee
        get() =
            MotionSpec(
                buildDirectionalMotionSpec(
                directionalMotionSpec(
                    defaultSpring = SpringParameters(stiffness = 300f, dampingRatio = .9f),
                    initialMapping = Mapping.Zero,
                ) {
+3 −3
Original line number Diff line number Diff line
@@ -34,8 +34,8 @@ import com.android.mechanics.spec.MotionSpec
import com.android.mechanics.spec.OnChangeSegmentHandler
import com.android.mechanics.spec.SegmentData
import com.android.mechanics.spec.SegmentKey
import com.android.mechanics.spec.buildDirectionalMotionSpec
import com.android.mechanics.spec.builder
import com.android.mechanics.spec.builder.directionalMotionSpec
import com.android.mechanics.spec.reverseBuilder
import com.android.mechanics.spring.SpringParameters

@@ -102,10 +102,10 @@ class VerticalExpandContainerSpec(
    ): MotionSpec {
        return with(density) {
            if (isFloating) {
                MotionSpec(buildDirectionalMotionSpec(Mapping.Fixed(intrinsicWidth)))
                MotionSpec(directionalMotionSpec(Mapping.Fixed(intrinsicWidth)))
            } else {
                MotionSpec(
                    buildDirectionalMotionSpec({ input ->
                    directionalMotionSpec({ input ->
                        val fraction = (input / detachHeight.toPx()).fastCoerceIn(0f, 1f)
                        intrinsicWidth - lerp(widthOffset.toPx(), 0f, fraction)
                    })
+1 −1
Original line number Diff line number Diff line
@@ -28,7 +28,7 @@ import com.android.mechanics.spring.SpringParameters
/**
 * Internal, reusable implementation of the [DirectionalBuilderScope].
 *
 * Clients must use [buildDirectionalMotionSpec] instead.
 * Clients must use [directionalMotionSpec] instead.
 */
internal class DirectionalBuilderImpl(
    override val defaultSpring: SpringParameters,
+41 −6
Original line number Diff line number Diff line
@@ -14,12 +14,47 @@
 * limitations under the License.
 */

package com.android.mechanics.spec
package com.android.mechanics.spec.builder

import com.android.mechanics.spec.builder.DirectionalBuilderFn
import com.android.mechanics.spec.builder.DirectionalBuilderImpl
import com.android.mechanics.spec.Breakpoint
import com.android.mechanics.spec.DirectionalMotionSpec
import com.android.mechanics.spec.Mapping
import com.android.mechanics.spec.SegmentSemanticValues
import com.android.mechanics.spec.SemanticValue
import com.android.mechanics.spring.SpringParameters

/**
 * Builds a [DirectionalMotionSpec] for spatial values by defining a sequence of ([Breakpoint],
 * [Mapping]) pairs
 *
 * The [initialMapping] is [Mapping.Identity], and the Material spatial.default spring is used,
 * unless otherwise specified.
 *
 * @see directionalMotionSpec
 */
fun MotionBuilderContext.spatialDirectionalMotionSpec(
    defaultSpring: SpringParameters = this.spatial.default,
    initialMapping: Mapping = Mapping.Identity,
    semantics: List<SemanticValue<*>> = emptyList(),
    init: DirectionalBuilderFn,
) = directionalMotionSpec(defaultSpring, initialMapping, semantics, init)

/**
 * Builds a [DirectionalMotionSpec] for effects values by defining a sequence of ([Breakpoint],
 * [Mapping]) pairs
 *
 * The [initialMapping] is [Mapping.Zero], and the Material effects.default spring is used, unless
 * otherwise specified.
 *
 * @see directionalMotionSpec
 */
fun MotionBuilderContext.effectsDirectionalMotionSpec(
    defaultSpring: SpringParameters = this.effects.default,
    initialMapping: Mapping = Mapping.Zero,
    semantics: List<SemanticValue<*>> = emptyList(),
    init: DirectionalBuilderFn,
) = directionalMotionSpec(defaultSpring, initialMapping, semantics, init)

/**
 * Builds a [DirectionalMotionSpec] by defining a sequence of ([Breakpoint], [Mapping]) pairs.
 *
@@ -30,7 +65,7 @@ import com.android.mechanics.spring.SpringParameters
 *
 * Example Usage:
 * ```kotlin
 * val motionSpec = buildDirectionalMotionSpec(
 * val motionSpec = directionalMotionSpec(
 *     defaultSpring = materialSpatial,
 *
 *     // Start as a constant transition, always 0.
@@ -56,7 +91,7 @@ import com.android.mechanics.spring.SpringParameters
 *   [initialMapping].
 *     @return The constructed [DirectionalMotionSpec].
 */
fun buildDirectionalMotionSpec(
fun directionalMotionSpec(
    defaultSpring: SpringParameters,
    initialMapping: Mapping = Mapping.Identity,
    semantics: List<SemanticValue<*>> = emptyList(),
@@ -78,7 +113,7 @@ fun buildDirectionalMotionSpec(
 * @param semantics Semantics values for this spec.
 * @return A new [DirectionalMotionSpec] instance configured with the provided parameters.
 */
fun buildDirectionalMotionSpec(
fun directionalMotionSpec(
    mapping: Mapping = Mapping.Identity,
    semantics: List<SemanticValue<*>> = emptyList(),
): DirectionalMotionSpec {
+18 −18
Original line number Diff line number Diff line
@@ -61,8 +61,8 @@ interface EffectApplyScope : MotionBuilderContext {
    /**
     * Defines spec simultaneously for both, the min and max direction.
     *
     * The behavior is the same as for `buildDirectionalMotionSpec`, with the notable exception that
     * the spec to be defined is confined within [minLimit] and [maxLimit]. Specifying breakpoints
     * The behavior is the same as for `directionalMotionSpec`, with the notable exception that the
     * spec to be defined is confined within [minLimit] and [maxLimit]. Specifying breakpoints
     * outside of this range will throw.
     *
     * Will throw if [forward] or [unidirectional] has been called in this scope before.
@@ -73,7 +73,7 @@ interface EffectApplyScope : MotionBuilderContext {
     * @param initialMapping [Mapping] for the first segment after [minLimit].
     * @param semantics Initial semantics for the effect.
     * @param init Configures the effect's spec using [DirectionalBuilderScope].
     * @see com.android.mechanics.spec.buildDirectionalMotionSpec for in-depth documentation.
     * @see com.android.mechanics.spec.directionalMotionSpec for in-depth documentation.
     */
    fun unidirectional(
        initialMapping: Mapping,
@@ -84,8 +84,8 @@ interface EffectApplyScope : MotionBuilderContext {
    /**
     * Defines spec simultaneously for both, the min and max direction, using a single segment only.
     *
     * The behavior is the same as for `buildDirectionalMotionSpec`, with the notable exception that
     * the spec to be defined is confined within [minLimit] and [maxLimit].
     * The behavior is the same as for `directionalMotionSpec`, with the notable exception that the
     * spec to be defined is confined within [minLimit] and [maxLimit].
     *
     * Will throw if [forward] or [unidirectional] has been called in this scope before.
     *
@@ -94,15 +94,15 @@ interface EffectApplyScope : MotionBuilderContext {
     *
     * @param mapping [Mapping] to be used between [minLimit] and [maxLimit].
     * @param semantics Initial semantics for the effect.
     * @see com.android.mechanics.spec.buildDirectionalMotionSpec for in depth documentation.
     * @see com.android.mechanics.spec.directionalMotionSpec for in depth documentation.
     */
    fun unidirectional(mapping: Mapping, semantics: List<SemanticValue<*>> = emptyList())

    /**
     * Defines the spec for max direction.
     *
     * The behavior is the same as for `buildDirectionalMotionSpec`, with the notable exception that
     * the spec to be defined is confined within [minLimit] and [maxLimit]. Specifying breakpoints
     * The behavior is the same as for `directionalMotionSpec`, with the notable exception that the
     * spec to be defined is confined within [minLimit] and [maxLimit]. Specifying breakpoints
     * outside of this range will throw.
     *
     * Will throw if [forward] or [unidirectional] has been called in this scope before.
@@ -113,7 +113,7 @@ interface EffectApplyScope : MotionBuilderContext {
     * @param initialMapping [Mapping] for the first segment after [minLimit].
     * @param semantics Initial semantics for the effect.
     * @param init Configures the effect's spec using [DirectionalBuilderScope].
     * @see com.android.mechanics.spec.buildDirectionalMotionSpec for in-depth documentation.
     * @see com.android.mechanics.spec.directionalMotionSpec for in-depth documentation.
     */
    fun forward(
        initialMapping: Mapping,
@@ -124,8 +124,8 @@ interface EffectApplyScope : MotionBuilderContext {
    /**
     * Defines the spec for max direction, using a single segment only.
     *
     * The behavior is the same as for `buildDirectionalMotionSpec`, with the notable exception that
     * the spec to be defined is confined within [minLimit] and [maxLimit].
     * The behavior is the same as for `directionalMotionSpec`, with the notable exception that the
     * spec to be defined is confined within [minLimit] and [maxLimit].
     *
     * Will throw if [forward] or [unidirectional] has been called in this scope before.
     *
@@ -134,15 +134,15 @@ interface EffectApplyScope : MotionBuilderContext {
     *
     * @param mapping [Mapping] to be used between [minLimit] and [maxLimit].
     * @param semantics Initial semantics for the effect.
     * @see com.android.mechanics.spec.buildDirectionalMotionSpec for in depth documentation.
     * @see com.android.mechanics.spec.directionalMotionSpec for in depth documentation.
     */
    fun forward(mapping: Mapping, semantics: List<SemanticValue<*>> = emptyList())

    /**
     * Defines the spec for min direction.
     *
     * The behavior is the same as for `buildDirectionalMotionSpec`, with the notable exception that
     * the spec to be defined is confined within [minLimit] and [maxLimit]. Specifying breakpoints
     * The behavior is the same as for `directionalMotionSpec`, with the notable exception that the
     * spec to be defined is confined within [minLimit] and [maxLimit]. Specifying breakpoints
     * outside of this range will throw.
     *
     * Will throw if [forward] or [unidirectional] has been called in this scope before.
@@ -153,7 +153,7 @@ interface EffectApplyScope : MotionBuilderContext {
     * @param initialMapping [Mapping] for the first segment after [minLimit].
     * @param semantics Initial semantics for the effect.
     * @param init Configures the effect's spec using [DirectionalBuilderScope].
     * @see com.android.mechanics.spec.buildDirectionalMotionSpec for in-depth documentation.
     * @see com.android.mechanics.spec.directionalMotionSpec for in-depth documentation.
     */
    fun backward(
        initialMapping: Mapping,
@@ -164,8 +164,8 @@ interface EffectApplyScope : MotionBuilderContext {
    /**
     * Defines the spec for min direction, using a single segment only.
     *
     * The behavior is the same as for `buildDirectionalMotionSpec`, with the notable exception that
     * the spec to be defined is confined within [minLimit] and [maxLimit].
     * The behavior is the same as for `directionalMotionSpec`, with the notable exception that the
     * spec to be defined is confined within [minLimit] and [maxLimit].
     *
     * Will throw if [forward] or [unidirectional] has been called in this scope before.
     *
@@ -174,7 +174,7 @@ interface EffectApplyScope : MotionBuilderContext {
     *
     * @param mapping [Mapping] to be used between [minLimit] and [maxLimit].
     * @param semantics Initial semantics for the effect.
     * @see com.android.mechanics.spec.buildDirectionalMotionSpec for in depth documentation.
     * @see com.android.mechanics.spec.directionalMotionSpec for in depth documentation.
     */
    fun backward(mapping: Mapping, semantics: List<SemanticValue<*>> = emptyList())

Loading