summaryrefslogtreecommitdiffstats
path: root/src/tuice/element.rs
blob: d7c1b432dbf1340ba2a253fe7b53c86bef3c1f93 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
use std::io::Stdout;

use tui::{
    backend::{Backend, CrosstermBackend},
    layout::Rect,
    Frame,
};

use super::*;

/// An [`Element`] is an instantiated [`Component`].
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)
    }
}