summaryrefslogtreecommitdiffstats
path: root/src/tuice/element.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/tuice/element.rs')
-rw-r--r--src/tuice/element.rs51
1 files changed, 38 insertions, 13 deletions
diff --git a/src/tuice/element.rs b/src/tuice/element.rs
index fe859e79..d7c1b432 100644
--- a/src/tuice/element.rs
+++ b/src/tuice/element.rs
@@ -1,18 +1,43 @@
-use enum_dispatch::enum_dispatch;
-use tui::{layout::Rect, Frame};
+use std::io::Stdout;
-use super::{
- Block, Bounds, Carousel, Container, DrawContext, Event, Flex, LayoutNode, Shortcut, Size,
- Status, TextTable, TmpComponent,
+use tui::{
+ backend::{Backend, CrosstermBackend},
+ layout::Rect,
+ Frame,
};
+use super::*;
+
/// An [`Element`] is an instantiated [`Component`].
-#[enum_dispatch(TmpComponent<Message>)]
-pub enum Element<'a, Message> {
- Block,
- Carousel,
- Container(Container<'a, Message>),
- Flex(Flex<'a, Message>),
- Shortcut,
- TextTable(TextTable<'a, Message>),
+pub struct Element<'a, Message, B = CrosstermBackend<Stdout>>
+where
+ B: Backend,
+{
+ component: Box<dyn Component<Message, B> + 'a>,
+}
+
+impl<'a, Message, B> Element<'a, Message, B>
+where
+ B: Backend,
+{
+ pub fn new<C: Component<Message, B> + 'a>(component: C) -> Self {
+ Self {
+ component: Box::new(component),
+ }
+ }
+
+ /// Draws the element.
+ pub fn draw(&mut self, context: DrawContext<'_>, frame: &mut Frame<'_, B>) {
+ self.component.draw(context, frame)
+ }
+
+ /// How an element should react to an [`Event`].
+ pub fn on_event(&mut self, area: Rect, event: Event, messages: &mut Vec<Message>) -> Status {
+ self.component.on_event(area, event, messages)
+ }
+
+ /// How an element should size itself and its children, given some [`Bounds`].
+ pub fn layout(&self, bounds: Bounds, node: &mut LayoutNode) -> Size {
+ self.component.layout(bounds, node)
+ }
}