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