Skip to content

Commit

Permalink
improve InsetsUtils - do not consume insets by default
Browse files Browse the repository at this point in the history
  • Loading branch information
kroegerama committed Jul 21, 2024
1 parent 56df484 commit 943ea0e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/ProjectInfo.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
object P {
const val projectVersion = "4.10.4"
const val projectVersion = "4.10.5"

const val projectGroupId = "com.kroegerama.android-kaiteki"

Expand Down
24 changes: 21 additions & 3 deletions core/src/main/kotlin/com/kroegerama/kaiteki/InsetsUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ fun View.doOnApplyWindowInsetsRelative(
fun <T : View> T.handleWindowInsets(
edge: WindowInsetsEdge,
typeMask: Int = WindowInsetsCompat.Type.systemBars() or WindowInsetsCompat.Type.displayCutout() or WindowInsetsCompat.Type.ime(),
consumer: (WindowInsetsCompat) -> WindowInsetsCompat = { WindowInsetsCompat.CONSUMED },
consumer: WindowInsetsCompat.(insets: Insets) -> WindowInsetsCompat = noopInsetsConsumer,
paddingUpdater: T.(start: Int, top: Int, end: Int, bottom: Int) -> Unit = View::setPaddingRelative
) {
if (this is ViewGroup && clipToPadding) {
Expand All @@ -114,14 +114,14 @@ fun <T : View> T.handleWindowInsets(
bars.bottom.takeIf(edge.bottom) + originalPadding.bottom
)

consumer(this)
consumer(bars)
}
}

fun MaterialCardView.handleWindowInsets(
edge: WindowInsetsEdge,
typeMask: Int = WindowInsetsCompat.Type.systemBars() or WindowInsetsCompat.Type.displayCutout() or WindowInsetsCompat.Type.ime(),
consumer: (WindowInsetsCompat) -> WindowInsetsCompat = { WindowInsetsCompat.CONSUMED },
consumer: WindowInsetsCompat.(insets: Insets) -> WindowInsetsCompat = noopInsetsConsumer
) = handleWindowInsets(edge, typeMask, consumer) { start, top, end, bottom ->
val isRtl = getLayoutDirection() == View.LAYOUT_DIRECTION_RTL
setContentPadding(
Expand All @@ -132,6 +132,8 @@ fun MaterialCardView.handleWindowInsets(
)
}

val noopInsetsConsumer: WindowInsetsCompat.(insets: Insets) -> WindowInsetsCompat = { this }

data class RelativePadding(
val start: Int,
val top: Int,
Expand All @@ -147,6 +149,22 @@ data class WindowInsetsEdge(
val end: Boolean = false,
val bottom: Boolean = false
) {
fun insetsConsumer(view: View): WindowInsetsCompat.(insets: Insets) -> WindowInsetsCompat = { insets ->
val isRtl = view.getLayoutDirection() == View.LAYOUT_DIRECTION_RTL
inset(this, insets, isRtl)
}

fun inset(original: WindowInsetsCompat, insets: Insets, isRtl: Boolean): WindowInsetsCompat {
val left = if (isRtl) end else start
val right = if (isRtl) start else end
return original.inset(
if (left) insets.left else 0,
if (top) insets.top else 0,
if (right) insets.right else 0,
if (bottom) insets.bottom else 0
)
}

companion object {
val Start = WindowInsetsEdge(
start = true,
Expand Down

0 comments on commit 943ea0e

Please sign in to comment.