Update DeviceConfigurationOverride.WindowInsets to match upstream impl

Change-Id: Ibce5b499fc67305e12bb3862fc6aefc3647adb75
pull/1559/head
Alex Vanyo 5 months ago
parent 420076f01a
commit 1591f263a8

@ -17,18 +17,17 @@
package com.google.samples.apps.nowinandroid.ui package com.google.samples.apps.nowinandroid.ui
import android.view.WindowInsets import android.view.WindowInsets
import android.widget.FrameLayout import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue import androidx.compose.runtime.getValue
import androidx.compose.runtime.rememberUpdatedState import androidx.compose.runtime.rememberUpdatedState
import androidx.compose.ui.platform.ComposeView import androidx.compose.ui.platform.AbstractComposeView
import androidx.compose.ui.test.DeviceConfigurationOverride import androidx.compose.ui.test.DeviceConfigurationOverride
import androidx.compose.ui.viewinterop.AndroidView import androidx.compose.ui.viewinterop.AndroidView
import androidx.core.view.WindowInsetsCompat import androidx.core.view.WindowInsetsCompat
import androidx.core.view.children import androidx.core.view.children
/** /**
* A [DeviceConfigurationOverride] that allows overriding the [windowInsets] available * A [DeviceConfigurationOverride] that overrides the window insets for the contained content.
* to the content under test.
*/ */
@Suppress("ktlint:standard:function-naming") @Suppress("ktlint:standard:function-naming")
fun DeviceConfigurationOverride.Companion.WindowInsets( fun DeviceConfigurationOverride.Companion.WindowInsets(
@ -38,10 +37,17 @@ fun DeviceConfigurationOverride.Companion.WindowInsets(
val currentWindowInsets by rememberUpdatedState(windowInsets) val currentWindowInsets by rememberUpdatedState(windowInsets)
AndroidView( AndroidView(
factory = { context -> factory = { context ->
object : FrameLayout(context) { object : AbstractComposeView(context) {
@Composable
override fun Content() {
currentContentUnderTest()
}
override fun dispatchApplyWindowInsets(insets: WindowInsets): WindowInsets { override fun dispatchApplyWindowInsets(insets: WindowInsets): WindowInsets {
children.forEach { children.forEach {
it.dispatchApplyWindowInsets(currentWindowInsets.toWindowInsets()) it.dispatchApplyWindowInsets(
WindowInsets(currentWindowInsets.toWindowInsets()),
)
} }
return WindowInsetsCompat.CONSUMED.toWindowInsets()!! return WindowInsetsCompat.CONSUMED.toWindowInsets()!!
} }
@ -52,17 +58,10 @@ fun DeviceConfigurationOverride.Companion.WindowInsets(
*/ */
@Deprecated("Deprecated in Java") @Deprecated("Deprecated in Java")
override fun requestFitSystemWindows() { override fun requestFitSystemWindows() {
dispatchApplyWindowInsets(currentWindowInsets.toWindowInsets()!!) dispatchApplyWindowInsets(WindowInsets(currentWindowInsets.toWindowInsets()!!))
} }
}.apply {
addView(
ComposeView(context).apply {
setContent {
currentContentUnderTest()
}
},
)
} }
}, },
update = { with(currentWindowInsets) { it.requestApplyInsets() } },
) )
} }

Loading…
Cancel
Save