playback: autoscale buttons

This commit is contained in:
Alexander Capehart
2026-07-04 22:16:14 -06:00
parent e52da251a9
commit ab5341f81c
8 changed files with 235 additions and 327 deletions
@@ -0,0 +1,118 @@
package org.oxycblt.auxio.playback.ui.button
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 org.oxycblt.auxio.util.lazyReflectedMethod
import java.lang.reflect.Method
import kotlin.math.max
/**
* Companion scalable button to [ScaledPlaybackButtonGroup], see that.
*
* @author Codex w/cleanup + cognitive ownership by Alexander Capehart (OxygenCobalt)
*/
class ScaledPlaybackButton
@JvmOverloads
constructor(
context: Context,
attrs: AttributeSet? = null,
@AttrRes defStyleAttr: Int = R.attr.materialButtonStyle,
) : WidthFixMaterialButton(context, attrs, defStyleAttr) {
private var baseMetrics: BaseMetrics
private var lastScale: Float? = 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,
)
}
val baseWidth: Int
get() = baseMetrics.width
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)
// MaterialButtonGroup mutates child widths while pressed so neighbors can shrink/grow.
// Only rewrite the baseline when the scale itself changes.
if (lastScale == scale) {
// Nothing to change
return
}
lastScale = scale
METHOD_RECOVER_ORIG_PARAMS.invoke(this)
val recoveredParams = layoutParams
if (recoveredParams != null) {
// Existing LayoutParams, update
recoveredParams.width = scaledWidth
recoveredParams.height = scaledHeight
} else {
// Otherwise make new LayoutParams
layoutParams = ViewGroup.LayoutParams(scaledWidth, scaledHeight)
}
// Scale minimum width/height
minimumWidth = minOf(baseMetrics.minimumWidth.scale(scale), scaledWidth)
minimumHeight = minOf(baseMetrics.minimumHeight.scale(scale), scaledHeight)
// Scale padding
setPaddingRelative(
baseMetrics.paddingStart.scale(scale),
baseMetrics.paddingTop.scale(scale),
baseMetrics.paddingEnd.scale(scale),
baseMetrics.paddingBottom.scale(scale),
)
// Scale icon size
iconSize = baseMetrics.iconSize.scale(scale)
// 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
}
private fun Int.scale(scale: Float) = (this * scale).toInt()
private data class BaseMetrics(
val width: Int,
val height: Int,
val minimumWidth: Int,
val minimumHeight: Int,
val paddingStart: Int,
val paddingTop: Int,
val paddingEnd: Int,
val paddingBottom: Int,
val iconSize: Int,
val strokeWidth: Int,
)
private companion object {
private val METHOD_RECOVER_ORIG_PARAMS: Method by lazyReflectedMethod(MaterialButton::class, "recoverOriginalLayoutParams")
}
}
@@ -0,0 +1,69 @@
package org.oxycblt.auxio.playback.ui.button
import android.content.Context
import android.util.AttributeSet
import androidx.annotation.AttrRes
import androidx.core.view.children
import com.google.android.material.R
import com.google.android.material.button.MaterialButtonGroup
import kotlin.math.min
/**
* A [com.google.android.material.button.MaterialButtonGroup] that scales playback controls down
* to actually fit mid-sized screens.
*
* M3 Expressive for some reason only has "Large" and then massively smaller "Medium"/"Small"
* size classes. So either you clip on smaller phones or jump to an unusable button row sizing.
*
* Fix this by just force-scaling down buttons.
*
* @author Alexander Capehart (OxygenCobalt)
*/
class ScaledPlaybackButtonGroup
@JvmOverloads
constructor(
context: Context,
attrs: AttributeSet? = null,
@AttrRes defStyleAttr: Int = R.attr.materialButtonGroupStyle,
) : MaterialButtonGroup(context, attrs, defStyleAttr) {
private val baseSpacing = spacing
private var lastAppliedSpacing = baseSpacing
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
var scale = 1f
val spbChildren = children.filterIsInstance<ScaledPlaybackButton>().toList()
if (spbChildren.isEmpty()) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec)
return
}
// Must equally space/distribute children across the row equally
val rowWidth = spbChildren.sumOf { it.baseWidth } + baseSpacing * (spbChildren.size - 1)
// Must still fit to the highest child (when scaled)
val rowHeight = spbChildren.maxOf { it.baseHeight }
// Build a scaling coefficient that fits exactly within the bounds but still
// scales down 1:1.
val widthMode = MeasureSpec.getMode(widthMeasureSpec)
val widthSize = MeasureSpec.getSize(widthMeasureSpec) - paddingLeft - paddingRight
if (widthMode != MeasureSpec.UNSPECIFIED && widthSize > 0 && rowWidth > widthSize) {
scale = min(scale, widthSize.toFloat() / rowWidth)
}
val heightMode = MeasureSpec.getMode(heightMeasureSpec)
val heightSize = MeasureSpec.getSize(heightMeasureSpec) - paddingTop - paddingBottom
if (heightMode != MeasureSpec.UNSPECIFIED && heightSize > 0 && rowHeight > heightSize) {
scale = min(scale, heightSize.toFloat() / rowHeight)
}
// Then apply the scaling
val spacing = (baseSpacing * scale).toInt()
if (lastAppliedSpacing != spacing) {
lastAppliedSpacing = spacing
setSpacing(spacing)
}
spbChildren.forEach { it.applyPlaybackScale(scale) }
super.onMeasure(widthMeasureSpec, heightMeasureSpec)
}
}
@@ -1,38 +1,21 @@
/*
* Copyright (c) 2026 Auxio Project
* WidthFixMaterialButton.kt is part of Auxio.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.oxycblt.auxio.ui
package org.oxycblt.auxio.playback.ui.button
import android.content.Context
import android.util.AttributeSet
import androidx.annotation.AttrRes
import androidx.annotation.DrawableRes
import com.google.android.material.R
import org.oxycblt.auxio.ui.RippleFixMaterialButton
/**
* [RippleFixMaterialButton] that works around another bug where switching the icon during a press
* [org.oxycblt.auxio.ui.RippleFixMaterialButton] that works around another bug where switching the icon during a press
* breaks width expansion animations.
*
* @author Alexander Capehart (OxygenCobalt)
*
* TODO: Should animate icon transitions to make this look less bad.
*/
class WidthFixMaterialButton
open class WidthFixMaterialButton
@JvmOverloads
constructor(
context: Context,
@@ -71,4 +54,4 @@ constructor(
pendingIconRes = null
appliedIconRes = null
}
}
}