Fix insets on API < 29

This commit is contained in:
FooIbar committed 2023-11-01 23:17:46 +08:00
1 parent d6a3f21754
commit 61e2ace5c8
1 file changed
+16 -1
@@ -1,5 +1,6 @@
package com.hippo.ehviewer.util
import android.graphics.Rect
import android.view.View
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.runtime.Composable
@@ -13,7 +14,21 @@ import kotlin.contracts.contract
inline fun buildWindowInsets(builderAction: WindowInsetsCompat.Builder.() -> Unit): WindowInsetsCompat {
contract { callsInPlace(builderAction, InvocationKind.EXACTLY_ONCE) }
val builder = WindowInsetsCompat.Builder()
val builder = if (isAtLeastQ) {
WindowInsetsCompat.Builder()
} else {
// TODO: Report to google issue tracker
// On API 20-28, WindowInsetsCompat.Builder() will copy the WindowInsets.CONSUMED static field,
// which isConsumed() returns true, resulting in the insets not being dispatched
runCatching {
val constructor = android.view.WindowInsets::class.java.getConstructor(Rect::class.java)
val platformInsets = constructor.newInstance(Rect())
WindowInsetsCompat.Builder(WindowInsetsCompat.toWindowInsetsCompat(platformInsets))
}.getOrElse {
it.printStackTrace()
WindowInsetsCompat.Builder()
}
}
builder.builderAction()
return builder.build()
}