pub trait Host {
// Required methods
fn idle(&mut self, tree: &mut UiTree);
fn command(&mut self, tree: &mut UiTree, cmd: Command) -> bool;
// Provided methods
fn attach_waker(&mut self, _waker: Arc<dyn Waker>) { ... }
fn event(&mut self, _tree: &mut UiTree, _ev: &UiEvent) -> EventResult { ... }
fn take_theme_request(&mut self) -> Option<Theme> { ... }
fn take_font_request(&mut self) -> Option<FontConfig> { ... }
fn take_snapshot_request(&mut self) -> bool { ... }
fn on_snapshot(&mut self, _pixels: &[u8], _width: u32, _height: u32) { ... }
fn ime_position(&mut self, _tree: &mut UiTree) -> Option<(f32, f32)> { ... }
}Expand description
应用层契约:平台运行时只负责窗口与事件,业务判断(命令、策略、快照去向)都由 Host 决定。
Required Methods§
Provided Methods§
Sourcefn attach_waker(&mut self, _waker: Arc<dyn Waker>)
fn attach_waker(&mut self, _waker: Arc<dyn Waker>)
接收唤醒器(平台层在窗口创建后调用一次)。
默认实现:忽略——这样不关心异步唤醒的 Host 无需改动。
需要它的 Host 应保存起来,交给后台线程在“有数据“时 wake()。
Sourcefn event(&mut self, _tree: &mut UiTree, _ev: &UiEvent) -> EventResult
fn event(&mut self, _tree: &mut UiTree, _ev: &UiEvent) -> EventResult
事件钩子(在 tree.dispatch 之前调用)。
Sourcefn take_theme_request(&mut self) -> Option<Theme>
fn take_theme_request(&mut self) -> Option<Theme>
应用请求切换主题(Host 表达意图,平台层执行)。
返回:Some(theme) = 请求切换,平台层在本帧末尾应用;None = 无请求。
一次请求只生效一次(平台层取走后即清空),避免每帧重复设置。
默认实现返回 None(不支持运行时切换的宿主无需实现)。
Sourcefn take_font_request(&mut self) -> Option<FontConfig>
fn take_font_request(&mut self) -> Option<FontConfig>
应用请求切换字体(族名 + 字号)。
返回:Some(font) = 请求切换;None = 无请求。
失败语义由平台层决定:字体加载失败时保留旧字体,并在 stderr 报告。
Sourcefn take_snapshot_request(&mut self) -> bool
fn take_snapshot_request(&mut self) -> bool
应用请求一帧真实 backbuffer 快照。
返回 true 时,平台层会在本帧 present 前 request,并在 render 后
通过 Host::on_snapshot 回传 BGRA 像素。应用通常由快捷键(F12)
或菜单置位,再把 PNG 落盘。
Sourcefn on_snapshot(&mut self, _pixels: &[u8], _width: u32, _height: u32)
fn on_snapshot(&mut self, _pixels: &[u8], _width: u32, _height: u32)
收到真实 backbuffer 像素:pixels 为 BGRA,长度 = widthheight4。
在 UI 线程、present 之后调用;实现方负责编码 PNG,不要做长耗时操作。
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".