Main.kt
composeApp/src/desktopMain/kotlin/eu/friedcutlet/yokan/Main.kt
1package eu.friedcutlet.yokan23import androidx.compose.runtime.collectAsState4import androidx.compose.runtime.getValue5import androidx.compose.runtime.mutableStateOf6import androidx.compose.runtime.remember7import androidx.compose.runtime.rememberCoroutineScope8import androidx.compose.runtime.setValue9import androidx.compose.ui.unit.DpSize10import androidx.compose.ui.unit.dp11import androidx.compose.ui.window.Tray12import androidx.compose.ui.window.Window13import androidx.compose.ui.window.rememberWindowState14import androidx.compose.ui.window.application15import androidx.compose.ui.window.isTraySupported16import eu.friedcutlet.yokan.app.DesktopBootstrapState17import eu.friedcutlet.yokan.app.YokanRuntime18import eu.friedcutlet.yokan.services.scheduling.DesktopAutostart19import eu.friedcutlet.yokan.services.scheduling.DesktopSession20import eu.friedcutlet.yokan.services.system.AppDeepLinks21import java.awt.Desktop22import javax.swing.SwingUtilities23import eu.friedcutlet.yokan.ui.YokanApp24import kotlinx.coroutines.launch25import org.jetbrains.compose.resources.painterResource26import yokan.composeapp.generated.resources.Res27import yokan.composeapp.generated.resources.icon_red2829private fun configureMacOsWindowAppearance() {30 if (!System.getProperty("os.name").startsWith("Mac", ignoreCase = true)) return31 val settings = YokanRuntime.database.settings()32 val appearance = when {33 settings.followSystemTheme -> "system"34 settings.themeIsDark -> "NSAppearanceNameDarkAqua"35 else -> "NSAppearanceNameAqua"36 }37 System.setProperty("apple.awt.application.appearance", appearance)38}3940/** macOS delivers registered URL schemes and associated files as app events, not arguments. */41private fun installOpenHandlers() {42 if (!Desktop.isDesktopSupported()) return43 val desktop = Desktop.getDesktop()44 if (desktop.isSupported(Desktop.Action.APP_OPEN_URI)) {45 desktop.setOpenURIHandler { event -> SwingUtilities.invokeLater { AppDeepLinks.dispatch(event.uri.toString()) } }46 }47 if (desktop.isSupported(Desktop.Action.APP_OPEN_FILE)) {48 desktop.setOpenFileHandler { event ->49 val file = event.files.firstOrNull() ?: return@setOpenFileHandler50 SwingUtilities.invokeLater { AppDeepLinks.dispatch(file.toURI().toString()) }51 }52 }53}5455fun main(args: Array<String>) {56 DesktopBootstrapState.args = args57 installOpenHandlers()58 configureMacOsWindowAppearance()59 YokanRuntime.bootstrap()60 application {61 // The application scope sits above any window's LifecycleOwner, and it must keep62 // observing while the window is hidden in the tray, so plain collectAsState is correct.63 val closeToTray by DesktopSession.closeToTray.collectAsState()64 val trayEnabled = closeToTray && isTraySupported65 // A login item passes --tray so the scheduler runs without a window until the user asks for one.66 var windowVisible by remember { mutableStateOf(!(DesktopAutostart.TRAY_FLAG in args && trayEnabled)) }67 if (!trayEnabled) windowVisible = true68 val scope = rememberCoroutineScope()69 val icon = painterResource(Res.drawable.icon_red)70 val windowState = rememberWindowState(size = DpSize(1280.dp, 860.dp))71 if (trayEnabled) {72 Tray(73 icon = icon,74 tooltip = "Yokan",75 onAction = { windowVisible = true },76 menu = {77 Item("Open Yokan") { windowVisible = true }78 Item("Update library now") { scope.launch { YokanRuntime.periodicTasks.updateLibrary() } }79 Separator()80 Item("Quit") { exitApplication() }81 },82 )83 }84 Window(85 onCloseRequest = { if (trayEnabled) windowVisible = false else exitApplication() },86 visible = windowVisible,87 title = "Yokan",88 icon = icon,89 state = windowState,90 ) {91 DesktopSession.frame = window92 YokanApp()93 }94 }95}96