feat(cli): tui: extend is_active usage

This commit is contained in:
Lucas Colombo 2024-09-18 02:38:37 -03:00
parent 0bb4a205b7
commit c2c700899d
Signed by: lucas
GPG Key ID: EF34786CFEFFAE35
3 changed files with 76 additions and 37 deletions

View File

@ -105,9 +105,11 @@ impl App {
let mut actions = Vec::new();
for component in self.components.iter_mut() {
if component.is_active() {
let component_actions = component.handle_events(Some(e.clone()))?;
actions.extend(component_actions);
}
}
for action in actions {
self.send(action)?;
@ -129,11 +131,13 @@ impl App {
let mut errors = Vec::new();
tui.draw(|f| {
for component in self.components.iter_mut() {
if component.is_active() {
let r = component.draw(f, f.area());
if let Err(e) = r {
errors.push(format!("Failed to draw: {:?}", e));
}
}
}
})?;
for error in errors {
self.send(Action::Error(error)).unwrap();
@ -143,11 +147,13 @@ impl App {
let mut errors = Vec::new();
tui.draw(|f| {
for component in self.components.iter_mut() {
if component.is_active() {
let r = component.draw(f, f.area());
if let Err(e) = r {
errors.push(format!("Failed to draw: {:?}", e));
}
}
}
})?;
for error in errors {
self.send(Action::Error(error)).unwrap();
@ -157,16 +163,20 @@ impl App {
}
for component in self.components.iter_mut() {
if component.is_active() {
component.update(a.clone())?;
}
}
} else {
// unrecognized action, might be a custom component action
// send it to all components as a raw string
for component in self.components.iter_mut() {
if component.is_active() {
let _ = component.receive_message(action.clone());
}
}
}
}
if self.should_suspend {
tui.suspend()?;
self.send(Action::Resume)?;

View File

@ -76,6 +76,7 @@ pub trait Component: Downcast {
///
/// * `Result<Option<Action>>` - An action to be processed or none.
fn handle_events(&mut self, event: Option<Event>) -> Result<Vec<Action>> {
if self.is_active() {
let mut actions = vec![];
let action = match event {
@ -93,12 +94,17 @@ pub trait Component: Downcast {
if let Some(children) = self.get_children() {
for child in children.values_mut() {
if child.is_active() {
let child_actions = child.handle_events(event.clone())?;
actions.extend(child_actions);
}
}
}
Ok(actions)
} else {
Ok(vec![])
}
}
/// Handle key events and produce actions if necessary.
@ -288,7 +294,9 @@ pub trait Component: Downcast {
///
/// # Arguments
/// * `active` - The active state of the component.
fn set_active(&mut self, _active: bool) {}
fn set_active(&mut self, active: bool) {
set_active_on_children(self, active);
}
}
impl_downcast!(Component);
@ -299,11 +307,15 @@ impl_downcast!(Component);
/// created to allow to easily override the default `update` method of a component implementation
/// and still be able to call the children's `update` method.
pub fn update_children<T: Component + ?Sized>(this: &mut T, action: Action) -> Result<()> {
if this.is_active() {
if let Some(children) = this.get_children() {
for child in children.values_mut() {
if child.is_active() {
child.update(action.clone())?;
}
}
}
}
Ok(())
}
@ -317,15 +329,32 @@ pub fn pass_message_to_children<T: Component + ?Sized>(
this: &mut T,
message: String,
) -> Result<()> {
if this.is_active() {
if let Some(children) = this.get_children() {
for child in children.values_mut() {
if child.is_active() {
child.receive_message(message.clone())?;
}
}
}
}
Ok(())
}
/// Set active/inactive to the children of a component.
///
/// This helper function is used to set active/inactive to the children of a component. It was
/// created to allow to easily implement the default `set_active` method of a component
/// implementation and be able to call the children's `set_active` method.
pub fn set_active_on_children<T: Component + ?Sized>(this: &mut T, active: bool) {
if let Some(children) = this.get_children() {
for child in children.values_mut() {
child.set_active(active);
}
}
}
/// Initialize the children of a component.
///
/// This helper function is used to initialize the children of a component. It was created to

View File

@ -20,7 +20,7 @@ pub mod utils {
pub mod component {
pub use super::super::framework::component::{
child_downcast, child_downcast_mut, init_children, pass_action_handler_to_children,
pass_message_to_children, update_children,
pass_message_to_children, set_active_on_children, update_children,
};
}