handler.rs 1009 B

1234567891011121314151617181920212223242526272829303132333435
  1. use crate::{
  2. app::{App, AppResult},
  3. backend::event::BackendEvent,
  4. };
  5. use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
  6. /// Handles the key events and updates the state of [`App`].
  7. pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> {
  8. match key_event.code {
  9. // Exit application on `ESC` or `q`
  10. KeyCode::Esc | KeyCode::Char('q') => {
  11. app.quit();
  12. }
  13. // Exit application on `Ctrl-C`
  14. KeyCode::Char('c') | KeyCode::Char('C') => {
  15. if key_event.modifiers == KeyModifiers::CONTROL {
  16. app.quit();
  17. }
  18. }
  19. // Counter handlers
  20. KeyCode::Right => {
  21. app.increment_counter();
  22. }
  23. KeyCode::Left => {
  24. app.decrement_counter();
  25. }
  26. // Other handlers you could add here.
  27. _ => {}
  28. }
  29. Ok(())
  30. }
  31. pub fn handle_backend_events(_backend_event: BackendEvent, _app: &mut App) -> AppResult<()> {
  32. return Ok(());
  33. }