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
//! Types describing rendered components.

use std::{ops::Deref, sync::Arc};

use crate::{
  components::{Component, Empty},
  event::KeyEvent,
  terminal::{Frame, Rect},
};

#[derive(Clone)]
pub struct Any(Arc<dyn Element + Send + Sync>);

impl Any {
  pub fn new<C: Element + 'static + Send + Sync>(element: C) -> Self {
    Self(Arc::new(element))
  }
}

impl<'a> Default for Any {
  fn default() -> Self {
    Empty {}.into()
  }
}

impl Deref for Any {
  type Target = Arc<dyn Element + Send + Sync>;

  fn deref(&self) -> &Self::Target {
    &self.0
  }
}

impl<C: Component> From<C> for Any {
  fn from(component: C) -> Self {
    component.render()
  }
}

pub trait Element {
  fn on_key(&self, _event: KeyEvent) {}
  fn draw(&self, _rect: Rect, _frame: &mut Frame) {}
}