Skip to content

Commit

Permalink
Merge pull request #62 from mori-atsushi/add-insets
Browse files Browse the repository at this point in the history
 Add supporting insets
  • Loading branch information
mori-atsushi authored Jun 25, 2023
2 parents c4dec84 + a357fc6 commit b71261c
Show file tree
Hide file tree
Showing 6 changed files with 340 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
package com.moriatsushi.insetsx

import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.captionBar as androidCaptionBar
import androidx.compose.foundation.layout.displayCutout as androidDisplayCutout
import androidx.compose.foundation.layout.ime as androidIme
import androidx.compose.foundation.layout.mandatorySystemGestures as androidMandatorySystemGestures
import androidx.compose.foundation.layout.navigationBars as androidNavigationBars
import androidx.compose.foundation.layout.safeContent as androidSafeContent
import androidx.compose.foundation.layout.safeDrawing as androidSafeDrawing
import androidx.compose.foundation.layout.safeGestures as androidSafeGestures
import androidx.compose.foundation.layout.statusBars as androidStatusBars
import androidx.compose.foundation.layout.systemBars as androidSystemBars
import androidx.compose.foundation.layout.systemGestures as androidSystemGestures
import androidx.compose.foundation.layout.tappableElement as androidTappableElement
import androidx.compose.foundation.layout.union
import androidx.compose.foundation.layout.waterfall as androidWaterfall
import androidx.compose.runtime.Composable
import androidx.compose.runtime.NonRestartableComposable

Expand All @@ -19,6 +26,14 @@ actual val WindowInsets.Companion.navigationBars: WindowInsets
@NonRestartableComposable
get() = androidNavigationBars

/**
* The insets representing a caption bar.
*/
actual val WindowInsets.Companion.captionBar: WindowInsets
@Composable
@NonRestartableComposable
get() = androidCaptionBar

/**
* The insets representing status bars.
*/
Expand All @@ -35,6 +50,23 @@ actual val WindowInsets.Companion.systemBars: WindowInsets
@NonRestartableComposable
get() = androidSystemBars

/**
* The insets representing system gestures that have priority and may consume some or all touch
* input.
*/
actual val WindowInsets.Companion.systemGestures: WindowInsets
@Composable
@NonRestartableComposable
get() = androidSystemGestures

/**
* The insets representing the tappable element.
*/
actual val WindowInsets.Companion.tappableElement: WindowInsets
@Composable
@NonRestartableComposable
get() = androidTappableElement

/**
* The insets that include unsafe areas such as system bars and display cutouts,
* but not including ime.
Expand All @@ -44,6 +76,23 @@ actual val WindowInsets.Companion.safeArea: WindowInsets
@NonRestartableComposable
get() = androidSystemBars.union(androidDisplayCutout)

/**
* The insets representing curved areas in a waterfall display.
*/
actual val WindowInsets.Companion.waterfall: WindowInsets
@Composable
@NonRestartableComposable
get() = androidWaterfall

/**
* The insets representing system gestures that have priority and may consume some or all touch
* input.
*/
actual val WindowInsets.Companion.mandatorySystemGestures: WindowInsets
@Composable
@NonRestartableComposable
get() = androidMandatorySystemGestures

/**
* The insets representing the area of the software keyboard.
*/
Expand All @@ -59,3 +108,19 @@ actual val WindowInsets.Companion.safeDrawing: WindowInsets
@Composable
@NonRestartableComposable
get() = androidSafeDrawing

/**
* The insets that include areas where gestures may be confused with other input.
*/
actual val WindowInsets.Companion.safeGestures: WindowInsets
@Composable
@NonRestartableComposable
get() = androidSafeGestures

/**
* The insets that include all areas that may be drawn over or have gesture confusion.
*/
actual val WindowInsets.Companion.safeContent: WindowInsets
@Composable
@NonRestartableComposable
get() = androidSafeContent
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package com.moriatsushi.insetsx

import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.runtime.Composable
import androidx.compose.runtime.Immutable
import androidx.compose.ui.unit.Density
import androidx.compose.ui.unit.LayoutDirection

/**
* The insets representing navigation bars.
Expand All @@ -15,6 +18,15 @@ import androidx.compose.runtime.Composable
expect val WindowInsets.Companion.navigationBars: WindowInsets
@Composable get

/**
* The insets representing a caption bar.
*
* * In Android: a caption bar
* * In iOS, desktop and web: return 0
*/
expect val WindowInsets.Companion.captionBar: WindowInsets
@Composable get

/**
* The insets representing status bars.
*
Expand All @@ -38,6 +50,36 @@ expect val WindowInsets.Companion.statusBars: WindowInsets
expect val WindowInsets.Companion.systemBars: WindowInsets
@Composable get

/**
* The insets representing system gestures that have priority and may consume some or all touch
* input.
*
* * In Android: system gestures
* * In iOS: bottom of safe area (home indicator)
* * In desktop and web: return 0
*/
expect val WindowInsets.Companion.systemGestures: WindowInsets
@Composable get

/**
* The insets representing the tappable element.
*
* * In Android: tappable element
* * In iOS: top of safe area (status bar)
* * In desktop and web: return 0
*/
expect val WindowInsets.Companion.tappableElement: WindowInsets
@Composable get

/**
* The insets representing curved areas in a waterfall display.
*
* * In Android: waterfall
* * In iOS, desktop and web: return 0
*/
expect val WindowInsets.Companion.waterfall: WindowInsets
@Composable get

/**
* The insets that include unsafe areas such as system bars and display cutouts,
* but not including [ime].
Expand All @@ -49,6 +91,17 @@ expect val WindowInsets.Companion.systemBars: WindowInsets
expect val WindowInsets.Companion.safeArea: WindowInsets
@Composable get

/**
* The insets representing system gestures that have priority and may consume some or all touch
* input.
*
* * In Android: mandatory system gestures
* * In iOS: bottom of safe area (home indicator)
* * In desktop and web: return 0
*/
expect val WindowInsets.Companion.mandatorySystemGestures: WindowInsets
@Composable get

/**
* The insets representing the area of the software keyboard.
*
Expand All @@ -70,3 +123,49 @@ expect val WindowInsets.Companion.ime: WindowInsets
@ExperimentalSoftwareKeyboardApi
expect val WindowInsets.Companion.safeDrawing: WindowInsets
@Composable get

/**
* The insets that include areas where gestures may be confused with other input.
*
* * In Android: [system gestures][systemGestures] + [mandatory system gestures][mandatorySystemGestures],
* [rounded display areas][waterfall], and [tappable areas][tappableElement].
* * In iOS: top and bottom of safe area (home indicator + status bar)
* * In desktop and web: return 0
*/
expect val WindowInsets.Companion.safeGestures: WindowInsets
@Composable get

/**
* The insets that include all areas that may be drawn over or have gesture confusion.
*
* * In Android: [systemGestures] + [safeGestures].
* * In iOS: safe area + IME
* * In desktop and web: return 0
*/
expect val WindowInsets.Companion.safeContent: WindowInsets
@Composable get

/**
* It always returns 0.
*/
internal val WindowInsets.Companion.zero: WindowInsets
get() = ZeroWindowInsets

@Immutable
private object ZeroWindowInsets : WindowInsets {
override fun getBottom(density: Density): Int {
return 0
}

override fun getLeft(density: Density, layoutDirection: LayoutDirection): Int {
return 0
}

override fun getRight(density: Density, layoutDirection: LayoutDirection): Int {
return 0
}

override fun getTop(density: Density): Int {
return 0
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,46 +10,89 @@ import androidx.compose.ui.composed
* Adds padding to accommodate the [safe drawing][WindowInsets.Companion.safeDrawing] insets.
*/
@ExperimentalSoftwareKeyboardApi
fun Modifier.safeDrawingPadding() = windowInsetsPadding {
fun Modifier.safeDrawingPadding(): Modifier = windowInsetsPadding {
WindowInsets.safeDrawing
}

/**
* Adds padding to accommodate the [safe area][WindowInsets.Companion.safeArea] insets.
*/
fun Modifier.safeAreaPadding() = windowInsetsPadding {
fun Modifier.safeAreaPadding(): Modifier = windowInsetsPadding {
WindowInsets.safeArea
}

/**
* Adds padding to accommodate the [status bars][WindowInsets.Companion.statusBars] insets.
*/
fun Modifier.statusBarsPadding(): Modifier = windowInsetsPadding {
WindowInsets.statusBars
}

/**
* Adds padding to accommodate the [system bars][WindowInsets.Companion.systemBars] insets.
*/
fun Modifier.systemBarsPadding() = windowInsetsPadding {
fun Modifier.systemBarsPadding(): Modifier = windowInsetsPadding {
WindowInsets.systemBars
}

/**
* Adds padding to accommodate the [status bars][WindowInsets.Companion.statusBars] insets.
* Adds padding to accommodate the [system gestures][WindowInsets.Companion.systemGestures] insets.
*/
fun Modifier.statusBarsPadding() = windowInsetsPadding {
WindowInsets.statusBars
fun Modifier.systemGesturesPadding(): Modifier = windowInsetsPadding {
WindowInsets.systemGestures
}

/**
* Adds padding to accommodate the [waterfall][WindowInsets.Companion.waterfall] insets.
*/
fun Modifier.waterfallPadding(): Modifier = windowInsetsPadding {
WindowInsets.waterfall
}

/**
* Adds padding to accommodate the
* [mandatory system gestures][WindowInsets.Companion.mandatorySystemGestures] insets.
*/
fun Modifier.mandatorySystemGesturesPadding(): Modifier = windowInsetsPadding {
WindowInsets.mandatorySystemGestures
}

/**
* Adds padding to accommodate the [ime][WindowInsets.Companion.ime] insets.
*/
@ExperimentalSoftwareKeyboardApi
fun Modifier.imePadding() = windowInsetsPadding {
fun Modifier.imePadding(): Modifier = windowInsetsPadding {
WindowInsets.ime
}

/**
* Adds padding to accommodate the [navigation bars][WindowInsets.Companion.navigationBars] insets.
*/
fun Modifier.navigationBarsPadding() = windowInsetsPadding {
fun Modifier.navigationBarsPadding(): Modifier = windowInsetsPadding {
WindowInsets.navigationBars
}

/**
* Adds padding to accommodate the [caption bar][WindowInsets.Companion.captionBar] insets.
*/
fun Modifier.captionBarPadding(): Modifier = windowInsetsPadding {
WindowInsets.captionBar
}

/**
* Adds padding to accommodate the [safe gestures][WindowInsets.Companion.safeGestures] insets.
*/
fun Modifier.safeGesturesPadding(): Modifier = windowInsetsPadding {
WindowInsets.safeGestures
}

/**
* Adds padding to accommodate the [safe content][WindowInsets.Companion.safeContent] insets.
*/
fun Modifier.safeContentPadding(): Modifier = windowInsetsPadding {
WindowInsets.safeContent
}

private inline fun Modifier.windowInsetsPadding(
crossinline block: @Composable () -> WindowInsets,
): Modifier = composed {
Expand Down
Loading

0 comments on commit b71261c

Please sign in to comment.