all: deslopping pt 1

This commit is contained in:
Alexander Capehart
2026-04-12 15:25:10 -06:00
parent 3f2e5bc0b1
commit fae6ff4cdf
7 changed files with 158 additions and 308 deletions
@@ -83,7 +83,7 @@ abstract class DetailFragment<P : MusicParent, C : Music> :
binding.detailNormalToolbar.apply { binding.detailNormalToolbar.apply {
setNavigationOnClickListener { findNavController().navigateUp() } setNavigationOnClickListener { findNavController().navigateUp() }
setOnMenuItemClickListener(this@DetailFragment) setOnMenuItemClickListener(this@DetailFragment)
overrideOnOverflowMenuClick { onOpenParentMenu() } setOnOverflowMenuClick { onOpenParentMenu() }
} }
binding.detailRecycler.apply { binding.detailRecycler.apply {
@@ -123,7 +123,7 @@ abstract class DetailFragment<P : MusicParent, C : Music> :
detailHeader.alpha = 1 - outRatio detailHeader.alpha = 1 - outRatio
val inRatio = max(ratio - 0.5f, 0f) * 2 val inRatio = max(ratio - 0.5f, 0f) * 2
animateToolbarView(binding.detailNormalToolbar.getTitleContainer(), inRatio) animateToolbarView(binding.detailNormalToolbar.titleContainer, inRatio)
animateToolbarActionButton( animateToolbarActionButton(
binding.detailNormalToolbar.getMenuButton(R.id.action_play), binding.detailNormalToolbar.getMenuButton(R.id.action_play),
inRatio, inRatio,
@@ -48,7 +48,7 @@ abstract class SelectionFragment<VB : ViewBinding> :
// Add cancel and menu item listeners to manage what occurs with the selection. // Add cancel and menu item listeners to manage what occurs with the selection.
setNavigationOnClickListener { listModel.dropSelection() } setNavigationOnClickListener { listModel.dropSelection() }
setOnMenuItemClickListener(this@SelectionFragment) setOnMenuItemClickListener(this@SelectionFragment)
overrideOnOverflowMenuClick { setOnOverflowMenuClick {
listModel.openMenu(R.menu.selection, listModel.peekSelection()) listModel.openMenu(R.menu.selection, listModel.peekSelection())
} }
} }
@@ -279,15 +279,6 @@ class PlaybackPanelFragment :
playbackModel.song.value?.let { detailModel.showAlbum(it.album) } playbackModel.song.value?.let { detailModel.showAlbum(it.album) }
} }
// PlayerFastSeekOverlay.PerformListener implementation
override fun onDoubleTap() {
// Already handled by onStepForward/onStepBack
}
override fun onDoubleTapEnd() {
// Animation cleanup is handled by the overlay
}
override fun getFastSeekDirection( override fun getFastSeekDirection(
portion: DisplayPortion portion: DisplayPortion
): PlayerFastSeekOverlay.PerformListener.FastSeekDirection { ): PlayerFastSeekOverlay.PerformListener.FastSeekDirection {
@@ -20,7 +20,6 @@ package org.oxycblt.auxio.playback.ui.stepper
import android.content.Context import android.content.Context
import android.util.AttributeSet import android.util.AttributeSet
import android.util.Log
import android.view.GestureDetector import android.view.GestureDetector
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.MotionEvent import android.view.MotionEvent
@@ -29,9 +28,9 @@ import com.google.android.material.shape.MaterialShapeDrawable
import com.google.android.material.shape.ShapeAppearanceModel import com.google.android.material.shape.ShapeAppearanceModel
import dagger.hilt.android.AndroidEntryPoint import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject import javax.inject.Inject
import org.oxycblt.auxio.BuildConfig
import org.oxycblt.auxio.R import org.oxycblt.auxio.R
import org.oxycblt.auxio.ui.UISettings import org.oxycblt.auxio.ui.UISettings
import timber.log.Timber as L
enum class DisplayPortion { enum class DisplayPortion {
LEFT, LEFT,
@@ -160,7 +159,7 @@ class PlayerFastSeekOverlay(context: Context, attrs: AttributeSet?) :
private var rightFadeOutRunnable: Runnable? = null private var rightFadeOutRunnable: Runnable? = null
override fun onDoubleTapStarted(portion: DisplayPortion) { override fun onDoubleTapStarted(portion: DisplayPortion) {
if (DEBUG) Log.d(TAG, "onDoubleTapStarted called with portion = [$portion]") L.d("onDoubleTapStarted called with portion = [$portion]")
val shouldForward: Boolean = val shouldForward: Boolean =
performListener?.getFastSeekDirection(portion)?.directionAsBoolean ?: return performListener?.getFastSeekDirection(portion)?.directionAsBoolean ?: return
@@ -178,8 +177,7 @@ class PlayerFastSeekOverlay(context: Context, attrs: AttributeSet?) :
val shouldForward: Boolean = val shouldForward: Boolean =
performListener?.getFastSeekDirection(portion)?.directionAsBoolean ?: return performListener?.getFastSeekDirection(portion)?.directionAsBoolean ?: return
if (DEBUG) L.d("onDoubleTapProgressDown called with shouldForward = [$shouldForward]")
Log.d(TAG, "onDoubleTapProgressDown called with " + "shouldForward = [$shouldForward]")
if (shouldForward) { if (shouldForward) {
handleRightOverlayTap() handleRightOverlayTap()
@@ -187,7 +185,6 @@ class PlayerFastSeekOverlay(context: Context, attrs: AttributeSet?) :
handleLeftOverlayTap() handleLeftOverlayTap()
} }
performListener?.onDoubleTap()
performListener?.seek(forward = shouldForward) performListener?.seek(forward = shouldForward)
} }
@@ -270,11 +267,10 @@ class PlayerFastSeekOverlay(context: Context, attrs: AttributeSet?) :
} }
override fun onDoubleTapFinished() { override fun onDoubleTapFinished() {
if (DEBUG) Log.d(TAG, "onDoubleTapFinished called") L.d("onDoubleTapFinished called")
// Handle left overlay fade out // Handle left overlay fade out
if (leftInitTap) { if (leftInitTap) {
performListener?.onDoubleTapEnd()
leftInitTap = false leftInitTap = false
leftSecondsView.stopAnimation() leftSecondsView.stopAnimation()
@@ -303,7 +299,6 @@ class PlayerFastSeekOverlay(context: Context, attrs: AttributeSet?) :
// Handle right overlay fade out // Handle right overlay fade out
if (rightInitTap) { if (rightInitTap) {
performListener?.onDoubleTapEnd()
rightInitTap = false rightInitTap = false
rightSecondsView.stopAnimation() rightSecondsView.stopAnimation()
@@ -332,9 +327,6 @@ class PlayerFastSeekOverlay(context: Context, attrs: AttributeSet?) :
} }
interface PerformListener { interface PerformListener {
fun onDoubleTap()
fun onDoubleTapEnd()
/** Determines if the playback should forward/rewind or do nothing. */ /** Determines if the playback should forward/rewind or do nothing. */
fun getFastSeekDirection(portion: DisplayPortion): FastSeekDirection fun getFastSeekDirection(portion: DisplayPortion): FastSeekDirection
@@ -348,11 +340,9 @@ class PlayerFastSeekOverlay(context: Context, attrs: AttributeSet?) :
} }
} }
override fun onTouchEvent(event: MotionEvent): Boolean { override fun onTouchEvent(event: MotionEvent): Boolean =
return gestureDetector.onTouchEvent(event) || super.onTouchEvent(event) gestureDetector.onTouchEvent(event) || super.onTouchEvent(event)
}
// GestureDetector.OnDoubleTapListener implementation
override fun onSingleTapConfirmed(e: MotionEvent) = false override fun onSingleTapConfirmed(e: MotionEvent) = false
override fun onDoubleTap(e: MotionEvent): Boolean { override fun onDoubleTap(e: MotionEvent): Boolean {
@@ -376,9 +366,4 @@ class PlayerFastSeekOverlay(context: Context, attrs: AttributeSet?) :
} }
override fun onDoubleTapEvent(e: MotionEvent) = false override fun onDoubleTapEvent(e: MotionEvent) = false
companion object {
private const val TAG = "PlayerFastSeekOverlay"
private val DEBUG = BuildConfig.DEBUG
}
} }
@@ -20,8 +20,6 @@ package org.oxycblt.auxio.ui
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.content.Context import android.content.Context
import android.content.res.ColorStateList
import android.graphics.drawable.Drawable
import android.util.AttributeSet import android.util.AttributeSet
import android.view.ContextThemeWrapper import android.view.ContextThemeWrapper
import android.view.LayoutInflater import android.view.LayoutInflater
@@ -30,244 +28,174 @@ import android.view.MenuItem
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import android.widget.FrameLayout import android.widget.FrameLayout
import android.widget.LinearLayout
import androidx.annotation.AttrRes import androidx.annotation.AttrRes
import androidx.annotation.DrawableRes
import androidx.annotation.MenuRes import androidx.annotation.MenuRes
import androidx.annotation.StringRes import androidx.appcompat.R as AR
import androidx.annotation.StyleRes
import androidx.appcompat.content.res.AppCompatResources
import androidx.appcompat.view.SupportMenuInflater import androidx.appcompat.view.SupportMenuInflater
import androidx.appcompat.view.menu.MenuBuilder import androidx.appcompat.view.menu.MenuBuilder
import androidx.appcompat.view.menu.MenuItemImpl import androidx.appcompat.view.menu.MenuItemImpl
import androidx.appcompat.widget.PopupMenu import androidx.appcompat.widget.PopupMenu
import androidx.appcompat.widget.Toolbar import androidx.appcompat.widget.Toolbar
import androidx.appcompat.widget.TooltipCompat import androidx.appcompat.widget.TooltipCompat
import androidx.core.view.MenuCompat
import androidx.core.view.children import androidx.core.view.children
import androidx.core.view.get
import androidx.core.view.isNotEmpty
import androidx.core.view.isVisible import androidx.core.view.isVisible
import androidx.core.widget.TextViewCompat import androidx.core.view.size
import com.google.android.material.R as MR import com.google.android.material.R as MR
import org.oxycblt.auxio.R import org.oxycblt.auxio.R
import org.oxycblt.auxio.databinding.ViewToolbarBinding import org.oxycblt.auxio.databinding.ViewToolbarBinding
import org.oxycblt.auxio.util.unlikelyToBeNull
/** /**
* A compound toolbar view that owns its entire layout instead of piggybacking on AppCompat's * Minimal MaterialToolbar replacement that implements everything with MaterialButtons compared to
* internal Toolbar structure. It preserves the small API surface Auxio actually uses while * the main androidx/MDC toolbar which is more or less unusable.
* rendering action items as an M3 Expressive *
* [com.google.android.material.button.MaterialButtonGroup]. * @author Alexander Capehart (OxygenCobalt)
*/ */
class AuxioToolbar class AuxioToolbar
@JvmOverloads @JvmOverloads
constructor( constructor(
context: Context, context: Context,
attrs: AttributeSet? = null, attrs: AttributeSet? = null,
@AttrRes defStyleAttr: Int = androidx.appcompat.R.attr.toolbarStyle, @AttrRes defStyleAttr: Int = AR.attr.toolbarStyle,
) : FrameLayout(context, attrs, defStyleAttr) { ) : FrameLayout(context, attrs, defStyleAttr) {
private var menuItemClickListener: Toolbar.OnMenuItemClickListener? = null private var inflatingLayout: Boolean
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 private val binding: ViewToolbarBinding
get() = checkNotNull(_binding) { "Toolbar binding was not initialized" } private var menuItemClickListener: Toolbar.OnMenuItemClickListener? = null
private var overflowClickListener: OnClickListener? = null
private val actionButtons = mutableMapOf<Int, RippleFixMaterialButton>() private val actionButtons = mutableMapOf<Int, RippleFixMaterialButton>()
@SuppressLint("RestrictedApi") private var menuBuilder = MenuBuilder(context) @SuppressLint("RestrictedApi") private var menuBuilder = MenuBuilder(context)
init { init {
_binding = ViewToolbarBinding.inflate(LayoutInflater.from(context), this, true) inflatingLayout = true
isInflatingInternalLayout = false binding = ViewToolbarBinding.inflate(LayoutInflater.from(context), this, true)
inflatingLayout = false
clipChildren = false clipChildren = false
clipToPadding = false clipToPadding = false
binding.toolbarRoot.clipChildren = false binding.toolbarRoot.apply {
binding.toolbarRoot.clipToPadding = false clipChildren = false
binding.toolbarContentFrame.clipChildren = false clipToPadding = false
binding.toolbarContentFrame.clipToPadding = false }
binding.toolbarActionGroup.clipChildren = false binding.toolbarContentFrame.apply {
binding.toolbarActionGroup.clipToPadding = false clipChildren = false
binding.toolbarActionGroup.spacing = 0 clipToPadding = false
}
binding.toolbarActionGroup.apply {
clipChildren = false
clipToPadding = false
spacing = 0
}
configureIconButton(binding.toolbarNavigationButton) configureIconButton(binding.toolbarNavigationButton)
val toolbarAttrs = val toolbarAttrs =
context.obtainStyledAttributes( context.obtainStyledAttributes(attrs, AR.styleable.Toolbar, defStyleAttr, 0)
attrs,
androidx.appcompat.R.styleable.Toolbar,
defStyleAttr,
0,
)
val materialToolbarAttrs = val materialToolbarAttrs =
context.obtainStyledAttributes(attrs, MR.styleable.MaterialToolbar, defStyleAttr, 0) context.obtainStyledAttributes(attrs, MR.styleable.MaterialToolbar, defStyleAttr, 0)
titleText = toolbarAttrs.getText(androidx.appcompat.R.styleable.Toolbar_title) val titleCentered =
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) materialToolbarAttrs.getBoolean(MR.styleable.MaterialToolbar_titleCentered, false)
subtitleCentered = binding.toolbarTitle.apply {
materialToolbarAttrs.getBoolean(MR.styleable.MaterialToolbar_subtitleCentered, false) text = toolbarAttrs.getText(AR.styleable.Toolbar_title)
updateTextGravity() isVisible = !text.isNullOrEmpty()
gravity =
setNavigationIcon( if (titleCentered) android.view.Gravity.CENTER_HORIZONTAL
toolbarAttrs.getDrawable(androidx.appcompat.R.styleable.Toolbar_navigationIcon) else android.view.Gravity.START
) textAlignment =
setNavigationContentDescription( if (titleCentered) {
toolbarAttrs.getText( TEXT_ALIGNMENT_CENTER
androidx.appcompat.R.styleable.Toolbar_navigationContentDescription } else {
TEXT_ALIGNMENT_VIEW_START
}
setTextAppearance(
toolbarAttrs.getResourceId(
AR.styleable.Toolbar_titleTextAppearance,
MR.attr.textAppearanceDisplaySmallEmphasized,
)
) )
) }
val menuResId = toolbarAttrs.getResourceId(androidx.appcompat.R.styleable.Toolbar_menu, 0) val subtitleCentered =
materialToolbarAttrs.getBoolean(MR.styleable.MaterialToolbar_subtitleCentered, false)
binding.toolbarSubtitle.apply {
text = toolbarAttrs.getText(AR.styleable.Toolbar_subtitle)
isVisible = !text.isNullOrEmpty()
gravity =
if (subtitleCentered) android.view.Gravity.CENTER_HORIZONTAL
else android.view.Gravity.START
textAlignment =
if (subtitleCentered) {
TEXT_ALIGNMENT_CENTER
} else {
TEXT_ALIGNMENT_VIEW_START
}
setTextAppearance(
toolbarAttrs.getResourceId(
AR.styleable.Toolbar_subtitleTextAppearance,
MR.attr.textAppearanceTitleLarge,
)
)
}
binding.toolbarNavigationButton.apply {
icon = toolbarAttrs.getDrawable(AR.styleable.Toolbar_navigationIcon)
contentDescription =
toolbarAttrs.getText(AR.styleable.Toolbar_navigationContentDescription)
isVisible = icon != null
TooltipCompat.setTooltipText(this, contentDescription)
}
val menuResId = toolbarAttrs.getResourceId(AR.styleable.Toolbar_menu, 0)
toolbarAttrs.recycle() toolbarAttrs.recycle()
materialToolbarAttrs.recycle() materialToolbarAttrs.recycle()
updateNavigationButton()
updateCenterContentVisibility()
if (menuResId != 0) { if (menuResId != 0) {
inflateMenu(menuResId) inflateMenu(menuResId)
} }
} }
override fun addView(child: View, index: Int, params: ViewGroup.LayoutParams?) { override fun addView(child: View?, index: Int, params: ViewGroup.LayoutParams?) {
val binding = _binding if (inflatingLayout) {
if (isInflatingInternalLayout || binding == null || child === binding.root) {
super.addView(child, index, params) super.addView(child, index, params)
return } else {
// hardcode layoutparams because injectable children logic is insane and never
binding.toolbarTitleContainer.addView(
child,
LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT),
)
} }
binding.toolbarContentFrame.addView(child, params)
updateCenterContentVisibility()
} }
override fun removeView(view: View) { override fun onFinishInflate() {
val binding = _binding super.onFinishInflate()
if (
binding != null &&
view.parent === binding.toolbarContentFrame &&
view !== binding.toolbarTitleContainer
) {
binding.toolbarContentFrame.removeView(view)
updateCenterContentVisibility()
return
}
super.removeView(view)
} }
var title: CharSequence? var title: CharSequence?
get() = titleText get() = binding.toolbarTitle.text
set(value) { set(value) {
titleText = value binding.toolbarTitle.apply {
binding.toolbarTitle.text = value text = value
updateCenterContentVisibility() isVisible = !text.isNullOrEmpty()
}
} }
fun setTitle(@StringRes resId: Int) {
title = if (resId != 0) context.getText(resId) else null
}
var subtitle: CharSequence? var subtitle: CharSequence?
get() = subtitleText get() = binding.toolbarSubtitle.text
set(value) { set(value) {
subtitleText = value binding.toolbarSubtitle.apply {
binding.toolbarSubtitle.text = value text = value
binding.toolbarSubtitle.isVisible = !value.isNullOrEmpty() isVisible = !text.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 val menu: Menu
get() = menuBuilder get() = menuBuilder
@SuppressLint("RestrictedApi")
fun inflateMenu(@MenuRes resId: Int) { fun inflateMenu(@MenuRes resId: Int) {
val builder = MenuBuilder(context) val builder = MenuBuilder(context)
SupportMenuInflater(context).inflate(resId, builder) SupportMenuInflater(context).inflate(resId, builder)
@@ -281,18 +209,24 @@ constructor(
fun getMenuButton(itemId: Int): RippleFixMaterialButton? = actionButtons[itemId] fun getMenuButton(itemId: Int): RippleFixMaterialButton? = actionButtons[itemId]
@SuppressLint("RestrictedApi")
fun setMenuItemEnabled(itemId: Int, enabled: Boolean) { fun setMenuItemEnabled(itemId: Int, enabled: Boolean) {
menuBuilder.findItem(itemId)?.isEnabled = enabled menuBuilder.findItem(itemId)?.isEnabled = enabled
getMenuButton(itemId)?.isEnabled = enabled getMenuButton(itemId)?.isEnabled = enabled
} }
fun getTitleContainer(): View = binding.toolbarTitleContainer val titleContainer: View
get() = binding.toolbarTitleContainer
fun setNavigationOnClickListener(listener: OnClickListener) {
binding.toolbarNavigationButton.setOnClickListener(listener)
}
/** /**
* Override the overflow button's click behavior. When set, the overflow button will call * Override the overflow button's click behavior. When set, the overflow button will call
* [block] instead of showing the default popup menu. * [block] instead of showing the default popup menu.
*/ */
fun overrideOnOverflowMenuClick(block: (View) -> Unit) { fun setOnOverflowMenuClick(block: OnClickListener) {
overflowClickListener = block overflowClickListener = block
} }
@@ -302,49 +236,6 @@ constructor(
button.iconSize = resources.getDimensionPixelSize(R.dimen.size_icon_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") @SuppressLint("RestrictedApi")
private fun rebuildActionButtons() { private fun rebuildActionButtons() {
actionButtons.clear() actionButtons.clear()
@@ -363,49 +254,45 @@ constructor(
} }
for (item in actionItems) { for (item in actionItems) {
createActionButton(item)?.let { button -> val btn = createActionButton(item)
binding.toolbarActionGroup.addView(button) binding.toolbarActionGroup.addView(btn)
actionButtons[item.itemId] = button actionButtons[item.itemId] = btn
}
} }
if (overflowItems.isNotEmpty()) { if (overflowItems.isNotEmpty()) {
binding.toolbarActionGroup.addView(createOverflowButton(overflowItems)) binding.toolbarActionGroup.addView(createOverflowButton(overflowItems))
} }
binding.toolbarActionGroup.isVisible = binding.toolbarActionGroup.childCount > 0 binding.toolbarActionGroup.isVisible = binding.toolbarActionGroup.isNotEmpty()
} }
@SuppressLint("RestrictedApi") @SuppressLint("RestrictedApi")
private fun createActionButton(item: MenuItemImpl): RippleFixMaterialButton? { private fun createActionButton(item: MenuItemImpl) =
val button = RippleFixMaterialButton(
RippleFixMaterialButton( createActionButtonContext(item.itemId),
createActionButtonContext(item.itemId), null,
null, MR.attr.materialIconButtonStyle,
MR.attr.materialIconButtonStyle, )
) .apply {
.apply { configureIconButton(this)
configureIconButton(this) id = item.itemId
id = item.itemId icon = item.icon
icon = item.icon contentDescription = item.title
contentDescription = item.title isEnabled = item.isEnabled
isEnabled = item.isEnabled TooltipCompat.setTooltipText(this, item.title)
TooltipCompat.setTooltipText(this, item.title) setOnClickListener { view ->
setOnClickListener { view -> if (item.hasSubMenu()) {
if (item.hasSubMenu()) { val subMenu = unlikelyToBeNull(item.subMenu)
showPopupMenu(view, item.subMenu) showPopupMenu(view, subMenu.children.toList())
} else { } else {
menuItemClickListener?.onMenuItemClick(item) menuItemClickListener?.onMenuItemClick(item)
}
} }
} }
}
return button
}
@SuppressLint("RestrictedApi") @SuppressLint("RestrictedApi")
private fun createOverflowButton(overflowItems: List<MenuItemImpl>): RippleFixMaterialButton { private fun createOverflowButton(overflowItems: List<MenuItemImpl>) =
return RippleFixMaterialButton(context, null, MR.attr.materialIconButtonStyle).apply { RippleFixMaterialButton(context, null, MR.attr.materialIconButtonStyle).apply {
configureIconButton(this) configureIconButton(this)
setIconResource(R.drawable.ic_more_vert_24) setIconResource(R.drawable.ic_more_vert_24)
contentDescription = context.getString(R.string.lbl_more) contentDescription = context.getString(R.string.lbl_more)
@@ -413,28 +300,16 @@ constructor(
setOnClickListener { view -> setOnClickListener { view ->
val customListener = overflowClickListener val customListener = overflowClickListener
if (customListener != null) { if (customListener != null) {
customListener(view) customListener.onClick(view)
} else { } else {
showPopupMenu(view, overflowItems, menuBuilder) showPopupMenu(view, overflowItems)
} }
} }
} }
}
private fun showPopupMenu(anchor: View, menu: Menu?) { @SuppressLint("RestrictedApi")
if (menu == null) { private fun showPopupMenu(anchor: View, items: List<MenuItem>) {
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) val popup = PopupMenu(context, anchor)
MenuCompat.setGroupDividerEnabled(
popup.menu,
(sourceMenu as? MenuBuilder)?.isGroupDividerEnabled ?: false,
)
val originalItems = mutableMapOf<Int, MenuItem>() val originalItems = mutableMapOf<Int, MenuItem>()
for (item in items) { for (item in items) {
popup.menu.add(item.groupId, item.itemId, item.order, item.title).apply { popup.menu.add(item.groupId, item.itemId, item.order, item.title).apply {
@@ -445,16 +320,10 @@ constructor(
} }
originalItems[item.itemId] = item originalItems[item.itemId] = item
} }
popup.setOnMenuItemClickListener { clickedItem -> popup.setOnMenuItemClickListener { clickedItem ->
val originalItem = val originalItem =
originalItems[clickedItem.itemId] ?: return@setOnMenuItemClickListener false originalItems[clickedItem.itemId] ?: return@setOnMenuItemClickListener false
if (originalItem.hasSubMenu()) { menuItemClickListener?.onMenuItemClick(originalItem) ?: false
showPopupMenu(anchor, originalItem.subMenu)
true
} else {
menuItemClickListener?.onMenuItemClick(originalItem) ?: false
}
} }
popup.show() popup.show()
} }
@@ -25,11 +25,12 @@ import androidx.annotation.DrawableRes
import com.google.android.material.R import com.google.android.material.R
/** /**
* A [RippleFixMaterialButton] that works around a bug where changing the icon while the button's * [RippleFixMaterialButton] that works around another bug where switching the icon during a press
* ripple/press animation is active causes a width change that "sticks". The workaround defers * breaks width expansion animations.
* re-applying the icon until the button is no longer pressed.
* *
* @author Alexander Capehart (OxygenCobalt) * @author Alexander Capehart (OxygenCobalt)
*
* TODO: Should animate icon transitions to make this look less bad.
*/ */
class WidthFixMaterialButton class WidthFixMaterialButton
@JvmOverloads @JvmOverloads
@@ -60,6 +61,7 @@ constructor(
override fun setIconResource(@DrawableRes iconRes: Int) { override fun setIconResource(@DrawableRes iconRes: Int) {
super.setIconResource(iconRes) super.setIconResource(iconRes)
pendingIconRes = iconRes pendingIconRes = iconRes
postOnAnimation(applyPendingIconRunnable)
removeCallbacks(applyPendingIconRunnable) removeCallbacks(applyPendingIconRunnable)
postOnAnimation(applyPendingIconRunnable) postOnAnimation(applyPendingIconRunnable)
} }
+4 -1
View File
@@ -44,6 +44,7 @@
android:includeFontPadding="false" android:includeFontPadding="false"
android:maxLines="1" android:maxLines="1"
android:singleLine="true" android:singleLine="true"
android:textAppearance="?attr/textAppearanceDisplaySmallEmphasized"
android:textColor="?attr/colorOnSurface" /> android:textColor="?attr/colorOnSurface" />
<TextView <TextView
@@ -54,8 +55,10 @@
android:includeFontPadding="false" android:includeFontPadding="false"
android:maxLines="1" android:maxLines="1"
android:singleLine="true" android:singleLine="true"
android:textColor="?android:attr/textColorSecondary" android:textColor="?attr/colorOnSurfaceVariant"
android:visibility="gone" /> android:visibility="gone" />
</LinearLayout> </LinearLayout>
</FrameLayout> </FrameLayout>