ratatui_kit/element/
any_element.rs1use super::{Element, ElementKey, element_ext::ElementExt};
2use crate::{
3 component::{Component, ComponentHelper, ComponentHelperExt},
4 props::AnyProps,
5 render::tree::{render, render_loop},
6 terminal::{CrossTerminal, Terminal},
7};
8use std::io;
9
10pub struct AnyElement<'a> {
11 key: ElementKey,
12 props: AnyProps<'a>,
13 helper: Box<dyn ComponentHelperExt>,
14}
15
16impl<'a, T> From<Element<'a, T>> for AnyElement<'a>
17where
18 T: Component,
19{
20 fn from(value: Element<'a, T>) -> Self {
21 Self {
22 key: value.key,
23 props: AnyProps::owned(value.props),
24 helper: ComponentHelper::<T>::boxed(),
25 }
26 }
27}
28
29impl<'a, 'b: 'a, T> From<&'a mut Element<'b, T>> for AnyElement<'a>
30where
31 T: Component,
32{
33 fn from(value: &'a mut Element<'b, T>) -> Self {
34 Self {
35 key: value.key.clone(),
36 props: AnyProps::borrowed(&mut value.props),
37 helper: ComponentHelper::<T>::boxed(),
38 }
39 }
40}
41
42impl<'a, 'b: 'a> From<&'a mut AnyElement<'b>> for AnyElement<'b> {
43 fn from(value: &'a mut AnyElement<'b>) -> Self {
44 Self {
45 key: value.key.clone(),
46 props: value.props.borrow(),
47 helper: value.helper.copy(),
48 }
49 }
50}
51
52impl<'a> ElementExt for AnyElement<'a> {
53 fn key(&self) -> &ElementKey {
54 &self.key
55 }
56
57 fn helper(&self) -> Box<dyn ComponentHelperExt> {
58 self.helper.copy()
59 }
60
61 fn props_mut(&mut self) -> AnyProps {
62 self.props.borrow()
63 }
64
65 fn render(&mut self) -> io::Result<()> {
66 let terminal = Terminal::new(CrossTerminal::new(false)?);
67 render(self, terminal)?;
68 Ok(())
69 }
70
71 async fn render_loop(&mut self) -> io::Result<()> {
72 let terminal = Terminal::new(CrossTerminal::new(false)?);
73 render_loop(self, terminal).await?;
74 Ok(())
75 }
76
77 async fn fullscreen(&mut self) -> io::Result<()> {
78 let terminal = Terminal::new(CrossTerminal::new(true)?);
79 render_loop(self, terminal).await?;
80 Ok(())
81 }
82}
83
84impl<'a> ElementExt for &mut AnyElement<'a> {
85 fn key(&self) -> &ElementKey {
86 &self.key
87 }
88
89 fn helper(&self) -> Box<dyn ComponentHelperExt> {
90 self.helper.copy()
91 }
92
93 fn props_mut(&mut self) -> AnyProps {
94 self.props.borrow()
95 }
96
97 fn render(&mut self) -> io::Result<()> {
98 let terminal = Terminal::new(CrossTerminal::new(false)?);
99 render(&mut **self, terminal)?;
100 Ok(())
101 }
102
103 async fn render_loop(&mut self) -> io::Result<()> {
104 let terminal = Terminal::new(CrossTerminal::new(false)?);
105 render_loop(&mut **self, terminal).await?;
106 Ok(())
107 }
108
109 async fn fullscreen(&mut self) -> io::Result<()> {
110 let terminal = Terminal::new(CrossTerminal::new(true)?);
111 render_loop(&mut **self, terminal).await?;
112 Ok(())
113 }
114}