Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shape the surface behind an AlertDialog according to the dialog's shape. #424

Merged
merged 2 commits into from
Mar 9, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 Down Expand Up @@ -179,6 +183,23 @@ interface AlertDialogProvider {
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
*/
@Composable
fun AlertDialog(
onDismissRequest: () -> Unit,
shape: Shape,
content: @Composable () -> Unit
) = AlertDialog(
onDismissRequest = onDismissRequest,
content = content
)
m-sasha marked this conversation as resolved.
Show resolved Hide resolved
}

// TODO(https://github.com/JetBrains/compose-jb/issues/933): is it right to use Popup to show a
Expand All @@ -192,6 +213,19 @@ object PopupAlertDialogProvider : AlertDialogProvider {
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 +263,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 @@ -256,6 +295,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