feat: the desktop closes to the tray and notifies from it (notifications, Phase 12)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Kgothatso NgakoandClaude Opus 5.5 committed 2026-11-22 13:04:24 +02:00
1 parent 51c2423ab6
commit 206a44e278
2 files changed
+63 -2

No files matched your search

@@ -831,4 +831,8 @@
<string name="stay_connected_supporting">Messages arrive within seconds. Android shows a notification for as long as it is on, and the battery runs down faster.</string>
<string name="stay_connected_channel">Connection</string>
<string name="stay_connected_notification">Connected for new messages</string>
<string name="tray_open">Open window</string>
<string name="tray_quit">Quit</string>
<string name="still_running">Giza is still running</string>
<string name="still_running_detail">It keeps checking for messages until you quit it from the tray.</string>
</resources>
@@ -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