| 1 | use std::time::Duration; |
| 2 | |
| 3 | use crate::app::{App, AppResult, SelectedTab}; |
| 4 | use crossterm::event::{KeyCode, KeyEvent, KeyModifiers}; |
| 5 | use ratatui::style::Modifier; |
| 6 | |
| 7 | pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> { |
| 8 | match key_event.code { |
| 9 | KeyCode::Char('q') | KeyCode::Esc => app.quit(), |
| 10 | KeyCode::Char('c') | KeyCode::Char('C') => { |
| 11 | if key_event.modifiers == KeyModifiers::CONTROL { |
| 12 | app.quit(); |
| 13 | } |
| 14 | } |
| 15 | |
| 16 | KeyCode::Char('j') | KeyCode::Down => match app.selected_tab { |
| 17 | SelectedTab::SongList => app.song_list.next(), |
| 18 | SelectedTab::Queue => app.queue_list.next(), |
| 19 | SelectedTab::Playlists => app.pl_list.next(), |
| 20 | }, |
| 21 | |
| 22 | KeyCode::Char('k') | KeyCode::Up => match app.selected_tab { |
| 23 | SelectedTab::SongList => app.song_list.prev(), |
| 24 | SelectedTab::Queue => app.queue_list.prev(), |
| 25 | SelectedTab::Playlists => app.pl_list.prev(), |
| 26 | }, |
| 27 | |
| 28 | KeyCode::Enter | KeyCode::Char('l') => { |
| 29 | // app.update_queue(); |
| 30 | |
| 31 | match app.selected_tab { |
| 32 | SelectedTab::SongList => { |
| 33 | let song = app.conn.get_song_with_only_filename( |
| 34 | app.conn.songs_filenames.get(app.song_list.index).unwrap(), |
| 35 | ); |
| 36 | app.conn.push(&song)?; |
| 37 | } |
| 38 | SelectedTab::Queue => {} |
| 39 | SelectedTab::Playlists => { |
| 40 | app.conn |
| 41 | .push_playlist(app.pl_list.list.get(app.pl_list.index).unwrap())?; |
| 42 | } |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | // Playback controls |
| 47 | // Toggle Pause |
| 48 | KeyCode::Char('p') => { |
| 49 | app.conn.toggle_pause(); |
| 50 | app.conn.update_state(); |
| 51 | } |
| 52 | |
| 53 | // Pause |
| 54 | KeyCode::Char('s') => { |
| 55 | app.conn.pause(); |
| 56 | app.conn.update_state(); |
| 57 | } |
| 58 | |
| 59 | // Clear Queue |
| 60 | KeyCode::Char('x') => { |
| 61 | app.conn.conn.clear()?; |
| 62 | app.conn.update_state(); |
| 63 | // app.update_queue(); |
| 64 | } |
| 65 | |
| 66 | KeyCode::Char('d') => { |
| 67 | app.conn.play_dmenu()?; |
| 68 | } |
| 69 | |
| 70 | KeyCode::Right => { |
| 71 | app.conn |
| 72 | .push_playlist(app.pl_list.list.get(app.pl_list.index).unwrap())?; |
| 73 | } |
| 74 | |
| 75 | KeyCode::Char('f') => { |
| 76 | let place = app.conn.conn.status().unwrap().song.unwrap().pos; |
| 77 | let (pos, _) = app.conn.conn.status().unwrap().time.unwrap(); |
| 78 | let pos = Duration::from_secs(pos.as_secs().wrapping_add(2)); |
| 79 | app.conn.conn.seek(place, pos)?; |
| 80 | } |
| 81 | |
| 82 | KeyCode::Char('b') => { |
| 83 | let place = app.conn.conn.status().unwrap().song.unwrap().pos; |
| 84 | let (pos, _) = app.conn.conn.status().unwrap().time.unwrap(); |
| 85 | let pos = Duration::from_secs(pos.as_secs().wrapping_add(2)); |
| 86 | app.conn.conn.seek(place, pos)?; |
| 87 | } |
| 88 | |
| 89 | KeyCode::Tab => { |
| 90 | app.cycle_tabls(); |
| 91 | } |
| 92 | |
| 93 | KeyCode::Char('1') => { |
| 94 | app.selected_tab = SelectedTab::SongList; |
| 95 | } |
| 96 | |
| 97 | KeyCode::Char('2') => { |
| 98 | app.selected_tab = SelectedTab::Queue; |
| 99 | } |
| 100 | |
| 101 | KeyCode::Char('3') => { |
| 102 | app.selected_tab = SelectedTab::Playlists; |
| 103 | } |
| 104 | |
| 105 | |
| 106 | KeyCode::Char('n') => { |
| 107 | app.conn.conn.next()?; |
| 108 | } |
| 109 | |
| 110 | KeyCode::Char('N') => { |
| 111 | app.conn.conn.prev()?; |
| 112 | } |
| 113 | |
| 114 | // Volume controls |
| 115 | KeyCode::Char('=') => { |
| 116 | app.conn.inc_volume(2); |
| 117 | } |
| 118 | |
| 119 | KeyCode::Char('-') => { |
| 120 | app.conn.dec_volume(2); |
| 121 | } |
| 122 | |
| 123 | |
| 124 | |
| 125 | _ => {} |
| 126 | } |
| 127 | |
| 128 | Ok(()) |
| 129 | } |