Skip to content

Commit

Permalink
Add AlertDialogWindow instead of UndecoratedWindowAlertDialogProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
MatkovIvan committed Aug 3, 2023
1 parent 6c8e5b9 commit fec2221
Showing 1 changed file with 166 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
/*
* Copyright 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package androidx.compose.material

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.window.WindowDraggableArea
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Shape
import androidx.compose.ui.input.key.Key
import androidx.compose.ui.input.key.key
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.DialogProperties
import androidx.compose.ui.window.DialogWindow
import androidx.compose.ui.window.rememberDialogState

/**
* <a href="https://material.io/components/dialogs#alert-dialog" class="external" target="_blank">Material Design alert dialog</a>.
*
* Alert dialogs interrupt users with urgent information, details, or actions.
*
* ![Dialogs image](https://developer.android.com/images/reference/androidx/compose/material/dialogs.png)
*
* The dialog will position its buttons based on the available space. By default it will try to
* place them horizontally next to each other and fallback to horizontal placement if not enough
* space is available. There is also another version of this composable that has a slot for buttons
* to provide custom buttons layout.
*
* @param onDismissRequest Executes when the user tries to dismiss the Dialog by clicking outside
* or pressing the back button. This is not called when the dismiss button is clicked.
* @param confirmButton A button which is meant to confirm a proposed action, thus resolving
* what triggered the dialog. The dialog does not set up any events for this button so they need
* to be set up by the caller.
* @param modifier Modifier to be applied to the layout of the dialog.
* @param dismissButton A button which is meant to dismiss the dialog. The dialog does not set up
* any events for this button so they need to be set up by the caller.
* @param title The title of the Dialog which should specify the purpose of the Dialog. The title
* is not mandatory, because there may be sufficient information inside the [text]. Provided text
* style will be [Typography.subtitle1].
* @param text The text which presents the details regarding the Dialog's purpose. Provided text
* style will be [Typography.body2].
* @param shape Defines the Dialog's shape
* @param backgroundColor The background color of the dialog.
* @param contentColor The preferred content color provided by this dialog to its children.
* @param properties Typically platform specific properties to further configure the dialog.
*/
@ExperimentalMaterialApi
@Composable
fun AlertDialogWindow(
onDismissRequest: () -> Unit,
confirmButton: @Composable () -> Unit,
modifier: Modifier = Modifier,
dismissButton: @Composable (() -> Unit)? = null,
title: @Composable (() -> Unit)? = null,
text: @Composable (() -> Unit)? = null,
shape: Shape = MaterialTheme.shapes.medium,
backgroundColor: Color = MaterialTheme.colors.surface,
contentColor: Color = contentColorFor(backgroundColor),
properties: DialogProperties = DialogProperties()
) {
AlertDialogWindow(
onDismissRequest = onDismissRequest,
buttons = {
// TODO: move the modifiers to FlowRow when it supports a modifier parameter
Box(Modifier.fillMaxWidth().padding(horizontal = 8.dp, vertical = 2.dp)) {
AlertDialogFlowRow(
mainAxisSpacing = 8.dp,
crossAxisSpacing = 12.dp
) {
dismissButton?.invoke()
confirmButton()
}
}
},
modifier = modifier,
title = title,
text = text,
shape = shape,
backgroundColor = backgroundColor,
contentColor = contentColor,
properties = properties
)
}

/**
* <a href="https://material.io/components/dialogs#alert-dialog" class="external" target="_blank">Material Design alert dialog</a>.
*
* Alert dialogs interrupt users with urgent information, details, or actions.
*
* ![Dialogs image](https://developer.android.com/images/reference/androidx/compose/material/dialogs.png)
*
* This function can be used to fully customize the button area, e.g. with:
*
* @param onDismissRequest Executes when the user tries to dismiss the Dialog by clicking outside
* or pressing the back button. This is not called when the dismiss button is clicked.
* @param buttons Function that emits the layout with the buttons.
* @param modifier Modifier to be applied to the layout of the dialog.
* @param title The title of the Dialog which should specify the purpose of the Dialog. The title
* is not mandatory, because there may be sufficient information inside the [text]. Provided text
* style will be [Typography.subtitle1].
* @param text The text which presents the details regarding the Dialog's purpose. Provided text
* style will be [Typography.body2].
* @param shape Defines the Dialog's shape.
* @param backgroundColor The background color of the dialog.
* @param contentColor The preferred content color provided by this dialog to its children.
* @param properties Typically platform specific properties to further configure the dialog.
*/
@ExperimentalMaterialApi
@Composable
fun AlertDialogWindow(
onDismissRequest: () -> Unit,
buttons: @Composable () -> Unit,
modifier: Modifier = Modifier,
title: (@Composable () -> Unit)? = null,
text: @Composable (() -> Unit)? = null,
shape: Shape = MaterialTheme.shapes.medium,
backgroundColor: Color = MaterialTheme.colors.surface,
contentColor: Color = contentColorFor(backgroundColor),
properties: DialogProperties = DialogProperties()
) {
DialogWindow(
onCloseRequest = onDismissRequest,
state = rememberDialogState(width = Dp.Unspecified, height = Dp.Unspecified),
undecorated = true,
transparent = true,
resizable = false,
onKeyEvent = {
if (properties.dismissOnBackPress && it.key == Key.Escape) {
onDismissRequest()
true
} else {
false
}
},
) {
WindowDraggableArea {
AlertDialogContent(
buttons = buttons,
modifier = modifier,
title = title,
text = text,
shape = shape,
backgroundColor = backgroundColor,
contentColor = contentColor
)
}
}
}

0 comments on commit fec2221

Please sign in to comment.