Nucleus
Native content

Native views

Embed an NSView, HWND or GtkWidget inside a Compose layout with NativeView.

View as Markdown

NativeView embeds a platform-native view in a Compose layout, the desktop counterpart of AndroidView and UIKitView. You hand it an NSView*, HWND or GtkWidget*, and Nucleus keeps it on its layout slot until the composable leaves. It comes with nucleus-application.

Embed a native view

Return a NucleusPlatformView from factory:

@Composable
fun BrowserPane(url: String) {
    NativeView(
        factory = {
            // createNativeWebView is your own interop code returning an NSView* as a Long.
            val handle = createNativeWebView(url)
            nucleusNsPlatformView(
                handle = { handle },
                onDispose = { releaseNativeWebView(handle) },
            )
        },
        modifier = Modifier.fillMaxSize(),
    )
}

You create the native view; factory runs once. The view follows its slot through layout changes, scrolling and resizes. Use nucleusHwndPlatformView on Windows and nucleusGtkPlatformView on Linux. On another OS, NativeView renders an empty Box(modifier), so one call site per platform is enough. You can also implement NucleusPlatformView.NsView, HWnd or GtkWidget on your own class.

On Linux, NativeView requires Wayland. Under X11 the widget stays hidden behind the window surface and its rectangle shows the desktop.

Release the view

dispose(), or the factory's onDispose, runs after the host has detached the handle. Free the native view there. On Linux, hold your own reference to the widget (g_object_ref_sink) while you use the handle and drop it in dispose(), or GTK finalizes it on detach.

Overlay Compose content

The content slot draws Compose UI on top of the native view:

NativeView(
    factory = { /* ... */ },
    modifier = Modifier.fillMaxSize(),
) {
    // The button handles its own clicks; clicks elsewhere reach the native view.
    Button(onClick = { /* reload */ }) { Text("Reload") }
}

Compose hit-tests first: anything drawn over the view keeps its pointer events. Events Compose does not consume go to the native view, trackpad pans included. Popups draw above the view.

Clip to rounded corners

Modifier.clip() does not reach native views. Pass cornerRadius = 12.dp instead; Dp.Infinity gives a circle. cornerRadius has no effect on Linux: draw a rounded border in content.

Place the view above Compose on macOS

AppKit controls that run their own mouse tracking, such as NSButton or NSTextField, need the events AppKit delivers itself. Pass placedAsOverlay = true:

NativeView(
    factory = { nucleusNsPlatformView(handle = { buttonHandle }) },
    placedAsOverlay = true,
)
  • The view sits above the Compose surface. No hole is punched, the window does not switch to transparency, and no pointer event is replayed: AppKit handles hover, press, drag and click.
  • Compose cannot draw over the view, neither in content nor in later siblings or in-scene popups.
  • The view is clipped to the bounds of its clipping ancestors (clipToBounds, scrolling containers), but not to their shapes.
  • Nucleus reads the flag when the view is first composed. Windows and Linux ignore it.

API reference

NativeView parameterDefaultNotes
factoryrequired() -> NucleusPlatformView, called once
modifierModifierSizes and places the slot
update{}Runs after every recomposition. Push new state into the view here
cornerRadiusDp.UnspecifiedRounded clip; unspecified applies none
placedAsOverlayfalsemacOS only. Mounts the view above the Compose surface; read once
content{}Compose overlay drawn over the view

The factories take a handle: () -> Long lambda, read each time the host needs the pointer, and optional callbacks:

MemberFactory callbackCalled when
resize(widthPx, heightPx)onResizeThe view's size changes.
setBounds(xPx, yPx, widthPx, heightPx)onSetBoundsThe frame changes. For views that draw outside a window rect, such as WebView2.
setCornerRadius(radiusPx)onSetCornerRadiusThe rounded clip changes.
clearFocus()onClearFocusThe window or a Compose layer takes focus.
dispose()onDisposeAfter detach. Release your native resources.

On Windows, a hwndHandle of 0L is valid for a view with no child window, such as WebView2 in composition mode; it drives its visual through setBounds and setCornerRadius.

Modifier.consumeOverlayPointerEvents() is deprecated and does nothing: use Modifier.pointerHoverIcon for its cursor argument.

What's next

  • WebView: a ready-made WebView built on NativeView.
  • TextureView: show GPU frames from an external producer.
  • Pointer icons: cursors for the Compose overlay.
Was this page helpful?
Edit on GitHub

Last updated on