Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -637,22 +637,14 @@ constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
companion object
}

/**
* Flag to disable WindowInsetsRulers. System UI needs to disable WindowInsets Rulers for all
* ComposeViews, so this is a global switch. We don't want to have them add a ComposeView and the
* new ComposeView suddenly requests WindowInsets updates, changing the behavior so that the insets
* suddenly notify.
*/
internal var areWindowInsetsRulersEnabled = true

/**
* Used to disable [androidx.compose.ui.layout.WindowInsetsRulers]. This can be used when UI never
* reads WindowInsets across all ComposeViews to reduce the overhead of requesting WindowInsets
* updates. Only call this when no ComposeViews will ever need to handle insets over the lifetime of
* the application. This should be called before the first [ComposeView] is created.
*/
fun ComposeView.Companion.disableWindowInsetsRulers() {
areWindowInsetsRulersEnabled = false
androidx.compose.ui.disableWindowInsetsRulers()
}

@OptIn(ExperimentalComposeUiApi::class)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2026 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package androidx.compose.ui

/**
* Flag to disable WindowInsetsRulers. Some integrations need to disable WindowInsets Rulers for all
* Compose roots, so this is a global switch. We don't want them to add a new Compose root and have
* it suddenly request WindowInsets updates, changing the behavior so that the insets suddenly
* notify.
*/
internal var areWindowInsetsRulersEnabled: Boolean = true

/**
* Used to disable [androidx.compose.ui.layout.WindowInsetsRulers]. This can be used when UI never
* reads WindowInsets across all Compose roots to reduce the overhead of requesting WindowInsets
* updates. Only call this when no Compose roots will ever need to handle insets over the lifetime of
* the application. This should be called before the first Compose root is created.
*/
@ExperimentalComposeUiApi
fun disableWindowInsetsRulers() {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Public API in commonMain is strictly no-go in fork. It will just crash on Android

areWindowInsetsRulersEnabled = false
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ internal object SkikoComposeUiFlags {
@Suppress("MutableBareField")
@JvmField
var isDialogAnimationEnabled: Boolean = true

@Suppress("MutableBareField")
@JvmField
var areWindowInsetsRulersEnabled: Boolean = true
}

/**
Expand All @@ -60,20 +56,3 @@ var ComposeUiFlags.isClearFocusOnMouseDownEnabled by SkikoComposeUiFlags::isClea
*/
@ExperimentalComposeUiApi
var ComposeUiFlags.isDialogAnimationEnabled by SkikoComposeUiFlags::isDialogAnimationEnabled

/**
* Enable WindowInsets rulers:
* * `SystemBarsRulers`
* * `ImeRulers`
* * `StatusBarsRulers`
* * `NavigationBarsRulers`
* * `CaptionBarRulers`
* * `MandatorySystemGesturesRulers`
* * `TappableElementRulers`
* * `WaterfallRulers`
* * `SafeDrawingRulers`
* * `SafeGesturesRulers`
* * `SafeContentRulers`
*/
@ExperimentalComposeUiApi
var ComposeUiFlags.areWindowInsetsRulersEnabled by SkikoComposeUiFlags::areWindowInsetsRulersEnabled
Original file line number Diff line number Diff line change
Expand Up @@ -1011,7 +1011,7 @@ private object IdentityPositionCalculator : PositionCalculator {
}

private fun Modifier.rulerProvider(windowInsets: PlatformWindowInsets) =
if (ComposeUiFlags.areWindowInsetsRulersEnabled) then(RulerProviderModifierElement(windowInsets)) else this
if (areWindowInsetsRulersEnabled) then(RulerProviderModifierElement(windowInsets)) else this

private data class RootWindowInsetsProviderModifierElement(
val windowInsets: PlatformWindowInsets,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* Copyright 2026 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package androidx.compose.ui.layout

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.mutableStateOf
import androidx.compose.ui.Modifier
import androidx.compose.ui.areWindowInsetsRulersEnabled
import androidx.compose.ui.platform.PlatformInsets
import androidx.compose.ui.platform.PlatformWindowInsets
import androidx.compose.ui.test.ExperimentalTestApi
import androidx.compose.ui.test.InternalTestApi
import androidx.compose.ui.test.v2.runInternalSkikoComposeUiTest
import kotlin.test.AfterTest
import kotlin.test.Test
import kotlin.test.assertTrue

@OptIn(ExperimentalTestApi::class, InternalTestApi::class)
class DisableWindowInsetsRulersTest {

@AfterTest
fun tearDown() {
areWindowInsetsRulersEnabled = true
}

@Test
fun disableWindowInsetsRulers() {
androidx.compose.ui.disableWindowInsetsRulers()

runInternalSkikoComposeUiTest(
windowInsets = TestWindowInsets(systemBarsInsets = mutableStateOf(PlatformInsets(top = 100)))
) {
var left = 0f
var top = 0f
var right = 0f
var bottom = 0f

setContent {
Box(
Modifier.fillMaxSize().layout { measurable, constraints ->
val placeable = measurable.measure(constraints)
layout(placeable.width, placeable.height) {
placeable.place(0, 0)
left = WindowInsetsRulers.StatusBars.current.left.current(Float.NaN)
top = WindowInsetsRulers.StatusBars.current.top.current(Float.NaN)
right = WindowInsetsRulers.StatusBars.current.right.current(Float.NaN)
bottom = WindowInsetsRulers.StatusBars.current.bottom.current(Float.NaN)
}
}
)
}

waitForIdle()

assertTrue(left.isNaN())
assertTrue(top.isNaN())
assertTrue(right.isNaN())
assertTrue(bottom.isNaN())
}
}
}

private fun TestWindowInsets(
systemBarsInsets: androidx.compose.runtime.State<PlatformInsets> = mutableStateOf(PlatformInsets.Zero)
): PlatformWindowInsets = object : PlatformWindowInsets {
override val statusBars: PlatformInsets get() = PlatformInsets(
getBottom = { systemBarsInsets.value.bottom },
getTop = { systemBarsInsets.value.top },
getLeft = { systemBarsInsets.value.left },
getRight = { systemBarsInsets.value.right }
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@ import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.Composable
import androidx.compose.runtime.State
import androidx.compose.runtime.mutableStateOf
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.areWindowInsetsRulersEnabled
import androidx.compose.ui.layout.Placeable.PlacementScope
import androidx.compose.ui.layout.WindowInsetsRulers.Companion.DisplayCutout
import androidx.compose.ui.layout.WindowInsetsRulers.Companion.StatusBars
import androidx.compose.ui.platform.PlatformInsets
import androidx.compose.ui.test.UIKitInstrumentedTest
import androidx.compose.ui.test.runUIKitInstrumentedTest
Expand All @@ -33,23 +36,30 @@ import androidx.compose.ui.unit.IntRect
import androidx.compose.ui.unit.IntSize
import androidx.compose.ui.unit.toPlatformInsets
import kotlin.math.roundToInt
import kotlin.test.AfterTest
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNotNull
import kotlin.test.assertTrue
import kotlinx.cinterop.ExperimentalForeignApi
import platform.UIKit.UIInterfaceOrientationLandscapeLeft
import platform.UIKit.UIInterfaceOrientationLandscapeRight
import platform.UIKit.UIInterfaceOrientationPortrait
import platform.UIKit.UIInterfaceOrientationPortraitUpsideDown
import platform.UIKit.UIView

@OptIn(ExperimentalForeignApi::class)
@OptIn(ExperimentalForeignApi::class, ExperimentalComposeUiApi::class)
class WindowInsetsRulersTest {

private var contentSize: IntSize = IntSize.Zero
private var insetsRect: IntRect? = null
private val displayCutoutRects = mutableObjectListOf<IntRect?>()

@AfterTest
fun tearDown() {
areWindowInsetsRulersEnabled = true
}

@Test
fun testDisplayCutoutsForPortrait() = runUIKitInstrumentedTest(
ignoreIf = UIKitInstrumentedTest.isRunningOnIPad,
Expand Down Expand Up @@ -209,6 +219,36 @@ class WindowInsetsRulersTest {
assertEquals(boundingRectFromDisplayCutouts, insetsRect)
}

@Test
fun testDisableWindowInsetsRulers() = runUIKitInstrumentedTest {
androidx.compose.ui.disableWindowInsetsRulers()

var left = 0f
var top = 0f
var right = 0f
var bottom = 0f

setContent {
Box(
Modifier.fillMaxSize().layout { measurable, constraints ->
val placeable = measurable.measure(constraints)
layout(placeable.width, placeable.height) {
placeable.place(0, 0)
left = StatusBars.current.left.current(Float.NaN)
top = StatusBars.current.top.current(Float.NaN)
right = StatusBars.current.right.current(Float.NaN)
bottom = StatusBars.current.bottom.current(Float.NaN)
}
}
)
}

assertTrue(left.isNaN())
assertTrue(top.isNaN())
assertTrue(right.isNaN())
assertTrue(bottom.isNaN())
}

private val boundingRectFromDisplayCutouts: IntRect get() {
var left = contentSize.width
var top = contentSize.height
Expand Down
Loading