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

View as Markdown

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() }
}
  • icon accepts an ImageVector (with an optional iconTint), a Painter, or a DrawableResource. Menu icons render at 16 px.
  • SubMenu nests to any depth.
  • Labels show as written: & and _ are not mnemonics, and a - label is a plain item. Add separators with Divider().
  • A click runs the handler from the latest composition, so it sees current state.
  • The trailing lambda runs in a ComposableTrayMenuScope. TrayApp takes its menu as a plain TrayMenuBuilder lambda. It offers the same functions, but they are not @Composable. Both types live in dev.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

Was this page helpful?
Edit on GitHub

Last updated on