diff --git a/insets/api/current.api b/insets/api/current.api index 1f99b909e..098e5a48c 100644 --- a/insets/api/current.api +++ b/insets/api/current.api @@ -46,6 +46,7 @@ package com.google.accompanist.insets { } public final class PaddingKt { + method public static inline androidx.compose.ui.Modifier cutoutPadding(androidx.compose.ui.Modifier, optional boolean start, optional boolean top, optional boolean end, optional boolean bottom); method public static inline androidx.compose.ui.Modifier imePadding(androidx.compose.ui.Modifier); method public static inline androidx.compose.ui.Modifier navigationBarsPadding(androidx.compose.ui.Modifier, optional boolean bottom, optional boolean start, optional boolean end); method public static inline androidx.compose.ui.Modifier navigationBarsWithImePadding(androidx.compose.ui.Modifier); diff --git a/insets/src/main/java/com/google/accompanist/insets/Padding.kt b/insets/src/main/java/com/google/accompanist/insets/Padding.kt index b075411b4..54a9987d7 100644 --- a/insets/src/main/java/com/google/accompanist/insets/Padding.kt +++ b/insets/src/main/java/com/google/accompanist/insets/Padding.kt @@ -142,6 +142,36 @@ inline fun Modifier.imePadding(): Modifier = composed { ) } +/** + * Selectively apply additional space which matches the width/height of any display cutout present + * on the respective edges of the screen. + * + * @param start Whether to apply padding to the start edge, which matches the display cutout width + * (if present) on the start edge of the screen. Defaults to `true`. + * @param top Whether to apply padding to the top edge, which matches the display cutout height + * (if present) at the top edge of the screen. Defaults to `true`. + * @param end Whether to apply padding to the end edge, which matches the display cutout width + * (if present) on the end edge of the screen. Defaults to `true`. + * @param bottom Whether to apply padding to the bottom edge, which matches the display cutout + * height (if present) at the bottom edge of the screen. Defaults to `true`. + */ +inline fun Modifier.cutoutPadding( + start: Boolean = true, + top: Boolean = true, + end: Boolean = true, + bottom: Boolean = true, +): Modifier = composed { + padding( + rememberInsetsPaddingValues( + insets = LocalWindowInsets.current.displayCutout, + applyStart = start, + applyTop = top, + applyEnd = end, + applyBottom = bottom + ) + ) +} + /** * Apply additional space which matches the height of the [WindowInsets.ime] (on-screen keyboard) * height and [WindowInsets.navigationBars]. This is what apps should use to handle any insets diff --git a/insets/src/sharedTest/kotlin/com/google/accompanist/insets/InsetsAssertions.kt b/insets/src/sharedTest/kotlin/com/google/accompanist/insets/InsetsAssertions.kt index bd4fb1b03..039d1d1ee 100644 --- a/insets/src/sharedTest/kotlin/com/google/accompanist/insets/InsetsAssertions.kt +++ b/insets/src/sharedTest/kotlin/com/google/accompanist/insets/InsetsAssertions.kt @@ -37,6 +37,8 @@ internal fun WindowInsets.assertEqualTo(insets: WindowInsetsCompat) { // It's difficult to create an expected value for isVisible as it depends on the system ui // of the device. + + displayCutout.assertEqualTo(insets.getInsets(WindowInsetsCompat.Type.displayCutout())) } internal fun WindowInsets.Type.assertEqualTo(insets: androidx.core.graphics.Insets) {