-
-
Save lockbooks/cf44c65595cfcc27f8aefd0eac34f00e to your computer and use it in GitHub Desktop.
System Gesture Insets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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