Skip to main content

mgui_core/render_object/
mod.rs

1//! RenderObject 子系统
2//!
3//! RenderObject 是 GUI 框架的渲染节点,负责布局(layout)和绘制(paint)。
4//! 每个 RenderObject 是一棵树上的节点,父节点向子节点传递 `BoxConstraints`,
5//! 子节点返回自身 `Size`,然后父节点确定子节点位置。
6//!
7//! 阶段 1 提供:
8//! - `RenderObject` trait
9//! - 叶子: `RRectRenderObject`(圆角矩形)
10//! - 容器: `ColumnRenderObject`(垂直堆叠)、`PaddingRenderObject`(四边等值内边距)
11//! - 绝对定位: `PositionedRenderObject`(在父容器内按 offset 摆放)
12//! - 多子叠加: `StackRenderObject`(多个子节点按各自 offset 叠加,适合 root)
13
14use crate::id::ElementId;
15use crate::paint::PaintList;
16use crate::reactive::Signal;
17use crate::{Color, Point, Rect, Size};
18use std::any::Any;
19use std::cell::{Cell, Ref, RefCell, RefMut};
20use std::rc::Rc;
21
22/// state → RO 就地更新的结果(P0-1 API,见架构审查 §11)
23#[derive(Clone, Copy, Debug, PartialEq, Eq)]
24pub enum StateApply {
25    /// 当前 RO 不支持由 state 就地更新(默认;框架回退 create_render_object)
26    Unsupported,
27    /// 已读取 state,但 layout/paint 的输入没有变化(无需标脏)
28    Unchanged,
29    /// 已就地更新,现有 layout/paint 需要重跑
30    Updated,
31}
32
33/// 共享 RO 的内部单元:RO 本体 + 跨帧布局缓存。
34struct RoCell {
35    ro: RefCell<Box<dyn RenderObject>>,
36    /// 子树内有影响布局的变化,下一次 layout_cached 必须真正执行。
37    layout_dirty: Cell<bool>,
38    /// 上次 layout 的约束与结果(约束相同且 clean 时直接复用)。
39    layout_cache: Cell<Option<(BoxConstraints, Size)>>,
40}
41
42/// 共享 RO —— `Rc<RoCell>`
43///
44/// 除 RO 本体的 `RefCell` 外,还携带布局 dirty/约束缓存,供容器在
45/// `layout_cached` 中跳过 clean 子树(容器级脏子树裁剪)。
46#[derive(Clone)]
47pub struct SharedRO(Rc<RoCell>);
48
49impl SharedRO {
50    pub fn borrow(&self) -> Ref<'_, Box<dyn RenderObject>> {
51        self.0.ro.borrow()
52    }
53
54    pub fn borrow_mut(&self) -> RefMut<'_, Box<dyn RenderObject>> {
55        self.0.ro.borrow_mut()
56    }
57
58    pub fn ptr_eq(&self, other: &SharedRO) -> bool {
59        Rc::ptr_eq(&self.0, &other.0)
60    }
61
62    pub fn mark_layout_dirty(&self) {
63        self.0.layout_dirty.set(true);
64    }
65
66    pub fn is_layout_dirty(&self) -> bool {
67        self.0.layout_dirty.get()
68    }
69
70    /// 带缓存的 layout:clean 且约束与上次相同时直接返回上次 size。
71    pub fn layout_cached(&self, constraints: BoxConstraints, ctx: &mut LayoutContext) -> Size {
72        if !self.0.layout_dirty.get() {
73            if let Some((cached_constraints, size)) = self.0.layout_cache.get() {
74                if cached_constraints == constraints {
75                    return size;
76                }
77            }
78        }
79        let size = self.0.ro.borrow_mut().layout(constraints, ctx);
80        self.0.layout_dirty.set(false);
81        self.0.layout_cache.set(Some((constraints, size)));
82        size
83    }
84}
85
86/// 便捷构造:`Box<dyn RO>` → SharedRO
87/// 避免到处写 `Rc::new(RefCell::new(ro))`
88pub fn make_ro_shared(ro: Box<dyn RenderObject>) -> SharedRO {
89    SharedRO(Rc::new(RoCell { ro: RefCell::new(ro), layout_dirty: Cell::new(true), layout_cache: Cell::new(None) }))
90}
91
92/// 布局约束(Flutter 范式:父→子 min/max,子→父 size)
93#[derive(Clone, Copy, Debug, PartialEq)]
94pub struct BoxConstraints {
95    pub min_width: f32,
96    pub max_width: f32,
97    pub min_height: f32,
98    pub max_height: f32,
99}
100
101impl BoxConstraints {
102    /// 无约束(子节点可以无限大)
103    pub const UNBOUNDED: Self =
104        Self { min_width: 0.0, max_width: f32::INFINITY, min_height: 0.0, max_height: f32::INFINITY };
105
106    /// 强制子节点等于给定尺寸
107    pub fn tight(size: Size) -> Self {
108        Self { min_width: size.width, max_width: size.width, min_height: size.height, max_height: size.height }
109    }
110
111    /// 松约束(子节点可在 0..size 内自由决定)
112    pub fn loose(size: Size) -> Self {
113        Self { min_width: 0.0, max_width: size.width, min_height: 0.0, max_height: size.height }
114    }
115
116    /// 把一个尺寸夹紧到约束范围内
117    pub fn constrain(&self, size: Size) -> Size {
118        Size::new(size.width.clamp(self.min_width, self.max_width), size.height.clamp(self.min_height, self.max_height))
119    }
120
121    /// 四边均匀内缩(用于带边框/焦点环的容器给 child 的约束)。
122    /// min 归 0(child 可以任意小),max 减去两侧 inset 并 clamp ≥0
123    /// (防 max < min)。∞ 减有限值仍为 ∞。
124    pub fn inset_uniform(&self, inset: f32) -> Self {
125        Self {
126            min_width: 0.0,
127            max_width: (self.max_width - inset * 2.0).max(0.0),
128            min_height: 0.0,
129            max_height: (self.max_height - inset * 2.0).max(0.0),
130        }
131    }
132
133    /// 约束是否在每个轴上都强制为唯一值(上下界相等)
134    pub fn is_tight(&self) -> bool {
135        self.min_width == self.max_width && self.min_height == self.max_height
136    }
137
138    /// 宽度轴是否被限制为有限值
139    pub fn is_bounded_width(&self) -> bool {
140        self.max_width.is_finite()
141    }
142
143    /// 高度轴是否被限制为有限值
144    pub fn is_bounded_height(&self) -> bool {
145        self.max_height.is_finite()
146    }
147
148    /// 收紧约束到指定的 width/height(等价于 tight + constrain)
149    pub fn tighten(&self, width: f32, height: f32) -> Self {
150        let w = width.clamp(self.min_width, self.max_width);
151        let h = height.clamp(self.min_height, self.max_height);
152        Self::tight(Size::new(w, h))
153    }
154
155    /// 把最小尺寸放宽为 0(返回只保留上限的松约束)
156    pub fn loosen(&self) -> Self {
157        Self { min_width: 0.0, max_width: self.max_width, min_height: 0.0, max_height: self.max_height }
158    }
159}
160
161impl Default for BoxConstraints {
162    fn default() -> Self {
163        Self::UNBOUNDED
164    }
165}
166
167/// 布局上下文(阶段 1 留空,后续可加 baseline、文字方向等)
168#[derive(Default)]
169pub struct LayoutContext;
170
171/// RenderObject 核心 trait
172///
173/// 负责:
174/// - `layout`:在给定约束下确定自身和子节点尺寸,返回自身最终尺寸
175/// - `paint`:在 `offset` 偏移处把绘制命令追加到 `PaintList`
176/// - `hit_test`:在 `position` 处做命中测试,返回命中的 `ElementId`
177/// - `set_element_id`:Element 树 mount 时注入自身 id,默认 noop
178/// - `propagate_element_ids`:容器 RO 把子 element_id 注入到内部子 RO(默认 noop)
179#[deny(missing_docs)]
180pub trait RenderObject {
181    /// 在给定约束下计算并缓存自身尺寸,返回最终 content size。
182    ///
183    /// 容器应在此递归布局子节点(优先走 `SharedRO::layout_cached`),并把
184    /// 子节点相对本 RO 的偏移记录下来供 `child_slots` / `for_each_child` 使用。
185    fn layout(&mut self, constraints: BoxConstraints, ctx: &mut LayoutContext) -> Size;
186
187    /// 把自身内容追加到 `painter`;`offset` 是本 RO 原点在目标坐标系中的位置。
188    fn paint(&self, offset: Point, painter: &mut PaintList);
189
190    /// 命中测试:`position` 为目标坐标系,`offset` 为本 RO 原点;返回命中的 element id。
191    fn hit_test(&self, position: Point, offset: Point) -> Option<ElementId> {
192        let _ = (position, offset);
193        None
194    }
195    /// Element 树 mount 时调用,让 RO 记录自己归属哪个 Element。
196    /// 阶段 2 仅 `RRectRenderObject` 有 element_id 字段,其他 RO 默认 noop。
197    fn set_element_id(&mut self, _id: ElementId) {}
198    /// 把子 element_id 注入到 RO 内部的子 RO。
199    ///
200    /// 阶段 2 协议:父 RO 在 mount 时调用此方法,把子 Element 的 id 传给自己的子 RO。
201    /// 容器 RO(Stack/Column/Positioned/Padding)override 此方法,叶子 RRect 已经有 element_id 不再需要。
202    ///
203    /// 阶段 3 扩展:`stateful_ros[i] = Some(ro)` 时,父 RO 把第 i 个 child slot 的 RO
204    /// 替换为这个 state-driven RO(覆盖 `widget.create_render_object()` 创建的 widget-driven RO)。
205    /// 这是 state 上提到 Element 层后的关键同步:state 持有 pressed/hover 等可变状态,
206    /// state.create_render_object() 返回的 RO 反映当前状态;但 Element 树的 root RO
207    /// 是从 `widget.create_render_object()` 递归构建的,默认是 widget-driven(不变),
208    /// 所以必须显式把 stateful child slot 替换成 state-driven RO,才能让 paint 反映 state。
209    fn propagate_element_ids(&mut self, _child_ids: &[ElementId], _stateful_ros: &mut [SharedRO]) {}
210
211    /// 阶段 4-D(候选 2):返回 RO 的内容 size(已 layout 后)。
212    ///
213    /// 用于 paint_frame 时把 layer 自己的 size 写到 `Layer.size`,
214    /// Renderer 据此创建对应大小的 GPU texture(小 layer 用小 texture,省 fillrate)。
215    ///
216    /// 默认 ZERO(让 Renderer fallback 全屏 texture 兼容)。叶子 RO + LayerProxy RO
217    /// override 返回真实 size;容器 RO(Stack/Column/Padding)布局复杂,默认 ZERO 即可
218    /// (这些一般不被包在 RepaintBoundary 里,直接走 root layer 全屏 paint)。
219    fn content_size(&self) -> Size {
220        Size::ZERO
221    }
222
223    /// P1-7:显式的有效内容尺寸(finite 且 > 0)。
224    ///
225    /// 默认由 `content_size()` 推导;容器/自定义 RO 可 override 以区分
226    /// “尚未布局”与“真实 ZERO”。layer sizing 不再依赖 ZERO 哨兵。
227    fn content_size_opt(&self) -> Option<Size> {
228        let size = self.content_size();
229        (size.width.is_finite() && size.height.is_finite() && size.width > 0.0 && size.height > 0.0).then_some(size)
230    }
231
232    /// P1-7:layer texture 的尺寸提示:优先真实内容尺寸,其次边界盒子尺寸。
233    ///
234    /// `content_size` 对 FittedBox/RotatedBox 是未缩放/未旋转的纹理尺寸,
235    /// `layer_layout_size` 是父布局中的盒子尺寸;两者都未知时由调用方回退
236    /// layout 返回值/视口,并记录日志。
237    fn layer_size_hint(&self) -> Option<Size> {
238        self.content_size_opt().or_else(|| {
239            self.layer_layout_size()
240                .filter(|s| s.width.is_finite() && s.height.is_finite() && s.width > 0.0 && s.height > 0.0)
241        })
242    }
243
244    /// 边界 RO 在父布局中实际占用的尺寸(供嵌套 layer 局部重排)。
245    ///
246    /// 与 `content_size()` 的区别:FittedBox/RotatedBox 的 content_size 是
247    /// **未缩放/未旋转的纹理尺寸**,而父布局尺寸才是子层再次 layout 时的盒子。
248    /// 默认 None = 未知(调用方保持原 UNBOUNDED 行为)。
249    fn layer_layout_size(&self) -> Option<Size> {
250        None
251    }
252
253    /// 嵌套 layer 局部重排时对该边界根使用的约束(默认 loose:允许内容收缩)。
254    ///
255    /// 变换类边界(FittedBox/RotatedBox)的盒子尺寸由 `constrain(child)` 推导,
256    /// 用 loose(已知盒子) 重排会把它收缩到 child 自然尺寸、把 transform 算错;
257    /// 它们覆盖为 tight(已知盒子),保证重排幂等。
258    fn layer_relayout_constraints(&self, layout_size: Size) -> BoxConstraints {
259        BoxConstraints::loose(layout_size)
260    }
261
262    /// bugfix(showcase effect 色块不显示):作为 **layer root** 收集本层内容时调用
263    /// (paint_layer_recursive 专用)。
264    ///
265    /// 边界 RO(RepaintBoundary / Transform / Opacity / ClipRRect)的 `paint` 是
266    /// noop —— 语义是"父层的 paint 走到这里必须跳过子树,内容由子 layer 的 GPU
267    /// texture 合成"。但这要求 effect 容器的 child 自己也是一个 boundary(有自己的
268    /// layer),否则**没有任何人把 child 内容画进 effect layer** → cmds=0 → 整层
269    /// 不可见(fx 区 effect layer 回归 fx_zone_effect_layers_paint_all_rects 实锤)。
270    ///
271    /// 默认实现 = `self.paint`(普通容器 RO 就是本层内容本身);边界 RO override
272    /// 为 `child.paint`(透传内容)。透传递归中若再遇到边界 RO(嵌套 layer),
273    /// 其 `paint` 仍是 noop,自动排除 —— 不会与子 layer 的 texture 重复。
274    fn paint_as_layer_root(&self, offset: Point, painter: &mut PaintList) {
275        self.paint(offset, painter);
276    }
277
278    /// 阶段 4-D:容器 RO 暴露 push_child 让 Element 层在 mount 时填 children RO。
279    /// 默认 noop(叶子 RO)。容器 RO(Stack/Column/ListView)override 实现 push 语义。
280    ///
281    /// **可扩展**:加 Padding/Positioned/ClipRRect 等容器时,override 此方法实现各自 push 语义。
282    /// leaf RO 不需要 override。
283    fn push_child(&mut self, _offset: Point, _child: SharedRO) {
284        // 叶子 RO 默认 noop
285    }
286
287    /// 阶段 4-D:容器 RO 的 children 数量(让 Element 层知道 RO 是否还有空 slot)
288    /// 默认 0(叶子 RO 没有 children)
289    fn children_count(&self) -> usize {
290        0
291    }
292
293    /// 阶段 5:返回此 RO 携带的 layer 视觉效果(Transform / Opacity / ClipRRect...)
294    ///
295    /// Element 层 mount RepaintBoundary 子类时调 `ro.layer_effect()` 把 effect
296    /// 写到 `Layer.effect`,Renderer Pass 2 blit 走 shader 对应分支。
297    ///
298    /// 默认 `None`(无效果 — 90% 的 layer 不需要 effect,shader 走快速路径)。
299    /// - RRect / Text / Image 等叶子 RO:None(自己直接画,没 layer 概念)
300    /// - RepaintBoundary 叶子 RO:None(只是个 layer 边界,不带 effect)
301    /// - **TransformRenderObject** / **OpacityRenderObject** / **ClipRRectRenderObject**:
302    ///   override 返回对应 effect
303    fn layer_effect(&self) -> crate::paint::LayerEffect {
304        crate::paint::LayerEffect::None
305    }
306
307    /// 阶段 2-E:RO 持有的 element_id(未设置 = None)
308    ///
309    /// 用于 `ElementTree::element_origin` 等 RO 树遍历(IME 光标定位:
310    /// 焦点 element 的窗口坐标)。叶子 RO override 返回自己的 element_id。
311    fn element_id(&self) -> Option<ElementId> {
312        None
313    }
314
315    /// 阶段 5-G:由 ElementTree 在 mount/update 时调用,把 element 的
316    /// `SubscriberId` 注入 RO。**只在 paint/hit_test 期间作为 signal 的
317    /// subscriber 用**——PositionedRenderObject 之类持 Signal 的 RO override 此钩子;
318    /// 其他 RO 默认 noop。
319    fn set_subscriber_id(&mut self, _id: crate::reactive::SubscriberId) {}
320
321    /// 阶段 2-E:子 (offset, RO) 槽位 — layout 后的最终位置(相对本 RO 原点)
322    ///
323    /// `element_origin` 沿 RO 树 DFS 时用。容器 RO override。
324    /// **局限**:嵌套 layer(RepaintBoundary)内的子树,其 layer 相对父 layer
325    /// 的 blit offset 不在此遍历内(阶段 2-E 简化;TextField 演示不包 RB)。
326    fn child_slots(&self) -> Vec<(Point, SharedRO)> {
327        Vec::new()
328    }
329
330    /// P1-7:无分配遍历直接子槽(offset 相对本 RO 原点)。
331    ///
332    /// 默认委托 `child_slots()`(会分配 Vec);容器 RO 应 override 为直接迭代。
333    /// origin / a11y bounds / 协议审计等 DFS 热路径使用此入口。
334    fn for_each_child(&self, f: &mut dyn FnMut(Point, &SharedRO)) {
335        for (offset, child) in self.child_slots() {
336            f(offset, &child);
337        }
338    }
339
340    /// P0-1:由具体 RO 从 state 读取可变量并**就地**写回自身。
341    ///
342    /// 默认 `Unsupported` = 框架回退 `State::create_render_object`(行为不变)。
343    /// 实现约定(详见架构审查 §11):
344    /// - 只能修改自身字段;不得替换自身、child slot 或任何子 RO;
345    /// - 修改了几何输入必须失效自身布局缓存;
346    /// - 结构/类型变化,或实现不确定时返回 `Unsupported`;
347    /// - 必须可重复调用,不得依赖调用次数。
348    fn apply_state(&mut self, _state: &mut dyn Any) -> StateApply {
349        StateApply::Unsupported
350    }
351}
352
353/// 让 `Box<T>` 自动转发 `RenderObject` 方法,
354/// 这样 `ColumnRenderObject::push(Box<dyn RenderObject>)` 等能直接通过。
355///
356/// orphan rule:`RenderObject` 是本 crate 的 trait,所以即使 `Box` 来自 std,
357/// 给 `Box<T: RenderObject + ?Sized>` impl 也是允许的。
358impl<T: RenderObject + ?Sized> RenderObject for Box<T> {
359    fn layout(&mut self, constraints: BoxConstraints, ctx: &mut LayoutContext) -> Size {
360        (**self).layout(constraints, ctx)
361    }
362    fn paint(&self, offset: Point, painter: &mut PaintList) {
363        (**self).paint(offset, painter);
364    }
365    fn hit_test(&self, position: Point, offset: Point) -> Option<ElementId> {
366        (**self).hit_test(position, offset)
367    }
368    fn set_element_id(&mut self, id: ElementId) {
369        (**self).set_element_id(id);
370    }
371    fn propagate_element_ids(&mut self, child_ids: &[ElementId], stateful_ros: &mut [SharedRO]) {
372        (**self).propagate_element_ids(child_ids, stateful_ros);
373    }
374    /// 阶段 4-D:Box 必须转发 content_size,否则 `Box<dyn RenderObject>` 调 content_size
375    /// 会走 RenderObject trait 默认实现(返回 ZERO),丢失真正的 size。
376    fn content_size(&self) -> Size {
377        (**self).content_size()
378    }
379    fn content_size_opt(&self) -> Option<Size> {
380        (**self).content_size_opt()
381    }
382    fn layer_size_hint(&self) -> Option<Size> {
383        (**self).layer_size_hint()
384    }
385    fn layer_layout_size(&self) -> Option<Size> {
386        (**self).layer_layout_size()
387    }
388    fn layer_relayout_constraints(&self, layout_size: Size) -> BoxConstraints {
389        (**self).layer_relayout_constraints(layout_size)
390    }
391    /// bugfix:Box 转发 paint_as_layer_root 到内层(否则边界 RO 被包在 Box 里时
392    /// 会走默认 self.paint,丢失透传语义)。
393    fn paint_as_layer_root(&self, offset: Point, painter: &mut PaintList) {
394        (**self).paint_as_layer_root(offset, painter)
395    }
396    /// 阶段 5:Box 转发 layer_effect 到内层
397    fn layer_effect(&self) -> crate::paint::LayerEffect {
398        (**self).layer_effect()
399    }
400    /// 阶段 5-G:Box 转发 set_subscriber_id 到内层(否则 Box 默认 noop 会吞掉
401    /// PositionedRenderObject 等 override 的钩子调用,导致 Signal 订阅链路断裂)
402    fn set_subscriber_id(&mut self, id: crate::reactive::SubscriberId) {
403        (**self).set_subscriber_id(id);
404    }
405    /// 阶段 2-E:Box 转发 element_id
406    fn element_id(&self) -> Option<ElementId> {
407        (**self).element_id()
408    }
409    /// 阶段 2-E:Box 转发 child_slots
410    fn child_slots(&self) -> Vec<(Point, SharedRO)> {
411        (**self).child_slots()
412    }
413    fn for_each_child(&self, f: &mut dyn FnMut(Point, &SharedRO)) {
414        (**self).for_each_child(f)
415    }
416    /// P0-1:Box 必须转发 apply_state,否则 `Box<dyn RenderObject>` 会走默认
417    /// `Unsupported`,所有就地更新都被静默跳过。
418    fn apply_state(&mut self, state: &mut dyn Any) -> StateApply {
419        (**self).apply_state(state)
420    }
421}
422
423// =================================================================
424// 阶段 5:effect 几何辅助函数
425// =================================================================
426
427/// 阶段 5:点是否在圆角矩形内(几何等价于 shader 的 RRect alpha mask)
428pub fn point_in_rrect(p: Point, rect_origin: Point, size: Size, radius: f32) -> bool {
429    if size.width <= 0.0 || size.height <= 0.0 {
430        return false;
431    }
432    let r = radius.max(0.0);
433    let left = rect_origin.x;
434    let top = rect_origin.y;
435    let right = left + size.width;
436    let bottom = top + size.height;
437    if p.x < left || p.x > right || p.y < top || p.y > bottom {
438        return false;
439    }
440    // 4 个角区域:以 corner center 为圆心,半径 r,点在圆外则不在 rrect 内
441    let r = r.min(size.width * 0.5).min(size.height * 0.5);
442    if r <= 0.0 {
443        return true; // 直角矩形,前面 bounds 判定通过即可
444    }
445    // 4 个 corner center
446    let c_tl = Point::new(left + r, top + r);
447    let c_tr = Point::new(right - r, top + r);
448    let c_bl = Point::new(left + r, bottom - r);
449    let c_br = Point::new(right - r, bottom - r);
450    // 左上角
451    if p.x < c_tl.x && p.y < c_tl.y {
452        return dist_sq(p, c_tl) <= r * r;
453    }
454    // 右上角
455    if p.x > c_tr.x && p.y < c_tr.y {
456        return dist_sq(p, c_tr) <= r * r;
457    }
458    // 左下角
459    if p.x < c_bl.x && p.y > c_bl.y {
460        return dist_sq(p, c_bl) <= r * r;
461    }
462    // 右下角
463    if p.x > c_br.x && p.y > c_br.y {
464        return dist_sq(p, c_br) <= r * r;
465    }
466    // 不在 corner 区域,前面 bounds 判定通过即可
467    true
468}
469
470fn dist_sq(a: Point, b: Point) -> f32 {
471    let dx = a.x - b.x;
472    let dy = a.y - b.y;
473    dx * dx + dy * dy
474}
475
476/// 阶段 5:把世界坐标 `p` 通过 Transform2D 反向变换到 layer 局部坐标
477///
478/// `forward(world_p) = R(rot) * S(sx, sy) * (local_p - center) + center + translate`
479/// 反向:
480///   `local_p = S(1/sx, 1/sy) * R(-rot) * (world_p - center - translate) + center`
481pub fn inverse_apply_transform(world_p: Point, t: crate::Transform2D, cx: f32, cy: f32) -> Point {
482    // 正向 = c + t + R(θ)·S·(p-c);逆变换必须按 S⁻¹·R⁻¹ 的顺序:
483    // 先反旋转,再反缩放。非均匀缩放时 R⁻¹·S⁻¹ 与 S⁻¹·R⁻¹ 不相等,
484    // 先除缩放会导致旋转+拉伸组合的命中坐标偏移。
485    let px = world_p.x - cx - t.translate.x;
486    let py = world_p.y - cy - t.translate.y;
487    let c = (-t.rotation).cos();
488    let sn = (-t.rotation).sin();
489    let rx = px * c - py * sn;
490    let ry = px * sn + py * c;
491    // 反向缩放(两轴独立;退化值兜底为 1.0 避免除零)
492    let sx = if t.scale_x.abs() < 1e-6 { 1.0 } else { t.scale_x };
493    let sy = if t.scale_y.abs() < 1e-6 { 1.0 } else { t.scale_y };
494    Point::new(rx / sx + cx, ry / sy + cy)
495}
496
497// =================================================================
498// 容器节点共享 slot
499// =================================================================
500
501struct ChildSlot {
502    child: SharedRO,
503    cached_size: Size,
504}
505
506// =================================================================
507// Submodule declarations
508// =================================================================
509
510pub mod border;
511pub mod clip_rrect;
512pub mod column;
513pub mod layer_proxy;
514pub mod margin;
515pub mod opacity;
516pub mod padding;
517pub mod positioned;
518pub mod proxy;
519pub mod rrect;
520pub mod stack;
521pub mod transform;
522
523// 组件专属 RO 已迁到 mgui-components/src/render_object/ (button/checkbox/slider/switch_ro/progress/focus_scope/scroll_view)
524
525// #[cfg(test)]
526// pub mod tests; // 已迁到 crates/mgui-test/tests/render_object.rs
527
528// =================================================================
529// Re-exports (保持全部原始 pub 名)
530// =================================================================
531
532pub use border::{Border, BorderRenderObject};
533pub use clip_rrect::ClipRRectRenderObject;
534pub use column::ColumnRenderObject;
535pub use layer_proxy::LayerProxyRenderObject;
536pub use margin::MarginRenderObject;
537pub use opacity::OpacityRenderObject;
538pub use padding::EdgeInsets;
539pub use padding::PaddingRenderObject;
540pub use positioned::PositionedRenderObject;
541pub use proxy::ProxyRenderObject;
542pub use rrect::RRectRenderObject;
543pub use stack::StackRenderObject;
544pub use transform::TransformRenderObject;