Files
2026-10-22 13:26:00 +08:00

36 lines
1.7 KiB
Plaintext

class Policy {
width: Number
function narrow(self: Policy, event: Event): Unit { self.width = 280 }
function wide(self: Policy, event: Event): Unit { self.width = 560 }
}
class ArrangedColumn: Column {
policy: Policy
function arrange(self: ArrangedColumn, mount: Mount, geometry: Bounds): Arrangement {
result = Column.arrange(self, mount, geometry)
result.children.at(0).width = self.policy.width
return result
}
}
class MeasuredColumn: Column {
width: Number
function measure(self: MeasuredColumn, mount: Mount, constraints: Constraints): MeasureResult {
result = Column.measure(self, mount, constraints)
result.width = self.width
return result
}
}
policy = Policy(280)
paragraph = TextBlock("源码 arrange 改变分配宽度后,文字会按实际宽度重新测量。增加的高度继续影响容器和相邻对象,直到整棵布局稳定后才发布。这里与下面使用的是同一个 TextBlock;每个 Mount 都有自己的约束与几何。同一套内存对象不需要复制业务状态,也不通过丢弃传播来结束更新。")
tail = TextBlock("这一行跟随上方段落。")
arranged = ArrangedColumn(0, 12, paragraph, tail, policy)
measured = MeasuredColumn(0, 12, paragraph, TextBlock("这一段的宽度来自源码 measure。"), 360)
narrow = Button("arrange 为 280")
wide = Button("arrange 为 560")
narrowConnection = narrow.on("click", policy.narrow)
wideConnection = wide.on("click", policy.wide)
page = Page()
page.title = "约束协商"
page.render(Column(24, 16, Row(0, 12, narrow, wide), arranged, measured))
ui.window().resize(850, 700)
ui.window().setTitle("测量与排列直到稳定")