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