Skip to content

Commit

Permalink
Add an alwaysOnTop flag to DialogWindow.
Browse files Browse the repository at this point in the history
  • Loading branch information
m-sasha committed Feb 19, 2024
1 parent 40d79c4 commit eaf671a
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 1 deletion.
3 changes: 2 additions & 1 deletion compose/ui/ui/api/desktop/ui.api
Original file line number Diff line number Diff line change
Expand Up @@ -3898,7 +3898,8 @@ public abstract interface class androidx/compose/ui/window/DialogWindowScope : a
public final class androidx/compose/ui/window/Dialog_desktopKt {
public static final fun Dialog (Lkotlin/jvm/functions/Function0;Landroidx/compose/ui/window/DialogState;ZLjava/lang/String;Landroidx/compose/ui/graphics/painter/Painter;ZZZZZLkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function3;Landroidx/compose/runtime/Composer;III)V
public static final fun Dialog (ZLkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function0;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function3;Landroidx/compose/runtime/Composer;II)V
public static final fun DialogWindow (Lkotlin/jvm/functions/Function0;Landroidx/compose/ui/window/DialogState;ZLjava/lang/String;Landroidx/compose/ui/graphics/painter/Painter;ZZZZZLkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function3;Landroidx/compose/runtime/Composer;III)V
public static final synthetic fun DialogWindow (Lkotlin/jvm/functions/Function0;Landroidx/compose/ui/window/DialogState;ZLjava/lang/String;Landroidx/compose/ui/graphics/painter/Painter;ZZZZZLkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function3;Landroidx/compose/runtime/Composer;III)V
public static final fun DialogWindow (Lkotlin/jvm/functions/Function0;Landroidx/compose/ui/window/DialogState;ZLjava/lang/String;Landroidx/compose/ui/graphics/painter/Painter;ZZZZZZLkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function3;Landroidx/compose/runtime/Composer;III)V
public static final fun DialogWindow (ZLkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function0;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function3;Landroidx/compose/runtime/Composer;II)V
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,50 @@ fun Dialog(
resizable,
enabled,
focusable,
alwaysOnTop = false,
onPreviewKeyEvent,
onKeyEvent,
content
)

@Deprecated(
level = DeprecationLevel.HIDDEN,
message = "Replaced by an overload that also takes alwaysOnTop",
)
@Composable
fun DialogWindow(
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,
alwaysOnTop = false,
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
Expand Down Expand Up @@ -129,6 +168,7 @@ fun Dialog(
* changing [state])
* @param enabled Can dialog react to input events
* @param focusable Can dialog receive focus
* @param alwaysOnTop Should the dialog always be on top of another windows and dialogs
* @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].
* Return true to stop propagation of this event. If you return false, the key event will be
Expand All @@ -151,6 +191,7 @@ fun DialogWindow(
resizable: Boolean = true,
enabled: Boolean = true,
focusable: Boolean = true,
alwaysOnTop: Boolean = false,
onPreviewKeyEvent: ((KeyEvent) -> Boolean) = { false },
onKeyEvent: ((KeyEvent) -> Boolean) = { false },
content: @Composable DialogWindowScope.() -> Unit
Expand All @@ -165,6 +206,7 @@ fun DialogWindow(
val currentResizable by rememberUpdatedState(resizable)
val currentEnabled by rememberUpdatedState(enabled)
val currentFocusable by rememberUpdatedState(focusable)
val currentAlwaysOnTop by rememberUpdatedState(alwaysOnTop)
val currentOnCloseRequest by rememberUpdatedState(onCloseRequest)

val updater = remember(::ComponentUpdater)
Expand Down Expand Up @@ -243,6 +285,7 @@ fun DialogWindow(
set(currentResizable, dialog::setResizable)
set(currentEnabled, dialog::setEnabled)
set(currentFocusable, dialog::setFocusableWindowState)
set(currentAlwaysOnTop, dialog::setAlwaysOnTop)
}
if (state.size != appliedState.size) {
dialog.setSizeSafely(state.size, WindowPlacement.Floating)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.awt.ComposeDialog
import androidx.compose.ui.awt.ComposeWindow
import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.focus.focusRequester
import androidx.compose.ui.focus.focusTarget
Expand Down Expand Up @@ -642,4 +643,28 @@ class DialogWindowTest {
assertThat(renderedText).isEqualTo("2")
}
}

@Test
fun `change alwaysOnTop`() = runApplicationTest {
var dialog: ComposeDialog? = null

var alwaysOnTop by mutableStateOf(false)

launchTestApplication {
DialogWindow(onCloseRequest = ::exitApplication, alwaysOnTop = alwaysOnTop) {
dialog = this.window
Box(Modifier.size(32.dp).background(Color.Red))
}
}

awaitIdle()
assertThat(dialog?.isAlwaysOnTop).isFalse()

alwaysOnTop = true
awaitIdle()
assertThat(dialog?.isAlwaysOnTop).isTrue()

dialog?.dispatchEvent(WindowEvent(dialog, WindowEvent.WINDOW_CLOSING))
}

}

0 comments on commit eaf671a

Please sign in to comment.