MutableWindowInsets


A WindowInsets whose values can change without changing the instance. This is useful to avoid recomposition when WindowInsets can change.

import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.MutableWindowInsets
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.asPaddingValues
import androidx.compose.foundation.layout.exclude
import androidx.compose.foundation.layout.navigationBars
import androidx.compose.foundation.layout.navigationBarsPadding
import androidx.compose.foundation.layout.onConsumedWindowInsetsChanged
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.safeContent
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.core.view.WindowCompat

class SampleActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        WindowCompat.setDecorFitsSystemWindows(window, false)
        super.onCreate(savedInstanceState)
        setContent {
            val remainingInsets = remember { MutableWindowInsets() }
            val safeContent = WindowInsets.safeContent
            Box(
                Modifier.navigationBarsPadding().onConsumedWindowInsetsChanged {
                    consumedWindowInsets ->
                    remainingInsets.insets = safeContent.exclude(consumedWindowInsets)
                }
            ) {
                // padding can be used without recomposition when insets change.
                val padding = remainingInsets.asPaddingValues()
                Box(Modifier.padding(padding))
            }
        }
    }
}

Note: This API as experimental since it doesn't enforce the right consumption patterns.

Summary

Public constructors

Cmn

Public functions

open Int
getBottom(density: Density)

The space, in pixels, at the bottom of the window that the inset represents.

Cmn
open Int
getLeft(density: Density, layoutDirection: LayoutDirection)

The space, in pixels, at the left of the window that the inset represents.

Cmn
open Int
getRight(density: Density, layoutDirection: LayoutDirection)

The space, in pixels, at the right of the window that the inset represents.

Cmn
open Int
getTop(density: Density)

The space, in pixels, at the top of the window that the inset represents.

Cmn

Public properties

WindowInsets

The WindowInsets that are used for left, top, right, and bottom values.

Cmn

Public constructors

MutableWindowInsets

MutableWindowInsets(
    initialInsets: WindowInsets = WindowInsets(0, 0, 0, 0)
)

Public functions

getBottom

open fun getBottom(density: Density): Int

The space, in pixels, at the bottom of the window that the inset represents.

getLeft

open fun getLeft(density: Density, layoutDirection: LayoutDirection): Int

The space, in pixels, at the left of the window that the inset represents.

getRight

open fun getRight(density: Density, layoutDirection: LayoutDirection): Int

The space, in pixels, at the right of the window that the inset represents.

getTop

open fun getTop(density: Density): Int

The space, in pixels, at the top of the window that the inset represents.

Public properties

insets

var insetsWindowInsets

The WindowInsets that are used for left, top, right, and bottom values.