ratatui_kit/hooks/
mod.rs

1//! hooks 模块:为组件提供响应式状态、副作用、事件、上下文等能力,灵感来源于 React Hooks。
2//!
3//! ## 如何实现一个规范的自定义 hook
4//!
5//! 1. 定义一个实现 [`Hook`] trait 的结构体,管理自己的状态和生命周期。
6//! 2. 在 `poll_change`、`pre_component_update`、`post_component_update`、`pre_component_draw`、`post_component_draw` 等方法中实现副作用或状态逻辑。
7//! 3. 提供 trait(如 `pub trait UseXxx`)暴露给用户,trait 方法通过 `Hooks::use_hook` 注册/获取 hook 实例。
8//! 4. 推荐通过 `private::Sealed` 限制 trait 只对框架内部实现。
9//!
10//! ```rust
11//! // 1. 定义 hook 状态结构体
12//! pub struct MyHook { ... }
13//! impl Hook for MyHook { ... }
14//!
15//! // 2. 提供 trait API
16//! pub trait UseMyHook: private::Sealed {
17//!     fn use_my_hook(&mut self, ...) -> ...;
18//! }
19//! impl UseMyHook for Hooks<'_, '_> {
20//!     fn use_my_hook(&mut self, ...) -> ... {
21//!         self.use_hook(|| MyHook { ... })
22//!     }
23//! }
24//! ```
25//!
26//! 这样可保证 hook 生命周期、类型安全和复用性。
27
28#![allow(unused)]
29use crate::{
30    context::ContextStack,
31    render::{ComponentDrawer, ComponentUpdater},
32};
33use std::{
34    any::Any,
35    pin::Pin,
36    task::{Context, Poll},
37};
38mod use_context;
39pub use use_context::*;
40mod use_events;
41pub use use_events::*;
42mod use_future;
43pub use use_future::*;
44mod use_state;
45pub use use_state::*;
46mod use_memo;
47pub use use_memo::*;
48mod use_effect;
49pub use use_effect::*;
50mod use_insert_before;
51pub use use_insert_before::*;
52mod use_size;
53pub use use_size::*;
54
55#[cfg(feature = "router")]
56mod use_router;
57#[cfg(feature = "router")]
58pub use use_router::*;
59
60/// 所有自定义 hook 的 trait 基础,定义生命周期相关回调。
61///
62/// - `poll_change`:异步/响应式副作用轮询,适合 use_future/use_effect 等。
63/// - `pre_component_update/post_component_update`:组件更新前后钩子。
64/// - `pre_component_draw/post_component_draw`:组件渲染前后钩子。
65///
66/// 通常无需手动实现,除非自定义复杂 hook。
67pub trait Hook: Unpin + Send {
68    fn poll_change(self: Pin<&mut Self>, _cx: &mut Context) -> Poll<()> {
69        Poll::Pending
70    }
71
72    fn pre_component_update(&mut self, _updater: &mut ComponentUpdater) {}
73    fn post_component_update(&mut self, _updater: &mut ComponentUpdater) {}
74
75    fn pre_component_draw(&mut self, _drawer: &mut ComponentDrawer) {}
76    fn post_component_draw(&mut self, _drawer: &mut ComponentDrawer) {}
77}
78
79pub(crate) trait AnyHook: Hook {
80    fn any_self_mut(&mut self) -> &mut dyn Any;
81}
82
83impl<T: Hook + 'static> AnyHook for T {
84    fn any_self_mut(&mut self) -> &mut dyn Any {
85        self
86    }
87}
88
89impl Hook for Vec<Box<dyn AnyHook>> {
90    fn poll_change(mut self: Pin<&mut Self>, _cx: &mut Context) -> Poll<()> {
91        let mut is_ready = false;
92        for hook in self.iter_mut() {
93            if Pin::new(&mut **hook).poll_change(_cx).is_ready() {
94                is_ready = true;
95            }
96        }
97
98        if is_ready {
99            Poll::Ready(())
100        } else {
101            Poll::Pending
102        }
103    }
104
105    fn pre_component_update(&mut self, _updater: &mut ComponentUpdater) {
106        for hook in self.iter_mut() {
107            hook.pre_component_update(_updater);
108        }
109    }
110
111    fn post_component_update(&mut self, _updater: &mut ComponentUpdater) {
112        for hook in self.iter_mut() {
113            hook.post_component_update(_updater);
114        }
115    }
116
117    fn pre_component_draw(&mut self, _updater: &mut ComponentDrawer) {
118        for hook in self.iter_mut() {
119            hook.pre_component_draw(_updater);
120        }
121    }
122
123    fn post_component_draw(&mut self, _updater: &mut ComponentDrawer) {
124        for hook in self.iter_mut() {
125            hook.post_component_draw(_updater);
126        }
127    }
128}
129
130/// hooks 管理器,负责组件内所有 hook 的注册、索引和生命周期。
131///
132/// - 通过 `use_hook` 注册/获取 hook 实例,保证顺序和类型安全。
133/// - 支持 context 注入、首次更新标记等。
134/// - 用户无需手动创建,框架自动管理。
135///
136/// # 示例
137/// ```rust
138/// let mut state = hooks.use_state(|| 0);
139/// let ctx = hooks.use_context::<MyType>();
140/// ```
141pub struct Hooks<'a, 'b: 'a> {
142    hooks: &'a mut Vec<Box<dyn AnyHook>>,
143    first_update: bool,
144    hook_index: usize,
145    pub(crate) context: Option<&'a ContextStack<'b>>,
146}
147
148impl<'a> Hooks<'a, '_> {
149    pub(crate) fn new(hooks: &'a mut Vec<Box<dyn AnyHook>>, first_update: bool) -> Self {
150        Self {
151            hooks,
152            first_update,
153            hook_index: 0,
154            context: None,
155        }
156    }
157
158    pub fn with_context_stack<'c, 'd>(
159        &'c mut self,
160        context: &'c ContextStack<'d>,
161    ) -> Hooks<'c, 'd> {
162        Hooks {
163            hooks: self.hooks,
164            first_update: self.first_update,
165            hook_index: self.hook_index,
166            context: Some(context),
167        }
168    }
169
170    pub fn use_hook<F, H>(&mut self, f: F) -> &mut H
171    where
172        F: FnOnce() -> H,
173        H: Hook + Unpin + 'static,
174    {
175        if self.first_update {
176            self.hooks.push(Box::new(f()));
177        }
178        let idx = self.hook_index;
179        self.hook_index += 1;
180
181        self.hooks
182            .get_mut(idx)
183            .and_then(|hook| hook.any_self_mut().downcast_mut::<H>())
184            .expect("Hook type mismatch, ensure the hook is of the correct type")
185    }
186}