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.
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
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 = truemakes it a menu bar app on macOS: no Dock icon while the window is hidden. Other systems ignore it.wasStartedAtLogin(args)needs theargsofmain: on Windows (MSI, NSIS) and Flatpak the login entry passes--nucleus-autostart, while macOS, MSIX and systemd are detected without it.launchAtLoginis read back fromAutoLaunch.state()because the user can refuse it in the system settings. When the state isDISABLED_BY_USER, callAutoLaunch.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()isUNSUPPORTEDon older versions. - macOS delivers notifications only to a bundled app the user has authorized: call
NotificationCenter.requestAuthorizationonce and test with./gradlew runDistributable.
What's next
- Tray menu: submenus, icons and state-driven items.
- Tray app: a popup window anchored to the icon instead of a menu.
- Launch at login: every backend and its states.
- Notifications: buttons, per-platform options and dismissal.
Was this page helpful?
Edit on GitHub
Last updated on