← Compose Multiplatform 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
name | description |
---|---|
onCloseRequest | Callback 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 = \{\} ) |
state | The 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. |
visible | Is [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. |
title | Title in the titlebar of the window |
icon | Icon 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) |
undecorated | Disables or enables decorations for this window. |
transparent | Disables or enables window transparency. Transparency should be set only if window is undecorated, otherwise an exception will be thrown. |
resizable | Can window be resized by the user (application still can resize the window changing [state]) |
enabled | Can window react to input events |
focusable | Can window receive focus |
alwaysOnTop | Should window always be on top of another windows |
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 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. |
onKeyEvent | This 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. |
content | Content 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
name | description |
---|---|
visible | Is [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. |
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 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. |
onKeyEvent | This 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. |
create | The block creating the [ComposeWindow] to be composed. |
dispose | The block to dispose [ComposeWindow] and free native resources. Usually it is simple ComposeWindow::dispose |
update | The callback to be invoked after the layout is inflated. |
content | Composable content of the creating window. |