ui: improve bottom sheet backgrounds

Try to improve the efficiency/management of bottom sheet bakcgrounds
somewhat.
This commit is contained in:
OxygenCobalt 2022-08-06 13:19:58 -06:00
parent 0474940ee3
commit 7b3490d5d3
No known key found for this signature in database
GPG key ID: 37DBE3621FE9AD47
4 changed files with 51 additions and 23 deletions

View file

@ -18,13 +18,17 @@
package org.oxycblt.auxio.playback
import android.content.Context
import android.graphics.drawable.LayerDrawable
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
import androidx.coordinatorlayout.widget.CoordinatorLayout
import com.google.android.material.shape.MaterialShapeDrawable
import com.google.android.material.shape.ShapeAppearanceModel
import org.oxycblt.auxio.R
import org.oxycblt.auxio.settings.Settings
import org.oxycblt.auxio.ui.AuxioSheetBehavior
import org.oxycblt.auxio.util.getAttrColorCompat
import org.oxycblt.auxio.util.getDimen
/**
@ -34,6 +38,21 @@ import org.oxycblt.auxio.util.getDimen
*/
class PlaybackSheetBehavior<V : View>(context: Context, attributeSet: AttributeSet?) :
AuxioSheetBehavior<V>(context, attributeSet) {
val sheetBackgroundDrawable =
MaterialShapeDrawable.createWithElevationOverlay(context).apply {
fillColor = context.getAttrColorCompat(R.attr.colorSurface)
elevation = context.getDimen(R.dimen.elevation_normal)
if (Settings(context).roundMode) {
val cornersMedium = context.getDimen(R.dimen.size_corners_medium)
shapeAppearanceModel =
ShapeAppearanceModel.Builder()
.setTopLeftCornerSize(cornersMedium)
.setTopRightCornerSize(cornersMedium)
.build()
}
}
init {
isHideable = true
if (Settings(context).roundMode) {
@ -48,4 +67,12 @@ class PlaybackSheetBehavior<V : View>(context: Context, attributeSet: AttributeS
// Note: This is an extension to Auxio's vendored BottomSheetBehavior
override fun isHideableWhenDragging() = false
override fun createBackground(context: Context) =
LayerDrawable(
arrayOf(
MaterialShapeDrawable(sheetBackgroundDrawable.shapeAppearanceModel).apply {
fillColor = sheetBackgroundDrawable.fillColor
},
sheetBackgroundDrawable))
}

View file

@ -22,6 +22,8 @@ import android.util.AttributeSet
import android.view.View
import android.view.WindowInsets
import androidx.coordinatorlayout.widget.CoordinatorLayout
import com.google.android.material.shape.MaterialShapeDrawable
import com.google.android.material.shape.ShapeAppearanceModel
import org.oxycblt.auxio.R
import org.oxycblt.auxio.ui.AuxioSheetBehavior
import org.oxycblt.auxio.util.*
@ -37,7 +39,6 @@ class QueueSheetBehavior<V : View>(context: Context, attributeSet: AttributeSet?
init {
isHideable = false
sheetBackgroundDrawable.setCornerSize(context.getDimen(R.dimen.size_corners_medium))
}
override fun layoutDependsOn(parent: CoordinatorLayout, child: V, dependency: View) =
@ -52,6 +53,19 @@ class QueueSheetBehavior<V : View>(context: Context, attributeSet: AttributeSet?
return false // No change, just grabbed the height
}
override fun createBackground(context: Context) =
MaterialShapeDrawable.createWithElevationOverlay(context).apply {
fillColor = context.getAttrColorCompat(R.attr.colorSurface)
elevation = context.getDimen(R.dimen.elevation_normal)
val cornersMedium = context.getDimen(R.dimen.size_corners_medium)
shapeAppearanceModel =
ShapeAppearanceModel.Builder()
.setTopLeftCornerSize(cornersMedium)
.setTopRightCornerSize(cornersMedium)
.build()
}
override fun applyWindowInsets(child: View, insets: WindowInsets): WindowInsets {
super.applyWindowInsets(child, insets)

View file

@ -18,7 +18,7 @@
package org.oxycblt.auxio.ui
import android.content.Context
import android.graphics.drawable.LayerDrawable
import android.graphics.drawable.Drawable
import android.os.Build
import android.util.AttributeSet
import android.view.View
@ -26,7 +26,6 @@ import android.view.ViewGroup
import android.view.WindowInsets
import androidx.coordinatorlayout.widget.CoordinatorLayout
import com.google.android.material.bottomsheet.NeoBottomSheetBehavior
import com.google.android.material.shape.MaterialShapeDrawable
import org.oxycblt.auxio.R
import org.oxycblt.auxio.util.*
@ -38,11 +37,6 @@ import org.oxycblt.auxio.util.*
abstract class AuxioSheetBehavior<V : View>(context: Context, attributeSet: AttributeSet?) :
NeoBottomSheetBehavior<V>(context, attributeSet) {
private var setup = false
val sheetBackgroundDrawable =
MaterialShapeDrawable.createWithElevationOverlay(context).apply {
fillColor = context.getAttrColorCompat(R.attr.colorSurface)
elevation = context.getDimen(R.dimen.elevation_normal)
}
init {
// We need to disable isFitToContents for us to have our bottom sheet expand to the
@ -50,6 +44,9 @@ abstract class AuxioSheetBehavior<V : View>(context: Context, attributeSet: Attr
isFitToContents = false
}
/** Called when the sheet background is being created */
abstract fun createBackground(context: Context): Drawable
/** Called when the child the bottom sheet applies to receives window insets. */
open fun applyWindowInsets(child: View, insets: WindowInsets): WindowInsets {
// All sheet behaviors derive their peek height from the size of the "bar" (i.e the
@ -70,14 +67,7 @@ abstract class AuxioSheetBehavior<V : View>(context: Context, attributeSet: Attr
if (!setup) {
child.apply {
// Sometimes the sheet background will fade out, so guard it with another
// colorSurface drawable to prevent content overlap.
val guardDrawable =
MaterialShapeDrawable(sheetBackgroundDrawable.shapeAppearanceModel).apply {
fillColor = context.getAttrColorCompat(R.attr.colorSurface)
}
background = LayerDrawable(arrayOf(guardDrawable, sheetBackgroundDrawable))
background = createBackground(context)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
// Shadows aren't disabled by default, do that.

View file

@ -30,8 +30,7 @@ import org.oxycblt.auxio.util.replaceSystemBarInsetsCompat
import org.oxycblt.auxio.util.systemBarInsetsCompat
/**
* A behavior that automatically re-layouts and re-insets content to align with the parent layout's
* bottom sheet.
* A behavior that automatically re-insets content to align with the parent layout's bottom sheet.
* @author OxygenCobalt
*/
class BottomSheetContentBehavior<V : View>(context: Context, attributeSet: AttributeSet?) :
@ -61,12 +60,10 @@ class BottomSheetContentBehavior<V : View>(context: Context, attributeSet: Attri
return false
}
if (consumed != lastConsumed) {
lastConsumed = consumed
lastInsets?.let(child::dispatchApplyWindowInsets)
return true
}
lastConsumed = consumed
lastInsets?.let(child::dispatchApplyWindowInsets)
// Re-insetting views does not lead to the child view changing size or position.
return false
}