ratatui_kit/components/adapter/
stateful_widget.rs

1use crate::{Component, State};
2use ratatui::widgets::StatefulWidget;
3use ratatui_kit_macros::Props;
4
5#[derive(Props)]
6pub struct StatefulWidgetAdapterProps<T>
7where
8    T: StatefulWidget + Sync + Send + 'static,
9    T::State: Sync + Send + 'static,
10{
11    pub inner: T,
12    pub state: State<T::State>,
13}
14
15pub struct StatefulWidgetAdapter<T>
16where
17    T: StatefulWidget + Sync + Send + 'static,
18    T::State: Sync + Send + 'static,
19{
20    inner: T,
21    state: State<T::State>,
22}
23
24impl<T> Component for StatefulWidgetAdapter<T>
25where
26    T: StatefulWidget + Sync + Send + 'static + Unpin + Clone,
27    T::State: Sync + Send + 'static + Unpin,
28{
29    type Props<'a> = StatefulWidgetAdapterProps<T>;
30
31    fn new(props: &Self::Props<'_>) -> Self {
32        Self {
33            inner: props.inner.clone(),
34            state: props.state,
35        }
36    }
37
38    fn update(
39        &mut self,
40        props: &mut Self::Props<'_>,
41        _hooks: crate::Hooks,
42        _updater: &mut crate::ComponentUpdater,
43    ) {
44        self.inner = props.inner.clone();
45        self.state = props.state;
46    }
47
48    fn draw(&mut self, drawer: &mut crate::ComponentDrawer<'_, '_>) {
49        drawer.render_stateful_widget(
50            self.inner.clone(),
51            drawer.area,
52            &mut self.state.write_no_update(),
53        );
54    }
55}