102 lines
3.0 KiB
Kotlin
102 lines
3.0 KiB
Kotlin
package com.bricks
|
|
|
|
import androidx.compose.desktop.ui.tooling.preview.Preview
|
|
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.window.Window
|
|
import androidx.compose.ui.window.application
|
|
import com.bricks.mp.core.BricksContext
|
|
import com.bricks.mp.core.BricksParser
|
|
import com.bricks.mp.core.BricksApp
|
|
import kotlinx.coroutines.CoroutineScope
|
|
import kotlinx.coroutines.Dispatchers
|
|
|
|
fun main() = application {
|
|
Window(
|
|
onCloseRequest = ::exitApplication,
|
|
title = "Bricks MP - Desktop",
|
|
state = rememberWindowState(width = 1200.dp, height = 800.dp)
|
|
) {
|
|
BricksDesktopApp()
|
|
}
|
|
}
|
|
|
|
@Composable
|
|
@Preview
|
|
fun BricksDesktopApp() {
|
|
val context = remember { BricksContext() }
|
|
var jsonInput by remember { mutableStateOf(SAMPLE_JSON) }
|
|
var rootWidget by remember { mutableStateOf<com.bricks.mp.core.BricksWidget?>(null) }
|
|
|
|
Column(modifier = Modifier.fillMaxSize()) {
|
|
// JSON 输入区
|
|
Row(modifier = Modifier.fillMaxWidth().weight(0.3f)) {
|
|
OutlinedTextField(
|
|
value = jsonInput,
|
|
onValueChange = { jsonInput = it },
|
|
modifier = Modifier.fillMaxSize().padding(8.dp),
|
|
label = { Text("Bricks JSON") },
|
|
textStyle = androidx.compose.ui.text.TextStyle(fontFamily = androidx.compose.ui.text.font.FontFamily.Monospace)
|
|
)
|
|
}
|
|
|
|
// 解析按钮
|
|
Row(modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.Center) {
|
|
Button(onClick = {
|
|
try {
|
|
rootWidget = BricksParser.parse(jsonInput)
|
|
} catch (e: Exception) {
|
|
println("Parse error: ${e.message}")
|
|
}
|
|
}) {
|
|
Text("Parse & Render")
|
|
}
|
|
}
|
|
|
|
// 渲染区
|
|
Surface(modifier = Modifier.fillMaxWidth().weight(0.6f)) {
|
|
if (rootWidget != null) {
|
|
BricksApp(rootWidget!!)
|
|
} else {
|
|
Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {
|
|
Text("Enter Bricks JSON and click Parse")
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
val SAMPLE_JSON = """
|
|
{
|
|
"widgettype": "VBox",
|
|
"options": {
|
|
"text": "Hello Bricks MP"
|
|
},
|
|
"subwidgets": [
|
|
{
|
|
"widgettype": "Title1",
|
|
"options": {
|
|
"text": "Welcome to Bricks"
|
|
}
|
|
},
|
|
{
|
|
"widgettype": "Text",
|
|
"options": {
|
|
"text": "Cross-platform JSON-driven UI"
|
|
}
|
|
},
|
|
{
|
|
"widgettype": "KeyinText",
|
|
"options": {
|
|
"placeholder": "Type here...",
|
|
"label": "Input"
|
|
}
|
|
}
|
|
]
|
|
}
|
|
""".trimIndent()
|