# System tray

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

`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.

## Add the dependency

```kotlin title="build.gradle.kts"
implementation("dev.nucleusframework:nucleus.tray:3.0.0")
```

`tray` holds `Tray` and the [menu DSL](/docs/os/tray/menu). 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`](/docs/os/tray/tray-app) instead.

## Add a tray icon

```kotlin
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.

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](/docs/migrate/from-2.x).

![A tray icon and its menu in the macOS menu bar](/assets/docs/os/tray-menu-macos.png)

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

```kotlin
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.

- Compose renders the icon offscreen when its state changes and writes it to a file for the
  native tray API. Identical renders share one file, and the cache keeps at most 64 files. Raise
  the cap with `-Dnucleus.tray.iconCacheSize=<n>`.
- The library records the menu items during composition and rebuilds the native menu when they
  change. A change that keeps the same icon skips the render. It does not use AWT.
- macOS uses an `NSStatusItem`, with light and dark variants of an untinted vector rendered ahead
  of time. Windows uses `Shell_NotifyIcon`. Linux uses a StatusNotifierItem with a DBusMenu menu.

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

- [Tray menu DSL](/docs/os/tray/menu): items, checkable items, submenus, shortcuts, and reactive menus.
- [Tray apps](/docs/os/tray/tray-app): open a Compose popup from the tray icon.
- [Application entry point](/docs/lifecycle/application): `nucleusApplication` and `exitApplication`.
