pub trait RenderObject {
Show 19 methods
// Required methods
fn layout(
&mut self,
constraints: BoxConstraints,
ctx: &mut LayoutContext,
) -> Size;
fn paint(&self, offset: Point, painter: &mut PaintList);
// Provided methods
fn hit_test(&self, position: Point, offset: Point) -> Option<ElementId> { ... }
fn set_element_id(&mut self, _id: ElementId) { ... }
fn propagate_element_ids(
&mut self,
_child_ids: &[ElementId],
_stateful_ros: &mut [SharedRO],
) { ... }
fn content_size(&self) -> Size { ... }
fn content_size_opt(&self) -> Option<Size> { ... }
fn layer_size_hint(&self) -> Option<Size> { ... }
fn layer_layout_size(&self) -> Option<Size> { ... }
fn layer_relayout_constraints(&self, layout_size: Size) -> BoxConstraints { ... }
fn paint_as_layer_root(&self, offset: Point, painter: &mut PaintList) { ... }
fn push_child(&mut self, _offset: Point, _child: SharedRO) { ... }
fn children_count(&self) -> usize { ... }
fn layer_effect(&self) -> LayerEffect { ... }
fn element_id(&self) -> Option<ElementId> { ... }
fn set_subscriber_id(&mut self, _id: SubscriberId) { ... }
fn child_slots(&self) -> Vec<(Point, SharedRO)> { ... }
fn for_each_child(&self, f: &mut dyn FnMut(Point, &SharedRO)) { ... }
fn apply_state(&mut self, _state: &mut dyn Any) -> StateApply { ... }
}Expand description
RenderObject 核心 trait
负责:
layout:在给定约束下确定自身和子节点尺寸,返回自身最终尺寸paint:在offset偏移处把绘制命令追加到PaintListhit_test:在position处做命中测试,返回命中的ElementIdset_element_id:Element 树 mount 时注入自身 id,默认 nooppropagate_element_ids:容器 RO 把子 element_id 注入到内部子 RO(默认 noop)
Required Methods§
Sourcefn layout(
&mut self,
constraints: BoxConstraints,
ctx: &mut LayoutContext,
) -> Size
fn layout( &mut self, constraints: BoxConstraints, ctx: &mut LayoutContext, ) -> Size
在给定约束下计算并缓存自身尺寸,返回最终 content size。
容器应在此递归布局子节点(优先走 SharedRO::layout_cached),并把
子节点相对本 RO 的偏移记录下来供 child_slots / for_each_child 使用。
Provided Methods§
Sourcefn hit_test(&self, position: Point, offset: Point) -> Option<ElementId>
fn hit_test(&self, position: Point, offset: Point) -> Option<ElementId>
命中测试:position 为目标坐标系,offset 为本 RO 原点;返回命中的 element id。
Sourcefn set_element_id(&mut self, _id: ElementId)
fn set_element_id(&mut self, _id: ElementId)
Element 树 mount 时调用,让 RO 记录自己归属哪个 Element。
阶段 2 仅 RRectRenderObject 有 element_id 字段,其他 RO 默认 noop。
Sourcefn propagate_element_ids(
&mut self,
_child_ids: &[ElementId],
_stateful_ros: &mut [SharedRO],
)
fn propagate_element_ids( &mut self, _child_ids: &[ElementId], _stateful_ros: &mut [SharedRO], )
把子 element_id 注入到 RO 内部的子 RO。
阶段 2 协议:父 RO 在 mount 时调用此方法,把子 Element 的 id 传给自己的子 RO。 容器 RO(Stack/Column/Positioned/Padding)override 此方法,叶子 RRect 已经有 element_id 不再需要。
阶段 3 扩展:stateful_ros[i] = Some(ro) 时,父 RO 把第 i 个 child slot 的 RO
替换为这个 state-driven RO(覆盖 widget.create_render_object() 创建的 widget-driven RO)。
这是 state 上提到 Element 层后的关键同步:state 持有 pressed/hover 等可变状态,
state.create_render_object() 返回的 RO 反映当前状态;但 Element 树的 root RO
是从 widget.create_render_object() 递归构建的,默认是 widget-driven(不变),
所以必须显式把 stateful child slot 替换成 state-driven RO,才能让 paint 反映 state。
Sourcefn content_size(&self) -> Size
fn content_size(&self) -> Size
阶段 4-D(候选 2):返回 RO 的内容 size(已 layout 后)。
用于 paint_frame 时把 layer 自己的 size 写到 Layer.size,
Renderer 据此创建对应大小的 GPU texture(小 layer 用小 texture,省 fillrate)。
默认 ZERO(让 Renderer fallback 全屏 texture 兼容)。叶子 RO + LayerProxy RO override 返回真实 size;容器 RO(Stack/Column/Padding)布局复杂,默认 ZERO 即可 (这些一般不被包在 RepaintBoundary 里,直接走 root layer 全屏 paint)。
Sourcefn content_size_opt(&self) -> Option<Size>
fn content_size_opt(&self) -> Option<Size>
P1-7:显式的有效内容尺寸(finite 且 > 0)。
默认由 content_size() 推导;容器/自定义 RO 可 override 以区分
“尚未布局”与“真实 ZERO”。layer sizing 不再依赖 ZERO 哨兵。
Sourcefn layer_size_hint(&self) -> Option<Size>
fn layer_size_hint(&self) -> Option<Size>
P1-7:layer texture 的尺寸提示:优先真实内容尺寸,其次边界盒子尺寸。
content_size 对 FittedBox/RotatedBox 是未缩放/未旋转的纹理尺寸,
layer_layout_size 是父布局中的盒子尺寸;两者都未知时由调用方回退
layout 返回值/视口,并记录日志。
Sourcefn layer_layout_size(&self) -> Option<Size>
fn layer_layout_size(&self) -> Option<Size>
边界 RO 在父布局中实际占用的尺寸(供嵌套 layer 局部重排)。
与 content_size() 的区别:FittedBox/RotatedBox 的 content_size 是
未缩放/未旋转的纹理尺寸,而父布局尺寸才是子层再次 layout 时的盒子。
默认 None = 未知(调用方保持原 UNBOUNDED 行为)。
Sourcefn layer_relayout_constraints(&self, layout_size: Size) -> BoxConstraints
fn layer_relayout_constraints(&self, layout_size: Size) -> BoxConstraints
嵌套 layer 局部重排时对该边界根使用的约束(默认 loose:允许内容收缩)。
变换类边界(FittedBox/RotatedBox)的盒子尺寸由 constrain(child) 推导,
用 loose(已知盒子) 重排会把它收缩到 child 自然尺寸、把 transform 算错;
它们覆盖为 tight(已知盒子),保证重排幂等。
Sourcefn paint_as_layer_root(&self, offset: Point, painter: &mut PaintList)
fn paint_as_layer_root(&self, offset: Point, painter: &mut PaintList)
bugfix(showcase effect 色块不显示):作为 layer root 收集本层内容时调用 (paint_layer_recursive 专用)。
边界 RO(RepaintBoundary / Transform / Opacity / ClipRRect)的 paint 是
noop —— 语义是“父层的 paint 走到这里必须跳过子树,内容由子 layer 的 GPU
texture 合成“。但这要求 effect 容器的 child 自己也是一个 boundary(有自己的
layer),否则没有任何人把 child 内容画进 effect layer → cmds=0 → 整层
不可见(fx 区 effect layer 回归 fx_zone_effect_layers_paint_all_rects 实锤)。
默认实现 = self.paint(普通容器 RO 就是本层内容本身);边界 RO override
为 child.paint(透传内容)。透传递归中若再遇到边界 RO(嵌套 layer),
其 paint 仍是 noop,自动排除 —— 不会与子 layer 的 texture 重复。
Sourcefn push_child(&mut self, _offset: Point, _child: SharedRO)
fn push_child(&mut self, _offset: Point, _child: SharedRO)
阶段 4-D:容器 RO 暴露 push_child 让 Element 层在 mount 时填 children RO。 默认 noop(叶子 RO)。容器 RO(Stack/Column/ListView)override 实现 push 语义。
可扩展:加 Padding/Positioned/ClipRRect 等容器时,override 此方法实现各自 push 语义。 leaf RO 不需要 override。
Sourcefn children_count(&self) -> usize
fn children_count(&self) -> usize
阶段 4-D:容器 RO 的 children 数量(让 Element 层知道 RO 是否还有空 slot) 默认 0(叶子 RO 没有 children)
Sourcefn layer_effect(&self) -> LayerEffect
fn layer_effect(&self) -> LayerEffect
阶段 5:返回此 RO 携带的 layer 视觉效果(Transform / Opacity / ClipRRect…)
Element 层 mount RepaintBoundary 子类时调 ro.layer_effect() 把 effect
写到 Layer.effect,Renderer Pass 2 blit 走 shader 对应分支。
默认 None(无效果 — 90% 的 layer 不需要 effect,shader 走快速路径)。
- RRect / Text / Image 等叶子 RO:None(自己直接画,没 layer 概念)
- RepaintBoundary 叶子 RO:None(只是个 layer 边界,不带 effect)
- TransformRenderObject / OpacityRenderObject / ClipRRectRenderObject: override 返回对应 effect
Sourcefn element_id(&self) -> Option<ElementId>
fn element_id(&self) -> Option<ElementId>
阶段 2-E:RO 持有的 element_id(未设置 = None)
用于 ElementTree::element_origin 等 RO 树遍历(IME 光标定位:
焦点 element 的窗口坐标)。叶子 RO override 返回自己的 element_id。
Sourcefn set_subscriber_id(&mut self, _id: SubscriberId)
fn set_subscriber_id(&mut self, _id: SubscriberId)
阶段 5-G:由 ElementTree 在 mount/update 时调用,把 element 的
SubscriberId 注入 RO。只在 paint/hit_test 期间作为 signal 的
subscriber 用——PositionedRenderObject 之类持 Signal 的 RO override 此钩子;
其他 RO 默认 noop。
Sourcefn child_slots(&self) -> Vec<(Point, SharedRO)>
fn child_slots(&self) -> Vec<(Point, SharedRO)>
阶段 2-E:子 (offset, RO) 槽位 — layout 后的最终位置(相对本 RO 原点)
element_origin 沿 RO 树 DFS 时用。容器 RO override。
局限:嵌套 layer(RepaintBoundary)内的子树,其 layer 相对父 layer
的 blit offset 不在此遍历内(阶段 2-E 简化;TextField 演示不包 RB)。
Sourcefn for_each_child(&self, f: &mut dyn FnMut(Point, &SharedRO))
fn for_each_child(&self, f: &mut dyn FnMut(Point, &SharedRO))
P1-7:无分配遍历直接子槽(offset 相对本 RO 原点)。
默认委托 child_slots()(会分配 Vec);容器 RO 应 override 为直接迭代。
origin / a11y bounds / 协议审计等 DFS 热路径使用此入口。
Sourcefn apply_state(&mut self, _state: &mut dyn Any) -> StateApply
fn apply_state(&mut self, _state: &mut dyn Any) -> StateApply
P0-1:由具体 RO 从 state 读取可变量并就地写回自身。
默认 Unsupported = 框架回退 State::create_render_object(行为不变)。
实现约定(详见架构审查 §11):
- 只能修改自身字段;不得替换自身、child slot 或任何子 RO;
- 修改了几何输入必须失效自身布局缓存;
- 结构/类型变化,或实现不确定时返回
Unsupported; - 必须可重复调用,不得依赖调用次数。
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".
Implementations on Foreign Types§
Source§impl<T: RenderObject + ?Sized> RenderObject for Box<T>
让 Box<T> 自动转发 RenderObject 方法,
这样 ColumnRenderObject::push(Box<dyn RenderObject>) 等能直接通过。
impl<T: RenderObject + ?Sized> RenderObject for Box<T>
让 Box<T> 自动转发 RenderObject 方法,
这样 ColumnRenderObject::push(Box<dyn RenderObject>) 等能直接通过。
orphan rule:RenderObject 是本 crate 的 trait,所以即使 Box 来自 std,
给 Box<T: RenderObject + ?Sized> impl 也是允许的。
Source§fn content_size(&self) -> Size
fn content_size(&self) -> Size
阶段 4-D:Box 必须转发 content_size,否则 Box<dyn RenderObject> 调 content_size
会走 RenderObject trait 默认实现(返回 ZERO),丢失真正的 size。
Source§fn paint_as_layer_root(&self, offset: Point, painter: &mut PaintList)
fn paint_as_layer_root(&self, offset: Point, painter: &mut PaintList)
bugfix:Box 转发 paint_as_layer_root 到内层(否则边界 RO 被包在 Box 里时 会走默认 self.paint,丢失透传语义)。
Source§fn layer_effect(&self) -> LayerEffect
fn layer_effect(&self) -> LayerEffect
阶段 5:Box 转发 layer_effect 到内层
Source§fn set_subscriber_id(&mut self, id: SubscriberId)
fn set_subscriber_id(&mut self, id: SubscriberId)
阶段 5-G:Box 转发 set_subscriber_id 到内层(否则 Box 默认 noop 会吞掉 PositionedRenderObject 等 override 的钩子调用,导致 Signal 订阅链路断裂)
Source§fn element_id(&self) -> Option<ElementId>
fn element_id(&self) -> Option<ElementId>
阶段 2-E:Box 转发 element_id
Source§fn apply_state(&mut self, state: &mut dyn Any) -> StateApply
fn apply_state(&mut self, state: &mut dyn Any) -> StateApply
P0-1:Box 必须转发 apply_state,否则 Box<dyn RenderObject> 会走默认
Unsupported,所有就地更新都被静默跳过。