diff --git a/app/src/main/java/org/oxycblt/auxio/playback/ui/button/ScaledPlaybackButton.kt b/app/src/main/java/org/oxycblt/auxio/playback/ui/button/ScaledPlaybackButton.kt new file mode 100644 index 000000000..8a84af177 --- /dev/null +++ b/app/src/main/java/org/oxycblt/auxio/playback/ui/button/ScaledPlaybackButton.kt @@ -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") + } +} diff --git a/app/src/main/java/org/oxycblt/auxio/playback/ui/button/ScaledPlaybackButtonGroup.kt b/app/src/main/java/org/oxycblt/auxio/playback/ui/button/ScaledPlaybackButtonGroup.kt new file mode 100644 index 000000000..2d0364087 --- /dev/null +++ b/app/src/main/java/org/oxycblt/auxio/playback/ui/button/ScaledPlaybackButtonGroup.kt @@ -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().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) + } +} diff --git a/app/src/main/java/org/oxycblt/auxio/ui/WidthFixMaterialButton.kt b/app/src/main/java/org/oxycblt/auxio/playback/ui/button/WidthFixMaterialButton.kt similarity index 63% rename from app/src/main/java/org/oxycblt/auxio/ui/WidthFixMaterialButton.kt rename to app/src/main/java/org/oxycblt/auxio/playback/ui/button/WidthFixMaterialButton.kt index 02ffab135..649442e16 100644 --- a/app/src/main/java/org/oxycblt/auxio/ui/WidthFixMaterialButton.kt +++ b/app/src/main/java/org/oxycblt/auxio/playback/ui/button/WidthFixMaterialButton.kt @@ -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 . - */ - -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 } -} +} \ No newline at end of file diff --git a/app/src/main/res/layout-h360dp/fragment_playback_panel.xml b/app/src/main/res/layout-h360dp/fragment_playback_panel.xml index 0a6e9404a..da51a2b9d 100644 --- a/app/src/main/res/layout-h360dp/fragment_playback_panel.xml +++ b/app/src/main/res/layout-h360dp/fragment_playback_panel.xml @@ -107,85 +107,59 @@ app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent"> - + - + app:materialSizeOverlay="@style/SizeOverlay.Material3Expressive.Button.IconButton.Medium" /> - + app:materialSizeOverlay="@style/SizeOverlay.Material3Expressive.Button.IconButton.Large.Narrow" /> - - + app:materialSizeOverlay="@style/SizeOverlay.Material3Expressive.Button.IconButton.Large.Narrow" /> - - + diff --git a/app/src/main/res/layout-h520dp/fragment_playback_panel.xml b/app/src/main/res/layout-h520dp/fragment_playback_panel.xml index 2fd19cdce..cf0c2fd00 100644 --- a/app/src/main/res/layout-h520dp/fragment_playback_panel.xml +++ b/app/src/main/res/layout-h520dp/fragment_playback_panel.xml @@ -110,79 +110,59 @@ app:layout_constraintStart_toStartOf="parent" app:layout_constraintWidth_max="400dp"> - - + app:materialSizeOverlay="@style/SizeOverlay.Material3Expressive.Button.IconButton.Medium" /> - + app:materialSizeOverlay="@style/SizeOverlay.Material3Expressive.Button.IconButton.Large.Narrow" /> - - + app:materialSizeOverlay="@style/SizeOverlay.Material3Expressive.Button.IconButton.Large.Narrow" /> - - + diff --git a/app/src/main/res/layout-w400dp-h520dp/fragment_playback_panel.xml b/app/src/main/res/layout-w400dp-h520dp/fragment_playback_panel.xml deleted file mode 100644 index 83a74ea3d..000000000 --- a/app/src/main/res/layout-w400dp-h520dp/fragment_playback_panel.xml +++ /dev/null @@ -1,188 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/app/src/main/res/layout/fragment_playback_bar.xml b/app/src/main/res/layout/fragment_playback_bar.xml index 0ccd8b41b..3bc296af5 100644 --- a/app/src/main/res/layout/fragment_playback_bar.xml +++ b/app/src/main/res/layout/fragment_playback_bar.xml @@ -73,7 +73,7 @@ app:materialSizeOverlay="@style/SizeOverlay.Material3Expressive.Button.IconButton.Small" app:icon="@drawable/sel_playing_state_24" /> - - + - - + app:materialSizeOverlay="@style/SizeOverlay.Material3Expressive.Button.IconButton.Medium" /> - + app:materialSizeOverlay="@style/SizeOverlay.Material3Expressive.Button.IconButton.Large.Narrow" /> - - + app:materialSizeOverlay="@style/SizeOverlay.Material3Expressive.Button.IconButton.Large.Narrow" /> - - +