feat: 新增布局模型与桌面编译闭环
- 服务端新增 LayoutDocument/LayoutItem(12 列网格、千分比坐标) - LayoutStore 内存存储 + 默认桌面布局 - LayoutCompiler 行分组编译为 Row + weight,图片预注册真实像素尺寸 - 新增 desktop 页面路由与首页入口,部署 1.0.0-layout2
This commit is contained in:
1 parent
244e539b47
commit
484380b11a
6 files changed
+431
-3
No files matched your search
@@ -22,7 +22,7 @@ spec:
|
||||
spec:
|
||||
containers:
|
||||
- name: server
|
||||
image: composeremote/server:1.0.0-imgcache5
|
||||
image: composeremote/server:1.0.0-layout2
|
||||
imagePullPolicy: Never
|
||||
ports:
|
||||
- name: http
|
||||
|
||||
+8
-2
@@ -1,6 +1,7 @@
|
||||
package com.ttstd.composeremote.server.controller;
|
||||
|
||||
import com.ttstd.composeremote.server.RemoteDocumentBuilderKt;
|
||||
import com.ttstd.composeremote.server.layout.LayoutPages;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.http.CacheControl;
|
||||
import org.springframework.http.HttpStatus;
|
||||
@@ -40,8 +41,13 @@ public class RemoteUIController {
|
||||
)
|
||||
public ResponseEntity<byte[]> getPage(@PathVariable(name = "page", required = false) String page) {
|
||||
String target = (page == null || page.isBlank()) ? "home" : page;
|
||||
byte[] document = RemoteDocumentBuilderKt.buildRemoteDocument(target);
|
||||
List<String> assets = RemoteDocumentBuilderKt.buildRemoteDocumentAssets(target);
|
||||
boolean layoutPage = LayoutPages.DESKTOP.equals(target);
|
||||
byte[] document = layoutPage
|
||||
? LayoutPages.buildDocument(target)
|
||||
: RemoteDocumentBuilderKt.buildRemoteDocument(target);
|
||||
List<String> assets = layoutPage
|
||||
? LayoutPages.assets(target)
|
||||
: RemoteDocumentBuilderKt.buildRemoteDocumentAssets(target);
|
||||
|
||||
ResponseEntity.BodyBuilder builder = ResponseEntity.ok()
|
||||
.contentType(MediaType.APPLICATION_OCTET_STREAM);
|
||||
|
||||
+8
@@ -23,6 +23,7 @@ import androidx.compose.remote.creation.dsl.verticalScroll
|
||||
import androidx.compose.remote.creation.dsl.width
|
||||
import androidx.compose.remote.creation.profile.Profile
|
||||
import androidx.compose.remote.creation.profile.RemoteComposeWriterFactory
|
||||
import com.ttstd.composeremote.server.layout.DESKTOP_PAGE
|
||||
import javax.imageio.ImageIO
|
||||
|
||||
/**
|
||||
@@ -134,6 +135,13 @@ private fun RcScope.homeContent(detailTarget: RcText, listTarget: RcText) {
|
||||
target = listTarget
|
||||
)
|
||||
Spacer(Modifier.height(RcDp(12f)))
|
||||
card(
|
||||
title = "桌面",
|
||||
body = "12 列网格 + 千分比坐标,由服务端布局模型编译生成。",
|
||||
hint = "进入桌面 →",
|
||||
target = remoteText(DESKTOP_PAGE)
|
||||
)
|
||||
Spacer(Modifier.height(RcDp(12f)))
|
||||
card(
|
||||
title = "新 Activity",
|
||||
body = "这张卡片会在一个新的 Activity 中打开详情页。",
|
||||
|
||||
+251
@@ -0,0 +1,251 @@
|
||||
package com.ttstd.composeremote.server.layout
|
||||
|
||||
import androidx.compose.remote.core.CoreDocument
|
||||
import androidx.compose.remote.creation.JvmRcPlatformServices
|
||||
import androidx.compose.remote.creation.RemoteComposeWriter
|
||||
import androidx.compose.remote.creation.dsl.Modifier
|
||||
import androidx.compose.remote.creation.dsl.RcContentScale
|
||||
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.RcRowScope
|
||||
import androidx.compose.remote.creation.dsl.RcScope
|
||||
import androidx.compose.remote.creation.dsl.RcSp
|
||||
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.width
|
||||
import androidx.compose.remote.creation.profile.Profile
|
||||
import androidx.compose.remote.creation.profile.RemoteComposeWriterFactory
|
||||
import kotlin.math.roundToInt
|
||||
|
||||
/** 文档逻辑宽度,单位 dp;高度按画布宽高比换算。 */
|
||||
private const val DOC_WIDTH_DP = 400f
|
||||
|
||||
/** 页面内边距,单位 dp。 */
|
||||
private const val PAGE_PADDING_DP = 16f
|
||||
|
||||
/** 与播放端约定的 action id,与 RemoteDocumentBuilder 保持一致。 */
|
||||
private const val ACTION_NAVIGATE = 1
|
||||
private const val ACTION_OPEN_ACTIVITY = 2
|
||||
|
||||
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()
|
||||
|
||||
/**
|
||||
* 布局文档对外的编译入口。
|
||||
*
|
||||
* 页面 id 与存储解耦:控制器只认页面 id,具体的布局从 [LayoutStore] 取,
|
||||
* 再交给 [LayoutCompiler] 编译成二进制文档。
|
||||
*/
|
||||
object LayoutPages {
|
||||
|
||||
/** 桌面页 id,供 Java 控制器判断是否走布局编译。 */
|
||||
const val DESKTOP = DESKTOP_PAGE
|
||||
|
||||
/** 编译指定页面;页面不存在时抛异常,由控制器转成 404/500。 */
|
||||
@JvmStatic
|
||||
fun buildDocument(page: String): ByteArray {
|
||||
val document = LayoutStore.get(page) ?: error("未知布局页: $page")
|
||||
return LayoutCompiler.compile(document)
|
||||
}
|
||||
|
||||
/** 页面引用的图片地址,供 `X-Remote-Assets` 响应头下发。 */
|
||||
@JvmStatic
|
||||
fun assets(page: String): List<String> = LayoutStore.get(page)?.items.orEmpty()
|
||||
.filter { it.type == ItemType.IMAGE }
|
||||
.mapNotNull { it.props["url"] }
|
||||
.distinct()
|
||||
}
|
||||
|
||||
/**
|
||||
* 布局编译器:把千分比坐标的组件编译成 Remote Compose 文档。
|
||||
*
|
||||
* 纵向重叠的组件归为一行,行内按 x 排序后用 `Row + weight` 布局:
|
||||
* - 组件宽度 → `Modifier.weight(w)`,保持相对比例;
|
||||
* - 组件间隙 → 固定宽度的 `Spacer`(按画布宽度换算成 dp);
|
||||
* - 行间距 → 固定高度的 `Spacer`(按画布高度换算成 dp)。
|
||||
*/
|
||||
object LayoutCompiler {
|
||||
|
||||
fun compile(document: LayoutDocument): ByteArray {
|
||||
val canvasHeightDp =
|
||||
DOC_WIDTH_DP * (document.canvas.aspectHeight / document.canvas.aspectWidth).toFloat()
|
||||
val contentWidthDp = DOC_WIDTH_DP - PAGE_PADDING_DP * 2
|
||||
val rows = groupRows(document.items)
|
||||
|
||||
lateinit var profile: Profile
|
||||
profile = Profile(
|
||||
CoreDocument.DOCUMENT_API_LEVEL,
|
||||
CoreDocument.PROFILE,
|
||||
JvmRcPlatformServices(),
|
||||
RemoteComposeWriterFactory { displayInfo, _, _ ->
|
||||
RemoteComposeWriter(displayInfo, "remote-ui", profile)
|
||||
}
|
||||
)
|
||||
val rcProfile = RcProfile(profile, false)
|
||||
|
||||
// 画布尺寸与密度语义放在文档头部,由播放端按设备密度解释 dp / sp。
|
||||
val header = arrayOf(
|
||||
RcHeaderTag.docWidth(DOC_WIDTH_DP.roundToInt()),
|
||||
RcHeaderTag.docHeight(canvasHeightDp.roundToInt()),
|
||||
RcHeaderTag.densityBehavior(RcDensityBehavior.Dp)
|
||||
)
|
||||
|
||||
return createRcBuffer(rcProfile, *header) {
|
||||
val writer = composeWriter()
|
||||
// URL 图片必须先按真实像素尺寸登记,否则播放端解码会报尺寸不匹配。
|
||||
document.items
|
||||
.filter { it.type == ItemType.IMAGE }
|
||||
.forEach { item ->
|
||||
val url = item.props["url"] ?: return@forEach
|
||||
val pixelWidth = item.props["pixelWidth"]?.toIntOrNull() ?: return@forEach
|
||||
val pixelHeight = item.props["pixelHeight"]?.toIntOrNull() ?: return@forEach
|
||||
writer?.addBitmapUrl(url, pixelWidth, pixelHeight)
|
||||
}
|
||||
|
||||
Column(
|
||||
Modifier.fillMaxSize().background(document.backgroundColor)
|
||||
.padding(RcDp(PAGE_PADDING_DP))
|
||||
) {
|
||||
var previousBottom = rows.firstOrNull()?.top ?: 0
|
||||
rows.forEach { row ->
|
||||
val gapDp = (row.top - previousBottom).coerceAtLeast(0) / 1000f * canvasHeightDp
|
||||
if (gapDp > 0f) {
|
||||
Spacer(Modifier.height(RcDp(gapDp)))
|
||||
}
|
||||
Row(Modifier.fillMaxWidth()) {
|
||||
var previousRight = 0
|
||||
row.items.forEach { item ->
|
||||
val gapPm = item.x - previousRight
|
||||
if (gapPm > 0) {
|
||||
Spacer(Modifier.width(RcDp(gapPm / 1000f * contentWidthDp)))
|
||||
}
|
||||
itemContent(item, item.h / 1000f * canvasHeightDp)
|
||||
previousRight = item.x + item.w
|
||||
}
|
||||
}
|
||||
previousBottom = row.bottom
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** 渲染单个组件的可见内容(位置与宽度由外层 Row 负责)。 */
|
||||
private fun RcRowScope.itemContent(item: LayoutItem, heightDp: Float) {
|
||||
val base = Modifier.weight(item.w.toFloat()).height(RcDp(heightDp))
|
||||
when (item.type) {
|
||||
ItemType.TEXT -> Box(base) {
|
||||
Text(
|
||||
text = item.props["text"].orEmpty(),
|
||||
fontSize = RcSp(item.props["fontSize"]?.toFloatOrNull() ?: 15f),
|
||||
color = TITLE_COLOR
|
||||
)
|
||||
}
|
||||
|
||||
ItemType.CARD -> {
|
||||
val action = item.action
|
||||
// remoteText 是 RcScope 的成员,onClick 的 lambda 里拿不到隐式 receiver,
|
||||
// 因此提前把跳转目标转成文本 id。
|
||||
val target = action?.let { remoteText(it.target) }
|
||||
val clickable = if (action != null && target != null) {
|
||||
base.onClick {
|
||||
hostAction(
|
||||
if (action.type == ActionType.OPEN_ACTIVITY) ACTION_OPEN_ACTIVITY else ACTION_NAVIGATE,
|
||||
target
|
||||
)
|
||||
}
|
||||
} else {
|
||||
base
|
||||
}
|
||||
Box(clickable.background(CARD_BG).padding(RcDp(10f))) {
|
||||
Column(Modifier.fillMaxWidth()) {
|
||||
Text(
|
||||
text = item.props["title"].orEmpty(),
|
||||
fontSize = RcSp(16f),
|
||||
color = TITLE_COLOR
|
||||
)
|
||||
Spacer(Modifier.height(RcDp(4f)))
|
||||
Text(
|
||||
text = item.props["body"].orEmpty(),
|
||||
fontSize = RcSp(13f),
|
||||
color = BODY_COLOR
|
||||
)
|
||||
item.props["hint"]?.let { hint ->
|
||||
Spacer(Modifier.height(RcDp(4f)))
|
||||
Text(text = hint, fontSize = RcSp(12f), color = ACCENT_COLOR)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ItemType.IMAGE -> {
|
||||
val url = item.props["url"].orEmpty()
|
||||
if (url.isEmpty()) {
|
||||
Box(base.background(CARD_BG)) {}
|
||||
} else {
|
||||
Image(
|
||||
remoteBitmapUrl(url),
|
||||
base,
|
||||
item.id,
|
||||
RcContentScale.Crop,
|
||||
1f
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** 编译期的行:组件的纵向区间 + 行内按 x 排序后的组件。 */
|
||||
private class LayoutRow(val top: Int, val bottom: Int, val items: List<LayoutItem>)
|
||||
|
||||
/**
|
||||
* 行分组:按 y 升序扫描,与当前行纵向区间重叠的组件归入同一行。
|
||||
*
|
||||
* 采用「与行底比较」的贪心策略即可覆盖简单网格:同行的组件 y 相同或接近,
|
||||
* 换行的组件 y 必然不早于上一行底部。
|
||||
*/
|
||||
private fun groupRows(items: List<LayoutItem>): List<LayoutRow> {
|
||||
if (items.isEmpty()) return emptyList()
|
||||
val sorted = items.sortedWith(compareBy({ it.y }, { it.x }))
|
||||
val groups = mutableListOf<MutableList<LayoutItem>>()
|
||||
var currentBottom = 0
|
||||
|
||||
sorted.forEach { item ->
|
||||
if (groups.isEmpty() || item.y >= currentBottom) {
|
||||
groups.add(mutableListOf(item))
|
||||
currentBottom = item.y + item.h
|
||||
} else {
|
||||
groups.last().add(item)
|
||||
currentBottom = maxOf(currentBottom, item.y + item.h)
|
||||
}
|
||||
}
|
||||
|
||||
return groups.map { group ->
|
||||
LayoutRow(
|
||||
top = group.minOf { it.y },
|
||||
bottom = group.maxOf { it.y + it.h },
|
||||
items = group.sortedBy { it.x }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 取出 DSL 背后的 [RemoteComposeWriter],用于登记图片真实尺寸。
|
||||
*
|
||||
* `createRcBuffer` 不会调用 Profile 里的 writer 工厂,alpha24 也没有公开入口,
|
||||
* 这里读 `RcScopeImpl` 的 writer 字段;取不到时返回 null(图片退回 1×1 的旧行为)。
|
||||
*/
|
||||
private fun RcScope.composeWriter(): RemoteComposeWriter? = runCatching {
|
||||
val field = javaClass.getDeclaredField("writer")
|
||||
field.isAccessible = true
|
||||
field.get(this) as RemoteComposeWriter
|
||||
}.getOrNull()
|
||||
+50
@@ -0,0 +1,50 @@
|
||||
package com.ttstd.composeremote.server.layout
|
||||
|
||||
/** 桌面页的页面 id,与客户端 `GET /api/remote-ui/{page}` 中的 path 一致。 */
|
||||
const val DESKTOP_PAGE = "desktop"
|
||||
|
||||
/**
|
||||
* 服务端布局模型。
|
||||
*
|
||||
* 坐标全部使用相对千分比(0..1000),与设备分辨率 / 密度无关:
|
||||
* [LayoutItem.x] / [LayoutItem.w] 相对画布宽度,[LayoutItem.y] / [LayoutItem.h] 相对画布高度。
|
||||
* 由 [LayoutCompiler] 编译成 Remote Compose 二进制文档后下发。
|
||||
*/
|
||||
data class LayoutDocument(
|
||||
val id: String,
|
||||
val title: String,
|
||||
val canvas: CanvasSpec = CanvasSpec(),
|
||||
val backgroundColor: Int = 0xFFFFFFFF.toInt(),
|
||||
val items: List<LayoutItem> = emptyList(),
|
||||
)
|
||||
|
||||
/**
|
||||
* 画布规格。
|
||||
*
|
||||
* 宽高比固定(默认 9:19.5),播放端按设备密度换算 dp;
|
||||
* [columns] 目前只用于编辑器吸附,编译时按组件的 w 换算成 Row 的 weight。
|
||||
*/
|
||||
data class CanvasSpec(
|
||||
val aspectWidth: Double = 9.0,
|
||||
val aspectHeight: Double = 19.5,
|
||||
val columns: Int = 12,
|
||||
)
|
||||
|
||||
/** 单个组件,位置与尺寸都是千分比。 */
|
||||
data class LayoutItem(
|
||||
val id: String,
|
||||
val type: ItemType,
|
||||
val x: Int,
|
||||
val y: Int,
|
||||
val w: Int,
|
||||
val h: Int,
|
||||
val props: Map<String, String> = emptyMap(),
|
||||
val action: LayoutAction? = null,
|
||||
)
|
||||
|
||||
enum class ItemType { TEXT, CARD, IMAGE }
|
||||
|
||||
/** 点击行为:页内跳转,或交给新的 Activity 打开。 */
|
||||
data class LayoutAction(val type: ActionType, val target: String)
|
||||
|
||||
enum class ActionType { NAVIGATE, OPEN_ACTIVITY }
|
||||
+113
@@ -0,0 +1,113 @@
|
||||
package com.ttstd.composeremote.server.layout
|
||||
|
||||
/**
|
||||
* 布局文档的内存存储。
|
||||
*
|
||||
* 当前先内置一份桌面布局,后续接入后台编辑器时,把保存结果 [put] 进来即可,
|
||||
* 页面接口仍然走 [LayoutCompiler] 编译,客户端无需感知存储实现。
|
||||
*/
|
||||
object LayoutStore {
|
||||
|
||||
private val layouts = mutableMapOf<String, LayoutDocument>()
|
||||
|
||||
init {
|
||||
put(desktopLayout())
|
||||
}
|
||||
|
||||
fun get(id: String): LayoutDocument? = layouts[id]
|
||||
|
||||
fun put(document: LayoutDocument) {
|
||||
layouts[document.id] = document
|
||||
}
|
||||
|
||||
/**
|
||||
* 默认桌面:12 列网格 + 千分比坐标的示例布局。
|
||||
*
|
||||
* 换算到 400dp 宽画布后内容约 716dp 高,加上内边距仍在手机可见区域内
|
||||
* (播放器被状态栏 / 导航栏内缩后约 765dp 高)。
|
||||
*/
|
||||
private fun desktopLayout(): LayoutDocument = LayoutDocument(
|
||||
id = DESKTOP_PAGE,
|
||||
title = "桌面",
|
||||
canvas = CanvasSpec(aspectWidth = 9.0, aspectHeight = 19.5, columns = 12),
|
||||
items = listOf(
|
||||
LayoutItem(
|
||||
id = "title",
|
||||
type = ItemType.TEXT,
|
||||
x = 0, y = 0, w = 1000, h = 64,
|
||||
props = mapOf("text" to "Remote UI 桌面", "fontSize" to "30")
|
||||
),
|
||||
LayoutItem(
|
||||
id = "subtitle",
|
||||
type = ItemType.TEXT,
|
||||
x = 0, y = 66, w = 1000, h = 38,
|
||||
props = mapOf("text" to "12 列网格 · 千分比坐标 · 服务端编译", "fontSize" to "14")
|
||||
),
|
||||
|
||||
LayoutItem(
|
||||
id = "grid-card",
|
||||
type = ItemType.CARD,
|
||||
x = 0, y = 112, w = 310, h = 132,
|
||||
props = mapOf("title" to "12 列网格", "body" to "x、w 千分比")
|
||||
),
|
||||
LayoutItem(
|
||||
id = "row-card",
|
||||
type = ItemType.CARD,
|
||||
x = 345, y = 112, w = 310, h = 132,
|
||||
props = mapOf("title" to "行分组", "body" to "重叠即同行")
|
||||
),
|
||||
LayoutItem(
|
||||
id = "weight-card",
|
||||
type = ItemType.CARD,
|
||||
x = 690, y = 112, w = 310, h = 132,
|
||||
props = mapOf("title" to "Row 权重", "body" to "w → weight")
|
||||
),
|
||||
|
||||
LayoutItem(
|
||||
id = "banner",
|
||||
type = ItemType.IMAGE,
|
||||
x = 0, y = 262, w = 1000, h = 240,
|
||||
props = mapOf(
|
||||
"url" to "https://ttstd.com/api/remote-ui/assets/spike-image.png",
|
||||
"pixelWidth" to "800",
|
||||
"pixelHeight" to "450"
|
||||
)
|
||||
),
|
||||
|
||||
LayoutItem(
|
||||
id = "detail-card",
|
||||
type = ItemType.CARD,
|
||||
x = 0, y = 520, w = 480, h = 156,
|
||||
props = mapOf(
|
||||
"title" to "详情",
|
||||
"body" to "跳转目标由文档 metadata 决定",
|
||||
"hint" to "打开详情 →"
|
||||
),
|
||||
action = LayoutAction(ActionType.NAVIGATE, "detail")
|
||||
),
|
||||
LayoutItem(
|
||||
id = "activity-card",
|
||||
type = ItemType.CARD,
|
||||
x = 520, y = 520, w = 480, h = 156,
|
||||
props = mapOf(
|
||||
"title" to "新 Activity",
|
||||
"body" to "在新窗口打开详情页",
|
||||
"hint" to "打开 →"
|
||||
),
|
||||
action = LayoutAction(ActionType.OPEN_ACTIVITY, "detail")
|
||||
),
|
||||
|
||||
LayoutItem(
|
||||
id = "list-card",
|
||||
type = ItemType.CARD,
|
||||
x = 0, y = 694, w = 1000, h = 132,
|
||||
props = mapOf(
|
||||
"title" to "列表",
|
||||
"body" to "布局页同样可以跳转到列表。",
|
||||
"hint" to "打开列表 →"
|
||||
),
|
||||
action = LayoutAction(ActionType.NAVIGATE, "list")
|
||||
)
|
||||
)
|
||||
)
|
||||
}
|
||||
Reference in new issue
Block a user