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::*;
52
53#[cfg(feature = "router")]
54mod use_router;
55#[cfg(feature = "router")]
56pub use use_router::*;
57
58/// 所有自定义 hook 的 trait 基础,定义生命周期相关回调。
59///
60/// - `poll_change`:异步/响应式副作用轮询,适合 use_future/use_effect 等。
61/// - `pre_component_update/post_component_update`:组件更新前后钩子。
62/// - `pre_component_draw/post_component_draw`:组件渲染前后钩子。
63///
64/// 通常无需手动实现,除非自定义复杂 hook。
65pub trait Hook: Unpin + Send {
66    fn poll_change(self: Pin<&mut Self>, _cx: &mut Context) -> Poll<()> {
67        Poll::Pending
68    }
69
70    fn pre_component_update(&mut self, _updater: &mut ComponentUpdater) {}
71    fn post_component_update(&mut self, _updater: &mut ComponentUpdater) {}
72
73    fn pre_component_draw(&mut self, _drawer: &mut ComponentDrawer) {}
74    fn post_component_draw(&mut self, _drawer: &mut ComponentDrawer) {}
75}
76
77pub(crate) trait AnyHook: Hook {
78    fn any_self_mut(&mut self) -> &mut dyn Any;
79}
80
81impl<T: Hook + 'static> AnyHook for T {
82    fn any_self_mut(&mut self) -> &mut dyn Any {
83        self
84    }
85}
86
87impl Hook for Vec<Box<dyn AnyHook>> {
88    fn poll_change(mut self: Pin<&mut Self>, _cx: &mut Context) -> Poll<()> {
89        let mut is_ready = false;
90        for hook in self.iter_mut() {
91            if Pin::new(&mut **hook).poll_change(_cx).is_ready() {
92                is_ready = true;
93            }
94        }
95
96        if is_ready {
97            Poll::Ready(())
98        } else {
99            Poll::Pending
100        }
101    }
102
103    fn pre_component_update(&mut self, _updater: &mut ComponentUpdater) {
104        for hook in self.iter_mut() {
105            hook.pre_component_update(_updater);
106        }
107    }
108
109    fn post_component_update(&mut self, _updater: &mut ComponentUpdater) {
110        for hook in self.iter_mut() {
111            hook.post_component_update(_updater);
112        }
113    }
114
115    fn pre_component_draw(&mut self, _updater: &mut ComponentDrawer) {
116        for hook in self.iter_mut() {
117            hook.pre_component_draw(_updater);
118        }
119    }
120
121    fn post_component_draw(&mut self, _updater: &mut ComponentDrawer) {
122        for hook in self.iter_mut() {
123            hook.post_component_draw(_updater);
124        }
125    }
126}
127
128/// hooks 管理器,负责组件内所有 hook 的注册、索引和生命周期。
129///
130/// - 通过 `use_hook` 注册/获取 hook 实例,保证顺序和类型安全。
131/// - 支持 context 注入、首次更新标记等。
132/// - 用户无需手动创建,框架自动管理。
133///
134/// # 示例
135/// ```rust
136/// let mut state = hooks.use_state(|| 0);
137/// let ctx = hooks.use_context::<MyType>();
138/// ```
139pub struct Hooks<'a, 'b: 'a> {
140    hooks: &'a mut Vec<Box<dyn AnyHook>>,
141    first_update: bool,
142    hook_index: usize,
143    pub(crate) context: Option<&'a ContextStack<'b>>,
144}
145
146impl<'a> Hooks<'a, '_> {
147    pub(crate) fn new(hooks: &'a mut Vec<Box<dyn AnyHook>>, first_update: bool) -> Self {
148        Self {
149            hooks,
150            first_update,
151            hook_index: 0,
152            context: None,
153        }
154    }
155
156    pub fn with_context_stack<'c, 'd>(
157        &'c mut self,
158        context: &'c ContextStack<'d>,
159    ) -> Hooks<'c, 'd> {
160        Hooks {
161            hooks: self.hooks,
162            first_update: self.first_update,
163            hook_index: self.hook_index,
164            context: Some(context),
165        }
166    }
167
168    pub fn use_hook<F, H>(&mut self, f: F) -> &mut H
169    where
170        F: FnOnce() -> H,
171        H: Hook + Unpin + 'static,
172    {
173        if self.first_update {
174            self.hooks.push(Box::new(f()));
175        }
176        let idx = self.hook_index;
177        self.hook_index += 1;
178
179        self.hooks
180            .get_mut(idx)
181            .and_then(|hook| hook.any_self_mut().downcast_mut::<H>())
182            .expect("Hook type mismatch, ensure the hook is of the correct type")
183    }
184}