From 206a44e278ab121a9894de237576e43742c75346 Mon Sep 17 00:00:00 2001 From: Kgothatso Ngako Date: Sun, 22 Nov 2026 13:03:53 +0200 Subject: [PATCH] feat: the desktop closes to the tray and notifies from it (notifications, Phase 12) Co-Authored-By: Claude Opus 5.5 --- .../composeResources/values/strings.xml | 4 ++ .../jvmMain/kotlin/mobi/giza/desktop/Main.kt | 61 ++++++++++++++++++- 2 files changed, 63 insertions(+), 2 deletions(-) diff --git a/composeApp/src/commonMain/composeResources/values/strings.xml b/composeApp/src/commonMain/composeResources/values/strings.xml index b262a0cd..20972ab2 100644 --- a/composeApp/src/commonMain/composeResources/values/strings.xml +++ b/composeApp/src/commonMain/composeResources/values/strings.xml @@ -831,4 +831,8 @@ Messages arrive within seconds. Android shows a notification for as long as it is on, and the battery runs down faster. Connection Connected for new messages + Open window + Quit + Giza is still running + It keeps checking for messages until you quit it from the tray. diff --git a/composeApp/src/jvmMain/kotlin/mobi/giza/desktop/Main.kt b/composeApp/src/jvmMain/kotlin/mobi/giza/desktop/Main.kt index 2e4e4c1c..85cf1dde 100644 --- a/composeApp/src/jvmMain/kotlin/mobi/giza/desktop/Main.kt +++ b/composeApp/src/jvmMain/kotlin/mobi/giza/desktop/Main.kt @@ -39,6 +39,17 @@ import kotlinx.coroutines.withContext import mobi.giza.compose.GizaApp import mobi.giza.compose.GizaGlobal import mobi.giza.compose.AppLifecycle +import mobi.giza.compose.ExperimentalFeatures +import mobi.giza.compose.notifications.DesktopNotifier +import mobi.giza.compose.resources.still_running +import mobi.giza.compose.resources.still_running_detail +import mobi.giza.compose.resources.tray_open +import mobi.giza.compose.resources.tray_quit +import androidx.compose.runtime.DisposableEffect +import androidx.compose.ui.window.Notification +import androidx.compose.ui.window.Tray +import androidx.compose.ui.window.rememberTrayState +import java.awt.SystemTray import mobi.giza.compose.GizaRuntime import mobi.giza.compose.PlatformContext import mobi.giza.compose.defaultGizaDir @@ -66,9 +77,55 @@ import java.io.File fun main() = application { val appDir = remember { defaultGizaDir().apply { mkdirs() } } + // Closed to the tray rather than quit, where there is a tray and syncing while closed is on: + // the process is what receives, so the window is only one way of looking at it. Some Linux + // sessions have no tray, and there closing quits, as it always did. + // docs/notifications-and-background-sync.md, Phase 12. + val trayAvailable = remember { ExperimentalFeatures.backgroundSync && SystemTray.isSupported() } + val trayState = rememberTrayState() + var windowVisible by remember { mutableStateOf(true) } + var toldStillRunning by remember { mutableStateOf(false) } + val appName = stringResource(Res.string.app_name) + val stillRunning = stringResource(Res.string.still_running) + val stillRunningDetail = stringResource(Res.string.still_running_detail) + + // A hidden window is nobody looking, whatever its lifecycle says; the policy posts for the + // room that was open in it. + LaunchedEffect(windowVisible) { + if (windowVisible) AppLifecycle.enteredForeground() else AppLifecycle.enteredBackground() + } + + if (trayAvailable) { + DisposableEffect(trayState) { + DesktopNotifier.sink = { title, body -> trayState.sendNotification(Notification(title, body, Notification.Type.None)) } + onDispose { DesktopNotifier.sink = null } + } + Tray( + icon = painterResource(Res.drawable.compose_multiplatform), + state = trayState, + tooltip = appName, + onAction = { windowVisible = true }, + menu = { + Item(stringResource(Res.string.tray_open), onClick = { windowVisible = true }) + Item(stringResource(Res.string.tray_quit), onClick = ::exitApplication) + }, + ) + } + Window( - onCloseRequest = ::exitApplication, - title = stringResource(Res.string.app_name), + onCloseRequest = { + if (trayAvailable) { + windowVisible = false + if (!toldStillRunning) { + toldStillRunning = true + trayState.sendNotification(Notification(stillRunning, stillRunningDetail, Notification.Type.Info)) + } + } else { + exitApplication() + } + }, + visible = windowVisible, + title = appName, // The title bar and taskbar on Windows and X11. It is the in-app logo, the same one // the QR codes carry. The installers' icons are the .icns/.ico/.png named in // build.gradle.kts; the macOS dock and GNOME on Wayland take theirs from the bundle