use super::*;
pub struct MarginRenderObject {
pub margin: EdgeInsets,
child: Option<SharedRO>,
cached_size: Size,
child_cached_size: Size,
}
impl MarginRenderObject {
pub fn new(margin: f32, child: impl RenderObject + 'static) -> Self {
Self::new_insets(EdgeInsets::all(margin), child)
}
pub fn new_insets(margin: EdgeInsets, child: impl RenderObject + 'static) -> Self {
Self {
margin,
child: Some(make_ro_shared(Box::new(child))),
cached_size: Size::ZERO,
child_cached_size: Size::ZERO,
}
}
pub fn empty(margin: EdgeInsets) -> Self {
Self { margin, child: None, cached_size: Size::ZERO, child_cached_size: Size::ZERO }
}
}
impl RenderObject for MarginRenderObject {
fn layout(&mut self, constraints: BoxConstraints, ctx: &mut LayoutContext) -> Size {
self.child_cached_size =
if let Some(child) = &mut self.child { child.layout_cached(constraints, ctx) } else { Size::ZERO };
let m = &self.margin;
self.cached_size =
Size::new(self.child_cached_size.width + m.horizontal(), self.child_cached_size.height + m.vertical());
constraints.constrain(self.cached_size)
}
fn content_size(&self) -> Size {
self.cached_size
}
fn paint(&self, offset: Point, painter: &mut PaintList) {
if let Some(child) = &self.child {
let m = &self.margin;
child.borrow().paint(Point::new(offset.x + m.left, offset.y + m.top), painter);
}
}
fn hit_test(&self, position: Point, offset: Point) -> Option<ElementId> {
if let Some(child) = &self.child {
let m = &self.margin;
return child.borrow().hit_test(position, Point::new(offset.x + m.left, offset.y + m.top));
}
None
}
fn propagate_element_ids(&mut self, child_ids: &[ElementId], stateful_ros: &mut [SharedRO]) {
if let Some(child) = &mut self.child {
if let Some(&cid) = child_ids.first() {
if let Some(new_ro) = stateful_ros.first_mut() {
*child = new_ro.clone();
}
child.borrow_mut().set_element_id(cid);
}
}
}
fn child_slots(&self) -> Vec<(Point, SharedRO)> {
self.child
.as_ref()
.map(|c| vec![(Point::new(self.margin.left, self.margin.top), c.clone())])
.unwrap_or_default()
}
fn for_each_child(&self, f: &mut dyn FnMut(Point, &SharedRO)) {
if let Some(c) = &self.child {
f(Point::new(self.margin.left, self.margin.top), c);
}
}
fn children_count(&self) -> usize {
if self.child.is_some() {
1
} else {
0
}
}
}