use super::*;
pub struct RowRenderObject {
children: Vec<ChildSlot>,
pub spacing: f32,
cached_size: Size,
}
impl RowRenderObject {
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
}
#[inline]
fn cross_offset(&self, child_height: f32) -> f32 {
(self.cached_size.height - child_height) * 0.5
}
}
impl RenderObject for RowRenderObject {
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_width: f32 = 0.0;
let mut max_height: 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_width += self.spacing;
}
total_width += slot.cached_size.width;
max_height = max_height.max(slot.cached_size.height);
}
self.cached_size = Size::new(total_width, max_height);
constraints.constrain(self.cached_size)
}
fn content_size(&self) -> Size {
self.cached_size
}
fn paint(&self, offset: Point, painter: &mut PaintList) {
let mut x = offset.x;
for slot in &self.children {
let y = offset.y + self.cross_offset(slot.cached_size.height);
slot.child.borrow().paint(Point::new(x, y), painter);
x += slot.cached_size.width + self.spacing;
}
}
fn hit_test(&self, position: Point, offset: Point) -> Option<ElementId> {
let mut xs: Vec<f32> = Vec::with_capacity(self.children.len());
let mut x = offset.x;
for slot in &self.children {
xs.push(x);
x += slot.cached_size.width + self.spacing;
}
for (slot, child_x) in self.children.iter().zip(xs.iter()).rev() {
let y = offset.y + self.cross_offset(slot.cached_size.height);
if let Some(id) = slot.child.borrow().hit_test(position, Point::new(*child_x, 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 x = 0.0;
self.children
.iter()
.map(|s| {
let off = Point::new(x, self.cross_offset(s.cached_size.height));
x += s.cached_size.width + self.spacing;
(off, s.child.clone())
})
.collect()
}
fn for_each_child(&self, f: &mut dyn FnMut(Point, &SharedRO)) {
let mut x = 0.0;
for (i, slot) in self.children.iter().enumerate() {
if i > 0 {
x += self.spacing;
}
f(Point::new(x, self.cross_offset(slot.cached_size.height)), &slot.child);
x += slot.cached_size.width;
}
}
fn children_count(&self) -> usize {
self.children.len()
}
}