mgui-core 0.1.0

mgui 框架核心:Element/RenderObject/Widget 三树、布局与绘制协议、帧管线与响应式原语
Documentation
#[cfg(feature = "frame-log")]
use log;

use super::*;

/// 多子节点容器:每个子节点有自己的 `offset`,互不参与布局
///
/// 适合作为 root 节点使用(类似 Flutter 的 `Stack`)
pub struct StackRenderObject {
    children: Vec<ChildSlot>,
    offsets: Vec<Point>,
    cached_size: Size,
}

impl StackRenderObject {
    pub fn new() -> Self {
        Self { children: Vec::new(), offsets: Vec::new(), cached_size: Size::ZERO }
    }

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

impl Default for StackRenderObject {
    fn default() -> Self {
        Self::new()
    }
}

impl RenderObject for StackRenderObject {
    fn layout(&mut self, constraints: BoxConstraints, ctx: &mut LayoutContext) -> Size {
        let child_constraints = constraints.loosen();
        for slot in &mut self.children {
            slot.cached_size = slot.child.layout_cached(child_constraints, ctx);
        }
        let mut max_w = 0.0_f32;
        let mut max_h = 0.0_f32;
        for (slot, offset) in self.children.iter().zip(self.offsets.iter()) {
            max_w = max_w.max(offset.x + slot.cached_size.width);
            max_h = max_h.max(offset.y + slot.cached_size.height);
        }
        self.cached_size = Size::new(max_w, max_h);
        #[cfg(feature = "frame-log")]
        log::trace!("[FRAME-STACK] layout children={} size={:?}", self.children.len(), self.cached_size);
        constraints.constrain(self.cached_size)
    }

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

    fn paint(&self, offset: Point, painter: &mut PaintList) {
        for (slot, off) in self.children.iter().zip(self.offsets.iter()) {
            slot.child.borrow().paint(Point::new(offset.x + off.x, offset.y + off.y), painter);
        }
    }

    fn hit_test(&self, position: Point, offset: Point) -> Option<ElementId> {
        // 反向遍历:顶层在上,优先匹配
        for (slot, off) in self.children.iter().rev().zip(self.offsets.iter().rev()) {
            if let Some(id) = slot.child.borrow().hit_test(position, Point::new(offset.x + off.x, offset.y + off.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() {
            // stateful 子:替换 child slot 的 RO 为 state-driven RO
            // Rc clone — slot.child 和 stateful_ros[i] 共享同一实例!
            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:Stack push child at offset
    fn push_child(&mut self, offset: Point, child: SharedRO) {
        self.children.push(ChildSlot { child, cached_size: Size::ZERO });
        self.offsets.push(offset);
    }

    /// 阶段 2-E:element_origin 遍历用 — 返回 owned SharedRO(Rc clone 一份)
    /// 调用方自己 .borrow() / .borrow_mut()
    fn child_slots(&self) -> Vec<(Point, SharedRO)> {
        self.children.iter().zip(self.offsets.iter()).map(|(s, &off)| (off, s.child.clone())).collect()
    }

    fn for_each_child(&self, f: &mut dyn FnMut(Point, &SharedRO)) {
        for (slot, &off) in self.children.iter().zip(self.offsets.iter()) {
            f(off, &slot.child);
        }
    }

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