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

Expect Dialog in common #632

Merged
merged 3 commits into from
Jul 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,23 @@ import kotlin.math.roundToInt
* set to `false` for Android [R][Build.VERSION_CODES.R] and earlier.
*/
@Immutable
class DialogProperties constructor(
val dismissOnBackPress: Boolean = true,
val dismissOnClickOutside: Boolean = true,
actual class DialogProperties constructor(
actual val dismissOnBackPress: Boolean = true,
actual val dismissOnClickOutside: Boolean = true,
val securePolicy: SecureFlagPolicy = SecureFlagPolicy.Inherit,
val usePlatformDefaultWidth: Boolean = true,
val decorFitsSystemWindows: Boolean = true
) {
actual constructor(
dismissOnBackPress: Boolean,
dismissOnClickOutside: Boolean
) : this(
dismissOnBackPress = dismissOnBackPress,
dismissOnClickOutside = dismissOnClickOutside,
securePolicy = SecureFlagPolicy.Inherit,
usePlatformDefaultWidth = true,
decorFitsSystemWindows = true
)

constructor(
dismissOnBackPress: Boolean = true,
Expand Down Expand Up @@ -148,7 +158,7 @@ class DialogProperties constructor(
* @param content The content to be displayed inside the dialog.
*/
@Composable
fun Dialog(
actual fun Dialog(
onDismissRequest: () -> Unit,
properties: DialogProperties = DialogProperties(),
content: @Composable () -> Unit
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* 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.ui.window

import androidx.compose.runtime.Composable
import androidx.compose.runtime.Immutable


/**
* Properties used to customize the behavior of a [Dialog].
*
* @property dismissOnBackPress whether the popup can be dismissed by pressing the back button
* * on Android or escape key on desktop.
* If true, pressing the back button will call onDismissRequest.
* @property dismissOnClickOutside whether the dialog can be dismissed by clicking outside the
* dialog's bounds. If true, clicking outside the dialog will call onDismissRequest.
*/
@Immutable
expect class DialogProperties(
dismissOnBackPress: Boolean = true,
dismissOnClickOutside: Boolean = true
) {
val dismissOnBackPress: Boolean
val dismissOnClickOutside: Boolean
}


/**
* Opens a dialog with the given content.
*
* A dialog is a small window that prompts the user to make a decision or enter
* additional information. A dialog does not fill the screen and is normally used
* for modal events that require users to take an action before they can proceed.
*
* The dialog is visible as long as it is part of the composition hierarchy.
* In order to let the user dismiss the Dialog, the implementation of [onDismissRequest] should
* contain a way to remove the dialog from the composition hierarchy.
*
* Example usage:
*
* @sample androidx.compose.ui.samples.DialogSample
*
* @param onDismissRequest Executes when the user tries to dismiss the dialog.
* @param properties [DialogProperties] for further customization of this dialog's behavior.
* @param content The content to be displayed inside the dialog.
*/
@Composable
expect fun Dialog(
onDismissRequest: () -> Unit,
properties: DialogProperties = DialogProperties(),
content: @Composable () -> Unit
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* 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.ui.window

import androidx.compose.runtime.Composable
import androidx.compose.runtime.Immutable
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.drawBehind
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.input.key.Key
import androidx.compose.ui.input.key.KeyEventType
import androidx.compose.ui.input.key.key
import androidx.compose.ui.input.key.type
import androidx.compose.ui.unit.IntOffset

@Immutable
actual class DialogProperties actual constructor(
actual val dismissOnBackPress: Boolean,
actual val dismissOnClickOutside: Boolean
) {
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other !is DialogProperties) return false

if (dismissOnBackPress != other.dismissOnBackPress) return false
if (dismissOnClickOutside != other.dismissOnClickOutside) return false

return true
}

override fun hashCode(): Int {
var result = dismissOnBackPress.hashCode()
result = 31 * result + dismissOnClickOutside.hashCode()
return result
}
}

@OptIn(ExperimentalComposeUiApi::class)
@Composable
actual fun Dialog(
igordmn marked this conversation as resolved.
Show resolved Hide resolved
onDismissRequest: () -> Unit,
properties: DialogProperties,
content: @Composable () -> Unit
) {
val popupPositioner = remember {
AlignmentOffsetPositionProvider(
alignment = Alignment.Center,
offset = IntOffset(0, 0)
)
}
PopupLayout(
popupPositionProvider = popupPositioner,
focusable = true,
if (properties.dismissOnClickOutside) onDismissRequest else null,
modifier = Modifier.drawBehind {
drawRect(Color.Black.copy(alpha = 0.4f))
},
onKeyEvent = {
if (properties.dismissOnBackPress &&
it.type == KeyEventType.KeyDown && it.key == Key.Escape
) {
onDismissRequest()
true
} else {
false
}
},
content = content
)
}