mgui-core 0.3.0

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

// =================================================================
// 叶子节点
// =================================================================

/// 圆角矩形(纯叶子,无子节点)
pub struct RRectRenderObject {
    pub size: Size,
    pub color: Color,
    pub radius: f32,
    pub element_id: Option<ElementId>,
}

impl RRectRenderObject {
    pub fn new(size: Size, color: Color, radius: f32) -> Self {
        Self { size, color, radius, element_id: None }
    }

    pub fn with_element_id(mut self, id: ElementId) -> Self {
        self.element_id = Some(id);
        self
    }
}

impl RenderObject for RRectRenderObject {
    fn layout(&mut self, constraints: BoxConstraints, _ctx: &mut LayoutContext) -> Size {
        self.size = constraints.constrain(self.size);
        self.size
    }

    fn paint(&self, offset: Point, painter: &mut PaintList) {
        let rect = Rect::new(offset.x, offset.y, self.size.width, self.size.height);
        painter.rounded_rect(rect, self.color, self.radius);
    }

    fn hit_test(&self, position: Point, offset: Point) -> Option<ElementId> {
        let rect = Rect::new(offset.x, offset.y, self.size.width, self.size.height);
        if rect.contains(position) {
            self.element_id
        } else {
            None
        }
    }

    fn set_element_id(&mut self, id: ElementId) {
        self.element_id = Some(id);
    }

    /// 阶段 2-E:RO 树遍历用
    fn element_id(&self) -> Option<ElementId> {
        self.element_id
    }

    /// 阶段 4-D:RRect 的 size = widget 声明的 size(在 layout 后可能再被 constrain)
    fn content_size(&self) -> Size {
        self.size
    }
}