Nucleus
OS integrationSystem tray

Tray-anchored apps

TrayApp opens a Compose popup window next to a system tray icon, for menu bar and tray utilities written in Kotlin.

View as Markdown

TrayApp anchors a Compose popup to a system tray icon. A left click on the icon opens the popup next to it, a right click opens its menu, and a click elsewhere closes the popup.

tray-appmacOSWindowsLinuxGradle plugin: Not neededWindow runtime: Required

A click on the menu bar icon opens the popup anchored under it; a second click closes it

Add the dependency

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

tray-app brings tray and nucleus-application. Its API lives in dev.nucleusframework.tray.app.

Changed in 3.0

TrayApp used to ship in the separate composenativetray-app library. It is now the Nucleus tray-app module: replace the dependency and the dev.nucleusframework.composenativetray.trayapp imports. See Migrate from 2.x.

Create a tray app

Call TrayApp inside nucleusApplication { }: it extends NucleusApplicationScope. Hold a TrayAppState to show, hide, or resize the popup from code:

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, 400.dp),
        initialDismissMode = TrayWindowDismissMode.AUTO,
    )

    TrayApp(
        icon = Icons.Default.Dashboard,
        tooltip = "Quick dashboard",
        state = state,
        menu = {
            Item(label = "Settings") { openSettings() }
            Divider()
            Item(label = "Quit") { exitApplication() }
        },
    ) {
        Column(Modifier.fillMaxSize().padding(16.dp)) {
            Text("CPU: 42%")
            Button(onClick = { state.hide() }) { Text("Close") }
        }
    }
}

menu takes the functions of the tray menu DSL, without @Composable.

Hide the Dock icon on macOS

Pass dockIconFollowsWindows = true to nucleusApplication instead of setting LSUIElement in Info.plist:

fun main() = nucleusApplication(dockIconFollowsWindows = true) {
    TrayApp(icon = Icons.Default.Dashboard, tooltip = "Dashboard") { Text("Popup content") }
}

A Dock tile appears only while a DecoratedWindow with hiddenFromDock = false is visible. TrayApp popups never count, so a tray-only app stays out of the Dock. Other systems ignore the flag. See Hide a window from the taskbar or Dock.

Notes

  • The popup has no title bar and does not appear in the taskbar, the Dock, or Alt+Tab.
  • In AUTO dismiss mode, the popup hides when it loses focus. Clicks on the tray icon and its menu do not count as outside clicks. In MANUAL, only hide() or a click on the icon closes it.
  • On macOS the popup joins every Space and stays when you click the wallpaper.
  • The popup keeps its composition while hidden, so its state survives a hide and show.
  • On macOS a visible popup follows its icon when other menu bar items appear or go.
  • Several TrayApps each anchor their popup to their own icon.
  • On Linux the popup needs X11 or XWayland. A Wayland session without XWayland gets an opaque undecorated window instead: the "fallback window only" parameters below apply to it.

Known limitations

  • Under GNOME, the AppIndicator extension opens the menu on a single click and calls the primary action on a double click, so the popup opens on a double click. The app never sees the single click, so it cannot change this.
  • Under a Linux Wayland session, a click on the desktop, the top bar, or a Wayland window does not reach the popup, so AUTO does not close it. The popup closes from the icon, from hide(), or from your code.

API reference

TrayApp parameters

ParameterDefaultNotes
icon / iconContent / windowsIcon + macLinuxIconrequiredImageVector, Painter, DrawableResource, or a composable, as in Tray.
tooltiprequiredTray icon tooltip.
statenullA TrayAppState; without it TrayApp keeps its own.
windowSizenullInitial size; falls back to the state's size, DpSize(300.dp, 200.dp) by default.
visibleOnStartfalseShow the popup at launch.
enterTransition / exitTransitionfadePopup animations.
undecorated, resizable, windowsTitle, windowIcontrue, false, "", nullFallback window only.
horizontalOffset / verticalOffset0 / per OSNudge the popup away from the tray anchor, in pixels.
onPreviewKeyEvent / onKeyEvent{ false }Key handlers for the popup.
menunullRight-click menu.

TrayAppState

MemberDescription
isVisible, windowSize, dismissModeStateFlows of the current values.
show(), hide(), toggle()Change visibility.
setWindowSize(size), setWindowSize(width, height)Resize the popup.
setDismissMode(mode)AUTO or MANUAL.
onVisibilityChanged(callback)Observe visibility changes.

getTrayPosition() returns the TrayPosition (TOP_LEFT, TOP_RIGHT, BOTTOM_LEFT, BOTTOM_RIGHT) of the screen corner that holds the tray.

What's next

Was this page helpful?
Edit on GitHub

Last updated on