1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
use crate::app::{App, AppResult, TuiSection};
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};

/// Handles the key events and updates the state of [`App`].
pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> {
    match key_event.code {
        // Exit application on `ESC` or `q`
        KeyCode::Esc | KeyCode::Char('q') => {
            app.quit();
        }
        // Exit application on `Ctrl-C`
        KeyCode::Char('c') | KeyCode::Char('C') => {
            if key_event.modifiers == KeyModifiers::CONTROL {
                app.quit();
            }
        }
        // Counter handlers
        KeyCode::Left | KeyCode::Char('h') => {
            app.pages.unselect();
            app.select_prev_section();
            match app.selected_section {
                TuiSection::BOOKS => {}
                TuiSection::PAGES => app.books.previous(),
                TuiSection::CONTENT => app.pages.previous(),
            }
        }
        KeyCode::Right | KeyCode::Char('l') => {
            match app.selected_section {
                TuiSection::BOOKS => app.pages.next(),
                TuiSection::PAGES => {}
                _ => {}
            }
            app.select_next_section();
        }
        KeyCode::Up | KeyCode::Char('k') => match app.selected_section {
            TuiSection::BOOKS => app.books.previous(),
            TuiSection::PAGES => app.pages.previous(),
            _ => {}
        },
        KeyCode::Down | KeyCode::Char('j') => match app.selected_section {
            TuiSection::BOOKS => app.books.next(),
            TuiSection::PAGES => app.pages.next(),
            _ => {}
        },
        // Other handlers you could add here.
        _ => {}
    }
    Ok(())
}