use super::*;
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct EdgeInsets {
pub left: f32,
pub top: f32,
pub right: f32,
pub bottom: f32,
}
impl EdgeInsets {
pub fn all(v: f32) -> Self {
Self { left: v, top: v, right: v, bottom: v }
}
pub fn symmetric(horizontal: f32, vertical: f32) -> Self {
Self { left: horizontal, top: vertical, right: horizontal, bottom: vertical }
}
pub fn only(left: f32, top: f32, right: f32, bottom: f32) -> Self {
Self { left, top, right, bottom }
}
pub fn horizontal(&self) -> f32 {
self.left + self.right
}
pub fn vertical(&self) -> f32 {
self.top + self.bottom
}
}
pub struct PaddingRenderObject {
pub padding: EdgeInsets,
child: Option<SharedRO>,
cached_size: Size,
child_cached_size: Size,
}
impl PaddingRenderObject {
pub fn new(padding: f32, child: impl RenderObject + 'static) -> Self {
Self::new_insets(EdgeInsets::all(padding), child)
}
pub fn new_insets(padding: EdgeInsets, child: impl RenderObject + 'static) -> Self {
Self {
padding,
child: Some(make_ro_shared(Box::new(child))),
cached_size: Size::ZERO,
child_cached_size: Size::ZERO,
}
}
pub fn empty(padding: EdgeInsets) -> Self {
Self { padding, child: None, cached_size: Size::ZERO, child_cached_size: Size::ZERO }
}
}
impl RenderObject for PaddingRenderObject {
fn layout(&mut self, constraints: BoxConstraints, ctx: &mut LayoutContext) -> Size {
let p = &self.padding;
let inner = BoxConstraints {
min_width: 0.0_f32.max(constraints.min_width - p.horizontal()),
max_width: 0.0_f32.max(constraints.max_width - p.horizontal()),
min_height: 0.0_f32.max(constraints.min_height - p.vertical()),
max_height: 0.0_f32.max(constraints.max_height - p.vertical()),
};
self.child_cached_size =
if let Some(child) = &mut self.child { child.layout_cached(inner, ctx) } else { Size::ZERO };
self.cached_size =
Size::new(self.child_cached_size.width + p.horizontal(), self.child_cached_size.height + p.vertical());
constraints.constrain(self.cached_size)
}
fn paint(&self, offset: Point, painter: &mut PaintList) {
if let Some(child) = &self.child {
let p = &self.padding;
child.borrow().paint(Point::new(offset.x + p.left, offset.y + p.top), painter);
}
}
fn hit_test(&self, position: Point, offset: Point) -> Option<ElementId> {
if let Some(child) = &self.child {
let p = &self.padding;
return child.borrow().hit_test(position, Point::new(offset.x + p.left, offset.y + p.top));
}
None
}
fn content_size(&self) -> Size {
self.cached_size
}
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 push_child(&mut self, _offset: Point, _child: SharedRO) {
}
fn child_slots(&self) -> Vec<(Point, SharedRO)> {
self.child
.as_ref()
.map(|c| vec![(Point::new(self.padding.left, self.padding.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.padding.left, self.padding.top), c);
}
}
fn children_count(&self) -> usize {
if self.child.is_some() {
1
} else {
0
}
}
}