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