Skip to content

Instantly share code, notes, and snippets.

@lockbooks

lockbooks/.kt Secret

Created January 20, 2023 12:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lockbooks/cf44c65595cfcc27f8aefd0eac34f00e to your computer and use it in GitHub Desktop.
Save lockbooks/cf44c65595cfcc27f8aefd0eac34f00e to your computer and use it in GitHub Desktop.
System Gesture Insets
private fun setupInsets() = with(binding) {
ViewCompat.setOnApplyWindowInsetsListener(root) { _, windowInsets ->
setupBottomSheetCollback(bottomSheetBehavior, windowInsets)
windowInsets
}
}
private fun <T : View> setupBottomSheetCollback(
bottomSheetBehavior: BottomSheetBehavior<T>,
windowInsets: WindowInsetsCompat
) {
bottomSheetCallback = object : BottomSheetBehavior.BottomSheetCallback() {
override fun onSlide(bottomSheet: View, slideOffset: Float) = Unit
override fun onStateChanged(bottomSheet: View, newState: Int) {
when (newState) {
BottomSheetBehavior.STATE_EXPANDED -> {
excludeGesturesHorizontalLit(windowInsets)
}
BottomSheetBehavior.STATE_COLLAPSED -> {
ViewCompat.setSystemGestureExclusionRects(binding.root, listOf())
}
}
}
}
bottomSheetCallback?.let(bottomSheetBehavior::addBottomSheetCallback)
}
private fun excludeGesturesHorizontalLit(windowInsets: WindowInsetsCompat) {
binding.root.doOnLayout {
val gestureInsets =
windowInsets.getInsets(WindowInsetsCompat.Type.systemGestures())
with(binding) {
val rectTop = root.bottom - bottomSheetInclude.list.height
val rectBottom = root.bottom
val leftExclusionRectLeft = 0
val leftExclusionRectRight = gestureInsets.left
val rightExclusionRectLeft = root.right - gestureInsets.right
val rightExclusionRectRight = root.right
root.setSystemGestureExclusionRectsCompat(
rects = listOf(
Rect(
leftExclusionRectLeft,
rectTop,
leftExclusionRectRight,
rectBottom
),
Rect(
rightExclusionRectLeft,
rectTop,
rightExclusionRectRight,
rectBottom
)
)
)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment