@@ -25,6 +25,8 @@ import androidx.compose.foundation.layout.displayCutout
2525import androidx.compose.foundation.layout.fillMaxSize
2626import androidx.compose.foundation.layout.imePadding
2727import androidx.compose.foundation.layout.safeDrawingPadding
28+ import androidx.compose.foundation.layout.size
29+ import androidx.compose.foundation.layout.statusBars
2830import androidx.compose.foundation.layout.systemGesturesPadding
2931import androidx.compose.foundation.layout.windowInsetsPadding
3032import androidx.compose.material.Text
@@ -36,6 +38,7 @@ import androidx.compose.runtime.getValue
3638import androidx.compose.runtime.mutableStateOf
3739import androidx.compose.runtime.remember
3840import androidx.compose.runtime.setValue
41+ import androidx.compose.ui.Alignment
3942import androidx.compose.ui.Modifier
4043import androidx.compose.ui.focus.FocusRequester
4144import androidx.compose.ui.focus.focusRequester
@@ -47,13 +50,16 @@ import androidx.compose.ui.unit.DpRect
4750import androidx.compose.ui.unit.DpSize
4851import androidx.compose.ui.unit.dp
4952import androidx.compose.ui.unit.toDpRect
53+ import androidx.compose.ui.viewinterop.UIKitView
54+ import androidx.compose.ui.window.ComposeUIView
5055import kotlin.test.Test
5156import kotlin.test.assertEquals
5257import kotlinx.cinterop.ExperimentalForeignApi
5358import kotlinx.cinterop.useContents
5459import org.jetbrains.skiko.OS
5560import org.jetbrains.skiko.OSVersion
5661import org.jetbrains.skiko.available
62+ import platform.UIKit.UIColor
5763import platform.UIKit.UIDevice
5864import platform.UIKit.UIInterfaceOrientationMaskLandscapeLeft
5965import platform.UIKit.UIInterfaceOrientationMaskLandscapeRight
@@ -164,6 +170,98 @@ class WindowInsetsPaddingTest {
164170
165171 assertEquals(false , recomposed.value)
166172 }
173+
174+ @OptIn(ExperimentalForeignApi ::class )
175+ @Test
176+ fun testWindowInsetsPaddingAppliedToNonFullscreenContent () = runUIKitInstrumentedTest {
177+ var innerBoxRect = DpRectZero ()
178+ var outerBoxRect = DpRectZero ()
179+
180+ setContent {
181+ Box (modifier = Modifier .background(Color .Red ).fillMaxSize()) {
182+ Box (
183+ modifier = Modifier
184+ .align(Alignment .Center )
185+ .background(Color .Blue )
186+ .size(200 .dp, 200 .dp)
187+ .onGloballyPositioned {
188+ outerBoxRect = it.boundsInWindow().toDpRect(density)
189+ },
190+ ) {
191+ Box (modifier = Modifier
192+ .windowInsetsPadding(WindowInsets .statusBars)
193+ .background(Color .Green )
194+ .fillMaxSize()
195+ .onGloballyPositioned {
196+ innerBoxRect = it.boundsInWindow().toDpRect(density)
197+ }
198+ )
199+ }
200+ }
201+ }
202+
203+ // WindowInsets.statusBars should only represent the insets at the top in portrait orientation
204+ val topSafeAreaInsetsDp = viewController.view.safeAreaInsets.useContents { top }.dp
205+
206+ assertEquals(
207+ DpRect (
208+ left = outerBoxRect.left,
209+ top = outerBoxRect.top + topSafeAreaInsetsDp,
210+ right = outerBoxRect.right,
211+ bottom = outerBoxRect.bottom
212+ ),
213+ innerBoxRect
214+ )
215+ }
216+
217+ @OptIn(ExperimentalForeignApi ::class )
218+ @Test
219+ fun testWindowInsetsPaddingAppliedToNonFullscreenComposeUIViewContent () = runUIKitInstrumentedTest {
220+ var innerBoxRect = DpRectZero ()
221+ var outerBoxRect = DpRectZero ()
222+
223+ setContent {
224+ Box (modifier = Modifier .background(Color .Red ).fillMaxSize()) {
225+ UIKitView (
226+ factory = {
227+ ComposeUIView (
228+ configure = { opaque = false }
229+ ) {
230+ Box (modifier = Modifier
231+ .windowInsetsPadding(WindowInsets .statusBars)
232+ .background(Color .Green )
233+ .fillMaxSize()
234+ .onGloballyPositioned {
235+ innerBoxRect = it.boundsInWindow().toDpRect(density)
236+ }
237+ )
238+ }.apply {
239+ backgroundColor = UIColor .blueColor
240+ }
241+ },
242+ modifier = Modifier
243+ .align(Alignment .Center )
244+ .size(200 .dp, 200 .dp)
245+ .onGloballyPositioned {
246+ outerBoxRect = it.boundsInWindow().toDpRect(density)
247+ }
248+ )
249+ }
250+ }
251+
252+ // WindowInsets.statusBars should only represent the insets at the top in portrait orientation
253+ val topSafeAreaInsetsDp = viewController.view.safeAreaInsets.useContents { top }.dp
254+
255+ assertEquals(
256+ DpRect (
257+ left = outerBoxRect.left,
258+ top = outerBoxRect.top + topSafeAreaInsetsDp,
259+ right = outerBoxRect.right,
260+ bottom = outerBoxRect.bottom
261+ ),
262+ innerBoxRect
263+ )
264+ }
167265}
168266
169267@Composable
0 commit comments