mgui-core 0.5.0

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

/// 水平堆叠容器([`crate::layout::RowWidget`] 的 RO;Column 的横向对偶)。
///
/// **主轴(x)从左到右累加,交叉轴(y)居中** —— 这一条是刻意与 Column 不对称的:
/// Column 的交叉轴是 x,它把子节点左对齐,因为正文/列表行的宽度差异不需要居中;
/// 而一行控件里"按钮 + 文本 + 徽标"的自然高度天生不同(按钮 32、文本 20),
/// 顶对齐会排出一条锯齿。真需要顶对齐的场景用 `PanelWidget::row`(Flex,有
/// `cross_align`)或 `StackWidget`(绝对定位),这条只是最常用的那一个默认。
///
/// 与 Column 一样**不消费** `ParentData::Flex`:要 grow/basis 就用 mgui-layout 的 Flex。
pub struct RowRenderObject {
    children: Vec<ChildSlot>,
    pub spacing: f32,
    cached_size: Size,
}

impl RowRenderObject {
    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
    }

    /// 某个子节点在交叉轴上的偏移:在本行高度内垂直居中。
    #[inline]
    fn cross_offset(&self, child_height: f32) -> f32 {
        (self.cached_size.height - child_height) * 0.5
    }
}

impl RenderObject for RowRenderObject {
    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_width: f32 = 0.0;
        let mut max_height: 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_width += self.spacing;
            }
            total_width += slot.cached_size.width;
            max_height = max_height.max(slot.cached_size.height);
        }
        self.cached_size = Size::new(total_width, max_height);
        constraints.constrain(self.cached_size)
    }

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

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

    fn hit_test(&self, position: Point, offset: Point) -> Option<ElementId> {
        // 与 paint 同一套累加,避免"画在这里、命中在那儿"
        let mut xs: Vec<f32> = Vec::with_capacity(self.children.len());
        let mut x = offset.x;
        for slot in &self.children {
            xs.push(x);
            x += slot.cached_size.width + self.spacing;
        }
        // 反向遍历:后画的在上,优先命中(与 Column/Stack/Flex 一致)
        for (slot, child_x) in self.children.iter().zip(xs.iter()).rev() {
            let y = offset.y + self.cross_offset(slot.cached_size.height);
            if let Some(id) = slot.child.borrow().hit_test(position, Point::new(*child_x, 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);
        }
    }

    fn push_child(&mut self, _offset: Point, child: SharedRO) {
        // 与 Column 同理:累加位置在 layout 时由 cached_size 决定,push 时的 offset 不参与
        self.children.push(ChildSlot { child, cached_size: Size::ZERO });
    }

    fn child_slots(&self) -> Vec<(Point, SharedRO)> {
        let mut x = 0.0;
        self.children
            .iter()
            .map(|s| {
                let off = Point::new(x, self.cross_offset(s.cached_size.height));
                x += s.cached_size.width + self.spacing;
                (off, s.child.clone())
            })
            .collect()
    }

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

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