# Migrate from ComposeNativeTray

Move an app from the ComposeNativeTray library to the Nucleus tray and tray-app modules. Swap the Maven coordinates and the imports, and review the few API and behavior changes.

Move an app from the ComposeNativeTray library (`composenativetray` and `composenativetray-app`)
to the Nucleus `tray` and `tray-app` modules. For most apps it is a dependency swap and a
search-and-replace of the imports: the `Tray` composables, the menu DSL and `TrayApp` keep their
signatures.

Its code moved into the Nucleus repository. Bug fixes and new tray features land in Nucleus, in
the `tray` and `tray-app` modules, under the Nucleus version.

## Why the code moved

ComposeNativeTray already depended on Nucleus: `composenativetray` on `core-runtime` and
`darkmode-detector`, and `TrayApp` on `nucleus-application` and the window runtime. Each Nucleus
release meant a matching tray release, from a second repository with its own CI.

In the Nucleus repository, the tray, the windows, the packaging and the other modules are built,
tested and released together:

- One version. `nucleus.tray` and `nucleus.tray-app` always match the `nucleus-application` and
  window runtime you use.
- One CI pipeline. Each pull request runs the Linux tray torture suites (seeded menu fuzzing,
  many trays at once, panel restarts, disposal races, leaked threads and bus names) and the
  `TrayApp` end-to-end tests against a real Nucleus application under a headless X server. The
  same kind of suites cover Windows and macOS (`./gradlew :tray:test` on those systems), and a
  local suite checks the tray against GNOME's real AppIndicator host in a headless GNOME Shell.
- The same rules as the rest of Nucleus: JNI only, `java.util.logging`, GraalVM metadata in the
  JAR, and a public API checked for binary compatibility.

These suites found and fixed bugs in the old backends, listed in [Behavior changes](#behavior-changes).

## You don't need the whole framework

Nucleus is modular: each module is a separate artifact with its own small set of dependencies.
`nucleus.tray` brings only `core-runtime` and `darkmode-detector`, not the window runtime and not
`nucleusApplication`: a Compose Desktop app keeps its own `application { }` and its own windows and
adds a tray, nothing more. Only `TrayApp` asks for more, because its popup is a window: it runs in
a `nucleusApplication`.

## Before you start

- Find your ComposeNativeTray version. This page covers 2.1.x, the last line. The import and
  `TrayApp` tables also list what changed from 2.0.x and 1.x.
- Use Compose Multiplatform 1.13 or later and JDK 17 or later, as Nucleus 3.0 requires.
- If you use `TrayApp`, your app moves to Nucleus 3.0 at the same time, since `tray-app` brings
  `nucleus-application` 3.0. Read [Migrate from Nucleus 2.x](/docs/migrate/from-2.x) for the
  application and window changes. A JetBrains Compose Desktop app reads
  [Migrate from JetBrains Compose Desktop](/docs/migrate/from-jb-compose).

## Replace the dependency

| ComposeNativeTray | Nucleus |
| --- | --- |
| `dev.nucleusframework:composenativetray` (2.x) | `dev.nucleusframework:nucleus.tray` |
| `dev.nucleusframework:composenativetray-app` (2.1.x) | `dev.nucleusframework:nucleus.tray-app` |
| `io.github.kdroidfilter:composenativetray` (1.x) | `dev.nucleusframework:nucleus.tray`, plus `nucleus.tray-app` for `TrayApp` |

```diff title="build.gradle.kts"
 dependencies {
-    implementation("dev.nucleusframework:composenativetray:<tray-version>")
-    implementation("dev.nucleusframework:composenativetray-app:<tray-version>")
+    implementation("dev.nucleusframework:nucleus.tray:3.0.0")
+    // Only if you use TrayApp
+    implementation("dev.nucleusframework:nucleus.tray-app:3.0.0")
 }
```

`<version>` is the Nucleus version, 3.0.0 or later. `tray-app` brings `tray` and
`nucleus-application` as API dependencies, so `tray-app` alone is enough for an app with a
`TrayApp`.

`tray` depends on neither `nucleus-application` nor the window runtime, so a daemon or an app
without Nucleus windows can use it. Inside a Nucleus app, call `Tray` from `nucleusApplication { }`:
Compose Desktop's AWT `Window` and `Tray` do not work there.

## Replace the imports

The `.api` and `.impl` segments are gone, `composenativetray` becomes `tray`, and `trayapp`
becomes `tray.app`:

| ComposeNativeTray | Nucleus |
| --- | --- |
| `dev.nucleusframework.composenativetray.tray.api.Tray` | `dev.nucleusframework.tray.Tray` |
| `dev.nucleusframework.composenativetray.menu.api.*` (`TrayMenuBuilder`, `ComposableTrayMenuScope`, `KeyShortcut`, `Key`) | `dev.nucleusframework.tray.menu.*` |
| `dev.nucleusframework.composenativetray.utils.*` (`IconRenderProperties`, `isMenuBarInDarkMode`) | `dev.nucleusframework.tray.utils.*` |
| `dev.nucleusframework.composenativetray.trayapp.*` (`TrayApp`, `TrayAppState`, `rememberTrayAppState`, `TrayWindowDismissMode`, `TrayPosition`, `getTrayPosition`, `getTrayWindowPosition`) | `dev.nucleusframework.tray.app.*` |
| 2.0.x: `…composenativetray.tray.api.TrayApp`, `TrayAppState`, `TrayWindowDismissMode` | `dev.nucleusframework.tray.app.*` |
| 2.0.x: `…composenativetray.utils.TrayPosition`, `getTrayPosition`, `getTrayWindowPosition` | `dev.nucleusframework.tray.app.*` |
| 1.x: `com.kdroid.composetray.tray.api.Tray` | `dev.nucleusframework.tray.Tray` |
| 1.x: `com.kdroid.composetray.tray.api.TrayApp`, `TrayAppState`, `TrayWindowDismissMode` | `dev.nucleusframework.tray.app.*` |
| 1.x: `com.kdroid.composetray.menu.api.*` | `dev.nucleusframework.tray.menu.*` |
| 1.x: `com.kdroid.composetray.utils.TrayPosition`, `getTrayPosition`, `getTrayWindowPosition` | `dev.nucleusframework.tray.app.*` |
| 1.x: `com.kdroid.composetray.utils.IconRenderProperties`, `isMenuBarInDarkMode` | `dev.nucleusframework.tray.utils.*` |

```diff
-import dev.nucleusframework.composenativetray.tray.api.Tray
-import dev.nucleusframework.composenativetray.menu.api.KeyShortcut
-import dev.nucleusframework.composenativetray.utils.isMenuBarInDarkMode
-import dev.nucleusframework.composenativetray.trayapp.TrayApp
-import dev.nucleusframework.composenativetray.trayapp.rememberTrayAppState
+import dev.nucleusframework.tray.Tray
+import dev.nucleusframework.tray.menu.KeyShortcut
+import dev.nucleusframework.tray.utils.isMenuBarInDarkMode
+import dev.nucleusframework.tray.app.TrayApp
+import dev.nucleusframework.tray.app.rememberTrayAppState
```

## Port the code

Coming from 2.1.x, the code itself does not change. Every `Tray` overload keeps its parameters:
`icon` as an `ImageVector` (with `tint`), a `Painter` or a `DrawableResource`, `iconContent`,
`windowsIcon` with `macLinuxIcon`, then `iconRenderProperties`, `tooltip`, `primaryAction`,
`onMenuOpened` and the menu. `Item`, `CheckableItem`, `SubMenu`, `Divider` and `KeyShortcut`
take the same arguments.

### A tray with a menu

```diff
-import dev.nucleusframework.composenativetray.tray.api.Tray
+import dev.nucleusframework.tray.Tray

 fun main() = nucleusApplication {
     Tray(
         icon = Icons.Default.Favorite,
         tooltip = "My App",
         primaryAction = { showWindow() },
     ) {
         Item(label = "Settings") { openSettings() }
         Divider()
         Item(label = "Quit") { exitApplication() }
     }
 }
```

### Checkable items and submenus

The menu lambda is `@Composable`: it reads state, and the native menu follows each change.

```kotlin
var notifications by remember { mutableStateOf(true) }
var advanced by remember { mutableStateOf(false) }

Tray(icon = Icons.Default.Settings, tooltip = "Settings") {
    CheckableItem(
        label = "Notifications",
        checked = notifications,
        onCheckedChange = { notifications = it },
    )
    SubMenu(label = "Options") {
        Item(label = "Option 1") { }
        SubMenu(label = "Advanced") {
            CheckableItem(label = "Verbose", checked = advanced, onCheckedChange = { advanced = it })
        }
    }
}
```

Only the imports change. Two checkable items may now share a label: each one toggles alone.

### Icon and tooltip updates

Pass state to `icon` and `tooltip`, as before. A change that keeps the same icon skips the icon
render, so a menu-only or tooltip-only change applies at once.

```kotlin
var unread by remember { mutableStateOf(0) }

Tray(
    icon = if (unread > 0) Icons.Default.MarkEmailUnread else Icons.Default.Email,
    tooltip = if (unread > 0) "$unread unread" else "No new mail",
) {
    Item(label = "Mark all read") { unread = 0 }
}
```

### TrayApp

From 2.1.x, `TrayApp`, `rememberTrayAppState` and `TrayAppState` keep their signatures. Only
the imports change:

```diff
-import dev.nucleusframework.composenativetray.trayapp.TrayApp
-import dev.nucleusframework.composenativetray.trayapp.TrayWindowDismissMode
-import dev.nucleusframework.composenativetray.trayapp.rememberTrayAppState
+import dev.nucleusframework.tray.app.TrayApp
+import dev.nucleusframework.tray.app.TrayWindowDismissMode
+import dev.nucleusframework.tray.app.rememberTrayAppState

 fun main() = nucleusApplication {
     val state = rememberTrayAppState(
         initialWindowSize = DpSize(300.dp, 420.dp),
         initialDismissMode = TrayWindowDismissMode.AUTO,
     )
     TrayApp(
         state = state,
         icon = Icons.Default.Dashboard,
         tooltip = "Quick settings",
         menu = {
             Item("Toggle popup") { state.toggle() }
             Divider()
             Item("Quit") { exitApplication() }
         },
     ) {
         Text("Quick settings")
     }
 }
```

Coming from 1.x, also:

| 1.x | Nucleus |
| --- | --- |
| `ApplicationScope.TrayApp` inside Compose Desktop's `application { }` | `NucleusApplicationScope.TrayApp` inside `nucleusApplication { }` |
| `@OptIn(ExperimentalTrayAppApi::class)` | Remove it: `TrayApp` needs no opt-in |
| `content: @Composable DialogWindowScope.() -> Unit` | `content: @Composable () -> Unit`: the popup is no `DialogWindow`, so code that used the `window` of that scope needs another way |
| `transparent = …` | Removed: on Windows and macOS the popup is always a transparent native panel |

`undecorated`, `resizable`, `windowsTitle` and `windowIcon` only apply to the fallback window of a
Linux Wayland session without XWayland. See [Tray apps](/docs/os/tray/tray-app).

### Remove the icon

The icon stays while `Tray` or `TrayApp` is in the composition. Take it out with an `if`, or end
the app with `exitApplication()`:

```diff
 Tray(icon = Icons.Default.Favorite, tooltip = "My App") {
     Item(label = "Exit") {
-        dispose()
-        exitProcess(0)
+        exitApplication()
     }
 }
```

`dispose()` is still in the menu DSL, but it does nothing, as it already did in the composable
menu of 2.x. A `Tray` that leaves the composition is disposed for good; bringing it back creates a
new tray.

## API mapping

| ComposeNativeTray | Nucleus |
| --- | --- |
| `Tray(…)`, every overload | `Tray(…)`, same parameters ([System tray](/docs/os/tray)) |
| `Tray(iconPath, windowsIconPath, …)`, deprecated | Still there and still deprecated. Use `iconContent` or a `DrawableResource` |
| `Item`, `CheckableItem`, `SubMenu`, `Divider` | Same ([Tray menu DSL](/docs/os/tray/menu)) |
| `KeyShortcut`, `Key` | Same, in `dev.nucleusframework.tray.menu` |
| `IconRenderProperties.forCurrentOperatingSystem`, `withoutScalingAndAliasing`, `forMenuItem` | Same, in `dev.nucleusframework.tray.utils` |
| `isMenuBarInDarkMode()` | Same, in `dev.nucleusframework.tray.utils` |
| `TrayApp(…)`, `TrayAppState`, `rememberTrayAppState`, `TrayWindowDismissMode` | Same, in `dev.nucleusframework.tray.app` ([Tray apps](/docs/os/tray/tray-app)) |
| `getTrayPosition()`, `TrayPosition` | Same, in `dev.nucleusframework.tray.app` |
| `getTrayWindowPosition(windowWidth, windowHeight, …)` | Same, in `dev.nucleusframework.tray.app` |
| `allowComposeNativeTrayLogging`, `composeNativeTrayLoggingLevel` | `allowNucleusRuntimeLogging`, `nucleusLoggingLevel` from `core-runtime` (`dev.nucleusframework.core.runtime.tools`). Tray logs go to the `dev.nucleusframework.tray` JUL logger |
| 1.x `SingleInstanceManager` | Built into `nucleusApplication`. React to a second launch with `SingleInstanceRestoreEffect` ([Single instance](/docs/lifecycle/single-instance)) |
| `MacOSWindowManager` (Dock show / hide) | No direct equivalent. For a tray app, `nucleusApplication(dockIconFollowsWindows = true)` keeps the app out of the Dock ([Tray apps](/docs/os/tray/tray-app#hide-the-dock-icon-on-macos)) |

### No public equivalent

These were public in ComposeNativeTray. In Nucleus they are internal: `tray-app` uses them, and
they can change in any release.

- `NativeTray`, the non-composable tray engine. It is marked `@InternalNucleusTrayApi`, whose
  opt-in level is an error. Use the `Tray` composables.
- `ComposableIconUtils`, `MenuContentHash`, `TrayClickTracker`, `AppIdProvider`,
  `MacTrayInitializer` and `WindowsTrayInitializer`, also marked `@InternalNucleusTrayApi`.
- `LinuxTrayInitializer` and `MacOSMenuBarThemeDetector`, now `internal`. For the menu bar theme,
  call `isMenuBarInDarkMode()`; it has no change listener. An `ImageVector` icon without `tint`
  already follows the menu bar theme.
- `getTrayWindowPositionForInstance` is still public, but it takes the instance key that only
  `NativeTray` gives. Use `getTrayWindowPosition` or `TrayApp`.

## Behavior changes

The calls are the same, but the backends were rewritten while the tray was being tested in
Nucleus. These changes can show in an app:

- **Labels are literal.** `&` on Windows and `_` on Linux are no longer read as mnemonics, and a
  `-` label is an item, not a separator. Use `Divider()` for separators.
- **Each tray is independent.** Several trays live side by side on every OS. On Linux, the old
  backend let only one tray live at a time. On Linux and Windows, disposing one tray dropped the
  menu callbacks of the others.
- **Callbacks see current state.** A click, `primaryAction` and `onMenuOpened` run the latest
  lambdas, and a new lambda on each recomposition no longer rebuilds the tray.
- **Changes are conflated.** A burst of updates reaches the native tray in order and collapses to
  the newest state, and disposal is final: no late update can bring the icon back.
- **Icons render to a capped cache.** Identical renders share a file, and at most 64 files are
  kept. Raise the cap with `-Dnucleus.tray.iconCacheSize=<n>`.
- **GNOME.** The AppIndicator extension opens the menu on a single click and calls
  `primaryAction` on a double click, so a `TrayApp` popup opens on a double click. With no menu,
  a single click does nothing. The app never receives the single click.
- **Linux, `primaryAction`.** The old backend always marked the item as menu only, so hosts
  opened the menu and never called the primary action. With a `primaryAction`, the item now asks
  the host to call it on a click (GNOME aside, see above).
- **TrayApp anchoring.** Several `TrayApp`s each anchor their popup to their own icon. On macOS a
  visible popup follows its icon when other menu bar items come or go.
- **Wayland.** In a Linux Wayland session, a click on the desktop, the top bar or a Wayland window
  does not reach the popup, so `TrayWindowDismissMode.AUTO` does not close it there. It closes
  from the icon, from `hide()` or from your code.

## Update the build rules

- **ProGuard.** The old README asked for keep rules on JNA and
  `dev.nucleusframework.composenativetray.**`. That package no longer exists, and the tray uses JNI,
  not JNA. If you keep a rule for the tray, point it at `dev.nucleusframework.tray.**`.
- **GraalVM.** `nucleus.tray` ships its reachability metadata in the JAR, so remove any metadata
  you copied for the `composenativetray` classes.

## Checklist

1. `composenativetray` / `composenativetray-app` replaced with `nucleus.tray` /
   `nucleus.tray-app` at the Nucleus version.
2. Every `dev.nucleusframework.composenativetray` (or `com.kdroid.composetray`) import replaced.
3. `TrayApp` called inside `nucleusApplication { }`, and the rest of the app ported to
   Nucleus 3.0.
4. Menu labels checked: no `&` or `_` mnemonics, `Divider()` instead of `-`.
5. `dispose()` calls in menus replaced with an `if` or `exitApplication()`.
6. No use of `NativeTray` or the other internal classes.
7. Logging switched to `allowNucleusRuntimeLogging` / `nucleusLoggingLevel`.
8. ProGuard and GraalVM rules for `composenativetray` removed or retargeted.
9. Clicks tested on each OS, including a double click under GNOME.

## What's next

- [System tray](/docs/os/tray): icon sources, clicks, and platform behavior.
- [Tray menu DSL](/docs/os/tray/menu): items, checkable items, submenus and shortcuts.
- [Tray apps](/docs/os/tray/tray-app): the popup anchored to the icon.
- [Migrate from Nucleus 2.x](/docs/migrate/from-2.x): the rest of the 3.0 changes.
