musichoard/src/tui/handler.rs

53 lines
1.4 KiB
Rust
Raw Normal View History

2023-04-11 09:04:00 +02:00
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
2023-04-12 10:20:20 +02:00
use super::{app::App, event::{Event, EventReceiver}};
2023-04-11 09:04:00 +02:00
2023-04-12 10:20:20 +02:00
pub struct EventHandler {
events: EventReceiver,
}
impl EventHandler {
pub fn new(events: EventReceiver) -> Self {
EventHandler { events }
}
pub fn handle_next_event(&mut self, app: &mut App) {
match self.events.recv() {
Event::Key(key_event) => Self::handle_key_event(app, key_event),
Event::Mouse(_) => {}
Event::Resize(_, _) => {}
2023-04-11 09:04:00 +02:00
}
2023-04-12 10:20:20 +02:00
}
fn handle_key_event(app: &mut App, key_event: KeyEvent) {
match key_event.code {
// Exit application on `ESC` or `q`.
KeyCode::Esc | KeyCode::Char('q') => {
2023-04-11 09:04:00 +02:00
app.quit();
}
2023-04-12 10:20:20 +02:00
// Exit application on `Ctrl-C`.
KeyCode::Char('c') | KeyCode::Char('C') => {
if key_event.modifiers == KeyModifiers::CONTROL {
app.quit();
}
}
// Category change.
KeyCode::Left => {
app.decrement_category();
}
KeyCode::Right => {
app.increment_category();
}
// Selection change.
KeyCode::Up => {
app.decrement_selection();
}
KeyCode::Down => {
app.increment_selection();
}
// Other keys.
_ => {}
2023-04-11 09:04:00 +02:00
}
}
}