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

View File

@ -76,29 +76,35 @@ pub trait Component: Downcast {
/// ///
/// * `Result<Option<Action>>` - An action to be processed or none. /// * `Result<Option<Action>>` - An action to be processed or none.
fn handle_events(&mut self, event: Option<Event>) -> Result<Vec<Action>> { fn handle_events(&mut self, event: Option<Event>) -> Result<Vec<Action>> {
let mut actions = vec![]; if self.is_active() {
let mut actions = vec![];
let action = match event { let action = match event {
Some(Event::Key(key_event)) => self.handle_key_events(key_event)?, Some(Event::Key(key_event)) => self.handle_key_events(key_event)?,
Some(Event::Mouse(mouse_event)) => self.handle_mouse_events(mouse_event)?, Some(Event::Mouse(mouse_event)) => self.handle_mouse_events(mouse_event)?,
Some(Event::Tick) => self.handle_tick_event()?, Some(Event::Tick) => self.handle_tick_event()?,
Some(Event::Render) => self.handle_frame_event()?, Some(Event::Render) => self.handle_frame_event()?,
Some(Event::Paste(ref event)) => self.handle_paste_event(event.clone())?, Some(Event::Paste(ref event)) => self.handle_paste_event(event.clone())?,
_ => None, _ => None,
}; };
if let Some(action) = action { if let Some(action) = action {
actions.push(action); actions.push(action);
}
if let Some(children) = self.get_children() {
for child in children.values_mut() {
let child_actions = child.handle_events(event.clone())?;
actions.extend(child_actions);
} }
}
Ok(actions) 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. /// Handle key events and produce actions if necessary.
@ -288,7 +294,9 @@ pub trait Component: Downcast {
/// ///
/// # Arguments /// # Arguments
/// * `active` - The active state of the component. /// * `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); impl_downcast!(Component);
@ -299,9 +307,13 @@ impl_downcast!(Component);
/// created to allow to easily override the default `update` method of a component implementation /// 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. /// and still be able to call the children's `update` method.
pub fn update_children<T: Component + ?Sized>(this: &mut T, action: Action) -> Result<()> { pub fn update_children<T: Component + ?Sized>(this: &mut T, action: Action) -> Result<()> {
if let Some(children) = this.get_children() { if this.is_active() {
for child in children.values_mut() { if let Some(children) = this.get_children() {
child.update(action.clone())?; for child in children.values_mut() {
if child.is_active() {
child.update(action.clone())?;
}
}
} }
} }
@ -317,15 +329,32 @@ pub fn pass_message_to_children<T: Component + ?Sized>(
this: &mut T, this: &mut T,
message: String, message: String,
) -> Result<()> { ) -> Result<()> {
if let Some(children) = this.get_children() { if this.is_active() {
for child in children.values_mut() { if let Some(children) = this.get_children() {
child.receive_message(message.clone())?; for child in children.values_mut() {
if child.is_active() {
child.receive_message(message.clone())?;
}
}
} }
} }
Ok(()) 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. /// Initialize the children of a component.
/// ///
/// This helper function is used to initialize the children of a component. It was created to /// 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 mod component {
pub use super::super::framework::component::{ pub use super::super::framework::component::{
child_downcast, child_downcast_mut, init_children, pass_action_handler_to_children, 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,
}; };
} }