package eu.friedcutlet.yokan import androidx.compose.runtime.collectAsState import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.runtime.setValue import androidx.compose.ui.unit.DpSize import androidx.compose.ui.unit.dp import androidx.compose.ui.window.Tray import androidx.compose.ui.window.Window import androidx.compose.ui.window.rememberWindowState import androidx.compose.ui.window.application import androidx.compose.ui.window.isTraySupported import eu.friedcutlet.yokan.app.DesktopBootstrapState import eu.friedcutlet.yokan.app.YokanRuntime import eu.friedcutlet.yokan.services.scheduling.DesktopAutostart import eu.friedcutlet.yokan.services.scheduling.DesktopSession import eu.friedcutlet.yokan.services.system.AppDeepLinks import java.awt.Desktop import javax.swing.SwingUtilities import eu.friedcutlet.yokan.ui.YokanApp import kotlinx.coroutines.launch import org.jetbrains.compose.resources.painterResource import yokan.composeapp.generated.resources.Res import yokan.composeapp.generated.resources.icon_red private fun configureMacOsWindowAppearance() { if (!System.getProperty("os.name").startsWith("Mac", ignoreCase = true)) return val settings = YokanRuntime.database.settings() val appearance = when { settings.followSystemTheme -> "system" settings.themeIsDark -> "NSAppearanceNameDarkAqua" else -> "NSAppearanceNameAqua" } System.setProperty("apple.awt.application.appearance", appearance) } /** macOS delivers registered URL schemes and associated files as app events, not arguments. */ private fun installOpenHandlers() { if (!Desktop.isDesktopSupported()) return val desktop = Desktop.getDesktop() if (desktop.isSupported(Desktop.Action.APP_OPEN_URI)) { desktop.setOpenURIHandler { event -> SwingUtilities.invokeLater { AppDeepLinks.dispatch(event.uri.toString()) } } } if (desktop.isSupported(Desktop.Action.APP_OPEN_FILE)) { desktop.setOpenFileHandler { event -> val file = event.files.firstOrNull() ?: return@setOpenFileHandler SwingUtilities.invokeLater { AppDeepLinks.dispatch(file.toURI().toString()) } } } } fun main(args: Array) { DesktopBootstrapState.args = args installOpenHandlers() configureMacOsWindowAppearance() YokanRuntime.bootstrap() application { // The application scope sits above any window's LifecycleOwner, and it must keep // observing while the window is hidden in the tray, so plain collectAsState is correct. val closeToTray by DesktopSession.closeToTray.collectAsState() val trayEnabled = closeToTray && isTraySupported // A login item passes --tray so the scheduler runs without a window until the user asks for one. var windowVisible by remember { mutableStateOf(!(DesktopAutostart.TRAY_FLAG in args && trayEnabled)) } if (!trayEnabled) windowVisible = true val scope = rememberCoroutineScope() val icon = painterResource(Res.drawable.icon_red) val windowState = rememberWindowState(size = DpSize(1280.dp, 860.dp)) if (trayEnabled) { Tray( icon = icon, tooltip = "Yokan", onAction = { windowVisible = true }, menu = { Item("Open Yokan") { windowVisible = true } Item("Update library now") { scope.launch { YokanRuntime.periodicTasks.updateLibrary() } } Separator() Item("Quit") { exitApplication() } }, ) } Window( onCloseRequest = { if (trayEnabled) windowVisible = false else exitApplication() }, visible = windowVisible, title = "Yokan", icon = icon, state = windowState, ) { DesktopSession.frame = window YokanApp() } } }