mgui-core 0.6.0

mgui 框架核心:Element/RenderObject/Widget 三树、布局与绘制协议、帧管线与响应式原语
Documentation
use super::*;
use crate::paint::PaintList;
use crate::Rect;

/// Border 样式(四边等值宽度)
#[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
    }

    /// 以「外圈实心 + 内圈挖空(用 inner_bg 填充)」方式绘制描边环。
    ///
    /// `PaintList` 无 stroke 原语,故用两个实心 RRect 嵌套模拟描边
    /// (与 Button ghost 模式同原理)。`inner_bg` 通常传装饰的背景色,
    /// 使得内圈与外圈颜色衔接形成环;无背景色时传 `TRANSPARENT`(退化为实心块)。
    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));
    }
}

/// Border 容器(画 RRect 边框,内缩 child)
///
/// 实现:
/// - layout:child 约束四边各减 border.width(跟 Padding 同逻辑)
/// - self_size = child_size + 2 * border.width
/// - paint:先画 border 实心覆盖 self_size 区域,然后 child 在 border_width 内缩位置画
///   → child 覆盖 border inner,视觉效果即边框(child 背景透明时 border 整体可见,
///   child 自己有 bg 时会盖掉 border inner,这是正确行为)
///
/// 和 Button ghost 模式原理相同(PaintList 没有 stroke,用 fill 模拟描边)。
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) {
        // 1. border 实心
        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);

        // 2. child 在内缩位置画(覆盖 border inner)
        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 自己画半径不受影响(border 的 inner 被 child 完全盖住)
            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
        }
    }
}