Make BottomSheet`s handle fit top window inset
This commit is contained in:
@@ -2,15 +2,11 @@ package org.koitharu.kotatsu.base.ui.widgets
|
||||
|
||||
import android.animation.LayoutTransition
|
||||
import android.content.Context
|
||||
import android.transition.AutoTransition
|
||||
import android.transition.TransitionManager
|
||||
import android.util.AttributeSet
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.view.WindowInsets
|
||||
import android.view.animation.AccelerateDecelerateInterpolator
|
||||
import android.view.animation.DecelerateInterpolator
|
||||
import androidx.annotation.AttrRes
|
||||
import androidx.annotation.StringRes
|
||||
import androidx.appcompat.widget.Toolbar
|
||||
@@ -43,7 +39,10 @@ class BottomSheetHeaderBar @JvmOverloads constructor(
|
||||
private val locationBuffer = IntArray(2)
|
||||
private val expansionListeners = LinkedList<OnExpansionChangeListener>()
|
||||
private var fitStatusBar = false
|
||||
private var transition: AutoTransition? = null
|
||||
private val minHandleHeight = context.resources.getDimensionPixelSize(R.dimen.bottom_sheet_handle_size_min)
|
||||
private val maxHandleHeight = context.resources.getDimensionPixelSize(R.dimen.bottom_sheet_handle_size_max)
|
||||
private var isLayoutSuppressedCompat = false
|
||||
private var isLayoutCalledWhileSuppressed = false
|
||||
|
||||
@Deprecated("")
|
||||
val toolbar: MaterialToolbar
|
||||
@@ -156,6 +155,14 @@ class BottomSheetHeaderBar @JvmOverloads constructor(
|
||||
binding.toolbar.setSubtitle(resId)
|
||||
}
|
||||
|
||||
override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {
|
||||
if (isLayoutSuppressedCompat) {
|
||||
isLayoutCalledWhileSuppressed = true
|
||||
} else {
|
||||
super.onLayout(changed, l, t, r, b)
|
||||
}
|
||||
}
|
||||
|
||||
private fun setBottomSheetBehavior(behavior: BottomSheetBehavior<*>?) {
|
||||
bottomSheetBehavior?.removeBottomSheetCallback(bottomSheetCallback)
|
||||
bottomSheetBehavior = behavior
|
||||
@@ -170,11 +177,21 @@ class BottomSheetHeaderBar @JvmOverloads constructor(
|
||||
if (isExpanded == binding.dragHandle.isGone) {
|
||||
return
|
||||
}
|
||||
TransitionManager.beginDelayedTransition(this, getTransition())
|
||||
suppressLayoutCompat(true)
|
||||
binding.toolbar.navigationIcon = (if (isExpanded) closeDrawable else null)
|
||||
binding.dragHandle.isGone = isExpanded
|
||||
expansionListeners.forEach { it.onExpansionStateChanged(this, isExpanded) }
|
||||
dispatchInsets(ViewCompat.getRootWindowInsets(this))
|
||||
suppressLayoutCompat(false)
|
||||
}
|
||||
|
||||
private fun suppressLayoutCompat(suppress: Boolean) {
|
||||
if (suppress == isLayoutSuppressedCompat) return
|
||||
isLayoutSuppressedCompat = suppress
|
||||
if (!suppress && isLayoutCalledWhileSuppressed) {
|
||||
requestLayout()
|
||||
}
|
||||
isLayoutCalledWhileSuppressed = false
|
||||
}
|
||||
|
||||
private fun dispatchInsets(insets: WindowInsetsCompat?) {
|
||||
@@ -182,11 +199,14 @@ class BottomSheetHeaderBar @JvmOverloads constructor(
|
||||
return
|
||||
}
|
||||
val isExpanded = binding.dragHandle.isGone
|
||||
val topInset = insets?.getInsets(WindowInsetsCompat.Type.systemBars())?.top ?: 0
|
||||
if (isExpanded) {
|
||||
val topInset = insets?.getInsets(WindowInsetsCompat.Type.systemBars())?.top ?: 0
|
||||
updatePadding(top = topInset)
|
||||
} else {
|
||||
updatePadding(top = 0)
|
||||
binding.dragHandle.updateLayoutParams {
|
||||
height = topInset.coerceIn(minHandleHeight, maxHandleHeight)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -225,7 +245,7 @@ class BottomSheetHeaderBar @JvmOverloads constructor(
|
||||
return true
|
||||
}
|
||||
val viewId = child.id
|
||||
return viewId == R.id.dragHandle || viewId == R.id.toolbar || viewId == R.id.frame
|
||||
return viewId == R.id.dragHandle || viewId == R.id.toolbar
|
||||
}
|
||||
|
||||
private fun convertLayoutParams(params: ViewGroup.LayoutParams?): Toolbar.LayoutParams? {
|
||||
@@ -242,15 +262,6 @@ class BottomSheetHeaderBar @JvmOverloads constructor(
|
||||
}
|
||||
}
|
||||
|
||||
private fun getTransition(): AutoTransition {
|
||||
transition?.let { return it }
|
||||
val t = AutoTransition()
|
||||
t.duration = context.getAnimationDuration(android.R.integer.config_shortAnimTime)
|
||||
t.addTarget(binding.dragHandle)
|
||||
transition = t
|
||||
return t
|
||||
}
|
||||
|
||||
private inner class Callback : BottomSheetBehavior.BottomSheetCallback(), View.OnClickListener {
|
||||
|
||||
override fun onStateChanged(bottomSheet: View, newState: Int) {
|
||||
|
||||
@@ -6,27 +6,20 @@
|
||||
android:layout_height="wrap_content"
|
||||
tools:parentTag="com.google.android.material.appbar.AppBarLayout">
|
||||
|
||||
<FrameLayout
|
||||
android:id="@+id/frame"
|
||||
<com.google.android.material.bottomsheet.BottomSheetDragHandleView
|
||||
android:id="@+id/dragHandle"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="?attr/actionBarSize">
|
||||
android:layout_height="@dimen/bottom_sheet_handle_size_min"
|
||||
android:minHeight="0dp"
|
||||
android:paddingTop="12dp"
|
||||
android:paddingBottom="0dp" />
|
||||
|
||||
<com.google.android.material.appbar.MaterialToolbar
|
||||
android:id="@+id/toolbar"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="?attr/actionBarSize"
|
||||
android:theme="@style/ThemeOverlay.Kotatsu.MainToolbar"
|
||||
tools:navigationIcon="?actionModeCloseDrawable"
|
||||
tools:title="@string/options" />
|
||||
|
||||
<com.google.android.material.bottomsheet.BottomSheetDragHandleView
|
||||
android:id="@+id/dragHandle"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:minHeight="0dp"
|
||||
android:paddingTop="12dp"
|
||||
android:paddingBottom="0dp" />
|
||||
|
||||
</FrameLayout>
|
||||
<com.google.android.material.appbar.MaterialToolbar
|
||||
android:id="@+id/toolbar"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="?attr/actionBarSize"
|
||||
android:theme="@style/ThemeOverlay.Kotatsu.MainToolbar"
|
||||
tools:navigationIcon="?actionModeCloseDrawable"
|
||||
tools:title="@string/options" />
|
||||
|
||||
</merge>
|
||||
|
||||
@@ -35,6 +35,8 @@
|
||||
<dimen name="search_suggestions_manga_spacing">4dp</dimen>
|
||||
|
||||
<dimen name="bottom_sheet_width">0dp</dimen>
|
||||
<dimen name="bottom_sheet_handle_size_min">16dp</dimen>
|
||||
<dimen name="bottom_sheet_handle_size_max">24dp</dimen>
|
||||
<dimen name="dialog_radius">8dp</dimen>
|
||||
|
||||
<dimen name="appwidget_corner_radius_inner">8dp</dimen>
|
||||
|
||||
Reference in New Issue
Block a user