| 1 | use crate::app::{App, AppResult}; |
| 2 | use crossterm::event::{KeyCode, KeyEvent, KeyModifiers}; |
| 3 | |
| 4 | pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> { |
| 5 | match key_event.code { |
| 6 | KeyCode::Char('q') | KeyCode::Esc => app.quit(), |
| 7 | KeyCode::Char('c') | KeyCode::Char('C') => { |
| 8 | if key_event.modifiers == KeyModifiers::CONTROL { |
| 9 | app.quit(); |
| 10 | } |
| 11 | } |
| 12 | |
| 13 | KeyCode::Char('j') => { |
| 14 | app.song_list.next(); |
| 15 | } |
| 16 | |
| 17 | KeyCode::Char('k') => { |
| 18 | app.song_list.prev(); |
| 19 | } |
| 20 | |
| 21 | KeyCode::Enter | KeyCode::Char('l') => { |
| 22 | let song = app.conn.get_song_with_only_filename( |
| 23 | app.conn.songs_filenames.get(app.song_list.index).unwrap(), |
| 24 | ); |
| 25 | app.conn.push(&song)?; |
| 26 | // app.update_queue(); |
| 27 | } |
| 28 | |
| 29 | // Playback controls |
| 30 | // Toggle Pause |
| 31 | KeyCode::Char('p') => { |
| 32 | app.conn.toggle_pause(); |
| 33 | } |
| 34 | |
| 35 | // Pause |
| 36 | KeyCode::Char('s') => { |
| 37 | app.conn.pause(); |
| 38 | } |
| 39 | |
| 40 | // Clearn Queue |
| 41 | KeyCode::Char('x') => { |
| 42 | app.conn.conn.clear()?; |
| 43 | // app.update_queue(); |
| 44 | } |
| 45 | |
| 46 | KeyCode::Char('d') => { |
| 47 | app.conn.play_dmenu()?; |
| 48 | } |
| 49 | |
| 50 | KeyCode::Down => { |
| 51 | if key_event.modifiers == KeyModifiers::SHIFT { |
| 52 | app.queue_list.next(); |
| 53 | } else { |
| 54 | app.pl_list.next(); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | KeyCode::Up => { |
| 59 | if key_event.modifiers == KeyModifiers::SHIFT { |
| 60 | app.queue_list.prev(); |
| 61 | } else { |
| 62 | app.pl_list.prev(); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | KeyCode::Right => { |
| 67 | app.conn |
| 68 | .push_playlist(app.pl_list.list.get(app.pl_list.index).unwrap())?; |
| 69 | } |
| 70 | |
| 71 | KeyCode::Char('f') => { |
| 72 | // let place = app.conn.conn.status().unwrap().duration; |
| 73 | let (pos, _) = app.conn.conn.status().unwrap().time.unwrap(); |
| 74 | let pos: i64 = (pos.as_secs() + 2).try_into().unwrap(); |
| 75 | app.conn.conn.seek(2, pos)?; |
| 76 | } |
| 77 | |
| 78 | KeyCode::Char('b') => { |
| 79 | // let place = app.conn.conn.status().unwrap().duration; |
| 80 | let (pos, _) = app.conn.conn.status().unwrap().time.unwrap(); |
| 81 | let pos: i64 = (pos.as_secs() - 2).try_into().unwrap(); |
| 82 | app.conn.conn.seek(2, pos)?; |
| 83 | } |
| 84 | _ => {} |
| 85 | } |
| 86 | |
| 87 | Ok(()) |
| 88 | } |