playback: scale radius on playback buttons too
This commit is contained in:
@@ -1,11 +1,18 @@
|
||||
package org.oxycblt.auxio.playback.ui.button
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.content.Context
|
||||
import android.util.AttributeSet
|
||||
import android.view.ViewGroup
|
||||
import androidx.annotation.AttrRes
|
||||
import com.google.android.material.R
|
||||
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 java.lang.reflect.Method
|
||||
import kotlin.math.max
|
||||
@@ -15,6 +22,7 @@ import kotlin.math.max
|
||||
*
|
||||
* @author Codex w/cleanup + cognitive ownership by Alexander Capehart (OxygenCobalt)
|
||||
*/
|
||||
@SuppressLint("RestrictedApi")
|
||||
class ScaledPlaybackButton
|
||||
@JvmOverloads
|
||||
constructor(
|
||||
@@ -24,24 +32,26 @@ constructor(
|
||||
) : WidthFixMaterialButton(context, attrs, defStyleAttr) {
|
||||
private var baseMetrics: BaseMetrics
|
||||
private var lastScale: Float? = null
|
||||
private var scaledShapeAppearance: ShapeAppearance? = null
|
||||
|
||||
init {
|
||||
// We need to capture the buttons original state w /o any transforms
|
||||
// so that our scaling applies to the correct dimens
|
||||
val contentWidth = paddingStart + paddingEnd + iconSize
|
||||
val contentHeight = paddingTop + paddingBottom + iconSize
|
||||
baseMetrics = BaseMetrics(
|
||||
width = max(minimumWidth, contentWidth),
|
||||
height = max(minimumHeight, contentHeight),
|
||||
minimumWidth = minimumWidth,
|
||||
minimumHeight = minimumHeight,
|
||||
paddingStart = paddingStart,
|
||||
paddingTop = paddingTop,
|
||||
paddingEnd = paddingEnd,
|
||||
paddingBottom = paddingBottom,
|
||||
iconSize = iconSize,
|
||||
strokeWidth = strokeWidth,
|
||||
)
|
||||
baseMetrics = BaseMetrics(
|
||||
width = max(minimumWidth, contentWidth),
|
||||
height = max(minimumHeight, contentHeight),
|
||||
minimumWidth = minimumWidth,
|
||||
minimumHeight = minimumHeight,
|
||||
paddingStart = paddingStart,
|
||||
paddingTop = paddingTop,
|
||||
paddingEnd = paddingEnd,
|
||||
paddingBottom = paddingBottom,
|
||||
iconSize = iconSize,
|
||||
strokeWidth = strokeWidth,
|
||||
shapeAppearance = shapeAppearance
|
||||
)
|
||||
}
|
||||
|
||||
val baseWidth: Int
|
||||
@@ -50,11 +60,6 @@ constructor(
|
||||
val baseHeight: Int
|
||||
get() = baseMetrics.height
|
||||
|
||||
override fun onAttachedToWindow() {
|
||||
super.onAttachedToWindow()
|
||||
|
||||
}
|
||||
|
||||
fun applyPlaybackScale(scale: Float) {
|
||||
val scaledWidth = baseMetrics.width.scale(scale)
|
||||
val scaledHeight = baseMetrics.height.scale(scale)
|
||||
@@ -95,10 +100,30 @@ constructor(
|
||||
// Scale stroke width if necessary
|
||||
// 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
|
||||
|
||||
shapeAppearance = baseMetrics.shapeAppearance.scale(scale)
|
||||
}
|
||||
|
||||
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(
|
||||
val width: Int,
|
||||
val height: Int,
|
||||
@@ -110,9 +135,11 @@ constructor(
|
||||
val paddingBottom: Int,
|
||||
val iconSize: Int,
|
||||
val strokeWidth: Int,
|
||||
@SuppressLint("RestrictedApi") val shapeAppearance: ShapeAppearance,
|
||||
)
|
||||
|
||||
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")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user