Tray-anchored apps
TrayApp opens a Compose popup window next to a system tray icon, for menu bar and tray utilities written in Kotlin.
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-appGradle plugin: Not neededWindow runtime: Required
Add the dependency
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
AUTOdismiss mode, the popup hides when it loses focus. Clicks on the tray icon and its menu do not count as outside clicks. InMANUAL, onlyhide()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
AUTOdoes not close it. The popup closes from the icon, fromhide(), or from your code.
API reference
TrayApp parameters
| Parameter | Default | Notes |
|---|---|---|
icon / iconContent / windowsIcon + macLinuxIcon | required | ImageVector, Painter, DrawableResource, or a composable, as in Tray. |
tooltip | required | Tray icon tooltip. |
state | null | A TrayAppState; without it TrayApp keeps its own. |
windowSize | null | Initial size; falls back to the state's size, DpSize(300.dp, 200.dp) by default. |
visibleOnStart | false | Show the popup at launch. |
enterTransition / exitTransition | fade | Popup animations. |
undecorated, resizable, windowsTitle, windowIcon | true, false, "", null | Fallback window only. |
horizontalOffset / verticalOffset | 0 / per OS | Nudge the popup away from the tray anchor, in pixels. |
onPreviewKeyEvent / onKeyEvent | { false } | Key handlers for the popup. |
menu | null | Right-click menu. |
TrayAppState
| Member | Description |
|---|---|
isVisible, windowSize, dismissMode | StateFlows 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
- System tray: the
TrayAPI thatTrayAppbuilds on. - Tray menu DSL: the right-click menu functions.
- Single instance: keep one running copy of a tray app.
Last updated on