pub trait State: Any {
Show 28 methods
// Required methods
fn as_any(&self) -> &dyn Any;
fn as_any_mut(&mut self) -> &mut dyn Any;
fn widget(&self) -> &WidgetNode;
fn set_widget(&mut self, widget: WidgetNode);
// Provided methods
fn init_state(&mut self) { ... }
fn did_update_widget(&mut self) { ... }
fn dispose(&mut self) { ... }
fn build(&self, ctx: &mut BuildContext<'_>) -> Vec<WidgetNode> { ... }
fn create_render_object(&self) -> Box<dyn RenderObject> { ... }
fn apply_to_ro(&mut self, ro: &mut dyn RenderObject) -> StateApply { ... }
fn semantics(&self) -> Option<WidgetSemantics> { ... }
fn handle_event(&mut self, event: &WidgetEvent, local_pos: Point) { ... }
fn handle_event_ctx(
&mut self,
event: &WidgetEvent,
local_pos: Point,
_ctx: &mut EventContext,
) { ... }
fn scroll_into_view(&mut self, _target: Rect) -> bool { ... }
fn tick(&mut self, _now_ms: u64) { ... }
fn handle_overlay_key(&mut self, _key: KeyCode, _mods: KeyModifiers) -> bool { ... }
fn is_animating(&self) -> bool { ... }
fn tick_changes_semantics(&self) -> bool { ... }
fn tick_changes_geometry(&self) -> bool { ... }
fn tick_settles_with_repaint(&self) -> bool { ... }
fn next_animation_delay(&self, _now_ms: u64) -> Option<u64> { ... }
fn take_needs_build(&mut self) -> bool { ... }
fn gesture_state(&self) -> Option<GestureState> { ... }
fn reject_gestures(&mut self) { ... }
fn handles_build_error(&self) -> bool { ... }
fn on_build_error(&mut self, msg: &str) { ... }
fn ime_cursor_area(&self) -> Option<(f32, f32, f32)> { ... }
fn as_overlay_aware(&mut self) -> Option<&mut dyn OverlayAware> { ... }
}Expand description
State — 在 Element 节点上持久化的状态对象
Widget 不可变(每次 rebuild 都是新实例),State 跟着 Element 走, 即使 Widget 实例换了,State 仍保留 pressed/hover/checked 等可变字段。
Required Methods§
Sourcefn as_any_mut(&mut self) -> &mut dyn Any
fn as_any_mut(&mut self) -> &mut dyn Any
必须显式实现(框架约定)
Sourcefn widget(&self) -> &WidgetNode
fn widget(&self) -> &WidgetNode
当前关联的 widget 节点(由框架在 mount/update 时写入)。
Sourcefn set_widget(&mut self, widget: WidgetNode)
fn set_widget(&mut self, widget: WidgetNode)
框架在 widget 更新时调用,State 据此 adopt 新配置(保留自身字段)。
Provided Methods§
Sourcefn init_state(&mut self)
fn init_state(&mut self)
首次挂载、State 创建后调用一次。
Sourcefn did_update_widget(&mut self)
fn did_update_widget(&mut self)
widget 被替换(同类型复用)后调用;默认空实现。
Sourcefn dispose(&mut self)
fn dispose(&mut self)
Element 卸载时调用;释放本 State 注册的订阅、定时器、手势等 UI 局部资源。
契约:State 只拥有随 Element 生死的资源。播放器会话、网络流、音频设备、 文件句柄这类跨页业务资源必须由 App/服务层持有并通过显式生命周期事件 (启动/停止/释放)管理 —— 切页签也会卸载子树并触发本回调,但那不等于 功能关闭,不能在这里 stop/dispose 业务会话。
Sourcefn build(&self, ctx: &mut BuildContext<'_>) -> Vec<WidgetNode>
fn build(&self, ctx: &mut BuildContext<'_>) -> Vec<WidgetNode>
build 阶段:从 ctx 取主题等,返回子节点列表(默认 widget().children())
Sourcefn create_render_object(&self) -> Box<dyn RenderObject>
fn create_render_object(&self) -> Box<dyn RenderObject>
创建 RenderObject —— 默认转发到 widget().create_render_object() (大多数 State 不需要自己实现,除非需要定制 RenderObject)
Sourcefn apply_to_ro(&mut self, ro: &mut dyn RenderObject) -> StateApply
fn apply_to_ro(&mut self, ro: &mut dyn RenderObject) -> StateApply
P0-1:尝试把当前 state 就地写回已有 RO。
默认把 state 交给 RO 侧匹配(RenderObject::apply_state);组件通常只需
在自己的 RO 实现该方法即可。返回 Unsupported 时框架回退到
create_render_object(行为与旧版本一致)。
逃生阀 MGUI_DISABLE_STATE_APPLY=1 会强制全部回退(进程内只读一次)。
Sourcefn semantics(&self) -> Option<WidgetSemantics>
fn semantics(&self) -> Option<WidgetSemantics>
State 侧语义覆盖:用于暴露静态 widget 无法提供的动态值
(例如 TextField 的当前文本)。None = 使用 widget.semantics()。
Sourcefn handle_event(&mut self, event: &WidgetEvent, local_pos: Point)
fn handle_event(&mut self, event: &WidgetEvent, local_pos: Point)
处理指针/键盘/IME 事件(local_pos 已折算到本 element 坐标系)。
Sourcefn handle_event_ctx(
&mut self,
event: &WidgetEvent,
local_pos: Point,
_ctx: &mut EventContext,
)
fn handle_event_ctx( &mut self, event: &WidgetEvent, local_pos: Point, _ctx: &mut EventContext, )
带消费语义的事件处理;默认转发 Self::handle_event 并继续冒泡。
Sourcefn scroll_into_view(&mut self, _target: Rect) -> bool
fn scroll_into_view(&mut self, _target: Rect) -> bool
无障碍 ScrollIntoView:把相对本元素左上角的 target 矩形滚动到可视区。
由 crate::ElementTree::reveal 沿目标元素祖先链调用;返回 true 表示
本 State 是滚动容器并已处理。默认 false。
Sourcefn handle_overlay_key(&mut self, _key: KeyCode, _mods: KeyModifiers) -> bool
fn handle_overlay_key(&mut self, _key: KeyCode, _mods: KeyModifiers) -> bool
覆盖层打开时的键盘导航钩子(方向键 / Home / End / Enter / Space)。
框架在把 KeyDown 路由到焦点元素之前调用最上层 overlay 根 element 的 State;返回 true = 已消费,不再向下路由。 典型实现:Menu / Dropdown / ContextMenu 的方向键选择与 Enter 激活。
Sourcefn is_animating(&self) -> bool
fn is_animating(&self) -> bool
ScrollView/ListView 特有:滚动动画中
Sourcefn tick_changes_semantics(&self) -> bool
fn tick_changes_semantics(&self) -> bool
is_animating 触发的重画是否同时改变了 a11y 语义。默认 true(保守:跟随旧行为)。
为什么需要这个开关:tick_states 里动画收敛后走的是 mark_element_dirty,
而它会推进 semantics_revision → 宿主重建整棵语义快照。对只换像素不换语义的
动效(视频帧、粒子、渐变)这就是每帧一次语义树重建 —— 读屏客户端活跃时尤其贵。
这条不是理论担忧:2026-11-05 的 video_with_5000 压力场景实测
semantics_bumps=112 / 131 帧,即每上一帧像素就推进一次语义版本。
与之对照,“纯 hover 不推进 revision“早就定成家规(README.md「工程约束」),
视频/像素类动画属于同一类,只是当时没有触发路径去暴露它。
Sourcefn tick_changes_geometry(&self) -> bool
fn tick_changes_geometry(&self) -> bool
is_animating 触发的重画是否改变了子元素的几何原点(child_slots 偏移)。默认 false。
与 tick_changes_semantics 同理,但针对的是 origin_cache:
mark_element_dirty_with_semantics(layout=false, ...)不会失效origin_cache;- opacity/scale 是 layer effect,
child_slots恒为 ZERO,origin 不受影响 → 保持默认 false; - offset(Positioned)动画改的就是 child 摆放位置,若不失效,IME 光标定位 /
local_position会从缓存读到旧坐标(与 paint/hit_test 实时位置不一致)。
框架在 refresh_layer_subtree 就地写回 RO 后,对本方法返回 true 的 state
失效 origin_cache —— 时机精确(RO 已是新值),且纯视觉动画零成本。
Sourcefn tick_settles_with_repaint(&self) -> bool
fn tick_settles_with_repaint(&self) -> bool
收敛帧(tick 前 animating=true、tick 后=false)是否需要补一次重画把终值写回 RO。
这是区分两类 is_animating 语义的关键开关:
- 持续式(属性轨道、lerp 类):
is_animating表“动画进行中“,收敛帧 tick 把 终值结算到字段里(如 scale 0.625→0.5)。若收敛帧不标脏,apply_to_ro不执行, RO 永远停在倒数第二帧。这类 state override 成true。 - 脉冲式(视频):
is_animating表“本 tick 是否取到帧“(got_frame)。没取到帧 的那一 tick 没有任何呈现变化,重画就是 idle 白付费 ——video_local_dirty.rs明确判过的回归。这类保持默认false。
框架不能在 tick 前用 was_animating 一刀切补标脏:那会把“上一帧取到帧、这一帧
没取到“的视频空 tick 也标脏。交给 state 自己回答才是正确的分层。
Sourcefn next_animation_delay(&self, _now_ms: u64) -> Option<u64>
fn next_animation_delay(&self, _now_ms: u64) -> Option<u64>
下一次需要 tick 的延迟(毫秒);None = 无定时任务。
is_animating == false 但未来需要翻转的状态(光标闪烁等)用它向
Runner 预约唤醒 —— 不必持续 Poll,也不会因事件循环休眠而停闪。
Sourcefn take_needs_build(&mut self) -> bool
fn take_needs_build(&mut self) -> bool
State 请求框架重建本 element 的 children(build + diff,与 update 同路径)。
handle_event 里改了影响 children 的状态(如 TabbedPane 切换内容页)后
返回 true —— 一次性,框架取走后应复位。框架在下一帧 paint_frame 开头
统一处理(事件 dispatch 正在遍历 hit 链,不能原地重建子树)。
Sourcefn gesture_state(&self) -> Option<GestureState>
fn gesture_state(&self) -> Option<GestureState>
竞技场仲裁:有持续型手势胜出 → 返回 Accepted 独占指针
Sourcefn reject_gestures(&mut self)
fn reject_gestures(&mut self)
被竞争失败:放弃进行中的手势
Sourcefn handles_build_error(&self) -> bool
fn handles_build_error(&self) -> bool
是否处理 build panic(ErrorBoundary 用)
Sourcefn on_build_error(&mut self, msg: &str)
fn on_build_error(&mut self, msg: &str)
build panic 时回调
Sourcefn ime_cursor_area(&self) -> Option<(f32, f32, f32)>
fn ime_cursor_area(&self) -> Option<(f32, f32, f32)>
TextField/TextBox 用:IME 光标区域(x, y, line_height) — 多行感知
Sourcefn as_overlay_aware(&mut self) -> Option<&mut dyn OverlayAware>
fn as_overlay_aware(&mut self) -> Option<&mut dyn OverlayAware>
Overlay-aware capability 探测 —— 返回 self 若实现了 OverlayAware trait,否则 None
默认返回 None(core 不感知任何组件 overlay 协议),组件侧 State 按需 override。
ElementTree 在 process_overlay_intents 中用此探测,统一消费各组件的 overlay 意图。
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".