| 1 | use crate::{ |
| 2 | app::{App, AppResult, SelectedTab}, |
| 3 | connection::VolumeStatus, |
| 4 | ui::InputMode, |
| 5 | }; |
| 6 | use crossterm::event::{KeyCode, KeyEvent, KeyModifiers, MouseEvent, MouseEventKind}; |
| 7 | use std::time::Duration; |
| 8 | |
| 9 | use super::{new_pl_keys, pl_append_keys, pl_rename_keys, search_keys}; |
| 10 | |
| 11 | pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> { |
| 12 | // searching, playlist renaming, playlist appending |
| 13 | if app.inputmode == InputMode::Editing { |
| 14 | search_keys::handle_search_keys(key_event, app)?; |
| 15 | } else if app.inputmode == InputMode::PlaylistRename { |
| 16 | pl_rename_keys::handle_pl_rename_keys(key_event, app)?; |
| 17 | } else if app.inputmode == InputMode::NewPlaylist { |
| 18 | new_pl_keys::handle_new_pl_keys(key_event, app)?; |
| 19 | } else if app.playlist_popup { |
| 20 | pl_append_keys::hande_pl_append_keys(key_event, app)?; |
| 21 | } else { |
| 22 | // General KeyMaps |
| 23 | match key_event.code { |
| 24 | // Quit |
| 25 | KeyCode::Char('q') => app.quit(), |
| 26 | KeyCode::Char('c') | KeyCode::Char('C') => { |
| 27 | if key_event.modifiers == KeyModifiers::CONTROL { |
| 28 | app.quit(); |
| 29 | } else { |
| 30 | app.conn.conn.clear()?; |
| 31 | app.conn.update_status(); |
| 32 | app.queue_list.list.clear(); |
| 33 | app.queue_list.reset_index(); |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | // Playback controls |
| 38 | // Toggle Pause |
| 39 | KeyCode::Char('p') => { |
| 40 | app.conn.toggle_pause(); |
| 41 | app.conn.update_status(); |
| 42 | } |
| 43 | |
| 44 | // Pause |
| 45 | KeyCode::Char('s') => { |
| 46 | app.conn.pause(); |
| 47 | app.conn.update_status(); |
| 48 | } |
| 49 | |
| 50 | // Toggle rpeat |
| 51 | KeyCode::Char('r') => { |
| 52 | app.conn.toggle_repeat(); |
| 53 | app.conn.update_status(); |
| 54 | } |
| 55 | |
| 56 | // Toggle random |
| 57 | KeyCode::Char('z') => { |
| 58 | app.conn.toggle_random(); |
| 59 | app.conn.update_status(); |
| 60 | } |
| 61 | |
| 62 | // Dmenu prompt |
| 63 | KeyCode::Char('D') => { |
| 64 | app.conn.play_dmenu()?; |
| 65 | app.conn.update_status(); |
| 66 | } |
| 67 | |
| 68 | // add to queue |
| 69 | KeyCode::Char('a') => app.playlist_popup = true, |
| 70 | |
| 71 | // Fast forward |
| 72 | KeyCode::Char('f') => { |
| 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_add(2)); |
| 78 | app.conn.conn.seek(place, pos)?; |
| 79 | app.conn.update_status(); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | // backward |
| 84 | KeyCode::Char('b') => { |
| 85 | if !app.queue_list.list.is_empty() { |
| 86 | let status = app.conn.conn.status().unwrap_or_default(); |
| 87 | let place = status.song.unwrap_or_default().pos; |
| 88 | let (pos, _) = status.time.unwrap_or_default(); |
| 89 | let pos = Duration::from_secs(pos.as_secs().wrapping_sub(2)); |
| 90 | app.conn.conn.seek(place, pos)?; |
| 91 | app.conn.update_status(); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | // Cycle through tabs |
| 96 | KeyCode::Tab => { |
| 97 | app.cycle_tabls(); |
| 98 | } |
| 99 | |
| 100 | // Directory browser tab |
| 101 | KeyCode::Char('1') => { |
| 102 | app.selected_tab = SelectedTab::Queue; |
| 103 | } |
| 104 | |
| 105 | // Playing queue tab |
| 106 | KeyCode::Char('2') => { |
| 107 | app.selected_tab = SelectedTab::DirectoryBrowser; |
| 108 | } |
| 109 | |
| 110 | // Playlists tab |
| 111 | KeyCode::Char('3') => { |
| 112 | app.selected_tab = SelectedTab::Playlists; |
| 113 | } |
| 114 | |
| 115 | // Play next song |
| 116 | KeyCode::Char('>') => { |
| 117 | if !app.queue_list.list.is_empty() { |
| 118 | app.conn.conn.next()?; |
| 119 | app.update_queue(); |
| 120 | app.conn.update_status(); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | // Play previous song |
| 125 | KeyCode::Char('<') => { |
| 126 | if !app.queue_list.list.is_empty() { |
| 127 | app.conn.conn.prev()?; |
| 128 | app.update_queue(); |
| 129 | app.conn.update_status(); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | // Volume controls |
| 134 | KeyCode::Char('=') | KeyCode::Char('+') => { |
| 135 | app.conn.inc_volume(2); |
| 136 | app.conn.update_status(); |
| 137 | } |
| 138 | |
| 139 | KeyCode::Char('-') => { |
| 140 | app.conn.dec_volume(2); |
| 141 | app.conn.update_status(); |
| 142 | } |
| 143 | |
| 144 | // Toggle Mute |
| 145 | KeyCode::Char('m') => { |
| 146 | match app.conn.volume_status { |
| 147 | VolumeStatus::Muted(v) => { |
| 148 | app.conn.conn.volume(v)?; |
| 149 | app.conn.volume_status = VolumeStatus::Unmuted; |
| 150 | } |
| 151 | VolumeStatus::Unmuted => { |
| 152 | let current_volume = app.conn.status.volume; |
| 153 | app.conn.conn.volume(0)?; |
| 154 | app.conn.volume_status = VolumeStatus::Muted(current_volume); |
| 155 | } |
| 156 | } |
| 157 | app.conn.update_status(); |
| 158 | } |
| 159 | |
| 160 | // Update MPD database |
| 161 | KeyCode::Char('U') => { |
| 162 | app.conn.conn.rescan()?; |
| 163 | app.should_update_song_list = true; |
| 164 | } |
| 165 | |
| 166 | // Search for songs |
| 167 | KeyCode::Char('/') => { |
| 168 | if app.inputmode == InputMode::Normal { |
| 169 | app.inputmode = InputMode::Editing; |
| 170 | } else { |
| 171 | app.inputmode = InputMode::Normal; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | // Add or Remove from Current Playlist |
| 176 | KeyCode::Char(' ') => { |
| 177 | app.handle_add_or_remove_from_current_playlist()?; |
| 178 | } |
| 179 | _ => {} |
| 180 | } |
| 181 | |
| 182 | // Tab specific keymaps |
| 183 | match app.selected_tab { |
| 184 | SelectedTab::Queue => { |
| 185 | match key_event.code { |
| 186 | // Go Up |
| 187 | KeyCode::Char('j') | KeyCode::Down => app.queue_list.next(), |
| 188 | |
| 189 | // Go down |
| 190 | KeyCode::Char('k') | KeyCode::Up => app.queue_list.prev(), |
| 191 | |
| 192 | // Next directory |
| 193 | KeyCode::Enter | KeyCode::Char('l') | KeyCode::Right => { |
| 194 | app.conn.conn.switch(app.queue_list.index as u32)?; |
| 195 | app.conn.update_status(); |
| 196 | } |
| 197 | |
| 198 | // Delete highlighted song from the queue |
| 199 | KeyCode::Char('d') => { |
| 200 | if app.queue_list.index >= app.queue_list.list.len() |
| 201 | && app.queue_list.index != 0 |
| 202 | { |
| 203 | app.queue_list.index -= 1; |
| 204 | } |
| 205 | |
| 206 | app.conn.conn.delete(app.queue_list.index as u32)?; |
| 207 | |
| 208 | if app.queue_list.index >= app.queue_list.list.len().saturating_sub(1) |
| 209 | && app.queue_list.index != 0 |
| 210 | { |
| 211 | app.queue_list.index -= 1; |
| 212 | } |
| 213 | |
| 214 | app.conn.update_status(); |
| 215 | app.update_queue(); |
| 216 | } |
| 217 | |
| 218 | // Swap highlighted song with next one |
| 219 | KeyCode::Char('J') => { |
| 220 | let current: u32 = app.queue_list.index as u32; |
| 221 | let next: u32 = if (current + 1) as usize == app.queue_list.list.len() { |
| 222 | app.queue_list.index as u32 |
| 223 | } else { |
| 224 | app.queue_list.index += 1; |
| 225 | current + 1 |
| 226 | }; |
| 227 | app.conn.conn.swap(current, next)?; |
| 228 | app.update_queue(); |
| 229 | app.conn.update_status(); |
| 230 | } |
| 231 | |
| 232 | // Swap highlighted song with previous one |
| 233 | KeyCode::Char('K') => { |
| 234 | let current: u32 = app.queue_list.index as u32; |
| 235 | let prev: u32 = if current == 0 { |
| 236 | app.queue_list.index as u32 |
| 237 | } else { |
| 238 | app.queue_list.index -= 1; |
| 239 | current - 1 |
| 240 | }; |
| 241 | app.conn.conn.swap(current, prev)?; |
| 242 | app.update_queue(); |
| 243 | app.conn.update_status(); |
| 244 | } |
| 245 | |
| 246 | // go to top of list |
| 247 | KeyCode::Char('g') => app.queue_list.index = 0, |
| 248 | |
| 249 | // go to bottom of list |
| 250 | KeyCode::Char('G') => app.queue_list.index = app.queue_list.list.len() - 1, |
| 251 | |
| 252 | _ => {} |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | SelectedTab::DirectoryBrowser => { |
| 257 | match key_event.code { |
| 258 | // Go Up |
| 259 | KeyCode::Char('j') | KeyCode::Down => app.browser.next(), |
| 260 | |
| 261 | // Go down |
| 262 | KeyCode::Char('k') | KeyCode::Up => app.browser.prev(), |
| 263 | |
| 264 | // Next directory |
| 265 | KeyCode::Enter | KeyCode::Char('l') | KeyCode::Right => { |
| 266 | // app.update_queue(); |
| 267 | app.handle_enter()?; |
| 268 | app.conn.update_status(); |
| 269 | } |
| 270 | |
| 271 | // head back to previous directory |
| 272 | KeyCode::Char('h') | KeyCode::Left => { |
| 273 | app.browser.handle_go_back(&mut app.conn)? |
| 274 | } |
| 275 | |
| 276 | // go to top of list |
| 277 | KeyCode::Char('g') => app.browser.selected = 0, |
| 278 | |
| 279 | // go to bottom of list |
| 280 | KeyCode::Char('G') => app.browser.selected = app.browser.filetree.len() - 1, |
| 281 | |
| 282 | _ => {} |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | SelectedTab::Playlists => { |
| 287 | match key_event.code { |
| 288 | // Go Up |
| 289 | KeyCode::Char('j') | KeyCode::Down => app.pl_list.next(), |
| 290 | |
| 291 | // Go down |
| 292 | KeyCode::Char('k') | KeyCode::Up => app.pl_list.prev(), |
| 293 | |
| 294 | // go to top of list |
| 295 | KeyCode::Char('g') => app.pl_list.index = 0, |
| 296 | |
| 297 | // go to bottom of list |
| 298 | KeyCode::Char('G') => app.pl_list.index = app.pl_list.list.len() - 1, |
| 299 | |
| 300 | // Playlist Rename |
| 301 | KeyCode::Char('R') => { |
| 302 | app.inputmode = InputMode::PlaylistRename; |
| 303 | } |
| 304 | |
| 305 | // add to current playlist |
| 306 | KeyCode::Enter | KeyCode::Char('l') | KeyCode::Right | KeyCode::Char(' ') => { |
| 307 | // app.update_queue(); |
| 308 | if !app.pl_list.list.is_empty() { |
| 309 | app.conn |
| 310 | .load_playlist(app.pl_list.list.get(app.pl_list.index).unwrap())?; |
| 311 | app.conn.update_status(); |
| 312 | } |
| 313 | } |
| 314 | _ => {} |
| 315 | } |
| 316 | } |
| 317 | } |
| 318 | } |
| 319 | Ok(()) |
| 320 | } |
| 321 | |
| 322 | pub fn handle_mouse_events(mouse_event: MouseEvent, app: &mut App) -> AppResult<()> { |
| 323 | match mouse_event.kind { |
| 324 | MouseEventKind::ScrollUp => app.handle_scroll_up(), |
| 325 | MouseEventKind::ScrollDown => app.handle_scroll_down(), |
| 326 | MouseEventKind::Down(button) => { |
| 327 | let (x, y) = (mouse_event.column, mouse_event.row); |
| 328 | if button == crossterm::event::MouseButton::Left { |
| 329 | app.handle_mouse_left_click(x, y)?; |
| 330 | } |
| 331 | } |
| 332 | _ => {} |
| 333 | } |
| 334 | Ok(()) |
| 335 | } |