Krolyxon
Commit be4505e

rmptuisrceventhandler.rs

301 lines · 10.5 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') => 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('=') => {
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 .unwrap()
142 .into_iter()
143 .map(|x| x.file)
144 .collect();
145
146 app.browser.update_directory(&mut app.conn)?;
147 }
148
149 // Search for songs
150 KeyCode::Char('/') => {
151 if app.inputmode == InputMode::Normal {
152 app.inputmode = InputMode::Editing;
153 } else {
154 app.inputmode = InputMode::Normal;
155 }
156 }
157
158 // Add or Remove from Current Playlist
159 KeyCode::Char(' ') | KeyCode::Backspace => {
160 app.handle_add_or_remove_from_current_playlist()?;
161 }
162 _ => {}
163 }
164
165 // Tab specific keymaps
166 match app.selected_tab {
167 SelectedTab::Queue => {
168 match key_event.code {
169 // Go Up
170 KeyCode::Char('j') | KeyCode::Down => app.queue_list.next(),
171
172 // Go down
173 KeyCode::Char('k') | KeyCode::Up => app.queue_list.prev(),
174
175 // Next directory
176 KeyCode::Enter | KeyCode::Char('l') | KeyCode::Right => {
177 app.conn.conn.switch(app.queue_list.index as u32)?;
178 app.conn.update_status();
179 }
180
181 // Delete highlighted song from the queue
182 KeyCode::Char('d') => {
183 if app.queue_list.index >= app.queue_list.list.len()
184 && app.queue_list.index != 0
185 {
186 app.queue_list.index -= 1;
187 }
188
189 app.conn.conn.delete(app.queue_list.index as u32)?;
190
191 if app.queue_list.index >= app.queue_list.list.len().saturating_sub(1)
192 && app.queue_list.index != 0
193 {
194 app.queue_list.index -= 1;
195 }
196
197 app.conn.update_status();
198 app.update_queue();
199 }
200
201 // Swap highlighted song with next one
202 KeyCode::Char('J') => {
203 let current: u32 = app.queue_list.index as u32;
204 let next: u32 = if (current + 1) as usize == app.queue_list.list.len() {
205 app.queue_list.index as u32
206 } else {
207 app.queue_list.index += 1;
208 current + 1
209 };
210 app.conn.conn.swap(current, next)?;
211 app.update_queue();
212 app.conn.update_status();
213 }
214
215 // Swap highlighted song with previous one
216 KeyCode::Char('K') => {
217 let current: u32 = app.queue_list.index as u32;
218 let prev: u32 = if current == 0 {
219 app.queue_list.index as u32
220 } else {
221 app.queue_list.index -= 1;
222 current - 1
223 };
224 app.conn.conn.swap(current, prev)?;
225 app.update_queue();
226 app.conn.update_status();
227 }
228
229 // go to top of list
230 KeyCode::Char('g') => app.queue_list.index = 0,
231
232 // go to bottom of list
233 KeyCode::Char('G') => app.queue_list.index = app.queue_list.list.len() - 1,
234
235 _ => {}
236 }
237 }
238
239 SelectedTab::DirectoryBrowser => {
240 match key_event.code {
241 // Go Up
242 KeyCode::Char('j') | KeyCode::Down => app.browser.next(),
243
244 // Go down
245 KeyCode::Char('k') | KeyCode::Up => app.browser.prev(),
246
247 // Next directory
248 KeyCode::Enter | KeyCode::Char('l') | KeyCode::Right => {
249 // app.update_queue();
250 app.handle_enter()?;
251 app.conn.update_status();
252 }
253
254 // head back to previous directory
255 KeyCode::Char('h') | KeyCode::Left => {
256 app.browser.handle_go_back(&mut app.conn)?
257 }
258
259 // go to top of list
260 KeyCode::Char('g') => app.browser.selected = 0,
261
262 // go to bottom of list
263 KeyCode::Char('G') => app.browser.selected = app.browser.filetree.len() - 1,
264
265 _ => {}
266 }
267 }
268
269 SelectedTab::Playlists => {
270 match key_event.code {
271 // Go Up
272 KeyCode::Char('j') | KeyCode::Down => app.pl_list.next(),
273
274 // Go down
275 KeyCode::Char('k') | KeyCode::Up => app.pl_list.prev(),
276
277 // go to top of list
278 KeyCode::Char('g') => app.pl_list.index = 0,
279
280 // go to bottom of list
281 KeyCode::Char('G') => app.pl_list.index = app.pl_list.list.len() - 1,
282
283 // Change playlist name
284 KeyCode::Char('e') => app.change_playlist_name()?,
285
286 // add to current playlist
287 KeyCode::Enter | KeyCode::Char('l') | KeyCode::Right | KeyCode::Char(' ') => {
288 // app.update_queue();
289 if !app.pl_list.list.is_empty() {
290 app.conn
291 .load_playlist(app.pl_list.list.get(app.pl_list.index).unwrap())?;
292 app.conn.update_status();
293 }
294 }
295 _ => {}
296 }
297 }
298 }
299 }
300 Ok(())
301}