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

Rename desktop's Dialog to DialogWindow #661

Merged
merged 2 commits into from
Jul 19, 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.
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 @@ -24,13 +24,13 @@ import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Dialog
import androidx.compose.ui.window.DialogWindow
import androidx.compose.ui.window.application
import androidx.compose.ui.window.rememberDialogState
import kotlinx.coroutines.delay

fun main() = application {
Dialog(
DialogWindow(
MatkovIvan marked this conversation as resolved.
Show resolved Hide resolved
onCloseRequest = ::exitApplication,
state = rememberDialogState(width = 400.dp, height = 600.dp),
undecorated = true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ import androidx.compose.ui.unit.IntOffset
import androidx.compose.ui.unit.IntSize
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.ApplicationScope
import androidx.compose.ui.window.Dialog
import androidx.compose.ui.window.DialogWindow
import androidx.compose.ui.window.Notification
import androidx.compose.ui.window.Popup
import androidx.compose.ui.window.TrayState
Expand Down Expand Up @@ -276,7 +276,7 @@ fun WindowScope.Content(
backgroundColor = Color(70, 70, 70)
)
} else {
Dialog(
DialogWindow(
onCloseRequest = dismiss
) {
WindowContent(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ import androidx.compose.ui.unit.DpSize
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.ApplicationScope
import androidx.compose.ui.window.AwtWindow
import androidx.compose.ui.window.Dialog
import androidx.compose.ui.window.DialogWindow
import androidx.compose.ui.window.MenuBar
import androidx.compose.ui.window.Notification
import androidx.compose.ui.window.Tray
Expand Down Expand Up @@ -262,7 +262,7 @@ fun dialog() = GlobalScope.launchApplication {
}

if (isDialogShowing) {
Dialog(onCloseRequest = { isDialogShowing = false }) {
DialogWindow(onCloseRequest = { isDialogShowing = false }) {
Text("Dialog")
}
}
Expand All @@ -279,7 +279,7 @@ fun hideDialog() = GlobalScope.launchApplication {
Text("Dialog")
}

Dialog(
DialogWindow(
onCloseRequest = { isDialogVisible = false },
visible = isDialogVisible
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ import androidx.compose.ui.window.Popup
import androidx.compose.ui.window.PopupPositionProvider
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.foundation.layout.PaddingValues
import androidx.compose.ui.draw.shadow
import androidx.compose.ui.graphics.RectangleShape
import androidx.compose.ui.input.key.KeyEventType
import androidx.compose.ui.input.key.type
import androidx.compose.ui.window.DialogWindow

/**
* The default padding for an [AlertDialog].
Expand Down Expand Up @@ -414,7 +414,7 @@ object UndecoratedWindowAlertDialogProvider : AlertDialogProvider {
onDismissRequest: () -> Unit,
content: @Composable () -> Unit
) {
CoreDialog(
DialogWindow(
onCloseRequest = onDismissRequest,
state = rememberDialogState(width = Dp.Unspecified, height = Dp.Unspecified),
undecorated = true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import androidx.compose.material.TextField
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.window.Dialog
import androidx.compose.ui.window.DialogWindow
import androidx.compose.ui.window.Popup
import org.junit.Ignore
import org.junit.Rule
Expand All @@ -45,7 +45,7 @@ class FiltersTest {
@Ignore // TODO: Fix Dialog to have the dialog() semantic property
fun testIsDialog() {
rule.setContent {
Dialog(
DialogWindow(
onCloseRequest = {},
){
Text(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ import kotlin.system.exitProcess
* example, from some event listener), use `GlobalScope.launchApplication` instead.
*
* Application can launch background tasks using [LaunchedEffect]
* or create [Window], [Dialog], or [Tray] in a declarative Compose way:
* or create [Window], [DialogWindow], or [Tray] in a declarative Compose way:
*
* ```
* fun main() = application {
Expand All @@ -86,15 +86,15 @@ import kotlin.system.exitProcess
*
* When there is no any active compositions, this function will end.
* Active composition is a composition that have active coroutine (for example, launched in
* [LaunchedEffect]) or that have child composition created inside [Window], [Dialog], or [Tray].
* [LaunchedEffect]) or that have child composition created inside [Window], [DialogWindow], or [Tray].
*
* Don't use any animation in this function
* (for example, [withFrameNanos] or [androidx.compose.animation.core.animateFloatAsState]),
* because underlying [MonotonicFrameClock] hasn't synchronized with any display, and produces
* frames as fast as possible.
*
* All animation's should be created inside Composable content of the
* [Window] / [Dialog] / [ComposePanel].
* [Window] / [DialogWindow] / [ComposePanel].
*
* @param exitProcessOnExit should `exitProcess(0)` be called after the application is closed.
* exitProcess speedup process exit (instant instead of 1-4sec).
Expand Down Expand Up @@ -156,7 +156,7 @@ fun CoroutineScope.launchApplication(
* An entry point for the Compose application.
*
* Application can launch background tasks using [LaunchedEffect]
* or create [Window], [Dialog], or [Tray] in a declarative Compose way:
* or create [Window], [DialogWindow], or [Tray] in a declarative Compose way:
*
* ```
* fun main() = runBlocking {
Expand All @@ -179,15 +179,15 @@ fun CoroutineScope.launchApplication(
*
* When there is no any active compositions, this function will end.
* Active composition is a composition that have active coroutine (for example, launched in
* [LaunchedEffect]) or that have child composition created inside [Window], [Dialog], or [Tray].
* [LaunchedEffect]) or that have child composition created inside [Window], [DialogWindow], or [Tray].
*
* Don't use any animation in this function
* (for example, [withFrameNanos] or [androidx.compose.animation.core.animateFloatAsState]),
* because underlying [MonotonicFrameClock] hasn't synchronized with any display, and produces
* frames as fast as possible.
*
* All animation's should be created inside Composable content of the
* [Window] / [Dialog] / [ComposePanel].
* [Window] / [DialogWindow] / [ComposePanel].
*/
suspend fun awaitApplication(
content: @Composable ApplicationScope.() -> Unit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ import java.awt.Window
*
* [AwtWindow] is needed for creating window's / dialog's that still can't be created with
* the default Compose functions [androidx.compose.ui.window.Window] or
* [androidx.compose.ui.window.Dialog].
* [androidx.compose.ui.window.DialogWindow].
*
* @param visible Is [Window] visible to user.
* Note that if we set `false` - native resources will not be released. They will be released
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,47 @@ import java.awt.event.WindowAdapter
import java.awt.event.WindowEvent
import javax.swing.JDialog

@Deprecated(
message = "Replaced by DialogWindow",
replaceWith = ReplaceWith("DialogWindow(onCloseRequest, state, visible, title, icon, undecorated, transparent, resizable, enabled, focusable, onPreviewKeyEvent, onKeyEvent, content)")
)
@Composable
fun Dialog(
onCloseRequest: () -> Unit,
state: DialogState = rememberDialogState(),
visible: Boolean = true,
title: String = "Untitled",
icon: Painter? = null,
undecorated: Boolean = false,
transparent: Boolean = false,
resizable: Boolean = true,
enabled: Boolean = true,
focusable: Boolean = true,
onPreviewKeyEvent: ((KeyEvent) -> Boolean) = { false },
onKeyEvent: ((KeyEvent) -> Boolean) = { false },
content: @Composable DialogWindowScope.() -> Unit
) = DialogWindow(
onCloseRequest,
state,
visible,
title,
icon,
undecorated,
transparent,
resizable,
enabled,
focusable,
onPreviewKeyEvent,
onKeyEvent,
content
)

/**
* Composes platform dialog in the current composition. When Dialog enters the composition,
* a new platform dialog will be created and receives the focus. When Dialog leaves the
* composition, dialog will be disposed and closed.
*
* Dialog is a modal window. It means it blocks the parent [Window] / [Dialog] in which composition
* Dialog is a modal window. It means it blocks the parent [Window] / [DialogWindow] in which composition
* context it was created.
*
* Usage:
Expand All @@ -72,11 +107,11 @@ import javax.swing.JDialog
* the native dialog will update its corresponding properties.
* If [DialogState.position] is not [WindowPosition.isSpecified], then after the first show on the
* screen [DialogState.position] will be set to the absolute values.
* @param visible Is [Dialog] visible to user.
* @param visible Is [DialogWindow] visible to user.
* If `false`:
* - internal state of [Dialog] is preserved and will be restored next time the dialog
* - internal state of [DialogWindow] is preserved and will be restored next time the dialog
* will be visible;
* - native resources will not be released. They will be released only when [Dialog]
* - native resources will not be released. They will be released only when [DialogWindow]
* will leave the composition.
* @param title Title in the titlebar of the dialog
* @param icon Icon in the titlebar of the window (for platforms which support this).
Expand All @@ -101,7 +136,7 @@ import javax.swing.JDialog
* @param content content of the dialog
*/
@Composable
fun Dialog(
fun DialogWindow(
onCloseRequest: () -> Unit,
state: DialogState = rememberDialogState(),
visible: Boolean = true,
Expand Down Expand Up @@ -150,8 +185,7 @@ fun Dialog(
}
}


Dialog(
DialogWindow(
visible = visible,
onPreviewKeyEvent = onPreviewKeyEvent,
onKeyEvent = onKeyEvent,
Expand Down Expand Up @@ -223,6 +257,29 @@ fun Dialog(
)
}

@Deprecated(
message = "Replaced by DialogWindow",
replaceWith = ReplaceWith("DialogWindow(visible, onPreviewKeyEvent, onKeyEvent, create, dispose, update, content)")
)
@Composable
fun Dialog(
visible: Boolean = true,
onPreviewKeyEvent: ((KeyEvent) -> Boolean) = { false },
onKeyEvent: ((KeyEvent) -> Boolean) = { false },
create: () -> ComposeDialog,
dispose: (ComposeDialog) -> Unit,
update: (ComposeDialog) -> Unit = {},
content: @Composable DialogWindowScope.() -> Unit
) = DialogWindow(
visible,
onPreviewKeyEvent,
onKeyEvent,
create,
dispose,
update,
content
)

// TODO(demin): fix mouse hover after opening a dialog.
// When we open a modal dialog, ComposeLayer/mouseExited will
// never be called for the parent window. See ./gradlew run3
Expand All @@ -234,7 +291,7 @@ fun Dialog(
* Once Dialog leaves the composition, [dispose] will be called to free resources that
* obtained by the [ComposeDialog].
*
* Dialog is a modal window. It means it blocks the parent [Window] / [Dialog] in which composition
* Dialog is a modal window. It means it blocks the parent [Window] / [DialogWindow] in which composition
* context it was created.
*
* The [update] block can be run multiple times (on the UI thread as well) due to recomposition,
Expand All @@ -243,13 +300,13 @@ fun Dialog(
* Note the block will also be ran once right after the [create] block completes.
*
* Dialog is needed for creating dialog's that still can't be created with
* the default Compose function [androidx.compose.ui.window.Dialog]
* the default Compose function [androidx.compose.ui.window.DialogWindow]
*
* @param visible Is [ComposeDialog] visible to user.
* If `false`:
* - internal state of [ComposeDialog] is preserved and will be restored next time the dialog
* will be visible;
* - native resources will not be released. They will be released only when [Dialog]
* - native resources will not be released. They will be released only when [DialogWindow]
* will leave the composition.
* @param onPreviewKeyEvent This callback is invoked when the user interacts with the hardware
* keyboard. It gives ancestors of a focused component the chance to intercept a [KeyEvent].
Expand All @@ -268,7 +325,7 @@ fun Dialog(
@OptIn(ExperimentalComposeUiApi::class)
@Suppress("unused")
@Composable
fun Dialog(
fun DialogWindow(
visible: Boolean = true,
onPreviewKeyEvent: ((KeyEvent) -> Boolean) = { false },
onKeyEvent: ((KeyEvent) -> Boolean) = { false },
Expand Down Expand Up @@ -312,12 +369,12 @@ fun Dialog(
}

/**
* Receiver scope which is used by [androidx.compose.ui.window.Dialog].
* Receiver scope which is used by [androidx.compose.ui.window.DialogWindow].
*/
@Stable
interface DialogWindowScope : WindowScope {
/**
* [ComposeDialog] that was created inside [androidx.compose.ui.window.Dialog].
* [ComposeDialog] that was created inside [androidx.compose.ui.window.DialogWindow].
*/
override val window: ComposeDialog
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ import java.awt.Window

/**
* Receiver scope which is used by [androidx.compose.ui.window.Window] and
* [androidx.compose.ui.window.Dialog].
* [androidx.compose.ui.window.DialogWindow].
*/
@Stable
interface WindowScope {
/**
* [Window] that was created inside [androidx.compose.ui.window.Window]
* or [androidx.compose.ui.window.Dialog]
* or [androidx.compose.ui.window.DialogWindow]
*/
val window: Window
}