mgui-core 0.8.0

mgui 框架核心:Element/RenderObject/Widget 三树、布局与绘制协议、帧管线与响应式原语
Documentation
//! 透传 RO —— 无自身视觉的包装容器(GestureDetector / Theme / Direction 等)。
//!
//! 协议要求:**直接 child 只有一个**。wrapper 的 `child_slots` 必须恰好返回它,
//! `propagate_element_ids` 用 element 树的直接 child RO 替换它——这样 element
//! 树与 RO 树严格同构。若 wrapper 直接把 child widget 的整棵 RO 树作为自己的 RO
//! (而不是包一层 proxy),`propagate` 只能按索引替换前 N 个 slot,会留下重复的
//! 陈旧子树(Theme 根节点曾因此重复绘制 nested boundary)。
//!
//! 另外 `set_element_id` 必须是 no-op:Proxy 只是透明包装,不拥有可命中的 RO 身份。
//! 父级 propagate 传入的是 wrapper element id,若转发给 child 会覆盖 child 的真实
//! id,使 hit_test 返回 wrapper、交互子控件失效。

use super::{make_ro_shared, BoxConstraints, LayoutContext, RenderObject, SharedRO};
use crate::id::ElementId;
use crate::paint::{LayerEffect, PaintList};
use crate::{Point, Size};

/// 透传 RO:布局/绘制/命中测试全部转发给 child。
pub struct ProxyRenderObject {
    child: SharedRO,
}

impl ProxyRenderObject {
    pub fn new(child: Box<dyn RenderObject>) -> Self {
        Self { child: make_ro_shared(child) }
    }
}

impl RenderObject for ProxyRenderObject {
    fn layout(&mut self, constraints: BoxConstraints, ctx: &mut LayoutContext) -> Size {
        self.child.layout_cached(constraints, ctx)
    }

    fn paint(&self, offset: Point, painter: &mut PaintList) {
        self.child.borrow().paint(offset, painter);
    }

    fn hit_test(&self, position: Point, offset: Point) -> Option<ElementId> {
        self.child.borrow().hit_test(position, offset)
    }

    fn set_element_id(&mut self, _id: ElementId) {
        // noop:Proxy 是透明包装,自己不是可命中节点。
        // 父级的 propagate_element_ids 会把 wrapper element 的 id 写进来;
        // 若继续转发给 child,会覆盖 child element 的真实 id,导致
        // hit_test 永远返回 wrapper,交互子控件收不到事件。
        // 与 LayerProxyRenderObject 同策略:只保留 child 的身份。
    }

    fn element_id(&self) -> Option<ElementId> {
        self.child.borrow().element_id()
    }

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

    fn layer_layout_size(&self) -> Option<Size> {
        self.child.borrow().layer_layout_size()
    }

    fn layer_relayout_constraints(&self, layout_size: Size) -> BoxConstraints {
        self.child.borrow().layer_relayout_constraints(layout_size)
    }

    fn paint_as_layer_root(&self, offset: Point, painter: &mut PaintList) {
        self.child.borrow().paint_as_layer_root(offset, painter);
    }

    fn layer_effect(&self) -> LayerEffect {
        self.child.borrow().layer_effect()
    }

    /// 透传容器的子槽/ID 注入必须完整,否则 element 树与 RO 树配对下降
    /// (`element_origin` / a11y bounds)会在这一层断链。
    fn propagate_element_ids(&mut self, child_ids: &[ElementId], stateful_ros: &mut [SharedRO]) {
        if let (Some(&cid), Some(new_ro)) = (child_ids.first(), stateful_ros.first_mut()) {
            self.child = new_ro.clone();
            self.child.borrow_mut().set_element_id(cid);
        }
    }

    fn child_slots(&self) -> Vec<(Point, SharedRO)> {
        vec![(Point::ZERO, self.child.clone())]
    }

    fn for_each_child(&self, f: &mut dyn FnMut(Point, &SharedRO)) {
        f(Point::ZERO, &self.child);
    }

    fn children_count(&self) -> usize {
        1
    }
}