Krolyxon
Commit 9daeca9

rmptuisrceventhandler.rs

309 lines · 10.7 KB · Rust

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