fix: 修复远程 UI 间距/自适应/服务端导航,并消除文字锯齿
- 服务端:Spacer 改用 Modifier.height(RcDp);画布改为 400×900dp + RcDensityBehavior.Dp; 字号统一 RcSp;卡片通过 hostAction 下发跳转目标 - Android:文档在后台解析并做 LRU 缓存;复用播放器实例;在 setDocument 之后注册 addIdActionListener,实现服务端驱动跳转;移除硬编码的 FilterChip - Android:代理播放器内部 PaintChanges,在每次画笔变更时强制开启 antiAlias, 规避 alpha24 播放端文字锯齿 - 配置:删除 Java 17 下无效的 spring.threads.virtual.enabled;部署镜像更新为 1.0.0-nav
This commit is contained in:
1 parent
5c5585debe
commit
8a5d0375a6
5 files changed
+304
-154
No files matched your search
@@ -4,22 +4,10 @@ import android.os.Bundle
|
|||||||
import androidx.activity.ComponentActivity
|
import androidx.activity.ComponentActivity
|
||||||
import androidx.activity.compose.setContent
|
import androidx.activity.compose.setContent
|
||||||
import androidx.activity.enableEdgeToEdge
|
import androidx.activity.enableEdgeToEdge
|
||||||
import androidx.compose.foundation.layout.Arrangement
|
|
||||||
import androidx.compose.foundation.layout.Box
|
import androidx.compose.foundation.layout.Box
|
||||||
import androidx.compose.foundation.layout.Column
|
|
||||||
import androidx.compose.foundation.layout.Row
|
|
||||||
import androidx.compose.foundation.layout.fillMaxSize
|
import androidx.compose.foundation.layout.fillMaxSize
|
||||||
import androidx.compose.foundation.layout.fillMaxWidth
|
import androidx.compose.foundation.layout.systemBarsPadding
|
||||||
import androidx.compose.foundation.layout.padding
|
|
||||||
import androidx.compose.material3.FilterChip
|
|
||||||
import androidx.compose.material3.MaterialTheme
|
|
||||||
import androidx.compose.material3.Text
|
|
||||||
import androidx.compose.runtime.getValue
|
|
||||||
import androidx.compose.runtime.mutableStateOf
|
|
||||||
import androidx.compose.runtime.remember
|
|
||||||
import androidx.compose.runtime.setValue
|
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
import androidx.compose.ui.unit.dp
|
|
||||||
import com.ttstd.composeremote.ui.RemoteUIScreen
|
import com.ttstd.composeremote.ui.RemoteUIScreen
|
||||||
import com.ttstd.composeremote.ui.theme.ComposeRemoteAndroidTheme
|
import com.ttstd.composeremote.ui.theme.ComposeRemoteAndroidTheme
|
||||||
|
|
||||||
@@ -29,29 +17,9 @@ class MainActivity : ComponentActivity() {
|
|||||||
enableEdgeToEdge()
|
enableEdgeToEdge()
|
||||||
setContent {
|
setContent {
|
||||||
ComposeRemoteAndroidTheme {
|
ComposeRemoteAndroidTheme {
|
||||||
// 页面 id 列表,每个 id 对应后端一个独立接口 /api/remote-ui/{page}
|
// 页面切换完全由服务端文档中的点击动作驱动,客户端只提供初始页面。
|
||||||
var page by remember { mutableStateOf("home") }
|
Box(modifier = Modifier.fillMaxSize().systemBarsPadding()) {
|
||||||
Column(modifier = Modifier.fillMaxSize()) {
|
RemoteUIScreen()
|
||||||
Row(
|
|
||||||
modifier = Modifier
|
|
||||||
.fillMaxWidth()
|
|
||||||
.padding(8.dp),
|
|
||||||
horizontalArrangement = Arrangement.spacedBy(8.dp)
|
|
||||||
) {
|
|
||||||
FilterChip(
|
|
||||||
selected = page == "home",
|
|
||||||
onClick = { page = "home" },
|
|
||||||
label = { Text("首页") }
|
|
||||||
)
|
|
||||||
FilterChip(
|
|
||||||
selected = page == "detail",
|
|
||||||
onClick = { page = "detail" },
|
|
||||||
label = { Text("详情") }
|
|
||||||
)
|
|
||||||
}
|
|
||||||
Box(modifier = Modifier.fillMaxSize().weight(1f)) {
|
|
||||||
RemoteUIScreen(page = page)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+178
-50
@@ -1,6 +1,7 @@
|
|||||||
package com.ttstd.composeremote.ui
|
package com.ttstd.composeremote.ui
|
||||||
|
|
||||||
import android.annotation.SuppressLint
|
import android.annotation.SuppressLint
|
||||||
|
import android.content.Context
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
import androidx.compose.foundation.layout.Arrangement
|
import androidx.compose.foundation.layout.Arrangement
|
||||||
import androidx.compose.foundation.layout.Box
|
import androidx.compose.foundation.layout.Box
|
||||||
@@ -11,49 +12,73 @@ import androidx.compose.material3.CircularProgressIndicator
|
|||||||
import androidx.compose.material3.ElevatedButton
|
import androidx.compose.material3.ElevatedButton
|
||||||
import androidx.compose.material3.MaterialTheme
|
import androidx.compose.material3.MaterialTheme
|
||||||
import androidx.compose.material3.Text
|
import androidx.compose.material3.Text
|
||||||
|
import androidx.compose.remote.player.core.RemoteDocument
|
||||||
|
import androidx.compose.remote.player.view.RemoteComposePlayer
|
||||||
import androidx.compose.runtime.Composable
|
import androidx.compose.runtime.Composable
|
||||||
import androidx.compose.runtime.LaunchedEffect
|
import androidx.compose.runtime.LaunchedEffect
|
||||||
import androidx.compose.runtime.getValue
|
import androidx.compose.runtime.getValue
|
||||||
import androidx.compose.runtime.key
|
|
||||||
import androidx.compose.runtime.mutableIntStateOf
|
import androidx.compose.runtime.mutableIntStateOf
|
||||||
import androidx.compose.runtime.mutableStateOf
|
import androidx.compose.runtime.mutableStateOf
|
||||||
import androidx.compose.runtime.remember
|
import androidx.compose.runtime.remember
|
||||||
|
import androidx.compose.runtime.rememberUpdatedState
|
||||||
|
import androidx.compose.runtime.saveable.rememberSaveable
|
||||||
import androidx.compose.runtime.setValue
|
import androidx.compose.runtime.setValue
|
||||||
import androidx.compose.ui.Alignment
|
import androidx.compose.ui.Alignment
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
import androidx.compose.ui.viewinterop.AndroidView
|
|
||||||
import androidx.compose.remote.player.view.RemoteComposePlayer
|
|
||||||
import androidx.compose.remote.player.core.RemoteDocument
|
|
||||||
import androidx.compose.ui.unit.dp
|
import androidx.compose.ui.unit.dp
|
||||||
|
import androidx.compose.ui.viewinterop.AndroidView
|
||||||
import com.ttstd.composeremote.RemoteConfig
|
import com.ttstd.composeremote.RemoteConfig
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
import okhttp3.OkHttpClient
|
import okhttp3.OkHttpClient
|
||||||
import okhttp3.Request
|
import okhttp3.Request
|
||||||
import java.io.IOException
|
import java.io.IOException
|
||||||
|
import java.lang.reflect.InvocationTargetException
|
||||||
|
import java.lang.reflect.Proxy
|
||||||
import java.util.concurrent.TimeUnit
|
import java.util.concurrent.TimeUnit
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 与后端约定的 action id:跳转到 metadata 指定的页面。
|
||||||
|
* 具体跳到哪个页面由文档内容决定,客户端不再写死页面列表。
|
||||||
|
*/
|
||||||
|
private const val ACTION_NAVIGATE = 1
|
||||||
|
|
||||||
private val client = OkHttpClient.Builder()
|
private val client = OkHttpClient.Builder()
|
||||||
.connectTimeout(10, TimeUnit.SECONDS)
|
.connectTimeout(10, TimeUnit.SECONDS)
|
||||||
.readTimeout(10, TimeUnit.SECONDS)
|
.readTimeout(10, TimeUnit.SECONDS)
|
||||||
.build()
|
.build()
|
||||||
|
|
||||||
private sealed interface RemoteUiState {
|
/**
|
||||||
data object Loading : RemoteUiState
|
* 已解析文档缓存。
|
||||||
data class Success(val bytes: ByteArray) : RemoteUiState {
|
*
|
||||||
override fun equals(other: Any?): Boolean =
|
* 切页时优先复用已解析的 [RemoteDocument],避免重复下载与重复解析;
|
||||||
other is Success && bytes.contentEquals(other.bytes)
|
* 文档较大时解析成本高,因此只缓存有限个页面(LRU)。
|
||||||
|
*/
|
||||||
override fun hashCode(): Int = bytes.contentHashCode()
|
private object RemoteDocumentStore {
|
||||||
|
private const val MAX_ENTRIES = 8
|
||||||
|
private val documents = object :
|
||||||
|
LinkedHashMap<String, RemoteDocument>(MAX_ENTRIES, 0.75f, true) {
|
||||||
|
override fun removeEldestEntry(
|
||||||
|
eldest: MutableMap.MutableEntry<String, RemoteDocument>,
|
||||||
|
): Boolean = size > MAX_ENTRIES
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun get(page: String): RemoteDocument? = documents[page]
|
||||||
|
|
||||||
|
fun put(page: String, document: RemoteDocument) {
|
||||||
|
documents[page] = document
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private sealed interface RemoteUiState {
|
||||||
|
data object Loading : RemoteUiState
|
||||||
|
data class Success(val document: RemoteDocument) : RemoteUiState
|
||||||
data class Error(val message: String) : RemoteUiState
|
data class Error(val message: String) : RemoteUiState
|
||||||
}
|
}
|
||||||
|
|
||||||
private suspend fun fetchRemoteUi(page: String, baseUrl: String): ByteArray =
|
private suspend fun fetchRemoteUi(page: String, baseUrl: String): ByteArray =
|
||||||
withContext(Dispatchers.IO) {
|
withContext(Dispatchers.IO) {
|
||||||
val url = "$baseUrl/api/remote-ui/$page"
|
val request = Request.Builder().url("$baseUrl/api/remote-ui/$page").build()
|
||||||
val request = Request.Builder().url(url).build()
|
|
||||||
client.newCall(request).execute().use { response ->
|
client.newCall(request).execute().use { response ->
|
||||||
if (!response.isSuccessful) {
|
if (!response.isSuccessful) {
|
||||||
throw IOException("HTTP ${response.code}")
|
throw IOException("HTTP ${response.code}")
|
||||||
@@ -63,67 +88,170 @@ private suspend fun fetchRemoteUi(page: String, baseUrl: String): ByteArray =
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 使用 [RemoteComposePlayer] 渲染 Remote Compose 文档。
|
* 下载并解析页面文档。
|
||||||
*
|
*
|
||||||
* 在 alpha17 版本中,建议显式创建 [RemoteDocument] 并使用 [RemoteComposePlayer.setDocument]
|
* 解析([RemoteDocument] 构造)放在 [Dispatchers.Default],避免大文档阻塞主线程。
|
||||||
* 的 RemoteDocument 重载版本。同时将 setDocument 放在 AndroidView 的 update 块中,
|
*/
|
||||||
* 以确保在数据变化时能正确触发 View 的更新。
|
private suspend fun loadDocument(page: String, baseUrl: String): RemoteDocument {
|
||||||
|
RemoteDocumentStore.get(page)?.let { return it }
|
||||||
|
val bytes = fetchRemoteUi(page, baseUrl)
|
||||||
|
val document = withContext(Dispatchers.Default) { RemoteDocument(bytes) }
|
||||||
|
RemoteDocumentStore.put(page, document)
|
||||||
|
return document
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 规避 alpha24 播放器的文字锯齿。
|
||||||
|
*
|
||||||
|
* 播放端共享的 `android.graphics.Paint` 默认 `isAntiAlias = false`,而 `CoreText`
|
||||||
|
* 每次绘制文字都会 `replacePaint()`:先 `Paint.reset()`(抗锯齿归零)再应用文本
|
||||||
|
* 自带的 PaintBundle,该 bundle 不含 antiAlias 标记,因此服务端无法通过文档打开
|
||||||
|
* 文字抗锯齿。这里把播放器内部的 `PaintChanges` 换成代理,在每次应用画笔变更时
|
||||||
|
* 顺带打开 antiAlias。
|
||||||
|
*
|
||||||
|
* 不直接替换 `mPaint` 实例:`savePaint()/restorePaint()` 会把 `mPaint` 换回普通
|
||||||
|
* `Paint` 拷贝,替换只对第一帧有效。
|
||||||
|
*/
|
||||||
|
private class AntiAliasRemoteComposePlayer(context: Context) : RemoteComposePlayer(context) {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* `mInner` 是父类的 protected 字段,`mARContext` / `mPaintContext` /
|
||||||
|
* `mCachedPaintChanges` 是播放库内部字段,只能通过反射拿到;
|
||||||
|
* 任一步取不到都保持原样,不影响正常渲染。
|
||||||
|
*/
|
||||||
|
fun enableAntiAliasing() {
|
||||||
|
try {
|
||||||
|
val arContext = mInner.declaredField("mARContext").get(mInner) ?: return
|
||||||
|
val paintContext = arContext.findDeclaredField("mPaintContext")?.get(arContext) ?: return
|
||||||
|
val changesField = paintContext.declaredField("mCachedPaintChanges")
|
||||||
|
val delegate = changesField.get(paintContext) ?: return
|
||||||
|
if (Proxy.isProxyClass(delegate.javaClass)) return
|
||||||
|
|
||||||
|
val changesClass = delegate.javaClass.interfaces.first()
|
||||||
|
val setAntiAlias = changesClass.getMethod("setAntiAlias", Boolean::class.javaPrimitiveType)
|
||||||
|
val proxy = Proxy.newProxyInstance(changesClass.classLoader, arrayOf(changesClass)) { _, method, args ->
|
||||||
|
when {
|
||||||
|
method.declaringClass == Any::class.java -> when (method.name) {
|
||||||
|
"equals" -> delegate === args?.firstOrNull()
|
||||||
|
"hashCode" -> System.identityHashCode(delegate)
|
||||||
|
else -> "AntiAliasPaintChanges"
|
||||||
|
}
|
||||||
|
|
||||||
|
else -> {
|
||||||
|
if (method.name.startsWith("set")) {
|
||||||
|
// 代理在绘制线程被调用,任何反射异常都不能冒泡到播放器绘制流程。
|
||||||
|
runCatching { setAntiAlias.invoke(delegate, true) }
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
method.invoke(delegate, *(args ?: emptyArray<Any?>()))
|
||||||
|
} catch (e: InvocationTargetException) {
|
||||||
|
throw e.targetException
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
changesField.set(paintContext, proxy)
|
||||||
|
} catch (e: Exception) {
|
||||||
|
Log.w("RemoteUIScreen", "开启文字抗锯齿失败", e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun Any.declaredField(name: String) =
|
||||||
|
javaClass.getDeclaredField(name).apply { isAccessible = true }
|
||||||
|
|
||||||
|
/** 沿继承链查找字段(`mPaintContext` 声明在父类上)。 */
|
||||||
|
private fun Any.findDeclaredField(name: String): java.lang.reflect.Field? {
|
||||||
|
var cls: Class<*>? = javaClass
|
||||||
|
while (cls != null) {
|
||||||
|
cls.declaredFields.firstOrNull { it.name == name }?.let {
|
||||||
|
return it.apply { isAccessible = true }
|
||||||
|
}
|
||||||
|
cls = cls.superclass
|
||||||
|
}
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 播放器只创建一次,切页时复用同一个 [RemoteComposePlayer] 实例,仅替换文档。
|
||||||
|
*
|
||||||
|
* 服务端通过 hostAction 下发的点击会回调 [RemoteComposePlayer.addIdActionListener],
|
||||||
|
* 这里把 metadata(页面 id)交给上层完成跳转,实现真正的服务端驱动导航。
|
||||||
*/
|
*/
|
||||||
@SuppressLint("RestrictedApiAndroidX")
|
@SuppressLint("RestrictedApiAndroidX")
|
||||||
@Composable
|
@Composable
|
||||||
private fun RemoteDocumentView(
|
private fun RemoteDocumentView(
|
||||||
bytes: ByteArray,
|
document: RemoteDocument?,
|
||||||
|
onNavigate: (String) -> Unit,
|
||||||
modifier: Modifier = Modifier,
|
modifier: Modifier = Modifier,
|
||||||
contentKey: Any = bytes.contentHashCode(),
|
|
||||||
) {
|
) {
|
||||||
key(contentKey) {
|
// factory 只执行一次,用 rememberUpdatedState 保证回调始终指向最新的 lambda。
|
||||||
AndroidView(
|
val currentOnNavigate by rememberUpdatedState(onNavigate)
|
||||||
factory = { ctx ->
|
|
||||||
// 仅创建播放器实例
|
AndroidView(
|
||||||
RemoteComposePlayer(ctx)
|
factory = { context -> AntiAliasRemoteComposePlayer(context) },
|
||||||
},
|
update = { view ->
|
||||||
update = { view ->
|
if (document != null && view.tag !== document) {
|
||||||
try {
|
try {
|
||||||
// 1. 将二进制显式解析为 RemoteDocument 对象
|
|
||||||
val document = RemoteDocument(bytes)
|
|
||||||
// 2. 使用 RemoteDocument 重载版本设置文档,确保解析与生命周期同步
|
|
||||||
view.setDocument(document)
|
view.setDocument(document)
|
||||||
Log.d("RemoteUIScreen", "文档解析并加载成功: ${bytes.size} 字节")
|
view.tag = document
|
||||||
|
// 文字绘制会重置画笔,因此在设置文档后再次确保抗锯齿生效。
|
||||||
|
view.enableAntiAliasing()
|
||||||
|
// 播放器只在已设置文档时才会真正登记点击回调,因此必须在 setDocument 之后注册。
|
||||||
|
if (document.document.idActionListeners.isEmpty()) {
|
||||||
|
view.addIdActionListener(
|
||||||
|
RemoteComposePlayer.IdActionCallbacks { actionId, metadata ->
|
||||||
|
if (actionId == ACTION_NAVIGATE && !metadata.isNullOrBlank()) {
|
||||||
|
currentOnNavigate(metadata)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
Log.e("RemoteUIScreen", "文档解析失败", e)
|
Log.e("RemoteUIScreen", "文档加载失败", e)
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
modifier = modifier
|
},
|
||||||
)
|
modifier = modifier
|
||||||
}
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun RemoteUIScreen(page: String, baseUrl: String = RemoteConfig.SERVER_BASE_URL) {
|
fun RemoteUIScreen(
|
||||||
var state by remember(page) { mutableStateOf<RemoteUiState>(RemoteUiState.Loading) }
|
startPage: String = "home",
|
||||||
|
baseUrl: String = RemoteConfig.SERVER_BASE_URL,
|
||||||
|
) {
|
||||||
|
var page by rememberSaveable { mutableStateOf(startPage) }
|
||||||
|
var state by remember { mutableStateOf<RemoteUiState>(RemoteUiState.Loading) }
|
||||||
var retryToken by remember { mutableIntStateOf(0) }
|
var retryToken by remember { mutableIntStateOf(0) }
|
||||||
|
|
||||||
LaunchedEffect(page, retryToken) {
|
LaunchedEffect(page, retryToken) {
|
||||||
|
// 命中缓存时直接渲染,不闪 loading、不重复请求。
|
||||||
|
RemoteDocumentStore.get(page)?.let {
|
||||||
|
state = RemoteUiState.Success(it)
|
||||||
|
return@LaunchedEffect
|
||||||
|
}
|
||||||
state = RemoteUiState.Loading
|
state = RemoteUiState.Loading
|
||||||
state = try {
|
state = try {
|
||||||
val bytes = fetchRemoteUi(page, baseUrl)
|
RemoteUiState.Success(loadDocument(page, baseUrl))
|
||||||
RemoteUiState.Success(bytes)
|
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
Log.e("RemoteUIScreen", "Fetch failed", e)
|
Log.e("RemoteUIScreen", "加载页面 $page 失败", e)
|
||||||
RemoteUiState.Error(e.message ?: "加载失败")
|
RemoteUiState.Error(e.message ?: "加载失败")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {
|
Box(modifier = Modifier.fillMaxSize()) {
|
||||||
when (val s = state) {
|
RemoteDocumentView(
|
||||||
is RemoteUiState.Loading -> CircularProgressIndicator()
|
document = (state as? RemoteUiState.Success)?.document,
|
||||||
is RemoteUiState.Error -> ErrorView(s.message) { retryToken++ }
|
onNavigate = { target -> page = target },
|
||||||
is RemoteUiState.Success -> {
|
modifier = Modifier.fillMaxSize()
|
||||||
RemoteDocumentView(
|
)
|
||||||
bytes = s.bytes,
|
when (val current = state) {
|
||||||
modifier = Modifier.fillMaxSize()
|
is RemoteUiState.Loading ->
|
||||||
)
|
CircularProgressIndicator(Modifier.align(Alignment.Center))
|
||||||
}
|
|
||||||
|
is RemoteUiState.Error -> ErrorView(current.message) { retryToken++ }
|
||||||
|
is RemoteUiState.Success -> Unit
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ spec:
|
|||||||
spec:
|
spec:
|
||||||
containers:
|
containers:
|
||||||
- name: server
|
- name: server
|
||||||
image: composeremote/server:1.0.0-alpha24
|
image: composeremote/server:1.0.0-nav
|
||||||
imagePullPolicy: Never
|
imagePullPolicy: Never
|
||||||
ports:
|
ports:
|
||||||
- name: http
|
- name: http
|
||||||
|
|||||||
+121
-64
@@ -3,90 +3,147 @@ package com.ttstd.composeremote.server
|
|||||||
import androidx.compose.remote.core.CoreDocument
|
import androidx.compose.remote.core.CoreDocument
|
||||||
import androidx.compose.remote.creation.JvmRcPlatformServices
|
import androidx.compose.remote.creation.JvmRcPlatformServices
|
||||||
import androidx.compose.remote.creation.RemoteComposeWriter
|
import androidx.compose.remote.creation.RemoteComposeWriter
|
||||||
import androidx.compose.remote.creation.dsl.RcColumnScope
|
import androidx.compose.remote.creation.dsl.Modifier
|
||||||
|
import androidx.compose.remote.creation.dsl.RcDensityBehavior
|
||||||
|
import androidx.compose.remote.creation.dsl.RcDp
|
||||||
|
import androidx.compose.remote.creation.dsl.RcHeaderTag
|
||||||
import androidx.compose.remote.creation.dsl.RcProfile
|
import androidx.compose.remote.creation.dsl.RcProfile
|
||||||
|
import androidx.compose.remote.creation.dsl.RcScope
|
||||||
import androidx.compose.remote.creation.dsl.RcSp
|
import androidx.compose.remote.creation.dsl.RcSp
|
||||||
|
import androidx.compose.remote.creation.dsl.RcText
|
||||||
|
import androidx.compose.remote.creation.dsl.background
|
||||||
import androidx.compose.remote.creation.dsl.createRcBuffer
|
import androidx.compose.remote.creation.dsl.createRcBuffer
|
||||||
import androidx.compose.remote.creation.dsl.fillMaxSize
|
import androidx.compose.remote.creation.dsl.fillMaxSize
|
||||||
import androidx.compose.remote.creation.dsl.fillMaxWidth
|
import androidx.compose.remote.creation.dsl.fillMaxWidth
|
||||||
|
import androidx.compose.remote.creation.dsl.height
|
||||||
|
import androidx.compose.remote.creation.dsl.onClick
|
||||||
import androidx.compose.remote.creation.dsl.padding
|
import androidx.compose.remote.creation.dsl.padding
|
||||||
import androidx.compose.remote.creation.dsl.background
|
|
||||||
import androidx.compose.remote.creation.dsl.Modifier
|
|
||||||
import androidx.compose.remote.creation.profile.Profile
|
import androidx.compose.remote.creation.profile.Profile
|
||||||
import androidx.compose.remote.creation.profile.RemoteComposeWriterFactory
|
import androidx.compose.remote.creation.profile.RemoteComposeWriterFactory
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 文档逻辑画布尺寸,单位 dp。
|
||||||
|
*
|
||||||
|
* 实际像素由播放端根据设备密度换算(DOC_DENSITY_BEHAVIOR = DP),
|
||||||
|
* 因此换设备 / 换窗口时不会拉伸,也不需要服务端写死某个机型的分辨率。
|
||||||
|
*/
|
||||||
|
private const val CANVAS_WIDTH_DP = 400
|
||||||
|
private const val CANVAS_HEIGHT_DP = 900
|
||||||
|
|
||||||
|
/** 服务端与播放端约定的 action id:跳转到 metadata 里指定的页面。 */
|
||||||
|
private const val ACTION_NAVIGATE = 1
|
||||||
|
|
||||||
fun buildRemoteDocument(page: String): ByteArray {
|
fun buildRemoteDocument(page: String): ByteArray {
|
||||||
val writerFactory = RemoteComposeWriterFactory { _, _, _ ->
|
lateinit var profile: Profile
|
||||||
RemoteComposeWriter(1080, 1920, "remote-ui", JvmRcPlatformServices())
|
profile = Profile(
|
||||||
}
|
|
||||||
val profile = Profile(
|
|
||||||
CoreDocument.DOCUMENT_API_LEVEL,
|
CoreDocument.DOCUMENT_API_LEVEL,
|
||||||
CoreDocument.PROFILE,
|
CoreDocument.PROFILE,
|
||||||
JvmRcPlatformServices(),
|
JvmRcPlatformServices(),
|
||||||
writerFactory
|
RemoteComposeWriterFactory { displayInfo, _, _ ->
|
||||||
|
RemoteComposeWriter(displayInfo, "remote-ui", profile)
|
||||||
|
}
|
||||||
)
|
)
|
||||||
val rcProfile = RcProfile(profile, false)
|
val rcProfile = RcProfile(profile, false)
|
||||||
return createRcBuffer(rcProfile) {
|
|
||||||
//加了RcRoot会不显示
|
// 画布尺寸与密度语义都放在文档头部,由播放端按设备密度解释 dp / sp。
|
||||||
// RcRoot {
|
val header = arrayOf(
|
||||||
Column(Modifier.fillMaxSize().background(0xFFFFFFFF.toInt()).padding(16f)) {
|
RcHeaderTag.docWidth(CANVAS_WIDTH_DP),
|
||||||
when (page) {
|
RcHeaderTag.docHeight(CANVAS_HEIGHT_DP),
|
||||||
"detail" -> detailContent()
|
RcHeaderTag.densityBehavior(RcDensityBehavior.Dp)
|
||||||
else -> homeContent()
|
)
|
||||||
}
|
|
||||||
|
return createRcBuffer(rcProfile, *header) {
|
||||||
|
// 跳转目标以文本形式随文档下发,播放端只负责把 metadata 当作页面 id 使用。
|
||||||
|
val homeTarget = remoteText("home")
|
||||||
|
val detailTarget = remoteText("detail")
|
||||||
|
|
||||||
|
Column(
|
||||||
|
Modifier.fillMaxSize().background(WINDOW_BG).padding(RcDp(16f))
|
||||||
|
) {
|
||||||
|
when (page) {
|
||||||
|
"detail" -> detailContent(homeTarget)
|
||||||
|
else -> homeContent(detailTarget)
|
||||||
}
|
}
|
||||||
// }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun RcColumnScope.homeContent() {
|
|
||||||
Text(
|
|
||||||
text = "Remote Compose",
|
|
||||||
fontSize = RcSp(36f),
|
|
||||||
color = TITLE_COLOR,
|
|
||||||
modifier = Modifier.padding(0f, 0f, 0f, 4f)
|
|
||||||
)
|
|
||||||
Text(
|
|
||||||
text = "服务端驱动 UI · 演示",
|
|
||||||
fontSize = RcSp(18f),
|
|
||||||
color = BODY_COLOR,
|
|
||||||
modifier = Modifier.padding(0f, 0f, 0f, 16f)
|
|
||||||
)
|
|
||||||
Spacer(Modifier)
|
|
||||||
card(title = "首页", body = "这个界面由后端实时生成并下发二进制文档。")
|
|
||||||
Spacer(Modifier)
|
|
||||||
card(title = "技术栈", body = "Spring Boot 4 + Remote Compose 1.0.0-alpha24")
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun RcColumnScope.detailContent() {
|
|
||||||
Text(
|
|
||||||
text = "详情页",
|
|
||||||
fontSize = RcSp(36f),
|
|
||||||
color = TITLE_COLOR,
|
|
||||||
modifier = Modifier.padding(0f, 0f, 0f, 4f)
|
|
||||||
)
|
|
||||||
Text(
|
|
||||||
text = "Detail",
|
|
||||||
fontSize = RcSp(18f),
|
|
||||||
color = BODY_COLOR,
|
|
||||||
modifier = Modifier.padding(0f, 0f, 0f, 16f)
|
|
||||||
)
|
|
||||||
Spacer(Modifier)
|
|
||||||
card(title = "接口", body = "GET /api/remote-ui/detail")
|
|
||||||
Spacer(Modifier)
|
|
||||||
card(title = "说明", body = "每个页面对应后端的一个独立接口,按需下发。")
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun RcColumnScope.card(title: String, body: String) {
|
|
||||||
Box(Modifier.fillMaxWidth().background(CARD_BG).padding(16f)) {
|
|
||||||
Column(Modifier.fillMaxWidth()) {
|
|
||||||
Text(text = title, fontSize = RcSp(22f), color = TITLE_COLOR, modifier = Modifier)
|
|
||||||
Spacer(Modifier)
|
|
||||||
Text(text = body, fontSize = RcSp(15f), color = BODY_COLOR, modifier = Modifier)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun RcScope.homeContent(detailTarget: RcText) {
|
||||||
|
Text(
|
||||||
|
text = "Remote Compose",
|
||||||
|
fontSize = RcSp(28f),
|
||||||
|
color = TITLE_COLOR
|
||||||
|
)
|
||||||
|
Spacer(Modifier.height(RcDp(4f)))
|
||||||
|
Text(
|
||||||
|
text = "服务端驱动 UI · 演示",
|
||||||
|
fontSize = RcSp(14f),
|
||||||
|
color = BODY_COLOR
|
||||||
|
)
|
||||||
|
Spacer(Modifier.height(RcDp(16f)))
|
||||||
|
card(
|
||||||
|
title = "首页",
|
||||||
|
body = "这个界面由后端实时生成并下发二进制文档。",
|
||||||
|
hint = "点击查看详情 →",
|
||||||
|
target = detailTarget
|
||||||
|
)
|
||||||
|
Spacer(Modifier.height(RcDp(12f)))
|
||||||
|
card(
|
||||||
|
title = "技术栈",
|
||||||
|
body = "Spring Boot 4 + Remote Compose 1.0.0-alpha24",
|
||||||
|
hint = "点击查看详情 →",
|
||||||
|
target = detailTarget
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun RcScope.detailContent(homeTarget: RcText) {
|
||||||
|
Text(
|
||||||
|
text = "详情页",
|
||||||
|
fontSize = RcSp(28f),
|
||||||
|
color = TITLE_COLOR
|
||||||
|
)
|
||||||
|
Spacer(Modifier.height(RcDp(4f)))
|
||||||
|
Text(
|
||||||
|
text = "Detail",
|
||||||
|
fontSize = RcSp(14f),
|
||||||
|
color = BODY_COLOR
|
||||||
|
)
|
||||||
|
Spacer(Modifier.height(RcDp(16f)))
|
||||||
|
card(
|
||||||
|
title = "接口",
|
||||||
|
body = "GET /api/remote-ui/detail",
|
||||||
|
hint = "点击返回首页 →",
|
||||||
|
target = homeTarget
|
||||||
|
)
|
||||||
|
Spacer(Modifier.height(RcDp(12f)))
|
||||||
|
card(
|
||||||
|
title = "说明",
|
||||||
|
body = "每个页面对应后端的一个独立接口,按需下发。",
|
||||||
|
hint = "点击返回首页 →",
|
||||||
|
target = homeTarget
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun RcScope.card(title: String, body: String, hint: String, target: RcText) {
|
||||||
|
Box(
|
||||||
|
Modifier
|
||||||
|
.fillMaxWidth()
|
||||||
|
.background(CARD_BG)
|
||||||
|
.padding(RcDp(16f))
|
||||||
|
.onClick { hostAction(ACTION_NAVIGATE, target) }
|
||||||
|
) {
|
||||||
|
Column(Modifier.fillMaxWidth()) {
|
||||||
|
Text(text = title, fontSize = RcSp(18f), color = TITLE_COLOR)
|
||||||
|
Spacer(Modifier.height(RcDp(8f)))
|
||||||
|
Text(text = body, fontSize = RcSp(14f), color = BODY_COLOR)
|
||||||
|
Spacer(Modifier.height(RcDp(8f)))
|
||||||
|
Text(text = hint, fontSize = RcSp(13f), color = ACCENT_COLOR)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private const val WINDOW_BG = 0xFFFFFFFF.toInt()
|
||||||
private const val TITLE_COLOR = 0xFF0D47A1.toInt()
|
private const val TITLE_COLOR = 0xFF0D47A1.toInt()
|
||||||
private const val BODY_COLOR = 0xFF212121.toInt()
|
private const val BODY_COLOR = 0xFF212121.toInt()
|
||||||
|
private const val ACCENT_COLOR = 0xFF1565C0.toInt()
|
||||||
private const val CARD_BG = 0xFFE3F2FD.toInt()
|
private const val CARD_BG = 0xFFE3F2FD.toInt()
|
||||||
@@ -4,6 +4,3 @@ server:
|
|||||||
spring:
|
spring:
|
||||||
application:
|
application:
|
||||||
name: ComposeRemoteServer
|
name: ComposeRemoteServer
|
||||||
threads:
|
|
||||||
virtual:
|
|
||||||
enabled: true # 开启虚拟线程
|
|
||||||
Reference in new issue
Block a user