| 1 | use crate::{ |
| 2 | app::{App, AppResult, SelectedTab}, |
| 3 | ui::InputMode, |
| 4 | }; |
| 5 | use crossterm::event::{KeyCode, KeyEvent, KeyModifiers}; |
| 6 | use std::time::Duration; |
| 7 | |
| 8 | use super::{pl_append_keys, pl_rename_keys, search_keys}; |
| 9 | |
| 10 | pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> { |
| 11 | // Live search update |
| 12 | if app.inputmode == InputMode::Editing { |
| 13 | search_keys::handle_search_keys(key_event, app)?; |
| 14 | } else if app.inputmode == InputMode::PlaylistRename { |
| 15 | pl_rename_keys::handle_pl_rename_keys(key_event, app)?; |
| 16 | } else if app.playlist_popup { |
| 17 | pl_append_keys::hande_pl_append_keys(key_event, app)?; |
| 18 | } else { |
| 19 | // General KeyMaps |
| 20 | match key_event.code { |
| 21 | // Quit |
| 22 | KeyCode::Char('q') => app.quit(), |
| 23 | KeyCode::Char('c') | KeyCode::Char('C') => { |
| 24 | if key_event.modifiers == KeyModifiers::CONTROL { |
| 25 | app.quit(); |
| 26 | } else { |
| 27 | app.conn.conn.clear()?; |
| 28 | app.conn.update_status(); |
| 29 | app.queue_list.list.clear(); |
| 30 | app.queue_list.reset_index(); |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | // Go Up |
| 35 | KeyCode::Char('j') | KeyCode::Down => match app.selected_tab { |
| 36 | SelectedTab::DirectoryBrowser => app.browser.next(), |
| 37 | SelectedTab::Queue => app.queue_list.next(), |
| 38 | SelectedTab::Playlists => app.pl_list.next(), |
| 39 | }, |
| 40 | |
| 41 | // Go down |
| 42 | KeyCode::Char('k') | KeyCode::Up => match app.selected_tab { |
| 43 | SelectedTab::DirectoryBrowser => app.browser.prev(), |
| 44 | SelectedTab::Queue => app.queue_list.prev(), |
| 45 | SelectedTab::Playlists => app.pl_list.prev(), |
| 46 | }, |
| 47 | |
| 48 | // Next directory |
| 49 | KeyCode::Enter | KeyCode::Char('l') | KeyCode::Right => { |
| 50 | // app.update_queue(); |
| 51 | |
| 52 | match app.selected_tab { |
| 53 | SelectedTab::DirectoryBrowser => { |
| 54 | app.handle_enter()?; |
| 55 | } |
| 56 | |
| 57 | SelectedTab::Queue => { |
| 58 | app.conn.conn.switch(app.queue_list.index as u32)?; |
| 59 | } |
| 60 | |
| 61 | SelectedTab::Playlists => { |
| 62 | app.conn |
| 63 | .load_playlist(app.pl_list.list.get(app.pl_list.index).unwrap())?; |
| 64 | } |
| 65 | } |
| 66 | app.conn.update_status(); |
| 67 | } |
| 68 | |
| 69 | // head back to previous directory |
| 70 | KeyCode::Char('h') | KeyCode::Left => match app.selected_tab { |
| 71 | SelectedTab::DirectoryBrowser => { |
| 72 | app.browser.handle_go_back(&mut app.conn)?; |
| 73 | } |
| 74 | SelectedTab::Queue => {} |
| 75 | SelectedTab::Playlists => {} |
| 76 | }, |
| 77 | |
| 78 | // Playback controls |
| 79 | // Toggle Pause |
| 80 | KeyCode::Char('p') => app.conn.toggle_pause(), |
| 81 | |
| 82 | // Pause |
| 83 | KeyCode::Char('s') => app.conn.pause(), |
| 84 | |
| 85 | // Toggle rpeat |
| 86 | KeyCode::Char('r') => { |
| 87 | app.conn.toggle_repeat(); |
| 88 | app.conn.update_status(); |
| 89 | } |
| 90 | |
| 91 | // Toggle random |
| 92 | KeyCode::Char('z') => { |
| 93 | app.conn.toggle_random(); |
| 94 | app.conn.update_status(); |
| 95 | } |
| 96 | |
| 97 | // Dmenu prompt |
| 98 | KeyCode::Char('D') => app.conn.play_dmenu()?, |
| 99 | |
| 100 | // add to queue |
| 101 | KeyCode::Char('a') => app.playlist_popup = true, |
| 102 | |
| 103 | // Fast forward |
| 104 | KeyCode::Char('f') => { |
| 105 | let place = app.conn.conn.status().unwrap().song.unwrap().pos; |
| 106 | let (pos, _) = app.conn.conn.status().unwrap().time.unwrap(); |
| 107 | let pos = Duration::from_secs(pos.as_secs().wrapping_add(2)); |
| 108 | app.conn.conn.seek(place, pos)?; |
| 109 | } |
| 110 | |
| 111 | // backward |
| 112 | KeyCode::Char('b') => { |
| 113 | let place = app.conn.conn.status().unwrap().song.unwrap().pos; |
| 114 | let (pos, _) = app.conn.conn.status().unwrap().time.unwrap(); |
| 115 | let pos = Duration::from_secs(pos.as_secs().wrapping_add(2)); |
| 116 | app.conn.conn.seek(place, pos)?; |
| 117 | } |
| 118 | |
| 119 | // Cycle through tabs |
| 120 | KeyCode::Tab => { |
| 121 | app.cycle_tabls(); |
| 122 | } |
| 123 | |
| 124 | // Directory browser tab |
| 125 | KeyCode::Char('1') => { |
| 126 | app.selected_tab = SelectedTab::Queue; |
| 127 | } |
| 128 | |
| 129 | // Playing queue tab |
| 130 | KeyCode::Char('2') => { |
| 131 | app.selected_tab = SelectedTab::DirectoryBrowser; |
| 132 | } |
| 133 | |
| 134 | // Playlists tab |
| 135 | KeyCode::Char('3') => { |
| 136 | app.selected_tab = SelectedTab::Playlists; |
| 137 | } |
| 138 | |
| 139 | // Play next song |
| 140 | KeyCode::Char('>') => { |
| 141 | if !app.queue_list.list.is_empty() { |
| 142 | app.conn.conn.next()?; |
| 143 | app.update_queue(); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | // Play previous song |
| 148 | KeyCode::Char('<') => { |
| 149 | if !app.queue_list.list.is_empty() { |
| 150 | app.conn.conn.prev()?; |
| 151 | app.update_queue(); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | // Volume controls |
| 156 | KeyCode::Char('=') => { |
| 157 | app.conn.inc_volume(2); |
| 158 | app.conn.update_status(); |
| 159 | } |
| 160 | |
| 161 | KeyCode::Char('-') => { |
| 162 | app.conn.dec_volume(2); |
| 163 | app.conn.update_status(); |
| 164 | } |
| 165 | |
| 166 | // Delete highlighted song from the queue |
| 167 | KeyCode::Char('d') => { |
| 168 | if app.queue_list.index >= app.queue_list.list.len() - 1 |
| 169 | && app.queue_list.index != 0 |
| 170 | { |
| 171 | app.queue_list.index -= 1; |
| 172 | } |
| 173 | |
| 174 | app.conn.conn.delete(app.queue_list.index as u32)?; |
| 175 | app.update_queue(); |
| 176 | } |
| 177 | |
| 178 | // Update MPD database |
| 179 | KeyCode::Char('U') => { |
| 180 | app.conn.conn.rescan()?; |
| 181 | app.browser.update_directory(&mut app.conn)?; |
| 182 | } |
| 183 | |
| 184 | // Search for songs |
| 185 | KeyCode::Char('/') => { |
| 186 | if app.inputmode == InputMode::Normal { |
| 187 | app.inputmode = InputMode::Editing; |
| 188 | } else { |
| 189 | app.inputmode = InputMode::Normal; |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | // Remove from Current Playlsit |
| 194 | KeyCode::Char(' ') | KeyCode::Backspace => { |
| 195 | app.handle_add_or_remove_from_current_playlist()?; |
| 196 | } |
| 197 | |
| 198 | // go to top of list |
| 199 | KeyCode::Char('g') => match app.selected_tab { |
| 200 | SelectedTab::Queue => app.queue_list.index = 0, |
| 201 | SelectedTab::DirectoryBrowser => app.browser.selected = 0, |
| 202 | SelectedTab::Playlists => app.pl_list.index = 0, |
| 203 | }, |
| 204 | |
| 205 | // go to bottom of list |
| 206 | KeyCode::Char('G') => match app.selected_tab { |
| 207 | SelectedTab::Queue => app.queue_list.index = app.queue_list.list.len() - 1, |
| 208 | SelectedTab::DirectoryBrowser => { |
| 209 | app.browser.selected = app.browser.filetree.len() - 1 |
| 210 | } |
| 211 | SelectedTab::Playlists => app.pl_list.index = app.pl_list.list.len() - 1, |
| 212 | }, |
| 213 | |
| 214 | // Change playlist name |
| 215 | KeyCode::Char('e') => app.change_playlist_name()?, |
| 216 | _ => {} |
| 217 | } |
| 218 | } |
| 219 | Ok(()) |
| 220 | } |