Skip to main content

bee_tui/
components.rs

1use crossterm::event::{KeyEvent, MouseEvent};
2use ratatui::{
3    Frame,
4    layout::{Rect, Size},
5};
6use tokio::sync::mpsc::UnboundedSender;
7
8use crate::{action::Action, config::Config, tui::Event};
9
10pub mod command_log;
11pub mod health;
12pub mod stamps;
13
14/// `Component` is a trait that represents a visual and interactive element of the user interface.
15///
16/// Implementors of this trait can be registered with the main application loop and will be able to
17/// receive events, update state, and be rendered on the screen.
18pub trait Component {
19    /// Register an action handler that can send actions for processing if necessary.
20    ///
21    /// # Arguments
22    ///
23    /// * `tx` - An unbounded sender that can send actions.
24    ///
25    /// # Returns
26    ///
27    /// * [`color_eyre::Result<()>`] - An Ok result or an error.
28    fn register_action_handler(&mut self, tx: UnboundedSender<Action>) -> color_eyre::Result<()> {
29        let _ = tx; // to appease clippy
30        Ok(())
31    }
32    /// Register a configuration handler that provides configuration settings if necessary.
33    ///
34    /// # Arguments
35    ///
36    /// * `config` - Configuration settings.
37    ///
38    /// # Returns
39    ///
40    /// * [`color_eyre::Result<()>`] - An Ok result or an error.
41    fn register_config_handler(&mut self, config: Config) -> color_eyre::Result<()> {
42        let _ = config; // to appease clippy
43        Ok(())
44    }
45    /// Initialize the component with a specified area if necessary.
46    ///
47    /// # Arguments
48    ///
49    /// * `area` - Rectangular area to initialize the component within.
50    ///
51    /// # Returns
52    ///
53    /// * [`color_eyre::Result<()>`] - An Ok result or an error.
54    fn init(&mut self, area: Size) -> color_eyre::Result<()> {
55        let _ = area; // to appease clippy
56        Ok(())
57    }
58    /// Handle incoming events and produce actions if necessary.
59    ///
60    /// # Arguments
61    ///
62    /// * `event` - An optional event to be processed.
63    ///
64    /// # Returns
65    ///
66    /// * [`color_eyre::Result<Option<Action>>`] - An action to be processed or none.
67    fn handle_events(&mut self, event: Option<Event>) -> color_eyre::Result<Option<Action>> {
68        let action = match event {
69            Some(Event::Key(key_event)) => self.handle_key_event(key_event)?,
70            Some(Event::Mouse(mouse_event)) => self.handle_mouse_event(mouse_event)?,
71            _ => None,
72        };
73        Ok(action)
74    }
75    /// Handle key events and produce actions if necessary.
76    ///
77    /// # Arguments
78    ///
79    /// * `key` - A key event to be processed.
80    ///
81    /// # Returns
82    ///
83    /// * [`color_eyre::Result<Option<Action>>`] - An action to be processed or none.
84    fn handle_key_event(&mut self, key: KeyEvent) -> color_eyre::Result<Option<Action>> {
85        let _ = key; // to appease clippy
86        Ok(None)
87    }
88    /// Handle mouse events and produce actions if necessary.
89    ///
90    /// # Arguments
91    ///
92    /// * `mouse` - A mouse event to be processed.
93    ///
94    /// # Returns
95    ///
96    /// * [`color_eyre::Result<Option<Action>>`] - An action to be processed or none.
97    fn handle_mouse_event(&mut self, mouse: MouseEvent) -> color_eyre::Result<Option<Action>> {
98        let _ = mouse; // to appease clippy
99        Ok(None)
100    }
101    /// Update the state of the component based on a received action. (REQUIRED)
102    ///
103    /// # Arguments
104    ///
105    /// * `action` - An action that may modify the state of the component.
106    ///
107    /// # Returns
108    ///
109    /// * [`color_eyre::Result<Option<Action>>`] - An action to be processed or none.
110    fn update(&mut self, action: Action) -> color_eyre::Result<Option<Action>> {
111        let _ = action; // to appease clippy
112        Ok(None)
113    }
114    /// Render the component on the screen. (REQUIRED)
115    ///
116    /// # Arguments
117    ///
118    /// * `f` - A frame used for rendering.
119    /// * `area` - The area in which the component should be drawn.
120    ///
121    /// # Returns
122    ///
123    /// * [`color_eyre::Result<()>`] - An Ok result or an error.
124    fn draw(&mut self, frame: &mut Frame, area: Rect) -> color_eyre::Result<()>;
125}