Skip to main content

ez_tui/components/concrete/
tick_indicator.rs

1use crate::{Component, EzArgs, EzCptIds, EzEvent, EzMsg, EzState, IndicatorMock, State};
2
3use crate::types::palette::EzPalette;
4use eztui_derive::MockComponent;
5use ratatui::style::palette::tailwind::SLATE;
6use std::fmt::Debug;
7use std::marker::PhantomData;
8
9/// A component to display a colored indicator alternating colors based on the tick rate of the terminal.
10#[derive(MockComponent, Debug)]
11pub struct TickIndicatorCpt<CA, CM>
12where
13    CA: EzArgs,
14    CM: EzMsg,
15{
16    component: IndicatorMock,
17    phantom_args: PhantomData<CA>,
18    phantom_msg: PhantomData<CM>,
19}
20
21impl<CA, CM> Default for TickIndicatorCpt<CA, CM>
22where
23    CA: EzArgs,
24    CM: EzMsg,
25{
26    fn default() -> Self {
27        Self {
28            component: IndicatorMock::with_palette("TPS".into(), &(SLATE.into())),
29            phantom_args: PhantomData,
30            phantom_msg: PhantomData,
31        }
32    }
33}
34impl<CA, CM> TickIndicatorCpt<CA, CM>
35where
36    CA: EzArgs,
37    CM: EzMsg,
38{
39    /// Create a new [`TickIndicatorCpt`] component with two styles derived from one [`Palette`].
40    #[must_use]
41    pub fn with_palette(palette: &EzPalette) -> Self {
42        Self {
43            component: IndicatorMock::with_palette("TPS".into(), palette),
44            phantom_args: PhantomData,
45            phantom_msg: PhantomData,
46        }
47    }
48
49    /// Create a new [`TickIndicatorCpt`] component with two styles derived from two [`Palette`]s.
50    #[must_use]
51    pub fn with_palettes(palette_a: &EzPalette, palette_b: &EzPalette) -> Self {
52        Self {
53            component: IndicatorMock::with_palettes("TPS".into(), palette_a, palette_b),
54            phantom_args: PhantomData,
55            phantom_msg: PhantomData,
56        }
57    }
58}
59impl<CID, CA, CS, CM> Component<CID, CA, CS, CM> for TickIndicatorCpt<CA, CM>
60where
61    CID: EzCptIds,
62    CA: EzArgs,
63    CS: EzState,
64    CM: EzMsg,
65{
66    fn on_event(&mut self, ev: EzEvent<CID, CM>, _: &mut State<CS>) -> Vec<EzEvent<CID, CM>> {
67        if EzEvent::Tick == ev {
68            self.component.cycle();
69        }
70        vec![]
71    }
72}