Krolyxon
Commit bb5bcea

rmptuisrceventhandler.rs

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