From 613fd7e6b4590d46a2c5f670bab32de6c3fe25e4 Mon Sep 17 00:00:00 2001 From: Alexander Capehart Date: Mon, 2 Mar 2026 08:48:31 -0700 Subject: [PATCH] ui: new group toolbar + tweaked expressive shapes --- .../auxio/home/list/CookieImageView.kt | 26 ++- .../org/oxycblt/auxio/home/list/ListUtil.kt | 8 +- .../auxio/list/recycler/SoftBurstImageView.kt | 2 +- .../list/recycler/SoftBurstShapeDrawable.kt | 4 +- .../oxycblt/auxio/ui/ButtonGroupToolbar.kt | 204 ++++++++++++++++++ .../org/oxycblt/auxio/util/FrameworkUtil.kt | 6 + app/src/main/res/layout/fragment_home.xml | 4 +- app/src/main/res/layout/fragment_search.xml | 2 +- 8 files changed, 243 insertions(+), 13 deletions(-) create mode 100644 app/src/main/java/org/oxycblt/auxio/ui/ButtonGroupToolbar.kt diff --git a/app/src/main/java/org/oxycblt/auxio/home/list/CookieImageView.kt b/app/src/main/java/org/oxycblt/auxio/home/list/CookieImageView.kt index de2ea29aa..154c633c7 100644 --- a/app/src/main/java/org/oxycblt/auxio/home/list/CookieImageView.kt +++ b/app/src/main/java/org/oxycblt/auxio/home/list/CookieImageView.kt @@ -1,11 +1,31 @@ +/* + * Copyright (c) 2026 Auxio Project + * CookieImageView.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.home.list import android.content.Context import android.util.AttributeSet import androidx.appcompat.widget.AppCompatImageView -import org.oxycblt.auxio.home.list.CookieShapeDrawable -/** A placeholder [androidx.appcompat.widget.AppCompatImageView] with a 6-sided cookie expressive background shape. */ +/** + * A placeholder [androidx.appcompat.widget.AppCompatImageView] with a 6-sided cookie expressive + * background shape. + */ class CookieImageView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) : @@ -14,4 +34,4 @@ constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0 background = CookieShapeDrawable(context) scaleType = ScaleType.CENTER_INSIDE } -} \ No newline at end of file +} diff --git a/app/src/main/java/org/oxycblt/auxio/home/list/ListUtil.kt b/app/src/main/java/org/oxycblt/auxio/home/list/ListUtil.kt index 02220cc54..b765871a1 100644 --- a/app/src/main/java/org/oxycblt/auxio/home/list/ListUtil.kt +++ b/app/src/main/java/org/oxycblt/auxio/home/list/ListUtil.kt @@ -31,13 +31,13 @@ import android.graphics.RectF import android.graphics.drawable.Drawable import androidx.core.text.isDigitsOnly import com.google.android.material.R -import org.oxycblt.auxio.ui.ExpressiveShapes -import org.oxycblt.auxio.util.getAttrColorCompat -import org.oxycblt.musikr.tag.Name import kotlin.math.hypot import kotlin.math.max import kotlin.math.min import kotlin.math.roundToInt +import org.oxycblt.auxio.ui.ExpressiveShapes +import org.oxycblt.auxio.util.getAttrColorCompat +import org.oxycblt.musikr.tag.Name fun Name.thumb() = when (this) { @@ -143,4 +143,4 @@ class CookieShapeDrawable(context: Context) : Drawable() { return maxRadius } -} \ No newline at end of file +} diff --git a/app/src/main/java/org/oxycblt/auxio/list/recycler/SoftBurstImageView.kt b/app/src/main/java/org/oxycblt/auxio/list/recycler/SoftBurstImageView.kt index ea1424ce6..d0f132a40 100644 --- a/app/src/main/java/org/oxycblt/auxio/list/recycler/SoftBurstImageView.kt +++ b/app/src/main/java/org/oxycblt/auxio/list/recycler/SoftBurstImageView.kt @@ -15,7 +15,7 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - + package org.oxycblt.auxio.list.recycler import android.content.Context diff --git a/app/src/main/java/org/oxycblt/auxio/list/recycler/SoftBurstShapeDrawable.kt b/app/src/main/java/org/oxycblt/auxio/list/recycler/SoftBurstShapeDrawable.kt index 3fedd17db..483145c39 100644 --- a/app/src/main/java/org/oxycblt/auxio/list/recycler/SoftBurstShapeDrawable.kt +++ b/app/src/main/java/org/oxycblt/auxio/list/recycler/SoftBurstShapeDrawable.kt @@ -15,7 +15,7 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - + package org.oxycblt.auxio.list.recycler import android.content.Context @@ -29,12 +29,12 @@ import android.graphics.PixelFormat import android.graphics.Rect import android.graphics.RectF import android.graphics.drawable.Drawable -import org.oxycblt.auxio.ui.ExpressiveShapes import com.google.android.material.R as MR import kotlin.math.hypot import kotlin.math.max import kotlin.math.min import kotlin.math.roundToInt +import org.oxycblt.auxio.ui.ExpressiveShapes import org.oxycblt.auxio.util.getAttrColorCompat /** diff --git a/app/src/main/java/org/oxycblt/auxio/ui/ButtonGroupToolbar.kt b/app/src/main/java/org/oxycblt/auxio/ui/ButtonGroupToolbar.kt new file mode 100644 index 000000000..2e70b9be7 --- /dev/null +++ b/app/src/main/java/org/oxycblt/auxio/ui/ButtonGroupToolbar.kt @@ -0,0 +1,204 @@ +/* + * Copyright (c) 2026 Auxio Project + * ButtonGroupToolbar.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 + +import android.annotation.SuppressLint +import android.content.Context +import android.util.AttributeSet +import android.view.Gravity +import android.view.Menu +import android.view.MenuInflater +import android.view.MenuItem +import android.view.View +import android.widget.LinearLayout +import androidx.annotation.AttrRes +import androidx.annotation.MenuRes +import androidx.appcompat.view.menu.MenuBuilder +import androidx.appcompat.view.menu.MenuItemImpl +import androidx.appcompat.widget.PopupMenu +import com.google.android.material.R as MR +import com.google.android.material.appbar.MaterialToolbar +import com.google.android.material.button.MaterialButtonGroup +import org.oxycblt.auxio.R + +/** + * A [MaterialToolbar] that replaces the standard + * [ActionMenuView][androidx.appcompat.widget.ActionMenuView] action items with a + * [MaterialButtonGroup] containing [RippleFixMaterialButton] instances. This enables M3 Expressive + * shape morphing and width animation behavior for toolbar action buttons. + * + * Since [MaterialToolbar] has no integration with [MaterialButtonGroup], this class intercepts menu + * inflation and constructs its own button group from the menu resource, hiding the internal + * [ActionMenuView][androidx.appcompat.widget.ActionMenuView] entirely. This is fragile, but the + * relevant internal APIs haven't changed in years. + * + * @author Alexander Capehart (OxygenCobalt) + */ +class ButtonGroupToolbar +@JvmOverloads +constructor( + context: Context, + attrs: AttributeSet? = null, + @AttrRes defStyleAttr: Int = androidx.appcompat.R.attr.toolbarStyle, +) : MaterialToolbar(context, attrs, defStyleAttr) { + private var menuItemClickListener: OnMenuItemClickListener? = null + private var overflowClickListener: ((View) -> Unit)? = null + private var buttonGroup: MaterialButtonGroup? = null + @SuppressLint("RestrictedApi") private var menuBuilder: MenuBuilder? = null + private var isInitialized = false + + // Intercept inflateMenu calls from the super constructor to prevent ActionMenuView creation. + // During the super constructor, isInitialized is false so this is a no-op. After + // construction, this delegates to buildButtonGroup. + override fun inflateMenu(@MenuRes resId: Int) { + if (isInitialized) { + buildButtonGroup(resId) + } + } + + init { + // Read the menu attribute ourselves since we suppressed inflateMenu during the super + // constructor. This uses the same Toolbar_menu styleable that Toolbar reads internally. + val a = + context.obtainStyledAttributes( + attrs, + androidx.appcompat.R.styleable.Toolbar, + defStyleAttr, + 0, + ) + val menuResId = a.getResourceId(androidx.appcompat.R.styleable.Toolbar_menu, 0) + a.recycle() + + isInitialized = true + if (menuResId != 0) { + buildButtonGroup(menuResId) + } + } + + @SuppressLint("RestrictedApi") + override fun getMenu(): Menu { + return menuBuilder ?: super.getMenu() + } + + override fun setOnMenuItemClickListener(listener: OnMenuItemClickListener?) { + menuItemClickListener = listener + } + + /** + * Override the overflow button's click behavior. When set, the overflow button will call + * [block] instead of showing the default popup menu. + */ + fun overrideOnOverflowMenuClick(block: (View) -> Unit) { + overflowClickListener = block + } + + @SuppressLint("RestrictedApi") + private fun buildButtonGroup(@MenuRes resId: Int) { + // Inflate the menu into a MenuBuilder, re-using the same pattern as MenuDialogFragment. + // Since we don't have (and don't want) a dummy view to inflate this menu, just + // depend on the AndroidX Toolbar internal API and hope for the best. + val builder = MenuBuilder(context) + MenuInflater(context).inflate(resId, builder) + menuBuilder = builder + + // Separate action items from overflow items based on showAsAction flags. + val actionItems = mutableListOf() + val overflowItems = mutableListOf() + for (i in 0 until builder.size()) { + val item = builder.getItem(i) as MenuItemImpl + if (!item.isVisible) continue + if (item.requiresActionButton() || item.requestsActionButton()) { + actionItems.add(item) + } else { + overflowItems.add(item) + } + } + + // Remove old button group if one exists. + buttonGroup?.let { removeView(it) } + + // Create new button group using the theme-configured style. + val group = + MaterialButtonGroup(context, null, MR.attr.materialButtonGroupStyle).apply { + layoutParams = + LayoutParams( + LayoutParams.WRAP_CONTENT, + LayoutParams.WRAP_CONTENT, + Gravity.END or Gravity.CENTER_VERTICAL, + ) + } + + // Create a MaterialButton for each action item. + for (item in actionItems) { + val button = + RippleFixMaterialButton(context, null, MR.attr.materialIconButtonStyle).apply { + layoutParams = + LinearLayout.LayoutParams( + LinearLayout.LayoutParams.WRAP_CONTENT, + LinearLayout.LayoutParams.WRAP_CONTENT, + ) + id = item.itemId + icon = item.icon + contentDescription = item.title + setOnClickListener { menuItemClickListener?.onMenuItemClick(item) } + } + group.addView(button) + } + + // Add an overflow button if there are overflow items. + if (overflowItems.isNotEmpty()) { + val overflowButton = + RippleFixMaterialButton(context, null, MR.attr.materialIconButtonStyle).apply { + layoutParams = + LinearLayout.LayoutParams( + LinearLayout.LayoutParams.WRAP_CONTENT, + LinearLayout.LayoutParams.WRAP_CONTENT, + ) + setIconResource(R.drawable.ic_more_24) + contentDescription = context.getString(R.string.lbl_more) + setOnClickListener { view -> + val customListener = overflowClickListener + if (customListener != null) { + customListener(view) + } else { + showOverflowPopup(view, overflowItems) + } + } + } + group.addView(overflowButton) + } + + addView(group) + buttonGroup = group + } + + private fun showOverflowPopup(anchor: View, items: List) { + val popup = PopupMenu(context, anchor) + for (item in items) { + popup.menu.add(item.groupId, item.itemId, item.order, item.title).apply { + icon = item.icon + isEnabled = item.isEnabled + } + } + popup.setOnMenuItemClickListener { clickedItem -> + menuItemClickListener?.onMenuItemClick(clickedItem) ?: false + } + popup.show() + } +} diff --git a/app/src/main/java/org/oxycblt/auxio/util/FrameworkUtil.kt b/app/src/main/java/org/oxycblt/auxio/util/FrameworkUtil.kt index d3bae0826..50a594b62 100644 --- a/app/src/main/java/org/oxycblt/auxio/util/FrameworkUtil.kt +++ b/app/src/main/java/org/oxycblt/auxio/util/FrameworkUtil.kt @@ -44,6 +44,7 @@ import androidx.viewbinding.ViewBinding import com.google.android.material.appbar.MaterialToolbar import java.lang.IllegalArgumentException import org.oxycblt.auxio.R +import org.oxycblt.auxio.ui.ButtonGroupToolbar import org.oxycblt.musikr.MusicParent import org.oxycblt.musikr.Song import timber.log.Timber as L @@ -114,6 +115,11 @@ val ViewBinding.context: Context */ @SuppressLint("RestrictedApi") fun Toolbar.overrideOnOverflowMenuClick(block: (View) -> Unit) { + if (this is ButtonGroupToolbar) { + // ButtonGroupToolbar manages its own overflow button, delegate directly. + overrideOnOverflowMenuClick(block) + return + } for (toolbarChild in children) { if (toolbarChild is ActionMenuView) { for (menuChild in toolbarChild.children) { diff --git a/app/src/main/res/layout/fragment_home.xml b/app/src/main/res/layout/fragment_home.xml index 314aaae15..26fe58731 100644 --- a/app/src/main/res/layout/fragment_home.xml +++ b/app/src/main/res/layout/fragment_home.xml @@ -18,7 +18,7 @@ android:layout_width="match_parent" android:layout_height="wrap_content"> - - -