Pass actions to components

This commit is contained in:
Shav Kinderlehrer 2024-03-11 09:21:46 -04:00
parent 832eb17a68
commit 0c5e8ab544
5 changed files with 15 additions and 4 deletions

View File

@ -128,6 +128,12 @@ impl App {
fn handle_action(&mut self, action: AppAction) -> Result<()> {
match action {
AppAction::Quit => Ok(self.quit()?),
_ => {
for component in &mut self.components {
component.handle_action(action.clone());
}
Ok(())
}
}
}
}

View File

@ -1,5 +1,8 @@
#[derive(Default, Clone, Copy)]
#[derive(Default, Clone)]
pub enum AppAction {
StatusBarMessage(String),
StatusBarError(String),
StatusBarInput(String),
#[default]
Quit,
}

View File

@ -10,6 +10,9 @@ pub trait Component {
Ok(())
}
#[allow(unused)]
fn handle_action(&mut self, action: AppAction) {}
#[allow(unused)]
fn handle_event(&mut self, event: AppEvent) -> Result<Option<AppAction>> {
match event {

View File

@ -77,7 +77,7 @@ impl Component for GlobalKeys {
for key_command in &mut self.key_commands {
if key_command.key_code == key_event {
return Ok(key_command.action);
return Ok(key_command.action.clone());
}
}
}

View File

@ -10,7 +10,6 @@ use eyre::Result;
fn main() -> Result<()> {
let mut app = app::App::new(std::time::Duration::from_millis(10))?;
app.run()?;
app.quit()
app.run()
}