Skip to main content

ez_tui/components/concrete/
legends.rs

1use crate::inputs::legend::Legend;
2use crate::{Component, EzArgs, EzCptIds, EzMsg, EzState, LegendMock, MockComponent, Props, Theme};
3use eztui_derive::MockProps;
4use ratatui::buffer::Buffer;
5use ratatui::layout::{Constraint, Layout, Rect};
6use std::fmt::Debug;
7use std::marker::PhantomData;
8
9/// A component to display a colored indicator alternating colors based on the frame rate of the terminal.
10#[derive(Debug, MockProps)]
11pub struct Legends<CA, CM>
12where
13    CA: EzArgs,
14    CM: EzMsg,
15{
16    components: Vec<LegendMock>,
17    props: Props,
18    phantom_args: PhantomData<CA>,
19    phantom_msg: PhantomData<CM>,
20}
21
22impl<CA, CM> Legends<CA, CM>
23where
24    CA: EzArgs,
25    CM: EzMsg,
26{
27    /// Create a new [`Legends`] component with the given [`Legend`]s.
28    #[must_use]
29    pub fn new(legends: Vec<Legend>) -> Self {
30        let cpts = legends.into_iter().map(LegendMock::with_hotkeys).collect();
31        Self {
32            components: cpts,
33            props: Props::default(),
34            phantom_args: PhantomData,
35            phantom_msg: PhantomData,
36        }
37    }
38
39    /// Reset this [`Legends`] component with the given [`Legend`]s.
40    pub fn set_legends(&mut self, legends: Vec<Legend>) {
41        self.components = legends.into_iter().map(LegendMock::with_hotkeys).collect();
42    }
43}
44impl<CID, CA, CS, CM> Component<CID, CA, CS, CM> for Legends<CA, CM>
45where
46    CID: EzCptIds,
47    CA: EzArgs,
48    CS: EzState,
49    CM: EzMsg,
50{
51}
52impl<CA, CM> MockComponent for Legends<CA, CM>
53where
54    CA: EzArgs,
55    CM: EzMsg,
56{
57    fn draw(&mut self, area: Rect, buf: &mut Buffer, theme: &Theme) {
58        let constraints = self
59            .components
60            .iter()
61            .map(|_| Constraint::Length(1))
62            .collect::<Vec<_>>();
63        let layout = Layout::vertical(&constraints);
64        let rects = layout.split(area);
65        for (i, cpt) in self.components.iter_mut().enumerate() {
66            cpt.draw(rects[i], buf, theme);
67        }
68    }
69}