ratatui_kit/element/
element_ext.rs

1use ratatui::TerminalOptions;
2
3use super::ElementKey;
4use crate::{component::ComponentHelperExt, props::AnyProps};
5use std::io;
6
7mod private {
8    use crate::{
9        component::Component,
10        element::{AnyElement, Element},
11    };
12
13    pub trait Sealed {}
14
15    impl<'a> Sealed for AnyElement<'a> {}
16    impl<'a> Sealed for &mut AnyElement<'a> {}
17
18    impl<'a, T> Sealed for Element<'a, T> where T: Component {}
19    impl<'a, T> Sealed for &mut Element<'a, T> where T: Component {}
20}
21
22/// ElementExt trait 为所有 UI 元素提供统一的扩展方法。
23///
24/// 支持获取/修改 key、props,辅助渲染,启动主循环、全屏渲染等。
25/// 适用于组件树的统一操作和终端 UI 应用的入口。
26///
27/// # 常用用法
28/// ```rust
29/// element!(MyComponent).fullscreen().await?;
30/// ```
31pub trait ElementExt: private::Sealed + Sized {
32    /// 获取元素的唯一 key,适合 diff、重用等场景。
33    fn key(&self) -> &ElementKey;
34    /// 获取并可变修改元素的属性(props)。
35    fn props_mut(&mut self) -> AnyProps;
36    /// 获取组件辅助操作对象,支持动态调度和扩展。
37    fn helper(&self) -> Box<dyn ComponentHelperExt>;
38    /// 启动渲染主循环,传入终端选项,适合自定义Viewport场景。
39    fn render_loop(&mut self, options: TerminalOptions) -> impl Future<Output = io::Result<()>>;
40    /// 以全屏模式运行当前元素,适合大多数终端 UI 应用入口。
41    fn fullscreen(&mut self) -> impl Future<Output = io::Result<()>>;
42}