Nucleus
OS integrationSystem tray

System tray

Draw a tray icon and its native menu from Compose on macOS, Windows, and Linux without AWT.

View as Markdown

Tray draws a system tray icon and its native menu from Compose on macOS, Windows, and Linux. The icon and the menu update when the state they read changes.

traymacOSWindowsLinuxGradle plugin: Not neededWindow runtime: Not needed

Add the dependency

build.gradle.kts
implementation("dev.nucleusframework:nucleus.tray:3.0.0")

tray holds Tray and the menu DSL. It needs no window backend and does not depend on nucleus-application, so a daemon or an app without Nucleus windows can use it. For a popup anchored to the icon, add tray-app instead.

Add a tray icon

import dev.nucleusframework.tray.Tray

fun main() = nucleusApplication {
    Tray(
        icon = Icons.Default.Favorite,
        tooltip = "My App",
        primaryAction = { showWindow() },
        onMenuOpened = { refreshStatus() },
    ) {
        Item(label = "Show window") { showWindow() }
        Divider()
        Item(label = "Quit") { exitApplication() }
    }
}

The icon stays while Tray is in the composition, so wrap it in an if to hide it. Pass menuContent = null for an icon without a menu.

Changed in 3.0

Compose Desktop's androidx.compose.ui.window.Tray relies on AWT and does not work under nucleusApplication. Use dev.nucleusframework.tray.Tray. The tray used to ship from the separate composenativetray library; it is now part of Nucleus and shares its version. See Migrate from 2.x.

A tray icon and its menu in the macOS menu bar

Handle clicks

primaryAction runs on a left click on macOS and Windows. On Linux, KDE fires it on a single click and GNOME on a double click. Without primaryAction, a click opens the menu. onMenuOpened runs each time the menu opens, so you can refresh data first.

Both callbacks always run the latest lambdas you passed, so they see current state. A new lambda on each recomposition does not rebuild the tray.

Set the icon

icon accepts an ImageVector (with an optional tint), a Painter, or a DrawableResource. iconContent takes any composable, such as a badge:

Tray(
    iconContent = {
        Box(Modifier.size(24.dp).background(Color.Red, CircleShape)) {
            Text("3", color = Color.White, modifier = Modifier.align(Alignment.Center))
        }
    },
    tooltip = "3 notifications",
) { /* … */ }
  • Without tint, an ImageVector renders white on a dark tray and black on a light one.
  • For a hand-drawn .ico on Windows, pass windowsIcon and macLinuxIcon instead of icon.
  • iconRenderProperties sets the render sizes. The default targets 44 px on macOS, 32 px on Windows, and 24 px on Linux.
  • Stock GNOME shows the icon only with the AppIndicator extension. KDE Plasma and Ubuntu show it by default.

Notes

  • Each Tray is independent: several trays can live in one app, and disposing one leaves the others and their menus untouched.
  • Under GNOME, the AppIndicator extension opens the menu on a single click and calls primaryAction on a double click. With no menu, a single click does nothing.
  • On Linux the tray registers as a StatusNotifierItem. When its usual bus name is taken, as can happen in a Flatpak sandbox, it registers under its connection's unique name instead.

What's next

Was this page helpful?
Edit on GitHub

Last updated on