From f638f4bd1e3a03bc2bdd5f9dcd57d4830fd3c553 Mon Sep 17 00:00:00 2001 From: Shav Kinderlehrer Date: Sun, 24 Mar 2024 14:18:53 -0400 Subject: Overhaul events system + add url_manager --- src/app.rs | 56 ++++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 44 insertions(+), 12 deletions(-) (limited to 'src/app.rs') diff --git a/src/app.rs b/src/app.rs index b9c3492..7d398f0 100644 --- a/src/app.rs +++ b/src/app.rs @@ -9,6 +9,7 @@ use crate::component::Component; use crate::components; use crate::keys::key_commands::KeyCommand; use crate::tui; + pub struct App { pub tui: tui::Tui, pub tick_rate: Duration, @@ -37,8 +38,16 @@ impl App { key_commands: self.key_commands.clone(), ..Default::default() }; - let status_bar = components::status::StatusBar::default(); - self.components = vec![Box::new(global_keys), Box::new(status_bar)]; + let status_bar = components::status::StatusBar { + message: "Press '?' to show the help menu".to_string(), + ..Default::default() + }; + let url_manager = components::url_manager::UrlManager::default(); + self.components = vec![ + Box::new(global_keys), + Box::new(status_bar), + Box::new(url_manager), + ]; for component in &mut self.components { component.init()?; @@ -67,16 +76,7 @@ impl App { }; if let Some(event) = event { - let mut actions: Vec = vec![]; - for component in &mut self.components { - if let Some(action) = component.handle_event(event.clone())? { - actions.push(action); - } - } - - for action in actions { - self.handle_action(action)?; - } + self.handle_event(event)?; } if self.should_quit { @@ -94,10 +94,28 @@ impl App { // status bar let _ = self.components[1].render(frame, layout[1]); + // global_keys let _ = self.components[0].render(frame, frame.size()); })?; + self.update()?; + + Ok(()) + } + + pub fn update(&mut self) -> Result<()> { + let mut events: Vec = vec![]; + for component in &mut self.components { + if let Some(event) = component.update() { + events.push(event); + } + } + + for event in events { + self.handle_event(event)?; + } + Ok(()) } @@ -119,4 +137,18 @@ impl App { } } } + + fn handle_event(&mut self, event: AppEvent) -> Result<()> { + let mut actions: Vec = vec![]; + for component in &mut self.components { + if let Some(action) = component.handle_event(event.clone()) { + actions.push(action); + } + } + + for action in actions { + self.handle_action(action)?; + } + Ok(()) + } } -- cgit v1.2.3