use super::{
ChildLayoutOverride, LayoutEngine,
shrink_distribution::{LineSpace, ShrinkOutcome, distribute},
};
use crate::{
app::glyph_engine::GlyphEngine,
layout::{
Constraints, Size,
layout_kind::{FlexParams, FlexWrap, LayoutKind, ShrinkRule},
layout_result::LayoutResult,
},
node::node::Node,
};
pub(super) struct FlexShrinkLine<'a> {
pub(super) children: &'a [Node],
pub(super) params: &'a [FlexParams],
pub(super) constraints: Constraints,
pub(super) max_main: f32,
pub(super) spacing: f32,
pub(super) shrink_total: f32,
pub(super) vertical: bool,
pub(super) collapsed: bool,
}
pub(super) fn occupies_line(child: &Node, result: &LayoutResult) -> bool {
!child.is_overlay_declaration() && !result.dropped
}
impl LayoutEngine {
pub(super) fn has_dirty_shrunk_child(node: &Node) -> bool {
let vertical = match node.layout_kind() {
LayoutKind::Column { wrap, .. } if *wrap != FlexWrap::Wrap => true,
LayoutKind::Row { wrap, .. } if *wrap != FlexWrap::Wrap => false,
_ => return false,
};
node.children().iter().any(|child| {
!child.is_overlay_declaration()
&& child.layout_cache.is_dirty()
&& child.state_flex().is_some_and(|params| params.shrink > 0.0)
&& child.layout_cache.constraints().is_some_and(|constraints| {
if vertical {
constraints.min_height == constraints.max_height
} else {
constraints.min_width == constraints.max_width
}
})
})
}
pub(super) fn shrink_flex_line(
&self,
glyph_engine: &mut GlyphEngine,
line: &FlexShrinkLine<'_>,
results: &mut [LayoutResult],
child_overrides: Option<&[Option<ChildLayoutOverride>]>,
) -> Vec<bool> {
let mut collapsed = vec![false; results.len()];
for (index, outcome) in self.shrink_outcomes(line, results) {
let child = &line.children[index];
match outcome {
ShrinkOutcome::Keep => {}
ShrinkOutcome::Resize(new_main) => {
if new_main == main_size(&results[index], line.vertical) {
continue;
}
let child_constraints = shrunk_constraints(line, new_main);
results[index] = self.layout_child_node(glyph_engine, child_overrides, index, child, child_constraints);
}
ShrinkOutcome::Collapsed(new_main) => {
collapsed[index] = true;
results[index] = self.layout_collapsed(glyph_engine, child, shrunk_constraints(line, new_main));
}
ShrinkOutcome::Drop => {
let zero = Constraints::tight(Size::default());
let mut dropped = self.layout_child_node(glyph_engine, child_overrides, index, child, zero);
dropped.size = Size::default();
dropped.dropped = true;
results[index] = dropped;
}
}
}
collapsed
}
pub(super) fn layout_collapsed(
&self,
glyph_engine: &mut GlyphEngine,
child: &Node,
constraints: Constraints,
) -> LayoutResult {
let depth = self.collapsed_lines.borrow().len();
let mut node = child;
loop {
self.collapsed_lines.borrow_mut().push(node_key(node));
match (node.layout_kind(), node.children().first()) {
(LayoutKind::LogicalModifier, Some(inner)) => node = inner,
_ => break,
}
}
let result = self.layout_node(glyph_engine, child, constraints);
self.collapsed_lines.borrow_mut().truncate(depth);
result
}
pub(super) fn is_collapsed(&self, node: &Node) -> bool {
self.collapsed_lines.borrow().contains(&node_key(node))
}
fn shrink_outcomes(&self, line: &FlexShrinkLine<'_>, results: &[LayoutResult]) -> Vec<(usize, ShrinkOutcome)> {
if line.shrink_total <= 0.0 || !line.max_main.is_finite() {
return Vec::new();
}
let total_children_main: f32 = results
.iter()
.zip(line.children)
.filter(|(_, child)| !child.is_overlay_declaration())
.map(|(result, _)| main_size(result, line.vertical))
.sum();
let occupied = line
.children
.iter()
.filter(|child| !child.is_overlay_declaration())
.count();
let total_spacing = line.spacing * (occupied as f32 - 1.0).max(0.0);
let overflow = total_children_main + total_spacing - line.max_main;
if overflow <= 0.0 && !line.collapsed {
return Vec::new();
}
let mut indices = Vec::new();
let mut items = Vec::new();
let mut whole_pixels = false;
for (index, child) in line.children.iter().enumerate() {
let factor = line.params[index].shrink;
if child.is_overlay_declaration() || factor <= 0.0 {
continue;
}
whole_pixels |= child.shrink_rule() != ShrinkRule::default();
indices.push(index);
items.push(self.shrink_item(child, &results[index], factor, line.vertical));
}
let space = LineSpace {
overflow,
gap: line.spacing,
occupied,
whole_pixels,
};
indices
.into_iter()
.zip(distribute(&items, space, line.collapsed))
.collect()
}
}
fn node_key(node: &Node) -> usize {
std::ptr::from_ref(node) as usize
}
pub(super) fn main_size(result: &LayoutResult, vertical: bool) -> f32 {
if vertical {
result.size.height
} else {
result.size.width
}
}
fn shrunk_constraints(line: &FlexShrinkLine<'_>, new_main: f32) -> Constraints {
if line.vertical {
Constraints {
min_width: 0.0,
max_width: line.constraints.max_width,
min_height: new_main,
max_height: new_main,
}
} else {
Constraints {
min_width: new_main,
max_width: new_main,
min_height: 0.0,
max_height: line.constraints.max_height,
}
}
}