| 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 | // searching, playlist renaming, playlist appending |
| 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 | // Playback controls |
| 35 | // Toggle Pause |
| 36 | KeyCode::Char('p') => app.conn.toggle_pause(), |
| 37 | |
| 38 | // Pause |
| 39 | KeyCode::Char('s') => app.conn.pause(), |
| 40 | |
| 41 | // Toggle rpeat |
| 42 | KeyCode::Char('r') => { |
| 43 | app.conn.toggle_repeat(); |
| 44 | app.conn.update_status(); |
| 45 | } |
| 46 | |
| 47 | // Toggle random |
| 48 | KeyCode::Char('z') => { |
| 49 | app.conn.toggle_random(); |
| 50 | app.conn.update_status(); |
| 51 | } |
| 52 | |
| 53 | // Dmenu prompt |
| 54 | KeyCode::Char('D') => app.conn.play_dmenu()?, |
| 55 | |
| 56 | // add to queue |
| 57 | KeyCode::Char('a') => app.playlist_popup = true, |
| 58 | |
| 59 | // Fast forward |
| 60 | KeyCode::Char('f') => { |
| 61 | if !app.queue_list.list.is_empty() { |
| 62 | let status = app.conn.conn.status().unwrap_or_default(); |
| 63 | let place = status.song.unwrap_or_default().pos; |
| 64 | let (pos, _) = status.time.unwrap_or_default(); |
| 65 | let pos = Duration::from_secs(pos.as_secs().wrapping_add(2)); |
| 66 | app.conn.conn.seek(place, pos)?; |
| 67 | app.conn.update_status(); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | // backward |
| 72 | KeyCode::Char('b') => { |
| 73 | if !app.queue_list.list.is_empty() { |
| 74 | let status = app.conn.conn.status().unwrap_or_default(); |
| 75 | let place = status.song.unwrap_or_default().pos; |
| 76 | let (pos, _) = status.time.unwrap_or_default(); |
| 77 | let pos = Duration::from_secs(pos.as_secs().wrapping_sub(2)); |
| 78 | app.conn.conn.seek(place, pos)?; |
| 79 | app.conn.update_status(); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | // Cycle through tabs |
| 84 | KeyCode::Tab => { |
| 85 | app.cycle_tabls(); |
| 86 | } |
| 87 | |
| 88 | // Directory browser tab |
| 89 | KeyCode::Char('1') => { |
| 90 | app.selected_tab = SelectedTab::Queue; |
| 91 | } |
| 92 | |
| 93 | // Playing queue tab |
| 94 | KeyCode::Char('2') => { |
| 95 | app.selected_tab = SelectedTab::DirectoryBrowser; |
| 96 | } |
| 97 | |
| 98 | // Playlists tab |
| 99 | KeyCode::Char('3') => { |
| 100 | app.selected_tab = SelectedTab::Playlists; |
| 101 | } |
| 102 | |
| 103 | // Play next song |
| 104 | KeyCode::Char('>') => { |
| 105 | if !app.queue_list.list.is_empty() { |
| 106 | app.conn.conn.next()?; |
| 107 | app.update_queue(); |
| 108 | app.conn.update_status(); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | // Play previous song |
| 113 | KeyCode::Char('<') => { |
| 114 | if !app.queue_list.list.is_empty() { |
| 115 | app.conn.conn.prev()?; |
| 116 | app.update_queue(); |
| 117 | app.conn.update_status(); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | // Volume controls |
| 122 | KeyCode::Char('=') | KeyCode::Char('+') => { |
| 123 | app.conn.inc_volume(2); |
| 124 | app.conn.update_status(); |
| 125 | } |
| 126 | |
| 127 | KeyCode::Char('-') => { |
| 128 | app.conn.dec_volume(2); |
| 129 | app.conn.update_status(); |
| 130 | } |
| 131 | |
| 132 | // Update MPD database |
| 133 | KeyCode::Char('U') => { |
| 134 | app.conn.conn.rescan()?; |
| 135 | |
| 136 | // Update the songs list |
| 137 | app.conn.songs_filenames = app |
| 138 | .conn |
| 139 | .conn |
| 140 | .listall()? |
| 141 | .into_iter() |
| 142 | .map(|x| x.file) |
| 143 | .collect(); |
| 144 | |
| 145 | app.browser.update_directory(&mut app.conn)?; |
| 146 | } |
| 147 | |
| 148 | // Search for songs |
| 149 | KeyCode::Char('/') => { |
| 150 | if app.inputmode == InputMode::Normal { |
| 151 | app.inputmode = InputMode::Editing; |
| 152 | } else { |
| 153 | app.inputmode = InputMode::Normal; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | // Add or Remove from Current Playlist |
| 158 | KeyCode::Char(' ') => { |
| 159 | app.handle_add_or_remove_from_current_playlist()?; |
| 160 | } |
| 161 | _ => {} |
| 162 | } |
| 163 | |
| 164 | // Tab specific keymaps |
| 165 | match app.selected_tab { |
| 166 | SelectedTab::Queue => { |
| 167 | match key_event.code { |
| 168 | // Go Up |
| 169 | KeyCode::Char('j') | KeyCode::Down => app.queue_list.next(), |
| 170 | |
| 171 | // Go down |
| 172 | KeyCode::Char('k') | KeyCode::Up => app.queue_list.prev(), |
| 173 | |
| 174 | // Next directory |
| 175 | KeyCode::Enter | KeyCode::Char('l') | KeyCode::Right => { |
| 176 | app.conn.conn.switch(app.queue_list.index as u32)?; |
| 177 | app.conn.update_status(); |
| 178 | } |
| 179 | |
| 180 | // Delete highlighted song from the queue |
| 181 | KeyCode::Char('d') => { |
| 182 | if app.queue_list.index >= app.queue_list.list.len() |
| 183 | && app.queue_list.index != 0 |
| 184 | { |
| 185 | app.queue_list.index -= 1; |
| 186 | } |
| 187 | |
| 188 | app.conn.conn.delete(app.queue_list.index as u32)?; |
| 189 | |
| 190 | if app.queue_list.index >= app.queue_list.list.len().saturating_sub(1) |
| 191 | && app.queue_list.index != 0 |
| 192 | { |
| 193 | app.queue_list.index -= 1; |
| 194 | } |
| 195 | |
| 196 | app.conn.update_status(); |
| 197 | app.update_queue(); |
| 198 | } |
| 199 | |
| 200 | // Swap highlighted song with next one |
| 201 | KeyCode::Char('J') => { |
| 202 | let current: u32 = app.queue_list.index as u32; |
| 203 | let next: u32 = if (current + 1) as usize == app.queue_list.list.len() { |
| 204 | app.queue_list.index as u32 |
| 205 | } else { |
| 206 | app.queue_list.index += 1; |
| 207 | current + 1 |
| 208 | }; |
| 209 | app.conn.conn.swap(current, next)?; |
| 210 | app.update_queue(); |
| 211 | app.conn.update_status(); |
| 212 | } |
| 213 | |
| 214 | // Swap highlighted song with previous one |
| 215 | KeyCode::Char('K') => { |
| 216 | let current: u32 = app.queue_list.index as u32; |
| 217 | let prev: u32 = if current == 0 { |
| 218 | app.queue_list.index as u32 |
| 219 | } else { |
| 220 | app.queue_list.index -= 1; |
| 221 | current - 1 |
| 222 | }; |
| 223 | app.conn.conn.swap(current, prev)?; |
| 224 | app.update_queue(); |
| 225 | app.conn.update_status(); |
| 226 | } |
| 227 | |
| 228 | // go to top of list |
| 229 | KeyCode::Char('g') => app.queue_list.index = 0, |
| 230 | |
| 231 | // go to bottom of list |
| 232 | KeyCode::Char('G') => app.queue_list.index = app.queue_list.list.len() - 1, |
| 233 | |
| 234 | _ => {} |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | SelectedTab::DirectoryBrowser => { |
| 239 | match key_event.code { |
| 240 | // Go Up |
| 241 | KeyCode::Char('j') | KeyCode::Down => app.browser.next(), |
| 242 | |
| 243 | // Go down |
| 244 | KeyCode::Char('k') | KeyCode::Up => app.browser.prev(), |
| 245 | |
| 246 | // Next directory |
| 247 | KeyCode::Enter | KeyCode::Char('l') | KeyCode::Right => { |
| 248 | // app.update_queue(); |
| 249 | app.handle_enter()?; |
| 250 | app.conn.update_status(); |
| 251 | } |
| 252 | |
| 253 | // head back to previous directory |
| 254 | KeyCode::Char('h') | KeyCode::Left => { |
| 255 | app.browser.handle_go_back(&mut app.conn)? |
| 256 | } |
| 257 | |
| 258 | // go to top of list |
| 259 | KeyCode::Char('g') => app.browser.selected = 0, |
| 260 | |
| 261 | // go to bottom of list |
| 262 | KeyCode::Char('G') => app.browser.selected = app.browser.filetree.len() - 1, |
| 263 | |
| 264 | _ => {} |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | SelectedTab::Playlists => { |
| 269 | match key_event.code { |
| 270 | // Go Up |
| 271 | KeyCode::Char('j') | KeyCode::Down => app.pl_list.next(), |
| 272 | |
| 273 | // Go down |
| 274 | KeyCode::Char('k') | KeyCode::Up => app.pl_list.prev(), |
| 275 | |
| 276 | // go to top of list |
| 277 | KeyCode::Char('g') => app.pl_list.index = 0, |
| 278 | |
| 279 | // go to bottom of list |
| 280 | KeyCode::Char('G') => app.pl_list.index = app.pl_list.list.len() - 1, |
| 281 | |
| 282 | // Change playlist name |
| 283 | KeyCode::Char('e') => app.change_playlist_name()?, |
| 284 | |
| 285 | // add to current playlist |
| 286 | KeyCode::Enter | KeyCode::Char('l') | KeyCode::Right | KeyCode::Char(' ') => { |
| 287 | // app.update_queue(); |
| 288 | if !app.pl_list.list.is_empty() { |
| 289 | app.conn |
| 290 | .load_playlist(app.pl_list.list.get(app.pl_list.index).unwrap())?; |
| 291 | app.conn.update_status(); |
| 292 | } |
| 293 | } |
| 294 | _ => {} |
| 295 | } |
| 296 | } |
| 297 | } |
| 298 | } |
| 299 | Ok(()) |
| 300 | } |