Skip to content

Commit

Permalink
Optimize Modifier.constrainedAspectRatio
Browse files Browse the repository at this point in the history
  • Loading branch information
MattiasBuelens committed Nov 27, 2023
1 parent 4d190e9 commit ef51a33
Showing 1 changed file with 40 additions and 17 deletions.
57 changes: 40 additions & 17 deletions ui/src/main/java/com/theoplayer/android/ui/Modifiers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ import androidx.compose.ui.layout.LayoutModifier
import androidx.compose.ui.layout.Measurable
import androidx.compose.ui.layout.MeasureResult
import androidx.compose.ui.layout.MeasureScope
import androidx.compose.ui.node.ModifierNodeElement
import androidx.compose.ui.platform.InspectorInfo
import androidx.compose.ui.platform.InspectorValueInfo
import androidx.compose.ui.platform.debugInspectorInfo
import androidx.compose.ui.unit.Constraints
import androidx.compose.ui.unit.IntSize
Expand Down Expand Up @@ -183,7 +183,7 @@ internal fun Modifier.constrainedAspectRatio(
ratio: Float,
matchHeightConstraintsFirst: Boolean = false
): Modifier = this.then(
ConstrainedAspectRatioModifier(
ConstrainedAspectRatioElement(
ratio,
matchHeightConstraintsFirst,
debugInspectorInfo {
Expand All @@ -194,11 +194,46 @@ internal fun Modifier.constrainedAspectRatio(
)
)

private class ConstrainedAspectRatioModifier(
private class ConstrainedAspectRatioElement(
val aspectRatio: Float,
val matchHeightConstraintsFirst: Boolean,
inspectorInfo: InspectorInfo.() -> Unit
) : LayoutModifier, InspectorValueInfo(inspectorInfo) {
val inspectorInfo: InspectorInfo.() -> Unit
) : ModifierNodeElement<ConstrainedAspectRatioNode>() {
init {
require(aspectRatio > 0) { "aspectRatio $aspectRatio must be > 0" }
}

override fun create(): ConstrainedAspectRatioNode {
return ConstrainedAspectRatioNode(
aspectRatio,
matchHeightConstraintsFirst
)
}

override fun update(node: ConstrainedAspectRatioNode) {
node.aspectRatio = aspectRatio
node.matchHeightConstraintsFirst = matchHeightConstraintsFirst
}

override fun InspectorInfo.inspectableProperties() {
inspectorInfo()
}

override fun equals(other: Any?): Boolean {
if (this === other) return true
val otherModifier = other as? ConstrainedAspectRatioElement ?: return false
return aspectRatio == otherModifier.aspectRatio &&
matchHeightConstraintsFirst == other.matchHeightConstraintsFirst
}

override fun hashCode(): Int =
aspectRatio.hashCode() * 31 + matchHeightConstraintsFirst.hashCode()
}

private class ConstrainedAspectRatioNode(
var aspectRatio: Float,
var matchHeightConstraintsFirst: Boolean
) : LayoutModifier, Modifier.Node() {
init {
require(aspectRatio > 0) { "aspectRatio $aspectRatio must be > 0" }
}
Expand Down Expand Up @@ -321,16 +356,4 @@ private class ConstrainedAspectRatioModifier(
}
return IntSize.Zero
}

override fun equals(other: Any?): Boolean {
if (this === other) return true
val otherModifier = other as? ConstrainedAspectRatioModifier ?: return false
return aspectRatio == otherModifier.aspectRatio &&
matchHeightConstraintsFirst == other.matchHeightConstraintsFirst
}

override fun hashCode(): Int =
aspectRatio.hashCode() * 31 + matchHeightConstraintsFirst.hashCode()

override fun toString(): String = "ConstrainedAspectRatioModifier(aspectRatio=$aspectRatio)"
}

0 comments on commit ef51a33

Please sign in to comment.