node_launchpad/components.rs
1// Copyright 2024 MaidSafe.net limited.
2//
3// This SAFE Network Software is licensed to you under The General Public License (GPL), version 3.
4// Unless required by applicable law or agreed to in writing, the SAFE Network Software distributed
5// under the GPL Licence is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
6// KIND, either express or implied. Please review the Licences for the specific language governing
7// permissions and limitations relating to use of the SAFE Network Software.
8
9use color_eyre::eyre::Result;
10use crossterm::event::{KeyEvent, MouseEvent};
11use ratatui::layout::Rect;
12use tokio::sync::mpsc::UnboundedSender;
13
14use crate::{
15 action::Action,
16 config::Config,
17 tui::{Event, Frame},
18};
19
20pub mod footer;
21pub mod header;
22pub mod help;
23pub mod options;
24pub mod popup;
25pub mod status;
26pub mod utils;
27
28/// `Component` is a trait that represents a visual and interactive element of the user interface.
29/// Implementors of this trait can be registered with the main application loop and will be able to receive events,
30/// 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 /// * `Result<()>` - An Ok result or an error.
41 #[expect(unused_variables)]
42 fn register_action_handler(&mut self, tx: UnboundedSender<Action>) -> Result<()> {
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 /// * `Result<()>` - An Ok result or an error.
54 #[expect(unused_variables)]
55 fn register_config_handler(&mut self, config: Config) -> Result<()> {
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 /// * `Result<()>` - An Ok result or an error.
67 fn init(&mut self, _area: Rect) -> Result<()> {
68 Ok(())
69 }
70 /// Handle incoming events and produce actions if necessary.
71 ///
72 /// # Arguments
73 ///
74 /// * `event` - An optional event to be processed.
75 ///
76 /// # Returns
77 ///
78 /// * `Result<Option<Action>>` - An action to be processed or none.
79 fn handle_events(&mut self, event: Option<Event>) -> Result<Vec<Action>> {
80 let r = match event {
81 Some(Event::Key(key_event)) => self.handle_key_events(key_event)?,
82 Some(Event::Mouse(mouse_event)) => self.handle_mouse_events(mouse_event)?,
83 _ => vec![],
84 };
85 Ok(r)
86 }
87 /// Handle key events and produce actions if necessary.
88 ///
89 /// # Arguments
90 ///
91 /// * `key` - A key event to be processed.
92 ///
93 /// # Returns
94 ///
95 /// * `Result<Option<Action>>` - An action to be processed or none.
96 #[expect(unused_variables)]
97 fn handle_key_events(&mut self, key: KeyEvent) -> Result<Vec<Action>> {
98 Ok(vec![])
99 }
100 /// Handle mouse events and produce actions if necessary.
101 ///
102 /// # Arguments
103 ///
104 /// * `mouse` - A mouse event to be processed.
105 ///
106 /// # Returns
107 ///
108 /// * `Result<Option<Action>>` - An action to be processed or none.
109 #[expect(unused_variables)]
110 fn handle_mouse_events(&mut self, mouse: MouseEvent) -> Result<Vec<Action>> {
111 Ok(vec![])
112 }
113 /// Update the state of the component based on a received action. (REQUIRED)
114 ///
115 /// # Arguments
116 ///
117 /// * `action` - An action that may modify the state of the component.
118 ///
119 /// # Returns
120 ///
121 /// * `Result<Option<Action>>` - An action to be processed or none.
122 #[expect(unused_variables)]
123 fn update(&mut self, action: Action) -> Result<Option<Action>> {
124 Ok(None)
125 }
126 /// Render the component on the screen. (REQUIRED)
127 ///
128 /// # Arguments
129 ///
130 /// * `f` - A frame used for rendering.
131 /// * `area` - The area in which the component should be drawn.
132 ///
133 /// # Returns
134 ///
135 /// * `Result<()>` - An Ok result or an error.
136 fn draw(&mut self, f: &mut Frame<'_>, area: Rect) -> Result<()>;
137}