diff options
author | Shav Kinderlehrer <[email protected]> | 2024-03-06 11:45:18 -0500 |
---|---|---|
committer | Shav Kinderlehrer <[email protected]> | 2024-03-06 11:45:18 -0500 |
commit | a5dbccee4f22de991e33449ae1d1835269c6075d (patch) | |
tree | 8a530c8efe28afb61768f0d44c7e71c7049829e2 /src/app.rs | |
parent | 557d3f32fd2ac7a21bd3da01e8e903db16a31e7e (diff) | |
download | molehole-a5dbccee4f22de991e33449ae1d1835269c6075d.tar.gz molehole-a5dbccee4f22de991e33449ae1d1835269c6075d.zip |
Fix event handling
Diffstat (limited to 'src/app.rs')
-rw-r--r-- | src/app.rs | 103 |
1 files changed, 62 insertions, 41 deletions
@@ -1,8 +1,8 @@ use crossterm::event::Event; use eyre::Result; -use ratatui::prelude::*; use std::time::Duration; +use crate::app_action::AppAction; use crate::app_event::AppEvent; use crate::component::Component; use crate::components; @@ -12,71 +12,92 @@ pub struct App { pub tui: tui::Tui, pub tick_rate: Duration, pub components: Vec<Box<dyn Component>>, + + should_quit: bool, } impl App { pub fn new(tick_rate: Duration) -> Result<Self> { let tui = tui::init()?; + let global_keys = components::global_keys::GlobalKeys::default(); let hello_world = components::hello_world::HelloWorld::default(); - let hello_world1 = components::hello_world::HelloWorld::default(); - let hello_world2 = components::hello_world::HelloWorld::default(); - let hello_world3 = components::hello_world::HelloWorld::default(); Ok(Self { tui, tick_rate, - components: vec![ - Box::new(hello_world), - Box::new(hello_world1), - Box::new(hello_world2), - Box::new(hello_world3), - ], + components: vec![Box::new(hello_world), Box::new(global_keys)], + should_quit: false, }) } pub fn run(&mut self) -> Result<()> { + for component in self.components.iter_mut() { + component.init()?; + } + loop { - let event: Option<AppEvent> = match tui::get_event(self.tick_rate)? - { - Some(event) => match event { - Event::Key(key) => Some(AppEvent::Key(key)), - Event::Mouse(mouse) => Some(AppEvent::Mouse(mouse)), - Event::FocusGained => todo!(), - Event::FocusLost => todo!(), - Event::Paste(_) => todo!(), - Event::Resize(_, _) => todo!(), - }, - None => None, - }; - - if event.is_some() { - for component in self.components.iter_mut() { - let _ = component.handle_event(event.expect(""))?; - } + if self.should_quit { + break Ok(()); } - self.tui.draw(|frame| { - let layout = Layout::default() - .direction(Direction::Vertical) - .constraints([ - Constraint::Percentage(25), - Constraint::Percentage(25), - Constraint::Percentage(25), - Constraint::Percentage(25), - ]) - .split(frame.size()); - - for (i, component) in self.components.iter_mut().enumerate() { - let _ = component.render(frame, layout[i]); + self.draw()?; + } + } + + pub fn draw(&mut self) -> Result<()> { + let event: Option<AppEvent> = match tui::get_event(self.tick_rate)? { + Some(event) => match event { + Event::Key(key) => Some(AppEvent::Key(key)), + Event::Mouse(mouse) => Some(AppEvent::Mouse(mouse)), + Event::FocusGained => todo!(), + Event::FocusLost => todo!(), + Event::Paste(_) => todo!(), + Event::Resize(_, _) => todo!(), + }, + None => None, + }; + + if let Some(event) = event { + let mut actions: Vec<AppAction> = vec![]; + for component in self.components.iter_mut() { + match component.handle_event(event)? { + Some(action) => actions.push(action), + None => (), } - })?; + } + + for action in actions { + self.handle_action(action)?; + } } + + if self.should_quit { + return Ok(()); + } + + self.tui.draw(|frame| { + for (_i, component) in self.components.iter_mut().enumerate() { + match component.render(frame, frame.size()) { + Ok(_) => (), + Err(_) => (), + } + } + })?; + + Ok(()) } pub fn quit(&mut self) -> Result<()> { tui::restore()?; + self.should_quit = true; Ok(()) } + + fn handle_action(&mut self, action: AppAction) -> Result<()> { + match action { + AppAction::Quit => Ok(self.quit()?), + } + } } |