Created
May 13, 2026 08:21
-
-
Save ozbeksu/35acc1fe2701ec0d57bae3a6fce72900 to your computer and use it in GitHub Desktop.
Kotlin Compose Desktop / Main.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.lambdastratum.appstream | |
| import androidx.compose.foundation.layout.* | |
| import androidx.compose.material3.* | |
| import androidx.compose.runtime.* | |
| import androidx.compose.ui.Alignment | |
| import androidx.compose.ui.Modifier | |
| import androidx.compose.ui.unit.dp | |
| import androidx.compose.ui.unit.sp | |
| import androidx.compose.ui.window.Window | |
| import androidx.compose.ui.window.application | |
| import clojure.java.api.Clojure | |
| import clojure.lang.Keyword | |
| import clojure.lang.PersistentVector | |
| import clojure.lang.IPersistentMap | |
| fun parseEdn(text: String): Any? { | |
| val readString = Clojure.`var`("clojure.edn", "read-string") | |
| return readString.invoke(text) | |
| } | |
| @Composable | |
| fun RenderNode(node: Any?, showToast: (String) -> Unit) { | |
| if (node !is PersistentVector || node.count() == 0) { | |
| return | |
| } | |
| val type = node.nth(0).toString() | |
| val maybeProps = if (node.count() > 1) node.nth(1) else null | |
| val props = maybeProps as? IPersistentMap | |
| val childrenStart = if (props != null) 2 else 1 | |
| when (type) { | |
| ":column" -> { | |
| val gap = ((props?.valAt(Keyword.intern("gap")) as? Long) ?: 8L).toInt() | |
| Column( | |
| verticalArrangement = Arrangement.spacedBy(gap.dp) | |
| ) { | |
| for (i in childrenStart until node.count()) { | |
| RenderNode(node.nth(i), showToast) | |
| } | |
| } | |
| } | |
| ":text" -> { | |
| val size = ((props?.valAt(Keyword.intern("size")) as? Long) ?: 16L).toInt() | |
| val content = node.nth(childrenStart).toString() | |
| Text( | |
| text = content, | |
| fontSize = size.sp | |
| ) | |
| } | |
| ":button" -> { | |
| val label = props | |
| ?.valAt(Keyword.intern("label")) | |
| ?.toString() | |
| ?: "Button" | |
| val toast = props | |
| ?.valAt(Keyword.intern("toast")) | |
| ?.toString() | |
| ?: "Clicked" | |
| Button( | |
| onClick = { | |
| showToast(toast) | |
| } | |
| ) { | |
| Text(label) | |
| } | |
| } | |
| ":toast" -> { | |
| val message = props | |
| ?.valAt(Keyword.intern("message")) | |
| ?.toString() | |
| ?: "Toast" | |
| LaunchedEffect(message) { | |
| showToast(message) | |
| } | |
| } | |
| } | |
| } | |
| fun main() = application { | |
| Window( | |
| onCloseRequest = ::exitApplication, | |
| title = "EDN Runtime Demo" | |
| ) { | |
| MaterialTheme { | |
| var ednText by remember { | |
| mutableStateOf( | |
| """ | |
| [:column | |
| {:gap 12} | |
| [:text | |
| {:size 24} | |
| "Hello from EDN"] | |
| [:button | |
| {:label "Click Me" | |
| :toast "Button clicked from EDN runtime"}]] | |
| """.trimIndent() | |
| ) | |
| } | |
| var parsedTree by remember { | |
| mutableStateOf<Any?>(null) | |
| } | |
| var toastMessage by remember { | |
| mutableStateOf<String?>(null) | |
| } | |
| LaunchedEffect(ednText) { | |
| try { | |
| parsedTree = parseEdn(ednText) | |
| } catch (e: Exception) { | |
| parsedTree = null | |
| } | |
| } | |
| Box( | |
| modifier = Modifier | |
| .fillMaxSize() | |
| .padding(16.dp) | |
| ) { | |
| Row( | |
| modifier = Modifier.fillMaxSize(), | |
| horizontalArrangement = Arrangement.spacedBy(16.dp) | |
| ) { | |
| Column( | |
| modifier = Modifier.weight(1f) | |
| ) { | |
| Text("EDN Input") | |
| Spacer(modifier = Modifier.height(8.dp)) | |
| OutlinedTextField( | |
| value = ednText, | |
| onValueChange = { | |
| ednText = it | |
| }, | |
| modifier = Modifier.fillMaxSize() | |
| ) | |
| } | |
| Column( | |
| modifier = Modifier.weight(1f), | |
| verticalArrangement = Arrangement.spacedBy(12.dp) | |
| ) { | |
| Text("Rendered UI") | |
| Card( | |
| modifier = Modifier.fillMaxWidth() | |
| ) { | |
| Column( | |
| modifier = Modifier.padding(16.dp) | |
| ) { | |
| RenderNode(parsedTree) { | |
| toastMessage = it | |
| } | |
| } | |
| } | |
| } | |
| } | |
| toastMessage?.let { msg -> | |
| Box( | |
| modifier = Modifier.fillMaxSize(), | |
| contentAlignment = Alignment.BottomCenter | |
| ) { | |
| Snackbar( | |
| modifier = Modifier.padding(16.dp) | |
| ) { | |
| Text(msg) | |
| } | |
| } | |
| LaunchedEffect(msg) { | |
| kotlinx.coroutines.delay(2000) | |
| toastMessage = null | |
| } | |
| } | |
| } | |
| } | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
build.gradle.kts