OS integrationSystem tray
Tray menu DSL
Build the tray context menu in Kotlin with items, checkable items, submenus, dividers, shortcut hints, and state-driven content.
The trailing lambda of Tray builds its native context menu. The lambda is @Composable: it
reads state, calls painterResource, and branches with if, and the native menu follows each
change.
Build a menu
var connected by remember { mutableStateOf(false) }
var advanced by remember { mutableStateOf(false) }
Tray(icon = Icons.Default.Wifi, tooltip = "Network") {
Item(label = if (connected) "Disconnect" else "Connect") { connected = !connected }
Item(label = "Export", icon = Icons.Default.Upload) { export() }
Item(label = "Disabled", isEnabled = false)
CheckableItem(
label = "Advanced options",
checked = advanced,
onCheckedChange = { advanced = it },
)
if (advanced) {
SubMenu(label = "Advanced", icon = Icons.Default.Build) {
Item(label = "DNS settings") { openDns() }
SubMenu(label = "More") { Item(label = "Proxy") { openProxy() } }
}
}
Divider()
Item(label = "Quit") { exitApplication() }
}iconaccepts anImageVector(with an optionaliconTint), aPainter, or aDrawableResource. Menu icons render at 16 px.SubMenunests to any depth.- Labels show as written:
&and_are not mnemonics, and a-label is a plain item. Add separators withDivider(). - A click runs the handler from the latest composition, so it sees current state.
- The trailing lambda runs in a
ComposableTrayMenuScope.TrayApptakes itsmenuas a plainTrayMenuBuilderlambda. It offers the same functions, but they are not@Composable. Both types live indev.nucleusframework.tray.menu.
Show shortcut hints
shortcut displays a key combination next to an item. It registers no hotkey; use
global hotkeys for that.
import dev.nucleusframework.tray.menu.Key
import dev.nucleusframework.tray.menu.KeyShortcut
Item(label = "New note", shortcut = KeyShortcut(Key.N, meta = true)) { newNote() }macOS draws the native glyphs (⌘N), Windows shows right-aligned text (Win+N), and the Linux
desktop renders its own.
What's next
- System tray: icon sources, clicks, and platform behavior.
- Tray apps: open a popup window from the icon.
- macOS menu bar: the application menu bar on macOS.
Was this page helpful?
Edit on GitHub
Last updated on