Skip to main content

mgui_core/render_object/
proxy.rs

1//! 透传 RO —— 无自身视觉的包装容器(GestureDetector / Theme / Direction 等)。
2//!
3//! 协议要求:**直接 child 只有一个**。wrapper 的 `child_slots` 必须恰好返回它,
4//! `propagate_element_ids` 用 element 树的直接 child RO 替换它——这样 element
5//! 树与 RO 树严格同构。若 wrapper 直接把 child widget 的整棵 RO 树作为自己的 RO
6//! (而不是包一层 proxy),`propagate` 只能按索引替换前 N 个 slot,会留下重复的
7//! 陈旧子树(Theme 根节点曾因此重复绘制 nested boundary)。
8
9use super::{make_ro_shared, BoxConstraints, LayoutContext, RenderObject, SharedRO};
10use crate::id::ElementId;
11use crate::paint::{LayerEffect, PaintList};
12use crate::{Point, Size};
13
14/// 透传 RO:布局/绘制/命中测试全部转发给 child。
15pub struct ProxyRenderObject {
16    child: SharedRO,
17}
18
19impl ProxyRenderObject {
20    pub fn new(child: Box<dyn RenderObject>) -> Self {
21        Self { child: make_ro_shared(child) }
22    }
23}
24
25impl RenderObject for ProxyRenderObject {
26    fn layout(&mut self, constraints: BoxConstraints, ctx: &mut LayoutContext) -> Size {
27        self.child.layout_cached(constraints, ctx)
28    }
29
30    fn paint(&self, offset: Point, painter: &mut PaintList) {
31        self.child.borrow().paint(offset, painter);
32    }
33
34    fn hit_test(&self, position: Point, offset: Point) -> Option<ElementId> {
35        self.child.borrow().hit_test(position, offset)
36    }
37
38    fn set_element_id(&mut self, id: ElementId) {
39        self.child.borrow_mut().set_element_id(id);
40    }
41
42    fn element_id(&self) -> Option<ElementId> {
43        self.child.borrow().element_id()
44    }
45
46    fn content_size(&self) -> Size {
47        self.child.borrow().content_size()
48    }
49
50    fn layer_layout_size(&self) -> Option<Size> {
51        self.child.borrow().layer_layout_size()
52    }
53
54    fn layer_relayout_constraints(&self, layout_size: Size) -> BoxConstraints {
55        self.child.borrow().layer_relayout_constraints(layout_size)
56    }
57
58    fn paint_as_layer_root(&self, offset: Point, painter: &mut PaintList) {
59        self.child.borrow().paint_as_layer_root(offset, painter);
60    }
61
62    fn layer_effect(&self) -> LayerEffect {
63        self.child.borrow().layer_effect()
64    }
65
66    /// 透传容器的子槽/ID 注入必须完整,否则 element 树与 RO 树配对下降
67    /// (`element_origin` / a11y bounds)会在这一层断链。
68    fn propagate_element_ids(&mut self, child_ids: &[ElementId], stateful_ros: &mut [SharedRO]) {
69        if let (Some(&cid), Some(new_ro)) = (child_ids.first(), stateful_ros.first_mut()) {
70            self.child = new_ro.clone();
71            self.child.borrow_mut().set_element_id(cid);
72        }
73    }
74
75    fn child_slots(&self) -> Vec<(Point, SharedRO)> {
76        vec![(Point::ZERO, self.child.clone())]
77    }
78
79    fn for_each_child(&self, f: &mut dyn FnMut(Point, &SharedRO)) {
80        f(Point::ZERO, &self.child);
81    }
82
83    fn children_count(&self) -> usize {
84        1
85    }
86}