use super::app::{App, Widget};
use crate::errors::AppResult;
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> {
let curr_widget = &mut app.widgets[app.widget_switcher as usize];
match key_event.code {
KeyCode::Char('q') => {
app.quit();
}
KeyCode::Esc => {
if app.show_popup {
app.toggle_popup();
}
}
KeyCode::Enter => {
if let Widget::QuestionList(_) = app.get_current_widget() {
app.toggle_popup();
app.update_question_in_popup()?;
}
}
KeyCode::Char('c') | KeyCode::Char('C') => {
if key_event.modifiers == KeyModifiers::CONTROL {
app.quit();
}
}
KeyCode::Char('p') | KeyCode::Char('P') => {
app.toggle_popup();
}
KeyCode::Up => match curr_widget {
super::app::Widget::QuestionList(ql) => {
ql.previous();
app.update_question_in_popup()?;
}
super::app::Widget::TopicTagList(tt) => tt.previous(),
},
KeyCode::Down => match curr_widget {
super::app::Widget::QuestionList(ql) => {
ql.next();
app.update_question_in_popup()?;
}
super::app::Widget::TopicTagList(tt) => tt.next(),
},
KeyCode::Left => app.prev_widget(),
KeyCode::Right => app.next_widget(),
_ => {}
}
app.update_question_list();
Ok(())
}