New Compose Multiplatform components arrived on Composables UICheck it out →

Component in Compose Ui

Window

Desktop

Composes platform window in the current composition. When Window enters the composition, a new platform window will be created and receives the focus. When Window leaves the composition, window will be disposed and closed.

Initial size of the window is controlled by [WindowState.size]. Initial position of the window is controlled by [WindowState.position].

Usage in single-window application ([ApplicationScope.exitApplication] will close all the windows and stop all effects defined in [application]):

fun main() = application {
    Window(onCloseRequest = ::exitApplication)
}

Last updated:

Installation

dependencies {
   implementation("androidx.compose.ui:ui:1.7.0-beta04")
}

Overloads

@Composable
fun Window(
    onCloseRequest: () -> Unit,
    state: WindowState = rememberWindowState(),
    visible: Boolean = true,
    title: String = "Untitled",
    icon: Painter? = null,
    undecorated: Boolean = false,
    transparent: Boolean = false,
    resizable: Boolean = true,
    enabled: Boolean = true,
    focusable: Boolean = true,
    alwaysOnTop: Boolean = false,
    onPreviewKeyEvent: (KeyEvent) -> Boolean = { false },
    onKeyEvent: (KeyEvent) -> Boolean = { false },
    content: @Composable FrameWindowScope.() -> Unit
)

Parameters

namedescription
onCloseRequestCallback that will be called when the user closes the window. Usually in this callback we need to manually tell Compose what to do: - change isOpen state of the window (which is manually defined) - close the whole application (onCloseRequest = ::exitApplication in [ApplicationScope]) - don't close the window on close request (onCloseRequest = \{\})
stateThe state object to be used to control or observe the window's state When size/position/status is changed by the user, state will be updated. When size/position/status of the window is changed by the application (changing state), the native window will update its corresponding properties. If application changes, for example [WindowState.placement], then after the next recomposition, [WindowState.size] will be changed to correspond the real size of the window. If [WindowState.position] is not [WindowPosition.isSpecified], then after the first show on the screen [WindowState.position] will be set to the absolute values.
visibleIs [Window] visible to user. If false: - internal state of [Window] is preserved and will be restored next time the window will be visible; - native resources will not be released. They will be released only when [Window] will leave the composition.
titleTitle in the titlebar of the window
iconIcon in the titlebar of the window (for platforms which support this). On macOs individual windows can't have a separate icon. To change the icon in the Dock, set it via iconFile in build.gradle (https://github.com/JetBrains/compose-jb/tree/master/tutorials/Native_distributions_and_local_execution#platform-specific-options)
undecoratedDisables or enables decorations for this window.
transparentDisables or enables window transparency. Transparency should be set only if window is undecorated, otherwise an exception will be thrown.
resizableCan window be resized by the user (application still can resize the window changing [state])
enabledCan window react to input events
focusableCan window receive focus
alwaysOnTopShould window always be on top of another windows
onPreviewKeyEventThis 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 sent to this [onPreviewKeyEvent]'s child. If none of the children consume the event, it will be sent back up to the root using the onKeyEvent callback.
onKeyEventThis callback is invoked when the user interacts with the hardware keyboard. While implementing this callback, return true to stop propagation of this event. If you return false, the key event will be sent to this [onKeyEvent]'s parent.
contentContent of the window
@Composable
@Suppress("unused"
@OptIn(ExperimentalComposeUiApi::class
fun Window(
    visible: Boolean = true,
    onPreviewKeyEvent: (KeyEvent) -> Boolean = { false },
    onKeyEvent: (KeyEvent) -> Boolean = { false },
    create: () -> ComposeWindow,
    dispose: (ComposeWindow) -> Unit,
    update: (ComposeWindow) -> Unit = {},
    content: @Composable FrameWindowScope.() -> Unit
)

Parameters

namedescription
visibleIs [ComposeWindow] visible to user. If false: - internal state of [ComposeWindow] is preserved and will be restored next time the window will be visible; - native resources will not be released. They will be released only when [Window] will leave the composition.
onPreviewKeyEventThis 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 sent to this [onPreviewKeyEvent]'s child. If none of the children consume the event, it will be sent back up to the root using the onKeyEvent callback.
onKeyEventThis callback is invoked when the user interacts with the hardware keyboard. While implementing this callback, return true to stop propagation of this event. If you return false, the key event will be sent to this [onKeyEvent]'s parent.
createThe block creating the [ComposeWindow] to be composed.
disposeThe block to dispose [ComposeWindow] and free native resources. Usually it is simple ComposeWindow::dispose
updateThe callback to be invoked after the layout is inflated.
contentComposable content of the creating window.