Nucleus
Windows

Window stateNew

Control and observe window size, position, placement, and screen with Compose's WindowState or the AWT-free window API v2 in dev.nucleusframework.window.tao.v2.

View as Markdown

A state object sets and reports a window's size, position, placement, and screen. DecoratedWindow accepts Compose's WindowState (v1, the default) and the window API v2 in dev.nucleusframework.window.tao.v2, which also moves windows between screens.

Use the v1 state

val state = rememberWindowState(
    size = DpSize(1100.dp, 720.dp),
    position = WindowPosition.Aligned(Alignment.Center),
)

DecoratedWindow(onCloseRequest = ::exitApplication, state = state) {
    Button(onClick = { state.placement = WindowPlacement.Maximized }) { Text("Maximize") }
}

size, position, placement, and isMinimized are mutable: write them to drive the window, read them to follow the user. Dialogs take rememberDialogState() the same way.

Size a window to its content

Set an axis to Dp.Unspecified and the window measures its content once, then takes that size:

val state = rememberWindowState(size = DpSize(Dp.Unspecified, Dp.Unspecified))

A dialog with a parent centres on it, and one without centres on the screen. Later content changes don't resize the window.

The window stays hidden until it has its measured size and position, so it never shows at a fallback size first. Content that never measures to a positive size appears at the fallback size after a short wait.

Use the window API v2

The v2 state replaces mutable properties with requests and adds screen selection:

import dev.nucleusframework.window.tao.v2.*

@OptIn(ExperimentalComposeUiApi::class)
fun main() = nucleusApplication {
    val state = rememberWindowState(
        initialScreenProvider = WindowScreenProvider.Primary,
        initialBoundsProvider = WindowBoundsProvider(
            sizeProvider = WindowSizeProvider.Fixed(1200.dp, 800.dp),
            positionProvider = WindowPositionProvider.CenteredInScreenBounds,
        ),
    )

    DecoratedWindow(onCloseRequest = ::exitApplication, state = state) {
        Button(onClick = { state.requestSize(900.dp, 600.dp) }) { Text("Resize") }
    }
}

The package mirrors Compose 1.13's androidx.compose.ui.window.v2 member for member. DecoratedWindow, DecoratedDialog, HostedWindow, and HostedDialog each have a v2 overload that replaces minimumSize with minSize and maxSize (inner sizes). The Decorated* v2 overloads need @OptIn(ExperimentalComposeUiApi::class).

Changed in 3.0

Nucleus does not accept Compose's androidx.compose.ui.window.v2 types: they depend on AWT. Change the import to dev.nucleusframework.window.tao.v2. See Migrate from 2.x.

Request geometry changes

RequestEffect
requestSize(...)Resize.
requestPosition(...)Move.
requestBounds(...)Resize and move together.
requestPlacement(placement)Floating, Maximized, or Fullscreen.
requestMinimized(value)Minimize or restore.
requestScreen(provider)Move to another screen.

Size, position, and bounds requests also take a provider. The window applies requests asynchronously: bounds requests in order, and only the latest screen, placement, or minimized request. A bounds request restores a maximized or fullscreen window first.

Read the observed state

The window writes bounds (decorations included), position, size, screenId, placement, and isMinimized back to the state. They throw until the window has been visible once:

val label = if (state.isInitialized) "${state.size} on ${state.screenId}" else "not shown yet"

rememberSaveable restores the last screen, placement, and bounds through WindowState.Saver.

Move a window to another screen

state.requestScreen { screens.last() }
state.requestScreen(WindowScreenProvider.ById(savedScreenId))

The scope lists screens, defaultScreen (the current one), and primaryScreen. The window keeps its relative position and stays fully visible. ById falls back to the default screen while that display is unplugged. A Screen exposes id, name, bounds, availableBounds (minus taskbar, menu bar, and docks), insets, and isPrimary, in dp.

To list displays outside a provider, call TaoMonitors.all(), primary(), forWindow(taoWindow), or byId(id) (null when unplugged). Their geometry is in physical pixels: convert with boundsDp(scale) using the scale of the window you position.

Size and place with providers

WindowSizeProviderSize
Default800 × 600 dp.
CurrentKeeps the current size.
Fixed(width, height)The given size.
UnconstrainedContent size, capped at the screen's available size.
PreferredWidth(height) / PreferredHeight(width)Content size on one axis.
WindowPositionProviderPosition
DefaultThe window manager places the window.
CurrentKeeps the current position.
CenteredInScreenBounds(offset)Centre of the screen's full bounds, taskbars and docks included.
CenteredInParentWindowCentre of the parent.
Absolute(x, y)The given point.
AlignedToScreenAvailableBounds(alignment, offset)Aligned inside the screen's available bounds.
AlignedToParentWindow(anchor, alignment, offset, excludeParentInsets)Aligned around the parent.

The parent-window providers throw when the window has no parent. WindowBoundsProvider(size, position) combines the two. Prefer it over WindowBoundsProvider.Absolute(rect) when either side is Default or content-sized: a DpRect can't express those.

A custom provider is a lambda with windowMetrics, parentWindowMetrics, contentToWindowSize, and measureWindowContent:

state.requestBounds {
    val work = windowMetrics.screen.availableBounds
    DpRect(work.left, work.top, work.left + (work.right - work.left) / 2, work.bottom)
}

Use a dialog state

DialogState has the screen and bounds requests of WindowState, without placement or minimize. Create it with rememberDialogState(initialScreenProvider, initialBoundsProvider) or rememberDialogStateWithBounds(initialPosition, initialSize), which centres on the screen by default. rememberWindowStateWithBounds is the window counterpart. See Dialogs and popups.

Known limits

  • X11 window managers apply their own policy to the initial position. Openbox places it at 0,0.
  • A themed NucleusWindowHost without the v2 overload drops maxSize. See Route windows through a host.

What's next

Was this page helpful?
Edit on GitHub

Last updated on