Skip to content

Instantly share code, notes, and snippets.

@Salzian
Last active October 27, 2022 22:34
Show Gist options
  • Save Salzian/f6a6ecb1939f25243fa035b2d8a66b6e to your computer and use it in GitHub Desktop.
Save Salzian/f6a6ecb1939f25243fa035b2d8a66b6e to your computer and use it in GitHub Desktop.
This piece of math recreates the BoxInsetLayout class behaviour for View based UI building on Wear OS in Jetpack Compose. Using this @Composabe will create a Box within the confines of a round display without cutting off the corners.
@Composable
fun RoundBoxInset(content: @Composable () -> Unit = {}) {
BoxWithConstraints {
val horizontalPadding = (maxWidth.value - (maxWidth.value / 2.0 * sqrt(2.0))) / 2.0
val verticalPadding = (maxHeight.value - (maxHeight.value / 2.0 * sqrt(2.0))) / 2.0
Box(
modifier = Modifier
.fillMaxSize()
.padding(horizontal = horizontalPadding.dp, vertical = verticalPadding.dp)
) {
content()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment