39 lines
1022 B
Rust
39 lines
1022 B
Rust
|
|
use ratatui::crossterm::event;
|
||
|
|
use ratatui::crossterm::event::{Event, KeyCode, KeyEvent, MouseEvent};
|
||
|
|
use std::time::Duration;
|
||
|
|
|
||
|
|
pub enum Action {
|
||
|
|
Noop,
|
||
|
|
Quit,
|
||
|
|
}
|
||
|
|
|
||
|
|
pub trait ClideComponent {
|
||
|
|
fn handle_events(&mut self) -> Action {
|
||
|
|
if !event::poll(Duration::from_millis(250)).expect("event poll failed") {
|
||
|
|
return Action::Noop;
|
||
|
|
}
|
||
|
|
|
||
|
|
let key_event = event::read().expect("event read failed");
|
||
|
|
match key_event {
|
||
|
|
Event::Key(key_event) => self.handle_key_events(key_event),
|
||
|
|
Event::Mouse(mouse_event) => self.handle_mouse_events(mouse_event),
|
||
|
|
_ => Action::Noop,
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
fn handle_key_events(&mut self, key: KeyEvent) -> Action {
|
||
|
|
match key.code {
|
||
|
|
KeyCode::Char('q') => Action::Quit,
|
||
|
|
_ => Action::Noop,
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
fn handle_mouse_events(&mut self, mouse: MouseEvent) -> Action {
|
||
|
|
Action::Noop
|
||
|
|
}
|
||
|
|
|
||
|
|
fn update(&mut self, action: Action) -> Action {
|
||
|
|
Action::Noop
|
||
|
|
}
|
||
|
|
}
|