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