From b0331863dc971bd008221788a6fd7ab180e4416f Mon Sep 17 00:00:00 2001 From: ShenMian Date: Mon, 23 Dec 2024 22:24:11 +0000 Subject: [PATCH] refactor(app): improve event handling and code organization - Update event loop to use method calls for event handling - Implement handle_key_events and handle_mouse_events as App methods - Remove redundant Ok() import from anyhow - Update comments and improve code readability --- src/app.rs | 56 +++++++++++++++++++++++++++--------------------------- 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/src/app.rs b/src/app.rs index 451d771..b9fb4b7 100644 --- a/src/app.rs +++ b/src/app.rs @@ -1,6 +1,6 @@ use std::time::{Duration, Instant}; -use anyhow::{Ok, Result}; +use anyhow::Result; use crossterm::event::{KeyCode, KeyEvent, KeyModifiers, MouseEvent}; use ratatui::{ layout::{Constraint, Layout}, @@ -57,8 +57,8 @@ impl App { match self.tui.events.next().await? { Event::Update => self.update().await, Event::Render => self.render()?, - Event::Key(event) => handle_key_events(event, self).await?, - Event::Mouse(event) => handle_mouse_events(event, self).await?, + Event::Key(event) => self.handle_key_events(event).await?, + Event::Mouse(event) => self.handle_mouse_events(event).await?, } } @@ -95,7 +95,7 @@ impl App { Ok(()) } - /// Handles the tick event of the terminal. + /// Handle update events. pub async fn update(&mut self) { const OBJECT_UPDATE_INTERVAL: Duration = Duration::from_secs(2 * 60); @@ -106,32 +106,32 @@ impl App { } } - /// Set running to false to quit the application. - pub fn quit(&mut self) { - self.running = false; - } -} - -async fn handle_key_events(event: KeyEvent, app: &mut App) -> Result<()> { - match event.code { - // Exit application on `ESC` - KeyCode::Esc => { - app.quit(); - } - // Exit application on `Ctrl-C` - KeyCode::Char('c') => { - if event.modifiers == KeyModifiers::CONTROL { - app.quit(); + async fn handle_key_events(&mut self, event: KeyEvent) -> Result<()> { + match event.code { + // Exit application on `ESC` + KeyCode::Esc => { + self.quit(); } + // Exit application on `Ctrl-C` + KeyCode::Char('c') => { + if event.modifiers == KeyModifiers::CONTROL { + self.quit(); + } + } + _ => {} } - _ => {} + Ok(()) + } + + async fn handle_mouse_events(&mut self, event: MouseEvent) -> Result<()> { + world_map::handle_mouse_events(event, self).await?; + object_information::handle_mouse_events(event, self).await?; + satellites::handle_mouse_events(event, self).await?; + Ok(()) } - Ok(()) -} -async fn handle_mouse_events(event: MouseEvent, app: &mut App) -> Result<()> { - world_map::handle_mouse_events(event, app).await?; - object_information::handle_mouse_events(event, app).await?; - satellites::handle_mouse_events(event, app).await?; - Ok(()) + /// Set running to false to quit the application. + pub fn quit(&mut self) { + self.running = false; + } }