Nucleus
Windows

Windows

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

View as Markdown

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

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. App-wide options (single instance, locale, Dock policy, exit behavior) live on Application.

Changed in 3.0

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.

Choose a window kindNew

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

Window / DialogWindowDecoratedWindow / DecoratedDialog
Title bar and frameDrawn by the platformDrawn in Compose, in a TitleBar slot
LookNative on each OS, follows system settingsYours: custom chrome, toolkits, macOS glass, Mica
ParametersCompose Desktop's, with the same defaultsExtra 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 import change:

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.

Import it explicitly

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 barNew

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:

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

FillCenter: the centre field spans the free width of the title bar

Set the window chromeNew

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:

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.

Changed in 3.0

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:

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

They read LocalNucleusApplicationScope, which throws outside nucleusApplication { }.

Changed in 3.0

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:

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 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:

val focused by nucleusWindow.focusFlow.collectAsState()
Button(onClick = { nucleusWindow.setMaximized(true) }) { Text(if (focused) "Maximize" else "…") }
GroupMembers
StateisFocused, isMinimized, isMaximized, isFullscreen, and a *Flow for each
GeometryboundsOnScreen(): outer bounds in dp, null before the window appears
Actionsshow, hide, toFront, requestFocus, setMinimized, setMaximized, setFullscreen, setAlwaysOnTop, setMinimumSize, setIcon, close
Nativeunsafe.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.
  • Linux: native Wayland ignores it. Pass forceX11 = true or see Linux and 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.

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:

WindowLifecycle state
Not shown yet, or minimizedCREATED
Visible, without focusSTARTED
FocusedRESUMED
Closing, content still composedCREATED, so ON_PAUSE and ON_STOP reach your observers
ClosedDESTROYED, 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.

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):

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.

API reference

DecoratedWindow parameters

ParameterDefaultNotes
onCloseRequestrequiredClose button, Alt+F4, Cmd+Q, and Dock > Quit.
staterememberWindowState()See Window state.
visible / enabled / focusabletrue
title / icon"" / null
resizable / minimizable / maximizabletrueReactive.
alwaysOnTop / alwaysOnBottomfalseReactive. alwaysOnBottom clears alwaysOnTop.
undecoratedfalseBorderless window with no controls.
popupFornullLinux: attach as a popup overlay of another window.
nativePopupLayersfalseSee Dialogs and popups.
nativeContextMenufalseSee Context menus.
hiddenFromDockfalseHide from the taskbar or Dock.
minimumSizenullThe v2 overload uses minSize and maxSize.
onPreviewKeyEvent / onKeyEvent{ false }Return true to consume.
transparent / forceX11falseCreation-time only.
clickThrough / visibleOnAllWorkspacesfalseReactive. See 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 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

Was this page helpful?
Edit on GitHub

Last updated on