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
@@ -22,7 +22,7 @@ spec:
|
||||
spec:
|
||||
containers:
|
||||
- name: server
|
||||
image: composeremote/server:1.0.0-alpha24
|
||||
image: composeremote/server:1.0.0-nav
|
||||
imagePullPolicy: Never
|
||||
ports:
|
||||
- name: http
|
||||
|
||||
+121
-64
@@ -3,90 +3,147 @@ package com.ttstd.composeremote.server
|
||||
import androidx.compose.remote.core.CoreDocument
|
||||
import androidx.compose.remote.creation.JvmRcPlatformServices
|
||||
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.RcScope
|
||||
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.fillMaxSize
|
||||
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.background
|
||||
import androidx.compose.remote.creation.dsl.Modifier
|
||||
import androidx.compose.remote.creation.profile.Profile
|
||||
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 {
|
||||
val writerFactory = RemoteComposeWriterFactory { _, _, _ ->
|
||||
RemoteComposeWriter(1080, 1920, "remote-ui", JvmRcPlatformServices())
|
||||
}
|
||||
val profile = Profile(
|
||||
lateinit var profile: Profile
|
||||
profile = Profile(
|
||||
CoreDocument.DOCUMENT_API_LEVEL,
|
||||
CoreDocument.PROFILE,
|
||||
JvmRcPlatformServices(),
|
||||
writerFactory
|
||||
RemoteComposeWriterFactory { displayInfo, _, _ ->
|
||||
RemoteComposeWriter(displayInfo, "remote-ui", profile)
|
||||
}
|
||||
)
|
||||
val rcProfile = RcProfile(profile, false)
|
||||
return createRcBuffer(rcProfile) {
|
||||
//加了RcRoot会不显示
|
||||
// RcRoot {
|
||||
Column(Modifier.fillMaxSize().background(0xFFFFFFFF.toInt()).padding(16f)) {
|
||||
when (page) {
|
||||
"detail" -> detailContent()
|
||||
else -> homeContent()
|
||||
}
|
||||
|
||||
// 画布尺寸与密度语义都放在文档头部,由播放端按设备密度解释 dp / sp。
|
||||
val header = arrayOf(
|
||||
RcHeaderTag.docWidth(CANVAS_WIDTH_DP),
|
||||
RcHeaderTag.docHeight(CANVAS_HEIGHT_DP),
|
||||
RcHeaderTag.densityBehavior(RcDensityBehavior.Dp)
|
||||
)
|
||||
|
||||
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 BODY_COLOR = 0xFF212121.toInt()
|
||||
private const val ACCENT_COLOR = 0xFF1565C0.toInt()
|
||||
private const val CARD_BG = 0xFFE3F2FD.toInt()
|
||||
@@ -4,6 +4,3 @@ server:
|
||||
spring:
|
||||
application:
|
||||
name: ComposeRemoteServer
|
||||
threads:
|
||||
virtual:
|
||||
enabled: true # 开启虚拟线程
|
||||
Reference in new issue
Block a user