use super::*;
use crate::paint::PaintList;
use crate::Rect;
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Border {
pub width: f32,
pub color: Color,
pub radius: f32,
}
impl Default for Border {
fn default() -> Self {
Self { width: 0.0, color: Color::TRANSPARENT, radius: 0.0 }
}
}
impl Border {
pub fn new(width: f32, color: Color) -> Self {
Self { width, color, radius: 0.0 }
}
pub fn with_radius(mut self, r: f32) -> Self {
self.radius = r;
self
}
pub fn paint_ring(&self, rect: Rect, inner_bg: Color, painter: &mut PaintList) {
if self.width <= 0.0 {
return;
}
painter.rounded_rect(rect, self.color, self.radius);
let t = self.width;
let inner =
Rect::new(rect.x + t, rect.y + t, (rect.width - t * 2.0).max(0.0), (rect.height - t * 2.0).max(0.0));
painter.rounded_rect(inner, inner_bg, (self.radius - t).max(0.0));
}
}
pub struct BorderRenderObject {
pub border: Border,
child: Option<SharedRO>,
cached_size: Size,
child_cached_size: Size,
}
impl BorderRenderObject {
pub fn new(border: Border, child: impl RenderObject + 'static) -> Self {
Self {
border,
child: Some(make_ro_shared(Box::new(child))),
cached_size: Size::ZERO,
child_cached_size: Size::ZERO,
}
}
pub fn empty(border: Border) -> Self {
Self { border, child: None, cached_size: Size::ZERO, child_cached_size: Size::ZERO }
}
}
impl RenderObject for BorderRenderObject {
fn layout(&mut self, constraints: BoxConstraints, ctx: &mut LayoutContext) -> Size {
let t = self.border.width;
let t2 = t * 2.0;
let inner = BoxConstraints {
min_width: 0.0_f32.max(constraints.min_width - t2),
max_width: 0.0_f32.max(constraints.max_width - t2),
min_height: 0.0_f32.max(constraints.min_height - t2),
max_height: 0.0_f32.max(constraints.max_height - t2),
};
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 + t2, self.child_cached_size.height + t2);
constraints.constrain(self.cached_size)
}
fn content_size(&self) -> Size {
self.cached_size
}
fn paint(&self, offset: Point, painter: &mut PaintList) {
let rect = crate::Rect::new(offset.x, offset.y, self.cached_size.width, self.cached_size.height);
painter.rounded_rect(rect, self.border.color, self.border.radius);
if let Some(child) = &self.child {
let t = self.border.width;
let inner_r = (self.border.radius - t).max(0.0);
let _ = inner_r; child.borrow().paint(Point::new(offset.x + t, offset.y + t), painter);
}
}
fn hit_test(&self, position: Point, offset: Point) -> Option<ElementId> {
if let Some(child) = &self.child {
let t = self.border.width;
return child.borrow().hit_test(position, Point::new(offset.x + t, offset.y + t));
}
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)> {
let t = self.border.width;
self.child.as_ref().map(|c| vec![(Point::new(t, t), c.clone())]).unwrap_or_default()
}
fn for_each_child(&self, f: &mut dyn FnMut(Point, &SharedRO)) {
let t = self.border.width;
if let Some(c) = &self.child {
f(Point::new(t, t), c);
}
}
fn children_count(&self) -> usize {
if self.child.is_some() {
1
} else {
0
}
}
}