playback: scale radius on playback buttons too

This commit is contained in:
Alexander Capehart
2026-07-05 23:16:08 -06:00
parent 0571fe4437
commit 6001ef5b14
@@ -1,11 +1,18 @@
package org.oxycblt.auxio.playback.ui.button package org.oxycblt.auxio.playback.ui.button
import android.annotation.SuppressLint
import android.content.Context import android.content.Context
import android.util.AttributeSet import android.util.AttributeSet
import android.view.ViewGroup import android.view.ViewGroup
import androidx.annotation.AttrRes import androidx.annotation.AttrRes
import com.google.android.material.R import com.google.android.material.R
import com.google.android.material.button.MaterialButton import com.google.android.material.button.MaterialButton
import com.google.android.material.shape.AbsoluteCornerSize
import com.google.android.material.shape.CornerSize
import com.google.android.material.shape.RelativeCornerSize
import com.google.android.material.shape.ShapeAppearance
import com.google.android.material.shape.ShapeAppearanceModel
import com.google.android.material.shape.StateListShapeAppearanceModel
import org.oxycblt.auxio.util.lazyReflectedMethod import org.oxycblt.auxio.util.lazyReflectedMethod
import java.lang.reflect.Method import java.lang.reflect.Method
import kotlin.math.max import kotlin.math.max
@@ -15,6 +22,7 @@ import kotlin.math.max
* *
* @author Codex w/cleanup + cognitive ownership by Alexander Capehart (OxygenCobalt) * @author Codex w/cleanup + cognitive ownership by Alexander Capehart (OxygenCobalt)
*/ */
@SuppressLint("RestrictedApi")
class ScaledPlaybackButton class ScaledPlaybackButton
@JvmOverloads @JvmOverloads
constructor( constructor(
@@ -24,24 +32,26 @@ constructor(
) : WidthFixMaterialButton(context, attrs, defStyleAttr) { ) : WidthFixMaterialButton(context, attrs, defStyleAttr) {
private var baseMetrics: BaseMetrics private var baseMetrics: BaseMetrics
private var lastScale: Float? = null private var lastScale: Float? = null
private var scaledShapeAppearance: ShapeAppearance? = null
init { init {
// We need to capture the buttons original state w /o any transforms // We need to capture the buttons original state w /o any transforms
// so that our scaling applies to the correct dimens // so that our scaling applies to the correct dimens
val contentWidth = paddingStart + paddingEnd + iconSize val contentWidth = paddingStart + paddingEnd + iconSize
val contentHeight = paddingTop + paddingBottom + iconSize val contentHeight = paddingTop + paddingBottom + iconSize
baseMetrics = BaseMetrics( baseMetrics = BaseMetrics(
width = max(minimumWidth, contentWidth), width = max(minimumWidth, contentWidth),
height = max(minimumHeight, contentHeight), height = max(minimumHeight, contentHeight),
minimumWidth = minimumWidth, minimumWidth = minimumWidth,
minimumHeight = minimumHeight, minimumHeight = minimumHeight,
paddingStart = paddingStart, paddingStart = paddingStart,
paddingTop = paddingTop, paddingTop = paddingTop,
paddingEnd = paddingEnd, paddingEnd = paddingEnd,
paddingBottom = paddingBottom, paddingBottom = paddingBottom,
iconSize = iconSize, iconSize = iconSize,
strokeWidth = strokeWidth, strokeWidth = strokeWidth,
) shapeAppearance = shapeAppearance
)
} }
val baseWidth: Int val baseWidth: Int
@@ -50,11 +60,6 @@ constructor(
val baseHeight: Int val baseHeight: Int
get() = baseMetrics.height get() = baseMetrics.height
override fun onAttachedToWindow() {
super.onAttachedToWindow()
}
fun applyPlaybackScale(scale: Float) { fun applyPlaybackScale(scale: Float) {
val scaledWidth = baseMetrics.width.scale(scale) val scaledWidth = baseMetrics.width.scale(scale)
val scaledHeight = baseMetrics.height.scale(scale) val scaledHeight = baseMetrics.height.scale(scale)
@@ -95,10 +100,30 @@ constructor(
// Scale stroke width if necessary // Scale stroke width if necessary
// A bit inaccurate (they actually step from 3, 2, 1) but better to interpolate honestly // A bit inaccurate (they actually step from 3, 2, 1) but better to interpolate honestly
strokeWidth = if (baseMetrics.strokeWidth > 0) max(1, baseMetrics.strokeWidth.scale(scale)) else 0 strokeWidth = if (baseMetrics.strokeWidth > 0) max(1, baseMetrics.strokeWidth.scale(scale)) else 0
shapeAppearance = baseMetrics.shapeAppearance.scale(scale)
} }
private fun Int.scale(scale: Float) = (this * scale).toInt() private fun Int.scale(scale: Float) = (this * scale).toInt()
private fun ShapeAppearance.scale(scale: Float): ShapeAppearance =
when (this) {
is StateListShapeAppearanceModel ->
withTransformedCornerSizes { it.scale(scale) }
is ShapeAppearanceModel ->
withTransformedCornerSizes { it.scale(scale) }
else -> this
}
private fun CornerSize.scale(scale: Float): CornerSize =
when (this) {
is RelativeCornerSize -> RelativeCornerSize(relativePercent * scale)
is AbsoluteCornerSize -> AbsoluteCornerSize(cornerSize * scale)
else -> CornerSize { bounds -> getCornerSize(bounds) * scale }
}
private data class BaseMetrics( private data class BaseMetrics(
val width: Int, val width: Int,
val height: Int, val height: Int,
@@ -110,9 +135,11 @@ constructor(
val paddingBottom: Int, val paddingBottom: Int,
val iconSize: Int, val iconSize: Int,
val strokeWidth: Int, val strokeWidth: Int,
@SuppressLint("RestrictedApi") val shapeAppearance: ShapeAppearance,
) )
private companion object { private companion object {
private val METHOD_RECOVER_ORIG_PARAMS: Method by lazyReflectedMethod(MaterialButton::class, "recoverOriginalLayoutParams") private val METHOD_RECOVER_ORIG_PARAMS: Method by
lazyReflectedMethod(MaterialButton::class, "recoverOriginalLayoutParams")
} }
} }