feat: constrain flex bases and redistribute bounded growth and shrink
Verify Tessera GUI / verify (push) Failing after 9m4s
Verify Tessera GUI / verify (push) Failing after 9m4s
This commit is contained in:
+116
-37
@@ -40,6 +40,25 @@ impl MainAlign {
|
||||
}
|
||||
}
|
||||
|
||||
/// Main-axis constraints interpreted independently by each parent occurrence.
|
||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||
pub struct FlexPolicy {
|
||||
pub basis: Option<f32>,
|
||||
pub min: f32,
|
||||
pub max: f32,
|
||||
pub shrink: f32,
|
||||
}
|
||||
impl Default for FlexPolicy {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
basis: None,
|
||||
min: 0.0,
|
||||
max: MAX_DIMENSION as f32,
|
||||
shrink: 0.0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default, Debug, Clone, Copy, PartialEq)]
|
||||
pub struct Size {
|
||||
pub width: f32,
|
||||
@@ -226,6 +245,7 @@ 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>,
|
||||
pub preferred: Vec<Size>,
|
||||
pub bounds: Option<[f32; 4]>,
|
||||
}
|
||||
|
||||
@@ -260,50 +280,109 @@ pub fn paint_native(node: &UiNode, x: f32, y: f32, size: Size) -> Option<RenderC
|
||||
}
|
||||
}
|
||||
|
||||
/// Zero-basis weighted allocation within the space actually assigned to a
|
||||
/// native container. Fixed children keep their measured sizes.
|
||||
pub fn flex_sizes(node: &UiNode, bounds: Size, children: &[Size]) -> Vec<Size> {
|
||||
/// Allocate main-axis space with bounded growth and basis-weighted shrinking.
|
||||
/// Preferred sizes come from this occurrence's first measurement in the solve;
|
||||
/// remeasurement must not turn a prior allocation into a new intrinsic basis.
|
||||
pub fn flex_sizes(node: &UiNode, bounds: Size, children: &[Size], preferred: &[Size]) -> Vec<Size> {
|
||||
let Some((row, padding, gap)) = container(node) else {
|
||||
return children.to_vec();
|
||||
};
|
||||
let flexible: Vec<_> = node
|
||||
.children()
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter(|(_, child)| child.flex > 0.0)
|
||||
.collect();
|
||||
if flexible.is_empty() {
|
||||
let nodes = node.children();
|
||||
if !nodes.iter().any(|child| child.has_flex()) {
|
||||
return children.to_vec();
|
||||
}
|
||||
let fixed = children
|
||||
.iter()
|
||||
.zip(node.children())
|
||||
.filter(|(_, child)| child.flex == 0.0)
|
||||
.map(|(size, _)| if row { size.width } else { size.height })
|
||||
.sum::<f32>();
|
||||
let available = ((if row { bounds.width } else { bounds.height })
|
||||
- padding * 2.0
|
||||
- gap * children.len().saturating_sub(1) as f32
|
||||
- fixed)
|
||||
let space = ((if row { bounds.width } else { bounds.height }) as f64
|
||||
- padding as f64 * 2.0
|
||||
- gap as f64 * children.len().saturating_sub(1) as f64)
|
||||
.max(0.0);
|
||||
let total = flexible
|
||||
let bases: Vec<f64> = nodes
|
||||
.iter()
|
||||
.map(|(_, child)| child.flex as f64)
|
||||
.sum::<f64>();
|
||||
let mut output = children.to_vec();
|
||||
let mut remaining = available;
|
||||
for (position, (index, child)) in flexible.iter().enumerate() {
|
||||
let share = if position + 1 == flexible.len() {
|
||||
remaining
|
||||
} else {
|
||||
((available as f64 * child.flex as f64 / total) as f32).min(remaining)
|
||||
};
|
||||
if row {
|
||||
output[*index].width = share;
|
||||
} else {
|
||||
output[*index].height = share;
|
||||
.zip(preferred)
|
||||
.map(|(child, size)| {
|
||||
child.flex_policy.basis.unwrap_or_else(|| {
|
||||
if child.flex > 0.0 {
|
||||
0.0
|
||||
} else if row {
|
||||
size.width
|
||||
} else {
|
||||
size.height
|
||||
}
|
||||
}) as f64
|
||||
})
|
||||
.collect();
|
||||
let mut sizes: Vec<f64> = nodes
|
||||
.iter()
|
||||
.zip(&bases)
|
||||
.map(|(child, basis)| {
|
||||
basis.clamp(child.flex_policy.min as f64, child.flex_policy.max as f64)
|
||||
})
|
||||
.collect();
|
||||
let free = space - sizes.iter().sum::<f64>();
|
||||
let growing = free > 0.0;
|
||||
let weights: Vec<f64> = nodes
|
||||
.iter()
|
||||
.zip(&bases)
|
||||
.map(|(child, basis)| {
|
||||
if growing {
|
||||
child.flex as f64
|
||||
} else {
|
||||
child.flex_policy.shrink as f64 * basis
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
let limits: Vec<f64> = nodes
|
||||
.iter()
|
||||
.map(|child| {
|
||||
if growing {
|
||||
child.flex_policy.max as f64
|
||||
} else {
|
||||
child.flex_policy.min as f64
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
let mut active: Vec<usize> = (0..nodes.len())
|
||||
.filter(|&i| {
|
||||
weights[i] > 0.0
|
||||
&& if growing {
|
||||
sizes[i] < limits[i]
|
||||
} else {
|
||||
sizes[i] > limits[i]
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
let mut remaining = free.abs();
|
||||
while remaining > 0.0 && !active.is_empty() {
|
||||
let total = active.iter().map(|&i| weights[i]).sum::<f64>();
|
||||
let mut spent = 0.0;
|
||||
let mut saturated = false;
|
||||
for &i in &active {
|
||||
let capacity = (limits[i] - sizes[i]).abs();
|
||||
let amount = (remaining * weights[i] / total).min(capacity);
|
||||
sizes[i] += if growing { amount } else { -amount };
|
||||
spent += amount;
|
||||
saturated |= amount >= capacity;
|
||||
}
|
||||
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;
|
||||
}
|
||||
active.retain(|&i| {
|
||||
if growing {
|
||||
sizes[i] < limits[i]
|
||||
} else {
|
||||
sizes[i] > limits[i]
|
||||
}
|
||||
});
|
||||
}
|
||||
let mut output = children.to_vec();
|
||||
for (size, along) in output.iter_mut().zip(sizes) {
|
||||
if row {
|
||||
size.width = along as f32;
|
||||
} else {
|
||||
size.height = along as f32;
|
||||
}
|
||||
remaining = (remaining - share).max(0.0);
|
||||
}
|
||||
output
|
||||
}
|
||||
@@ -508,7 +587,7 @@ impl LayoutRuntime {
|
||||
continue;
|
||||
}
|
||||
let child_sizes: Vec<_> = item.children.iter().map(|child| sizes[*child]).collect();
|
||||
let child_sizes = flex_sizes(item.node, size, &child_sizes);
|
||||
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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user