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

Commit 064e7215 authored by Mike Schneider's avatar Mike Schneider
Browse files

Add `identity` mapping to `DirectionalBuilderScope`

This is a common usecase for spatial effects. This will simplify
deleting the FluentBuilder in the next CL

Bug: 406734758
Test: DirectionalBuilderImplTest
Flag: com.android.systemui.scene_container
Change-Id: Ide47b5a53f636cea1abb1ea5b08b178760c57e3b
parent d639c1e4
Loading
Loading
Loading
Loading
+38 −0
Original line number Diff line number Diff line
@@ -229,6 +229,44 @@ interface DirectionalBuilderScope {
        semantics: List<SemanticValue<*>> = emptyList(),
        mapping: Mapping,
    ): CanBeLastSegment

    /**
     * Ends the current segment at the [breakpoint] position and defines the next segment to produce
     * the input value as output (optionally with an offset of [delta]).
     *
     * Note: This segment can be used as the last segment in the specification.
     *
     * @param breakpoint The breakpoint defining the end of the current segment and the start of the
     *   next.
     * @param delta An optional offset to apply to the mapped value to determine the constant value.
     * @param spring The [SpringParameters] for the transition to this breakpoint.
     * @param guarantee The animation guarantee for this transition.
     * @param key A unique [BreakpointKey] for this breakpoint.
     * @param semantics Updated semantics values to be applied. Must be a subset of the
     *   [SemanticKey]s used when first creating this builder.
     */
    fun identity(
        breakpoint: Float,
        delta: Float = 0f,
        spring: SpringParameters = defaultSpring,
        guarantee: Guarantee = Guarantee.None,
        key: BreakpointKey = BreakpointKey(),
        semantics: List<SemanticValue<*>> = emptyList(),
    ): CanBeLastSegment {
        return if (delta == 0f) {
            mapping(breakpoint, spring, guarantee, key, semantics, Mapping.Identity)
        } else {
            fractionalInput(
                breakpoint,
                fraction = 1f,
                from = breakpoint + delta,
                spring = spring,
                guarantee = guarantee,
                key = key,
                semantics = semantics,
            )
        }
    }
}

/** Marker interface to indicate that a segment can be the last one in a [DirectionalMotionSpec]. */
+2 −2
Original line number Diff line number Diff line
@@ -33,9 +33,9 @@ import com.android.mechanics.spring.SpringParameters
 * @see directionalMotionSpec
 */
fun MotionBuilderContext.spatialDirectionalMotionSpec(
    defaultSpring: SpringParameters = this.spatial.default,
    initialMapping: Mapping = Mapping.Identity,
    semantics: List<SemanticValue<*>> = emptyList(),
    defaultSpring: SpringParameters = this.spatial.default,
    init: DirectionalBuilderFn,
) = directionalMotionSpec(defaultSpring, initialMapping, semantics, init)

@@ -49,9 +49,9 @@ fun MotionBuilderContext.spatialDirectionalMotionSpec(
 * @see directionalMotionSpec
 */
fun MotionBuilderContext.effectsDirectionalMotionSpec(
    defaultSpring: SpringParameters = this.effects.default,
    initialMapping: Mapping = Mapping.Zero,
    semantics: List<SemanticValue<*>> = emptyList(),
    defaultSpring: SpringParameters = this.effects.default,
    init: DirectionalBuilderFn,
) = directionalMotionSpec(defaultSpring, initialMapping, semantics, init)

+18 −0
Original line number Diff line number Diff line
@@ -167,6 +167,24 @@ class DirectionalBuilderImplTest {
            .inOrder()
    }

    @Test
    fun directionalSpec_mappingBuilder_identity_addsIdentityMapping() {
        val result = directionalMotionSpec(Spring, Mapping.Zero) { identity(breakpoint = 10f) }
        assertThat(result).mappings().containsExactly(Mapping.Zero, Mapping.Identity).inOrder()
        assertThat(result).breakpoints().positions().containsExactly(10f)
    }

    @Test
    fun directionalSpec_mappingBuilder_identityWithDelta_producesLinearMapping() {
        val result =
            directionalMotionSpec(Spring, Mapping.Zero) { identity(breakpoint = 10f, delta = 2f) }

        assertThat(result)
            .mappings()
            .atOrAfter(10f)
            .matchesLinearMapping(in1 = 10f, out1 = 12f, in2 = 20f, out2 = 22f)
    }

    @Test
    fun semantics_appliedForSingleSegment() {
        val result = directionalMotionSpec(Mapping.Identity, listOf(S1 with "One", S2 with "Two"))