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

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

Merge "Extract min/max breakpoint key, and add Breakpoint factory" into main

parents 17c37a98 f9e5eb28
Loading
Loading
Loading
Loading
+30 −2
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@

package com.android.mechanics.spec

import androidx.compose.ui.util.fastIsFinite
import com.android.mechanics.spring.SpringParameters

/**
@@ -42,6 +43,11 @@ class BreakpointKey(val debugLabel: String? = null, val identity: Any = Object()
        return "BreakpointKey(${debugLabel ?: ""}" +
            "@${System.identityHashCode(identity).toString(16).padStart(8,'0')})"
    }

    internal companion object {
        val MinLimit = BreakpointKey("built-in::min")
        val MaxLimit = BreakpointKey("built-in::max")
    }
}

/**
@@ -66,11 +72,20 @@ data class Breakpoint(
    val spring: SpringParameters,
    val guarantee: Guarantee,
) : Comparable<Breakpoint> {

    init {
        when (key) {
            BreakpointKey.MinLimit -> require(position == Float.NEGATIVE_INFINITY)
            BreakpointKey.MaxLimit -> require(position == Float.POSITIVE_INFINITY)
            else -> require(position.fastIsFinite())
        }
    }

    companion object {
        /** First breakpoint of each spec. */
        val minLimit =
            Breakpoint(
                BreakpointKey("built-in::min"),
                BreakpointKey.MinLimit,
                Float.NEGATIVE_INFINITY,
                SpringParameters.Snap,
                Guarantee.None,
@@ -79,11 +94,24 @@ data class Breakpoint(
        /** Last breakpoint of each spec. */
        val maxLimit =
            Breakpoint(
                BreakpointKey("built-in::max"),
                BreakpointKey.MaxLimit,
                Float.POSITIVE_INFINITY,
                SpringParameters.Snap,
                Guarantee.None,
            )

        internal fun create(
            breakpointKey: BreakpointKey,
            breakpointPosition: Float,
            springSpec: SpringParameters,
            guarantee: Guarantee,
        ): Breakpoint {
            return when (breakpointKey) {
                BreakpointKey.MinLimit -> minLimit
                BreakpointKey.MaxLimit -> maxLimit
                else -> Breakpoint(breakpointKey, breakpointPosition, springSpec, guarantee)
            }
        }
    }

    override fun compareTo(other: Breakpoint): Int {
+7 −31
Original line number Diff line number Diff line
@@ -87,7 +87,7 @@ internal class DirectionalBuilderImpl(
            check(mappings.size == breakpoints.size)
        }

        if (key == Breakpoint.maxLimit.key) {
        if (key == BreakpointKey.MaxLimit) {
            check(targetValue.isNaN()) { "cant specify target value for last segment" }
            check(semantics.isEmpty()) { "cant specify semantics for last breakpoint" }
        } else {
@@ -330,13 +330,12 @@ internal class DirectionalBuilderImpl(
        guarantee: Guarantee,
    ): Breakpoint {
        val breakpoint =
            if (breakpointKey == Breakpoint.maxLimit.key) {
                check(breakpointPosition == Float.POSITIVE_INFINITY)
                Breakpoint.maxLimit
            } else {
                check(breakpointPosition.isFinite())
                Breakpoint(checkNotNull(breakpointKey), breakpointPosition, springSpec, guarantee)
            }
            Breakpoint.create(
                checkNotNull(breakpointKey),
                breakpointPosition,
                springSpec,
                guarantee,
            )

        breakpoints.add(breakpoint)
        breakpointPosition = Float.NaN
@@ -344,29 +343,6 @@ internal class DirectionalBuilderImpl(

        return breakpoint
    }

    private fun completeImpl() {
        if (breakpoints.last() == Breakpoint.maxLimit) {
            return
        }

        check(targetValue.isNaN()) { "cant specify target value for last segment" }

        if (!fractionalMapping.isNaN()) {
            check(!sourceValue.isNaN())

            val sourcePosition = breakpoints.last().position

            mappings.add(
                Mapping.Linear(
                    fractionalMapping,
                    sourceValue - (sourcePosition * fractionalMapping),
                )
            )
        }

        breakpoints.add(Breakpoint.maxLimit)
    }
}

internal class SegmentSemanticValuesBuilder<T>(seed: SemanticValue<T>) {