Nucleus
Recipes

Build a menu bar app

An app that lives in the tray or menu bar, opens its window on demand, starts at login and notifies the user.

View as Markdown

The app keeps an icon in the macOS menu bar, the Windows notification area or the Linux panel. Its window opens from the icon and hides instead of quitting. The user can turn on launch at login from the menu, and a notification brings the window back.

Add the modules

build.gradle.kts
dependencies {
    implementation("dev.nucleusframework:nucleus.nucleus-application:3.0.0")
    implementation("dev.nucleusframework:nucleus.tray:3.0.0")
    implementation("dev.nucleusframework:nucleus.autolaunch:3.0.0")
    implementation("dev.nucleusframework:nucleus.notification-common:3.0.0")
}

Put it together

import dev.nucleusframework.application.Window
import dev.nucleusframework.application.nucleusApplication
import dev.nucleusframework.autolaunch.AutoLaunch
import dev.nucleusframework.autolaunch.AutoLaunchState
import dev.nucleusframework.tray.Tray

fun main(args: Array<String>) = nucleusApplication(args, dockIconFollowsWindows = true) {
    // Started by the OS at login: stay in the tray until the user asks for the window.
    var windowVisible by remember { mutableStateOf(!AutoLaunch.wasStartedAtLogin(args)) }
    var launchAtLogin by remember { mutableStateOf(AutoLaunch.state() == AutoLaunchState.ENABLED) }

    Tray(icon = Icons.Default.Sync, tooltip = "Sync", primaryAction = { windowVisible = true }) {
        Item(label = "Open") { windowVisible = true }
        CheckableItem(
            label = "Launch at login",
            checked = launchAtLogin,
            onCheckedChange = { on ->
                if (on) AutoLaunch.enable() else AutoLaunch.disable()
                launchAtLogin = AutoLaunch.state() == AutoLaunchState.ENABLED
            },
        )
        Divider()
        Item(label = "Quit") { exitApplication() }
    }

    // Closing hides the window: the app keeps running in the tray.
    Window(onCloseRequest = { windowVisible = false }, visible = windowVisible, title = "Sync") {
        SyncScreen(onSynced = { count ->
            notification(
                title = "Sync complete",
                message = "$count files updated",
                onActivated = { windowVisible = true },
            ).send()
        })
    }
}
  • dockIconFollowsWindows = true makes it a menu bar app on macOS: no Dock icon while the window is hidden. Other systems ignore it.
  • wasStartedAtLogin(args) needs the args of main: on Windows (MSI, NSIS) and Flatpak the login entry passes --nucleus-autostart, while macOS, MSIX and systemd are detected without it.
  • launchAtLogin is read back from AutoLaunch.state() because the user can refuse it in the system settings. When the state is DISABLED_BY_USER, call AutoLaunch.openSystemSettings().

Platform limits

Stock GNOME shows tray icons only with the AppIndicator extension; KDE Plasma and Ubuntu show them by default. Under GNOME a single click opens the menu and a double click runs primaryAction.

  • Launch at login uses SMAppService on macOS 13+; state() is UNSUPPORTED on older versions.
  • macOS delivers notifications only to a bundled app the user has authorized: call NotificationCenter.requestAuthorization once and test with ./gradlew runDistributable.

What's next

Was this page helpful?
Edit on GitHub

Last updated on