ui: introduce self-rolled toolbar

This commit is contained in:
Alexander Capehart
2026-03-07 16:46:36 -07:00
parent 0e5a6bb15f
commit 1c672e40f5
26 changed files with 711 additions and 571 deletions
@@ -43,6 +43,7 @@ import org.oxycblt.auxio.util.navigateSafe
import org.oxycblt.auxio.util.showToast
import org.oxycblt.auxio.util.unlikelyToBeNull
import org.oxycblt.musikr.Album
import org.oxycblt.musikr.Artist
import org.oxycblt.musikr.Music
import org.oxycblt.musikr.MusicParent
import org.oxycblt.musikr.Song
@@ -62,6 +63,8 @@ class AlbumDetailFragment : DetailFragment<Album, Song>() {
override fun getDetailListAdapter() = albumListAdapter
override fun getToolbarParent() = detailModel.currentAlbum.value
override fun onBindingCreated(binding: FragmentDetailBinding, savedInstanceState: Bundle?) {
super.onBindingCreated(binding, savedInstanceState)
@@ -91,6 +94,14 @@ class AlbumDetailFragment : DetailFragment<Album, Song>() {
detailModel.albumSongInstructions.consume()
}
override fun onPlayParent(parent: Album) {
playbackModel.play(parent)
}
override fun onShuffleParent(parent: Album) {
playbackModel.shuffle(parent)
}
override fun onRealClick(item: Song) {
playbackModel.play(item, detailModel.playInAlbumWith)
}
@@ -118,7 +129,7 @@ class AlbumDetailFragment : DetailFragment<Album, Song>() {
val context = requireContext()
val name = album.name.resolve(context)
binding.detailToolbarTitle.text = name
binding.detailNormalToolbar.title = name
binding.detailCover.bind(album)
// The type text depends on the release type (Album, EP, Single, etc.)
binding.detailType.text = album.releaseType.resolve(context)
@@ -146,15 +157,10 @@ class AlbumDetailFragment : DetailFragment<Album, Song>() {
binding.detailPlayButton?.setOnClickListener {
playbackModel.play(unlikelyToBeNull(detailModel.currentAlbum.value))
}
binding.detailToolbarPlay.setOnClickListener {
playbackModel.play(unlikelyToBeNull(detailModel.currentAlbum.value))
}
binding.detailShuffleButton?.setOnClickListener {
playbackModel.shuffle(unlikelyToBeNull(detailModel.currentAlbum.value))
}
binding.detailToolbarShuffle.setOnClickListener {
playbackModel.shuffle(unlikelyToBeNull(detailModel.currentAlbum.value))
}
setToolbarPlaybackButtonsEnabled(true)
updatePlayback(
playbackModel.song.value,
playbackModel.parent.value,
@@ -62,6 +62,8 @@ class ArtistDetailFragment : DetailFragment<Artist, Music>() {
override fun getDetailListAdapter() = artistListAdapter
override fun getToolbarParent() = detailModel.currentArtist.value
override fun onBindingCreated(binding: FragmentDetailBinding, savedInstanceState: Bundle?) {
super.onBindingCreated(binding, savedInstanceState)
@@ -91,6 +93,14 @@ class ArtistDetailFragment : DetailFragment<Artist, Music>() {
detailModel.artistSongInstructions.consume()
}
override fun onPlayParent(parent: Artist) {
playbackModel.play(parent)
}
override fun onShuffleParent(parent: Artist) {
playbackModel.shuffle(parent)
}
override fun onRealClick(item: Music) {
when (item) {
is Album -> detailModel.showAlbum(item)
@@ -124,7 +134,7 @@ class ArtistDetailFragment : DetailFragment<Artist, Music>() {
val binding = requireBinding()
val context = requireContext()
val name = artist.name.resolve(context)
binding.detailToolbarTitle.text = name
binding.detailNormalToolbar.title = name
binding.detailCover.bind(artist)
binding.detailType.text = context.getString(R.string.lbl_artist)
@@ -159,8 +169,7 @@ class ArtistDetailFragment : DetailFragment<Artist, Music>() {
// we want to reset the visibility of all information that was hidden.
binding.detailPlayButton?.isEnabled = true
binding.detailShuffleButton?.isEnabled = true
binding.detailToolbarPlay.isEnabled = true
binding.detailToolbarShuffle.isEnabled = true
setToolbarPlaybackButtonsEnabled(true)
} else {
// The artist does not have any songs, so hide functionality that makes no sense.
// ex. Play and Shuffle, Song Counts, and Genre Information.
@@ -169,22 +178,15 @@ class ArtistDetailFragment : DetailFragment<Artist, Music>() {
binding.detailSubhead.isVisible = false
binding.detailPlayButton?.isEnabled = false
binding.detailShuffleButton?.isEnabled = false
binding.detailToolbarPlay.isEnabled = false
binding.detailToolbarShuffle.isEnabled = false
setToolbarPlaybackButtonsEnabled(false)
}
binding.detailPlayButton?.setOnClickListener {
playbackModel.play(unlikelyToBeNull(detailModel.currentArtist.value))
}
binding.detailToolbarPlay.setOnClickListener {
playbackModel.play(unlikelyToBeNull(detailModel.currentArtist.value))
}
binding.detailShuffleButton?.setOnClickListener {
playbackModel.shuffle(unlikelyToBeNull(detailModel.currentArtist.value))
}
binding.detailToolbarShuffle.setOnClickListener {
playbackModel.shuffle(unlikelyToBeNull(detailModel.currentArtist.value))
}
updatePlayback(
playbackModel.song.value,
playbackModel.parent.value,
@@ -20,6 +20,8 @@ package org.oxycblt.auxio.detail
import android.os.Bundle
import android.view.LayoutInflater
import android.view.MenuItem
import android.view.View
import androidx.fragment.app.activityViewModels
import androidx.navigation.findNavController
import androidx.recyclerview.widget.GridLayoutManager
@@ -38,10 +40,13 @@ import org.oxycblt.auxio.list.PlainHeader
import org.oxycblt.auxio.music.MusicViewModel
import org.oxycblt.auxio.playback.PlaybackViewModel
import org.oxycblt.auxio.util.getDimenPixels
import org.oxycblt.auxio.util.overrideOnOverflowMenuClick
import org.oxycblt.auxio.util.setFullWidthLookup
import org.oxycblt.musikr.Album
import org.oxycblt.musikr.Artist
import org.oxycblt.musikr.Genre
import org.oxycblt.musikr.Music
import org.oxycblt.musikr.MusicParent
import org.oxycblt.musikr.Playlist
abstract class DetailFragment<P : MusicParent, C : Music> :
ListFragment<C, FragmentDetailBinding>(),
@@ -68,6 +73,8 @@ abstract class DetailFragment<P : MusicParent, C : Music> :
abstract fun getDetailListAdapter(): DetailListAdapter
abstract fun getToolbarParent(): P?
override fun getSelectionToolbar(binding: FragmentDetailBinding) =
binding.detailSelectionToolbar
@@ -120,13 +127,73 @@ abstract class DetailFragment<P : MusicParent, C : Music> :
detailHeader.alpha = 1 - outRatio
val inRatio = max(ratio - 0.5f, 0f) * 2
val detailContent = binding.detailToolbarContent
detailContent.alpha = inRatio
detailContent.translationY = spacingSmall * (1 - inRatio)
animateToolbarView(binding.detailNormalToolbar.getTitleContainer(), inRatio)
animateToolbarActionButton(binding.detailNormalToolbar.getMenuButton(R.id.action_play), inRatio)
animateToolbarActionButton(
binding.detailNormalToolbar.getMenuButton(R.id.action_shuffle),
inRatio,
)
// Enable fast scrolling once fully collapsed
binding.detailRecycler.fastScrollingEnabled = ratio == 1f
}
override fun onMenuItemClick(item: MenuItem): Boolean {
if (super.onMenuItemClick(item)) {
return true
}
val parent = getToolbarParent() ?: return false
return when (item.itemId) {
R.id.action_play -> {
onPlayParent(parent)
true
}
R.id.action_shuffle -> {
onShuffleParent(parent)
true
}
else -> false
}
}
protected fun setToolbarPlaybackButtonsEnabled(
playEnabled: Boolean,
shuffleEnabled: Boolean = playEnabled,
) {
binding?.detailNormalToolbar?.apply {
setMenuItemEnabled(R.id.action_play, playEnabled)
setMenuItemEnabled(R.id.action_shuffle, shuffleEnabled)
}
}
private fun animateToolbarView(view: View?, ratio: Float) {
view?.alpha = ratio
view?.translationY = spacingSmall * (1 - ratio)
}
private fun animateToolbarActionButton(view: View?, ratio: Float) {
if (view == null) {
return
}
if (ratio <= 0f) {
view.alpha = 0f
view.translationY = spacingSmall.toFloat()
view.visibility = View.GONE
return
}
if (view.visibility != View.VISIBLE) {
view.visibility = View.VISIBLE
}
view.alpha = ratio
view.translationY = spacingSmall * (1 - ratio)
}
protected abstract fun onPlayParent(parent: P)
protected abstract fun onShuffleParent(parent: P)
abstract fun onOpenParentMenu()
}
@@ -39,6 +39,7 @@ import org.oxycblt.auxio.util.getPlural
import org.oxycblt.auxio.util.navigateSafe
import org.oxycblt.auxio.util.showToast
import org.oxycblt.auxio.util.unlikelyToBeNull
import org.oxycblt.musikr.Album
import org.oxycblt.musikr.Artist
import org.oxycblt.musikr.Genre
import org.oxycblt.musikr.Music
@@ -60,6 +61,8 @@ class GenreDetailFragment : DetailFragment<Genre, Music>() {
override fun getDetailListAdapter() = genreListAdapter
override fun getToolbarParent() = detailModel.currentGenre.value
override fun onBindingCreated(binding: FragmentDetailBinding, savedInstanceState: Bundle?) {
super.onBindingCreated(binding, savedInstanceState)
@@ -89,6 +92,14 @@ class GenreDetailFragment : DetailFragment<Genre, Music>() {
detailModel.genreSongInstructions.consume()
}
override fun onPlayParent(parent: Genre) {
playbackModel.play(parent)
}
override fun onShuffleParent(parent: Genre) {
playbackModel.shuffle(parent)
}
override fun onRealClick(item: Music) {
when (item) {
is Artist -> detailModel.showArtist(item)
@@ -122,7 +133,7 @@ class GenreDetailFragment : DetailFragment<Genre, Music>() {
val binding = requireBinding()
val context = requireContext()
val name = genre.name.resolve(context)
binding.detailToolbarTitle.text = name
binding.detailNormalToolbar.title = name
binding.detailCover.bind(genre)
binding.detailType.text = context.getString(R.string.lbl_genre)
binding.detailName.text = genre.name.resolve(context)
@@ -138,15 +149,10 @@ class GenreDetailFragment : DetailFragment<Genre, Music>() {
binding.detailPlayButton?.setOnClickListener {
playbackModel.play(unlikelyToBeNull(detailModel.currentGenre.value))
}
binding.detailToolbarPlay.setOnClickListener {
playbackModel.play(unlikelyToBeNull(detailModel.currentGenre.value))
}
binding.detailShuffleButton?.setOnClickListener {
playbackModel.shuffle(unlikelyToBeNull(detailModel.currentGenre.value))
}
binding.detailToolbarShuffle.setOnClickListener {
playbackModel.shuffle(unlikelyToBeNull(detailModel.currentGenre.value))
}
setToolbarPlaybackButtonsEnabled(true)
updatePlayback(
playbackModel.song.value,
playbackModel.parent.value,
@@ -48,6 +48,7 @@ import org.oxycblt.auxio.util.getPlural
import org.oxycblt.auxio.util.navigateSafe
import org.oxycblt.auxio.util.showToast
import org.oxycblt.auxio.util.unlikelyToBeNull
import org.oxycblt.musikr.Genre
import org.oxycblt.musikr.Music
import org.oxycblt.musikr.MusicParent
import org.oxycblt.musikr.Playlist
@@ -74,6 +75,8 @@ class PlaylistDetailFragment :
override fun getDetailListAdapter() = playlistListAdapter
override fun getToolbarParent() = detailModel.currentPlaylist.value
override fun onBindingCreated(binding: FragmentDetailBinding, savedInstanceState: Bundle?) {
super.onBindingCreated(binding, savedInstanceState)
@@ -164,6 +167,14 @@ class PlaylistDetailFragment :
editNavigationListener = null
}
override fun onPlayParent(parent: Playlist) {
playbackModel.play(parent)
}
override fun onShuffleParent(parent: Playlist) {
playbackModel.shuffle(parent)
}
override fun onRealClick(item: Song) {
playbackModel.play(item, detailModel.playInPlaylistWith)
}
@@ -198,7 +209,7 @@ class PlaylistDetailFragment :
return
}
val binding = requireBinding()
binding.detailToolbarTitle.text = playlist.name.resolve(requireContext())
binding.detailNormalToolbar.title = playlist.name.resolve(requireContext())
binding.detailEditToolbar.title =
getString(R.string.fmt_editing, playlist.name.resolve(requireContext()))
@@ -244,24 +255,13 @@ class PlaylistDetailFragment :
playbackModel.play(unlikelyToBeNull(detailModel.currentPlaylist.value))
}
}
binding.detailToolbarPlay.apply {
isEnabled = playable
setOnClickListener {
playbackModel.play(unlikelyToBeNull(detailModel.currentPlaylist.value))
}
}
binding.detailShuffleButton?.apply {
isEnabled = playable
setOnClickListener {
playbackModel.shuffle(unlikelyToBeNull(detailModel.currentPlaylist.value))
}
}
binding.detailToolbarShuffle.apply {
isEnabled = playable
setOnClickListener {
playbackModel.shuffle(unlikelyToBeNull(detailModel.currentPlaylist.value))
}
}
setToolbarPlaybackButtonsEnabled(playable)
updatePlayback(
playbackModel.song.value,
playbackModel.parent.value,
@@ -279,9 +279,10 @@ class PlaylistDetailFragment :
if (editedPlaylist != null) {
L.d("Updating save button state")
requireBinding().detailEditToolbar.menu.findItem(R.id.action_save).apply {
isEnabled = editedPlaylist != detailModel.currentPlaylist.value?.songs
}
requireBinding().detailEditToolbar.setMenuItemEnabled(
R.id.action_save,
editedPlaylist != detailModel.currentPlaylist.value?.songs,
)
}
updateMultiToolbar()
@@ -25,8 +25,8 @@ import androidx.viewbinding.ViewBinding
import org.oxycblt.auxio.R
import org.oxycblt.auxio.music.MusicViewModel
import org.oxycblt.auxio.playback.PlaybackViewModel
import org.oxycblt.auxio.ui.AuxioToolbar
import org.oxycblt.auxio.ui.ViewBindingFragment
import org.oxycblt.auxio.util.overrideOnOverflowMenuClick
import org.oxycblt.auxio.util.showToast
/**
@@ -40,7 +40,7 @@ abstract class SelectionFragment<VB : ViewBinding> :
protected abstract val musicModel: MusicViewModel
protected abstract val playbackModel: PlaybackViewModel
open fun getSelectionToolbar(binding: VB): Toolbar? = null
open fun getSelectionToolbar(binding: VB): AuxioToolbar? = null
override fun onBindingCreated(binding: VB, savedInstanceState: Bundle?) {
super.onBindingCreated(binding, savedInstanceState)
@@ -0,0 +1,462 @@
/*
* 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 <https://www.gnu.org/licenses/>.
*/
package org.oxycblt.auxio.ui
import android.annotation.SuppressLint
import android.content.Context
import android.content.res.ColorStateList
import android.graphics.drawable.Drawable
import android.util.AttributeSet
import android.view.ContextThemeWrapper
import android.view.LayoutInflater
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import androidx.annotation.AttrRes
import androidx.annotation.DrawableRes
import androidx.annotation.MenuRes
import androidx.annotation.StringRes
import androidx.annotation.StyleRes
import androidx.appcompat.content.res.AppCompatResources
import androidx.appcompat.view.SupportMenuInflater
import androidx.appcompat.view.menu.MenuBuilder
import androidx.appcompat.view.menu.MenuItemImpl
import androidx.appcompat.widget.PopupMenu
import androidx.appcompat.widget.Toolbar
import androidx.appcompat.widget.TooltipCompat
import androidx.core.view.MenuCompat
import androidx.core.view.children
import androidx.core.view.isVisible
import androidx.core.view.updateLayoutParams
import androidx.core.widget.TextViewCompat
import com.google.android.material.R as MR
import org.oxycblt.auxio.R
import org.oxycblt.auxio.databinding.ViewToolbarBinding
/**
* A compound toolbar view that owns its entire layout instead of piggybacking on AppCompat's
* internal Toolbar structure. It preserves the small API surface Auxio actually uses while
* rendering action items as an M3 Expressive [com.google.android.material.button.MaterialButtonGroup].
*/
class AuxioToolbar
@JvmOverloads
constructor(
context: Context,
attrs: AttributeSet? = null,
@AttrRes defStyleAttr: Int = androidx.appcompat.R.attr.toolbarStyle,
) : FrameLayout(context, attrs, defStyleAttr) {
private var menuItemClickListener: Toolbar.OnMenuItemClickListener? = null
private var navigationClickListener: OnClickListener? = null
private var navigationIconDrawable: Drawable? = null
private var navigationContentDescriptionText: CharSequence? = null
private var titleText: CharSequence? = null
private var subtitleText: CharSequence? = null
private var titleCentered = false
private var subtitleCentered = false
private var overflowClickListener: ((View) -> Unit)? = null
private var isInflatingInternalLayout = true
private var _binding: ViewToolbarBinding? = null
private val binding: ViewToolbarBinding
get() = checkNotNull(_binding) { "Toolbar binding was not initialized" }
private val actionButtons = mutableMapOf<Int, RippleFixMaterialButton>()
@SuppressLint("RestrictedApi") private var menuBuilder = MenuBuilder(context)
init {
_binding = ViewToolbarBinding.inflate(LayoutInflater.from(context), this, true)
isInflatingInternalLayout = false
clipChildren = false
clipToPadding = false
binding.toolbarRoot.clipChildren = false
binding.toolbarRoot.clipToPadding = false
binding.toolbarContentFrame.clipChildren = false
binding.toolbarContentFrame.clipToPadding = false
binding.toolbarActionGroup.clipChildren = false
binding.toolbarActionGroup.clipToPadding = false
binding.toolbarActionGroup.spacing = 0
configureIconButton(binding.toolbarNavigationButton)
val toolbarAttrs =
context.obtainStyledAttributes(
attrs,
androidx.appcompat.R.styleable.Toolbar,
defStyleAttr,
0,
)
val materialToolbarAttrs =
context.obtainStyledAttributes(attrs, MR.styleable.MaterialToolbar, defStyleAttr, 0)
titleText = toolbarAttrs.getText(androidx.appcompat.R.styleable.Toolbar_title)
subtitleText = toolbarAttrs.getText(androidx.appcompat.R.styleable.Toolbar_subtitle)
binding.toolbarTitle.text = titleText
binding.toolbarSubtitle.text = subtitleText
binding.toolbarSubtitle.isVisible = !subtitleText.isNullOrEmpty()
val titleTextAppearance =
toolbarAttrs.getResourceId(androidx.appcompat.R.styleable.Toolbar_titleTextAppearance, 0)
if (titleTextAppearance != 0) {
setTitleTextAppearance(titleTextAppearance)
}
val subtitleTextAppearance =
toolbarAttrs.getResourceId(
androidx.appcompat.R.styleable.Toolbar_subtitleTextAppearance,
0,
)
if (subtitleTextAppearance != 0) {
setSubtitleTextAppearance(subtitleTextAppearance)
}
val titleTextColor =
toolbarAttrs.getColorStateList(androidx.appcompat.R.styleable.Toolbar_titleTextColor)
if (titleTextColor != null) {
setTitleTextColor(titleTextColor)
}
val subtitleTextColor =
toolbarAttrs.getColorStateList(
androidx.appcompat.R.styleable.Toolbar_subtitleTextColor
)
if (subtitleTextColor != null) {
setSubtitleTextColor(subtitleTextColor)
}
titleCentered =
materialToolbarAttrs.getBoolean(MR.styleable.MaterialToolbar_titleCentered, false)
subtitleCentered =
materialToolbarAttrs.getBoolean(MR.styleable.MaterialToolbar_subtitleCentered, false)
updateTextGravity()
setNavigationIcon(toolbarAttrs.getDrawable(androidx.appcompat.R.styleable.Toolbar_navigationIcon))
setNavigationContentDescription(
toolbarAttrs.getText(androidx.appcompat.R.styleable.Toolbar_navigationContentDescription)
)
val menuResId = toolbarAttrs.getResourceId(androidx.appcompat.R.styleable.Toolbar_menu, 0)
toolbarAttrs.recycle()
materialToolbarAttrs.recycle()
updateNavigationButton()
updateCenterContentVisibility()
if (menuResId != 0) {
inflateMenu(menuResId)
}
}
override fun addView(child: View, index: Int, params: ViewGroup.LayoutParams?) {
val binding = _binding
if (isInflatingInternalLayout || binding == null || child === binding.root) {
super.addView(child, index, params)
return
}
binding.toolbarContentFrame.addView(child, params)
updateCenterContentVisibility()
}
override fun removeView(view: View) {
val binding = _binding
if (
binding != null &&
view.parent === binding.toolbarContentFrame &&
view !== binding.toolbarTitleContainer
) {
binding.toolbarContentFrame.removeView(view)
updateCenterContentVisibility()
return
}
super.removeView(view)
}
var title: CharSequence?
get() = titleText
set(value) {
titleText = value
binding.toolbarTitle.text = value
updateCenterContentVisibility()
}
fun setTitle(@StringRes resId: Int) {
title = if (resId != 0) context.getText(resId) else null
}
var subtitle: CharSequence?
get() = subtitleText
set(value) {
subtitleText = value
binding.toolbarSubtitle.text = value
binding.toolbarSubtitle.isVisible = !value.isNullOrEmpty()
updateCenterContentVisibility()
}
fun setSubtitle(@StringRes resId: Int) {
subtitle = if (resId != 0) context.getText(resId) else null
}
fun setTitleTextAppearance(@StyleRes resId: Int) {
TextViewCompat.setTextAppearance(binding.toolbarTitle, resId)
}
fun setSubtitleTextAppearance(@StyleRes resId: Int) {
TextViewCompat.setTextAppearance(binding.toolbarSubtitle, resId)
}
fun setTitleTextColor(color: ColorStateList) {
binding.toolbarTitle.setTextColor(color)
}
fun setSubtitleTextColor(color: ColorStateList) {
binding.toolbarSubtitle.setTextColor(color)
}
fun setNavigationIcon(@DrawableRes resId: Int) {
setNavigationIcon(
if (resId != 0) AppCompatResources.getDrawable(context, resId) else null
)
}
fun setNavigationIcon(icon: Drawable?) {
navigationIconDrawable = icon
updateNavigationButton()
}
fun getNavigationIcon(): Drawable? = navigationIconDrawable
fun setNavigationContentDescription(description: CharSequence?) {
navigationContentDescriptionText = description
updateNavigationButton()
}
fun setNavigationContentDescription(@StringRes resId: Int) {
setNavigationContentDescription(if (resId != 0) context.getText(resId) else null)
}
fun getNavigationContentDescription(): CharSequence? = navigationContentDescriptionText
fun setNavigationOnClickListener(listener: OnClickListener?) {
navigationClickListener = listener
updateNavigationButton()
}
val menu: Menu
get() = menuBuilder
fun inflateMenu(@MenuRes resId: Int) {
val builder = MenuBuilder(context)
SupportMenuInflater(context).inflate(resId, builder)
menuBuilder = builder
rebuildActionButtons()
}
fun setOnMenuItemClickListener(listener: Toolbar.OnMenuItemClickListener?) {
menuItemClickListener = listener
}
fun getMenuButton(itemId: Int): RippleFixMaterialButton? = actionButtons[itemId]
fun setMenuItemEnabled(itemId: Int, enabled: Boolean) {
menuBuilder.findItem(itemId)?.isEnabled = enabled
getMenuButton(itemId)?.isEnabled = enabled
}
fun getTitleContainer(): View = binding.toolbarTitleContainer
/**
* 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
}
private fun configureIconButton(button: RippleFixMaterialButton) {
button.minimumWidth = resources.getDimensionPixelSize(R.dimen.size_touchable_small)
button.minimumHeight = resources.getDimensionPixelSize(R.dimen.size_touchable_small)
button.iconSize = resources.getDimensionPixelSize(R.dimen.size_icon_small)
}
private fun updateNavigationButton() {
binding.toolbarNavigationButton.apply {
icon = navigationIconDrawable
contentDescription = navigationContentDescriptionText
isVisible = navigationIconDrawable != null
setOnClickListener(navigationClickListener)
TooltipCompat.setTooltipText(this, navigationContentDescriptionText)
}
}
private fun updateTextGravity() {
binding.toolbarTitle.apply {
gravity = if (titleCentered) android.view.Gravity.CENTER_HORIZONTAL else android.view.Gravity.START
textAlignment =
if (titleCentered) {
View.TEXT_ALIGNMENT_CENTER
} else {
View.TEXT_ALIGNMENT_VIEW_START
}
}
binding.toolbarSubtitle.apply {
gravity =
if (subtitleCentered) android.view.Gravity.CENTER_HORIZONTAL else android.view.Gravity.START
textAlignment =
if (subtitleCentered) {
View.TEXT_ALIGNMENT_CENTER
} else {
View.TEXT_ALIGNMENT_VIEW_START
}
}
}
private fun updateCenterContentVisibility() {
val hasCustomContent =
binding.toolbarContentFrame.children.any { it !== binding.toolbarTitleContainer }
val showBuiltInTitle = !hasCustomContent && (!titleText.isNullOrEmpty() || !subtitleText.isNullOrEmpty())
binding.toolbarTitleContainer.isVisible = showBuiltInTitle
}
@SuppressLint("RestrictedApi")
private fun rebuildActionButtons() {
actionButtons.clear()
binding.toolbarActionGroup.removeAllViews()
val actionItems = mutableListOf<MenuItemImpl>()
val overflowItems = mutableListOf<MenuItemImpl>()
for (i in 0 until menuBuilder.size()) {
val item = menuBuilder.getItem(i) as MenuItemImpl
if (!item.isVisible) continue
if (item.requiresActionButton() || item.requestsActionButton()) {
actionItems.add(item)
} else {
overflowItems.add(item)
}
}
for (item in actionItems) {
createActionButton(item)?.let { button ->
binding.toolbarActionGroup.addView(button)
actionButtons[item.itemId] = button
}
}
if (overflowItems.isNotEmpty()) {
binding.toolbarActionGroup.addView(createOverflowButton(overflowItems))
}
binding.toolbarActionGroup.isVisible = binding.toolbarActionGroup.childCount > 0
}
@SuppressLint("RestrictedApi")
private fun createActionButton(item: MenuItemImpl): RippleFixMaterialButton? {
val button =
RippleFixMaterialButton(
createActionButtonContext(item.itemId),
null,
MR.attr.materialIconButtonStyle,
).apply {
configureIconButton(this)
id = item.itemId
icon = item.icon
contentDescription = item.title
isEnabled = item.isEnabled
TooltipCompat.setTooltipText(this, item.title)
setOnClickListener { view ->
if (item.hasSubMenu()) {
showPopupMenu(view, item.subMenu)
} else {
menuItemClickListener?.onMenuItemClick(item)
}
}
}
return button
}
@SuppressLint("RestrictedApi")
private fun createOverflowButton(overflowItems: List<MenuItemImpl>): RippleFixMaterialButton {
return RippleFixMaterialButton(context, null, MR.attr.materialIconButtonStyle).apply {
configureIconButton(this)
setIconResource(R.drawable.ic_more_vert_24)
contentDescription = context.getString(R.string.lbl_more)
TooltipCompat.setTooltipText(this, contentDescription)
setOnClickListener { view ->
val customListener = overflowClickListener
if (customListener != null) {
customListener(view)
} else {
showPopupMenu(view, overflowItems, menuBuilder)
}
}
}
}
private fun showPopupMenu(anchor: View, menu: Menu?) {
if (menu == null) {
return
}
showPopupMenu(anchor, List(menu.size()) { menu.getItem(it) }.filter { it.isVisible }, menu)
}
private fun showPopupMenu(anchor: View, items: List<MenuItem>, sourceMenu: Menu) {
val popup = PopupMenu(context, anchor)
MenuCompat.setGroupDividerEnabled(
popup.menu,
(sourceMenu as? MenuBuilder)?.isGroupDividerEnabled ?: false,
)
val originalItems = mutableMapOf<Int, MenuItem>()
for (item in items) {
popup.menu.add(item.groupId, item.itemId, item.order, item.title).apply {
icon = item.icon
isEnabled = item.isEnabled
isCheckable = item.isCheckable
isChecked = item.isChecked
}
originalItems[item.itemId] = item
}
popup.setOnMenuItemClickListener { clickedItem ->
val originalItem = originalItems[clickedItem.itemId] ?: return@setOnMenuItemClickListener false
if (originalItem.hasSubMenu()) {
showPopupMenu(anchor, originalItem.subMenu)
true
} else {
menuItemClickListener?.onMenuItemClick(originalItem) ?: false
}
}
popup.show()
}
private fun createActionButtonContext(itemId: Int): Context {
val themeOverlayRes =
when (itemId) {
R.id.action_play -> R.style.ThemeOverlay_Auxio_IconButton_Style_Small_Secondary
R.id.action_shuffle -> R.style.ThemeOverlay_Auxio_IconButton_Style_Small_Primary
else -> 0
}
return if (themeOverlayRes != 0) ContextThemeWrapper(context, themeOverlayRes) else context
}
}
@@ -1,205 +0,0 @@
/*
* 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 <https://www.gnu.org/licenses/>.
*/
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.MenuItem
import android.view.View
import android.widget.LinearLayout
import androidx.annotation.AttrRes
import androidx.annotation.MenuRes
import androidx.appcompat.view.SupportMenuInflater
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)
SupportMenuInflater(context).inflate(resId, builder)
menuBuilder = builder
// Separate action items from overflow items based on showAsAction flags.
val actionItems = mutableListOf<MenuItemImpl>()
val overflowItems = mutableListOf<MenuItemImpl>()
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,
)
spacing = 0
}
// 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_vert_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<MenuItem>) {
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()
}
}
@@ -41,10 +41,8 @@ import androidx.navigation.NavController
import androidx.navigation.NavDirections
import androidx.recyclerview.widget.GridLayoutManager
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
@@ -109,33 +107,6 @@ val View.isRtl: Boolean
val ViewBinding.context: Context
get() = root.context
/**
* Override the behavior of a [MaterialToolbar]'s overflow menu to do something else. This is
* extremely dumb, but required to hook overflow menus to bottom sheet menus.
*/
@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) {
// The overflow menu's view implementation is package-private, so test for the
// first child that isn't a plain action button.
if (menuChild !is ActionMenuItemView) {
// Override all listeners related to opening the overflow menu.
menuChild.setOnTouchListener(null)
menuChild.setOnClickListener(block)
return
}
}
}
}
}
/**
* Shortcut to easily set up a [GridLayoutManager.SpanSizeLookup].
*