mgui-core 0.3.0

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

/// 垂直堆叠容器
pub struct ColumnRenderObject {
    children: Vec<ChildSlot>,
    pub spacing: f32,
    cached_size: Size,
}

impl ColumnRenderObject {
    pub fn new(spacing: f32) -> Self {
        Self { children: Vec::new(), spacing, cached_size: Size::ZERO }
    }

    pub fn push(mut self, child: impl RenderObject + 'static) -> Self {
        self.children.push(ChildSlot { child: make_ro_shared(Box::new(child)), cached_size: Size::ZERO });
        self
    }
}

impl RenderObject for ColumnRenderObject {
    fn layout(&mut self, constraints: BoxConstraints, ctx: &mut LayoutContext) -> Size {
        let child_constraints = BoxConstraints {
            min_width: 0.0,
            max_width: constraints.max_width,
            min_height: 0.0,
            max_height: constraints.max_height,
        };
        let mut total_height: f32 = 0.0;
        let mut max_width: f32 = 0.0;
        for (i, slot) in self.children.iter_mut().enumerate() {
            slot.cached_size = slot.child.layout_cached(child_constraints, ctx);
            if i > 0 {
                total_height += self.spacing;
            }
            total_height += slot.cached_size.height;
            max_width = max_width.max(slot.cached_size.width);
        }
        self.cached_size = Size::new(max_width, total_height);
        constraints.constrain(self.cached_size)
    }

    fn content_size(&self) -> Size {
        self.cached_size
    }

    fn paint(&self, offset: Point, painter: &mut PaintList) {
        let mut y = offset.y;
        for slot in &self.children {
            slot.child.borrow().paint(Point::new(offset.x, y), painter);
            y += slot.cached_size.height + self.spacing;
        }
    }

    fn hit_test(&self, position: Point, offset: Point) -> Option<ElementId> {
        // 先预计算每个子节点的实际 y 位置(与 paint 同逻辑)
        let mut ys: Vec<f32> = Vec::with_capacity(self.children.len());
        let mut y = offset.y;
        for slot in &self.children {
            ys.push(y);
            y += slot.cached_size.height + self.spacing;
        }
        // 反向遍历:后画的在上,优先命中(与 Stack/Flex 一致)
        for (slot, child_y) in self.children.iter().zip(ys.iter()).rev() {
            if let Some(id) = slot.child.borrow().hit_test(position, Point::new(offset.x, *child_y)) {
                return Some(id);
            }
        }
        None
    }

    fn propagate_element_ids(&mut self, child_ids: &[ElementId], stateful_ros: &mut [SharedRO]) {
        for (i, (slot, &cid)) in self.children.iter_mut().zip(child_ids).enumerate() {
            if let Some(new_ro) = stateful_ros.get_mut(i) {
                slot.child = new_ro.clone();
            }
            slot.child.borrow_mut().set_element_id(cid);
        }
    }

    /// 阶段 4-D:ListView/Column 都是垂直累加,push_child 时追加到 children Vec
    fn push_child(&mut self, _offset: Point, child: SharedRO) {
        // Column/ListView 的累加是 layout 时算的(从 cached_size),push 时 offset 不重要
        self.children.push(ChildSlot { child, cached_size: Size::ZERO });
    }

    /// 阶段 2-E:element_origin 遍历用 — 垂直累加(和 paint/hit_test 同逻辑)
    fn child_slots(&self) -> Vec<(Point, SharedRO)> {
        let mut y = 0.0;
        self.children
            .iter()
            .map(|s| {
                let off = Point::new(0.0, y);
                y += s.cached_size.height + self.spacing;
                (off, s.child.clone())
            })
            .collect()
    }

    fn for_each_child(&self, f: &mut dyn FnMut(Point, &SharedRO)) {
        let mut y = 0.0;
        for (i, slot) in self.children.iter().enumerate() {
            if i > 0 {
                y += self.spacing;
            }
            f(Point::new(0.0, y), &slot.child);
            y += slot.cached_size.height;
        }
    }

    fn children_count(&self) -> usize {
        self.children.len()
    }
}