mecomp_tui/ui/components/
mod.rs

1pub mod content_view;
2pub mod control_panel;
3pub mod queuebar;
4pub mod sidebar;
5
6use crossterm::event::{KeyEvent, MouseEvent};
7use ratatui::{Frame, layout::Rect};
8use tokio::sync::mpsc::UnboundedSender;
9
10use crate::state::action::Action;
11
12use super::AppState;
13
14#[derive(Debug, Clone, Copy)]
15pub struct RenderProps {
16    pub area: Rect,
17    pub is_focused: bool,
18}
19
20pub trait Component {
21    fn new(state: &AppState, action_tx: UnboundedSender<Action>) -> Self
22    where
23        Self: Sized;
24    #[must_use]
25    fn move_with_state(self, state: &AppState) -> Self
26    where
27        Self: Sized;
28
29    fn name(&self) -> &str;
30
31    fn handle_key_event(&mut self, key: KeyEvent);
32
33    fn handle_mouse_event(&mut self, _mouse: MouseEvent, _area: Rect);
34}
35
36pub trait ComponentRender<Props> {
37    /// Render the border of the view, and return the props updated with the remaining area for the view.
38    fn render_border(&self, frame: &mut Frame, props: Props) -> Props;
39
40    /// Render the view's content.
41    fn render_content(&self, frame: &mut Frame, props: Props);
42
43    /// Render the view (border and content).
44    fn render(&self, frame: &mut Frame, props: Props) {
45        let props = self.render_border(frame, props);
46        self.render_content(frame, props);
47    }
48}