merge: resolve manuscript conflicts with latest main comments
This commit is contained in:
@@ -79,6 +79,7 @@ impl CrossAlign {
|
||||
|
||||
/// 根据交叉轴对齐方式计算有限剩余空间中的偏移。
|
||||
fn offset(self, available: f32, occupied: f32) -> f32 {
|
||||
// 溢出时剩余空间归零,Center/End 都退化为起点对齐,避免把内容前缘移出可达滚动范围。
|
||||
let spare = (available - occupied).max(0.0);
|
||||
match self {
|
||||
Self::Start | Self::Stretch => 0.0,
|
||||
@@ -125,9 +126,11 @@ impl MainAlign {
|
||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||
/// 主轴伸缩策略与限值,不能用一个宽度常量替代完整分配规则。
|
||||
pub struct FlexPolicy {
|
||||
// Some 指定主轴首选基准,None 使用首次内容测量;grow/fill 的缺省基准在 flex_sizes 中解释为零。
|
||||
pub basis: Option<f32>,
|
||||
pub min: f32,
|
||||
pub max: f32,
|
||||
// 收缩权重还会乘 basis;默认零表示空间不足时不主动压缩该项。
|
||||
pub shrink: f32,
|
||||
}
|
||||
impl Default for FlexPolicy {
|
||||
@@ -170,7 +173,9 @@ struct Key {
|
||||
#[derive(Debug, Clone, Default)]
|
||||
/// 布局缓存、稳定出现身份与统计。构建在临时副本上完成,成功后才写回。
|
||||
pub struct LayoutRuntime {
|
||||
// 按 Block 身份只保留最后一份 Shape/Size;共享 Block 不同约束可交替覆盖,但每次复用前仍比较完整 Shape。
|
||||
sizes: HashMap<String, (Shape, Size)>,
|
||||
// 出现身份取父 Mount、稳定 slot 和 Block 身份三元组,区别于仅按业务组件分配一个全局 ID。
|
||||
ids: HashMap<Key, u64>,
|
||||
next_id: u64,
|
||||
pub version: u64,
|
||||
@@ -209,6 +214,7 @@ fn coordinate(value: f32) -> Result<f32> {
|
||||
|
||||
/// 从页面模式和当前视口生成根节点约束;无视口时使用明确的无界轴哨兵。
|
||||
pub fn root_constraints(viewport: Option<Size>) -> [f32; 4] {
|
||||
// 无视口时以 MAX_DIMENSION 作为无界哨兵;它是有限数值预算,但 Fill 必须另行要求真正有限分配。
|
||||
let size = viewport.unwrap_or(Size {
|
||||
width: MAX_DIMENSION as f32,
|
||||
height: MAX_DIMENSION as f32,
|
||||
@@ -218,6 +224,7 @@ pub fn root_constraints(viewport: Option<Size>) -> [f32; 4] {
|
||||
|
||||
/// 从父级上限扣除内边距;无界哨兵保持无界,不因减法被误认为有限。
|
||||
fn content_limit(available: f32, inset: f32) -> f32 {
|
||||
// 不能直接从哨兵减 padding,否则略小于 MAX_DIMENSION 会被误认为有限空间,从而错误允许 Fill。
|
||||
if available >= MAX_DIMENSION as f32 {
|
||||
available
|
||||
} else {
|
||||
@@ -227,6 +234,7 @@ fn content_limit(available: f32, inset: f32) -> f32 {
|
||||
/// 把父盒子的可用尺寸传递给子项,扣除容器内边距并考虑主轴是否可延伸。
|
||||
pub fn child_constraints(node: &UiNode, parent: [f32; 4]) -> [f32; 4] {
|
||||
let parent = node.sizing.available(parent);
|
||||
// Overlay 双轴都受内容盒上限约束;不像 Row/Column 在主轴自然展开。
|
||||
if let UiKind::Overlay { .. } = node.kind {
|
||||
let padding = insets(node);
|
||||
return [
|
||||
@@ -240,6 +248,7 @@ pub fn child_constraints(node: &UiNode, parent: [f32; 4]) -> [f32; 4] {
|
||||
node.kind,
|
||||
UiKind::Canvas { .. } | UiKind::Scene2D { .. } | UiKind::CanvasViewport { .. }
|
||||
) {
|
||||
// 世界 Canvas/Scene2D 与其观察视口不把屏幕尺寸传入世界布局,场景孩子用无界轴约束。
|
||||
return root_constraints(None);
|
||||
}
|
||||
if let UiKind::Scroll { width, height, .. } = node.kind {
|
||||
@@ -249,6 +258,7 @@ pub fn child_constraints(node: &UiNode, parent: [f32; 4]) -> [f32; 4] {
|
||||
constrained(Size { width, height }, parent),
|
||||
);
|
||||
}
|
||||
// Row 主轴 x 无界、交叉轴 y 有限;Column/Button/Scroll 的普通方向相反,具体滚动另有上方分支。
|
||||
match container(node) {
|
||||
Some((true, padding, _)) => [
|
||||
0.0,
|
||||
@@ -272,6 +282,7 @@ pub fn margin_constraints(child: &UiNode, mut constraints: [f32; 4]) -> [f32; 4]
|
||||
.into_iter()
|
||||
.enumerate()
|
||||
{
|
||||
// 仅对真正有限轴扣 margin,保持无界哨兵;最小约束同时收窄以维持 min<=max。
|
||||
if constraints[axis + 2] < MAX_DIMENSION as f32 {
|
||||
constraints[axis + 2] = (constraints[axis + 2] - margin).max(0.0);
|
||||
constraints[axis] = constraints[axis].min(constraints[axis + 2]);
|
||||
@@ -290,6 +301,7 @@ pub fn occurrence_constraints(
|
||||
offer: Size,
|
||||
child_index: usize,
|
||||
) -> [f32; 4] {
|
||||
// 世界面板 UI 根必须精确按纹理分辨率布局,最小与最大相同,不让外层视口世界尺寸泄入纹理空间。
|
||||
if let UiKind::Viewport3D { panels, .. } = &parent.kind {
|
||||
let size = panels[child_index];
|
||||
return [size.width, size.height, size.width, size.height];
|
||||
@@ -299,6 +311,7 @@ pub fn occurrence_constraints(
|
||||
(0, child.sizing.width, offer.width, inset.horizontal()),
|
||||
(1, child.sizing.height, offer.height, inset.vertical()),
|
||||
] {
|
||||
// Fill 若从父容器主轴得到无界 base,但父本轮 offer 有限,则用内容可用长度测量,后面再参与权重分配。
|
||||
if dimension == crate::box_model::Dimension::Fill
|
||||
&& base[axis + 2] >= MAX_DIMENSION as f32
|
||||
&& available < MAX_DIMENSION as f32
|
||||
@@ -333,6 +346,7 @@ pub fn initial_child_constraints(
|
||||
/// including a source parent's allocation rather than only constructor size.
|
||||
/// 根据已分配父尺寸重新细化子项约束,使实际安排能够反向影响换行等测量。
|
||||
pub fn arranged_child_constraints(node: &UiNode, available: [f32; 4], bounds: Size) -> [f32; 4] {
|
||||
// 滚动允许轴无界以容纳内容,禁用轴使用实际已安排的视口尺寸,文字才能按最终交叉轴宽度换行。
|
||||
if let UiKind::Scroll { axis, .. } = node.kind {
|
||||
let padding = insets(node);
|
||||
return [
|
||||
@@ -364,6 +378,7 @@ pub fn constrained(size: Size, constraints: [f32; 4]) -> Size {
|
||||
/// Effective content inset, including borders, in border-box coordinates.
|
||||
/// 计算布局使用的有效内边距和边框占用,兼容旧的统一 padding。
|
||||
pub fn insets(node: &UiNode) -> Insets {
|
||||
// 单边 padding 优先,其次兼容容器的统一 padding,最后再加 border;其他叶节点无统一 padding 默认值。
|
||||
let fallback = match node.kind {
|
||||
UiKind::Row { padding, .. }
|
||||
| UiKind::Column { padding, .. }
|
||||
@@ -406,6 +421,7 @@ fn measure_shape(shape: &Shape) -> Result<Size> {
|
||||
Shape::Padded(shape, padding) => padding.outer(measure_shape(shape)?),
|
||||
Shape::Text(value, size, width) => {
|
||||
let style = TextStyle::ui(*size);
|
||||
// 自然文字宽度与受限换行高度分别测量;布局宽度不超过可用宽度,高度反映完整段落行数。
|
||||
let intrinsic = text::measure(value, style);
|
||||
let wrapped = text::measure_wrapped(value, style, *width);
|
||||
Size {
|
||||
@@ -418,6 +434,7 @@ fn measure_shape(shape: &Shape) -> Result<Size> {
|
||||
height: *height,
|
||||
},
|
||||
Shape::Container(row, padding, gap, children) => {
|
||||
// 主轴相加、交叉轴取最大,再加对应 inset 与 n-1 个 gap;saturating_sub 保证零孩子不出现负间距。
|
||||
let along = children
|
||||
.iter()
|
||||
.map(|s| if *row { s.width } else { s.height })
|
||||
@@ -461,6 +478,7 @@ fn shape(node: &UiNode, children: Vec<Size>, constraints: [f32; 4]) -> Result<Sh
|
||||
let shape = match &node.kind {
|
||||
UiKind::Overlay { .. } => {
|
||||
let padding = insets(node);
|
||||
// Overlay 取含各自 margin 的子盒最大宽高,不把重叠项逐个相加。
|
||||
let size = children.into_iter().zip(node.children()).fold(
|
||||
Size::default(),
|
||||
|size, (child, node)| {
|
||||
@@ -476,11 +494,13 @@ fn shape(node: &UiNode, children: Vec<Size>, constraints: [f32; 4]) -> Result<Sh
|
||||
size.height + padding.vertical(),
|
||||
)
|
||||
}
|
||||
// 独立场景视口按自己的有限盒测量,不因远方世界几何或面板内容大小扩展其布局分配。
|
||||
UiKind::CanvasViewport { width, height, .. } | UiKind::Viewport3D { width, height, .. } => {
|
||||
Shape::Rect(*width, *height)
|
||||
}
|
||||
UiKind::Canvas { positions, .. } | UiKind::Scene2D { positions, .. } => {
|
||||
let mut size = Size::default();
|
||||
// 场景尺寸仅累计孩子右下端最大值;负方向内容不向左上平移整体,导航仍由相机负责。
|
||||
for (position, child) in positions.iter().zip(children) {
|
||||
size.width = size.width.max(position[0] + child.width);
|
||||
size.height = size.height.max(position[1] + child.height);
|
||||
@@ -511,6 +531,7 @@ fn shape(node: &UiNode, children: Vec<Size>, constraints: [f32; 4]) -> Result<Sh
|
||||
Shape::Container(row, padding, gap, children)
|
||||
}
|
||||
};
|
||||
// 只有非默认盒尺寸策略才包 Sized 并把约束加入键;文字等自身 Shape 已包含其实际影响测量的参数。
|
||||
Ok(if node.sizing == crate::box_model::Sizing::default() {
|
||||
shape
|
||||
} else {
|
||||
@@ -530,12 +551,14 @@ pub fn measure_native(node: &UiNode, children: Vec<Size>, constraints: [f32; 4])
|
||||
pub struct Context {
|
||||
pub node: Option<std::sync::Arc<UiNode>>,
|
||||
pub children: Vec<Size>,
|
||||
// 本轮首次测量的首选尺寸,不应被上轮 flex 实际分配覆盖,否则反复测量会改变伸缩基准。
|
||||
pub preferred: Vec<Size>,
|
||||
pub bounds: Option<[f32; 4]>,
|
||||
}
|
||||
|
||||
/// 为单个原生节点按已分配几何生成内容命令;容器背景/边框与整树遍历由外层组织。
|
||||
pub fn paint_native(node: &UiNode, x: f32, y: f32, size: Size) -> Option<RenderCommand> {
|
||||
// 文字内容盒需要减 inset 并带 wrap_bounds;Rect/Button 背景则画完整 border-box。
|
||||
let size_for_text = size;
|
||||
let inset = insets(node);
|
||||
match &node.kind {
|
||||
@@ -580,6 +603,7 @@ pub fn paint_native(node: &UiNode, x: f32, y: f32, size: Size) -> Option<RenderC
|
||||
/// 从一个节点及其分配几何生成原生绘制命令,包括背景、边框与内容。
|
||||
pub fn paint_commands(node: &UiNode, x: f32, y: f32, size: Size) -> Vec<RenderCommand> {
|
||||
let mut commands: Vec<_> = paint_native(node, x, y, size).into_iter().collect();
|
||||
// 边框按四个零圆角条分别追加,宽厚各自钳到盒大小;不使用单一 stroke_width 混淆不等厚四边。
|
||||
let border = node.border;
|
||||
for [x, y, width, height] in [
|
||||
[x, y, size.width, border.top.min(size.height)],
|
||||
@@ -639,6 +663,7 @@ fn main_limits(child: &UiNode, row: bool) -> (f32, f32) {
|
||||
|
||||
/// 按权重分配主轴剩余空间,尊重每项 min/max;达到上限的项退出后重新分配剩余量。
|
||||
pub fn flex_sizes(node: &UiNode, bounds: Size, children: &[Size], preferred: &[Size]) -> Vec<Size> {
|
||||
// Overlay 各孩子独立填内容盒,不共享主轴剩余量;普通线性容器才做下方加权增长/收缩。
|
||||
if matches!(node.kind, UiKind::Overlay { .. }) {
|
||||
let inset = insets(node);
|
||||
return children
|
||||
@@ -668,6 +693,7 @@ pub fn flex_sizes(node: &UiNode, bounds: Size, children: &[Size], preferred: &[S
|
||||
};
|
||||
let nodes = node.children();
|
||||
let mut output = children.to_vec();
|
||||
// 先处理交叉轴 Stretch,仅 Auto 维度可拉伸;明确 Fixed 不因父对齐策略被改写。
|
||||
for (child, size) in nodes.iter().zip(&mut output) {
|
||||
if child.align_self.unwrap_or(node.cross_align) == CrossAlign::Stretch {
|
||||
let sizing = child.sizing;
|
||||
@@ -682,9 +708,11 @@ pub fn flex_sizes(node: &UiNode, bounds: Size, children: &[Size], preferred: &[S
|
||||
}
|
||||
}
|
||||
}
|
||||
// 无主轴伸缩需求时也可能已完成交叉轴拉伸,直接返回这份 output,不丢弃上面调整。
|
||||
if !nodes.iter().any(|child| child.has_flex()) {
|
||||
return output;
|
||||
}
|
||||
// 可分配主轴空间先扣父 inset、每个孩子两侧 margin 和固定 gap;使用 f64 累计减小权重分配误差。
|
||||
let space = ((if row { bounds.width } else { bounds.height }) as f64
|
||||
- if row {
|
||||
padding.horizontal()
|
||||
@@ -703,6 +731,7 @@ pub fn flex_sizes(node: &UiNode, bounds: Size, children: &[Size], preferred: &[S
|
||||
.sum::<f32>() as f64
|
||||
- gap as f64 * children.len().saturating_sub(1) as f64)
|
||||
.max(0.0);
|
||||
// 显式 basis 优先;grow/fill 默认从零参与分配,其余用 preferred,不拿已经压缩后的 children 充当自然基准。
|
||||
let bases: Vec<f64> = nodes
|
||||
.iter()
|
||||
.zip(preferred)
|
||||
@@ -726,8 +755,10 @@ pub fn flex_sizes(node: &UiNode, bounds: Size, children: &[Size], preferred: &[S
|
||||
basis.clamp(min as f64, max as f64)
|
||||
})
|
||||
.collect();
|
||||
// 初始 basis 先钳 min/max,剩余正数为增长,负数为收缩;非参与项仍占据其合法基准尺寸。
|
||||
let free = space - sizes.iter().sum::<f64>();
|
||||
let growing = free > 0.0;
|
||||
// 增长按 flex,主轴 Fill 且 flex=0 用权重1;收缩按 shrink×原 basis,保持大内容承担较多缩减。
|
||||
let weights: Vec<f64> = nodes
|
||||
.iter()
|
||||
.zip(&bases)
|
||||
@@ -750,6 +781,7 @@ pub fn flex_sizes(node: &UiNode, bounds: Size, children: &[Size], preferred: &[S
|
||||
if growing { max as f64 } else { min as f64 }
|
||||
})
|
||||
.collect();
|
||||
// 只让有正权重且未到 min/max 的项参与,每轮触顶/到底的项退出,剩余空间继续分给未饱和者。
|
||||
let mut active: Vec<usize> = (0..nodes.len())
|
||||
.filter(|&i| {
|
||||
weights[i] > 0.0
|
||||
@@ -767,6 +799,7 @@ pub fn flex_sizes(node: &UiNode, bounds: Size, children: &[Size], preferred: &[S
|
||||
let mut saturated = false;
|
||||
for &i in &active {
|
||||
let capacity = (limits[i] - sizes[i]).abs();
|
||||
// 本轮按同一 remaining 比例计算,再限制到该项剩余容量;不能为单项超配后再粗暴钳制丢掉剩余量。
|
||||
let amount = (remaining * weights[i] / total).min(capacity);
|
||||
sizes[i] += if growing { amount } else { -amount };
|
||||
spent += amount;
|
||||
@@ -775,6 +808,7 @@ pub fn flex_sizes(node: &UiNode, bounds: Size, children: &[Size], preferred: &[S
|
||||
remaining = (remaining - spent).max(0.0);
|
||||
// Without saturation this round distributed all available space. Do not
|
||||
// spin on floating point residue; a saturated round freezes a child.
|
||||
// 没有项饱和说明比例分配已完成,微小浮点残差不能导致无进展死循环。
|
||||
if !saturated {
|
||||
break;
|
||||
}
|
||||
@@ -804,6 +838,7 @@ pub fn arrange_native(
|
||||
bounds: Size,
|
||||
children: &[Size],
|
||||
) -> Result<Vec<(f32, f32)>> {
|
||||
// 3D 面板根在各自纹理空间均从 (0,0) 开始,世界摆放交由 Panel world 矩阵,不把世界坐标写入普通 UI 布局。
|
||||
if matches!(node.kind, UiKind::Viewport3D { .. }) {
|
||||
return Ok(node.children().iter().map(|_| (0., 0.)).collect());
|
||||
}
|
||||
@@ -881,6 +916,7 @@ pub fn arrange_native(
|
||||
MainAlign::End => spare,
|
||||
_ => 0.0,
|
||||
};
|
||||
// space-between 只在至少两个孩子间分配额外剩余量,原固定 gap 仍保留;单项保持 start。
|
||||
let distributed_gap = if node.main_align == MainAlign::SpaceBetween && children.len() > 1 {
|
||||
spare / (children.len() - 1) as f32
|
||||
} else {
|
||||
@@ -920,6 +956,7 @@ pub fn arrange_native(
|
||||
coordinate(point.0 + child.width)?;
|
||||
coordinate(point.1 + child.height)?;
|
||||
points.push(point);
|
||||
// 主轴游标推进当前盒尺寸、两侧 margin、固定及额外 gap;交叉轴对齐不改变主轴顺序。
|
||||
cursor += if row { child.width } else { child.height };
|
||||
cursor += if row {
|
||||
margin.horizontal()
|
||||
@@ -954,6 +991,7 @@ impl LayoutRuntime {
|
||||
|
||||
/// 清空结构出现身份表;业务对象和源状态不因此被复制。
|
||||
pub fn clear_occurrences(&mut self) {
|
||||
// 清出现映射不重置 next_id,高水位继续增加,避免旧事件与新出现数值身份意外重合。
|
||||
self.ids.clear();
|
||||
}
|
||||
/// 在临时 LayoutRuntime 中生成完整几何,成功后才替换现有缓存和身份状态。
|
||||
@@ -982,6 +1020,7 @@ impl LayoutRuntime {
|
||||
children: Vec<Size>,
|
||||
constraints: [f32; 4],
|
||||
) -> Result<Size> {
|
||||
// 缓存只有完整 Shape 相等才复用,纯颜色变化不在测量 Shape 中,因此无需重做尺寸求解。
|
||||
let shape = shape(node, children, constraints)?;
|
||||
if let Some((_, size)) = self
|
||||
.sizes
|
||||
@@ -1003,12 +1042,14 @@ impl LayoutRuntime {
|
||||
self.settle_passes = usize::from(!structural);
|
||||
self.slices = usize::from(!structural);
|
||||
let mut items = Vec::<Item<'_>>::new();
|
||||
// 用显式栈构建父先子后 items,根采用 slot0/无父;孩子逆序入栈保持原始布局顺序。
|
||||
let mut work = vec![(root, None::<usize>, 0, self.root_constraints())];
|
||||
let mut used = HashSet::new();
|
||||
while let Some((node, parent, slot, constraints)) = work.pop() {
|
||||
if items.len() >= MAX_LAYOUT_VISITS {
|
||||
bail!("expanded UI layout exceeds 10000 nodes");
|
||||
}
|
||||
// slot 与孩子必须一一对应,否则稳定出现身份不可信,不能用 zip 静默丢掉多余项。
|
||||
if node.slots.len() != node.children().len() {
|
||||
bail!("invalid child slot table");
|
||||
}
|
||||
@@ -1017,6 +1058,7 @@ impl LayoutRuntime {
|
||||
slot,
|
||||
block: node.block_id.clone(),
|
||||
};
|
||||
// 结构三元组未变则复用 ID,新出现 checked_add 分配;只在成功构建副本后才提交整表。
|
||||
let id = if let Some(id) = self.ids.get(&key) {
|
||||
*id
|
||||
} else {
|
||||
@@ -1027,6 +1069,7 @@ impl LayoutRuntime {
|
||||
self.ids.insert(key.clone(), self.next_id);
|
||||
self.next_id
|
||||
};
|
||||
// 同一展示出现键不得重复;共享组件在不同父或不同 slot 下使用不同 key,仍允许多次出现。
|
||||
if !used.insert(key) {
|
||||
bail!("duplicate child slot in one presentation");
|
||||
}
|
||||
@@ -1054,6 +1097,7 @@ impl LayoutRuntime {
|
||||
}
|
||||
let mut sizes = vec![Size::default(); items.len()];
|
||||
if !structural {
|
||||
// 反向 items 自底向上测量,孩子尺寸已经求出;structural=true 完全跳过此步骤,零几何仅用于挂载准备。
|
||||
for (index, item) in items.iter().enumerate().rev() {
|
||||
sizes[index] = constrained(
|
||||
self.measure_node(
|
||||
@@ -1069,6 +1113,7 @@ impl LayoutRuntime {
|
||||
let mut mounts = Vec::with_capacity(items.len());
|
||||
let mut commands = Vec::new();
|
||||
let mut siblings = vec![None; items.len()];
|
||||
// 先按每个父的孩子顺序建立 next_sibling,最后一个默认 None;这与业务孩子列表身份不同。
|
||||
for item in &items {
|
||||
for pair in item.children.windows(2) {
|
||||
siblings[pair[0]] = Some(items[pair[1]].id);
|
||||
@@ -1113,6 +1158,7 @@ impl LayoutRuntime {
|
||||
continue;
|
||||
}
|
||||
let child_sizes: Vec<_> = item.children.iter().map(|child| sizes[*child]).collect();
|
||||
// 自顶向下用父实际尺寸更新孩子分配后再求原点;更复杂的源协议反复求解由 source_layout 驱动。
|
||||
let child_sizes = flex_sizes(item.node, size, &child_sizes, &child_sizes);
|
||||
for (child, allocated) in item.children.iter().zip(&child_sizes) {
|
||||
sizes[*child] = *allocated;
|
||||
@@ -1126,6 +1172,7 @@ impl LayoutRuntime {
|
||||
points[*child] = point;
|
||||
}
|
||||
}
|
||||
// 成功求解后丢弃已消失出现和不在本树的测量缓存,避免历史页面不断积累派生条目。
|
||||
self.ids.retain(|key, _| used.contains(key));
|
||||
let blocks: HashSet<_> = items
|
||||
.iter()
|
||||
@@ -1133,6 +1180,7 @@ impl LayoutRuntime {
|
||||
.collect();
|
||||
self.sizes
|
||||
.retain(|block, _| blocks.contains(block.as_str()));
|
||||
// 每次结构/完整构建成功都推进版本,禁止溢出复用;该版本用于后续 Mount/输入归属校验。
|
||||
self.version = self
|
||||
.version
|
||||
.checked_add(1)
|
||||
|
||||
Reference in New Issue
Block a user