use super::*;
pub struct ColumnRenderObject {
children: Vec<ChildSlot>,
pub spacing: f32,
cached_size: Size,
}
impl ColumnRenderObject {
pub fn new(spacing: f32) -> Self {
Self { children: Vec::new(), spacing, cached_size: Size::ZERO }
}
pub fn push(mut self, child: impl RenderObject + 'static) -> Self {
self.children.push(ChildSlot { child: make_ro_shared(Box::new(child)), cached_size: Size::ZERO });
self
}
}
impl RenderObject for ColumnRenderObject {
fn layout(&mut self, constraints: BoxConstraints, ctx: &mut LayoutContext) -> Size {
let child_constraints = BoxConstraints {
min_width: 0.0,
max_width: constraints.max_width,
min_height: 0.0,
max_height: constraints.max_height,
};
let mut total_height: f32 = 0.0;
let mut max_width: f32 = 0.0;
for (i, slot) in self.children.iter_mut().enumerate() {
slot.cached_size = slot.child.layout_cached(child_constraints, ctx);
if i > 0 {
total_height += self.spacing;
}
total_height += slot.cached_size.height;
max_width = max_width.max(slot.cached_size.width);
}
self.cached_size = Size::new(max_width, total_height);
constraints.constrain(self.cached_size)
}
fn content_size(&self) -> Size {
self.cached_size
}
fn paint(&self, offset: Point, painter: &mut PaintList) {
let mut y = offset.y;
for slot in &self.children {
slot.child.borrow().paint(Point::new(offset.x, y), painter);
y += slot.cached_size.height + self.spacing;
}
}
fn hit_test(&self, position: Point, offset: Point) -> Option<ElementId> {
let mut ys: Vec<f32> = Vec::with_capacity(self.children.len());
let mut y = offset.y;
for slot in &self.children {
ys.push(y);
y += slot.cached_size.height + self.spacing;
}
for (slot, child_y) in self.children.iter().zip(ys.iter()).rev() {
if let Some(id) = slot.child.borrow().hit_test(position, Point::new(offset.x, *child_y)) {
return Some(id);
}
}
None
}
fn propagate_element_ids(&mut self, child_ids: &[ElementId], stateful_ros: &mut [SharedRO]) {
for (i, (slot, &cid)) in self.children.iter_mut().zip(child_ids).enumerate() {
if let Some(new_ro) = stateful_ros.get_mut(i) {
slot.child = new_ro.clone();
}
slot.child.borrow_mut().set_element_id(cid);
}
}
fn push_child(&mut self, _offset: Point, child: SharedRO) {
self.children.push(ChildSlot { child, cached_size: Size::ZERO });
}
fn child_slots(&self) -> Vec<(Point, SharedRO)> {
let mut y = 0.0;
self.children
.iter()
.map(|s| {
let off = Point::new(0.0, y);
y += s.cached_size.height + self.spacing;
(off, s.child.clone())
})
.collect()
}
fn for_each_child(&self, f: &mut dyn FnMut(Point, &SharedRO)) {
let mut y = 0.0;
for (i, slot) in self.children.iter().enumerate() {
if i > 0 {
y += self.spacing;
}
f(Point::new(0.0, y), &slot.child);
y += slot.cached_size.height;
}
}
fn children_count(&self) -> usize {
self.children.len()
}
}