use std::sync::Arc;
use super::{
ChildLayoutOverride, LayoutEngine,
shrink_distribution::{LineSpace, ShrinkOutcome, distribute},
};
use crate::{
app::glyph_engine::GlyphEngine,
layout::{
Constraints, Size,
layout_kind::{FlexWrap, LayoutKind, ShrinkRule},
layout_result::{ChildLayout, LayoutResult},
},
node::node::Node,
};
pub(super) struct FlexShrinkLine<'a> {
pub(super) children: &'a [Node],
pub(super) constraints: Constraints,
pub(super) max_main: f32,
pub(super) overflow: f32,
pub(super) spacing: f32,
pub(super) shrink_total: f32,
pub(super) vertical: bool,
pub(super) collapsed: bool,
}
#[derive(Clone, Copy)]
pub(super) struct DroppedRoom {
pub(super) constraints: Constraints,
pub(super) max_main: f32,
pub(super) spacing: f32,
pub(super) vertical: bool,
}
#[derive(Clone, Copy)]
pub(super) struct FreeRoom {
pub(super) remaining: f32,
pub(super) free: f32,
pub(super) grow_total: f32,
}
pub(super) struct LineOverflow {
room: FreeRoom,
vertical: bool,
granted: f32,
}
impl LineOverflow {
pub(super) fn new(room: FreeRoom, vertical: bool) -> Self {
Self {
room,
vertical,
granted: 0.0,
}
}
fn shares_room(&self) -> bool {
self.room.free > 0.0 && self.room.grow_total > 0.0
}
pub(super) fn add_granted(&mut self, result: &LayoutResult, grant: f32) {
let size = main_size(result, self.vertical);
self.granted += if self.shares_room() { size - grant } else { size };
}
pub(super) fn total(&self) -> f32 {
if !self.room.remaining.is_finite() {
0.0
} else if self.shares_room() {
self.granted
} else {
self.granted - self.room.remaining
}
}
}
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.shrink_rule().marker)
&& 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 [ChildLayout],
child_overrides: Option<&[Option<ChildLayoutOverride>]>,
) -> Vec<bool> {
let mut collapsed = Vec::new();
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].result, line.vertical) {
continue;
}
let child_constraints = shrunk_constraints(line, new_main);
results[index].result =
self.layout_child_node(glyph_engine, child_overrides, index, child, child_constraints);
}
ShrinkOutcome::Collapsed(new_main) => {
collapsed.resize(results.len(), false);
collapsed[index] = true;
results[index].result = 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);
Arc::make_mut(&mut dropped).size = Size::default();
Arc::make_mut(&mut dropped).dropped = true;
results[index].result = dropped;
}
}
}
collapsed
}
pub(super) fn grow_into_dropped_room(
&self,
glyph_engine: &mut GlyphEngine,
children: &[Node],
results: &mut [ChildLayout],
child_overrides: Option<&[Option<ChildLayoutOverride>]>,
collapsed: &[bool],
line: DroppedRoom,
) {
let DroppedRoom {
constraints,
max_main,
spacing,
vertical,
} = line;
if !max_main.is_finite() || !results.iter().any(|layout| layout.result.dropped) {
return;
}
let growing = |index: usize, child: &Node, result: &LayoutResult| {
occupies_line(child, result)
&& collapsed.get(index) != Some(&true)
&& child.state_flex().is_some_and(|params| params.grow > 0.0)
};
let grow_total: f32 = children
.iter()
.enumerate()
.filter(|(index, child)| growing(*index, child, &results[*index].result))
.filter_map(|(_, child)| child.state_flex().map(|params| params.grow))
.sum();
if grow_total <= 0.0 {
return;
}
let occupying = children
.iter()
.zip(results.iter())
.filter(|(child, layout)| occupies_line(child, &layout.result))
.count();
let used: f32 = children
.iter()
.zip(results.iter())
.filter(|(child, layout)| occupies_line(child, &layout.result))
.map(|(_, layout)| main_size(&layout.result, vertical))
.sum::<f32>()
+ spacing * (occupying as f32 - 1.0).max(0.0);
let left = max_main - used;
if left <= 0.5 {
return;
}
for (index, child) in children.iter().enumerate() {
if !growing(index, child, &results[index].result) {
continue;
}
let grow = child.state_flex().map_or(0.0, |params| params.grow);
let main = main_size(&results[index].result, vertical) + left * grow / grow_total;
let child_constraints = if vertical {
Constraints {
min_width: 0.0,
max_width: constraints.max_width,
min_height: main,
max_height: main,
}
} else {
Constraints {
min_width: main,
max_width: main,
min_height: 0.0,
max_height: constraints.max_height,
}
};
results[index].result = self.layout_child_node(glyph_engine, child_overrides, index, child, child_constraints);
}
}
pub(super) fn layout_collapsed(
&self,
glyph_engine: &mut GlyphEngine,
child: &Node,
constraints: Constraints,
) -> Arc<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: &[ChildLayout]) -> Vec<(usize, ShrinkOutcome)> {
let occupied = line
.children
.iter()
.filter(|child| !child.is_overlay_declaration())
.count();
let marker = line
.children
.iter()
.position(|child| !child.is_overlay_declaration() && child.shrink_rule().marker);
let Some(marker) = marker else {
return self.give_way(line, results, line.overflow, occupied);
};
let gap = if occupied > 1 { line.spacing } else { 0.0 };
let room = main_size(&results[marker].result, line.vertical) + gap;
let without = self.give_way(line, results, line.overflow - room, occupied - 1);
let dropped = without
.iter()
.any(|(_, outcome)| matches!(outcome, ShrinkOutcome::Drop | ShrinkOutcome::Collapsed(_)));
if dropped {
self.give_way(line, results, line.overflow, occupied)
} else {
let mut outcomes = without;
outcomes.push((marker, ShrinkOutcome::Drop));
outcomes
}
}
fn give_way(
&self,
line: &FlexShrinkLine<'_>,
results: &[ChildLayout],
overflow: f32,
occupied: usize,
) -> Vec<(usize, ShrinkOutcome)> {
if line.shrink_total <= 0.0 || !line.max_main.is_finite() {
return Vec::new();
}
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() {
if child.is_overlay_declaration() {
continue;
}
let factor = child.state_flex().map_or(0.0, |params| params.shrink);
if factor <= 0.0 {
continue;
}
whole_pixels |= child.shrink_rule() != ShrinkRule::default();
indices.push(index);
items.push(self.shrink_item(child, &results[index].result, 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,
}
}
}