# Windows

Open native desktop windows from Compose with nucleusApplication, Window, and DecoratedWindow, set their chrome, and route secondary windows through a host.

`nucleusApplication` starts the Nucleus runtime. `DecoratedWindow` opens a native window with a
Compose title bar slot, and `Window` opens one with the platform's own title bar. All three come
with `nucleus-application`.

## Open a window

```kotlin
fun main() = nucleusApplication {
    DecoratedWindow(
        onCloseRequest = ::exitApplication,
        state = rememberWindowState(size = DpSize(1024.dp, 720.dp)),
        title = "MyApp",
        minimumSize = DpSize(640.dp, 480.dp),
    ) {
        TitleBar { _ -> Text("MyApp", Modifier.align(Alignment.CenterHorizontally)) }
        Box(Modifier.fillMaxSize()) { /* app content */ }
    }
}
```

`TitleBar` drags the window from its empty space and receives the `DecoratedWindowState` (focus,
maximized, fullscreen). Call `DecoratedWindow` again to open a second window. For custom chrome,
macOS glass, or Windows 11 Mica, use [`WindowScaffold`](/docs/windows/window-scaffold). App-wide
options (single instance, locale, Dock policy, exit behavior) live on
[Application](/docs/lifecycle/application).

Nucleus has a single window backend: `NucleusBackend`, `LocalNucleusBackend`, and `backend =` are
gone. Compose Desktop's AWT `Window`, `DialogWindow`, and `Tray` don't work: use the Nucleus
`Window` and `DialogWindow` below, and the `tray` module. See
[Migrate from 2.x](/docs/migrate/from-2.x).

## Choose a window kind 

Nucleus has two kinds of window. Both are native, AWT-free windows with the same window handle,
lifecycle, and view models.

| | `Window` / `DialogWindow` | `DecoratedWindow` / `DecoratedDialog` |
| --- | --- | --- |
| Title bar and frame | Drawn by the platform | Drawn in Compose, in a `TitleBar` slot |
| Look | Native on each OS, follows system settings | Yours: custom chrome, toolkits, macOS glass, Mica |
| Parameters | Compose Desktop's, with the same defaults | Extra options: `minimizable`, `popupFor`, `hiddenFromDock`, `clickThrough`, and more |

`Window` is the Nucleus counterpart of Compose Desktop's `androidx.compose.ui.window.Window`, which
needs AWT. It takes the same parameters with the same defaults, so moving a call site over is an

```kotlin
import dev.nucleusframework.application.Window
import dev.nucleusframework.application.nucleusApplication

fun main() = nucleusApplication {
    Window(
        onCloseRequest = ::exitApplication,
        state = rememberWindowState(size = DpSize(1024.dp, 720.dp)),
        title = "MyApp",
    ) {
        Box(Modifier.fillMaxSize()) { /* app content */ }
    }
}
```

The platform draws the decorations:

- **macOS**: the standard AppKit title bar, with the traffic lights, native drag, and double-click.
- **Windows**: the Win32 caption and frame, with Snap Layouts, the Alt+Space menu, and the system
  colours.
- **Linux**: the window manager on X11, KWin's server-side decorations on Plasma, and GTK's own
  title bar on GNOME Wayland.

Nucleus draws no chrome in a `Window`. A `TitleBar` or `WindowScaffold` in its content is plain
content: it shows no window controls and reserves no room for them. `undecorated = true` gives a
fully borderless window. `nucleusWindow` works in the content as in any other window.

Choose `Window` for a standard app or a port from Compose Desktop. Choose `DecoratedWindow` when you
want your own title bar: a search field or tabs in the bar, a toolkit theme, or the materials of
[`WindowScaffold`](/docs/windows/window-scaffold).

The name is Compose's on purpose. A file that imports both `androidx.compose.ui.window.*` and
`dev.nucleusframework.application.*` must add `import dev.nucleusframework.application.Window`, or
the call is ambiguous.

## Lay out the title bar 

Inside `TitleBar`, `Modifier.align` puts each child at the start, the centre, or the end of the bar.
By default a centred child keeps its own width. Pass `TitleBarLayoutPolicy.FillCenter` to give it
the whole width left between the start and end items and the window controls, for a search field
or a tab strip:

```kotlin
TitleBar(layoutPolicy = TitleBarLayoutPolicy.FillCenter) { _ ->
    Text("MyApp", Modifier.align(Alignment.Start))
    QuickSearch(Modifier.align(Alignment.CenterHorizontally))
    AccountButton(Modifier.align(Alignment.End))
}
```

`FillCenter` accepts at most one centred child. `DialogTitleBar` and the Material and Jewel title
bars take the same `layoutPolicy` parameter.

![Default layout: the centre field keeps its own width](/assets/docs/windows/title-bar-layout-default.png)

![FillCenter: the centre field spans the free width of the title bar](/assets/docs/windows/title-bar-layout-fill-center.png)

## Set the window chrome 

`resizable`, `minimizable`, and `maximizable` default to `true` and react to changes.
`resizable = false` also removes the maximize button, so a close-only window needs two flags:

```kotlin
DecoratedWindow(onCloseRequest = ::exitApplication, resizable = false, minimizable = false) { /* ... */ }
```

On Linux, desktop shortcuts (Super+H, Alt+Space) still minimize and maximize. On macOS the traffic
lights grey out and the **Window** menu items follow. `DecoratedDialog` passes `false` for both.

`minimizable` and `maximizable` are new on every `DecoratedWindow` and `HostedWindow` overload and
on `NucleusWindowHost.Window`. A custom `NucleusWindowHost` must add them.

## Open a window from composition

`Window`, `DialogWindow`, `DecoratedWindow`, and `DecoratedDialog` also exist without a receiver.
Call them from a navigation destination or a row action:

```kotlin
@Composable
fun EditorWindow(onClose: () -> Unit) {
    DecoratedWindow(onCloseRequest = onClose, title = "Editor") { EditorContent() }
}
```

They read `LocalNucleusApplicationScope`, which throws outside `nucleusApplication { }`.

Material and Jewel windows and dialogs lost their receiver-less overloads. Call them inside
`with(LocalNucleusApplicationScope.current) { }`.

## Route windows through a host

A library or navigation layer calls `HostedWindow` / `HostedDialog`. They open through
`LocalNucleusWindowHost` / `LocalNucleusDialogHost`, so the app picks the window type once:

```kotlin
CompositionLocalProvider(LocalNucleusWindowHost provides MyMaterialWindowHost) {
    NavigationGraph() // every HostedWindow below uses this host
}
```

The default hosts draw no title bar: compose `TitleBar` in the content. To theme hosted windows,
implement the `NucleusWindowHost.Window` `fun interface`, and override the
[window API v2](/docs/windows/window-state) overload too: the default drops `maxSize` and
`requestScreen`. `transparent`, `clickThrough`, `visibleOnAllWorkspaces`, and `forceX11` exist only
on `DecoratedWindow`.

## Use the window handle

`nucleusWindow` in the content lambda, or `LocalNucleusWindow` deeper in the tree, returns the
`NucleusWindow` handle:

```kotlin
val focused by nucleusWindow.focusFlow.collectAsState()
Button(onClick = { nucleusWindow.setMaximized(true) }) { Text(if (focused) "Maximize" else "…") }
```

| Group | Members |
| --- | --- |
| State | `isFocused`, `isMinimized`, `isMaximized`, `isFullscreen`, and a `*Flow` for each |
| Geometry | `boundsOnScreen()`: outer bounds in dp, `null` before the window appears |
| Actions | `show`, `hide`, `toFront`, `requestFocus`, `setMinimized`, `setMaximized`, `setFullscreen`, `setAlwaysOnTop`, `setMinimumSize`, `setIcon`, `close` |
| Native | `unsafe.taoWindow`, `unsafe.taoHandle` (replace the removed `awtWindow` / `awtDialog`) |

## Hide a window from the taskbar or Dock

`hiddenFromDock = true` keeps a window visible but removes it from the taskbar, Alt+Tab, or Dock.

- **macOS**: the app loses its Dock icon and menu bar. The setting is app-wide: the last window to
  set it wins. For a menu-bar app, use `dockIconFollowsWindows = true` instead. See
  [Hide the Dock icon on macOS](/docs/os/tray/tray-app#hide-the-dock-icon-on-macos).
- **Linux**: native Wayland ignores it. Pass `forceX11 = true` or see
  [Linux and Wayland](/docs/windows/linux-wayland).

## Text input

IME composition reaches Compose text fields as preedit on every platform, and the candidate window
follows the caret. Keys the IME consumes don't reach the text field.

## View models and lifecycle

Each window and each dialog has its own view-model store, lifecycle and saved-state registry.
Inside its content, `LocalViewModelStoreOwner`, `LocalLifecycleOwner` and
`LocalSavedStateRegistryOwner` resolve to that window. `viewModel { }` (from
`org.jetbrains.androidx.lifecycle:lifecycle-viewmodel-compose`) returns one instance per window,
and the window's view models are cleared when it closes.

```kotlin
fun main() = nucleusApplication {
    for (document in documents) {
        key(document.id) {
            DecoratedWindow(onCloseRequest = { close(document) }) {
                val editor = viewModel { EditorViewModel(document) } // one per window
                Editor(editor)
            }
        }
    }
}
```

The window's lifecycle follows its state:

| Window | Lifecycle state |
| --- | --- |
| Not shown yet, or minimized | `CREATED` |
| Visible, without focus | `STARTED` |
| Focused | `RESUMED` |
| Closing, content still composed | `CREATED`, so `ON_PAUSE` and `ON_STOP` reach your observers |
| Closed | `DESTROYED`, and its view models are cleared |

- Popups use the lifecycle and store of the window that opens them.
- A `SavedStateHandle` lives in memory only: it doesn't survive an app restart.
- On native Wayland, the app can't tell that the compositor minimized a window, so the window stays
  `STARTED` or `RESUMED`. Only the focus change is seen.
- A satellite or a tab keeps its own view models when it moves between windows. See
  [Tool windows](/docs/tool-windows#shared-ideas).

## Handle uncaught exceptions

An exception that escapes a window event is fatal: Nucleus logs it, shows an error dialog, and
exits with code 1 (`exitProcessOnExit = false` rethrows instead). Skip the dialog in CI with
`-Dnucleus.tao.fatalErrorDialog=false`.

To keep a window alive, provide `LocalWindowExceptionHandlerFactory` (`@ExperimentalComposeUiApi`):

```kotlin
CompositionLocalProvider(
    LocalWindowExceptionHandlerFactory provides WindowExceptionHandlerFactory { _ ->
        WindowExceptionHandler { throwable -> /* return to drop the frame, rethrow to exit */ }
    },
) {
    DecoratedWindow(onCloseRequest = ::exitApplication) { MyContent() }
}
```

A failure inside composition can't resume: close and recreate the window, or rethrow.

## Notes

- On a regular JVM, macOS needs `-XstartOnFirstThread`. The Nucleus plugin adds it to Hot Reload
  tasks. A native image doesn't need it.
- `NucleusApplicationScope` extends `ApplicationScope`, but composables that start AWT don't work.

- Each window composes in its own scene. Nucleus bridges the parent composition locals into it.
- The exception handler covers layout, draw, input, IME, accessibility, and `nativePopupLayers`
  popups, and runs on the thread where the failure happened.
- On Linux, input methods resolve like GTK widgets: ibus or fcitx5 on X11, text-input-v3 on
  Wayland. On macOS, the OS decides whether a held letter repeats or opens the accent picker.

## API reference

### DecoratedWindow parameters

| Parameter | Default | Notes |
| --- | --- | --- |
| `onCloseRequest` | required | Close button, Alt+F4, Cmd+Q, and Dock > Quit. |
| `state` | `rememberWindowState()` | See [Window state](/docs/windows/window-state). |
| `visible` / `enabled` / `focusable` | `true` | |
| `title` / `icon` | `""` / `null` | |
| `resizable` / `minimizable` / `maximizable` | `true` | Reactive. |
| `alwaysOnTop` / `alwaysOnBottom` | `false` | Reactive. `alwaysOnBottom` clears `alwaysOnTop`. |
| `undecorated` | `false` | Borderless window with no controls. |
| `popupFor` | `null` | Linux: attach as a popup overlay of another window. |
| `nativePopupLayers` | `false` | See [Dialogs and popups](/docs/windows/dialogs-popups). |
| `nativeContextMenu` | `false` | See [Context menus](/docs/os/menus/context-menu). |
| `hiddenFromDock` | `false` | Hide from the taskbar or Dock. |
| `minimumSize` | `null` | The v2 overload uses `minSize` and `maxSize`. |
| `onPreviewKeyEvent` / `onKeyEvent` | `{ false }` | Return `true` to consume. |
| `transparent` / `forceX11` | `false` | Creation-time only. |
| `clickThrough` / `visibleOnAllWorkspaces` | `false` | Reactive. See [Overlay windows](/docs/windows/overlay-windows). |

### Window parameters

`Window` takes Compose Desktop's parameters with the same defaults: `onCloseRequest`, `state`,
`visible`, `title` (`"Untitled"`), `icon`, `undecorated`, `transparent`, `resizable`, `enabled`,
`focusable`, `alwaysOnTop`, `onPreviewKeyEvent`, and `onKeyEvent`. The
[window API v2](/docs/windows/window-state) overload adds `minSize` and `maxSize`.

### Title bar modifiers

- `Modifier.macOSLargeCornerRadius()`: macOS 26 large window corner radius.
- `Modifier.newFullscreenControls()`: in fullscreen, the bar slides in over the content on demand.
  On macOS it follows the menu bar down. On Windows and Linux, a mouse reveals it at the top edge.
  On a touch screen, a tap anywhere reveals it, a tap outside hides it, and it hides on its own
  after a few seconds.

## What's next

- [Window state](/docs/windows/window-state): size, position, screens, and the window API v2.
- [Window scaffold and chrome](/docs/windows/window-scaffold): full-window layouts and materials.
- [Dialogs and popups](/docs/windows/dialogs-popups): modal dialogs and native popup surfaces.
- [Toolkits](/docs/toolkits): Material and Jewel window themes.
