Skip to content

Commit

Permalink
Shape the surface behind an AlertDialog according to the dialog's sha…
Browse files Browse the repository at this point in the history
…pe. (#424)
  • Loading branch information
m-sasha committed Mar 9, 2023
1 parent 3e02fe5 commit 22b4b01
Showing 1 changed file with 53 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import androidx.compose.ui.window.rememberDialogState
import java.awt.event.KeyEvent
import androidx.compose.ui.window.Dialog as CoreDialog
import androidx.compose.foundation.background
import androidx.compose.ui.graphics.RectangleShape
import androidx.compose.ui.input.key.KeyEventType
import androidx.compose.ui.input.key.type

Expand Down Expand Up @@ -149,7 +150,10 @@ fun AlertDialog(
dialogProvider: AlertDialogProvider = PopupAlertDialogProvider
) {
with(dialogProvider) {
AlertDialog(onDismissRequest = onDismissRequest) {
AlertDialog(
onDismissRequest = onDismissRequest,
shape = shape,
) {
AlertDialogContent(
buttons = buttons,
modifier = modifier.width(IntrinsicSize.Min),
Expand All @@ -174,10 +178,29 @@ interface AlertDialogProvider {
* @param onDismissRequest Callback that will be called when the user closes the dialog
* @param content Content of the dialog
*/
@Deprecated("Will be removed in 1.5; use the overload that takes the dialog shape")
@Composable
fun AlertDialog(
onDismissRequest: () -> Unit,
content: @Composable () -> Unit
)

/**
* Dialog which will be used to place AlertDialog's [content].
*
* @param onDismissRequest Callback that will be called when the user closes the dialog
* @param shape The Dialog's shape
* @param content Content of the dialog
*/
@Suppress("DEPRECATION")
@Composable
fun AlertDialog(
onDismissRequest: () -> Unit,
shape: Shape,
content: @Composable () -> Unit
) = AlertDialog(
onDismissRequest = onDismissRequest,
content = content
)
}

Expand All @@ -188,10 +211,25 @@ interface AlertDialogProvider {
*/
@ExperimentalMaterialApi
object PopupAlertDialogProvider : AlertDialogProvider {

@Deprecated("Will be removed in 1.5; use the overload that takes the dialog shape")
@Composable
override fun AlertDialog(
onDismissRequest: () -> Unit,
content: @Composable () -> Unit
) {
AlertDialog(
onDismissRequest = onDismissRequest,
shape = RectangleShape,
content = content
)
}

@Composable
override fun AlertDialog(
onDismissRequest: () -> Unit,
shape: Shape,
content: @Composable () -> Unit
) {
// Popups on the desktop are by default embedded in the component in which
// they are defined and aligned within its bounds. But an [AlertDialog] needs
Expand Down Expand Up @@ -229,13 +267,18 @@ object PopupAlertDialogProvider : AlertDialogProvider {
},
contentAlignment = Alignment.Center
) {
Surface(Modifier.pointerInput(onDismissRequest) {
detectTapGestures(onPress = {
// Workaround to disable clicks on Surface background https://github.com/JetBrains/compose-jb/issues/2581
})
}, elevation = 24.dp) {
content()
}
Surface(
modifier = Modifier.pointerInput(onDismissRequest) {
detectTapGestures(onPress = {
// Workaround to disable clicks on Surface background
// https://github.com/JetBrains/compose-jb/issues/2581
})
},
shape = shape,
color = Color.Transparent,
elevation = 24.dp,
content = content
)
}
}
}
Expand All @@ -246,6 +289,7 @@ object PopupAlertDialogProvider : AlertDialogProvider {
*/
@ExperimentalMaterialApi
object UndecoratedWindowAlertDialogProvider : AlertDialogProvider {
@Deprecated("Will be removed in 1.5; use the overload that takes the dialog shape")
@OptIn(ExperimentalComposeUiApi::class)
@Composable
override fun AlertDialog(
Expand All @@ -256,6 +300,7 @@ object UndecoratedWindowAlertDialogProvider : AlertDialogProvider {
onCloseRequest = onDismissRequest,
state = rememberDialogState(width = Dp.Unspecified, height = Dp.Unspecified),
undecorated = true,
transparent = true,
resizable = false,
onKeyEvent = {
if (it.key == Key.Escape) {
Expand Down

0 comments on commit 22b4b01

Please sign in to comment.